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 #include <wayland-egl.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"
39 G_DEFINE_TYPE (GdkDisplayWayland, _gdk_display_wayland, GDK_TYPE_DISPLAY)
42 gdk_input_init (GdkDisplay *display)
44 GdkDisplayWayland *display_wayland;
45 GdkDeviceManager *device_manager;
49 display_wayland = GDK_DISPLAY_WAYLAND (display);
50 device_manager = gdk_display_get_device_manager (display);
52 /* For backwards compatibility, just add
53 * floating devices that are not keyboards.
55 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
57 for (l = list; l; l = l->next)
61 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
64 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, l->data);
69 /* Now set "core" pointer to the first
70 * master device that is a pointer.
72 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
74 for (l = list; l; l = l->next)
78 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
81 display->core_pointer = device;
85 /* Add the core pointer to the devices list */
86 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, display->core_pointer);
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)
98 g_signal_emit_by_name (screen, "monitors-changed");
99 g_signal_emit_by_name (screen, "size-changed");
103 display_handle_mode(void *data,
104 struct wl_output *wl_output,
112 static const struct wl_output_listener output_listener = {
113 output_handle_geometry,
118 gdk_display_handle_global(struct wl_display *display, uint32_t id,
119 const char *interface, uint32_t version, void *data)
121 GdkDisplayWayland *display_wayland = data;
122 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
123 struct wl_input_device *input;
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,
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);
149 gdk_display_init_egl(GdkDisplay *display)
151 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (display);
152 EGLint major, minor, i;
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) }
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");
169 eglBindAPI(EGL_OPENGL_API);
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");
178 if (!eglMakeCurrent(display_wayland->egl_display,
179 NULL, NULL, display_wayland->egl_context)) {
180 fprintf(stderr, "faile to make context current\n");
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");
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;
196 fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
205 _gdk_wayland_display_open (const gchar *display_name)
207 struct wl_display *wl_display;
209 GdkDisplayWayland *display_wayland;
211 wl_display = wl_display_connect(display_name);
215 display = g_object_new (GDK_TYPE_DISPLAY_WAYLAND, NULL);
216 display_wayland = GDK_DISPLAY_WAYLAND (display);
218 display_wayland->wl_display = wl_display;
220 display_wayland->screen = _gdk_wayland_screen_new (display);
222 display->device_manager = _gdk_wayland_device_manager_new (display);
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);
228 gdk_display_init_egl(display);
230 display_wayland->event_source =
231 _gdk_wayland_display_event_source_new (display);
233 gdk_input_init (display);
235 g_signal_emit_by_name (display, "opened");
236 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
242 gdk_wayland_display_dispose (GObject *object)
244 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
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);
251 _gdk_screen_close (display_wayland->screen);
253 if (display_wayland->event_source)
255 g_source_destroy (display_wayland->event_source);
256 g_source_unref (display_wayland->event_source);
257 display_wayland->event_source = NULL;
260 eglTerminate(display_wayland->egl_display);
262 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->dispose (object);
266 gdk_wayland_display_finalize (GObject *object)
268 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
271 if (display_wayland->keymap)
272 g_object_unref (display_wayland->keymap);
274 /* input GdkDevice list */
275 g_list_free_full (display_wayland->input_devices, g_object_unref);
277 g_object_unref (display_wayland->screen);
279 g_free (display_wayland->startup_notification_id);
281 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->finalize (object);
285 gdk_wayland_display_get_name (GdkDisplay *display)
291 gdk_wayland_display_get_n_screens (GdkDisplay *display)
297 gdk_wayland_display_get_screen (GdkDisplay *display,
300 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
301 g_return_val_if_fail (screen_num == 0, NULL);
303 return GDK_DISPLAY_WAYLAND (display)->screen;
307 gdk_wayland_display_get_default_screen (GdkDisplay *display)
309 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
311 return GDK_DISPLAY_WAYLAND (display)->screen;
315 gdk_wayland_display_beep (GdkDisplay *display)
317 g_return_if_fail (GDK_IS_DISPLAY (display));
321 gdk_wayland_display_sync (GdkDisplay *display)
323 GdkDisplayWayland *display_wayland;
325 g_return_if_fail (GDK_IS_DISPLAY (display));
327 display_wayland = GDK_DISPLAY_WAYLAND (display);
329 wl_display_roundtrip(display_wayland->wl_display);
333 gdk_wayland_display_flush (GdkDisplay *display)
335 g_return_if_fail (GDK_IS_DISPLAY (display));
337 if (!display->closed)
338 _gdk_wayland_display_flush (display,
339 GDK_DISPLAY_WAYLAND (display)->event_source);
343 gdk_wayland_display_has_pending (GdkDisplay *display)
349 gdk_wayland_display_get_default_group (GdkDisplay *display)
351 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
358 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
364 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
372 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
378 gdk_wayland_display_store_clipboard (GdkDisplay *display,
379 GdkWindow *clipboard_window,
381 const GdkAtom *targets,
387 gdk_wayland_display_supports_shapes (GdkDisplay *display)
393 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
399 gdk_wayland_display_supports_composite (GdkDisplay *display)
405 gdk_wayland_display_list_devices (GdkDisplay *display)
407 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
409 return GDK_DISPLAY_WAYLAND (display)->input_devices;
413 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
418 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
420 /* Post the damage here instead? */
424 gdk_wayland_display_get_next_serial (GdkDisplay *display)
426 static gulong serial = 0;
431 _gdk_wayland_display_make_default (GdkDisplay *display)
436 * gdk_wayland_display_broadcast_startup_message:
437 * @display: a #GdkDisplay
438 * @message_type: startup notification message type ("new", "change",
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.)
444 * Sends a startup notification message of type @message_type to
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.
457 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
458 const char *message_type,
463 const char *key, *value, *p;
465 message = g_string_new (message_type);
466 g_string_append_c (message, ':');
468 va_start (ap, message_type);
469 while ((key = va_arg (ap, const char *)))
471 value = va_arg (ap, const char *);
475 g_string_append_printf (message, " %s=\"", key);
476 for (p = value; *p; p++)
483 g_string_append_c (message, '\\');
487 g_string_append_c (message, *p);
489 g_string_append_c (message, '\"');
493 printf ("startup message: %s\n", message->str);
495 g_string_free (message, TRUE);
499 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
500 const gchar *startup_id)
502 gdk_wayland_display_broadcast_startup_message (display, "remove",
508 gdk_wayland_display_event_data_copy (GdkDisplay *display,
515 gdk_wayland_display_event_data_free (GdkDisplay *display,
521 gdk_wayland_display_get_keymap (GdkDisplay *display)
523 GdkDisplayWayland *display_wayland;
525 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
526 display_wayland = GDK_DISPLAY_WAYLAND (display);
528 if (!display_wayland->keymap)
529 display_wayland->keymap = _gdk_wayland_keymap_new (display);
531 return display_wayland->keymap;
535 gdk_wayland_display_push_error_trap (GdkDisplay *display)
540 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
547 _gdk_display_wayland_class_init (GdkDisplayWaylandClass * class)
549 GObjectClass *object_class = G_OBJECT_CLASS (class);
550 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
552 object_class->dispose = gdk_wayland_display_dispose;
553 object_class->finalize = gdk_wayland_display_finalize;
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;
602 _gdk_display_wayland_init (GdkDisplayWayland *display)
604 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
605 GDK_DISPLAY (display));