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