]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkdevice-wayland.c
wayland: Report middle and right buttons correctly
[~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   int gdk_button;
382
383   switch (button) {
384   case 273:
385     gdk_button = 3;
386     break;
387   case 274:
388     gdk_button = 2;
389     break;
390   default:
391     gdk_button = button - 271;
392     break;
393   }
394
395   device->time = time;
396   event = gdk_event_new (state ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
397   event->button.window = g_object_ref (device->pointer_focus);
398   gdk_event_set_device (event, device->pointer);
399   event->button.time = time;
400   event->button.x = (gdouble) device->surface_x;
401   event->button.y = (gdouble) device->surface_y;
402   event->button.x_root = (gdouble) device->x;
403   event->button.y_root = (gdouble) device->y;
404   event->button.axes = NULL;
405   event->button.state = device->modifiers;
406   event->button.button = gdk_button;
407   gdk_event_set_screen (event, display->screen);
408
409   modifier = 1 << (8 + gdk_button - 1);
410   if (state)
411     device->modifiers |= modifier;
412   else
413     device->modifiers &= ~modifier;
414
415   GDK_NOTE (EVENTS,
416             g_message ("button %d %s, state %d",
417                        event->button.button,
418                        state ? "press" : "release", event->button.state));
419
420   _gdk_wayland_display_deliver_event (device->display, event);
421 }
422
423 static void
424 translate_keyboard_string (GdkEventKey *event)
425 {
426   gunichar c = 0;
427   gchar buf[7];
428
429   /* Fill in event->string crudely, since various programs
430    * depend on it.
431    */
432   event->string = NULL;
433
434   if (event->keyval != GDK_KEY_VoidSymbol)
435     c = gdk_keyval_to_unicode (event->keyval);
436
437   if (c)
438     {
439       gsize bytes_written;
440       gint len;
441
442       /* Apply the control key - Taken from Xlib
443        */
444       if (event->state & GDK_CONTROL_MASK)
445         {
446           if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
447           else if (c == '2')
448             {
449               event->string = g_memdup ("\0\0", 2);
450               event->length = 1;
451               buf[0] = '\0';
452               return;
453             }
454           else if (c >= '3' && c <= '7') c -= ('3' - '\033');
455           else if (c == '8') c = '\177';
456           else if (c == '/') c = '_' & 0x1F;
457         }
458
459       len = g_unichar_to_utf8 (c, buf);
460       buf[len] = '\0';
461
462       event->string = g_locale_from_utf8 (buf, len,
463                                           NULL, &bytes_written,
464                                           NULL);
465       if (event->string)
466         event->length = bytes_written;
467     }
468   else if (event->keyval == GDK_KEY_Escape)
469     {
470       event->length = 1;
471       event->string = g_strdup ("\033");
472     }
473   else if (event->keyval == GDK_KEY_Return ||
474           event->keyval == GDK_KEY_KP_Enter)
475     {
476       event->length = 1;
477       event->string = g_strdup ("\r");
478     }
479
480   if (!event->string)
481     {
482       event->length = 0;
483       event->string = g_strdup ("");
484     }
485 }
486
487 static gboolean
488 keyboard_repeat (gpointer data);
489
490 static gboolean
491 deliver_key_event(GdkWaylandDevice *device,
492                   uint32_t time, uint32_t key, uint32_t state)
493 {
494   GdkEvent *event;
495   uint32_t code, modifier, level;
496   struct xkb_desc *xkb;
497   GdkKeymap *keymap;
498
499   keymap = gdk_keymap_get_for_display (device->display);
500   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
501
502   device->time = time;
503   event = gdk_event_new (state ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
504   event->key.window = g_object_ref (device->keyboard_focus);
505   gdk_event_set_device (event, device->keyboard);
506   event->button.time = time;
507   event->key.state = device->modifiers;
508   event->key.group = 0;
509   code = key + xkb->min_key_code;
510   event->key.hardware_keycode = code;
511
512   level = 0;
513   if (device->modifiers & XKB_COMMON_SHIFT_MASK &&
514       XkbKeyGroupWidth(xkb, code, 0) > 1)
515     level = 1;
516
517   event->key.keyval = XkbKeySymEntry(xkb, code, level, 0);
518
519   modifier = xkb->map->modmap[code];
520   if (state)
521     device->modifiers |= modifier;
522   else
523     device->modifiers &= ~modifier;
524
525   event->key.is_modifier = modifier > 0;
526
527   translate_keyboard_string (&event->key);
528
529   _gdk_wayland_display_deliver_event (device->display, event);
530
531   GDK_NOTE (EVENTS,
532             g_message ("keyboard event, code %d, sym %d, "
533                        "string %s, mods 0x%x",
534                        code, event->key.keyval,
535                        event->key.string, event->key.state));
536
537   device->repeat_count++;
538   device->repeat_key = key;
539
540   if (state == 0)
541     {
542       if (device->repeat_timer)
543         {
544           g_source_remove (device->repeat_timer);
545           device->repeat_timer = 0;
546         }
547       return FALSE;
548     }
549   else if (modifier)
550     {
551       return FALSE;
552     }
553   else switch (device->repeat_count)
554     {
555     case 1:
556       if (device->repeat_timer)
557         {
558           g_source_remove (device->repeat_timer);
559           device->repeat_timer = 0;
560         }
561
562       device->repeat_timer =
563         gdk_threads_add_timeout (400, keyboard_repeat, device);
564       return TRUE;
565     case 2:
566       device->repeat_timer =
567         gdk_threads_add_timeout (80, keyboard_repeat, device);
568       return FALSE;
569     default:
570       return TRUE;
571     }
572 }
573
574 static gboolean
575 keyboard_repeat (gpointer data)
576 {
577   GdkWaylandDevice *device = data;
578
579   return deliver_key_event (device, device->time, device->repeat_key, 1);
580 }
581
582 static void
583 input_handle_key(void *data, struct wl_input_device *input_device,
584                  uint32_t time, uint32_t key, uint32_t state)
585 {
586   GdkWaylandDevice *device = data;
587
588   device->repeat_count = 0;
589   deliver_key_event (data, time, key, state);
590 }
591
592 static void
593 input_handle_pointer_focus(void *data,
594                            struct wl_input_device *input_device,
595                            uint32_t time, struct wl_surface *surface,
596                            int32_t x, int32_t y, int32_t sx, int32_t sy)
597 {
598   GdkWaylandDevice *device = data;
599   GdkEvent *event;
600
601   device->time = time;
602   if (device->pointer_focus)
603     {
604       event = gdk_event_new (GDK_LEAVE_NOTIFY);
605       event->crossing.window = g_object_ref (device->pointer_focus);
606       gdk_event_set_device (event, device->pointer);
607       event->crossing.subwindow = NULL;
608       event->crossing.time = time;
609       event->crossing.x = (gdouble) device->surface_x;
610       event->crossing.y = (gdouble) device->surface_y;
611       event->crossing.x_root = (gdouble) device->x;
612       event->crossing.y_root = (gdouble) device->y;
613
614       event->crossing.mode = GDK_CROSSING_NORMAL;
615       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
616       event->crossing.focus = TRUE;
617       event->crossing.state = 0;
618
619       _gdk_wayland_display_deliver_event (device->display, event);
620
621       GDK_NOTE (EVENTS,
622                 g_message ("leave, device %p surface %p",
623                            device, device->pointer_focus));
624
625       g_object_unref(device->pointer_focus);
626       device->pointer_focus = NULL;
627     }
628
629   if (surface)
630     {
631       device->pointer_focus = wl_surface_get_user_data(surface);
632       g_object_ref(device->pointer_focus);
633
634       event = gdk_event_new (GDK_ENTER_NOTIFY);
635       event->crossing.window = g_object_ref (device->pointer_focus);
636       gdk_event_set_device (event, device->pointer);
637       event->crossing.subwindow = NULL;
638       event->crossing.time = time;
639       event->crossing.x = (gdouble) sx;
640       event->crossing.y = (gdouble) sy;
641       event->crossing.x_root = (gdouble) x;
642       event->crossing.y_root = (gdouble) y;
643
644       event->crossing.mode = GDK_CROSSING_NORMAL;
645       event->crossing.detail = GDK_NOTIFY_ANCESTOR;
646       event->crossing.focus = TRUE;
647       event->crossing.state = 0;
648
649       device->surface_x = sx;
650       device->surface_y = sy;
651       device->x = x;
652       device->y = y;
653
654       _gdk_wayland_display_deliver_event (device->display, event);
655
656       GDK_NOTE (EVENTS,
657                 g_message ("enter, device %p surface %p",
658                            device, device->pointer_focus));
659     }
660 }
661
662 static void
663 update_modifiers(GdkWaylandDevice *device, struct wl_array *keys)
664 {
665   uint32_t *k, *end;
666   GdkKeymap *keymap;
667   struct xkb_desc *xkb;
668
669   keymap = gdk_keymap_get_for_display (device->display);
670   xkb = _gdk_wayland_keymap_get_xkb_desc (keymap);
671
672   end = keys->data + keys->size;
673   device->modifiers = 0;
674   for (k = keys->data; k < end; k++)
675     device->modifiers |= xkb->map->modmap[*k];
676 }
677
678 static void
679 input_handle_keyboard_focus(void *data,
680                             struct wl_input_device *input_device,
681                             uint32_t time,
682                             struct wl_surface *surface,
683                             struct wl_array *keys)
684 {
685   GdkWaylandDevice *device = data;
686   GdkEvent *event;
687
688   device->time = time;
689   if (device->keyboard_focus)
690     {
691       _gdk_wayland_window_remove_focus (device->keyboard_focus);
692       event = gdk_event_new (GDK_FOCUS_CHANGE);
693       event->focus_change.window = g_object_ref (device->keyboard_focus);
694       event->focus_change.send_event = FALSE;
695       event->focus_change.in = FALSE;
696       gdk_event_set_device (event, device->keyboard);
697
698       g_object_unref(device->keyboard_focus);
699       device->keyboard_focus = NULL;
700
701       GDK_NOTE (EVENTS,
702                 g_message ("focus out, device %p surface %p",
703                            device, device->keyboard_focus));
704
705       _gdk_wayland_display_deliver_event (device->display, event);
706     }
707
708   if (surface)
709     {
710       device->keyboard_focus = wl_surface_get_user_data(surface);
711       g_object_ref(device->keyboard_focus);
712
713       event = gdk_event_new (GDK_FOCUS_CHANGE);
714       event->focus_change.window = g_object_ref (device->keyboard_focus);
715       event->focus_change.send_event = FALSE;
716       event->focus_change.in = TRUE;
717       gdk_event_set_device (event, device->keyboard);
718
719       update_modifiers (device, keys);
720
721       GDK_NOTE (EVENTS,
722                 g_message ("focus int, device %p surface %p",
723                            device, device->keyboard_focus));
724
725       _gdk_wayland_display_deliver_event (device->display, event);
726
727       _gdk_wayland_window_add_focus (device->keyboard_focus);
728     }
729 }
730
731 static const struct wl_input_device_listener input_device_listener = {
732   input_handle_motion,
733   input_handle_button,
734   input_handle_key,
735   input_handle_pointer_focus,
736   input_handle_keyboard_focus,
737 };
738
739 struct _DataOffer {
740   struct wl_data_offer *offer;
741   gint ref_count;
742   GPtrArray *types;
743 };
744
745 static void
746 data_offer_offer (void                 *data,
747                   struct wl_data_offer *wl_data_offer,
748                   const char           *type)
749 {
750   DataOffer *offer = (DataOffer *)data;
751   g_debug (G_STRLOC ": %s wl_data_offer = %p type = %s",
752            G_STRFUNC, wl_data_offer, type);
753
754   g_ptr_array_add (offer->types, g_strdup (type));
755 }
756
757 static void
758 data_offer_unref (DataOffer *offer)
759 {
760   offer->ref_count--;
761
762   if (offer->ref_count == 0)
763     {
764       g_ptr_array_free (offer->types, TRUE);
765       g_free (offer);
766     }
767 }
768
769 static const struct wl_data_offer_listener data_offer_listener = {
770   data_offer_offer,
771 };
772
773 static void
774 data_device_data_offer (void                  *data,
775                         struct wl_data_device *data_device,
776                         uint32_t               id)
777 {
778   DataOffer *offer;
779
780   g_debug (G_STRLOC ": %s data_device = %p id = %lu",
781            G_STRFUNC, data_device, (long unsigned int)id);
782
783   /* This structure is reference counted to handle the case where you get a
784    * leave but are in the middle of transferring data
785    */
786   offer = g_new0 (DataOffer, 1);
787   offer->ref_count = 1;
788   offer->types = g_ptr_array_new_with_free_func (g_free);
789   offer->offer = (struct wl_data_offer *)
790     wl_proxy_create_for_id ((struct wl_proxy *) data_device,
791                             id,
792                             &wl_data_offer_interface);
793
794   /* The DataOffer structure is then retrieved later since this sets the user
795    * data.
796    */
797   wl_data_offer_add_listener (offer->offer,
798                               &data_offer_listener,
799                               offer);
800 }
801
802 static void
803 data_device_enter (void                  *data,
804                    struct wl_data_device *data_device,
805                    uint32_t               time,
806                    struct wl_surface     *surface,
807                    int32_t                x,
808                    int32_t                y,
809                    struct wl_data_offer  *offer)
810 {
811   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
812
813   g_debug (G_STRLOC ": %s data_device = %p time = %d, surface = %p, x = %d y = %d, offer = %p",
814            G_STRFUNC, data_device, time, surface, x, y, offer);
815
816   /* Retrieve the DataOffer associated with with the wl_data_offer - this
817    * association is made when the listener is attached.
818    */
819   g_assert (device->drag_offer == NULL);
820   device->drag_offer = wl_data_offer_get_user_data (offer);
821 }
822
823 static void
824 data_device_leave (void                  *data,
825                    struct wl_data_device *data_device)
826 {
827   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
828
829   g_debug (G_STRLOC ": %s data_device = %p",
830            G_STRFUNC, data_device);
831
832   data_offer_unref (device->drag_offer);
833   device->drag_offer = NULL;
834 }
835
836 static void
837 data_device_motion (void                  *data,
838                     struct wl_data_device *data_device,
839                     uint32_t               time,
840                     int32_t                x,
841                     int32_t                y)
842 {
843   g_debug (G_STRLOC ": %s data_device = %p, time = %d, x = %d, y = %d",
844            G_STRFUNC, data_device, time, x, y);
845 }
846
847 static void
848 data_device_drop (void                  *data,
849                   struct wl_data_device *data_device)
850 {
851   g_debug (G_STRLOC ": %s data_device = %p",
852            G_STRFUNC, data_device);
853 }
854
855 static void
856 data_device_selection (void                  *data,
857                        struct wl_data_device *wl_data_device,
858                        struct wl_data_offer  *offer)
859 {
860   GdkWaylandDevice *device = (GdkWaylandDevice *)data;
861
862   g_debug (G_STRLOC ": %s wl_data_device = %p wl_data_offer = %p",
863            G_STRFUNC, wl_data_device, offer);
864
865   if (!offer)
866     {
867       if (device->selection_offer)
868         {
869           data_offer_unref (device->selection_offer);
870           device->selection_offer = NULL;
871         }
872
873       return;
874     }
875
876   if (device->selection_offer)
877     {
878       data_offer_unref (device->selection_offer);
879       device->selection_offer = NULL;
880     }
881
882   /* Retrieve the DataOffer associated with with the wl_data_offer - this
883    * association is made when the listener is attached.
884    */
885   g_assert (device->selection_offer == NULL);
886   device->selection_offer = wl_data_offer_get_user_data (offer);
887 }
888
889 static const struct wl_data_device_listener data_device_listener = {
890   data_device_data_offer,
891   data_device_enter,
892   data_device_leave,
893   data_device_motion,
894   data_device_drop,
895   data_device_selection
896 };
897
898 void
899 _gdk_wayland_device_manager_add_device (GdkDeviceManager *device_manager,
900                                         struct wl_input_device *wl_device)
901 {
902   GdkDisplay *display;
903   GdkDisplayWayland *display_wayland;
904   GdkDeviceManagerCore *device_manager_core =
905     GDK_DEVICE_MANAGER_CORE(device_manager);
906   GdkWaylandDevice *device;
907
908   device = g_new0 (GdkWaylandDevice, 1);
909   display = gdk_device_manager_get_display (device_manager);
910   display_wayland = GDK_DISPLAY_WAYLAND (display);
911
912   device->display = display;
913   device->pointer = g_object_new (GDK_TYPE_DEVICE_CORE,
914                                   "name", "Core Pointer",
915                                   "type", GDK_DEVICE_TYPE_MASTER,
916                                   "input-source", GDK_SOURCE_MOUSE,
917                                   "input-mode", GDK_MODE_SCREEN,
918                                   "has-cursor", TRUE,
919                                   "display", display,
920                                   "device-manager", device_manager,
921                                   NULL);
922
923   device->keyboard = g_object_new (GDK_TYPE_DEVICE_CORE,
924                                    "name", "Core Keyboard",
925                                    "type", GDK_DEVICE_TYPE_MASTER,
926                                    "input-source", GDK_SOURCE_KEYBOARD,
927                                    "input-mode", GDK_MODE_SCREEN,
928                                    "has-cursor", FALSE,
929                                    "display", display,
930                                    "device-manager", device_manager,
931                                    NULL);
932
933   GDK_DEVICE_CORE (device->pointer)->device = device;
934   GDK_DEVICE_CORE (device->keyboard)->device = device;
935   device->device = wl_device;
936
937   wl_input_device_add_listener(device->device,
938                                &input_device_listener, device);
939
940   device->data_device =
941     wl_data_device_manager_get_data_device (display_wayland->data_device_manager,
942                                             device->device);
943   wl_data_device_add_listener (device->data_device,
944                                &data_device_listener, device);
945
946   device_manager_core->devices =
947     g_list_prepend (device_manager_core->devices, device->keyboard);
948   device_manager_core->devices =
949     g_list_prepend (device_manager_core->devices, device->pointer);
950
951   _gdk_device_set_associated_device (device->pointer, device->keyboard);
952   _gdk_device_set_associated_device (device->keyboard, device->pointer);
953 }
954
955 static void
956 free_device (gpointer data)
957 {
958   g_object_unref (data);
959 }
960
961 static void
962 gdk_device_manager_core_finalize (GObject *object)
963 {
964   GdkDeviceManagerCore *device_manager_core;
965
966   device_manager_core = GDK_DEVICE_MANAGER_CORE (object);
967
968   g_list_free_full (device_manager_core->devices, free_device);
969
970   G_OBJECT_CLASS (gdk_device_manager_core_parent_class)->finalize (object);
971 }
972
973 static GList *
974 gdk_device_manager_core_list_devices (GdkDeviceManager *device_manager,
975                                       GdkDeviceType     type)
976 {
977   GdkDeviceManagerCore *device_manager_core;
978   GList *devices = NULL;
979
980   if (type == GDK_DEVICE_TYPE_MASTER)
981     {
982       device_manager_core = (GdkDeviceManagerCore *) device_manager;
983       devices = g_list_copy(device_manager_core->devices);
984     }
985
986   return devices;
987 }
988
989 static GdkDevice *
990 gdk_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager)
991 {
992   GdkDeviceManagerCore *device_manager_core;
993
994   device_manager_core = (GdkDeviceManagerCore *) device_manager;
995   return device_manager_core->devices->data;
996 }
997
998 static void
999 gdk_device_manager_core_class_init (GdkDeviceManagerCoreClass *klass)
1000 {
1001   GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
1002   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1003
1004   object_class->finalize = gdk_device_manager_core_finalize;
1005   device_manager_class->list_devices = gdk_device_manager_core_list_devices;
1006   device_manager_class->get_client_pointer = gdk_device_manager_core_get_client_pointer;
1007 }
1008
1009 static void
1010 gdk_device_manager_core_init (GdkDeviceManagerCore *device_manager)
1011 {
1012 }
1013
1014 GdkDeviceManager *
1015 _gdk_wayland_device_manager_new (GdkDisplay *display)
1016 {
1017   return g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
1018                        "display", display,
1019                        NULL);
1020 }
1021
1022 gint
1023 gdk_wayland_device_get_selection_type_atoms (GdkDevice  *gdk_device,
1024                                              GdkAtom   **atoms_out)
1025 {
1026   gint i;
1027   GdkAtom *atoms;
1028   GdkWaylandDevice *device;
1029
1030   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1031   g_return_val_if_fail (atoms_out != NULL, 0);
1032
1033   device = GDK_DEVICE_CORE (gdk_device)->device;
1034
1035   if (!device->selection_offer || device->selection_offer->types->len == 0)
1036     {
1037       *atoms_out = NULL;
1038       return 0;
1039     }
1040
1041   atoms = g_new0 (GdkAtom, device->selection_offer->types->len);
1042
1043   /* Convert list of targets to atoms */
1044   for (i = 0; i < device->selection_offer->types->len; i++)
1045     {
1046       atoms[i] = gdk_atom_intern (device->selection_offer->types->pdata[i],
1047                                   FALSE);
1048       GDK_NOTE (MISC,
1049                 g_message (G_STRLOC ": Adding atom for %s",
1050                            (char *)device->selection_offer->types->pdata[i]));
1051     }
1052
1053   *atoms_out = atoms;
1054   return device->selection_offer->types->len;
1055 }
1056
1057 typedef struct
1058 {
1059   GdkWaylandDevice *device;
1060   DataOffer *offer;
1061   GIOChannel *channel;
1062   GdkDeviceWaylandRequestContentCallback cb;
1063   gpointer userdata;
1064 } RequestContentClosure;
1065
1066 static gboolean
1067 _request_content_io_func (GIOChannel *channel,
1068                           GIOCondition condition,
1069                           gpointer userdata)
1070 {
1071   RequestContentClosure *closure = (RequestContentClosure *)userdata;
1072   gchar *data = NULL;
1073   gsize len = 0;
1074   GError *error = NULL;
1075
1076   /* FIXME: We probably want to do something better than this to avoid
1077    * blocking on the transfer of large pieces of data: call the callback
1078    * multiple times I should think.
1079    */
1080   if (g_io_channel_read_to_end (channel,
1081                                 &data,
1082                                 &len,
1083                                 &error) != G_IO_STATUS_NORMAL)
1084     {
1085       g_warning (G_STRLOC ": Error reading content from pipe: %s", error->message);
1086       g_clear_error (&error);
1087     }
1088
1089   /* Since we use _read_to_end we've got a guaranteed EOF and thus can go
1090    * ahead and close the fd
1091    */
1092   g_io_channel_shutdown (channel, TRUE, NULL);
1093
1094   closure->cb (closure->device->pointer, data, len, closure->userdata);
1095
1096   g_free (data);
1097   data_offer_unref (closure->offer);
1098   g_io_channel_unref (channel);
1099   g_free (closure);
1100
1101   return FALSE;
1102 }
1103
1104 gboolean
1105 gdk_wayland_device_request_selection_content (GdkDevice                              *gdk_device,
1106                                               const gchar                            *requested_mime_type,
1107                                               GdkDeviceWaylandRequestContentCallback  cb,
1108                                               gpointer                                userdata)
1109 {
1110   int pipe_fd[2];
1111   RequestContentClosure *closure;
1112   GdkWaylandDevice *device;
1113   GError *error = NULL;
1114
1115   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), FALSE);
1116   g_return_val_if_fail (requested_mime_type != NULL, FALSE);
1117   g_return_val_if_fail (cb != NULL, FALSE);
1118
1119   device = GDK_DEVICE_CORE (gdk_device)->device;
1120
1121   if (!device->selection_offer)
1122       return FALSE;
1123
1124   /* TODO: Check mimetypes */
1125
1126   closure = g_new0 (RequestContentClosure, 1);
1127
1128   device->selection_offer->ref_count++;
1129
1130   pipe2 (pipe_fd, O_CLOEXEC);
1131   wl_data_offer_receive (device->selection_offer->offer,
1132                          requested_mime_type,
1133                          pipe_fd[1]);
1134   close (pipe_fd[1]);
1135
1136   closure->device = device;
1137   closure->offer = device->selection_offer;
1138   closure->channel = g_io_channel_unix_new (pipe_fd[0]);
1139   closure->cb = cb;
1140   closure->userdata = userdata;
1141
1142   if (!g_io_channel_set_encoding (closure->channel, NULL, &error))
1143     {
1144       g_warning (G_STRLOC ": Error setting encoding on channel: %s",
1145                  error->message);
1146       g_clear_error (&error);
1147       goto error;
1148     }
1149
1150   g_io_add_watch (closure->channel,
1151                   G_IO_IN,
1152                   _request_content_io_func,
1153                   closure);
1154
1155   return TRUE;
1156
1157 error:
1158   data_offer_unref (closure->offer);
1159   g_io_channel_unref (closure->channel);
1160   close (pipe_fd[1]);
1161   g_free (closure);
1162
1163   return FALSE;
1164 }
1165
1166 struct _GdkWaylandSelectionOffer {
1167   GdkDeviceWaylandOfferContentCallback cb;
1168   gpointer userdata;
1169   struct wl_data_source *source;
1170   GdkWaylandDevice *device;
1171 };
1172
1173 static void
1174 data_source_target (void                  *data,
1175                     struct wl_data_source *source,
1176                     const char            *mime_type)
1177 {
1178   g_debug (G_STRLOC ": %s source = %p, mime_type = %s",
1179            G_STRFUNC, source, mime_type);
1180 }
1181
1182 static void
1183 data_source_send (void                  *data,
1184                   struct wl_data_source *source,
1185                   const char            *mime_type,
1186                   int32_t                fd)
1187 {
1188   GdkWaylandSelectionOffer *offer = (GdkWaylandSelectionOffer *)data;;
1189   gchar *buf;
1190   gssize len, bytes_written = 0;
1191
1192   g_debug (G_STRLOC ": %s source = %p, mime_type = %s fd = %d",
1193            G_STRFUNC, source, mime_type, fd);
1194
1195   buf = offer->cb (offer->device->pointer, mime_type, &len, offer->userdata);
1196
1197   while (len > 0)
1198     {
1199       bytes_written += write (fd, buf + bytes_written, len);
1200       if (bytes_written == -1)
1201         goto error;
1202       len -= bytes_written;
1203     }
1204
1205   close (fd);
1206   g_free (buf);
1207
1208   return;
1209 error:
1210
1211   g_warning (G_STRLOC ": Error writing data to client: %s",
1212              g_strerror (errno));
1213
1214   close (fd);
1215   g_free (buf);
1216 }
1217
1218 static void
1219 data_source_cancelled (void                  *data,
1220                        struct wl_data_source *source)
1221 {
1222   g_debug (G_STRLOC ": %s source = %p",
1223            G_STRFUNC, source);
1224 }
1225
1226 static const struct wl_data_source_listener data_source_listener = {
1227     data_source_target,
1228     data_source_send,
1229     data_source_cancelled
1230 };
1231
1232 static guint32
1233 _wl_time_now (void)
1234 {
1235   struct timeval tv;
1236
1237   gettimeofday(&tv, NULL);
1238
1239   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
1240 }
1241
1242 gboolean
1243 gdk_wayland_device_offer_selection_content (GdkDevice                             *gdk_device,
1244                                             const gchar                          **mime_types,
1245                                             gint                                   nr_mime_types,
1246                                             GdkDeviceWaylandOfferContentCallback   cb,
1247                                             gpointer                               userdata)
1248 {
1249   GdkDisplay *display;
1250   GdkDisplayWayland *display_wayland;
1251   GdkWaylandSelectionOffer *offer;
1252   GdkWaylandDevice *device;
1253   gint i;
1254
1255   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1256   device = GDK_DEVICE_CORE (gdk_device)->device;
1257
1258   display = device->display;
1259   display_wayland = GDK_DISPLAY_WAYLAND (display);
1260
1261   offer = g_new0 (GdkWaylandSelectionOffer, 1);
1262   offer->cb = cb;
1263   offer->userdata = userdata;
1264   offer->source =
1265     wl_data_device_manager_create_data_source (display_wayland->data_device_manager);
1266   offer->device = device;
1267
1268   for (i = 0; i < nr_mime_types; i++)
1269     {
1270       wl_data_source_offer (offer->source,
1271                             mime_types[i]);
1272     }
1273
1274   wl_data_source_add_listener (offer->source,
1275                                &data_source_listener,
1276                                offer);
1277
1278   wl_data_device_set_selection (device->data_device,
1279                                 offer->source,
1280                                 _wl_time_now ());
1281
1282   device->selection_offer_out = offer;
1283
1284   return TRUE;
1285 }
1286
1287 gboolean
1288 gdk_wayland_device_clear_selection_content (GdkDevice *gdk_device)
1289 {
1290   GdkWaylandDevice *device;
1291
1292   g_return_val_if_fail (GDK_IS_DEVICE_CORE (gdk_device), 0);
1293   device = GDK_DEVICE_CORE (gdk_device)->device;
1294
1295   if (!device->selection_offer_out)
1296     return FALSE;
1297
1298   wl_data_device_set_selection (device->data_device,
1299                                 NULL,
1300                                 _wl_time_now ());
1301
1302   wl_data_source_destroy (device->selection_offer_out->source);
1303   g_free (device->selection_offer_out);
1304   device->selection_offer_out = NULL;
1305
1306   return TRUE;
1307 }