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 static void _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display);
43 G_DEFINE_TYPE (GdkWaylandDisplay, _gdk_wayland_display, GDK_TYPE_DISPLAY)
46 gdk_input_init (GdkDisplay *display)
48 GdkWaylandDisplay *display_wayland;
49 GdkDeviceManager *device_manager;
53 display_wayland = GDK_WAYLAND_DISPLAY (display);
54 device_manager = gdk_display_get_device_manager (display);
56 /* For backwards compatibility, just add
57 * floating devices that are not keyboards.
59 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
61 for (l = list; l; l = l->next)
65 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
68 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, l->data);
73 /* Now set "core" pointer to the first
74 * master device that is a pointer.
76 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
78 for (l = list; l; l = l->next)
82 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
85 display->core_pointer = device;
89 /* Add the core pointer to the devices list */
90 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, display->core_pointer);
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,
103 g_signal_emit_by_name (screen, "monitors-changed");
104 g_signal_emit_by_name (screen, "size-changed");
108 display_handle_mode(void *data,
109 struct wl_output *wl_output,
117 static const struct wl_output_listener output_listener = {
118 output_handle_geometry,
123 gdk_registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
124 const char *interface, uint32_t version)
126 GdkWaylandDisplay *display_wayland = data;
127 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
128 struct wl_seat *seat;
130 if (strcmp(interface, "wl_compositor") == 0) {
131 display_wayland->compositor =
132 wl_registry_bind(display_wayland->wl_registry, id, &wl_compositor_interface, 1);
133 } else if (strcmp(interface, "wl_shm") == 0) {
134 display_wayland->shm =
135 wl_registry_bind(display_wayland->wl_registry, id, &wl_shm_interface, 1);
137 /* SHM interface is prerequisite */
138 _gdk_wayland_display_load_cursor_theme(display_wayland);
139 } else if (strcmp(interface, "wl_shell") == 0) {
140 display_wayland->shell =
141 wl_registry_bind(display_wayland->wl_registry, id, &wl_shell_interface, 1);
142 } else if (strcmp(interface, "wl_output") == 0) {
143 display_wayland->output =
144 wl_registry_bind(display_wayland->wl_registry, id, &wl_output_interface, 1);
145 wl_output_add_listener(display_wayland->output,
146 &output_listener, display_wayland);
147 } else if (strcmp(interface, "wl_seat") == 0) {
148 seat = wl_registry_bind(display_wayland->wl_registry, id, &wl_seat_interface, 1);
149 _gdk_wayland_device_manager_add_device (gdk_display->device_manager,
151 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
152 display_wayland->data_device_manager =
153 wl_registry_bind(display_wayland->wl_registry, id,
154 &wl_data_device_manager_interface, 1);
158 #ifdef GDK_WAYLAND_USE_EGL
160 gdk_display_init_egl(GdkDisplay *display)
162 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
163 EGLint major, minor, i;
166 static const struct { const char *f; unsigned int offset; }
167 extension_functions[] = {
168 { "glEGLImageTargetTexture2DOES", offsetof(GdkWaylandDisplay, image_target_texture_2d) },
169 { "eglCreateImageKHR", offsetof(GdkWaylandDisplay, create_image) },
170 { "eglDestroyImageKHR", offsetof(GdkWaylandDisplay, destroy_image) }
173 display_wayland->egl_display =
174 eglGetDisplay(display_wayland->wl_display);
175 if (!eglInitialize(display_wayland->egl_display, &major, &minor)) {
176 fprintf(stderr, "failed to initialize display\n");
180 eglBindAPI(EGL_OPENGL_API);
182 display_wayland->egl_context =
183 eglCreateContext(display_wayland->egl_display, NULL, EGL_NO_CONTEXT, NULL);
184 if (display_wayland->egl_context == NULL) {
185 fprintf(stderr, "failed to create context\n");
189 if (!eglMakeCurrent(display_wayland->egl_display,
190 NULL, NULL, display_wayland->egl_context)) {
191 fprintf(stderr, "faile to make context current\n");
195 display_wayland->cairo_device =
196 cairo_egl_device_create(display_wayland->egl_display,
197 display_wayland->egl_context);
198 if (cairo_device_status (display_wayland->cairo_device) != CAIRO_STATUS_SUCCESS) {
199 fprintf(stderr, "failed to get cairo drm device\n");
203 for (i = 0; i < G_N_ELEMENTS(extension_functions); i++) {
204 p = eglGetProcAddress(extension_functions[i].f);
205 *(void **) ((char *) display_wayland + extension_functions[i].offset) = p;
207 fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
216 static const struct wl_registry_listener registry_listener = {
217 gdk_registry_handle_global
221 _gdk_wayland_display_open (const gchar *display_name)
223 struct wl_display *wl_display;
225 GdkWaylandDisplay *display_wayland;
227 wl_display = wl_display_connect(display_name);
231 display = g_object_new (GDK_TYPE_WAYLAND_DISPLAY, NULL);
232 display_wayland = GDK_WAYLAND_DISPLAY (display);
234 display_wayland->wl_display = wl_display;
236 display_wayland->screen = _gdk_wayland_screen_new (display);
238 display->device_manager = _gdk_wayland_device_manager_new (display);
240 /* Set up listener so we'll catch all events. */
241 display_wayland->wl_registry = wl_display_get_registry(display_wayland->wl_display);
242 wl_registry_add_listener(display_wayland->wl_registry, ®istry_listener, display_wayland);
244 #ifdef GDK_WAYLAND_USE_EGL
245 gdk_display_init_egl(display);
247 wl_display_dispatch(display_wayland->wl_display);
250 display_wayland->event_source =
251 _gdk_wayland_display_event_source_new (display);
253 gdk_input_init (display);
255 g_signal_emit_by_name (display, "opened");
256 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
262 gdk_wayland_display_dispose (GObject *object)
264 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
266 _gdk_wayland_display_manager_remove_display (gdk_display_manager_get (),
267 GDK_DISPLAY (display_wayland));
268 g_list_foreach (display_wayland->input_devices,
269 (GFunc) g_object_run_dispose, NULL);
271 _gdk_screen_close (display_wayland->screen);
273 if (display_wayland->event_source)
275 g_source_destroy (display_wayland->event_source);
276 g_source_unref (display_wayland->event_source);
277 display_wayland->event_source = NULL;
280 #ifdef GDK_WAYLAND_USE_EGL
281 eglTerminate(display_wayland->egl_display);
284 G_OBJECT_CLASS (_gdk_wayland_display_parent_class)->dispose (object);
288 gdk_wayland_display_finalize (GObject *object)
290 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
293 if (display_wayland->keymap)
294 g_object_unref (display_wayland->keymap);
296 /* input GdkDevice list */
297 g_list_free_full (display_wayland->input_devices, g_object_unref);
299 g_object_unref (display_wayland->screen);
301 g_free (display_wayland->startup_notification_id);
303 G_OBJECT_CLASS (_gdk_wayland_display_parent_class)->finalize (object);
307 gdk_wayland_display_get_name (GdkDisplay *display)
313 gdk_wayland_display_get_n_screens (GdkDisplay *display)
319 gdk_wayland_display_get_screen (GdkDisplay *display,
322 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
323 g_return_val_if_fail (screen_num == 0, NULL);
325 return GDK_WAYLAND_DISPLAY (display)->screen;
329 gdk_wayland_display_get_default_screen (GdkDisplay *display)
331 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
333 return GDK_WAYLAND_DISPLAY (display)->screen;
337 gdk_wayland_display_beep (GdkDisplay *display)
339 g_return_if_fail (GDK_IS_DISPLAY (display));
343 gdk_wayland_display_sync (GdkDisplay *display)
345 GdkWaylandDisplay *display_wayland;
347 g_return_if_fail (GDK_IS_DISPLAY (display));
349 display_wayland = GDK_WAYLAND_DISPLAY (display);
351 wl_display_roundtrip(display_wayland->wl_display);
355 gdk_wayland_display_flush (GdkDisplay *display)
357 g_return_if_fail (GDK_IS_DISPLAY (display));
359 if (!display->closed)
360 wl_display_flush(GDK_WAYLAND_DISPLAY (display)->wl_display);;
364 gdk_wayland_display_has_pending (GdkDisplay *display)
370 gdk_wayland_display_get_default_group (GdkDisplay *display)
372 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
379 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
385 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
393 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
399 gdk_wayland_display_store_clipboard (GdkDisplay *display,
400 GdkWindow *clipboard_window,
402 const GdkAtom *targets,
408 gdk_wayland_display_supports_shapes (GdkDisplay *display)
414 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
420 gdk_wayland_display_supports_composite (GdkDisplay *display)
426 gdk_wayland_display_list_devices (GdkDisplay *display)
428 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
430 return GDK_WAYLAND_DISPLAY (display)->input_devices;
434 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
439 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
441 /* Post the damage here instead? */
445 gdk_wayland_display_get_next_serial (GdkDisplay *display)
447 static gulong serial = 0;
452 _gdk_wayland_display_make_default (GdkDisplay *display)
457 * gdk_wayland_display_broadcast_startup_message:
458 * @display: a #GdkDisplay
459 * @message_type: startup notification message type ("new", "change",
461 * @...: a list of key/value pairs (as strings), terminated by a
462 * %NULL key. (A %NULL value for a key will cause that key to be
463 * skipped in the output.)
465 * Sends a startup notification message of type @message_type to
468 * This is a convenience function for use by code that implements the
469 * freedesktop startup notification specification. Applications should
470 * not normally need to call it directly. See the <ulink
471 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
472 * Notification Protocol specification</ulink> for
473 * definitions of the message types and keys that can be used.
478 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
479 const char *message_type,
484 const char *key, *value, *p;
486 message = g_string_new (message_type);
487 g_string_append_c (message, ':');
489 va_start (ap, message_type);
490 while ((key = va_arg (ap, const char *)))
492 value = va_arg (ap, const char *);
496 g_string_append_printf (message, " %s=\"", key);
497 for (p = value; *p; p++)
504 g_string_append_c (message, '\\');
508 g_string_append_c (message, *p);
510 g_string_append_c (message, '\"');
514 printf ("startup message: %s\n", message->str);
516 g_string_free (message, TRUE);
520 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
521 const gchar *startup_id)
523 gdk_wayland_display_broadcast_startup_message (display, "remove",
529 gdk_wayland_display_event_data_copy (GdkDisplay *display,
536 gdk_wayland_display_event_data_free (GdkDisplay *display,
542 _gdk_wayland_display_get_keymap (GdkDisplay *display)
544 GdkDeviceManager *device_manager;
546 GdkDevice *core_keyboard = NULL;
548 device_manager = gdk_display_get_device_manager (display);
549 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
551 for (l = list; l; l = l->next)
556 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
559 core_keyboard = device;
563 return core_keyboard?_gdk_wayland_device_get_keymap (core_keyboard):NULL;
567 gdk_wayland_display_push_error_trap (GdkDisplay *display)
572 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
579 _gdk_wayland_display_class_init (GdkWaylandDisplayClass * class)
581 GObjectClass *object_class = G_OBJECT_CLASS (class);
582 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
584 object_class->dispose = gdk_wayland_display_dispose;
585 object_class->finalize = gdk_wayland_display_finalize;
587 display_class->window_type = _gdk_wayland_window_get_type ();
588 display_class->get_name = gdk_wayland_display_get_name;
589 display_class->get_n_screens = gdk_wayland_display_get_n_screens;
590 display_class->get_screen = gdk_wayland_display_get_screen;
591 display_class->get_default_screen = gdk_wayland_display_get_default_screen;
592 display_class->beep = gdk_wayland_display_beep;
593 display_class->sync = gdk_wayland_display_sync;
594 display_class->flush = gdk_wayland_display_flush;
595 display_class->has_pending = gdk_wayland_display_has_pending;
596 display_class->queue_events = _gdk_wayland_display_queue_events;
597 display_class->get_default_group = gdk_wayland_display_get_default_group;
598 display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
599 display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
600 display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
601 display_class->store_clipboard = gdk_wayland_display_store_clipboard;
602 display_class->supports_shapes = gdk_wayland_display_supports_shapes;
603 display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
604 display_class->supports_composite = gdk_wayland_display_supports_composite;
605 display_class->list_devices = gdk_wayland_display_list_devices;
606 display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
607 display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
608 display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
609 display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
610 display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
611 display_class->get_cursor_for_pixbuf = _gdk_wayland_display_get_cursor_for_pixbuf;
612 display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
613 display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
614 display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
615 display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
616 display_class->get_next_serial = gdk_wayland_display_get_next_serial;
617 display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
618 display_class->event_data_copy = gdk_wayland_display_event_data_copy;
619 display_class->event_data_free = gdk_wayland_display_event_data_free;
620 display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
621 display_class->get_keymap = _gdk_wayland_display_get_keymap;
622 display_class->push_error_trap = gdk_wayland_display_push_error_trap;
623 display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
624 display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
625 display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
626 display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
627 display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
628 display_class->convert_selection = _gdk_wayland_display_convert_selection;
629 display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
630 display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
634 _gdk_wayland_display_init (GdkWaylandDisplay *display)
636 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
637 GDK_DISPLAY (display));
639 display->xkb_context = xkb_context_new (0);
643 _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display)
646 gchar *theme_name = NULL; /* FIXME: Do something here */
648 g_assert (wayland_display);
649 g_assert (wayland_display->shm);
651 _gdk_wayland_display_get_default_cursor_size (GDK_DISPLAY (wayland_display),
654 wayland_display->cursor_theme = wl_cursor_theme_load (theme_name,
656 wayland_display->shm);
660 _gdk_wayland_display_get_serial (GdkWaylandDisplay *wayland_display)
662 return wayland_display->serial;
666 _gdk_wayland_display_update_serial (GdkWaylandDisplay *wayland_display,
669 if (serial > wayland_display->serial)
670 wayland_display->serial = serial;