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