]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkdisplay-wayland.c
wayland: update to work with stable libxkbcommon
[~andy/gtk] / gdk / wayland / gdkdisplay-wayland.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #ifdef GDK_WAYLAND_USE_EGL
21 #include <wayland-egl.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include <glib.h>
31 #include "gdkwayland.h"
32 #include "gdkdisplay.h"
33 #include "gdkdisplay-wayland.h"
34 #include "gdkscreen.h"
35 #include "gdkinternals.h"
36 #include "gdkdeviceprivate.h"
37 #include "gdkdevicemanager.h"
38 #include "gdkkeysprivate.h"
39 #include "gdkprivate-wayland.h"
40
41 static void _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display);
42
43 G_DEFINE_TYPE (GdkWaylandDisplay, _gdk_wayland_display, GDK_TYPE_DISPLAY)
44
45 static void
46 gdk_input_init (GdkDisplay *display)
47 {
48   GdkWaylandDisplay *display_wayland;
49   GdkDeviceManager *device_manager;
50   GdkDevice *device;
51   GList *list, *l;
52
53   display_wayland = GDK_WAYLAND_DISPLAY (display);
54   device_manager = gdk_display_get_device_manager (display);
55
56   /* For backwards compatibility, just add
57    * floating devices that are not keyboards.
58    */
59   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
60
61   for (l = list; l; l = l->next)
62     {
63       device = l->data;
64
65       if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
66         continue;
67
68       display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, l->data);
69     }
70
71   g_list_free (list);
72
73   /* Now set "core" pointer to the first
74    * master device that is a pointer.
75    */
76   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
77
78   for (l = list; l; l = l->next)
79     {
80       device = list->data;
81
82       if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
83         continue;
84
85       display->core_pointer = device;
86       break;
87     }
88
89   /* Add the core pointer to the devices list */
90   display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, display->core_pointer);
91
92   g_list_free (list);
93 }
94
95 static void
96 output_handle_geometry(void *data,
97                        struct wl_output *wl_output,
98                        int x, int y, int physical_width, int physical_height,
99                        int subpixel, const char *make, const char *model,
100                        int32_t transform)
101 {
102   /*
103     g_signal_emit_by_name (screen, "monitors-changed");
104     g_signal_emit_by_name (screen, "size-changed");
105   */
106 }
107 static void
108 display_handle_mode(void *data,
109                    struct wl_output *wl_output,
110                    uint32_t flags,
111                    int width,
112                    int height,
113                    int refresh)
114 {
115 }
116
117 static const struct wl_output_listener output_listener = {
118         output_handle_geometry,
119         display_handle_mode
120 };
121
122 static void
123 gdk_registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
124                                         const char *interface, uint32_t version)
125 {
126   GdkWaylandDisplay *display_wayland = data;
127   GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
128   struct wl_seat *seat;
129
130   if (strcmp(interface, "wl_compositor") == 0) {
131     display_wayland->compositor =
132         wl_registry_bind(display_wayland->wl_registry, id, &wl_compositor_interface, 1);
133   } else if (strcmp(interface, "wl_shm") == 0) {
134    display_wayland->shm =
135         wl_registry_bind(display_wayland->wl_registry, id, &wl_shm_interface, 1);
136
137     /* SHM interface is prerequisite */
138     _gdk_wayland_display_load_cursor_theme(display_wayland);
139   } else if (strcmp(interface, "wl_shell") == 0) {
140     display_wayland->shell =
141         wl_registry_bind(display_wayland->wl_registry, id, &wl_shell_interface, 1);
142   } else if (strcmp(interface, "wl_output") == 0) {
143     display_wayland->output =
144       wl_registry_bind(display_wayland->wl_registry, id, &wl_output_interface, 1);
145     wl_output_add_listener(display_wayland->output,
146                            &output_listener, display_wayland);
147   } else if (strcmp(interface, "wl_seat") == 0) {
148     seat = wl_registry_bind(display_wayland->wl_registry, id, &wl_seat_interface, 1);
149     _gdk_wayland_device_manager_add_device (gdk_display->device_manager,
150                                             seat);
151   } else if (strcmp(interface, "wl_data_device_manager") == 0) {
152       display_wayland->data_device_manager =
153         wl_registry_bind(display_wayland->wl_registry, id,
154                                         &wl_data_device_manager_interface, 1);
155   }
156 }
157
158 #ifdef GDK_WAYLAND_USE_EGL
159 static gboolean
160 gdk_display_init_egl(GdkDisplay *display)
161 {
162   GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
163   EGLint major, minor, i;
164   void *p;
165
166   static const struct { const char *f; unsigned int offset; }
167   extension_functions[] = {
168     { "glEGLImageTargetTexture2DOES", offsetof(GdkWaylandDisplay, image_target_texture_2d) },
169     { "eglCreateImageKHR", offsetof(GdkWaylandDisplay, create_image) },
170     { "eglDestroyImageKHR", offsetof(GdkWaylandDisplay, destroy_image) }
171   };
172
173   display_wayland->egl_display =
174     eglGetDisplay(display_wayland->wl_display);
175   if (!eglInitialize(display_wayland->egl_display, &major, &minor)) {
176     fprintf(stderr, "failed to initialize display\n");
177     return FALSE;
178   }
179
180   eglBindAPI(EGL_OPENGL_API);
181
182   display_wayland->egl_context =
183     eglCreateContext(display_wayland->egl_display, NULL, EGL_NO_CONTEXT, NULL);
184   if (display_wayland->egl_context == NULL) {
185     fprintf(stderr, "failed to create context\n");
186     return FALSE;
187   }
188
189   if (!eglMakeCurrent(display_wayland->egl_display,
190                       NULL, NULL, display_wayland->egl_context)) {
191     fprintf(stderr, "faile to make context current\n");
192     return FALSE;
193   }
194
195   display_wayland->cairo_device =
196     cairo_egl_device_create(display_wayland->egl_display,
197                             display_wayland->egl_context);
198   if (cairo_device_status (display_wayland->cairo_device) != CAIRO_STATUS_SUCCESS) {
199     fprintf(stderr, "failed to get cairo drm device\n");
200     return FALSE;
201   }
202
203   for (i = 0; i < G_N_ELEMENTS(extension_functions); i++) {
204     p = eglGetProcAddress(extension_functions[i].f);
205     *(void **) ((char *) display_wayland + extension_functions[i].offset) = p;
206     if (p == NULL) {
207       fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
208       return FALSE;
209     }
210   }
211
212   return TRUE;
213 }
214 #endif
215
216 static const struct wl_registry_listener registry_listener = {
217         gdk_registry_handle_global
218 };
219
220 GdkDisplay *
221 _gdk_wayland_display_open (const gchar *display_name)
222 {
223   struct wl_display *wl_display;
224   GdkDisplay *display;
225   GdkWaylandDisplay *display_wayland;
226
227   wl_display = wl_display_connect(display_name);
228   if (!wl_display)
229     return NULL;
230
231   display = g_object_new (GDK_TYPE_WAYLAND_DISPLAY, NULL);
232   display_wayland = GDK_WAYLAND_DISPLAY (display);
233
234   display_wayland->wl_display = wl_display;
235
236   display_wayland->screen = _gdk_wayland_screen_new (display);
237
238   display->device_manager = _gdk_wayland_device_manager_new (display);
239
240   /* Set up listener so we'll catch all events. */
241   display_wayland->wl_registry = wl_display_get_registry(display_wayland->wl_display);
242   wl_registry_add_listener(display_wayland->wl_registry, &registry_listener, display_wayland);
243
244 #ifdef GDK_WAYLAND_USE_EGL
245   gdk_display_init_egl(display);
246 #else
247   wl_display_dispatch(display_wayland->wl_display);
248 #endif
249
250   display_wayland->event_source =
251     _gdk_wayland_display_event_source_new (display);
252
253   gdk_input_init (display);
254
255   g_signal_emit_by_name (display, "opened");
256   g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
257
258   return display;
259 }
260
261 static void
262 gdk_wayland_display_dispose (GObject *object)
263 {
264   GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
265
266   _gdk_wayland_display_manager_remove_display (gdk_display_manager_get (),
267                                                GDK_DISPLAY (display_wayland));
268   g_list_foreach (display_wayland->input_devices,
269                   (GFunc) g_object_run_dispose, NULL);
270
271   _gdk_screen_close (display_wayland->screen);
272
273   if (display_wayland->event_source)
274     {
275       g_source_destroy (display_wayland->event_source);
276       g_source_unref (display_wayland->event_source);
277       display_wayland->event_source = NULL;
278     }
279
280 #ifdef GDK_WAYLAND_USE_EGL
281   eglTerminate(display_wayland->egl_display);
282 #endif
283
284   G_OBJECT_CLASS (_gdk_wayland_display_parent_class)->dispose (object);
285 }
286
287 static void
288 gdk_wayland_display_finalize (GObject *object)
289 {
290   GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
291
292   /* Keymap */
293   if (display_wayland->keymap)
294     g_object_unref (display_wayland->keymap);
295
296   /* input GdkDevice list */
297   g_list_free_full (display_wayland->input_devices, g_object_unref);
298
299   g_object_unref (display_wayland->screen);
300
301   g_free (display_wayland->startup_notification_id);
302
303   G_OBJECT_CLASS (_gdk_wayland_display_parent_class)->finalize (object);
304 }
305
306 static const gchar *
307 gdk_wayland_display_get_name (GdkDisplay *display)
308 {
309   return "Wayland";
310 }
311
312 static gint
313 gdk_wayland_display_get_n_screens (GdkDisplay *display)
314 {
315   return 1;
316 }
317
318 static GdkScreen *
319 gdk_wayland_display_get_screen (GdkDisplay *display, 
320                                 gint        screen_num)
321 {
322   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
323   g_return_val_if_fail (screen_num == 0, NULL);
324
325   return GDK_WAYLAND_DISPLAY (display)->screen;
326 }
327
328 static GdkScreen *
329 gdk_wayland_display_get_default_screen (GdkDisplay *display)
330 {
331   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
332
333   return GDK_WAYLAND_DISPLAY (display)->screen;
334 }
335
336 static void
337 gdk_wayland_display_beep (GdkDisplay *display)
338 {
339   g_return_if_fail (GDK_IS_DISPLAY (display));
340 }
341
342 static void
343 gdk_wayland_display_sync (GdkDisplay *display)
344 {
345   GdkWaylandDisplay *display_wayland;
346
347   g_return_if_fail (GDK_IS_DISPLAY (display));
348
349   display_wayland = GDK_WAYLAND_DISPLAY (display);
350
351   wl_display_roundtrip(display_wayland->wl_display);
352 }
353
354 static void
355 gdk_wayland_display_flush (GdkDisplay *display)
356 {
357   g_return_if_fail (GDK_IS_DISPLAY (display));
358
359   if (!display->closed)
360     wl_display_flush(GDK_WAYLAND_DISPLAY (display)->wl_display);;
361 }
362
363 static gboolean
364 gdk_wayland_display_has_pending (GdkDisplay *display)
365 {
366   return FALSE;
367 }
368
369 static GdkWindow *
370 gdk_wayland_display_get_default_group (GdkDisplay *display)
371 {
372   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
373
374   return NULL;
375 }
376
377
378 static gboolean
379 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
380 {
381   return TRUE;
382 }
383
384 static gboolean
385 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
386                                                     GdkAtom     selection)
387
388 {
389     return FALSE;
390 }
391
392 static gboolean
393 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
394 {
395   return FALSE;
396 }
397
398 static void
399 gdk_wayland_display_store_clipboard (GdkDisplay    *display,
400                                      GdkWindow     *clipboard_window,
401                                      guint32        time_,
402                                      const GdkAtom *targets,
403                                      gint           n_targets)
404 {
405 }
406
407 static gboolean
408 gdk_wayland_display_supports_shapes (GdkDisplay *display)
409 {
410   return TRUE;
411 }
412
413 static gboolean
414 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
415 {
416   return TRUE;
417 }
418
419 static gboolean
420 gdk_wayland_display_supports_composite (GdkDisplay *display)
421 {
422   return TRUE;
423 }
424
425 static GList *
426 gdk_wayland_display_list_devices (GdkDisplay *display)
427 {
428   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
429
430   return GDK_WAYLAND_DISPLAY (display)->input_devices;
431 }
432
433 static void
434 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
435 {
436 }
437
438 static void
439 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
440 {
441   /* Post the damage here instead? */
442 }
443
444 static gulong
445 gdk_wayland_display_get_next_serial (GdkDisplay *display)
446 {
447   static gulong serial = 0;
448   return ++serial;
449 }
450
451 void
452 _gdk_wayland_display_make_default (GdkDisplay *display)
453 {
454 }
455
456 /**
457  * gdk_wayland_display_broadcast_startup_message:
458  * @display: a #GdkDisplay
459  * @message_type: startup notification message type ("new", "change",
460  * or "remove")
461  * @...: a list of key/value pairs (as strings), terminated by a
462  * %NULL key. (A %NULL value for a key will cause that key to be
463  * skipped in the output.)
464  *
465  * Sends a startup notification message of type @message_type to
466  * @display. 
467  *
468  * This is a convenience function for use by code that implements the
469  * freedesktop startup notification specification. Applications should
470  * not normally need to call it directly. See the <ulink
471  * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
472  * Notification Protocol specification</ulink> for
473  * definitions of the message types and keys that can be used.
474  *
475  * Since: 2.12
476  **/
477 void
478 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
479                                                const char *message_type,
480                                                ...)
481 {
482   GString *message;
483   va_list ap;
484   const char *key, *value, *p;
485
486   message = g_string_new (message_type);
487   g_string_append_c (message, ':');
488
489   va_start (ap, message_type);
490   while ((key = va_arg (ap, const char *)))
491     {
492       value = va_arg (ap, const char *);
493       if (!value)
494         continue;
495
496       g_string_append_printf (message, " %s=\"", key);
497       for (p = value; *p; p++)
498         {
499           switch (*p)
500             {
501             case ' ':
502             case '"':
503             case '\\':
504               g_string_append_c (message, '\\');
505               break;
506             }
507
508           g_string_append_c (message, *p);
509         }
510       g_string_append_c (message, '\"');
511     }
512   va_end (ap);
513
514   printf ("startup message: %s\n", message->str);
515
516   g_string_free (message, TRUE);
517 }
518
519 static void
520 gdk_wayland_display_notify_startup_complete (GdkDisplay  *display,
521                                              const gchar *startup_id)
522 {
523   gdk_wayland_display_broadcast_startup_message (display, "remove",
524                                                  "ID", startup_id,
525                                                  NULL);
526 }
527
528 static void
529 gdk_wayland_display_event_data_copy (GdkDisplay     *display,
530                                      const GdkEvent *src,
531                                      GdkEvent       *dst)
532 {
533 }
534
535 static void
536 gdk_wayland_display_event_data_free (GdkDisplay *display,
537                                      GdkEvent   *event)
538 {
539 }
540
541 GdkKeymap *
542 _gdk_wayland_display_get_keymap (GdkDisplay *display)
543 {
544   GdkDeviceManager *device_manager;
545   GList *list, *l;
546   GdkDevice *core_keyboard = NULL;
547
548   device_manager = gdk_display_get_device_manager (display);
549   list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
550
551   for (l = list; l; l = l->next)
552     {
553       GdkDevice *device;
554       device = list->data;
555
556       if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
557         continue;
558
559       core_keyboard = device;
560       break;
561     }
562
563   return core_keyboard?_gdk_wayland_device_get_keymap (core_keyboard):NULL;
564 }
565
566 static void
567 gdk_wayland_display_push_error_trap (GdkDisplay *display)
568 {
569 }
570
571 static gint
572 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
573                                     gboolean    ignored)
574 {
575   return 0;
576 }
577
578 static void
579 _gdk_wayland_display_class_init (GdkWaylandDisplayClass * class)
580 {
581   GObjectClass *object_class = G_OBJECT_CLASS (class);
582   GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
583
584   object_class->dispose = gdk_wayland_display_dispose;
585   object_class->finalize = gdk_wayland_display_finalize;
586
587   display_class->window_type = _gdk_wayland_window_get_type ();
588   display_class->get_name = gdk_wayland_display_get_name;
589   display_class->get_n_screens = gdk_wayland_display_get_n_screens;
590   display_class->get_screen = gdk_wayland_display_get_screen;
591   display_class->get_default_screen = gdk_wayland_display_get_default_screen;
592   display_class->beep = gdk_wayland_display_beep;
593   display_class->sync = gdk_wayland_display_sync;
594   display_class->flush = gdk_wayland_display_flush;
595   display_class->has_pending = gdk_wayland_display_has_pending;
596   display_class->queue_events = _gdk_wayland_display_queue_events;
597   display_class->get_default_group = gdk_wayland_display_get_default_group;
598   display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
599   display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
600   display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
601   display_class->store_clipboard = gdk_wayland_display_store_clipboard;
602   display_class->supports_shapes = gdk_wayland_display_supports_shapes;
603   display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
604   display_class->supports_composite = gdk_wayland_display_supports_composite;
605   display_class->list_devices = gdk_wayland_display_list_devices;
606   display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
607   display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
608   display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
609   display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
610   display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
611   display_class->get_cursor_for_pixbuf = _gdk_wayland_display_get_cursor_for_pixbuf;
612   display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
613   display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
614   display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
615   display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
616   display_class->get_next_serial = gdk_wayland_display_get_next_serial;
617   display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
618   display_class->event_data_copy = gdk_wayland_display_event_data_copy;
619   display_class->event_data_free = gdk_wayland_display_event_data_free;
620   display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
621   display_class->get_keymap = _gdk_wayland_display_get_keymap;
622   display_class->push_error_trap = gdk_wayland_display_push_error_trap;
623   display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
624   display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
625   display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
626   display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
627   display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
628   display_class->convert_selection = _gdk_wayland_display_convert_selection;
629   display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
630   display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
631 }
632
633 static void
634 _gdk_wayland_display_init (GdkWaylandDisplay *display)
635 {
636   _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
637                                             GDK_DISPLAY (display));
638
639   display->xkb_context = xkb_context_new (0);
640 }
641
642 static void
643 _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display)
644 {
645   guint w, h;
646   gchar *theme_name = NULL; /* FIXME: Do something here */
647
648   g_assert (wayland_display);
649   g_assert (wayland_display->shm);
650
651   _gdk_wayland_display_get_default_cursor_size (GDK_DISPLAY (wayland_display),
652                                                 &w, &h);
653
654   wayland_display->cursor_theme = wl_cursor_theme_load (theme_name,
655                                                         w,
656                                                         wayland_display->shm);
657 }
658
659 guint32
660 _gdk_wayland_display_get_serial (GdkWaylandDisplay *wayland_display)
661 {
662   return wayland_display->serial;
663 }
664
665 void
666 _gdk_wayland_display_update_serial (GdkWaylandDisplay *wayland_display,
667                                     guint32            serial)
668 {
669   if (serial > wayland_display->serial)
670     wayland_display->serial = serial;
671 }