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