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