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/>.
27 #include "gdkwayland.h"
28 #include "gdkdisplay.h"
29 #include "gdkdisplay-wayland.h"
30 #include "gdkscreen.h"
31 #include "gdkinternals.h"
32 #include "gdkdeviceprivate.h"
33 #include "gdkdevicemanager.h"
34 #include "gdkkeysprivate.h"
35 #include "gdkprivate-wayland.h"
37 static void _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display);
39 G_DEFINE_TYPE (GdkWaylandDisplay, gdk_wayland_display, GDK_TYPE_DISPLAY)
42 gdk_input_init (GdkDisplay *display)
44 GdkWaylandDisplay *display_wayland;
45 GdkDeviceManager *device_manager;
49 display_wayland = GDK_WAYLAND_DISPLAY (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,
99 g_signal_emit_by_name (screen, "monitors-changed");
100 g_signal_emit_by_name (screen, "size-changed");
104 display_handle_mode(void *data,
105 struct wl_output *wl_output,
113 static const struct wl_output_listener output_listener = {
114 output_handle_geometry,
119 gdk_registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
120 const char *interface, uint32_t version)
122 GdkWaylandDisplay *display_wayland = data;
123 GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (data);
124 struct wl_seat *seat;
126 if (strcmp(interface, "wl_compositor") == 0) {
127 display_wayland->compositor =
128 wl_registry_bind(display_wayland->wl_registry, id, &wl_compositor_interface, 1);
129 } else if (strcmp(interface, "wl_shm") == 0) {
130 display_wayland->shm =
131 wl_registry_bind(display_wayland->wl_registry, id, &wl_shm_interface, 1);
133 /* SHM interface is prerequisite */
134 _gdk_wayland_display_load_cursor_theme(display_wayland);
135 } else if (strcmp(interface, "wl_shell") == 0) {
136 display_wayland->shell =
137 wl_registry_bind(display_wayland->wl_registry, id, &wl_shell_interface, 1);
138 } else if (strcmp(interface, "wl_output") == 0) {
139 display_wayland->output =
140 wl_registry_bind(display_wayland->wl_registry, id, &wl_output_interface, 1);
141 wl_output_add_listener(display_wayland->output,
142 &output_listener, display_wayland);
143 } else if (strcmp(interface, "wl_seat") == 0) {
144 seat = wl_registry_bind(display_wayland->wl_registry, id, &wl_seat_interface, 1);
145 _gdk_wayland_device_manager_add_device (gdk_display->device_manager,
147 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
148 display_wayland->data_device_manager =
149 wl_registry_bind(display_wayland->wl_registry, id,
150 &wl_data_device_manager_interface, 1);
155 static const struct wl_registry_listener registry_listener = {
156 gdk_registry_handle_global
160 _gdk_wayland_display_open (const gchar *display_name)
162 struct wl_display *wl_display;
164 GdkWaylandDisplay *display_wayland;
166 wl_display = wl_display_connect(display_name);
170 display = g_object_new (GDK_TYPE_WAYLAND_DISPLAY, NULL);
171 display_wayland = GDK_WAYLAND_DISPLAY (display);
173 display_wayland->wl_display = wl_display;
175 display_wayland->screen = _gdk_wayland_screen_new (display);
177 display->device_manager = _gdk_wayland_device_manager_new (display);
179 /* Set up listener so we'll catch all events. */
180 display_wayland->wl_registry = wl_display_get_registry(display_wayland->wl_display);
181 wl_registry_add_listener(display_wayland->wl_registry, ®istry_listener, display_wayland);
183 wl_display_dispatch(display_wayland->wl_display);
185 display_wayland->event_source =
186 _gdk_wayland_display_event_source_new (display);
188 gdk_input_init (display);
190 g_signal_emit_by_name (display, "opened");
191 g_signal_emit_by_name (gdk_display_manager_get(), "display_opened", display);
197 gdk_wayland_display_dispose (GObject *object)
199 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
201 _gdk_wayland_display_manager_remove_display (gdk_display_manager_get (),
202 GDK_DISPLAY (display_wayland));
203 g_list_foreach (display_wayland->input_devices,
204 (GFunc) g_object_run_dispose, NULL);
206 _gdk_screen_close (display_wayland->screen);
208 if (display_wayland->event_source)
210 g_source_destroy (display_wayland->event_source);
211 g_source_unref (display_wayland->event_source);
212 display_wayland->event_source = NULL;
215 G_OBJECT_CLASS (gdk_wayland_display_parent_class)->dispose (object);
219 gdk_wayland_display_finalize (GObject *object)
221 GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
224 if (display_wayland->keymap)
225 g_object_unref (display_wayland->keymap);
227 /* input GdkDevice list */
228 g_list_free_full (display_wayland->input_devices, g_object_unref);
230 g_object_unref (display_wayland->screen);
232 g_free (display_wayland->startup_notification_id);
234 G_OBJECT_CLASS (gdk_wayland_display_parent_class)->finalize (object);
238 gdk_wayland_display_get_name (GdkDisplay *display)
244 gdk_wayland_display_get_n_screens (GdkDisplay *display)
250 gdk_wayland_display_get_screen (GdkDisplay *display,
253 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
254 g_return_val_if_fail (screen_num == 0, NULL);
256 return GDK_WAYLAND_DISPLAY (display)->screen;
260 gdk_wayland_display_get_default_screen (GdkDisplay *display)
262 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
264 return GDK_WAYLAND_DISPLAY (display)->screen;
268 gdk_wayland_display_beep (GdkDisplay *display)
270 g_return_if_fail (GDK_IS_DISPLAY (display));
274 gdk_wayland_display_sync (GdkDisplay *display)
276 GdkWaylandDisplay *display_wayland;
278 g_return_if_fail (GDK_IS_DISPLAY (display));
280 display_wayland = GDK_WAYLAND_DISPLAY (display);
282 wl_display_roundtrip(display_wayland->wl_display);
286 gdk_wayland_display_flush (GdkDisplay *display)
288 g_return_if_fail (GDK_IS_DISPLAY (display));
290 if (!display->closed)
291 wl_display_flush(GDK_WAYLAND_DISPLAY (display)->wl_display);;
295 gdk_wayland_display_has_pending (GdkDisplay *display)
301 gdk_wayland_display_get_default_group (GdkDisplay *display)
303 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
310 gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
316 gdk_wayland_display_request_selection_notification (GdkDisplay *display,
324 gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
330 gdk_wayland_display_store_clipboard (GdkDisplay *display,
331 GdkWindow *clipboard_window,
333 const GdkAtom *targets,
339 gdk_wayland_display_supports_shapes (GdkDisplay *display)
345 gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
351 gdk_wayland_display_supports_composite (GdkDisplay *display)
357 gdk_wayland_display_list_devices (GdkDisplay *display)
359 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
361 return GDK_WAYLAND_DISPLAY (display)->input_devices;
365 gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
370 gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
372 /* Post the damage here instead? */
376 gdk_wayland_display_get_next_serial (GdkDisplay *display)
378 static gulong serial = 0;
383 _gdk_wayland_display_make_default (GdkDisplay *display)
388 * gdk_wayland_display_broadcast_startup_message:
389 * @display: a #GdkDisplay
390 * @message_type: startup notification message type ("new", "change",
392 * @...: a list of key/value pairs (as strings), terminated by a
393 * %NULL key. (A %NULL value for a key will cause that key to be
394 * skipped in the output.)
396 * Sends a startup notification message of type @message_type to
399 * This is a convenience function for use by code that implements the
400 * freedesktop startup notification specification. Applications should
401 * not normally need to call it directly. See the <ulink
402 * url="http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt">Startup
403 * Notification Protocol specification</ulink> for
404 * definitions of the message types and keys that can be used.
409 gdk_wayland_display_broadcast_startup_message (GdkDisplay *display,
410 const char *message_type,
415 const char *key, *value, *p;
417 message = g_string_new (message_type);
418 g_string_append_c (message, ':');
420 va_start (ap, message_type);
421 while ((key = va_arg (ap, const char *)))
423 value = va_arg (ap, const char *);
427 g_string_append_printf (message, " %s=\"", key);
428 for (p = value; *p; p++)
435 g_string_append_c (message, '\\');
439 g_string_append_c (message, *p);
441 g_string_append_c (message, '\"');
445 printf ("startup message: %s\n", message->str);
447 g_string_free (message, TRUE);
451 gdk_wayland_display_notify_startup_complete (GdkDisplay *display,
452 const gchar *startup_id)
454 gdk_wayland_display_broadcast_startup_message (display, "remove",
460 gdk_wayland_display_event_data_copy (GdkDisplay *display,
467 gdk_wayland_display_event_data_free (GdkDisplay *display,
473 _gdk_wayland_display_get_keymap (GdkDisplay *display)
475 GdkDeviceManager *device_manager;
477 GdkDevice *core_keyboard = NULL;
478 static GdkKeymap *tmp_keymap = NULL;
480 device_manager = gdk_display_get_device_manager (display);
481 list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
483 for (l = list; l; l = l->next)
488 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
491 core_keyboard = device;
495 if (core_keyboard && tmp_keymap)
497 g_object_unref (tmp_keymap);
502 return _gdk_wayland_device_get_keymap (core_keyboard);
504 tmp_keymap = _gdk_wayland_keymap_new ();
510 gdk_wayland_display_push_error_trap (GdkDisplay *display)
515 gdk_wayland_display_pop_error_trap (GdkDisplay *display,
522 gdk_wayland_display_class_init (GdkWaylandDisplayClass * class)
524 GObjectClass *object_class = G_OBJECT_CLASS (class);
525 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);
527 object_class->dispose = gdk_wayland_display_dispose;
528 object_class->finalize = gdk_wayland_display_finalize;
530 display_class->window_type = _gdk_wayland_window_get_type ();
531 display_class->get_name = gdk_wayland_display_get_name;
532 display_class->get_n_screens = gdk_wayland_display_get_n_screens;
533 display_class->get_screen = gdk_wayland_display_get_screen;
534 display_class->get_default_screen = gdk_wayland_display_get_default_screen;
535 display_class->beep = gdk_wayland_display_beep;
536 display_class->sync = gdk_wayland_display_sync;
537 display_class->flush = gdk_wayland_display_flush;
538 display_class->has_pending = gdk_wayland_display_has_pending;
539 display_class->queue_events = _gdk_wayland_display_queue_events;
540 display_class->get_default_group = gdk_wayland_display_get_default_group;
541 display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
542 display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
543 display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
544 display_class->store_clipboard = gdk_wayland_display_store_clipboard;
545 display_class->supports_shapes = gdk_wayland_display_supports_shapes;
546 display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
547 display_class->supports_composite = gdk_wayland_display_supports_composite;
548 display_class->list_devices = gdk_wayland_display_list_devices;
549 display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
550 display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
551 display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
552 display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
553 display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
554 display_class->get_cursor_for_pixbuf = _gdk_wayland_display_get_cursor_for_pixbuf;
555 display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
556 display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
557 display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
558 display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
559 display_class->get_next_serial = gdk_wayland_display_get_next_serial;
560 display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
561 display_class->event_data_copy = gdk_wayland_display_event_data_copy;
562 display_class->event_data_free = gdk_wayland_display_event_data_free;
563 display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
564 display_class->get_keymap = _gdk_wayland_display_get_keymap;
565 display_class->push_error_trap = gdk_wayland_display_push_error_trap;
566 display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
567 display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
568 display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
569 display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
570 display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
571 display_class->convert_selection = _gdk_wayland_display_convert_selection;
572 display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
573 display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
577 gdk_wayland_display_init (GdkWaylandDisplay *display)
579 _gdk_wayland_display_manager_add_display (gdk_display_manager_get (),
580 GDK_DISPLAY (display));
582 display->xkb_context = xkb_context_new (0);
586 _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *wayland_display)
589 gchar *theme_name = NULL; /* FIXME: Do something here */
591 g_assert (wayland_display);
592 g_assert (wayland_display->shm);
594 _gdk_wayland_display_get_default_cursor_size (GDK_DISPLAY (wayland_display),
597 wayland_display->cursor_theme = wl_cursor_theme_load (theme_name,
599 wayland_display->shm);
603 _gdk_wayland_display_get_serial (GdkWaylandDisplay *wayland_display)
605 return wayland_display->serial;
609 _gdk_wayland_display_update_serial (GdkWaylandDisplay *wayland_display,
612 if (serial > wayland_display->serial)
613 wayland_display->serial = serial;
617 * gdk_wayland_display_get_wl_display
618 * @display: (type GdkWaylandDisplay): a #GdkDisplay
620 * Returns the Wayland wl_display of a #GdkDisplay
622 * Returns: (transfer none): a Wayland wl_display
627 gdk_wayland_display_get_wl_display(GdkDisplay *display)
629 GdkWaylandDisplay *wayland_display = GDK_WAYLAND_DISPLAY(display);
631 g_return_val_if_fail (GDK_IS_WAYLAND_DISPLAY(display), NULL);
633 return wayland_display->wl_display;
637 * gdk_wayland_display_get_wl_compositor
638 * @display: (type GdkWaylandDisplay): a #GdkDisplay
640 * Returns the Wayland global singleton compositor of a #GdkDisplay
642 * Returns: (transfer none): a Wayland wl_compositor
646 struct wl_compositor *
647 gdk_wayland_display_get_wl_compositor (GdkDisplay *display)
649 GdkWaylandDisplay *wayland_display = GDK_WAYLAND_DISPLAY(display);
651 g_return_val_if_fail (GDK_IS_WAYLAND_DISPLAY(display), NULL);
653 return wayland_display->compositor;
657 * gdk_wayland_display_get_wl_shell
658 * @display: (type GdkWaylandDisplay): a #GdkDisplay
660 * Returns the Wayland global singleton shell of a #GdkDisplay
662 * Returns: (transfer none): a Wayland wl_shell
667 gdk_wayland_display_get_wl_shell (GdkDisplay *display)
669 GdkWaylandDisplay *wayland_display = GDK_WAYLAND_DISPLAY(display);
671 g_return_val_if_fail (GDK_IS_WAYLAND_DISPLAY(display), NULL);
673 return wayland_display->shell;