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 "gdkscreen-wayland.h"
36 #include "gdkinternals.h"
37 #include "gdkdeviceprivate.h"
38 #include "gdkdevicemanager.h"
39 #include "gdkkeysprivate.h"
41 typedef struct _GdkEventTypeWayland GdkEventTypeWayland;
43 struct _GdkEventTypeWayland
49 G_DEFINE_TYPE (GdkDisplayWayland, _gdk_display_wayland, GDK_TYPE_DISPLAY)
56 * GDK_SELECTION_ REQUEST
57 * GDK_SELECTION_NOTIFY
60 * new keyboard mapping: _gdk_keymap_keys_changed (display);
62 * Selection owner change: GDK_OWNER_CHANGE;
64 * Screen size changes: _gdk_wayland_screen_size_changed (screen, xevent);
66 * XkbStateNotify: _gdk_keymap_state_changed (display, xevent);
70 gdk_input_init (GdkDisplay *display)
72 GdkDisplayWayland *display_wayland;
73 GdkDeviceManager *device_manager;
77 display_wayland = GDK_DISPLAY_WAYLAND (display);
78 device_manager = gdk_display_get_device_manager (display);
80 /* For backwards compatibility, just add
81 * floating devices that are not keyboards.
83 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
85 for (l = list; l; l = l->next)
89 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
92 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, l->data);
97 /* Now set "core" pointer to the first
98 * master device that is a pointer.
100 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
102 for (l = list; l; l = l->next)
106 if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
109 display->core_pointer = device;
113 /* Add the core pointer to the devices list */
114 display_wayland->input_devices = g_list_prepend (display_wayland->input_devices, display->core_pointer);
120 shell_handle_configure(void *data, struct wl_shell *shell,
121 uint32_t time, uint32_t edges,
122 struct wl_surface *surface,
123 int32_t width, int32_t height)
129 window = wl_surface_get_user_data(surface);
131 printf("got configure: window %p, %dx%d, edges %d\n",
132 window, width, height, edges);
134 display = gdk_window_get_display (window);
136 event = gdk_event_new (GDK_CONFIGURE);
137 event->configure.window = window;
138 event->configure.send_event = FALSE;
139 event->configure.width = width;
140 event->configure.height = height;
142 window->width = width;
143 window->height = height;
145 _gdk_window_update_size (window);
146 _gdk_wayland_window_update_size (window);
148 g_object_ref(window);
150 _gdk_wayland_display_deliver_event (display, event);
153 static const struct wl_shell_listener shell_listener = {
154 shell_handle_configure,
158 output_handle_geometry(void *data,
159 struct wl_output *output,
160 int32_t x, int32_t y, int32_t width, int32_t height)
163 g_signal_emit_by_name (screen, "monitors-changed");
164 g_signal_emit_by_name (screen, "size-changed");
168 static const struct wl_output_listener output_listener = {
169 output_handle_geometry,
173 gdk_display_handle_global(struct wl_display *display, uint32_t id,
174 const char *interface, uint32_t version, void *data)
176 GdkDisplayWayland *display_wayland = data;
177 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
178 struct wl_input_device *input;
180 if (strcmp(interface, "compositor") == 0) {
181 display_wayland->compositor = wl_compositor_create(display, id);
182 } else if (strcmp(interface, "shm") == 0) {
183 display_wayland->shm = wl_shm_create(display, id);
184 } else if (strcmp(interface, "shell") == 0) {
185 display_wayland->shell = wl_shell_create(display, id);
186 wl_shell_add_listener(display_wayland->shell,
187 &shell_listener, display_wayland);
188 } else if (strcmp(interface, "output") == 0) {
189 display_wayland->output = wl_output_create(display, id);
190 wl_output_add_listener(display_wayland->output,
191 &output_listener, display_wayland);
192 } else if (strcmp(interface, "input_device") == 0) {
193 input = wl_input_device_create(display, id);
194 _gdk_wayland_device_manager_add_device (gdk_display->device_manager,
200 gdk_display_init_egl(GdkDisplay *display)
202 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (display);
203 EGLint major, minor, i;
206 static const struct { const char *f; unsigned int offset; }
207 extension_functions[] = {
208 { "glEGLImageTargetTexture2DOES", offsetof(GdkDisplayWayland, image_target_texture_2d) },
209 { "eglCreateImageKHR", offsetof(GdkDisplayWayland, create_image) },
210 { "eglDestroyImageKHR", offsetof(GdkDisplayWayland, destroy_image) }
213 display_wayland->egl_display =
214 eglGetDisplay(display_wayland->native_display);
215 if (!eglInitialize(display_wayland->egl_display, &major, &minor)) {
216 fprintf(stderr, "failed to initialize display\n");
220 eglBindAPI(EGL_OPENGL_API);
222 display_wayland->egl_context =
223 eglCreateContext(display_wayland->egl_display, NULL, EGL_NO_CONTEXT, NULL);
224 if (display_wayland->egl_context == NULL) {
225 fprintf(stderr, "failed to create context\n");
229 if (!eglMakeCurrent(display_wayland->egl_display,
230 NULL, NULL, display_wayland->egl_context)) {
231 fprintf(stderr, "faile to make context current\n");
235 display_wayland->cairo_device =
236 cairo_egl_device_create(display_wayland->egl_display,
237 display_wayland->egl_context);
238 if (cairo_device_status (display_wayland->cairo_device) != CAIRO_STATUS_SUCCESS) {
239 fprintf(stderr, "failed to get cairo drm device\n");
243 for (i = 0; i < G_N_ELEMENTS(extension_functions); i++) {
244 p = eglGetProcAddress(extension_functions[i].f);
245 *(void **) ((char *) display_wayland + extension_functions[i].offset) = p;
247 fprintf(stderr, "failed to look up %s\n", extension_functions[i].f);
252 fprintf(stderr, "egl initialized\n");
258 _gdk_wayland_display_open (const gchar *display_name)
260 struct wl_display *wl_display;
262 GdkDisplayWayland *display_wayland;
266 wl_display = wl_display_connect(display_name);
270 display = g_object_new (GDK_TYPE_DISPLAY_WAYLAND, NULL);
271 display_wayland = GDK_DISPLAY_WAYLAND (display);
273 display_wayland->wl_display = wl_display;
275 display_wayland->native_display = wl_egl_display_create(wl_display);
276 if (display_wayland->native_display == NULL) {
277 wl_display_destroy(wl_display);
281 /* initialize the display's screens */
282 display_wayland->screens = g_new (GdkScreen *, 1);
283 for (i = 0; i < 1; i++)
284 display_wayland->screens[i] = _gdk_wayland_screen_new (display);
286 /*set the default screen */
287 display_wayland->default_screen = display_wayland->screens[0];
289 display->device_manager = _gdk_wayland_device_manager_new (display);
291 /* Set up listener so we'll catch all events. */
292 wl_display_add_global_listener(display_wayland->wl_display,
293 gdk_display_handle_global, display_wayland);
295 gdk_display_init_egl(display);
297 display_wayland->event_source = _gdk_wayland_display_event_source_new (display);
299 gdk_input_init (display);
301 g_signal_emit_by_name (display, "opened");
302 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
308 gdk_wayland_display_dispose (GObject *object)
310 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
313 _gdk_wayland_display_manager_remove_display (gdk_display_manager_get (),
314 GDK_DISPLAY (display_wayland));
315 g_list_foreach (display_wayland->input_devices,
316 (GFunc) g_object_run_dispose, NULL);
318 for (i = 0; i < 1; i++)
319 _gdk_screen_close (display_wayland->screens[i]);
321 if (display_wayland->event_source)
323 g_source_destroy (display_wayland->event_source);
324 g_source_unref (display_wayland->event_source);
325 display_wayland->event_source = NULL;
328 eglTerminate(display_wayland->egl_display);
329 wl_egl_display_destroy(display_wayland->native_display);
331 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->dispose (object);
335 gdk_wayland_display_finalize (GObject *object)
337 GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (object);
341 if (display_wayland->keymap)
342 g_object_unref (display_wayland->keymap);
344 /* input GdkDevice list */
345 g_list_foreach (display_wayland->input_devices, (GFunc) g_object_unref, NULL);
346 g_list_free (display_wayland->input_devices);
348 /* input GdkWindow list */
349 g_list_foreach (display_wayland->input_windows, (GFunc) g_free, NULL);
350 g_list_free (display_wayland->input_windows);
352 /* Free all GdkScreens */
353 for (i = 0; i < 1; i++)
354 g_object_unref (display_wayland->screens[i]);
355 g_free (display_wayland->screens);
357 g_free (display_wayland->startup_notification_id);
359 G_OBJECT_CLASS (_gdk_display_wayland_parent_class)->finalize (object);
362 static G_CONST_RETURN gchar *
363 gdk_wayland_display_get_name (GdkDisplay *display)
369 gdk_wayland_display_get_n_screens (GdkDisplay *display)
375 gdk_wayland_display_get_screen (GdkDisplay *display,
378 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
379 g_return_val_if_fail (screen_num == 0, NULL);
381 return GDK_DISPLAY_WAYLAND (display)->screens[0];
385 gdk_wayland_display_get_default_screen (GdkDisplay *display)
387 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
389 return GDK_DISPLAY_WAYLAND (display)->default_screen;
393 gdk_wayland_display_beep (GdkDisplay *display)
395 g_return_if_fail (GDK_IS_DISPLAY (display));
399 sync_callback(void *data)
401 gboolean *done = data;
407 gdk_wayland_display_sync (GdkDisplay *display)
409 GdkDisplayWayland *display_wayland;
412 g_return_if_fail (GDK_IS_DISPLAY (display));
414 display_wayland = GDK_DISPLAY_WAYLAND (display);
416 wl_display_sync_callback(display_wayland->wl_display, sync_callback, &done);
417 wl_display_iterate(display_wayland->wl_display, WL_DISPLAY_WRITABLE);
419 wl_display_iterate(display_wayland->wl_display, WL_DISPLAY_READABLE);
423 gdk_wayland_display_flush (GdkDisplay *display)
425 g_return_if_fail (GDK_IS_DISPLAY (display));
427 if (!display->closed)
428 _gdk_wayland_display_flush (display,
429 GDK_DISPLAY_WAYLAND (display)->event_source);
433 gdk_wayland_display_has_pending (GdkDisplay *display)
439 gdk_wayland_display_get_default_group (GdkDisplay *display)
441 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
448 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
454 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
462 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
468 gdk_wayland_display_store_clipboard (GdkDisplay *display,
469 GdkWindow *clipboard_window,
471 const GdkAtom *targets,
477 gdk_wayland_display_supports_shapes (GdkDisplay *display)
483 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
489 gdk_wayland_display_supports_composite (GdkDisplay *display)
495 gdk_wayland_display_list_devices (GdkDisplay *display)
497 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
499 return GDK_DISPLAY_WAYLAND (display)->input_devices;
503 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
508 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
510 /* Post the damage here instead? */
514 gdk_wayland_display_get_next_serial (GdkDisplay *display)
520 _gdk_wayland_display_make_default (GdkDisplay *display)
525 * gdk_wayland_display_broadcast_startup_message:
526 * @display: a #GdkDisplay
527 * @message_type: startup notification message type ("new", "change",
529 * @...: a list of key/value pairs (as strings), terminated by a
530 * %NULL key. (A %NULL value for a key will cause that key to be
531 * skipped in the output.)
533 * Sends a startup notification message of type @message_type to
536 * This is a convenience function for use by code that implements the
537 * freedesktop startup notification specification. Applications should
538 * not normally need to call it directly. See the <ulink
539 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
540 * Notification Protocol specification</ulink> for
541 * definitions of the message types and keys that can be used.
546 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
547 const char *message_type,
552 const char *key, *value, *p;
554 message = g_string_new (message_type);
555 g_string_append_c (message, ':');
557 va_start (ap, message_type);
558 while ((key = va_arg (ap, const char *)))
560 value = va_arg (ap, const char *);
564 g_string_append_printf (message, " %s=\"", key);
565 for (p = value; *p; p++)
572 g_string_append_c (message, '\\');
576 g_string_append_c (message, *p);
578 g_string_append_c (message, '\"');
582 printf ("startup message: %s\n", message->str);
584 g_string_free (message, TRUE);
588 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
589 const gchar *startup_id)
591 gdk_wayland_display_broadcast_startup_message (display, "remove",
597 gdk_wayland_display_event_data_copy (GdkDisplay *display,
604 gdk_wayland_display_event_data_free (GdkDisplay *display,
610 gdk_wayland_display_get_keymap (GdkDisplay *display)
612 GdkDisplayWayland *display_wayland;
614 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
615 display_wayland = GDK_DISPLAY_WAYLAND (display);
617 if (!display_wayland->keymap)
618 display_wayland->keymap = _gdk_wayland_keymap_new (display);
620 return display_wayland->keymap;
624 gdk_wayland_display_push_error_trap (GdkDisplay *display)
629 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
636 _gdk_display_wayland_class_init (GdkDisplayWaylandClass * class)
638 GObjectClass *object_class = G_OBJECT_CLASS (class);
639 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
641 object_class->dispose = gdk_wayland_display_dispose;
642 object_class->finalize = gdk_wayland_display_finalize;
644 display_class->window_type = _gdk_wayland_window_get_type ();
645 display_class->get_name = gdk_wayland_display_get_name;
646 display_class->get_n_screens = gdk_wayland_display_get_n_screens;
647 display_class->get_screen = gdk_wayland_display_get_screen;
648 display_class->get_default_screen = gdk_wayland_display_get_default_screen;
649 display_class->beep = gdk_wayland_display_beep;
650 display_class->sync = gdk_wayland_display_sync;
651 display_class->flush = gdk_wayland_display_flush;
652 display_class->has_pending = gdk_wayland_display_has_pending;
653 display_class->queue_events = _gdk_wayland_display_queue_events;
654 display_class->get_default_group = gdk_wayland_display_get_default_group;
655 display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
656 display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
657 display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
658 display_class->store_clipboard = gdk_wayland_display_store_clipboard;
659 display_class->supports_shapes = gdk_wayland_display_supports_shapes;
660 display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
661 display_class->supports_composite = gdk_wayland_display_supports_composite;
662 display_class->list_devices = gdk_wayland_display_list_devices;
663 display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
664 display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
665 display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
666 display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
667 display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
668 display_class->get_cursor_for_pixbuf = _gdk_wayland_display_get_cursor_for_pixbuf;
669 display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
670 display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
671 display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
672 display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
673 display_class->get_next_serial = gdk_wayland_display_get_next_serial;
674 display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
675 display_class->event_data_copy = gdk_wayland_display_event_data_copy;
676 display_class->event_data_free = gdk_wayland_display_event_data_free;
677 display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
678 display_class->get_keymap = gdk_wayland_display_get_keymap;
679 display_class->push_error_trap = gdk_wayland_display_push_error_trap;
680 display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
681 display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
682 display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
683 display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
684 display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
685 display_class->convert_selection = _gdk_wayland_display_convert_selection;
686 display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
687 display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
691 _gdk_display_wayland_init (GdkDisplayWayland *display)
693 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
694 GDK_DISPLAY (display));