]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkdevice-wayland.c
wayland: Fix memory handling for the selection callbacks
[~andy/gtk] / gdk / wayland / gdkdevice-wayland.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #define _GNU_SOURCE
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24
25 #include "config.h"
26
27 #include <string.h>
28 #include <gdk/gdkwindow.h>
29 #include <gdk/gdktypes.h>
30 #include "gdkprivate-wayland.h"
31 #include "gdkwayland.h"
32 #include "gdkkeysyms.h"
33 #include "gdkdeviceprivate.h"
34 #include "gdkdevicemanagerprivate.h"
35 #include "gdkprivate-wayland.h"
36
37 #include <X11/extensions/XKBcommon.h>
38 #include <X11/keysym.h>
39
40 #include <sys/time.h>
41
42 #define GDK_TYPE_DEVICE_CORE         (gdk_device_core_get_type ())
43 #define GDK_DEVICE_CORE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE_CORE, GdkDeviceCore))
44 #define GDK_DEVICE_CORE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_DEVICE_CORE, GdkDeviceCoreClass))
45 #define GDK_IS_DEVICE_CORE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE_CORE))
46 #define GDK_IS_DEVICE_CORE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_DEVICE_CORE))
47 #define GDK_DEVICE_CORE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_DEVICE_CORE, GdkDeviceCoreClass))
48
49 typedef struct _GdkDeviceCore GdkDeviceCore;
50 typedef struct _GdkDeviceCoreClass GdkDeviceCoreClass;
51 typedef struct _GdkWaylandDevice GdkWaylandDevice;
52
53 typedef struct _DataOffer DataOffer;
54
55 typedef struct _GdkWaylandSelectionOffer GdkWaylandSelectionOffer;
56
57 struct _GdkWaylandDevice
58 {
59   GdkDisplay *display;
60   GdkDevice *pointer;
61   GdkDevice *keyboard;
62   GdkModifierType modifiers;
63   GdkWindow *pointer_focus;
64   GdkWindow *keyboard_focus;
65   struct wl_input_device *device;
66   struct wl_data_device *data_device;
67   int32_t x, y, surface_x, surface_y;
68   uint32_t time;
69   GdkWindow *pointer_grab_window;
70   uint32_t pointer_grab_time;
71
72   DataOffer *drag_offer;
73   DataOffer *selection_offer;
74
75   GdkWaylandSelectionOffer *selection_offer_out;
76 };
77
78 struct _GdkDeviceCore
79 {
80   GdkDevice parent_instance;
81   GdkWaylandDevice *device;
82 };
83
84 struct _GdkDeviceCoreClass
85 {
86   GdkDeviceClass parent_class;
87 };
88
89 G_DEFINE_TYPE (GdkDeviceCore, gdk_device_core, GDK_TYPE_DEVICE)
90
91 #define GDK_TYPE_DEVICE_MANAGER_CORE         (gdk_device_manager_core_get_type ())
92 #define GDK_DEVICE_MANAGER_CORE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCore))
93 #define GDK_DEVICE_MANAGER_CORE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCoreClass))
94 #define GDK_IS_DEVICE_MANAGER_CORE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE_MANAGER_CORE))
95 #define GDK_IS_DEVICE_MANAGER_CORE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_DEVICE_MANAGER_CORE))
96 #define GDK_DEVICE_MANAGER_CORE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCoreClass))
97
98 typedef struct _GdkDeviceManagerCore GdkDeviceManagerCore;
99 typedef struct _GdkDeviceManagerCoreClass GdkDeviceManagerCoreClass;
100
101 struct _GdkDeviceManagerCore
102 {
103   GdkDeviceManager parent_object;
104   GdkDevice *core_pointer;
105   GdkDevice *core_keyboard;
106   GList *devices;
107 };
108
109 struct _GdkDeviceManagerCoreClass
110 {
111   GdkDeviceManagerClass parent_class;
112 };
113
114 G_DEFINE_TYPE (GdkDeviceManagerCore,
115                gdk_device_manager_core, GDK_TYPE_DEVICE_MANAGER)
116
117 static gboolean
118 gdk_device_core_get_history (GdkDevice      *device,
119                              GdkWindow      *window,
120                              guint32         start,
121                              guint32         stop,
122                              GdkTimeCoord ***events,
123                              gint           *n_events)
124 {
125   return FALSE;
126 }
127
128 static void
129 gdk_device_core_get_state (GdkDevice       *device,
130                            GdkWindow       *window,
131                            gdouble         *axes,
132                            GdkModifierType *mask)
133 {
134   gint x_int, y_int;
135
136   gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
137
138   if (axes)
139     {
140       axes[0] = x_int;
141       axes[1] = y_int;
142     }
143 }
144
145 static void
146 gdk_device_core_set_window_cursor (GdkDevice *device,
147                                    GdkWindow *window,
148                                    GdkCursor *cursor)
149 {
150   GdkWaylandDevice *wd = GDK_DEVICE_CORE(device)->device;
151   struct wl_buffer *buffer;
152   int x, y;
153
154   if (cursor)
155     g_object_ref (cursor);
156
157   /* Setting the cursor to NULL means that we should use the default cursor */
158   if (!cursor)
159     {
160       /* FIXME: Is this the best sensible default ? */
161       cursor = _gdk_wayland_display_get_cursor_for_type (device->display,
162                                                          GDK_LEFT_PTR);
163     }
164
165   buffer = _gdk_wayland_cursor_get_buffer(cursor, &x, &y);
166   wl_input_device_attach(wd->device, wd->time, buffer, x, y);
167
168   g_object_unref (cursor);
169 }
170
171 static void
172 gdk_device_core_warp (GdkDevice *device,
173                       GdkScreen *screen,
174                       gint       x,
175                       gint       y)
176 {
177 }
178
179 static gboolean
180 gdk_device_core_query_state (GdkDevice        *device,
181                              GdkWindow        *window,
182                              GdkWindow       **root_window,
183                              GdkWindow       **child_window,
184                              gint             *root_x,
185                              gint             *root_y,
186                              gint             *win_x,
187                              gint             *win_y,
188                              GdkModifierType  *mask)
189 {
190   GdkWaylandDevice *wd;
191   GdkScreen *default_screen;
192
193   wd = GDK_DEVICE_CORE(device)->device;
194   default_screen = gdk_display_get_default_screen (wd->display);
195
196   if (root_window)
197     *root_window = gdk_screen_get_root_window (default_screen);
198   if (child_window)
199     *child_window = wd->pointer_focus;
200   if (root_x)
201     *root_x = wd->x;
202   if (root_y)
203     *root_y = wd->y;
204   if (win_x)
205     *win_x = wd->surface_x;
206   if (win_y)
207     *win_y = wd->surface_y;
208   if (mask)
209     *mask = wd->modifiers;
210
211   return TRUE;
212 }
213
214 static GdkGrabStatus
215 gdk_device_core_grab (GdkDevice    *device,
216                       GdkWindow    *window,
217                       gboolean      owner_events,
218                       GdkEventMask  event_mask,
219                       GdkWindow    *confine_to,
220                       GdkCursor    *cursor,
221                       guint32       time_)
222 {
223   GdkWaylandDevice *wayland_device = GDK_DEVICE_CORE (device)->device;
224
225   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
226     {
227       /* Device is a keyboard */
228       return GDK_GRAB_SUCCESS;
229     }
230   else
231     {
232       /* Device is a pointer */
233
234       if (wayland_device->pointer_grab_window != NULL &&
235           time_ != 0 && wayland_device->pointer_grab_time > time_)
236         {
237           return GDK_GRAB_ALREADY_GRABBED;
238         }
239
240       if (time_ == 0)
241         time_ = wayland_device->time;
242
243       wayland_device->pointer_grab_window = window;
244       wayland_device->pointer_grab_time = time_;
245     }
246
247   return GDK_GRAB_SUCCESS;
248 }
249
250 static void
251 gdk_device_core_ungrab (GdkDevice *device,
252                         guint32    time_)
253 {
254   GdkDisplay *display;
255   GdkDeviceGrabInfo *grab;
256
257   display = gdk_device_get_display (device);
258
259   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
260     {
261       /* Device is a keyboard */
262     }
263   else
264     {
265       /* Device is a pointer */
266       grab = _gdk_display_get_last_device_grab (display, device);
267
268       if (grab)
269         grab->serial_end = grab->serial_start;
270     }
271 }
272
273 static GdkWindow *
274 gdk_device_core_window_at_position (GdkDevice       *device,
275                                     gint            *win_x,
276                                     gint            *win_y,
277                                     GdkModifierType *mask,
278                                     gboolean         get_toplevel)
279 {
280   GdkWaylandDevice *wd;
281
282   wd = GDK_DEVICE_CORE(device)->device;
283   if (win_x)
284     *win_x = wd->surface_x;
285   if (win_y)
286     *win_y = wd->surface_y;
287   if (mask)
288     *mask = wd->modifiers;
289
290   return wd->pointer_focus;
291 }
292
293 static void
294 gdk_device_core_select_window_events (GdkDevice    *device,
295                                       GdkWindow    *window,
296                                       GdkEventMask  event_mask)
297 {
298 }
299
300 static void
301 gdk_device_core_class_init (GdkDeviceCoreClass *klass)
302 {
303   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
304
305   device_class->get_history = gdk_device_core_get_history;
306   device_class->get_state = gdk_device_core_get_state;
307   device_class->set_window_cursor = gdk_device_core_set_window_cursor;
308   device_class->warp = gdk_device_core_warp;
309   device_class->query_state = gdk_device_core_query_state;
310   device_class->grab = gdk_device_core_grab;
311   device_class->ungrab = gdk_device_core_ungrab;
312   device_class->window_at_position = gdk_device_core_window_at_position;
313   device_class->select_window_events = gdk_device_core_select_window_events;
314 }
315
316 static void
317 gdk_device_core_init (GdkDeviceCore *device_core)
318 {
319   GdkDevice *device;
320
321   device = GDK_DEVICE (device_core);
322
323   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
324   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
325 }
326
327 struct wl_input_device *
328 _gdk_wayland_device_get_device (GdkDevice *device)
329 {
330   return GDK_DEVICE_CORE (device)->device->device;
331 }
332
333 static void
334 input_handle_motion(void *data, struct wl_input_device *input_device,
335                     uint32_t time,
336                     int32_t x, int32_t y, int32_t sx, int32_t sy)
337 {
338   GdkWaylandDevice *device = data;
339   GdkDisplayWayland *display = GDK_DISPLAY_WAYLAND (device->display);
340   GdkEvent *event;
341
342   event = gdk_event_new (GDK_NOTHING);
343
344   device->time = time;
345   device->x = x;
346   device->y = y;
347   device->surface_x = sx;
348   device->surface_y = sy;
349
350   event->motion.type = GDK_MOTION_NOTIFY;
351   event->motion.window = g_object_ref (device->pointer_focus);
352   gdk_event_set_device (event, device->pointer);
353   event->motion.time = time;
354   event->motion.x = (gdouble) sx;
355   event->motion.y = (gdouble) sy;
356   event->motion.x_root = (gdouble) x;
357   event->motion.y_root = (gdouble) y;
358   event->motion.axes = NULL;
359   event->motion.state = device->modifiers;
360   event->motion.is_hint = 0;
361   gdk_event_set_screen (event, display->screen);
362
363   GDK_NOTE (EVENTS,
364             g_message ("motion %d %d, state %d",
365                        sx, sy, event->button.state));
366
367   _gdk_wayland_display_deliver_event (device->display, event);
368 }
369
370 static void
371 input_handle_button(void *data, struct wl_input_device *input_device,
372                      uint32_t time, uint32_t button, uint32_t state)
373 {
374   GdkWaylandDevice *device = data;
375   GdkDisplayWayland *display = GDK_DISPLAY_WAYLAND (device->display);
376   GdkEvent *event;
377   uint32_t modifier;
378
379   device->time = time;
380   event = gdk_event_new (state ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
381   event->button.window = g_object_ref (device->pointer_focus);
382   gdk_event_set_device (event, device->pointer);
383   event->button.time = time;
384   event->button.x = (gdouble) device->surface_x;
385   event->button.y = (gdouble) device->surface_y;
386   event->button.x_root = (gdouble) device->x;
387   event->button.y_root = (gdouble) device->y;
388   event->button.axes = NULL;
389   event->button.state = device->modifiers;
390   event->button.button = button - 271;
391   gdk_event_set_screen (event, display->screen);
392
393   modifier = 1 << (8 + button - 272);
394   if (state)
395     device->modifiers |= modifier;
396   else
397     device->modifiers &= ~modifier;
398
399   GDK_NOTE (EVENTS,
400             g_message ("button %d %s, state %d",
401                        event->button.button,
402                        state ? "press" : "release", event->button.state));
403
404   _gdk_wayland_display_deliver_event (device->display, event);
405 }
406
407 static void
408 translate_keyboard_string (GdkEventKey *event)
409 {
410   gunichar c = 0;
411   gchar buf[7];
412
413   /* Fill in event->string crudely, since various programs
414    * depend on it.
415    */
416   event->string = NULL;
417
418   if (event->keyval != GDK_KEY_VoidSymbol)
419     c = gdk_keyval_to_unicode (event->keyval);
420
421   if (c)
422     {
423       gsize bytes_written;
424       gint len;
425
426       /* Apply the control key - Taken from Xlib
427        */
428       if (event->state & GDK_CONTROL_MASK)
429         {
430           if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
431           else if (c == '2')
432             {
433               event->string = g_memdup ("\0\0", 2);
434               event->length = 1;
435               buf[0] = '\0';
436               return;
437             }
438           else if (c >= '3' && c <= '7') c -= ('3' - '\033');
439           else if (c == '8') c = '\177';
440           else if (c == '/') c = '_' & 0x1F;
441         }
442
443       len = g_unichar_to_utf8 (c, buf);
444       buf[len] = '\0';
445
446       event->string = g_locale_from_utf8 (buf, len,
447                                           NULL, &bytes_written,
448                                           NULL);
449       if (event->string)
450         event->length = bytes_written;
451     }
452   else if (event->keyval == GDK_KEY_Escape)
453     {
454       event->length = 1;
455       event->string = g_strdup ("\033");
456     }
457   else if (event->keyval == GDK_KEY_Return ||
458           event->keyval == GDK_KEY_KP_Enter)
459     {
460       event->length = 1;
461       event->string = g_strdup ("\r");
462     }
463
464   if (!event->string)
465     {
466       event->length = 0;
467       event->string = g_strdup ("");
468     }
469 }
470
471 static void
472 input_handle_key(void *data, struct wl_input_device *input_device,
473                  uint32_t time, uint32_t key, uint32_t state)
474 {
475   GdkWaylandDevice *device = data;
476   GdkEvent *event;
477   uint32_t code, modifier, level;
478   struct xkb_desc *xkb;
479   GdkKeymap *keymap;
480
481   keymap = gdk_keymap_get_for_display (device->display);
482   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
483
484   device->time = time;
485   event = gdk_event_new (state ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
486   event->key.window = g_object_ref (device->keyboard_focus);
487   gdk_event_set_device (event, device->keyboard);
488   event->button.time = time;
489   event->key.state = device->modifiers;
490   event->key.group = 0;
491   code = key + xkb->min_key_code;
492   event->key.hardware_keycode = code;
493
494   level = 0;
495   if (device->modifiers & XKB_COMMON_SHIFT_MASK &&
496       XkbKeyGroupWidth(xkb, code, 0) > 1)
497     level = 1;
498
499   event->key.keyval = XkbKeySymEntry(xkb, code, level, 0);
500
501   modifier = xkb->map->modmap[code];
502   if (state)
503     device->modifiers |= modifier;
504   else
505     device->modifiers &= ~modifier;
506
507   event->key.is_modifier = modifier > 0;
508
509   translate_keyboard_string (&event->key);
510
511   _gdk_wayland_display_deliver_event (device->display, event);
512
513   GDK_NOTE (EVENTS,
514             g_message ("keyboard event, code %d, sym %d, "
515                        "string %s, mods 0x%x",
516                        code, event->key.keyval,
517                        event->key.string, event->key.state));
518 }
519
520 static void
521 input_handle_pointer_focus(void *data,
522                            struct wl_input_device *input_device,
523                            uint32_t time, struct wl_surface *surface,
524                            int32_t x, int32_t y, int32_t sx, int32_t sy)
525 {
526   GdkWaylandDevice *device = data;
527   GdkEvent *event;
528
529   device->time = time;
530   if (device->pointer_focus)
531     {
532       event = gdk_event_new (GDK_LEAVE_NOTIFY);
533       event->crossing.window = g_object_ref (device->pointer_focus);
534       gdk_event_set_device (event, device->pointer);
535       event->crossing.subwindow = NULL;
536       event->crossing.time = time;
537       event->crossing.x = (gdouble) device->surface_x;
538       event->crossing.y = (gdouble) device->surface_y;
539       event->crossing.x_root = (gdouble) device->x;
540       event->crossing.y_root = (gdouble) device->y;
541
542       event->crossing.mode = GDK_CROSSING_NORMAL;
543       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
544       event->crossing.focus = TRUE;
545       event->crossing.state = 0;
546
547       _gdk_wayland_display_deliver_event (device->display, event);
548
549       GDK_NOTE (EVENTS,
550                 g_message ("leave, device %p surface %p",
551                            device, device->pointer_focus));
552
553       g_object_unref(device->pointer_focus);
554       device->pointer_focus = NULL;
555     }
556
557   if (surface)
558     {
559       device->pointer_focus = wl_surface_get_user_data(surface);
560       g_object_ref(device->pointer_focus);
561
562       event = gdk_event_new (GDK_ENTER_NOTIFY);
563       event->crossing.window = g_object_ref (device->pointer_focus);
564       gdk_event_set_device (event, device->pointer);
565       event->crossing.subwindow = NULL;
566       event->crossing.time = time;
567       event->crossing.x = (gdouble) sx;
568       event->crossing.y = (gdouble) sy;
569       event->crossing.x_root = (gdouble) x;
570       event->crossing.y_root = (gdouble) y;
571
572       event->crossing.mode = GDK_CROSSING_NORMAL;
573       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
574       event->crossing.focus = TRUE;
575       event->crossing.state = 0;
576
577       device->surface_x = sx;
578       device->surface_y = sy;
579       device->x = x;
580       device->y = y;
581
582       _gdk_wayland_display_deliver_event (device->display, event);
583
584       GDK_NOTE (EVENTS,
585                 g_message ("enter, device %p surface %p",
586                            device, device->pointer_focus));
587     }
588 }
589
590 static void
591 update_modifiers(GdkWaylandDevice *device, struct wl_array *keys)
592 {
593   uint32_t *k, *end;
594   GdkKeymap *keymap;
595   struct xkb_desc *xkb;
596
597   keymap = gdk_keymap_get_for_display (device->display);
598   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
599
600   end = keys->data + keys->size;
601   device->modifiers = 0;
602   for (k = keys->data; k < end; k++)
603     device->modifiers |= xkb->map->modmap[*k];
604 }
605
606 static void
607 input_handle_keyboard_focus(void *data,
608                             struct wl_input_device *input_device,
609                             uint32_t time,
610                             struct wl_surface *surface,
611                             struct wl_array *keys)
612 {
613   GdkWaylandDevice *device = data;
614   GdkEvent *event;
615
616   device->time = time;
617   if (device->keyboard_focus)
618     {
619       event = gdk_event_new (GDK_FOCUS_CHANGE);
620       event->focus_change.window = g_object_ref (device->keyboard_focus);
621       event->focus_change.send_event = FALSE;
622       event->focus_change.in = FALSE;
623       gdk_event_set_device (event, device->keyboard);
624
625       g_object_unref(device->keyboard_focus);
626       device->keyboard_focus = NULL;
627
628       GDK_NOTE (EVENTS,
629                 g_message ("focus out, device %p surface %p",
630                            device, device->keyboard_focus));
631
632       _gdk_wayland_display_deliver_event (device->display, event);
633     }
634
635   if (surface)
636     {
637       device->keyboard_focus = wl_surface_get_user_data(surface);
638       g_object_ref(device->keyboard_focus);
639
640       event = gdk_event_new (GDK_FOCUS_CHANGE);
641       event->focus_change.window = g_object_ref (device->keyboard_focus);
642       event->focus_change.send_event = FALSE;
643       event->focus_change.in = TRUE;
644       gdk_event_set_device (event, device->keyboard);
645
646       update_modifiers (device, keys);
647
648       GDK_NOTE (EVENTS,
649                 g_message ("focus int, device %p surface %p",
650                            device, device->keyboard_focus));
651
652       _gdk_wayland_display_deliver_event (device->display, event);
653     }
654 }
655
656 static const struct wl_input_device_listener input_device_listener = {
657   input_handle_motion,
658   input_handle_button,
659   input_handle_key,
660   input_handle_pointer_focus,
661   input_handle_keyboard_focus,
662 };
663
664 struct _DataOffer {
665   struct wl_data_offer *offer;
666   gint ref_count;
667   GPtrArray *types;
668 };
669
670 static void
671 data_offer_offer (void                 *data,
672                   struct wl_data_offer *wl_data_offer,
673                   const char           *type)
674 {
675   DataOffer *offer = (DataOffer *)data;
676   g_debug (G_STRLOC ": %s wl_data_offer = %p type = %s",
677            G_STRFUNC, wl_data_offer, type);
678
679   g_ptr_array_add (offer->types, g_strdup (type));
680 }
681
682 static void
683 data_offer_unref (DataOffer *offer)
684 {
685   offer->ref_count--;
686
687   if (offer->ref_count == 0)
688     {
689       g_ptr_array_free (offer->types, TRUE);
690       g_free (offer);
691     }
692 }
693
694 static const struct wl_data_offer_listener data_offer_listener = {
695   data_offer_offer,
696 };
697
698 static void
699 data_device_data_offer (void                  *data,
700                         struct wl_data_device *data_device,
701                         uint32_t               id)
702 {
703   DataOffer *offer;
704
705   g_debug (G_STRLOC ": %s data_device = %p id = %lu",
706            G_STRFUNC, data_device, (long unsigned int)id);
707
708   /* This structure is reference counted to handle the case where you get a
709    * leave but are in the middle of transferring data
710    */
711   offer = g_new0 (DataOffer, 1);
712   offer->ref_count = 1;
713   offer->types = g_ptr_array_new_with_free_func (g_free);
714   offer->offer = (struct wl_data_offer *)
715     wl_proxy_create_for_id ((struct wl_proxy *) data_device,
716                             id,
717                             &wl_data_offer_interface);
718
719   /* The DataOffer structure is then retrieved later since this sets the user
720    * data.
721    */
722   wl_data_offer_add_listener (offer->offer,
723                               &data_offer_listener,
724                               offer);
725 }
726
727 static void
728 data_device_enter (void                  *data,
729                    struct wl_data_device *data_device,
730                    uint32_t               time,
731                    struct wl_surface     *surface,
732                    int32_t                x,
733                    int32_t                y,
734                    struct wl_data_offer  *offer)
735 {
736   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
737
738   g_debug (G_STRLOC ": %s data_device = %p time = %d, surface = %p, x = %d y = %d, offer = %p",
739            G_STRFUNC, data_device, time, surface, x, y, offer);
740
741   /* Retrieve the DataOffer associated with with the wl_data_offer - this
742    * association is made when the listener is attached.
743    */
744   g_assert (device->drag_offer == NULL);
745   device->drag_offer = wl_data_offer_get_user_data (offer);
746 }
747
748 static void
749 data_device_leave (void                  *data,
750                    struct wl_data_device *data_device)
751 {
752   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
753
754   g_debug (G_STRLOC ": %s data_device = %p",
755            G_STRFUNC, data_device);
756
757   data_offer_unref (device->drag_offer);
758   device->drag_offer = NULL;
759 }
760
761 static void
762 data_device_motion (void                  *data,
763                     struct wl_data_device *data_device,
764                     uint32_t               time,
765                     int32_t                x,
766                     int32_t                y)
767 {
768   g_debug (G_STRLOC ": %s data_device = %p, time = %d, x = %d, y = %d",
769            G_STRFUNC, data_device, time, x, y);
770 }
771
772 static void
773 data_device_drop (void                  *data,
774                   struct wl_data_device *data_device)
775 {
776   g_debug (G_STRLOC ": %s data_device = %p",
777            G_STRFUNC, data_device);
778 }
779
780 static void
781 data_device_selection (void                  *data,
782                        struct wl_data_device *wl_data_device,
783                        struct wl_data_offer  *offer)
784 {
785   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
786
787   g_debug (G_STRLOC ": %s wl_data_device = %p wl_data_offer = %p",
788            G_STRFUNC, wl_data_device, offer);
789
790   if (device->selection_offer)
791     {
792       data_offer_unref (device->selection_offer);
793       device->selection_offer = NULL;
794     }
795
796   /* Retrieve the DataOffer associated with with the wl_data_offer - this
797    * association is made when the listener is attached.
798    */
799   g_assert (device->selection_offer == NULL);
800   device->selection_offer = wl_data_offer_get_user_data (offer);
801 }
802
803 static const struct wl_data_device_listener data_device_listener = {
804   data_device_data_offer,
805   data_device_enter,
806   data_device_leave,
807   data_device_motion,
808   data_device_drop,
809   data_device_selection
810 };
811
812 void
813 _gdk_wayland_device_manager_add_device (GdkDeviceManager *device_manager,
814                                         struct wl_input_device *wl_device)
815 {
816   GdkDisplay *display;
817   GdkDisplayWayland *display_wayland;
818   GdkDeviceManagerCore *device_manager_core =
819     GDK_DEVICE_MANAGER_CORE(device_manager);
820   GdkWaylandDevice *device;
821
822   device = g_new0 (GdkWaylandDevice, 1);
823   display = gdk_device_manager_get_display (device_manager);
824   display_wayland = GDK_DISPLAY_WAYLAND (display);
825
826   device->display = display;
827   device->pointer = g_object_new (GDK_TYPE_DEVICE_CORE,
828                                   "name", "Core Pointer",
829                                   "type", GDK_DEVICE_TYPE_MASTER,
830                                   "input-source", GDK_SOURCE_MOUSE,
831                                   "input-mode", GDK_MODE_SCREEN,
832                                   "has-cursor", TRUE,
833                                   "display", display,
834                                   "device-manager", device_manager,
835                                   NULL);
836
837   device->keyboard = g_object_new (GDK_TYPE_DEVICE_CORE,
838                                    "name", "Core Keyboard",
839                                    "type", GDK_DEVICE_TYPE_MASTER,
840                                    "input-source", GDK_SOURCE_KEYBOARD,
841                                    "input-mode", GDK_MODE_SCREEN,
842                                    "has-cursor", FALSE,
843                                    "display", display,
844                                    "device-manager", device_manager,
845                                    NULL);
846
847   GDK_DEVICE_CORE (device->pointer)->device = device;
848   GDK_DEVICE_CORE (device->keyboard)->device = device;
849   device->device = wl_device;
850
851   wl_input_device_add_listener(device->device,
852                                &input_device_listener, device);
853
854   device->data_device =
855     wl_data_device_manager_get_data_device (display_wayland->data_device_manager,
856                                             device->device);
857   wl_data_device_add_listener (device->data_device,
858                                &data_device_listener, device);
859
860   device_manager_core->devices =
861     g_list_prepend (device_manager_core->devices, device->keyboard);
862   device_manager_core->devices =
863     g_list_prepend (device_manager_core->devices, device->pointer);
864
865   _gdk_device_set_associated_device (device->pointer, device->keyboard);
866   _gdk_device_set_associated_device (device->keyboard, device->pointer);
867 }
868
869 static void
870 free_device (gpointer data)
871 {
872   g_object_unref (data);
873 }
874
875 static void
876 gdk_device_manager_core_finalize (GObject *object)
877 {
878   GdkDeviceManagerCore *device_manager_core;
879
880   device_manager_core = GDK_DEVICE_MANAGER_CORE (object);
881
882   g_list_free_full (device_manager_core->devices, free_device);
883
884   G_OBJECT_CLASS (gdk_device_manager_core_parent_class)->finalize (object);
885 }
886
887 static GList *
888 gdk_device_manager_core_list_devices (GdkDeviceManager *device_manager,
889                                       GdkDeviceType     type)
890 {
891   GdkDeviceManagerCore *device_manager_core;
892   GList *devices = NULL;
893
894   if (type == GDK_DEVICE_TYPE_MASTER)
895     {
896       device_manager_core = (GdkDeviceManagerCore *) device_manager;
897       devices = g_list_copy(device_manager_core->devices);
898     }
899
900   return devices;
901 }
902
903 static GdkDevice *
904 gdk_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager)
905 {
906   GdkDeviceManagerCore *device_manager_core;
907
908   device_manager_core = (GdkDeviceManagerCore *) device_manager;
909   return device_manager_core->devices->data;
910 }
911
912 static void
913 gdk_device_manager_core_class_init (GdkDeviceManagerCoreClass *klass)
914 {
915   GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
916   GObjectClass *object_class = G_OBJECT_CLASS (klass);
917
918   object_class->finalize = gdk_device_manager_core_finalize;
919   device_manager_class->list_devices = gdk_device_manager_core_list_devices;
920   device_manager_class->get_client_pointer = gdk_device_manager_core_get_client_pointer;
921 }
922
923 static void
924 gdk_device_manager_core_init (GdkDeviceManagerCore *device_manager)
925 {
926 }
927
928 GdkDeviceManager *
929 _gdk_wayland_device_manager_new (GdkDisplay *display)
930 {
931   return g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
932                        "display", display,
933                        NULL);
934 }
935
936 gint
937 gdk_wayland_device_get_selection_type_atoms (GdkDevice  *gdk_device,
938                                              GdkAtom   **atoms_out)
939 {
940   gint i;
941   GdkAtom *atoms;
942   GdkWaylandDevice *device;
943
944   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
945   g_return_val_if_fail (atoms_out != NULL, 0);
946
947   device = GDK_DEVICE_CORE (gdk_device)->device;
948
949   if (device->selection_offer->types->len == 0)
950     {
951       *atoms_out = NULL;
952       return 0;
953     }
954
955   atoms = g_new0 (GdkAtom, device->selection_offer->types->len);
956
957   /* Convert list of targets to atoms */
958   for (i = 0; i < device->selection_offer->types->len; i++)
959     {
960       atoms[i] = gdk_atom_intern (device->selection_offer->types->pdata[i],
961                                   FALSE);
962       GDK_NOTE (MISC,
963                 g_message (G_STRLOC ": Adding atom for %s",
964                            (char *)device->selection_offer->types->pdata[i]));
965     }
966
967   *atoms_out = atoms;
968   return device->selection_offer->types->len;
969 }
970
971 typedef struct
972 {
973   GdkWaylandDevice *device;
974   DataOffer *offer;
975   GIOChannel *channel;
976   GdkDeviceWaylandRequestContentCallback cb;
977   gpointer userdata;
978 } RequestContentClosure;
979
980 static gboolean
981 _request_content_io_func (GIOChannel *channel,
982                           GIOCondition condition,
983                           gpointer userdata)
984 {
985   RequestContentClosure *closure = (RequestContentClosure *)userdata;
986   gchar *data = NULL;
987   gsize len = 0;
988   GError *error = NULL;
989
990   /* FIXME: We probably want to do something better than this to avoid
991    * blocking on the transfer of large pieces of data: call the callback
992    * multiple times I should think.
993    */
994   if (g_io_channel_read_to_end (channel,
995                                 &data,
996                                 &len,
997                                 &error) != G_IO_STATUS_NORMAL)
998     {
999       g_warning (G_STRLOC ": Error reading content from pipe: %s", error->message);
1000       g_clear_error (&error);
1001     }
1002
1003   /* Since we use _read_to_end we've got a guaranteed EOF and thus can go
1004    * ahead and close the fd
1005    */
1006   g_io_channel_shutdown (channel, TRUE, NULL);
1007
1008   closure->cb (closure->device->pointer, data, len, closure->userdata);
1009
1010   g_free (data);
1011   data_offer_unref (closure->offer);
1012   g_io_channel_unref (channel);
1013   g_free (closure);
1014
1015   return FALSE;
1016 }
1017
1018 gboolean
1019 gdk_wayland_device_request_selection_content (GdkDevice                              *gdk_device,
1020                                               const gchar                            *requested_mime_type,
1021                                               GdkDeviceWaylandRequestContentCallback  cb,
1022                                               gpointer                                userdata)
1023 {
1024   int pipe_fd[2];
1025   RequestContentClosure *closure;
1026   GdkWaylandDevice *device;
1027   GError *error = NULL;
1028
1029   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), FALSE);
1030   g_return_val_if_fail (requested_mime_type != NULL, FALSE);
1031   g_return_val_if_fail (cb != NULL, FALSE);
1032
1033   device = GDK_DEVICE_CORE (gdk_device)->device;
1034
1035   if (!device->selection_offer)
1036       return FALSE;
1037
1038   /* TODO: Check mimetypes */
1039
1040   closure = g_new0 (RequestContentClosure, 1);
1041
1042   device->selection_offer->ref_count++;
1043
1044   pipe2 (pipe_fd, O_CLOEXEC);
1045   wl_data_offer_receive (device->selection_offer->offer,
1046                          requested_mime_type,
1047                          pipe_fd[1]);
1048   close (pipe_fd[1]);
1049
1050   closure->device = device;
1051   closure->offer = device->selection_offer;
1052   closure->channel = g_io_channel_unix_new (pipe_fd[0]);
1053   closure->cb = cb;
1054   closure->userdata = userdata;
1055
1056   if (!g_io_channel_set_encoding (closure->channel, NULL, &error))
1057     {
1058       g_warning (G_STRLOC ": Error setting encoding on channel: %s",
1059                  error->message);
1060       g_clear_error (&error);
1061       goto error;
1062     }
1063
1064   g_io_add_watch (closure->channel,
1065                   G_IO_IN,
1066                   _request_content_io_func,
1067                   closure);
1068
1069   return TRUE;
1070
1071 error:
1072   data_offer_unref (closure->offer);
1073   g_io_channel_unref (closure->channel);
1074   close (pipe_fd[1]);
1075   g_free (closure);
1076
1077   return FALSE;
1078 }
1079
1080 struct _GdkWaylandSelectionOffer {
1081   GdkDeviceWaylandOfferContentCallback cb;
1082   gpointer userdata;
1083   struct wl_data_source *source;
1084   GdkWaylandDevice *device;
1085 };
1086
1087 static void
1088 data_source_target (void                  *data,
1089                     struct wl_data_source *source,
1090                     const char            *mime_type)
1091 {
1092   g_debug (G_STRLOC ": %s source = %p, mime_type = %s",
1093            G_STRFUNC, source, mime_type);
1094 }
1095
1096 static void
1097 data_source_send (void                  *data,
1098                   struct wl_data_source *source,
1099                   const char            *mime_type,
1100                   int32_t                fd)
1101 {
1102   GdkWaylandSelectionOffer *offer = (GdkWaylandSelectionOffer *)data;;
1103   gchar *buf;
1104   gssize len, bytes_written = 0;
1105
1106   g_debug (G_STRLOC ": %s source = %p, mime_type = %s fd = %d",
1107            G_STRFUNC, source, mime_type, fd);
1108
1109   buf = offer->cb (offer->device->pointer, mime_type, &len, offer->userdata);
1110
1111   while (len > 0)
1112     {
1113       bytes_written += write (fd, buf + bytes_written, len);
1114       if (bytes_written == -1)
1115         goto error;
1116       len -= bytes_written;
1117     }
1118
1119   close (fd);
1120   g_free (buf);
1121
1122   return;
1123 error:
1124
1125   g_warning (G_STRLOC ": Error writing data to client: %s",
1126              g_strerror (errno));
1127
1128   close (fd);
1129   g_free (buf);
1130 }
1131
1132 static void
1133 data_source_cancelled (void                  *data,
1134                        struct wl_data_source *source)
1135 {
1136   g_debug (G_STRLOC ": %s source = %p",
1137            G_STRFUNC, source);
1138 }
1139
1140 static const struct wl_data_source_listener data_source_listener = {
1141     data_source_target,
1142     data_source_send,
1143     data_source_cancelled
1144 };
1145
1146 static guint32
1147 _wl_time_now (void)
1148 {
1149   struct timeval tv;
1150
1151   gettimeofday(&tv, NULL);
1152
1153   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1154 }
1155
1156 gboolean
1157 gdk_wayland_device_offer_selection_content (GdkDevice                             *gdk_device,
1158                                             const gchar                          **mime_types,
1159                                             gint                                   nr_mime_types,
1160                                             GdkDeviceWaylandOfferContentCallback   cb,
1161                                             gpointer                               userdata)
1162 {
1163   GdkDisplay *display;
1164   GdkDisplayWayland *display_wayland;
1165   GdkWaylandSelectionOffer *offer;
1166   GdkWaylandDevice *device;
1167   gint i;
1168
1169   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1170   device = GDK_DEVICE_CORE (gdk_device)->device;
1171
1172   display = device->display;
1173   display_wayland = GDK_DISPLAY_WAYLAND (display);
1174
1175   offer = g_new0 (GdkWaylandSelectionOffer, 1);
1176   offer->cb = cb;
1177   offer->userdata = userdata;
1178   offer->source =
1179     wl_data_device_manager_create_data_source (display_wayland->data_device_manager);
1180   offer->device = device;
1181
1182   for (i = 0; i < nr_mime_types; i++)
1183     {
1184       wl_data_source_offer (offer->source,
1185                             mime_types[i]);
1186     }
1187
1188   wl_data_source_add_listener (offer->source,
1189                                &data_source_listener,
1190                                offer);
1191
1192   wl_data_device_set_selection (device->data_device,
1193                                 offer->source,
1194                                 _wl_time_now ());
1195
1196   device->selection_offer_out = offer;
1197
1198   return TRUE;
1199 }
1200
1201 gboolean
1202 gdk_wayland_device_clear_selection_content (GdkDevice *gdk_device)
1203 {
1204   GdkWaylandDevice *device;
1205
1206   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1207   device = GDK_DEVICE_CORE (gdk_device)->device;
1208
1209   if (!device->selection_offer_out)
1210     return FALSE;
1211
1212   wl_data_device_set_selection (device->data_device,
1213                                 NULL,
1214                                 _wl_time_now ());
1215
1216   wl_data_source_destroy (device->selection_offer_out->source);
1217   g_free (device->selection_offer_out);
1218   device->selection_offer_out = NULL;
1219
1220   return TRUE;
1221 }