]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkdevice-wayland.c
cef1ddfc2c386ac618b4e8aee32e9815efc673b8
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _GNU_SOURCE
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <errno.h>
22
23 #include "config.h"
24
25 #include <string.h>
26 #include <gdk/gdkwindow.h>
27 #include <gdk/gdktypes.h>
28 #include "gdkprivate-wayland.h"
29 #include "gdkwayland.h"
30 #include "gdkkeysyms.h"
31 #include "gdkdeviceprivate.h"
32 #include "gdkdevicemanagerprivate.h"
33 #include "gdkprivate-wayland.h"
34
35 #include <xkbcommon/xkbcommon.h>
36 #include <X11/keysym.h>
37
38 #include <sys/time.h>
39 #include <sys/mman.h>
40
41 #define GDK_TYPE_DEVICE_CORE         (gdk_device_core_get_type ())
42 #define GDK_DEVICE_CORE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE_CORE, GdkDeviceCore))
43 #define GDK_DEVICE_CORE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_DEVICE_CORE, GdkDeviceCoreClass))
44 #define GDK_IS_DEVICE_CORE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE_CORE))
45 #define GDK_IS_DEVICE_CORE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_DEVICE_CORE))
46 #define GDK_DEVICE_CORE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_DEVICE_CORE, GdkDeviceCoreClass))
47
48 typedef struct _GdkDeviceCore GdkDeviceCore;
49 typedef struct _GdkDeviceCoreClass GdkDeviceCoreClass;
50 typedef struct _GdkWaylandDevice GdkWaylandDevice;
51
52 typedef struct _DataOffer DataOffer;
53
54 typedef struct _GdkWaylandSelectionOffer GdkWaylandSelectionOffer;
55
56 struct _GdkWaylandDevice
57 {
58   struct wl_seat *wl_seat;
59   struct wl_pointer *wl_pointer;
60   struct wl_keyboard *wl_keyboard;
61
62   GdkDisplay *display;
63   GdkDeviceManager *device_manager;
64
65   GdkDevice *pointer;
66   GdkDevice *keyboard;
67
68   GdkKeymap *keymap;
69
70   GdkModifierType modifiers;
71   GdkWindow *pointer_focus;
72   GdkWindow *keyboard_focus;
73   struct wl_data_device *data_device;
74   double surface_x, surface_y;
75   uint32_t time;
76   GdkWindow *pointer_grab_window;
77   uint32_t pointer_grab_time;
78   guint32 repeat_timer;
79   guint32 repeat_key;
80   guint32 repeat_count;
81
82   DataOffer *drag_offer;
83   DataOffer *selection_offer;
84
85   GdkWaylandSelectionOffer *selection_offer_out;
86
87   struct wl_surface *pointer_surface;
88 };
89
90 struct _GdkDeviceCore
91 {
92   GdkDevice parent_instance;
93   GdkWaylandDevice *device;
94 };
95
96 struct _GdkDeviceCoreClass
97 {
98   GdkDeviceClass parent_class;
99 };
100
101 G_DEFINE_TYPE (GdkDeviceCore, gdk_device_core, GDK_TYPE_DEVICE)
102
103 #define GDK_TYPE_DEVICE_MANAGER_CORE         (gdk_device_manager_core_get_type ())
104 #define GDK_DEVICE_MANAGER_CORE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCore))
105 #define GDK_DEVICE_MANAGER_CORE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCoreClass))
106 #define GDK_IS_DEVICE_MANAGER_CORE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE_MANAGER_CORE))
107 #define GDK_IS_DEVICE_MANAGER_CORE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), GDK_TYPE_DEVICE_MANAGER_CORE))
108 #define GDK_DEVICE_MANAGER_CORE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GDK_TYPE_DEVICE_MANAGER_CORE, GdkDeviceManagerCoreClass))
109
110 typedef struct _GdkDeviceManagerCore GdkDeviceManagerCore;
111 typedef struct _GdkDeviceManagerCoreClass GdkDeviceManagerCoreClass;
112
113 struct _GdkDeviceManagerCore
114 {
115   GdkDeviceManager parent_object;
116   GdkDevice *core_pointer;
117   GdkDevice *core_keyboard;
118   GList *devices;
119 };
120
121 struct _GdkDeviceManagerCoreClass
122 {
123   GdkDeviceManagerClass parent_class;
124 };
125
126 G_DEFINE_TYPE (GdkDeviceManagerCore,
127                gdk_device_manager_core, GDK_TYPE_DEVICE_MANAGER)
128
129 static gboolean
130 gdk_device_core_get_history (GdkDevice      *device,
131                              GdkWindow      *window,
132                              guint32         start,
133                              guint32         stop,
134                              GdkTimeCoord ***events,
135                              gint           *n_events)
136 {
137   return FALSE;
138 }
139
140 static void
141 gdk_device_core_get_state (GdkDevice       *device,
142                            GdkWindow       *window,
143                            gdouble         *axes,
144                            GdkModifierType *mask)
145 {
146   gint x_int, y_int;
147
148   gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
149
150   if (axes)
151     {
152       axes[0] = x_int;
153       axes[1] = y_int;
154     }
155 }
156
157 static void
158 gdk_device_core_set_window_cursor (GdkDevice *device,
159                                    GdkWindow *window,
160                                    GdkCursor *cursor)
161 {
162   GdkWaylandDevice *wd = GDK_DEVICE_CORE(device)->device;
163   GdkWaylandDisplay *wayland_display =
164     GDK_WAYLAND_DISPLAY (gdk_window_get_display (window));
165   struct wl_buffer *buffer;
166   int x, y, w, h;
167
168   if (cursor)
169     g_object_ref (cursor);
170
171   /* Setting the cursor to NULL means that we should use the default cursor */
172   if (!cursor)
173     {
174       /* FIXME: Is this the best sensible default ? */
175       cursor = _gdk_wayland_display_get_cursor_for_type (device->display,
176                                                          GDK_LEFT_PTR);
177     }
178
179   buffer = _gdk_wayland_cursor_get_buffer (cursor, &x, &y, &w, &h);
180   wl_pointer_set_cursor (wd->wl_pointer,
181                          _gdk_wayland_display_get_serial (wayland_display),
182                          wd->pointer_surface,
183                          x, y);
184   wl_surface_attach (wd->pointer_surface, buffer, 0, 0);
185   wl_surface_damage (wd->pointer_surface,  0, 0, w, h);
186   wl_surface_commit(wd->pointer_surface);
187
188   g_object_unref (cursor);
189 }
190
191 static void
192 gdk_device_core_warp (GdkDevice *device,
193                       GdkScreen *screen,
194                       gint       x,
195                       gint       y)
196 {
197 }
198
199 static void
200 gdk_device_core_query_state (GdkDevice        *device,
201                              GdkWindow        *window,
202                              GdkWindow       **root_window,
203                              GdkWindow       **child_window,
204                              gint             *root_x,
205                              gint             *root_y,
206                              gint             *win_x,
207                              gint             *win_y,
208                              GdkModifierType  *mask)
209 {
210   GdkWaylandDevice *wd;
211   GdkScreen *default_screen;
212
213   wd = GDK_DEVICE_CORE(device)->device;
214   default_screen = gdk_display_get_default_screen (wd->display);
215
216   if (root_window)
217     *root_window = gdk_screen_get_root_window (default_screen);
218   if (child_window)
219     *child_window = wd->pointer_focus;
220   /* Do something clever for relative here */
221 #if 0
222   if (root_x)
223     *root_x = wd->x;
224   if (root_y)
225     *root_y = wd->y;
226 #endif
227   if (win_x)
228     *win_x = wd->surface_x;
229   if (win_y)
230     *win_y = wd->surface_y;
231   if (mask)
232     *mask = wd->modifiers;
233 }
234
235 static GdkGrabStatus
236 gdk_device_core_grab (GdkDevice    *device,
237                       GdkWindow    *window,
238                       gboolean      owner_events,
239                       GdkEventMask  event_mask,
240                       GdkWindow    *confine_to,
241                       GdkCursor    *cursor,
242                       guint32       time_)
243 {
244   GdkWaylandDevice *wayland_device = GDK_DEVICE_CORE (device)->device;
245
246   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
247     {
248       /* Device is a keyboard */
249       return GDK_GRAB_SUCCESS;
250     }
251   else
252     {
253       /* Device is a pointer */
254
255       if (wayland_device->pointer_grab_window != NULL &&
256           time_ != 0 && wayland_device->pointer_grab_time > time_)
257         {
258           return GDK_GRAB_ALREADY_GRABBED;
259         }
260
261       if (time_ == 0)
262         time_ = wayland_device->time;
263
264       wayland_device->pointer_grab_window = window;
265       wayland_device->pointer_grab_time = time_;
266
267       /* FIXME: This probably breaks if you end up with multiple grabs on the
268        * same window - but we need to know the input device for when we are
269        * asked to map a popup window so that the grab can be managed by the
270        * compositor.
271        */
272       _gdk_wayland_window_set_device_grabbed (window,
273                                               wayland_device->wl_seat,
274                                               time_);
275     }
276
277   return GDK_GRAB_SUCCESS;
278 }
279
280 static void
281 gdk_device_core_ungrab (GdkDevice *device,
282                         guint32    time_)
283 {
284   GdkWaylandDevice *wayland_device = GDK_DEVICE_CORE (device)->device;
285   GdkDisplay *display;
286   GdkDeviceGrabInfo *grab;
287
288   display = gdk_device_get_display (device);
289
290   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
291     {
292       /* Device is a keyboard */
293     }
294   else
295     {
296       /* Device is a pointer */
297       grab = _gdk_display_get_last_device_grab (display, device);
298
299       if (grab)
300         grab->serial_end = grab->serial_start;
301
302       if (wayland_device->pointer_grab_window)
303         _gdk_wayland_window_set_device_grabbed (wayland_device->pointer_grab_window,
304                                                 NULL,
305                                                 0);
306     }
307 }
308
309 static GdkWindow *
310 gdk_device_core_window_at_position (GdkDevice       *device,
311                                     gint            *win_x,
312                                     gint            *win_y,
313                                     GdkModifierType *mask,
314                                     gboolean         get_toplevel)
315 {
316   GdkWaylandDevice *wd;
317
318   wd = GDK_DEVICE_CORE(device)->device;
319   if (win_x)
320     *win_x = wd->surface_x;
321   if (win_y)
322     *win_y = wd->surface_y;
323   if (mask)
324     *mask = wd->modifiers;
325
326   return wd->pointer_focus;
327 }
328
329 static void
330 gdk_device_core_select_window_events (GdkDevice    *device,
331                                       GdkWindow    *window,
332                                       GdkEventMask  event_mask)
333 {
334 }
335
336 static void
337 gdk_device_core_class_init (GdkDeviceCoreClass *klass)
338 {
339   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
340
341   device_class->get_history = gdk_device_core_get_history;
342   device_class->get_state = gdk_device_core_get_state;
343   device_class->set_window_cursor = gdk_device_core_set_window_cursor;
344   device_class->warp = gdk_device_core_warp;
345   device_class->query_state = gdk_device_core_query_state;
346   device_class->grab = gdk_device_core_grab;
347   device_class->ungrab = gdk_device_core_ungrab;
348   device_class->window_at_position = gdk_device_core_window_at_position;
349   device_class->select_window_events = gdk_device_core_select_window_events;
350 }
351
352 static void
353 gdk_device_core_init (GdkDeviceCore *device_core)
354 {
355   GdkDevice *device;
356
357   device = GDK_DEVICE (device_core);
358
359   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
360   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
361 }
362
363 struct wl_seat *
364 _gdk_wayland_device_get_wl_seat (GdkDevice *device)
365 {
366   return GDK_DEVICE_CORE (device)->device->wl_seat;
367 }
368
369 struct wl_pointer *
370 _gdk_wayland_device_get_wl_pointer (GdkDevice *device)
371 {
372   return GDK_DEVICE_CORE (device)->device->wl_pointer;
373 }
374
375
376 struct wl_keyboard *
377 _gdk_wayland_device_get_wl_keyboard (GdkDevice *device)
378 {
379   return GDK_DEVICE_CORE (device)->device->wl_keyboard;
380 }
381
382 GdkKeymap *
383 _gdk_wayland_device_get_keymap (GdkDevice *device)
384 {
385   return GDK_DEVICE_CORE (device)->device->keymap;
386 }
387
388 #if 0
389 static void
390 input_handle_motion(void *data, struct wl_input_device *input_device,
391                     uint32_t time,
392                     int32_t x, int32_t y, int32_t sx, int32_t sy)
393 {
394   GdkWaylandDevice *device = data;
395   GdkWaylandDisplay *display = GDK_WAYLAND_DISPLAY (device->display);
396   GdkEvent *event;
397
398   event = gdk_event_new (GDK_NOTHING);
399
400   device->time = time;
401   device->x = x;
402   device->y = y;
403   device->surface_x = sx;
404   device->surface_y = sy;
405
406   event->motion.type = GDK_MOTION_NOTIFY;
407   event->motion.window = g_object_ref (device->pointer_focus);
408   gdk_event_set_device (event, device->pointer);
409   event->motion.time = time;
410   event->motion.x = (gdouble) sx;
411   event->motion.y = (gdouble) sy;
412   event->motion.x_root = (gdouble) x;
413   event->motion.y_root = (gdouble) y;
414   event->motion.axes = NULL;
415   event->motion.state = device->modifiers;
416   event->motion.is_hint = 0;
417   gdk_event_set_screen (event, display->screen);
418
419   GDK_NOTE (EVENTS,
420             g_message ("motion %d %d, state %d",
421                        sx, sy, event->button.state));
422
423   _gdk_wayland_display_deliver_event (device->display, event);
424 }
425
426 static void
427 input_handle_button(void *data, struct wl_input_device *input_device,
428                      uint32_t time, uint32_t button, uint32_t state)
429 {
430   GdkWaylandDevice *device = data;
431   GdkWaylandDisplay *display = GDK_WAYLAND_DISPLAY (device->display);
432   GdkEvent *event;
433   uint32_t modifier;
434   int gdk_button;
435
436   switch (button) {
437   case 273:
438     gdk_button = 3;
439     break;
440   case 274:
441     gdk_button = 2;
442     break;
443   default:
444     gdk_button = button - 271;
445     break;
446   }
447
448   device->time = time;
449   event = gdk_event_new (state ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
450   event->button.window = g_object_ref (device->pointer_focus);
451   gdk_event_set_device (event, device->pointer);
452   event->button.time = time;
453   event->button.x = (gdouble) device->surface_x;
454   event->button.y = (gdouble) device->surface_y;
455   event->button.x_root = (gdouble) device->x;
456   event->button.y_root = (gdouble) device->y;
457   event->button.axes = NULL;
458   event->button.state = device->modifiers;
459   event->button.button = gdk_button;
460   gdk_event_set_screen (event, display->screen);
461
462   modifier = 1 << (8 + gdk_button - 1);
463   if (state)
464     device->modifiers |= modifier;
465   else
466     device->modifiers &= ~modifier;
467
468   GDK_NOTE (EVENTS,
469             g_message ("button %d %s, state %d",
470                        event->button.button,
471                        state ? "press" : "release", event->button.state));
472
473   _gdk_wayland_display_deliver_event (device->display, event);
474 }
475
476 static void
477 translate_keyboard_string (GdkEventKey *event)
478 {
479   gunichar c = 0;
480   gchar buf[7];
481
482   /* Fill in event->string crudely, since various programs
483    * depend on it.
484    */
485   event->string = NULL;
486
487   if (event->keyval != GDK_KEY_VoidSymbol)
488     c = gdk_keyval_to_unicode (event->keyval);
489
490   if (c)
491     {
492       gsize bytes_written;
493       gint len;
494
495       /* Apply the control key - Taken from Xlib
496        */
497       if (event->state & GDK_CONTROL_MASK)
498         {
499           if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
500           else if (c == '2')
501             {
502               event->string = g_memdup ("\0\0", 2);
503               event->length = 1;
504               buf[0] = '\0';
505               return;
506             }
507           else if (c >= '3' && c <= '7') c -= ('3' - '\033');
508           else if (c == '8') c = '\177';
509           else if (c == '/') c = '_' & 0x1F;
510         }
511
512       len = g_unichar_to_utf8 (c, buf);
513       buf[len] = '\0';
514
515       event->string = g_locale_from_utf8 (buf, len,
516                                           NULL, &bytes_written,
517                                           NULL);
518       if (event->string)
519         event->length = bytes_written;
520     }
521   else if (event->keyval == GDK_KEY_Escape)
522     {
523       event->length = 1;
524       event->string = g_strdup ("\033");
525     }
526   else if (event->keyval == GDK_KEY_Return ||
527           event->keyval == GDK_KEY_KP_Enter)
528     {
529       event->length = 1;
530       event->string = g_strdup ("\r");
531     }
532
533   if (!event->string)
534     {
535       event->length = 0;
536       event->string = g_strdup ("");
537     }
538 }
539
540 static gboolean
541 keyboard_repeat (gpointer data);
542
543 static gboolean
544 deliver_key_event(GdkWaylandDevice *device,
545                   uint32_t time, uint32_t key, uint32_t state)
546 {
547   GdkEvent *event;
548   uint32_t code, modifier, level;
549   struct xkb_desc *xkb;
550   GdkKeymap *keymap;
551
552   keymap = gdk_keymap_get_for_display (device->display);
553   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
554
555   device->time = time;
556   event = gdk_event_new (state ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
557   event->key.window = g_object_ref (device->keyboard_focus);
558   gdk_event_set_device (event, device->keyboard);
559   event->button.time = time;
560   event->key.state = device->modifiers;
561   event->key.group = 0;
562   code = key + xkb->min_key_code;
563   event->key.hardware_keycode = code;
564
565   level = 0;
566   if (device->modifiers & XKB_COMMON_SHIFT_MASK &&
567       XkbKeyGroupWidth(xkb, code, 0) > 1)
568     level = 1;
569
570   event->key.keyval = XkbKeySymEntry(xkb, code, level, 0);
571
572   modifier = xkb->map->modmap[code];
573   if (state)
574     device->modifiers |= modifier;
575   else
576     device->modifiers &= ~modifier;
577
578   event->key.is_modifier = modifier > 0;
579
580   translate_keyboard_string (&event->key);
581
582   _gdk_wayland_display_deliver_event (device->display, event);
583
584   GDK_NOTE (EVENTS,
585             g_message ("keyboard event, code %d, sym %d, "
586                        "string %s, mods 0x%x",
587                        code, event->key.keyval,
588                        event->key.string, event->key.state));
589
590   device->repeat_count++;
591   device->repeat_key = key;
592
593   if (state == 0)
594     {
595       if (device->repeat_timer)
596         {
597           g_source_remove (device->repeat_timer);
598           device->repeat_timer = 0;
599         }
600       return FALSE;
601     }
602   else if (modifier)
603     {
604       return FALSE;
605     }
606   else switch (device->repeat_count)
607     {
608     case 1:
609       if (device->repeat_timer)
610         {
611           g_source_remove (device->repeat_timer);
612           device->repeat_timer = 0;
613         }
614
615       device->repeat_timer =
616         gdk_threads_add_timeout (400, keyboard_repeat, device);
617       return TRUE;
618     case 2:
619       device->repeat_timer =
620         gdk_threads_add_timeout (80, keyboard_repeat, device);
621       return FALSE;
622     default:
623       return TRUE;
624     }
625 }
626
627 static gboolean
628 keyboard_repeat (gpointer data)
629 {
630   GdkWaylandDevice *device = data;
631
632   return deliver_key_event (device, device->time, device->repeat_key, 1);
633 }
634
635 static void
636 input_handle_key(void *data, struct wl_input_device *input_device,
637                  uint32_t time, uint32_t key, uint32_t state)
638 {
639   GdkWaylandDevice *device = data;
640
641   device->repeat_count = 0;
642   deliver_key_event (data, time, key, state);
643 }
644
645 static void
646 input_handle_pointer_focus(void *data,
647                            struct wl_input_device *input_device,
648                            uint32_t time, struct wl_surface *surface,
649                            int32_t x, int32_t y, int32_t sx, int32_t sy)
650 {
651   GdkWaylandDevice *device = data;
652   GdkEvent *event;
653
654   device->time = time;
655   if (device->pointer_focus)
656     {
657       event = gdk_event_new (GDK_LEAVE_NOTIFY);
658       event->crossing.window = g_object_ref (device->pointer_focus);
659       gdk_event_set_device (event, device->pointer);
660       event->crossing.subwindow = NULL;
661       event->crossing.time = time;
662       event->crossing.x = (gdouble) device->surface_x;
663       event->crossing.y = (gdouble) device->surface_y;
664       event->crossing.x_root = (gdouble) device->x;
665       event->crossing.y_root = (gdouble) device->y;
666
667       event->crossing.mode = GDK_CROSSING_NORMAL;
668       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
669       event->crossing.focus = TRUE;
670       event->crossing.state = 0;
671
672       _gdk_wayland_display_deliver_event (device->display, event);
673
674       GDK_NOTE (EVENTS,
675                 g_message ("leave, device %p surface %p",
676                            device, device->pointer_focus));
677
678       g_object_unref(device->pointer_focus);
679       device->pointer_focus = NULL;
680     }
681
682   if (surface)
683     {
684       device->pointer_focus = wl_surface_get_user_data(surface);
685       g_object_ref(device->pointer_focus);
686
687       event = gdk_event_new (GDK_ENTER_NOTIFY);
688       event->crossing.window = g_object_ref (device->pointer_focus);
689       gdk_event_set_device (event, device->pointer);
690       event->crossing.subwindow = NULL;
691       event->crossing.time = time;
692       event->crossing.x = (gdouble) sx;
693       event->crossing.y = (gdouble) sy;
694       event->crossing.x_root = (gdouble) x;
695       event->crossing.y_root = (gdouble) y;
696
697       event->crossing.mode = GDK_CROSSING_NORMAL;
698       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
699       event->crossing.focus = TRUE;
700       event->crossing.state = 0;
701
702       device->surface_x = sx;
703       device->surface_y = sy;
704       device->x = x;
705       device->y = y;
706
707       _gdk_wayland_display_deliver_event (device->display, event);
708
709       GDK_NOTE (EVENTS,
710                 g_message ("enter, device %p surface %p",
711                            device, device->pointer_focus));
712     }
713 }
714
715 static void
716 update_modifiers(GdkWaylandDevice *device, struct wl_array *keys)
717 {
718   uint32_t *k, *end;
719   GdkKeymap *keymap;
720   struct xkb_desc *xkb;
721
722   keymap = gdk_keymap_get_for_display (device->display);
723   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
724
725   end = keys->data + keys->size;
726   device->modifiers = 0;
727   for (k = keys->data; k < end; k++)
728     device->modifiers |= xkb->map->modmap[*k];
729 }
730
731 static void
732 input_handle_keyboard_focus(void *data,
733                             struct wl_input_device *input_device,
734                             uint32_t time,
735                             struct wl_surface *surface,
736                             struct wl_array *keys)
737 {
738   GdkWaylandDevice *device = data;
739   GdkEvent *event;
740
741   device->time = time;
742   if (device->keyboard_focus)
743     {
744       _gdk_wayland_window_remove_focus (device->keyboard_focus);
745       event = gdk_event_new (GDK_FOCUS_CHANGE);
746       event->focus_change.window = g_object_ref (device->keyboard_focus);
747       event->focus_change.send_event = FALSE;
748       event->focus_change.in = FALSE;
749       gdk_event_set_device (event, device->keyboard);
750
751       g_object_unref(device->keyboard_focus);
752       device->keyboard_focus = NULL;
753
754       GDK_NOTE (EVENTS,
755                 g_message ("focus out, device %p surface %p",
756                            device, device->keyboard_focus));
757
758       _gdk_wayland_display_deliver_event (device->display, event);
759     }
760
761   if (surface)
762     {
763       device->keyboard_focus = wl_surface_get_user_data(surface);
764       g_object_ref(device->keyboard_focus);
765
766       event = gdk_event_new (GDK_FOCUS_CHANGE);
767       event->focus_change.window = g_object_ref (device->keyboard_focus);
768       event->focus_change.send_event = FALSE;
769       event->focus_change.in = TRUE;
770       gdk_event_set_device (event, device->keyboard);
771
772       update_modifiers (device, keys);
773
774       GDK_NOTE (EVENTS,
775                 g_message ("focus int, device %p surface %p",
776                            device, device->keyboard_focus));
777
778       _gdk_wayland_display_deliver_event (device->display, event);
779
780       _gdk_wayland_window_add_focus (device->keyboard_focus);
781     }
782 }
783
784 static const struct wl_input_device_listener input_device_listener = {
785   input_handle_motion,
786   input_handle_button,
787   input_handle_key,
788   input_handle_pointer_focus,
789   input_handle_keyboard_focus,
790 };
791 #endif
792
793 struct _DataOffer {
794   struct wl_data_offer *offer;
795   gint ref_count;
796   GPtrArray *types;
797 };
798
799 static void
800 data_offer_offer (void                 *data,
801                   struct wl_data_offer *wl_data_offer,
802                   const char           *type)
803 {
804   DataOffer *offer = (DataOffer *)data;
805   g_debug (G_STRLOC ": %s wl_data_offer = %p type = %s",
806            G_STRFUNC, wl_data_offer, type);
807
808   g_ptr_array_add (offer->types, g_strdup (type));
809 }
810
811 static void
812 data_offer_unref (DataOffer *offer)
813 {
814   offer->ref_count--;
815
816   if (offer->ref_count == 0)
817     {
818       g_ptr_array_free (offer->types, TRUE);
819       g_free (offer);
820     }
821 }
822
823 static const struct wl_data_offer_listener data_offer_listener = {
824   data_offer_offer,
825 };
826
827 static void
828 data_device_data_offer (void                  *data,
829                         struct wl_data_device *data_device,
830                         struct wl_data_offer  *_offer)
831 {
832   DataOffer *offer;
833
834   /* This structure is reference counted to handle the case where you get a
835    * leave but are in the middle of transferring data
836    */
837   offer = g_new0 (DataOffer, 1);
838   offer->ref_count = 1;
839   offer->types = g_ptr_array_new_with_free_func (g_free);
840   offer->offer = _offer;
841
842   /* The DataOffer structure is then retrieved later since this sets the user
843    * data.
844    */
845   wl_data_offer_add_listener (offer->offer,
846                               &data_offer_listener,
847                               offer);
848 }
849
850 static void
851 data_device_enter (void                  *data,
852                    struct wl_data_device *data_device,
853                    uint32_t               time,
854                    struct wl_surface     *surface,
855                    int32_t                x,
856                    int32_t                y,
857                    struct wl_data_offer  *offer)
858 {
859   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
860
861   g_debug (G_STRLOC ": %s data_device = %p time = %d, surface = %p, x = %d y = %d, offer = %p",
862            G_STRFUNC, data_device, time, surface, x, y, offer);
863
864   /* Retrieve the DataOffer associated with with the wl_data_offer - this
865    * association is made when the listener is attached.
866    */
867   g_assert (device->drag_offer == NULL);
868   device->drag_offer = wl_data_offer_get_user_data (offer);
869 }
870
871 static void
872 data_device_leave (void                  *data,
873                    struct wl_data_device *data_device)
874 {
875   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
876
877   g_debug (G_STRLOC ": %s data_device = %p",
878            G_STRFUNC, data_device);
879
880   data_offer_unref (device->drag_offer);
881   device->drag_offer = NULL;
882 }
883
884 static void
885 data_device_motion (void                  *data,
886                     struct wl_data_device *data_device,
887                     uint32_t               time,
888                     int32_t                x,
889                     int32_t                y)
890 {
891   g_debug (G_STRLOC ": %s data_device = %p, time = %d, x = %d, y = %d",
892            G_STRFUNC, data_device, time, x, y);
893 }
894
895 static void
896 data_device_drop (void                  *data,
897                   struct wl_data_device *data_device)
898 {
899   g_debug (G_STRLOC ": %s data_device = %p",
900            G_STRFUNC, data_device);
901 }
902
903 static void
904 data_device_selection (void                  *data,
905                        struct wl_data_device *wl_data_device,
906                        struct wl_data_offer  *offer)
907 {
908   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
909
910   g_debug (G_STRLOC ": %s wl_data_device = %p wl_data_offer = %p",
911            G_STRFUNC, wl_data_device, offer);
912
913   if (!offer)
914     {
915       if (device->selection_offer)
916         {
917           data_offer_unref (device->selection_offer);
918           device->selection_offer = NULL;
919         }
920
921       return;
922     }
923
924   if (device->selection_offer)
925     {
926       data_offer_unref (device->selection_offer);
927       device->selection_offer = NULL;
928     }
929
930   /* Retrieve the DataOffer associated with with the wl_data_offer - this
931    * association is made when the listener is attached.
932    */
933   g_assert (device->selection_offer == NULL);
934   device->selection_offer = wl_data_offer_get_user_data (offer);
935 }
936
937 static const struct wl_data_device_listener data_device_listener = {
938   data_device_data_offer,
939   data_device_enter,
940   data_device_leave,
941   data_device_motion,
942   data_device_drop,
943   data_device_selection
944 };
945
946
947
948 static void
949 pointer_handle_enter (void              *data,
950                       struct wl_pointer *pointer,
951                       uint32_t           serial,
952                       struct wl_surface *surface,
953                       wl_fixed_t         sx,
954                       wl_fixed_t         sy)
955 {
956   GdkWaylandDevice *device = data;
957   GdkEvent *event;
958   GdkWaylandDisplay *wayland_display =
959     GDK_WAYLAND_DISPLAY (device->display);
960
961   _gdk_wayland_display_update_serial (wayland_display, serial);
962
963   device->pointer_focus = wl_surface_get_user_data(surface);
964   g_object_ref(device->pointer_focus);
965
966   event = gdk_event_new (GDK_ENTER_NOTIFY);
967   event->crossing.window = g_object_ref (device->pointer_focus);
968   gdk_event_set_device (event, device->pointer);
969   event->crossing.subwindow = NULL;
970   event->crossing.time = (guint32)(g_get_monotonic_time () / 1000);
971   event->crossing.x = wl_fixed_to_double (sx);
972   event->crossing.y = wl_fixed_to_double (sy);
973
974   event->crossing.mode = GDK_CROSSING_NORMAL;
975   event->crossing.detail = GDK_NOTIFY_ANCESTOR;
976   event->crossing.focus = TRUE;
977   event->crossing.state = 0;
978
979   device->surface_x = wl_fixed_to_double (sx);
980   device->surface_y = wl_fixed_to_double (sy);
981
982   _gdk_wayland_display_deliver_event (device->display, event);
983
984   GDK_NOTE (EVENTS,
985             g_message ("enter, device %p surface %p",
986                        device, device->pointer_focus));
987 }
988
989 static void
990 pointer_handle_leave (void              *data,
991                       struct wl_pointer *pointer,
992                       uint32_t           serial,
993                       struct wl_surface *surface)
994 {
995   GdkWaylandDevice *device = data;
996   GdkEvent *event;
997   GdkWaylandDisplay *wayland_display =
998     GDK_WAYLAND_DISPLAY (device->display);
999
1000   _gdk_wayland_display_update_serial (wayland_display, serial);
1001
1002   event = gdk_event_new (GDK_LEAVE_NOTIFY);
1003   event->crossing.window = g_object_ref (device->pointer_focus);
1004   gdk_event_set_device (event, device->pointer);
1005   event->crossing.subwindow = NULL;
1006   event->crossing.time = (guint32)(g_get_monotonic_time () / 1000);
1007   event->crossing.x = device->surface_x;
1008   event->crossing.y = device->surface_y;
1009
1010   event->crossing.mode = GDK_CROSSING_NORMAL;
1011   event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1012   event->crossing.focus = TRUE;
1013   event->crossing.state = 0;
1014
1015   _gdk_wayland_display_deliver_event (device->display, event);
1016
1017   GDK_NOTE (EVENTS,
1018             g_message ("leave, device %p surface %p",
1019                        device, device->pointer_focus));
1020
1021   g_object_unref(device->pointer_focus);
1022   device->pointer_focus = NULL;
1023 }
1024
1025 static void
1026 pointer_handle_motion (void              *data,
1027                        struct wl_pointer *pointer,
1028                        uint32_t           time,
1029                        wl_fixed_t         sx,
1030                        wl_fixed_t         sy)
1031 {
1032   GdkWaylandDevice *device = data;
1033   GdkWaylandDisplay *display = GDK_WAYLAND_DISPLAY (device->display);
1034   GdkEvent *event;
1035
1036   event = gdk_event_new (GDK_NOTHING);
1037
1038   device->time = time;
1039   device->surface_x = wl_fixed_to_double (sx);
1040   device->surface_y = wl_fixed_to_double (sy);
1041
1042   event->motion.type = GDK_MOTION_NOTIFY;
1043   event->motion.window = g_object_ref (device->pointer_focus);
1044   gdk_event_set_device (event, device->pointer);
1045   event->motion.time = time;
1046   event->motion.x = wl_fixed_to_double (sx);
1047   event->motion.y = wl_fixed_to_double (sy);
1048   event->motion.axes = NULL;
1049   event->motion.state = device->modifiers;
1050   event->motion.is_hint = 0;
1051   gdk_event_set_screen (event, display->screen);
1052
1053   GDK_NOTE (EVENTS,
1054             g_message ("motion %d %d, state %d",
1055                        sx, sy, event->button.state));
1056
1057   _gdk_wayland_display_deliver_event (device->display, event);
1058 }
1059
1060 static void
1061 pointer_handle_button (void              *data,
1062                        struct wl_pointer *pointer,
1063                        uint32_t           serial,
1064                        uint32_t           time,
1065                        uint32_t           button,
1066                        uint32_t           state)
1067 {
1068   GdkWaylandDevice *device = data;
1069   GdkWaylandDisplay *display = GDK_WAYLAND_DISPLAY (device->display);
1070   GdkEvent *event;
1071   uint32_t modifier;
1072   int gdk_button;
1073   GdkWaylandDisplay *wayland_display =
1074     GDK_WAYLAND_DISPLAY (device->display);
1075
1076   _gdk_wayland_display_update_serial (wayland_display, serial);
1077
1078   switch (button) {
1079   case 273:
1080     gdk_button = 3;
1081     break;
1082   case 274:
1083     gdk_button = 2;
1084     break;
1085   default:
1086     gdk_button = button - 271;
1087     break;
1088   }
1089
1090   device->time = time;
1091
1092   event = gdk_event_new (state ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
1093   event->button.window = g_object_ref (device->pointer_focus);
1094   gdk_event_set_device (event, device->pointer);
1095   event->button.time = time;
1096   event->button.x = device->surface_x;
1097   event->button.y = device->surface_y;
1098   event->button.axes = NULL;
1099   event->button.state = device->modifiers;
1100   event->button.button = gdk_button;
1101   gdk_event_set_screen (event, display->screen);
1102
1103   modifier = 1 << (8 + gdk_button - 1);
1104   if (state)
1105     device->modifiers |= modifier;
1106   else
1107     device->modifiers &= ~modifier;
1108
1109   GDK_NOTE (EVENTS,
1110             g_message ("button %d %s, state %d",
1111                        event->button.button,
1112                        state ? "press" : "release", event->button.state));
1113
1114   _gdk_wayland_display_deliver_event (device->display, event);
1115 }
1116
1117 static void
1118 pointer_handle_axis (void              *data,
1119                      struct wl_pointer *pointer,
1120                      uint32_t           time,
1121                      uint32_t           axis,
1122                      wl_fixed_t         value)
1123 {
1124   GdkWaylandDevice *device = data;
1125   GdkWaylandDisplay *display = GDK_WAYLAND_DISPLAY (device->display);
1126   GdkEvent *event;
1127   gdouble delta_x, delta_y;
1128
1129   switch (axis) {
1130   case WL_POINTER_AXIS_VERTICAL_SCROLL:
1131     delta_x = 0;
1132     delta_y = -wl_fixed_to_double (value);
1133     break;
1134   case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
1135     delta_x = -wl_fixed_to_double (value);
1136     delta_y = 0;
1137   default:
1138     g_return_if_reached ();
1139   }
1140
1141   device->time = time;
1142   event = gdk_event_new (GDK_SCROLL);
1143   event->scroll.window = g_object_ref (device->pointer_focus);
1144   gdk_event_set_device (event, device->pointer);
1145   event->scroll.time = time;
1146   event->scroll.x = (gdouble) device->surface_x;
1147   event->scroll.y = (gdouble) device->surface_y;
1148   event->scroll.direction = GDK_SCROLL_SMOOTH;
1149   event->scroll.delta_x = delta_x;
1150   event->scroll.delta_y = delta_y;
1151   event->scroll.state = device->modifiers;
1152   gdk_event_set_screen (event, display->screen);
1153
1154   GDK_NOTE (EVENTS,
1155             g_message ("scroll %f %f",
1156                        event->scroll.delta_x, event->scroll.delta_y));
1157
1158   _gdk_wayland_display_deliver_event (device->display, event);
1159 }
1160
1161 static void
1162 keyboard_handle_keymap (void               *data,
1163                        struct wl_keyboard *keyboard,
1164                        uint32_t            format,
1165                        int                 fd,
1166                        uint32_t            size)
1167 {
1168   GdkWaylandDevice *device = data;
1169   if (device->keymap)
1170     g_object_unref (device->keymap);
1171
1172   device->keymap = _gdk_wayland_keymap_new_from_fd (format, fd, size);
1173 }
1174
1175 static void
1176 keyboard_handle_enter (void               *data,
1177                        struct wl_keyboard *keyboard,
1178                        uint32_t            serial,
1179                        struct wl_surface  *surface,
1180                        struct wl_array    *keys)
1181 {
1182   GdkWaylandDevice *device = data;
1183   GdkEvent *event;
1184   GdkWaylandDisplay *wayland_display =
1185     GDK_WAYLAND_DISPLAY (device->display);
1186
1187   _gdk_wayland_display_update_serial (wayland_display, serial);
1188
1189   device->keyboard_focus = wl_surface_get_user_data(surface);
1190   g_object_ref(device->keyboard_focus);
1191
1192   event = gdk_event_new (GDK_FOCUS_CHANGE);
1193   event->focus_change.window = g_object_ref (device->keyboard_focus);
1194   event->focus_change.send_event = FALSE;
1195   event->focus_change.in = TRUE;
1196   gdk_event_set_device (event, device->keyboard);
1197
1198   GDK_NOTE (EVENTS,
1199             g_message ("focus int, device %p surface %p",
1200                        device, device->keyboard_focus));
1201
1202   _gdk_wayland_display_deliver_event (device->display, event);
1203
1204   _gdk_wayland_window_add_focus (device->keyboard_focus);
1205 }
1206
1207 static void
1208 keyboard_handle_leave (void               *data,
1209                        struct wl_keyboard *keyboard,
1210                        uint32_t            serial,
1211                        struct wl_surface  *surface)
1212 {
1213   GdkWaylandDevice *device = data;
1214   GdkEvent *event;
1215   GdkWaylandDisplay *wayland_display =
1216     GDK_WAYLAND_DISPLAY (device->display);
1217
1218   _gdk_wayland_display_update_serial (wayland_display, serial);
1219
1220   _gdk_wayland_window_remove_focus (device->keyboard_focus);
1221   event = gdk_event_new (GDK_FOCUS_CHANGE);
1222   event->focus_change.window = g_object_ref (device->keyboard_focus);
1223   event->focus_change.send_event = FALSE;
1224   event->focus_change.in = FALSE;
1225   gdk_event_set_device (event, device->keyboard);
1226
1227   g_object_unref(device->keyboard_focus);
1228   device->keyboard_focus = NULL;
1229
1230   GDK_NOTE (EVENTS,
1231             g_message ("focus out, device %p surface %p",
1232                        device, device->keyboard_focus));
1233
1234   _gdk_wayland_display_deliver_event (device->display, event);
1235 }
1236
1237 static gboolean
1238 keyboard_repeat (gpointer data);
1239
1240 static GdkModifierType
1241 get_modifier (struct xkb_state *state)
1242 {
1243   GdkModifierType modifiers = 0;
1244   modifiers |= (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_SHIFT, XKB_STATE_EFFECTIVE) > 0)?GDK_SHIFT_MASK:0;
1245   modifiers |= (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_CAPS, XKB_STATE_EFFECTIVE) > 0)?GDK_LOCK_MASK:0;
1246   modifiers |= (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_CTRL, XKB_STATE_EFFECTIVE) > 0)?GDK_CONTROL_MASK:0;
1247   modifiers |= (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_ALT, XKB_STATE_EFFECTIVE) > 0)?GDK_MOD1_MASK:0;
1248   modifiers |= (xkb_state_mod_name_is_active (state, "Mod2", XKB_STATE_EFFECTIVE) > 0)?GDK_MOD2_MASK:0;
1249   modifiers |= (xkb_state_mod_name_is_active (state, "Mod3", XKB_STATE_EFFECTIVE) > 0)?GDK_MOD3_MASK:0;
1250   modifiers |= (xkb_state_mod_name_is_active (state, XKB_MOD_NAME_LOGO, XKB_STATE_EFFECTIVE) > 0)?GDK_MOD4_MASK:0;
1251   modifiers |= (xkb_state_mod_name_is_active (state, "Mod5", XKB_STATE_EFFECTIVE) > 0)?GDK_MOD5_MASK:0;
1252
1253   return modifiers;
1254 }
1255
1256 static void
1257 translate_keyboard_string (GdkEventKey *event)
1258 {
1259   gunichar c = 0;
1260   gchar buf[7];
1261
1262   /* Fill in event->string crudely, since various programs
1263    * depend on it.
1264    */
1265   event->string = NULL;
1266
1267   if (event->keyval != GDK_KEY_VoidSymbol)
1268     c = gdk_keyval_to_unicode (event->keyval);
1269
1270   if (c)
1271     {
1272       gsize bytes_written;
1273       gint len;
1274
1275       /* Apply the control key - Taken from Xlib
1276        */
1277       if (event->state & GDK_CONTROL_MASK)
1278         {
1279           if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
1280           else if (c == '2')
1281             {
1282               event->string = g_memdup ("\0\0", 2);
1283               event->length = 1;
1284               buf[0] = '\0';
1285               return;
1286             }
1287           else if (c >= '3' && c <= '7') c -= ('3' - '\033');
1288           else if (c == '8') c = '\177';
1289           else if (c == '/') c = '_' & 0x1F;
1290         }
1291
1292       len = g_unichar_to_utf8 (c, buf);
1293       buf[len] = '\0';
1294
1295       event->string = g_locale_from_utf8 (buf, len,
1296                                           NULL, &bytes_written,
1297                                           NULL);
1298       if (event->string)
1299         event->length = bytes_written;
1300     }
1301   else if (event->keyval == GDK_KEY_Escape)
1302     {
1303       event->length = 1;
1304       event->string = g_strdup ("\033");
1305     }
1306   else if (event->keyval == GDK_KEY_Return ||
1307           event->keyval == GDK_KEY_KP_Enter)
1308     {
1309       event->length = 1;
1310       event->string = g_strdup ("\r");
1311     }
1312
1313   if (!event->string)
1314     {
1315       event->length = 0;
1316       event->string = g_strdup ("");
1317     }
1318 }
1319
1320 static gboolean
1321 deliver_key_event(GdkWaylandDevice *device,
1322                   uint32_t time, uint32_t key, uint32_t state)
1323 {
1324   GdkEvent *event;
1325   struct xkb_state *xkb_state;
1326   GdkKeymap *keymap;
1327   xkb_keysym_t sym;
1328   uint32_t num_syms;
1329   const xkb_keysym_t *syms;
1330
1331   keymap = device->keymap;
1332   xkb_state = _gdk_wayland_keymap_get_xkb_state (keymap);
1333
1334   num_syms = xkb_key_get_syms (xkb_state, key, &syms);
1335   sym = XKB_KEY_NoSymbol;
1336   if (num_syms == 1)
1337     sym = syms[0];
1338
1339   device->time = time;
1340   device->modifiers = get_modifier (xkb_state);
1341
1342   event = gdk_event_new (state ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
1343   event->key.window = device->keyboard_focus?g_object_ref (device->keyboard_focus):NULL;
1344   gdk_event_set_device (event, device->keyboard);
1345   event->button.time = time;
1346   event->key.state = device->modifiers;
1347   event->key.group = 0;
1348   event->key.hardware_keycode = sym;
1349   event->key.keyval = sym;
1350
1351   event->key.is_modifier = device->modifiers > 0;
1352
1353   translate_keyboard_string (&event->key);
1354
1355   _gdk_wayland_display_deliver_event (device->display, event);
1356
1357   GDK_NOTE (EVENTS,
1358             g_message ("keyboard event, code %d, sym %d, "
1359                        "string %s, mods 0x%x",
1360                        event->key.hardware_keycode, event->key.keyval,
1361                        event->key.string, event->key.state));
1362
1363   device->repeat_count++;
1364   device->repeat_key = key;
1365
1366   if (state == 0)
1367     {
1368       if (device->repeat_timer)
1369         {
1370           g_source_remove (device->repeat_timer);
1371           device->repeat_timer = 0;
1372         }
1373       return FALSE;
1374     }
1375   else if (device->modifiers)
1376     {
1377       return FALSE;
1378     }
1379   else switch (device->repeat_count)
1380     {
1381     case 1:
1382       if (device->repeat_timer)
1383         {
1384           g_source_remove (device->repeat_timer);
1385           device->repeat_timer = 0;
1386         }
1387
1388       device->repeat_timer =
1389         gdk_threads_add_timeout (400, keyboard_repeat, device);
1390       return TRUE;
1391     case 2:
1392       device->repeat_timer =
1393         gdk_threads_add_timeout (80, keyboard_repeat, device);
1394       return FALSE;
1395     default:
1396       return TRUE;
1397     }
1398 }
1399
1400 static gboolean
1401 keyboard_repeat (gpointer data)
1402 {
1403   GdkWaylandDevice *device = data;
1404
1405   return deliver_key_event (device, device->time, device->repeat_key, 1);
1406 }
1407
1408 static void
1409 keyboard_handle_key (void               *data,
1410                      struct wl_keyboard *keyboard,
1411                      uint32_t            serial,
1412                      uint32_t            time,
1413                      uint32_t            key,
1414                      uint32_t            state_w)
1415 {
1416   GdkWaylandDevice *device = data;
1417   GdkWaylandDisplay *wayland_display =
1418     GDK_WAYLAND_DISPLAY (device->display);
1419
1420   device->repeat_count = 0;
1421   _gdk_wayland_display_update_serial (wayland_display, serial);
1422   deliver_key_event (data, time, key + 8, state_w);
1423 }
1424
1425 static void
1426 keyboard_handle_modifiers (void               *data,
1427                            struct wl_keyboard *keyboard,
1428                            uint32_t            serial,
1429                            uint32_t            mods_depressed,
1430                            uint32_t            mods_latched,
1431                            uint32_t            mods_locked,
1432                            uint32_t            group)
1433 {
1434   GdkWaylandDevice *device = data;
1435   GdkKeymap *keymap;
1436   struct xkb_state *xkb_state;
1437
1438   keymap = device->keymap;
1439   xkb_state = _gdk_wayland_keymap_get_xkb_state (keymap);
1440   device->modifiers = mods_depressed | mods_latched | mods_locked;
1441
1442   xkb_state_update_mask (xkb_state, mods_depressed, mods_latched, mods_locked, group, 0, 0);
1443 }
1444
1445 static const struct wl_pointer_listener pointer_listener = {
1446     pointer_handle_enter,
1447     pointer_handle_leave,
1448     pointer_handle_motion,
1449     pointer_handle_button,
1450     pointer_handle_axis,
1451 };
1452
1453 static const struct wl_keyboard_listener keyboard_listener = {
1454     keyboard_handle_keymap,
1455     keyboard_handle_enter,
1456     keyboard_handle_leave,
1457     keyboard_handle_key,
1458     keyboard_handle_modifiers,
1459 };
1460
1461 static void
1462 seat_handle_capabilities(void *data, struct wl_seat *seat,
1463                          enum wl_seat_capability caps)
1464 {
1465   GdkWaylandDevice *device = data;
1466   GdkDeviceManagerCore *device_manager_core =
1467     GDK_DEVICE_MANAGER_CORE(device->device_manager);
1468
1469   if ((caps & WL_SEAT_CAPABILITY_POINTER) && !device->wl_pointer)
1470     {
1471       device->wl_pointer = wl_seat_get_pointer(seat);
1472       wl_pointer_set_user_data(device->wl_pointer, device);
1473       wl_pointer_add_listener(device->wl_pointer, &pointer_listener,
1474                               device);
1475
1476       device->pointer = g_object_new (GDK_TYPE_DEVICE_CORE,
1477                                       "name", "Core Pointer",
1478                                       "type", GDK_DEVICE_TYPE_MASTER,
1479                                       "input-source", GDK_SOURCE_MOUSE,
1480                                       "input-mode", GDK_MODE_SCREEN,
1481                                       "has-cursor", TRUE,
1482                                       "display", device->display,
1483                                       "device-manager", device->device_manager,
1484                                       NULL);
1485       GDK_DEVICE_CORE (device->pointer)->device = device;
1486
1487       device_manager_core->devices =
1488         g_list_prepend (device_manager_core->devices, device->pointer);
1489     }
1490   else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && device->wl_pointer)
1491     {
1492       wl_pointer_destroy(device->wl_pointer);
1493       device->wl_pointer = NULL;
1494
1495       device_manager_core->devices =
1496         g_list_remove (device_manager_core->devices, device->pointer);
1497
1498       g_object_unref (device->pointer);
1499       device->pointer = NULL;
1500     }
1501
1502   if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !device->wl_keyboard) 
1503     {
1504       device->wl_keyboard = wl_seat_get_keyboard(seat);
1505       wl_keyboard_set_user_data(device->wl_keyboard, device);
1506       wl_keyboard_add_listener(device->wl_keyboard, &keyboard_listener,
1507                                device);
1508
1509       device->keyboard = g_object_new (GDK_TYPE_DEVICE_CORE,
1510                                        "name", "Core Keyboard",
1511                                        "type", GDK_DEVICE_TYPE_MASTER,
1512                                        "input-source", GDK_SOURCE_KEYBOARD,
1513                                        "input-mode", GDK_MODE_SCREEN,
1514                                        "has-cursor", FALSE,
1515                                        "display", device->display,
1516                                        "device-manager", device->device_manager,
1517                                        NULL);
1518       GDK_DEVICE_CORE (device->keyboard)->device = device;
1519
1520       device_manager_core->devices =
1521         g_list_prepend (device_manager_core->devices, device->keyboard);
1522     }
1523   else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && device->wl_keyboard) 
1524     {
1525       wl_keyboard_destroy(device->wl_keyboard);
1526       device->wl_keyboard = NULL;
1527
1528       device_manager_core->devices =
1529         g_list_remove (device_manager_core->devices, device->keyboard);
1530
1531       g_object_unref (device->keyboard);
1532       device->keyboard = NULL;
1533     }
1534
1535   if (device->keyboard && device->pointer)
1536     {
1537       _gdk_device_set_associated_device (device->pointer, device->keyboard);
1538       _gdk_device_set_associated_device (device->keyboard, device->pointer);
1539     }
1540 }
1541
1542 static const struct wl_seat_listener seat_listener = {
1543     seat_handle_capabilities,
1544 };
1545
1546 void
1547 _gdk_wayland_device_manager_add_device (GdkDeviceManager *device_manager,
1548                                         struct wl_seat *wl_seat)
1549 {
1550   GdkDisplay *display;
1551   GdkWaylandDisplay *display_wayland;
1552   GdkWaylandDevice *device;
1553
1554   display = gdk_device_manager_get_display (device_manager);
1555   display_wayland = GDK_WAYLAND_DISPLAY (display);
1556
1557   device = g_new0 (GdkWaylandDevice, 1);
1558   device->keymap = _gdk_wayland_keymap_new ();
1559   device->display = display;
1560   device->device_manager = device_manager;
1561
1562   device->wl_seat = wl_seat;
1563
1564   wl_seat_add_listener (device->wl_seat, &seat_listener, device);
1565   wl_seat_set_user_data (device->wl_seat, device);
1566
1567   device->data_device =
1568     wl_data_device_manager_get_data_device (display_wayland->data_device_manager,
1569                                             device->wl_seat);
1570   wl_data_device_add_listener (device->data_device,
1571                                &data_device_listener, device);
1572
1573   device->pointer_surface =
1574     wl_compositor_create_surface (display_wayland->compositor);
1575 }
1576
1577 static void
1578 free_device (gpointer data)
1579 {
1580   g_object_unref (data);
1581 }
1582
1583 static void
1584 gdk_device_manager_core_finalize (GObject *object)
1585 {
1586   GdkDeviceManagerCore *device_manager_core;
1587
1588   device_manager_core = GDK_DEVICE_MANAGER_CORE (object);
1589
1590   g_list_free_full (device_manager_core->devices, free_device);
1591
1592   G_OBJECT_CLASS (gdk_device_manager_core_parent_class)->finalize (object);
1593 }
1594
1595 static GList *
1596 gdk_device_manager_core_list_devices (GdkDeviceManager *device_manager,
1597                                       GdkDeviceType     type)
1598 {
1599   GdkDeviceManagerCore *device_manager_core;
1600   GList *devices = NULL;
1601
1602   if (type == GDK_DEVICE_TYPE_MASTER)
1603     {
1604       device_manager_core = (GdkDeviceManagerCore *) device_manager;
1605       devices = g_list_copy(device_manager_core->devices);
1606     }
1607
1608   return devices;
1609 }
1610
1611 static GdkDevice *
1612 gdk_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager)
1613 {
1614   GdkDeviceManagerCore *device_manager_core;
1615   GList *l;
1616
1617   device_manager_core = (GdkDeviceManagerCore *) device_manager;
1618
1619   /* Find the first pointer device */
1620   for (l = device_manager_core->devices; l != NULL; l = l->next)
1621     {
1622       GdkDevice *device = l->data;
1623
1624       if (gdk_device_get_source (device) == GDK_SOURCE_MOUSE)
1625         return device;
1626     }
1627
1628   return NULL;
1629 }
1630
1631 static void
1632 gdk_device_manager_core_class_init (GdkDeviceManagerCoreClass *klass)
1633 {
1634   GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
1635   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1636
1637   object_class->finalize = gdk_device_manager_core_finalize;
1638   device_manager_class->list_devices = gdk_device_manager_core_list_devices;
1639   device_manager_class->get_client_pointer = gdk_device_manager_core_get_client_pointer;
1640 }
1641
1642 static void
1643 gdk_device_manager_core_init (GdkDeviceManagerCore *device_manager)
1644 {
1645 }
1646
1647 GdkDeviceManager *
1648 _gdk_wayland_device_manager_new (GdkDisplay *display)
1649 {
1650   return g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
1651                        "display", display,
1652                        NULL);
1653 }
1654
1655 gint
1656 gdk_wayland_device_get_selection_type_atoms (GdkDevice  *gdk_device,
1657                                              GdkAtom   **atoms_out)
1658 {
1659   gint i;
1660   GdkAtom *atoms;
1661   GdkWaylandDevice *device;
1662
1663   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1664   g_return_val_if_fail (atoms_out != NULL, 0);
1665
1666   device = GDK_DEVICE_CORE (gdk_device)->device;
1667
1668   if (!device->selection_offer || device->selection_offer->types->len == 0)
1669     {
1670       *atoms_out = NULL;
1671       return 0;
1672     }
1673
1674   atoms = g_new0 (GdkAtom, device->selection_offer->types->len);
1675
1676   /* Convert list of targets to atoms */
1677   for (i = 0; i < device->selection_offer->types->len; i++)
1678     {
1679       atoms[i] = gdk_atom_intern (device->selection_offer->types->pdata[i],
1680                                   FALSE);
1681       GDK_NOTE (MISC,
1682                 g_message (G_STRLOC ": Adding atom for %s",
1683                            (char *)device->selection_offer->types->pdata[i]));
1684     }
1685
1686   *atoms_out = atoms;
1687   return device->selection_offer->types->len;
1688 }
1689
1690 typedef struct
1691 {
1692   GdkWaylandDevice *device;
1693   DataOffer *offer;
1694   GIOChannel *channel;
1695   GdkDeviceWaylandRequestContentCallback cb;
1696   gpointer userdata;
1697 } RequestContentClosure;
1698
1699 static gboolean
1700 _request_content_io_func (GIOChannel *channel,
1701                           GIOCondition condition,
1702                           gpointer userdata)
1703 {
1704   RequestContentClosure *closure = (RequestContentClosure *)userdata;
1705   gchar *data = NULL;
1706   gsize len = 0;
1707   GError *error = NULL;
1708
1709   /* FIXME: We probably want to do something better than this to avoid
1710    * blocking on the transfer of large pieces of data: call the callback
1711    * multiple times I should think.
1712    */
1713   if (g_io_channel_read_to_end (channel,
1714                                 &data,
1715                                 &len,
1716                                 &error) != G_IO_STATUS_NORMAL)
1717     {
1718       g_warning (G_STRLOC ": Error reading content from pipe: %s", error->message);
1719       g_clear_error (&error);
1720     }
1721
1722   /* Since we use _read_to_end we've got a guaranteed EOF and thus can go
1723    * ahead and close the fd
1724    */
1725   g_io_channel_shutdown (channel, TRUE, NULL);
1726
1727   closure->cb (closure->device->pointer, data, len, closure->userdata);
1728
1729   g_free (data);
1730   data_offer_unref (closure->offer);
1731   g_io_channel_unref (channel);
1732   g_free (closure);
1733
1734   return FALSE;
1735 }
1736
1737 gboolean
1738 gdk_wayland_device_request_selection_content (GdkDevice                              *gdk_device,
1739                                               const gchar                            *requested_mime_type,
1740                                               GdkDeviceWaylandRequestContentCallback  cb,
1741                                               gpointer                                userdata)
1742 {
1743   int pipe_fd[2];
1744   RequestContentClosure *closure;
1745   GdkWaylandDevice *device;
1746   GError *error = NULL;
1747
1748   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), FALSE);
1749   g_return_val_if_fail (requested_mime_type != NULL, FALSE);
1750   g_return_val_if_fail (cb != NULL, FALSE);
1751
1752   device = GDK_DEVICE_CORE (gdk_device)->device;
1753
1754   if (!device->selection_offer)
1755       return FALSE;
1756
1757   /* TODO: Check mimetypes */
1758
1759   closure = g_new0 (RequestContentClosure, 1);
1760
1761   device->selection_offer->ref_count++;
1762
1763   pipe2 (pipe_fd, O_CLOEXEC);
1764   wl_data_offer_receive (device->selection_offer->offer,
1765                          requested_mime_type,
1766                          pipe_fd[1]);
1767   close (pipe_fd[1]);
1768
1769   closure->device = device;
1770   closure->offer = device->selection_offer;
1771   closure->channel = g_io_channel_unix_new (pipe_fd[0]);
1772   closure->cb = cb;
1773   closure->userdata = userdata;
1774
1775   if (!g_io_channel_set_encoding (closure->channel, NULL, &error))
1776     {
1777       g_warning (G_STRLOC ": Error setting encoding on channel: %s",
1778                  error->message);
1779       g_clear_error (&error);
1780       goto error;
1781     }
1782
1783   g_io_add_watch (closure->channel,
1784                   G_IO_IN,
1785                   _request_content_io_func,
1786                   closure);
1787
1788   return TRUE;
1789
1790 error:
1791   data_offer_unref (closure->offer);
1792   g_io_channel_unref (closure->channel);
1793   close (pipe_fd[1]);
1794   g_free (closure);
1795
1796   return FALSE;
1797 }
1798
1799 struct _GdkWaylandSelectionOffer {
1800   GdkDeviceWaylandOfferContentCallback cb;
1801   gpointer userdata;
1802   struct wl_data_source *source;
1803   GdkWaylandDevice *device;
1804 };
1805
1806 static void
1807 data_source_target (void                  *data,
1808                     struct wl_data_source *source,
1809                     const char            *mime_type)
1810 {
1811   g_debug (G_STRLOC ": %s source = %p, mime_type = %s",
1812            G_STRFUNC, source, mime_type);
1813 }
1814
1815 static void
1816 data_source_send (void                  *data,
1817                   struct wl_data_source *source,
1818                   const char            *mime_type,
1819                   int32_t                fd)
1820 {
1821   GdkWaylandSelectionOffer *offer = (GdkWaylandSelectionOffer *)data;;
1822   gchar *buf;
1823   gssize len, bytes_written = 0;
1824
1825   g_debug (G_STRLOC ": %s source = %p, mime_type = %s fd = %d",
1826            G_STRFUNC, source, mime_type, fd);
1827
1828   buf = offer->cb (offer->device->pointer, mime_type, &len, offer->userdata);
1829
1830   while (len > 0)
1831     {
1832       bytes_written += write (fd, buf + bytes_written, len);
1833       if (bytes_written == -1)
1834         goto error;
1835       len -= bytes_written;
1836     }
1837
1838   close (fd);
1839   g_free (buf);
1840
1841   return;
1842 error:
1843
1844   g_warning (G_STRLOC ": Error writing data to client: %s",
1845              g_strerror (errno));
1846
1847   close (fd);
1848   g_free (buf);
1849 }
1850
1851 static void
1852 data_source_cancelled (void                  *data,
1853                        struct wl_data_source *source)
1854 {
1855   g_debug (G_STRLOC ": %s source = %p",
1856            G_STRFUNC, source);
1857 }
1858
1859 static const struct wl_data_source_listener data_source_listener = {
1860     data_source_target,
1861     data_source_send,
1862     data_source_cancelled
1863 };
1864
1865 static guint32
1866 _wl_time_now (void)
1867 {
1868   struct timeval tv;
1869
1870   gettimeofday(&tv, NULL);
1871
1872   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1873 }
1874
1875 gboolean
1876 gdk_wayland_device_offer_selection_content (GdkDevice                             *gdk_device,
1877                                             const gchar                          **mime_types,
1878                                             gint                                   nr_mime_types,
1879                                             GdkDeviceWaylandOfferContentCallback   cb,
1880                                             gpointer                               userdata)
1881 {
1882   GdkDisplay *display;
1883   GdkWaylandDisplay *display_wayland;
1884   GdkWaylandSelectionOffer *offer;
1885   GdkWaylandDevice *device;
1886   gint i;
1887
1888   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1889   device = GDK_DEVICE_CORE (gdk_device)->device;
1890
1891   display = device->display;
1892   display_wayland = GDK_WAYLAND_DISPLAY (display);
1893
1894   offer = g_new0 (GdkWaylandSelectionOffer, 1);
1895   offer->cb = cb;
1896   offer->userdata = userdata;
1897   offer->source =
1898     wl_data_device_manager_create_data_source (display_wayland->data_device_manager);
1899   offer->device = device;
1900
1901   for (i = 0; i < nr_mime_types; i++)
1902     {
1903       wl_data_source_offer (offer->source,
1904                             mime_types[i]);
1905     }
1906
1907   wl_data_source_add_listener (offer->source,
1908                                &data_source_listener,
1909                                offer);
1910
1911   wl_data_device_set_selection (device->data_device,
1912                                 offer->source,
1913                                 _wl_time_now ());
1914
1915   device->selection_offer_out = offer;
1916
1917   return TRUE;
1918 }
1919
1920 gboolean
1921 gdk_wayland_device_clear_selection_content (GdkDevice *gdk_device)
1922 {
1923   GdkWaylandDevice *device;
1924
1925   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1926   device = GDK_DEVICE_CORE (gdk_device)->device;
1927
1928   if (!device->selection_offer_out)
1929     return FALSE;
1930
1931   wl_data_device_set_selection (device->data_device,
1932                                 NULL,
1933                                 _wl_time_now ());
1934
1935   wl_data_source_destroy (device->selection_offer_out->source);
1936   g_free (device->selection_offer_out);
1937   device->selection_offer_out = NULL;
1938
1939   return TRUE;
1940 }