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)
102 g_signal_emit_by_name (screen, "monitors-changed");
103 g_signal_emit_by_name (screen, "size-changed");
107 display_handle_mode(void *data,
108 struct wl_output *wl_output,
116 static const struct wl_output_listener output_listener = {
117 output_handle_geometry,
122 gdk_display_handle_global(struct wl_display *display, uint32_t id,
123 const char *interface, uint32_t version, void *data)
125 GdkWaylandDisplay *display_wayland = data;
126 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
127 struct wl_seat *seat;
129 if (strcmp(interface, "wl_compositor") == 0) {
130 display_wayland->compositor =
131 wl_display_bind(display, id, &wl_compositor_interface);
132 } else if (strcmp(interface, "wl_shm") == 0) {
133 display_wayland->shm = wl_display_bind(display, id, &wl_shm_interface);
135 /* SHM interface is prerequisite */
136 _gdk_wayland_display_load_cursor_theme(display_wayland);
137 } else if (strcmp(interface, "wl_shell") == 0) {
138 display_wayland->shell = wl_display_bind(display, id, &wl_shell_interface);
139 } else if (strcmp(interface, "wl_output") == 0) {
140 display_wayland->output =
141 wl_display_bind(display, id, &wl_output_interface);
142 wl_output_add_listener(display_wayland->output,
143 &output_listener, display_wayland);
144 } else if (strcmp(interface, "wl_seat") == 0) {
145 seat = wl_display_bind (display, id, &wl_seat_interface);
146 _gdk_wayland_device_manager_add_device (gdk_display->device_manager,
148 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
149 display_wayland->data_device_manager =
150 wl_display_bind(display, id,
151 &wl_data_device_manager_interface);
155 #ifdef GDK_WAYLAND_USE_EGL
157 gdk_display_init_egl(GdkDisplay *display)
159 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
160 EGLint major, minor, i;
163 static const struct { const char *f; unsigned int offset; }
164 extension_functions[] = {
165 { "glEGLImageTargetTexture2DOES", offsetof(GdkWaylandDisplay, image_target_texture_2d) },
166 { "eglCreateImageKHR", offsetof(GdkWaylandDisplay, create_image) },
167 { "eglDestroyImageKHR", offsetof(GdkWaylandDisplay, destroy_image) }
170 display_wayland->egl_display =
171 eglGetDisplay(display_wayland->wl_display);
172 if (!eglInitialize(display_wayland->egl_display, &major, &minor)) {
173 fprintf(stderr, "failed to initialize display\n");
177 eglBindAPI(EGL_OPENGL_API);
179 display_wayland->egl_context =
180 eglCreateContext(display_wayland->egl_display, NULL, EGL_NO_CONTEXT, NULL);
181 if (display_wayland->egl_context == NULL) {
182 fprintf(stderr, "failed to create context\n");
186 if (!eglMakeCurrent(display_wayland->egl_display,
187 NULL, NULL, display_wayland->egl_context)) {
188 fprintf(stderr, "faile to make context current\n");
192 display_wayland->cairo_device =
193 cairo_egl_device_create(display_wayland->egl_display,
194 display_wayland->egl_context);
195 if (cairo_device_status (display_wayland->cairo_device) != CAIRO_STATUS_SUCCESS) {
196 fprintf(stderr, "failed to get cairo drm device\n");
200 for (i = 0; i < G_N_ELEMENTS(extension_functions); i++) {
201 p = eglGetProcAddress(extension_functions[i].f);
202 *(void **) ((char *) display_wayland + extension_functions[i].offset) = p;
204 fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
214 _gdk_wayland_display_open (const gchar *display_name)
216 struct wl_display *wl_display;
218 GdkWaylandDisplay *display_wayland;
220 wl_display = wl_display_connect(display_name);
224 display = g_object_new (GDK_TYPE_WAYLAND_DISPLAY, NULL);
225 display_wayland = GDK_WAYLAND_DISPLAY (display);
227 display_wayland->wl_display = wl_display;
229 display_wayland->screen = _gdk_wayland_screen_new (display);
231 display->device_manager = _gdk_wayland_device_manager_new (display);
233 /* Set up listener so we'll catch all events. */
234 wl_display_add_global_listener(display_wayland->wl_display,
235 gdk_display_handle_global, display_wayland);
237 #ifdef GDK_WAYLAND_USE_EGL
238 gdk_display_init_egl(display);
240 wl_display_iterate(wl_display, WL_DISPLAY_READABLE);
241 wl_display_roundtrip(wl_display);
244 display_wayland->event_source =
245 _gdk_wayland_display_event_source_new (display);
247 gdk_input_init (display);
249 g_signal_emit_by_name (display, "opened");
250 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
256 gdk_wayland_display_dispose (GObject *object)
258 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
260 _gdk_wayland_display_manager_remove_display (gdk_display_manager_get (),
261 GDK_DISPLAY (display_wayland));
262 g_list_foreach (display_wayland->input_devices,
263 (GFunc) g_object_run_dispose, NULL);
265 _gdk_screen_close (display_wayland->screen);
267 if (display_wayland->event_source)
269 g_source_destroy (display_wayland->event_source);
270 g_source_unref (display_wayland->event_source);
271 display_wayland->event_source = NULL;
274 #ifdef GDK_WAYLAND_USE_EGL
275 eglTerminate(display_wayland->egl_display);
278 G_OBJECT_CLASS (_gdk_wayland_display_parent_class)->dispose (object);
282 gdk_wayland_display_finalize (GObject *object)
284 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
287 if (display_wayland->keymap)
288 g_object_unref (display_wayland->keymap);
290 /* input GdkDevice list */
291 g_list_free_full (display_wayland->input_devices, g_object_unref);
293 g_object_unref (display_wayland->screen);
295 g_free (display_wayland->startup_notification_id);
297 G_OBJECT_CLASS (_gdk_wayland_display_parent_class)->finalize (object);
301 gdk_wayland_display_get_name (GdkDisplay *display)
307 gdk_wayland_display_get_n_screens (GdkDisplay *display)
313 gdk_wayland_display_get_screen (GdkDisplay *display,
316 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
317 g_return_val_if_fail (screen_num == 0, NULL);
319 return GDK_WAYLAND_DISPLAY (display)->screen;
323 gdk_wayland_display_get_default_screen (GdkDisplay *display)
325 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
327 return GDK_WAYLAND_DISPLAY (display)->screen;
331 gdk_wayland_display_beep (GdkDisplay *display)
333 g_return_if_fail (GDK_IS_DISPLAY (display));
337 gdk_wayland_display_sync (GdkDisplay *display)
339 GdkWaylandDisplay *display_wayland;
341 g_return_if_fail (GDK_IS_DISPLAY (display));
343 display_wayland = GDK_WAYLAND_DISPLAY (display);
345 wl_display_roundtrip(display_wayland->wl_display);
349 gdk_wayland_display_flush (GdkDisplay *display)
351 g_return_if_fail (GDK_IS_DISPLAY (display));
353 if (!display->closed)
354 _gdk_wayland_display_flush (display,
355 GDK_WAYLAND_DISPLAY (display)->event_source);
359 gdk_wayland_display_has_pending (GdkDisplay *display)
365 gdk_wayland_display_get_default_group (GdkDisplay *display)
367 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
374 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
380 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
388 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
394 gdk_wayland_display_store_clipboard (GdkDisplay *display,
395 GdkWindow *clipboard_window,
397 const GdkAtom *targets,
403 gdk_wayland_display_supports_shapes (GdkDisplay *display)
409 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
415 gdk_wayland_display_supports_composite (GdkDisplay *display)
421 gdk_wayland_display_list_devices (GdkDisplay *display)
423 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
425 return GDK_WAYLAND_DISPLAY (display)->input_devices;
429 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
434 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
436 /* Post the damage here instead? */
440 gdk_wayland_display_get_next_serial (GdkDisplay *display)
442 static gulong serial = 0;
447 _gdk_wayland_display_make_default (GdkDisplay *display)
452 * gdk_wayland_display_broadcast_startup_message:
453 * @display: a #GdkDisplay
454 * @message_type: startup notification message type ("new", "change",
456 * @...: a list of key/value pairs (as strings), terminated by a
457 * %NULL key. (A %NULL value for a key will cause that key to be
458 * skipped in the output.)
460 * Sends a startup notification message of type @message_type to
463 * This is a convenience function for use by code that implements the
464 * freedesktop startup notification specification. Applications should
465 * not normally need to call it directly. See the <ulink
466 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
467 * Notification Protocol specification</ulink> for
468 * definitions of the message types and keys that can be used.
473 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
474 const char *message_type,
479 const char *key, *value, *p;
481 message = g_string_new (message_type);
482 g_string_append_c (message, ':');
484 va_start (ap, message_type);
485 while ((key = va_arg (ap, const char *)))
487 value = va_arg (ap, const char *);
491 g_string_append_printf (message, " %s=\"", key);
492 for (p = value; *p; p++)
499 g_string_append_c (message, '\\');
503 g_string_append_c (message, *p);
505 g_string_append_c (message, '\"');
509 printf ("startup message: %s\n", message->str);
511 g_string_free (message, TRUE);
515 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
516 const gchar *startup_id)
518 gdk_wayland_display_broadcast_startup_message (display, "remove",
524 gdk_wayland_display_event_data_copy (GdkDisplay *display,
531 gdk_wayland_display_event_data_free (GdkDisplay *display,
537 gdk_wayland_display_get_keymap (GdkDisplay *display)
539 GdkWaylandDisplay *display_wayland;
541 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
542 display_wayland = GDK_WAYLAND_DISPLAY (display);
544 if (!display_wayland->keymap)
545 display_wayland->keymap = _gdk_wayland_keymap_new (display);
547 return display_wayland->keymap;
551 gdk_wayland_display_push_error_trap (GdkDisplay *display)
556 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
563 _gdk_wayland_display_class_init (GdkWaylandDisplayClass * class)
565 GObjectClass *object_class = G_OBJECT_CLASS (class);
566 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
568 object_class->dispose = gdk_wayland_display_dispose;
569 object_class->finalize = gdk_wayland_display_finalize;
571 display_class->window_type = _gdk_wayland_window_get_type ();
572 display_class->get_name = gdk_wayland_display_get_name;
573 display_class->get_n_screens = gdk_wayland_display_get_n_screens;
574 display_class->get_screen = gdk_wayland_display_get_screen;
575 display_class->get_default_screen = gdk_wayland_display_get_default_screen;
576 display_class->beep = gdk_wayland_display_beep;
577 display_class->sync = gdk_wayland_display_sync;
578 display_class->flush = gdk_wayland_display_flush;
579 display_class->has_pending = gdk_wayland_display_has_pending;
580 display_class->queue_events = _gdk_wayland_display_queue_events;
581 display_class->get_default_group = gdk_wayland_display_get_default_group;
582 display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
583 display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
584 display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
585 display_class->store_clipboard = gdk_wayland_display_store_clipboard;
586 display_class->supports_shapes = gdk_wayland_display_supports_shapes;
587 display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
588 display_class->supports_composite = gdk_wayland_display_supports_composite;
589 display_class->list_devices = gdk_wayland_display_list_devices;
590 display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
591 display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
592 display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
593 display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
594 display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
595 display_class->get_cursor_for_pixbuf = _gdk_wayland_display_get_cursor_for_pixbuf;
596 display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
597 display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
598 display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
599 display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
600 display_class->get_next_serial = gdk_wayland_display_get_next_serial;
601 display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
602 display_class->event_data_copy = gdk_wayland_display_event_data_copy;
603 display_class->event_data_free = gdk_wayland_display_event_data_free;
604 display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
605 display_class->get_keymap = gdk_wayland_display_get_keymap;
606 display_class->push_error_trap = gdk_wayland_display_push_error_trap;
607 display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
608 display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
609 display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
610 display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
611 display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
612 display_class->convert_selection = _gdk_wayland_display_convert_selection;
613 display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
614 display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
618 _gdk_wayland_display_init (GdkWaylandDisplay *display)
620 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
621 GDK_DISPLAY (display));
625 _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display)
628 gchar *theme_name = NULL; /* FIXME: Do something here */
630 g_assert (wayland_display);
631 g_assert (wayland_display->shm);
633 _gdk_wayland_display_get_default_cursor_size (GDK_DISPLAY (wayland_display),
636 wayland_display->cursor_theme = wl_cursor_theme_load (theme_name,
638 wayland_display->shm);
642 _gdk_wayland_display_get_serial (GdkWaylandDisplay *wayland_display)
644 return wayland_display->serial;
648 _gdk_wayland_display_update_serial (GdkWaylandDisplay *wayland_display,
651 if (serial > wayland_display->serial)
652 wayland_display->serial = serial;