1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <ctk/ctk.h>
#include <cdk/wayland/cdkwayland.h>

static CtkWidget *window;<--- Shadowed variable
static CtkWidget *label;
static CtkWidget *entry;
static CtkWidget *unexport_button;
static char *export_handle;
int export_count;

static void
update_ui (void)
{
  gboolean can_unexport;
  char *label_text;

  ctk_entry_set_text (CTK_ENTRY (entry), export_handle ? export_handle : "");

  label_text = g_strdup_printf ("Export count: %d", export_count);
  ctk_label_set_text (CTK_LABEL (label), label_text);
  g_free (label_text);

  can_unexport = !!export_handle;
  ctk_widget_set_sensitive (unexport_button, can_unexport);
}

static void
exported_callback (CdkWindow  *window G_GNUC_UNUSED,<--- Shadow argument
                   const char *handle,
                   gpointer    user_data G_GNUC_UNUSED)
{
  if (!export_handle)
    export_handle = g_strdup (handle);

  g_assert (g_str_equal (handle, export_handle));

  export_count++;

  update_ui ();
}

static void
export_callback (CtkWidget *widget G_GNUC_UNUSED,
                 gpointer   data G_GNUC_UNUSED)
{
  if (!cdk_wayland_window_export_handle (ctk_widget_get_window (window),
                                         exported_callback,
                                         g_strdup ("user_data"), g_free))
    g_error ("Failed to export window");

  update_ui ();
}

static void
unexport_callback (CtkWidget *widget G_GNUC_UNUSED,
                   gpointer   data G_GNUC_UNUSED)
{
  cdk_wayland_window_unexport_handle (ctk_widget_get_window (window));

  export_count--;
  if (export_count == 0)
    g_clear_pointer (&export_handle, g_free);

  update_ui ();
}

int
main (int   argc,
      char *argv[])
{
  CdkDisplay *display;
  CtkWidget *vbox;
  CtkWidget *hbox;
  CtkWidget *export_button;

  ctk_init (&argc, &argv);


  window = ctk_window_new (CTK_WINDOW_TOPLEVEL);

  display = ctk_widget_get_display (window);
  if (!CDK_IS_WAYLAND_DISPLAY (display))
    g_error ("This test is only works on Wayland");

  vbox = ctk_box_new (CTK_ORIENTATION_VERTICAL, 10);
  hbox = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 10);

  label = ctk_label_new (NULL);
  entry = ctk_entry_new ();
  ctk_editable_set_editable (CTK_EDITABLE (entry), FALSE);

  export_button = ctk_button_new_with_label ("Export");
  unexport_button = ctk_button_new_with_label("Unexport");
  g_signal_connect (export_button, "clicked",
                    G_CALLBACK (export_callback), NULL);
  g_signal_connect (unexport_button, "clicked",
                    G_CALLBACK (unexport_callback), NULL);

  ctk_container_add (CTK_CONTAINER (hbox), export_button);
  ctk_container_add (CTK_CONTAINER (hbox), unexport_button);

  ctk_container_add (CTK_CONTAINER (vbox), entry);
  ctk_container_add (CTK_CONTAINER (vbox), label);
  ctk_container_add (CTK_CONTAINER (vbox), hbox);

  ctk_container_add (CTK_CONTAINER (window), vbox);

  update_ui ();

  g_signal_connect (window, "delete-event", G_CALLBACK (ctk_main_quit), NULL);

  ctk_widget_show_all (window);

  ctk_main ();

  return 0;
}