]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkdevice-wayland.c
wayland: Set GdkWindow focused state based on keyboard focus
[~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       _gdk_wayland_window_remove_focus (device->keyboard_focus);
679       event = gdk_event_new (GDK_FOCUS_CHANGE);
680       event->focus_change.window = g_object_ref (device->keyboard_focus);
681       event->focus_change.send_event = FALSE;
682       event->focus_change.in = FALSE;
683       gdk_event_set_device (event, device->keyboard);
684
685       g_object_unref(device->keyboard_focus);
686       device->keyboard_focus = NULL;
687
688       GDK_NOTE (EVENTS,
689                 g_message ("focus out, device %p surface %p",
690                            device, device->keyboard_focus));
691
692       _gdk_wayland_display_deliver_event (device->display, event);
693     }
694
695   if (surface)
696     {
697       device->keyboard_focus = wl_surface_get_user_data(surface);
698       g_object_ref(device->keyboard_focus);
699
700       event = gdk_event_new (GDK_FOCUS_CHANGE);
701       event->focus_change.window = g_object_ref (device->keyboard_focus);
702       event->focus_change.send_event = FALSE;
703       event->focus_change.in = TRUE;
704       gdk_event_set_device (event, device->keyboard);
705
706       update_modifiers (device, keys);
707
708       GDK_NOTE (EVENTS,
709                 g_message ("focus int, device %p surface %p",
710                            device, device->keyboard_focus));
711
712       _gdk_wayland_display_deliver_event (device->display, event);
713
714       _gdk_wayland_window_add_focus (device->keyboard_focus);
715     }
716 }
717
718 static const struct wl_input_device_listener input_device_listener = {
719   input_handle_motion,
720   input_handle_button,
721   input_handle_key,
722   input_handle_pointer_focus,
723   input_handle_keyboard_focus,
724 };
725
726 struct _DataOffer {
727   struct wl_data_offer *offer;
728   gint ref_count;
729   GPtrArray *types;
730 };
731
732 static void
733 data_offer_offer (void                 *data,
734                   struct wl_data_offer *wl_data_offer,
735                   const char           *type)
736 {
737   DataOffer *offer = (DataOffer *)data;
738   g_debug (G_STRLOC ": %s wl_data_offer = %p type = %s",
739            G_STRFUNC, wl_data_offer, type);
740
741   g_ptr_array_add (offer->types, g_strdup (type));
742 }
743
744 static void
745 data_offer_unref (DataOffer *offer)
746 {
747   offer->ref_count--;
748
749   if (offer->ref_count == 0)
750     {
751       g_ptr_array_free (offer->types, TRUE);
752       g_free (offer);
753     }
754 }
755
756 static const struct wl_data_offer_listener data_offer_listener = {
757   data_offer_offer,
758 };
759
760 static void
761 data_device_data_offer (void                  *data,
762                         struct wl_data_device *data_device,
763                         uint32_t               id)
764 {
765   DataOffer *offer;
766
767   g_debug (G_STRLOC ": %s data_device = %p id = %lu",
768            G_STRFUNC, data_device, (long unsigned int)id);
769
770   /* This structure is reference counted to handle the case where you get a
771    * leave but are in the middle of transferring data
772    */
773   offer = g_new0 (DataOffer, 1);
774   offer->ref_count = 1;
775   offer->types = g_ptr_array_new_with_free_func (g_free);
776   offer->offer = (struct wl_data_offer *)
777     wl_proxy_create_for_id ((struct wl_proxy *) data_device,
778                             id,
779                             &wl_data_offer_interface);
780
781   /* The DataOffer structure is then retrieved later since this sets the user
782    * data.
783    */
784   wl_data_offer_add_listener (offer->offer,
785                               &data_offer_listener,
786                               offer);
787 }
788
789 static void
790 data_device_enter (void                  *data,
791                    struct wl_data_device *data_device,
792                    uint32_t               time,
793                    struct wl_surface     *surface,
794                    int32_t                x,
795                    int32_t                y,
796                    struct wl_data_offer  *offer)
797 {
798   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
799
800   g_debug (G_STRLOC ": %s data_device = %p time = %d, surface = %p, x = %d y = %d, offer = %p",
801            G_STRFUNC, data_device, time, surface, x, y, offer);
802
803   /* Retrieve the DataOffer associated with with the wl_data_offer - this
804    * association is made when the listener is attached.
805    */
806   g_assert (device->drag_offer == NULL);
807   device->drag_offer = wl_data_offer_get_user_data (offer);
808 }
809
810 static void
811 data_device_leave (void                  *data,
812                    struct wl_data_device *data_device)
813 {
814   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
815
816   g_debug (G_STRLOC ": %s data_device = %p",
817            G_STRFUNC, data_device);
818
819   data_offer_unref (device->drag_offer);
820   device->drag_offer = NULL;
821 }
822
823 static void
824 data_device_motion (void                  *data,
825                     struct wl_data_device *data_device,
826                     uint32_t               time,
827                     int32_t                x,
828                     int32_t                y)
829 {
830   g_debug (G_STRLOC ": %s data_device = %p, time = %d, x = %d, y = %d",
831            G_STRFUNC, data_device, time, x, y);
832 }
833
834 static void
835 data_device_drop (void                  *data,
836                   struct wl_data_device *data_device)
837 {
838   g_debug (G_STRLOC ": %s data_device = %p",
839            G_STRFUNC, data_device);
840 }
841
842 static void
843 data_device_selection (void                  *data,
844                        struct wl_data_device *wl_data_device,
845                        struct wl_data_offer  *offer)
846 {
847   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
848
849   g_debug (G_STRLOC ": %s wl_data_device = %p wl_data_offer = %p",
850            G_STRFUNC, wl_data_device, offer);
851
852   if (!offer)
853     {
854       if (device->selection_offer)
855         {
856           data_offer_unref (device->selection_offer);
857           device->selection_offer = NULL;
858         }
859
860       return;
861     }
862
863   if (device->selection_offer)
864     {
865       data_offer_unref (device->selection_offer);
866       device->selection_offer = NULL;
867     }
868
869   /* Retrieve the DataOffer associated with with the wl_data_offer - this
870    * association is made when the listener is attached.
871    */
872   g_assert (device->selection_offer == NULL);
873   device->selection_offer = wl_data_offer_get_user_data (offer);
874 }
875
876 static const struct wl_data_device_listener data_device_listener = {
877   data_device_data_offer,
878   data_device_enter,
879   data_device_leave,
880   data_device_motion,
881   data_device_drop,
882   data_device_selection
883 };
884
885 void
886 _gdk_wayland_device_manager_add_device (GdkDeviceManager *device_manager,
887                                         struct wl_input_device *wl_device)
888 {
889   GdkDisplay *display;
890   GdkDisplayWayland *display_wayland;
891   GdkDeviceManagerCore *device_manager_core =
892     GDK_DEVICE_MANAGER_CORE(device_manager);
893   GdkWaylandDevice *device;
894
895   device = g_new0 (GdkWaylandDevice, 1);
896   display = gdk_device_manager_get_display (device_manager);
897   display_wayland = GDK_DISPLAY_WAYLAND (display);
898
899   device->display = display;
900   device->pointer = g_object_new (GDK_TYPE_DEVICE_CORE,
901                                   "name", "Core Pointer",
902                                   "type", GDK_DEVICE_TYPE_MASTER,
903                                   "input-source", GDK_SOURCE_MOUSE,
904                                   "input-mode", GDK_MODE_SCREEN,
905                                   "has-cursor", TRUE,
906                                   "display", display,
907                                   "device-manager", device_manager,
908                                   NULL);
909
910   device->keyboard = g_object_new (GDK_TYPE_DEVICE_CORE,
911                                    "name", "Core Keyboard",
912                                    "type", GDK_DEVICE_TYPE_MASTER,
913                                    "input-source", GDK_SOURCE_KEYBOARD,
914                                    "input-mode", GDK_MODE_SCREEN,
915                                    "has-cursor", FALSE,
916                                    "display", display,
917                                    "device-manager", device_manager,
918                                    NULL);
919
920   GDK_DEVICE_CORE (device->pointer)->device = device;
921   GDK_DEVICE_CORE (device->keyboard)->device = device;
922   device->device = wl_device;
923
924   wl_input_device_add_listener(device->device,
925                                &input_device_listener, device);
926
927   device->data_device =
928     wl_data_device_manager_get_data_device (display_wayland->data_device_manager,
929                                             device->device);
930   wl_data_device_add_listener (device->data_device,
931                                &data_device_listener, device);
932
933   device_manager_core->devices =
934     g_list_prepend (device_manager_core->devices, device->keyboard);
935   device_manager_core->devices =
936     g_list_prepend (device_manager_core->devices, device->pointer);
937
938   _gdk_device_set_associated_device (device->pointer, device->keyboard);
939   _gdk_device_set_associated_device (device->keyboard, device->pointer);
940 }
941
942 static void
943 free_device (gpointer data)
944 {
945   g_object_unref (data);
946 }
947
948 static void
949 gdk_device_manager_core_finalize (GObject *object)
950 {
951   GdkDeviceManagerCore *device_manager_core;
952
953   device_manager_core = GDK_DEVICE_MANAGER_CORE (object);
954
955   g_list_free_full (device_manager_core->devices, free_device);
956
957   G_OBJECT_CLASS (gdk_device_manager_core_parent_class)->finalize (object);
958 }
959
960 static GList *
961 gdk_device_manager_core_list_devices (GdkDeviceManager *device_manager,
962                                       GdkDeviceType     type)
963 {
964   GdkDeviceManagerCore *device_manager_core;
965   GList *devices = NULL;
966
967   if (type == GDK_DEVICE_TYPE_MASTER)
968     {
969       device_manager_core = (GdkDeviceManagerCore *) device_manager;
970       devices = g_list_copy(device_manager_core->devices);
971     }
972
973   return devices;
974 }
975
976 static GdkDevice *
977 gdk_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager)
978 {
979   GdkDeviceManagerCore *device_manager_core;
980
981   device_manager_core = (GdkDeviceManagerCore *) device_manager;
982   return device_manager_core->devices->data;
983 }
984
985 static void
986 gdk_device_manager_core_class_init (GdkDeviceManagerCoreClass *klass)
987 {
988   GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
989   GObjectClass *object_class = G_OBJECT_CLASS (klass);
990
991   object_class->finalize = gdk_device_manager_core_finalize;
992   device_manager_class->list_devices = gdk_device_manager_core_list_devices;
993   device_manager_class->get_client_pointer = gdk_device_manager_core_get_client_pointer;
994 }
995
996 static void
997 gdk_device_manager_core_init (GdkDeviceManagerCore *device_manager)
998 {
999 }
1000
1001 GdkDeviceManager *
1002 _gdk_wayland_device_manager_new (GdkDisplay *display)
1003 {
1004   return g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
1005                        "display", display,
1006                        NULL);
1007 }
1008
1009 gint
1010 gdk_wayland_device_get_selection_type_atoms (GdkDevice  *gdk_device,
1011                                              GdkAtom   **atoms_out)
1012 {
1013   gint i;
1014   GdkAtom *atoms;
1015   GdkWaylandDevice *device;
1016
1017   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1018   g_return_val_if_fail (atoms_out != NULL, 0);
1019
1020   device = GDK_DEVICE_CORE (gdk_device)->device;
1021
1022   if (device->selection_offer->types->len == 0)
1023     {
1024       *atoms_out = NULL;
1025       return 0;
1026     }
1027
1028   atoms = g_new0 (GdkAtom, device->selection_offer->types->len);
1029
1030   /* Convert list of targets to atoms */
1031   for (i = 0; i < device->selection_offer->types->len; i++)
1032     {
1033       atoms[i] = gdk_atom_intern (device->selection_offer->types->pdata[i],
1034                                   FALSE);
1035       GDK_NOTE (MISC,
1036                 g_message (G_STRLOC ": Adding atom for %s",
1037                            (char *)device->selection_offer->types->pdata[i]));
1038     }
1039
1040   *atoms_out = atoms;
1041   return device->selection_offer->types->len;
1042 }
1043
1044 typedef struct
1045 {
1046   GdkWaylandDevice *device;
1047   DataOffer *offer;
1048   GIOChannel *channel;
1049   GdkDeviceWaylandRequestContentCallback cb;
1050   gpointer userdata;
1051 } RequestContentClosure;
1052
1053 static gboolean
1054 _request_content_io_func (GIOChannel *channel,
1055                           GIOCondition condition,
1056                           gpointer userdata)
1057 {
1058   RequestContentClosure *closure = (RequestContentClosure *)userdata;
1059   gchar *data = NULL;
1060   gsize len = 0;
1061   GError *error = NULL;
1062
1063   /* FIXME: We probably want to do something better than this to avoid
1064    * blocking on the transfer of large pieces of data: call the callback
1065    * multiple times I should think.
1066    */
1067   if (g_io_channel_read_to_end (channel,
1068                                 &data,
1069                                 &len,
1070                                 &error) != G_IO_STATUS_NORMAL)
1071     {
1072       g_warning (G_STRLOC ": Error reading content from pipe: %s", error->message);
1073       g_clear_error (&error);
1074     }
1075
1076   /* Since we use _read_to_end we've got a guaranteed EOF and thus can go
1077    * ahead and close the fd
1078    */
1079   g_io_channel_shutdown (channel, TRUE, NULL);
1080
1081   closure->cb (closure->device->pointer, data, len, closure->userdata);
1082
1083   g_free (data);
1084   data_offer_unref (closure->offer);
1085   g_io_channel_unref (channel);
1086   g_free (closure);
1087
1088   return FALSE;
1089 }
1090
1091 gboolean
1092 gdk_wayland_device_request_selection_content (GdkDevice                              *gdk_device,
1093                                               const gchar                            *requested_mime_type,
1094                                               GdkDeviceWaylandRequestContentCallback  cb,
1095                                               gpointer                                userdata)
1096 {
1097   int pipe_fd[2];
1098   RequestContentClosure *closure;
1099   GdkWaylandDevice *device;
1100   GError *error = NULL;
1101
1102   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), FALSE);
1103   g_return_val_if_fail (requested_mime_type != NULL, FALSE);
1104   g_return_val_if_fail (cb != NULL, FALSE);
1105
1106   device = GDK_DEVICE_CORE (gdk_device)->device;
1107
1108   if (!device->selection_offer)
1109       return FALSE;
1110
1111   /* TODO: Check mimetypes */
1112
1113   closure = g_new0 (RequestContentClosure, 1);
1114
1115   device->selection_offer->ref_count++;
1116
1117   pipe2 (pipe_fd, O_CLOEXEC);
1118   wl_data_offer_receive (device->selection_offer->offer,
1119                          requested_mime_type,
1120                          pipe_fd[1]);
1121   close (pipe_fd[1]);
1122
1123   closure->device = device;
1124   closure->offer = device->selection_offer;
1125   closure->channel = g_io_channel_unix_new (pipe_fd[0]);
1126   closure->cb = cb;
1127   closure->userdata = userdata;
1128
1129   if (!g_io_channel_set_encoding (closure->channel, NULL, &error))
1130     {
1131       g_warning (G_STRLOC ": Error setting encoding on channel: %s",
1132                  error->message);
1133       g_clear_error (&error);
1134       goto error;
1135     }
1136
1137   g_io_add_watch (closure->channel,
1138                   G_IO_IN,
1139                   _request_content_io_func,
1140                   closure);
1141
1142   return TRUE;
1143
1144 error:
1145   data_offer_unref (closure->offer);
1146   g_io_channel_unref (closure->channel);
1147   close (pipe_fd[1]);
1148   g_free (closure);
1149
1150   return FALSE;
1151 }
1152
1153 struct _GdkWaylandSelectionOffer {
1154   GdkDeviceWaylandOfferContentCallback cb;
1155   gpointer userdata;
1156   struct wl_data_source *source;
1157   GdkWaylandDevice *device;
1158 };
1159
1160 static void
1161 data_source_target (void                  *data,
1162                     struct wl_data_source *source,
1163                     const char            *mime_type)
1164 {
1165   g_debug (G_STRLOC ": %s source = %p, mime_type = %s",
1166            G_STRFUNC, source, mime_type);
1167 }
1168
1169 static void
1170 data_source_send (void                  *data,
1171                   struct wl_data_source *source,
1172                   const char            *mime_type,
1173                   int32_t                fd)
1174 {
1175   GdkWaylandSelectionOffer *offer = (GdkWaylandSelectionOffer *)data;;
1176   gchar *buf;
1177   gssize len, bytes_written = 0;
1178
1179   g_debug (G_STRLOC ": %s source = %p, mime_type = %s fd = %d",
1180            G_STRFUNC, source, mime_type, fd);
1181
1182   buf = offer->cb (offer->device->pointer, mime_type, &len, offer->userdata);
1183
1184   while (len > 0)
1185     {
1186       bytes_written += write (fd, buf + bytes_written, len);
1187       if (bytes_written == -1)
1188         goto error;
1189       len -= bytes_written;
1190     }
1191
1192   close (fd);
1193   g_free (buf);
1194
1195   return;
1196 error:
1197
1198   g_warning (G_STRLOC ": Error writing data to client: %s",
1199              g_strerror (errno));
1200
1201   close (fd);
1202   g_free (buf);
1203 }
1204
1205 static void
1206 data_source_cancelled (void                  *data,
1207                        struct wl_data_source *source)
1208 {
1209   g_debug (G_STRLOC ": %s source = %p",
1210            G_STRFUNC, source);
1211 }
1212
1213 static const struct wl_data_source_listener data_source_listener = {
1214     data_source_target,
1215     data_source_send,
1216     data_source_cancelled
1217 };
1218
1219 static guint32
1220 _wl_time_now (void)
1221 {
1222   struct timeval tv;
1223
1224   gettimeofday(&tv, NULL);
1225
1226   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1227 }
1228
1229 gboolean
1230 gdk_wayland_device_offer_selection_content (GdkDevice                             *gdk_device,
1231                                             const gchar                          **mime_types,
1232                                             gint                                   nr_mime_types,
1233                                             GdkDeviceWaylandOfferContentCallback   cb,
1234                                             gpointer                               userdata)
1235 {
1236   GdkDisplay *display;
1237   GdkDisplayWayland *display_wayland;
1238   GdkWaylandSelectionOffer *offer;
1239   GdkWaylandDevice *device;
1240   gint i;
1241
1242   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1243   device = GDK_DEVICE_CORE (gdk_device)->device;
1244
1245   display = device->display;
1246   display_wayland = GDK_DISPLAY_WAYLAND (display);
1247
1248   offer = g_new0 (GdkWaylandSelectionOffer, 1);
1249   offer->cb = cb;
1250   offer->userdata = userdata;
1251   offer->source =
1252     wl_data_device_manager_create_data_source (display_wayland->data_device_manager);
1253   offer->device = device;
1254
1255   for (i = 0; i < nr_mime_types; i++)
1256     {
1257       wl_data_source_offer (offer->source,
1258                             mime_types[i]);
1259     }
1260
1261   wl_data_source_add_listener (offer->source,
1262                                &data_source_listener,
1263                                offer);
1264
1265   wl_data_device_set_selection (device->data_device,
1266                                 offer->source,
1267                                 _wl_time_now ());
1268
1269   device->selection_offer_out = offer;
1270
1271   return TRUE;
1272 }
1273
1274 gboolean
1275 gdk_wayland_device_clear_selection_content (GdkDevice *gdk_device)
1276 {
1277   GdkWaylandDevice *device;
1278
1279   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1280   device = GDK_DEVICE_CORE (gdk_device)->device;
1281
1282   if (!device->selection_offer_out)
1283     return FALSE;
1284
1285   wl_data_device_set_selection (device->data_device,
1286                                 NULL,
1287                                 _wl_time_now ());
1288
1289   wl_data_source_destroy (device->selection_offer_out->source);
1290   g_free (device->selection_offer_out);
1291   device->selection_offer_out = NULL;
1292
1293   return TRUE;
1294 }