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