]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkdevice-wayland.c
gdk/*: Use g_list_free_full convenience function
[~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 #include "config.h"
21
22 #include <string.h>
23 #include <gdk/gdkwindow.h>
24 #include <gdk/gdktypes.h>
25 #include "gdkprivate-wayland.h"
26 #include "gdkwayland.h"
27 #include "gdkkeysyms.h"
28 #include "gdkdeviceprivate.h"
29 #include "gdkdevicemanagerprivate.h"
30 #include "gdkprivate-wayland.h"
31
32 #include <X11/extensions/XKBcommon.h>
33 #include <X11/keysym.h>
34
35 #define GDK_TYPE_DEVICE_CORE         (gdk_device_core_get_type ())
36 #define GDK_DEVICE_CORE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE_CORE, GdkDeviceCore))
37 #define GDK_DEVICE_CORE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_DEVICE_CORE, GdkDeviceCoreClass))
38 #define GDK_IS_DEVICE_CORE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE_CORE))
39 #define GDK_IS_DEVICE_CORE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_DEVICE_CORE))
40 #define GDK_DEVICE_CORE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_DEVICE_CORE, GdkDeviceCoreClass))
41
42 typedef struct _GdkDeviceCore GdkDeviceCore;
43 typedef struct _GdkDeviceCoreClass GdkDeviceCoreClass;
44 typedef struct _GdkWaylandDevice GdkWaylandDevice;
45
46 typedef struct _DataOffer DataOffer;
47
48 struct _GdkWaylandDevice
49 {
50   GdkDisplay *display;
51   GdkDevice *pointer;
52   GdkDevice *keyboard;
53   GdkModifierType modifiers;
54   GdkWindow *pointer_focus;
55   GdkWindow *keyboard_focus;
56   struct wl_input_device *device;
57   struct wl_data_device *data_device;
58   int32_t x, y, surface_x, surface_y;
59   uint32_t time;
60
61   DataOffer *drag_offer;
62   DataOffer *selection_offer;
63 };
64
65 struct _GdkDeviceCore
66 {
67   GdkDevice parent_instance;
68   GdkWaylandDevice *device;
69 };
70
71 struct _GdkDeviceCoreClass
72 {
73   GdkDeviceClass parent_class;
74 };
75
76 G_DEFINE_TYPE (GdkDeviceCore, gdk_device_core, GDK_TYPE_DEVICE)
77
78 #define GDK_TYPE_DEVICE_MANAGER_CORE         (gdk_device_manager_core_get_type ())
79 #define GDK_DEVICE_MANAGER_CORE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCore))
80 #define GDK_DEVICE_MANAGER_CORE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCoreClass))
81 #define GDK_IS_DEVICE_MANAGER_CORE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE_MANAGER_CORE))
82 #define GDK_IS_DEVICE_MANAGER_CORE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_DEVICE_MANAGER_CORE))
83 #define GDK_DEVICE_MANAGER_CORE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCoreClass))
84
85 typedef struct _GdkDeviceManagerCore GdkDeviceManagerCore;
86 typedef struct _GdkDeviceManagerCoreClass GdkDeviceManagerCoreClass;
87
88 struct _GdkDeviceManagerCore
89 {
90   GdkDeviceManager parent_object;
91   GdkDevice *core_pointer;
92   GdkDevice *core_keyboard;
93   GList *devices;
94 };
95
96 struct _GdkDeviceManagerCoreClass
97 {
98   GdkDeviceManagerClass parent_class;
99 };
100
101 G_DEFINE_TYPE (GdkDeviceManagerCore,
102                gdk_device_manager_core, GDK_TYPE_DEVICE_MANAGER)
103
104 static gboolean
105 gdk_device_core_get_history (GdkDevice      *device,
106                              GdkWindow      *window,
107                              guint32         start,
108                              guint32         stop,
109                              GdkTimeCoord ***events,
110                              gint           *n_events)
111 {
112   return FALSE;
113 }
114
115 static void
116 gdk_device_core_get_state (GdkDevice       *device,
117                            GdkWindow       *window,
118                            gdouble         *axes,
119                            GdkModifierType *mask)
120 {
121   gint x_int, y_int;
122
123   gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
124
125   if (axes)
126     {
127       axes[0] = x_int;
128       axes[1] = y_int;
129     }
130 }
131
132 static void
133 gdk_device_core_set_window_cursor (GdkDevice *device,
134                                    GdkWindow *window,
135                                    GdkCursor *cursor)
136 {
137   GdkWaylandDevice *wd = GDK_DEVICE_CORE(device)->device;
138   struct wl_buffer *buffer;
139   int x, y;
140
141   if (cursor)
142     {
143       buffer = _gdk_wayland_cursor_get_buffer(cursor, &x, &y);
144       wl_input_device_attach(wd->device, wd->time, buffer, x, y);
145     }
146   else
147     {
148       wl_input_device_attach(wd->device, wd->time, NULL, 0, 0);
149     }
150 }
151
152 static void
153 gdk_device_core_warp (GdkDevice *device,
154                       GdkScreen *screen,
155                       gint       x,
156                       gint       y)
157 {
158 }
159
160 static gboolean
161 gdk_device_core_query_state (GdkDevice        *device,
162                              GdkWindow        *window,
163                              GdkWindow       **root_window,
164                              GdkWindow       **child_window,
165                              gint             *root_x,
166                              gint             *root_y,
167                              gint             *win_x,
168                              gint             *win_y,
169                              GdkModifierType  *mask)
170 {
171   GdkWaylandDevice *wd;
172   GdkScreen *default_screen;
173
174   wd = GDK_DEVICE_CORE(device)->device;
175   default_screen = gdk_display_get_default_screen (wd->display);
176
177   if (root_window)
178     *root_window = gdk_screen_get_root_window (default_screen);
179   if (child_window)
180     *child_window = wd->pointer_focus;
181   if (root_x)
182     *root_x = wd->x;
183   if (root_y)
184     *root_y = wd->y;
185   if (win_x)
186     *win_x = wd->surface_x;
187   if (win_y)
188     *win_y = wd->surface_y;
189   if (mask)
190     *mask = wd->modifiers;
191
192   return TRUE;
193 }
194
195 static GdkGrabStatus
196 gdk_device_core_grab (GdkDevice    *device,
197                       GdkWindow    *window,
198                       gboolean      owner_events,
199                       GdkEventMask  event_mask,
200                       GdkWindow    *confine_to,
201                       GdkCursor    *cursor,
202                       guint32       time_)
203 {
204   return GDK_GRAB_SUCCESS;
205 }
206
207 static void
208 gdk_device_core_ungrab (GdkDevice *device,
209                         guint32    time_)
210 {
211 }
212
213 static GdkWindow *
214 gdk_device_core_window_at_position (GdkDevice       *device,
215                                     gint            *win_x,
216                                     gint            *win_y,
217                                     GdkModifierType *mask,
218                                     gboolean         get_toplevel)
219 {
220   GdkWaylandDevice *wd;
221
222   wd = GDK_DEVICE_CORE(device)->device;
223   if (win_x)
224     *win_x = wd->surface_x;
225   if (win_y)
226     *win_y = wd->surface_y;
227   if (mask)
228     *mask = wd->modifiers;
229
230   return wd->pointer_focus;
231 }
232
233 static void
234 gdk_device_core_select_window_events (GdkDevice    *device,
235                                       GdkWindow    *window,
236                                       GdkEventMask  event_mask)
237 {
238 }
239
240 static void
241 gdk_device_core_class_init (GdkDeviceCoreClass *klass)
242 {
243   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
244
245   device_class->get_history = gdk_device_core_get_history;
246   device_class->get_state = gdk_device_core_get_state;
247   device_class->set_window_cursor = gdk_device_core_set_window_cursor;
248   device_class->warp = gdk_device_core_warp;
249   device_class->query_state = gdk_device_core_query_state;
250   device_class->grab = gdk_device_core_grab;
251   device_class->ungrab = gdk_device_core_ungrab;
252   device_class->window_at_position = gdk_device_core_window_at_position;
253   device_class->select_window_events = gdk_device_core_select_window_events;
254 }
255
256 static void
257 gdk_device_core_init (GdkDeviceCore *device_core)
258 {
259   GdkDevice *device;
260
261   device = GDK_DEVICE (device_core);
262
263   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
264   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
265 }
266
267 struct wl_input_device *
268 _gdk_wayland_device_get_device (GdkDevice *device)
269 {
270   return GDK_DEVICE_CORE (device)->device->device;
271 }
272
273 static void
274 input_handle_motion(void *data, struct wl_input_device *input_device,
275                     uint32_t time,
276                     int32_t x, int32_t y, int32_t sx, int32_t sy)
277 {
278   GdkWaylandDevice *device = data;
279   GdkDisplayWayland *display = GDK_DISPLAY_WAYLAND (device->display);
280   GdkEvent *event;
281
282   event = gdk_event_new (GDK_NOTHING);
283
284   device->time = time;
285   device->x = x;
286   device->y = y;
287   device->surface_x = sx;
288   device->surface_y = sy;
289
290   event->motion.type = GDK_MOTION_NOTIFY;
291   event->motion.window = g_object_ref (device->pointer_focus);
292   gdk_event_set_device (event, device->pointer);
293   event->motion.time = time;
294   event->motion.x = (gdouble) sx;
295   event->motion.y = (gdouble) sy;
296   event->motion.x_root = (gdouble) x;
297   event->motion.y_root = (gdouble) y;
298   event->motion.axes = NULL;
299   event->motion.state = device->modifiers;
300   event->motion.is_hint = 0;
301   gdk_event_set_screen (event, display->screen);
302
303   GDK_NOTE (EVENTS,
304             g_message ("motion %d %d, state %d",
305                        sx, sy, event->button.state));
306
307   _gdk_wayland_display_deliver_event (device->display, event);
308 }
309
310 static void
311 input_handle_button(void *data, struct wl_input_device *input_device,
312                      uint32_t time, uint32_t button, uint32_t state)
313 {
314   GdkWaylandDevice *device = data;
315   GdkDisplayWayland *display = GDK_DISPLAY_WAYLAND (device->display);
316   GdkEvent *event;
317   uint32_t modifier;
318
319   device->time = time;
320   event = gdk_event_new (state ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
321   event->button.window = g_object_ref (device->pointer_focus);
322   gdk_event_set_device (event, device->pointer);
323   event->button.time = time;
324   event->button.x = (gdouble) device->surface_x;
325   event->button.y = (gdouble) device->surface_y;
326   event->button.x_root = (gdouble) device->x;
327   event->button.y_root = (gdouble) device->y;
328   event->button.axes = NULL;
329   event->button.state = device->modifiers;
330   event->button.button = button - 271;
331   gdk_event_set_screen (event, display->screen);
332
333   modifier = 1 << (8 + button - 272);
334   if (state)
335     device->modifiers |= modifier;
336   else
337     device->modifiers &= ~modifier;
338
339   GDK_NOTE (EVENTS,
340             g_message ("button %d %s, state %d",
341                        event->button.button,
342                        state ? "press" : "release", event->button.state));
343
344   _gdk_wayland_display_deliver_event (device->display, event);
345 }
346
347 static void
348 translate_keyboard_string (GdkEventKey *event)
349 {
350   gunichar c = 0;
351   gchar buf[7];
352
353   /* Fill in event->string crudely, since various programs
354    * depend on it.
355    */
356   event->string = NULL;
357
358   if (event->keyval != GDK_KEY_VoidSymbol)
359     c = gdk_keyval_to_unicode (event->keyval);
360
361   if (c)
362     {
363       gsize bytes_written;
364       gint len;
365
366       /* Apply the control key - Taken from Xlib
367        */
368       if (event->state & GDK_CONTROL_MASK)
369         {
370           if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
371           else if (c == '2')
372             {
373               event->string = g_memdup ("\0\0", 2);
374               event->length = 1;
375               buf[0] = '\0';
376               return;
377             }
378           else if (c >= '3' && c <= '7') c -= ('3' - '\033');
379           else if (c == '8') c = '\177';
380           else if (c == '/') c = '_' & 0x1F;
381         }
382
383       len = g_unichar_to_utf8 (c, buf);
384       buf[len] = '\0';
385
386       event->string = g_locale_from_utf8 (buf, len,
387                                           NULL, &bytes_written,
388                                           NULL);
389       if (event->string)
390         event->length = bytes_written;
391     }
392   else if (event->keyval == GDK_KEY_Escape)
393     {
394       event->length = 1;
395       event->string = g_strdup ("\033");
396     }
397   else if (event->keyval == GDK_KEY_Return ||
398           event->keyval == GDK_KEY_KP_Enter)
399     {
400       event->length = 1;
401       event->string = g_strdup ("\r");
402     }
403
404   if (!event->string)
405     {
406       event->length = 0;
407       event->string = g_strdup ("");
408     }
409 }
410
411 static void
412 input_handle_key(void *data, struct wl_input_device *input_device,
413                  uint32_t time, uint32_t key, uint32_t state)
414 {
415   GdkWaylandDevice *device = data;
416   GdkEvent *event;
417   uint32_t code, modifier, level;
418   struct xkb_desc *xkb;
419   GdkKeymap *keymap;
420
421   keymap = gdk_keymap_get_for_display (device->display);
422   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
423
424   device->time = time;
425   event = gdk_event_new (state ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
426   event->key.window = g_object_ref (device->keyboard_focus);
427   gdk_event_set_device (event, device->keyboard);
428   event->button.time = time;
429   event->key.state = device->modifiers;
430   event->key.group = 0;
431   code = key + xkb->min_key_code;
432   event->key.hardware_keycode = code;
433
434   level = 0;
435   if (device->modifiers & XKB_COMMON_SHIFT_MASK &&
436       XkbKeyGroupWidth(xkb, code, 0) > 1)
437     level = 1;
438
439   event->key.keyval = XkbKeySymEntry(xkb, code, level, 0);
440
441   modifier = xkb->map->modmap[code];
442   if (state)
443     device->modifiers |= modifier;
444   else
445     device->modifiers &= ~modifier;
446
447   event->key.is_modifier = modifier > 0;
448
449   translate_keyboard_string (&event->key);
450
451   _gdk_wayland_display_deliver_event (device->display, event);
452
453   GDK_NOTE (EVENTS,
454             g_message ("keyboard event, code %d, sym %d, "
455                        "string %s, mods 0x%x",
456                        code, event->key.keyval,
457                        event->key.string, event->key.state));
458 }
459
460 static void
461 input_handle_pointer_focus(void *data,
462                            struct wl_input_device *input_device,
463                            uint32_t time, struct wl_surface *surface,
464                            int32_t x, int32_t y, int32_t sx, int32_t sy)
465 {
466   GdkWaylandDevice *device = data;
467   GdkEvent *event;
468
469   device->time = time;
470   if (device->pointer_focus)
471     {
472       event = gdk_event_new (GDK_LEAVE_NOTIFY);
473       event->crossing.window = g_object_ref (device->pointer_focus);
474       gdk_event_set_device (event, device->pointer);
475       event->crossing.subwindow = NULL;
476       event->crossing.time = time;
477       event->crossing.x = (gdouble) device->surface_x;
478       event->crossing.y = (gdouble) device->surface_y;
479       event->crossing.x_root = (gdouble) device->x;
480       event->crossing.y_root = (gdouble) device->y;
481
482       event->crossing.mode = GDK_CROSSING_NORMAL;
483       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
484       event->crossing.focus = TRUE;
485       event->crossing.state = 0;
486
487       _gdk_wayland_display_deliver_event (device->display, event);
488
489       GDK_NOTE (EVENTS,
490                 g_message ("leave, device %p surface %p",
491                            device, device->pointer_focus));
492
493       g_object_unref(device->pointer_focus);
494       device->pointer_focus = NULL;
495     }
496
497   if (surface)
498     {
499       device->pointer_focus = wl_surface_get_user_data(surface);
500       g_object_ref(device->pointer_focus);
501
502       event = gdk_event_new (GDK_ENTER_NOTIFY);
503       event->crossing.window = g_object_ref (device->pointer_focus);
504       gdk_event_set_device (event, device->pointer);
505       event->crossing.subwindow = NULL;
506       event->crossing.time = time;
507       event->crossing.x = (gdouble) sx;
508       event->crossing.y = (gdouble) sy;
509       event->crossing.x_root = (gdouble) x;
510       event->crossing.y_root = (gdouble) y;
511
512       event->crossing.mode = GDK_CROSSING_NORMAL;
513       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
514       event->crossing.focus = TRUE;
515       event->crossing.state = 0;
516
517       device->surface_x = sx;
518       device->surface_y = sy;
519       device->x = x;
520       device->y = y;
521
522       _gdk_wayland_display_deliver_event (device->display, event);
523
524       GDK_NOTE (EVENTS,
525                 g_message ("enter, device %p surface %p",
526                            device, device->pointer_focus));
527     }
528 }
529
530 static void
531 update_modifiers(GdkWaylandDevice *device, struct wl_array *keys)
532 {
533   uint32_t *k, *end;
534   GdkKeymap *keymap;
535   struct xkb_desc *xkb;
536
537   keymap = gdk_keymap_get_for_display (device->display);
538   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
539
540   end = keys->data + keys->size;
541   device->modifiers = 0;
542   for (k = keys->data; k < end; k++)
543     device->modifiers |= xkb->map->modmap[*k];
544 }
545
546 static void
547 input_handle_keyboard_focus(void *data,
548                             struct wl_input_device *input_device,
549                             uint32_t time,
550                             struct wl_surface *surface,
551                             struct wl_array *keys)
552 {
553   GdkWaylandDevice *device = data;
554   GdkEvent *event;
555
556   device->time = time;
557   if (device->keyboard_focus)
558     {
559       event = gdk_event_new (GDK_FOCUS_CHANGE);
560       event->focus_change.window = g_object_ref (device->keyboard_focus);
561       event->focus_change.send_event = FALSE;
562       event->focus_change.in = FALSE;
563       gdk_event_set_device (event, device->keyboard);
564
565       g_object_unref(device->keyboard_focus);
566       device->keyboard_focus = NULL;
567
568       GDK_NOTE (EVENTS,
569                 g_message ("focus out, device %p surface %p",
570                            device, device->keyboard_focus));
571
572       _gdk_wayland_display_deliver_event (device->display, event);
573     }
574
575   if (surface)
576     {
577       device->keyboard_focus = wl_surface_get_user_data(surface);
578       g_object_ref(device->keyboard_focus);
579
580       event = gdk_event_new (GDK_FOCUS_CHANGE);
581       event->focus_change.window = g_object_ref (device->keyboard_focus);
582       event->focus_change.send_event = FALSE;
583       event->focus_change.in = TRUE;
584       gdk_event_set_device (event, device->keyboard);
585
586       update_modifiers (device, keys);
587
588       GDK_NOTE (EVENTS,
589                 g_message ("focus int, device %p surface %p",
590                            device, device->keyboard_focus));
591
592       _gdk_wayland_display_deliver_event (device->display, event);
593     }
594 }
595
596 static const struct wl_input_device_listener input_device_listener = {
597   input_handle_motion,
598   input_handle_button,
599   input_handle_key,
600   input_handle_pointer_focus,
601   input_handle_keyboard_focus,
602 };
603
604 struct _DataOffer {
605   struct wl_data_offer *offer;
606   gint ref_count;
607   GPtrArray *types;
608 };
609
610 static void
611 data_offer_offer (void                 *data,
612                   struct wl_data_offer *wl_data_offer,
613                   const char           *type)
614 {
615   DataOffer *offer = (DataOffer *)data;
616   g_debug (G_STRLOC ": %s wl_data_offer = %p type = %s",
617            G_STRFUNC, wl_data_offer, type);
618
619   g_ptr_array_add (offer->types, g_strdup (type));
620 }
621
622 static void
623 data_offer_unref (DataOffer *offer)
624 {
625   offer->ref_count--;
626
627   if (offer->ref_count == 0)
628     {
629       g_ptr_array_free (offer->types, TRUE);
630       g_free (offer);
631     }
632 }
633
634 static const struct wl_data_offer_listener data_offer_listener = {
635   data_offer_offer,
636 };
637
638 static void
639 data_device_data_offer (void                  *data,
640                         struct wl_data_device *data_device,
641                         uint32_t               id)
642 {
643   DataOffer *offer;
644
645   g_debug (G_STRLOC ": %s data_device = %p id = %lu",
646            G_STRFUNC, data_device, (long unsigned int)id);
647
648   /* This structure is reference counted to handle the case where you get a
649    * leave but are in the middle of transferring data
650    */
651   offer = g_new0 (DataOffer, 1);
652   offer->ref_count = 1;
653   offer->types = g_ptr_array_new_with_free_func (g_free);
654   offer->offer = (struct wl_data_offer *)
655     wl_proxy_create_for_id ((struct wl_proxy *) data_device,
656                             id,
657                             &wl_data_offer_interface);
658
659   /* The DataOffer structure is then retrieved later since this sets the user
660    * data.
661    */
662   wl_data_offer_add_listener (offer->offer,
663                               &data_offer_listener,
664                               offer);
665 }
666
667 static void
668 data_device_enter (void                  *data,
669                    struct wl_data_device *data_device,
670                    uint32_t               time,
671                    struct wl_surface     *surface,
672                    int32_t                x,
673                    int32_t                y,
674                    struct wl_data_offer  *offer)
675 {
676   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
677
678   g_debug (G_STRLOC ": %s data_device = %p time = %d, surface = %p, x = %d y = %d, offer = %p",
679            G_STRFUNC, data_device, time, surface, x, y, offer);
680
681   /* Retrieve the DataOffer associated with with the wl_data_offer - this
682    * association is made when the listener is attached.
683    */
684   g_assert (device->drag_offer == NULL);
685   device->drag_offer = wl_data_offer_get_user_data (offer);
686 }
687
688 static void
689 data_device_leave (void                  *data,
690                    struct wl_data_device *data_device)
691 {
692   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
693
694   g_debug (G_STRLOC ": %s data_device = %p",
695            G_STRFUNC, data_device);
696
697   data_offer_unref (device->drag_offer);
698   device->drag_offer = NULL;
699 }
700
701 static void
702 data_device_motion (void                  *data,
703                     struct wl_data_device *data_device,
704                     uint32_t               time,
705                     int32_t                x,
706                     int32_t                y)
707 {
708   g_debug (G_STRLOC ": %s data_device = %p, time = %d, x = %d, y = %d",
709            G_STRFUNC, data_device, time, x, y);
710 }
711
712 static void
713 data_device_drop (void                  *data,
714                   struct wl_data_device *data_device)
715 {
716   g_debug (G_STRLOC ": %s data_device = %p",
717            G_STRFUNC, data_device);
718 }
719
720 static void
721 data_device_selection (void                  *data,
722                        struct wl_data_device *wl_data_device,
723                        struct wl_data_offer  *offer)
724 {
725   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
726   GdkDeviceManager *device_manager =
727     gdk_display_get_device_manager (device->display);
728   GdkDeviceManagerCore *device_manager_core =
729     GDK_DEVICE_MANAGER_CORE (device_manager);
730
731   g_debug (G_STRLOC ": %s wl_data_device = %p wl_data_offer = %p",
732            G_STRFUNC, wl_data_device, offer);
733
734   if (device->selection_offer)
735     {
736       data_offer_unref (device->selection_offer);
737       device->selection_offer = NULL;
738     }
739
740   /* Retrieve the DataOffer associated with with the wl_data_offer - this
741    * association is made when the listener is attached.
742    */
743   g_assert (device->selection_offer == NULL);
744   device->selection_offer = wl_data_offer_get_user_data (offer);
745 }
746
747 static const struct wl_data_device_listener data_device_listener = {
748   data_device_data_offer,
749   data_device_enter,
750   data_device_leave,
751   data_device_motion,
752   data_device_drop,
753   data_device_selection
754 };
755
756 void
757 _gdk_wayland_device_manager_add_device (GdkDeviceManager *device_manager,
758                                         struct wl_input_device *wl_device)
759 {
760   GdkDisplay *display;
761   GdkDisplayWayland *display_wayland;
762   GdkDeviceManagerCore *device_manager_core =
763     GDK_DEVICE_MANAGER_CORE(device_manager);
764   GdkWaylandDevice *device;
765
766   device = g_new0 (GdkWaylandDevice, 1);
767   display = gdk_device_manager_get_display (device_manager);
768   display_wayland = GDK_DISPLAY_WAYLAND (display);
769
770   device->display = display;
771   device->pointer = g_object_new (GDK_TYPE_DEVICE_CORE,
772                                   "name", "Core Pointer",
773                                   "type", GDK_DEVICE_TYPE_MASTER,
774                                   "input-source", GDK_SOURCE_MOUSE,
775                                   "input-mode", GDK_MODE_SCREEN,
776                                   "has-cursor", TRUE,
777                                   "display", display,
778                                   "device-manager", device_manager,
779                                   NULL);
780
781   device->keyboard = g_object_new (GDK_TYPE_DEVICE_CORE,
782                                    "name", "Core Keyboard",
783                                    "type", GDK_DEVICE_TYPE_MASTER,
784                                    "input-source", GDK_SOURCE_KEYBOARD,
785                                    "input-mode", GDK_MODE_SCREEN,
786                                    "has-cursor", FALSE,
787                                    "display", display,
788                                    "device-manager", device_manager,
789                                    NULL);
790
791   GDK_DEVICE_CORE (device->pointer)->device = device;
792   GDK_DEVICE_CORE (device->keyboard)->device = device;
793   device->device = wl_device;
794
795   wl_input_device_add_listener(device->device,
796                                &input_device_listener, device);
797
798   device->data_device =
799     wl_data_device_manager_get_data_device (display_wayland->data_device_manager,
800                                             device->device);
801   wl_data_device_add_listener (device->data_device,
802                                &data_device_listener, device);
803
804   device_manager_core->devices =
805     g_list_prepend (device_manager_core->devices, device->keyboard);
806   device_manager_core->devices =
807     g_list_prepend (device_manager_core->devices, device->pointer);
808
809   _gdk_device_set_associated_device (device->pointer, device->keyboard);
810   _gdk_device_set_associated_device (device->keyboard, device->pointer);
811 }
812
813 static void
814 free_device (void *data, void *user_data)
815 {
816   g_object_unref (data);
817 }
818
819 static void
820 gdk_device_manager_core_finalize (GObject *object)
821 {
822   GdkDeviceManagerCore *device_manager_core;
823
824   device_manager_core = GDK_DEVICE_MANAGER_CORE (object);
825
826   g_list_free_full (device_manager_core->devices, free_device);
827
828   G_OBJECT_CLASS (gdk_device_manager_core_parent_class)->finalize (object);
829 }
830
831 static GList *
832 gdk_device_manager_core_list_devices (GdkDeviceManager *device_manager,
833                                       GdkDeviceType     type)
834 {
835   GdkDeviceManagerCore *device_manager_core;
836   GList *devices = NULL;
837
838   if (type == GDK_DEVICE_TYPE_MASTER)
839     {
840       device_manager_core = (GdkDeviceManagerCore *) device_manager;
841       devices = g_list_copy(device_manager_core->devices);
842     }
843
844   return devices;
845 }
846
847 static GdkDevice *
848 gdk_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager)
849 {
850   GdkDeviceManagerCore *device_manager_core;
851
852   device_manager_core = (GdkDeviceManagerCore *) device_manager;
853   return device_manager_core->devices->data;
854 }
855
856 static void
857 gdk_device_manager_core_class_init (GdkDeviceManagerCoreClass *klass)
858 {
859   GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
860   GObjectClass *object_class = G_OBJECT_CLASS (klass);
861
862   object_class->finalize = gdk_device_manager_core_finalize;
863   device_manager_class->list_devices = gdk_device_manager_core_list_devices;
864   device_manager_class->get_client_pointer = gdk_device_manager_core_get_client_pointer;
865 }
866
867 static void
868 gdk_device_manager_core_init (GdkDeviceManagerCore *device_manager)
869 {
870 }
871
872 GdkDeviceManager *
873 _gdk_wayland_device_manager_new (GdkDisplay *display)
874 {
875   return g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
876                        "display", display,
877                        NULL);
878 }