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