2 * Copyright © 2010 Intel Corporation
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.
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.
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/>.
20 #ifdef GDK_WAYLAND_USE_EGL
21 #include <wayland-egl.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"
41 G_DEFINE_TYPE (GdkDisplayWayland, _gdk_display_wayland, GDK_TYPE_DISPLAY)
44 gdk_input_init (GdkDisplay *display)
46 GdkDisplayWayland *display_wayland;
47 GdkDeviceManager *device_manager;
51 display_wayland = GDK_DISPLAY_WAYLAND (display);
52 device_manager = gdk_display_get_device_manager (display);
54 /* For backwards compatibility, just add
55 * floating devices that are not keyboards.
57 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
59 for (l = list; l; l = l->next)
63 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
66 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, l->data);
71 /* Now set "core" pointer to the first
72 * master device that is a pointer.
74 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
76 for (l = list; l; l = l->next)
80 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
83 display->core_pointer = device;
87 /* Add the core pointer to the devices list */
88 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, display->core_pointer);
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)
100 g_signal_emit_by_name (screen, "monitors-changed");
101 g_signal_emit_by_name (screen, "size-changed");
105 display_handle_mode(void *data,
106 struct wl_output *wl_output,
114 static const struct wl_output_listener output_listener = {
115 output_handle_geometry,
120 gdk_display_handle_global(struct wl_display *display, uint32_t id,
121 const char *interface, uint32_t version, void *data)
123 GdkDisplayWayland *display_wayland = data;
124 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
125 struct wl_input_device *input;
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,
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);
150 #ifdef GDK_WAYLAND_USE_EGL
152 gdk_display_init_egl(GdkDisplay *display)
154 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (display);
155 EGLint major, minor, i;
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) }
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");
172 eglBindAPI(EGL_OPENGL_API);
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");
181 if (!eglMakeCurrent(display_wayland->egl_display,
182 NULL, NULL, display_wayland->egl_context)) {
183 fprintf(stderr, "faile to make context current\n");
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");
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;
199 fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
209 _gdk_wayland_display_open (const gchar *display_name)
211 struct wl_display *wl_display;
213 GdkDisplayWayland *display_wayland;
215 wl_display = wl_display_connect(display_name);
219 display = g_object_new (GDK_TYPE_DISPLAY_WAYLAND, NULL);
220 display_wayland = GDK_DISPLAY_WAYLAND (display);
222 display_wayland->wl_display = wl_display;
224 display_wayland->screen = _gdk_wayland_screen_new (display);
226 display->device_manager = _gdk_wayland_device_manager_new (display);
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);
232 #ifdef GDK_WAYLAND_USE_EGL
233 gdk_display_init_egl(display);
235 wl_display_iterate(wl_display, WL_DISPLAY_READABLE);
236 wl_display_roundtrip(wl_display);
239 display_wayland->event_source =
240 _gdk_wayland_display_event_source_new (display);
242 gdk_input_init (display);
244 g_signal_emit_by_name (display, "opened");
245 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
251 gdk_wayland_display_dispose (GObject *object)
253 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
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);
260 _gdk_screen_close (display_wayland->screen);
262 if (display_wayland->event_source)
264 g_source_destroy (display_wayland->event_source);
265 g_source_unref (display_wayland->event_source);
266 display_wayland->event_source = NULL;
269 #ifdef GDK_WAYLAND_USE_EGL
270 eglTerminate(display_wayland->egl_display);
273 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->dispose (object);
277 gdk_wayland_display_finalize (GObject *object)
279 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
282 if (display_wayland->keymap)
283 g_object_unref (display_wayland->keymap);
285 /* input GdkDevice list */
286 g_list_free_full (display_wayland->input_devices, g_object_unref);
288 g_object_unref (display_wayland->screen);
290 g_free (display_wayland->startup_notification_id);
292 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->finalize (object);
296 gdk_wayland_display_get_name (GdkDisplay *display)
302 gdk_wayland_display_get_n_screens (GdkDisplay *display)
308 gdk_wayland_display_get_screen (GdkDisplay *display,
311 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
312 g_return_val_if_fail (screen_num == 0, NULL);
314 return GDK_DISPLAY_WAYLAND (display)->screen;
318 gdk_wayland_display_get_default_screen (GdkDisplay *display)
320 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
322 return GDK_DISPLAY_WAYLAND (display)->screen;
326 gdk_wayland_display_beep (GdkDisplay *display)
328 g_return_if_fail (GDK_IS_DISPLAY (display));
332 gdk_wayland_display_sync (GdkDisplay *display)
334 GdkDisplayWayland *display_wayland;
336 g_return_if_fail (GDK_IS_DISPLAY (display));
338 display_wayland = GDK_DISPLAY_WAYLAND (display);
340 wl_display_roundtrip(display_wayland->wl_display);
344 gdk_wayland_display_flush (GdkDisplay *display)
346 g_return_if_fail (GDK_IS_DISPLAY (display));
348 if (!display->closed)
349 _gdk_wayland_display_flush (display,
350 GDK_DISPLAY_WAYLAND (display)->event_source);
354 gdk_wayland_display_has_pending (GdkDisplay *display)
360 gdk_wayland_display_get_default_group (GdkDisplay *display)
362 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
369 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
375 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
383 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
389 gdk_wayland_display_store_clipboard (GdkDisplay *display,
390 GdkWindow *clipboard_window,
392 const GdkAtom *targets,
398 gdk_wayland_display_supports_shapes (GdkDisplay *display)
404 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
410 gdk_wayland_display_supports_composite (GdkDisplay *display)
416 gdk_wayland_display_list_devices (GdkDisplay *display)
418 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
420 return GDK_DISPLAY_WAYLAND (display)->input_devices;
424 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
429 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
431 /* Post the damage here instead? */
435 gdk_wayland_display_get_next_serial (GdkDisplay *display)
437 static gulong serial = 0;
442 _gdk_wayland_display_make_default (GdkDisplay *display)
447 * gdk_wayland_display_broadcast_startup_message:
448 * @display: a #GdkDisplay
449 * @message_type: startup notification message type ("new", "change",
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.)
455 * Sends a startup notification message of type @message_type to
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.
468 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
469 const char *message_type,
474 const char *key, *value, *p;
476 message = g_string_new (message_type);
477 g_string_append_c (message, ':');
479 va_start (ap, message_type);
480 while ((key = va_arg (ap, const char *)))
482 value = va_arg (ap, const char *);
486 g_string_append_printf (message, " %s=\"", key);
487 for (p = value; *p; p++)
494 g_string_append_c (message, '\\');
498 g_string_append_c (message, *p);
500 g_string_append_c (message, '\"');
504 printf ("startup message: %s\n", message->str);
506 g_string_free (message, TRUE);
510 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
511 const gchar *startup_id)
513 gdk_wayland_display_broadcast_startup_message (display, "remove",
519 gdk_wayland_display_event_data_copy (GdkDisplay *display,
526 gdk_wayland_display_event_data_free (GdkDisplay *display,
532 gdk_wayland_display_get_keymap (GdkDisplay *display)
534 GdkDisplayWayland *display_wayland;
536 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
537 display_wayland = GDK_DISPLAY_WAYLAND (display);
539 if (!display_wayland->keymap)
540 display_wayland->keymap = _gdk_wayland_keymap_new (display);
542 return display_wayland->keymap;
546 gdk_wayland_display_push_error_trap (GdkDisplay *display)
551 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
558 _gdk_display_wayland_class_init (GdkDisplayWaylandClass * class)
560 GObjectClass *object_class = G_OBJECT_CLASS (class);
561 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
563 object_class->dispose = gdk_wayland_display_dispose;
564 object_class->finalize = gdk_wayland_display_finalize;
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;
613 _gdk_display_wayland_init (GdkDisplayWayland *display)
615 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
616 GDK_DISPLAY (display));