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