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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #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 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)
103 window = wl_surface_get_user_data(surface);
105 display = gdk_window_get_display (window);
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;
113 _gdk_window_update_size (window);
114 _gdk_wayland_window_update_size (window, width, height, edges);
116 g_object_ref(window);
118 _gdk_wayland_display_deliver_event (display, event);
121 static const struct wl_shell_listener shell_listener = {
122 shell_handle_configure,
126 output_handle_geometry(void *data,
127 struct wl_output *output,
128 int32_t x, int32_t y, int32_t width, int32_t height)
131 g_signal_emit_by_name (screen, "monitors-changed");
132 g_signal_emit_by_name (screen, "size-changed");
136 static const struct wl_output_listener output_listener = {
137 output_handle_geometry,
141 gdk_display_handle_global(struct wl_display *display, uint32_t id,
142 const char *interface, uint32_t version, void *data)
144 GdkDisplayWayland *display_wayland = data;
145 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
146 struct wl_input_device *input;
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,
168 gdk_display_init_egl(GdkDisplay *display)
170 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (display);
171 EGLint major, minor, i;
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) }
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");
188 eglBindAPI(EGL_OPENGL_API);
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");
197 if (!eglMakeCurrent(display_wayland->egl_display,
198 NULL, NULL, display_wayland->egl_context)) {
199 fprintf(stderr, "faile to make context current\n");
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");
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;
215 fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
224 _gdk_wayland_display_open (const gchar *display_name)
226 struct wl_display *wl_display;
228 GdkDisplayWayland *display_wayland;
230 wl_display = wl_display_connect(display_name);
234 display = g_object_new (GDK_TYPE_DISPLAY_WAYLAND, NULL);
235 display_wayland = GDK_DISPLAY_WAYLAND (display);
237 display_wayland->wl_display = wl_display;
239 display_wayland->native_display = wl_egl_display_create(wl_display);
240 if (display_wayland->native_display == NULL) {
241 wl_display_destroy(wl_display);
245 display_wayland->screen = _gdk_wayland_screen_new (display);
247 display->device_manager = _gdk_wayland_device_manager_new (display);
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);
253 gdk_display_init_egl(display);
255 display_wayland->event_source = _gdk_wayland_display_event_source_new (display);
257 gdk_input_init (display);
259 g_signal_emit_by_name (display, "opened");
260 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
266 gdk_wayland_display_dispose (GObject *object)
268 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
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);
275 _gdk_screen_close (display_wayland->screen);
277 if (display_wayland->event_source)
279 g_source_destroy (display_wayland->event_source);
280 g_source_unref (display_wayland->event_source);
281 display_wayland->event_source = NULL;
284 eglTerminate(display_wayland->egl_display);
285 wl_egl_display_destroy(display_wayland->native_display);
287 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->dispose (object);
291 gdk_wayland_display_finalize (GObject *object)
293 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
296 if (display_wayland->keymap)
297 g_object_unref (display_wayland->keymap);
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);
303 g_object_unref (display_wayland->screen);
305 g_free (display_wayland->startup_notification_id);
307 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->finalize (object);
310 static G_CONST_RETURN gchar *
311 gdk_wayland_display_get_name (GdkDisplay *display)
317 gdk_wayland_display_get_n_screens (GdkDisplay *display)
323 gdk_wayland_display_get_screen (GdkDisplay *display,
326 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
327 g_return_val_if_fail (screen_num == 0, NULL);
329 return GDK_DISPLAY_WAYLAND (display)->screen;
333 gdk_wayland_display_get_default_screen (GdkDisplay *display)
335 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
337 return GDK_DISPLAY_WAYLAND (display)->screen;
341 gdk_wayland_display_beep (GdkDisplay *display)
343 g_return_if_fail (GDK_IS_DISPLAY (display));
347 sync_callback(void *data)
349 gboolean *done = data;
355 gdk_wayland_display_sync (GdkDisplay *display)
357 GdkDisplayWayland *display_wayland;
360 g_return_if_fail (GDK_IS_DISPLAY (display));
362 display_wayland = GDK_DISPLAY_WAYLAND (display);
364 wl_display_sync_callback(display_wayland->wl_display, sync_callback, &done);
365 wl_display_iterate(display_wayland->wl_display, WL_DISPLAY_WRITABLE);
367 wl_display_iterate(display_wayland->wl_display, WL_DISPLAY_READABLE);
371 gdk_wayland_display_flush (GdkDisplay *display)
373 g_return_if_fail (GDK_IS_DISPLAY (display));
375 if (!display->closed)
376 _gdk_wayland_display_flush (display,
377 GDK_DISPLAY_WAYLAND (display)->event_source);
381 gdk_wayland_display_has_pending (GdkDisplay *display)
387 gdk_wayland_display_get_default_group (GdkDisplay *display)
389 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
396 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
402 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
410 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
416 gdk_wayland_display_store_clipboard (GdkDisplay *display,
417 GdkWindow *clipboard_window,
419 const GdkAtom *targets,
425 gdk_wayland_display_supports_shapes (GdkDisplay *display)
431 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
437 gdk_wayland_display_supports_composite (GdkDisplay *display)
443 gdk_wayland_display_list_devices (GdkDisplay *display)
445 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
447 return GDK_DISPLAY_WAYLAND (display)->input_devices;
451 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
456 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
458 /* Post the damage here instead? */
462 gdk_wayland_display_get_next_serial (GdkDisplay *display)
468 _gdk_wayland_display_make_default (GdkDisplay *display)
473 * gdk_wayland_display_broadcast_startup_message:
474 * @display: a #GdkDisplay
475 * @message_type: startup notification message type ("new", "change",
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.)
481 * Sends a startup notification message of type @message_type to
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.
494 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
495 const char *message_type,
500 const char *key, *value, *p;
502 message = g_string_new (message_type);
503 g_string_append_c (message, ':');
505 va_start (ap, message_type);
506 while ((key = va_arg (ap, const char *)))
508 value = va_arg (ap, const char *);
512 g_string_append_printf (message, " %s=\"", key);
513 for (p = value; *p; p++)
520 g_string_append_c (message, '\\');
524 g_string_append_c (message, *p);
526 g_string_append_c (message, '\"');
530 printf ("startup message: %s\n", message->str);
532 g_string_free (message, TRUE);
536 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
537 const gchar *startup_id)
539 gdk_wayland_display_broadcast_startup_message (display, "remove",
545 gdk_wayland_display_event_data_copy (GdkDisplay *display,
552 gdk_wayland_display_event_data_free (GdkDisplay *display,
558 gdk_wayland_display_get_keymap (GdkDisplay *display)
560 GdkDisplayWayland *display_wayland;
562 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
563 display_wayland = GDK_DISPLAY_WAYLAND (display);
565 if (!display_wayland->keymap)
566 display_wayland->keymap = _gdk_wayland_keymap_new (display);
568 return display_wayland->keymap;
572 gdk_wayland_display_push_error_trap (GdkDisplay *display)
577 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
584 _gdk_display_wayland_class_init (GdkDisplayWaylandClass * class)
586 GObjectClass *object_class = G_OBJECT_CLASS (class);
587 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
589 object_class->dispose = gdk_wayland_display_dispose;
590 object_class->finalize = gdk_wayland_display_finalize;
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;
639 _gdk_display_wayland_init (GdkDisplayWayland *display)
641 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
642 GDK_DISPLAY (display));