gdkdisplay-wayland.c 40.8 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2010 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Kristian Høgsberg's avatar
Kristian Høgsberg committed
16 17 18 19 20 21 22 23 24
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
25 26

#ifdef HAVE_LINUX_MEMFD_H
27
#include <linux/memfd.h>
28 29
#endif

30
#include <sys/mman.h>
31
#include <sys/syscall.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
32 33 34 35 36 37 38 39 40 41

#include <glib.h>
#include "gdkwayland.h"
#include "gdkdisplay.h"
#include "gdkdisplay-wayland.h"
#include "gdkscreen.h"
#include "gdkinternals.h"
#include "gdkdeviceprivate.h"
#include "gdkdevicemanager.h"
#include "gdkkeysprivate.h"
42
#include "gdkprivate-wayland.h"
43
#include "gdkglcontext-wayland.h"
44
#include "gdkwaylandmonitor.h"
45
#include "pointer-gestures-unstable-v1-client-protocol.h"
46
#include "tablet-unstable-v2-client-protocol.h"
47
#include "xdg-shell-unstable-v6-client-protocol.h"
48
#include "xdg-foreign-unstable-v1-client-protocol.h"
49
#include "server-decoration-client-protocol.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
50

51 52 53 54 55 56
/**
 * SECTION:wayland_interaction
 * @Short_description: Wayland backend-specific functions
 * @Title: Wayland Interaction
 *
 * The functions in this section are specific to the GDK Wayland backend.
57 58 59
 * To use them, you need to include the `<gdk/gdkwayland.h>` header and use
 * the Wayland-specific pkg-config files to build your application (either
 * `gdk-wayland-3.0` or `gtk+-wayland-3.0`).
60 61 62 63 64
 *
 * To make your code compile with other GDK backends, guard backend-specific
 * calls by an ifdef as follows. Since GDK may be built with multiple
 * backends, you should also check for the backend that is in use (e.g. by
 * using the GDK_IS_WAYLAND_DISPLAY() macro).
65
 * |[<!-- language="C" -->
66 67 68
 * #ifdef GDK_WINDOWING_WAYLAND
 *   if (GDK_IS_WAYLAND_DISPLAY (display))
 *     {
69
 *       // make Wayland-specific calls here
70 71 72 73 74 75
 *     }
 *   else
 * #endif
 * #ifdef GDK_WINDOWING_X11
 *   if (GDK_IS_X11_DISPLAY (display))
 *     {
76
 *       // make X11-specific calls here
77 78 79 80 81 82 83
 *     }
 *   else
 * #endif
 *   g_error ("Unsupported GDK backend");
 * ]|
 */

84 85
#define MIN_SYSTEM_BELL_DELAY_MS 20

86 87
#define GTK_SHELL1_VERSION       2

88
static void _gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *display_wayland);
89

90
G_DEFINE_TYPE (GdkWaylandDisplay, gdk_wayland_display, GDK_TYPE_DISPLAY)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
91

92 93 94 95 96 97 98 99 100 101 102 103
static void
async_roundtrip_callback (void               *data,
                          struct wl_callback *callback,
                          uint32_t            time)
{
  GdkWaylandDisplay *display_wayland = data;

  display_wayland->async_roundtrips =
    g_list_remove (display_wayland->async_roundtrips, callback);
  wl_callback_destroy (callback);
}

104
static const struct wl_callback_listener async_roundtrip_listener = {
105 106 107 108 109 110 111 112 113 114
  async_roundtrip_callback
};

static void
_gdk_wayland_display_async_roundtrip (GdkWaylandDisplay *display_wayland)
{
  struct wl_callback *callback;

  callback = wl_display_sync (display_wayland->wl_display);
  wl_callback_add_listener (callback,
115
                            &async_roundtrip_listener,
116 117 118 119 120
                            display_wayland);
  display_wayland->async_roundtrips =
    g_list_append (display_wayland->async_roundtrips, callback);
}

121
static void
122 123 124
xdg_shell_ping (void                 *data,
                struct zxdg_shell_v6 *xdg_shell,
                uint32_t              serial)
125
{
126
  GdkWaylandDisplay *display_wayland = data;
127

128
  _gdk_wayland_display_update_serial (display_wayland, serial);
129

Matthias Clasen's avatar
Matthias Clasen committed
130 131 132
  GDK_NOTE (EVENTS,
            g_message ("ping, shell %p, serial %u\n", xdg_shell, serial));

133
  zxdg_shell_v6_pong (xdg_shell, serial);
134 135
}

136
static const struct zxdg_shell_v6_listener xdg_shell_listener = {
137 138 139
  xdg_shell_ping,
};

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static gboolean
is_known_global (gpointer key, gpointer value, gpointer user_data)
{
  const char *required_global = user_data;
  const char *known_global = value;

  return g_strcmp0 (required_global, known_global) == 0;
}

static gboolean
has_required_globals (GdkWaylandDisplay *display_wayland,
                      const char *required_globals[])
{
  int i = 0;

  while (required_globals[i])
    {
      if (g_hash_table_find (display_wayland->known_globals,
                             is_known_global,
                             (gpointer)required_globals[i]) == NULL)
        return FALSE;

      i++;
    }

  return TRUE;
}

typedef struct _OnHasGlobalsClosure OnHasGlobalsClosure;

typedef void (*HasGlobalsCallback) (GdkWaylandDisplay *display_wayland,
                                    OnHasGlobalsClosure *closure);

struct _OnHasGlobalsClosure
{
  HasGlobalsCallback handler;
  const char **required_globals;
};

static void
process_on_globals_closures (GdkWaylandDisplay *display_wayland)
{
  GList *iter;

  iter = display_wayland->on_has_globals_closures;
  while (iter != NULL)
    {
      GList *next = iter->next;
      OnHasGlobalsClosure *closure = iter->data;

      if (has_required_globals (display_wayland,
                                closure->required_globals))
        {
          closure->handler (display_wayland, closure);
          g_free (closure);
          display_wayland->on_has_globals_closures =
            g_list_delete_link (display_wayland->on_has_globals_closures, iter);
        }

      iter = next;
    }
}

typedef struct
{
  OnHasGlobalsClosure base;
  uint32_t id;
  uint32_t version;
} SeatAddedClosure;

static void
_gdk_wayland_display_add_seat (GdkWaylandDisplay *display_wayland,
                               uint32_t id,
                               uint32_t version)
{
  GdkDisplay *gdk_display = GDK_DISPLAY_OBJECT (display_wayland);
  struct wl_seat *seat;

218
  display_wayland->seat_version = MIN (version, 5);
219
  seat = wl_registry_bind (display_wayland->wl_registry,
220 221
                           id, &wl_seat_interface,
                           display_wayland->seat_version);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  _gdk_wayland_device_manager_add_seat (gdk_display->device_manager,
                                        id, seat);
  _gdk_wayland_display_async_roundtrip (display_wayland);
}

static void
seat_added_closure_run (GdkWaylandDisplay *display_wayland,
                        OnHasGlobalsClosure *closure)
{
  SeatAddedClosure *seat_added_closure = (SeatAddedClosure*)closure;

  _gdk_wayland_display_add_seat (display_wayland,
                                 seat_added_closure->id,
                                 seat_added_closure->version);
}

static void
postpone_on_globals_closure (GdkWaylandDisplay *display_wayland,
                             OnHasGlobalsClosure *closure)
{
  display_wayland->on_has_globals_closures =
    g_list_append (display_wayland->on_has_globals_closures, closure);
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#ifdef G_ENABLE_DEBUG

static const char *
get_format_name (enum wl_shm_format format)
{
  int i;
#define FORMAT(s) { WL_SHM_FORMAT_ ## s, #s }
  struct { int format; const char *name; } formats[] = {
    FORMAT(ARGB8888),
    FORMAT(XRGB8888),
    FORMAT(C8),
    FORMAT(RGB332),
    FORMAT(BGR233),
    FORMAT(XRGB4444),
    FORMAT(XBGR4444),
    FORMAT(RGBX4444),
    FORMAT(BGRX4444),
    FORMAT(ARGB4444),
    FORMAT(ABGR4444),
    FORMAT(RGBA4444),
    FORMAT(BGRA4444),
    FORMAT(XRGB1555),
    FORMAT(XBGR1555),
    FORMAT(RGBX5551),
    FORMAT(BGRX5551),
    FORMAT(ARGB1555),
    FORMAT(ABGR1555),
    FORMAT(RGBA5551),
    FORMAT(BGRA5551),
    FORMAT(RGB565),
    FORMAT(BGR565),
    FORMAT(RGB888),
    FORMAT(BGR888),
    FORMAT(XBGR8888),
    FORMAT(RGBX8888),
    FORMAT(BGRX8888),
    FORMAT(ABGR8888),
    FORMAT(RGBA8888),
    FORMAT(BGRA8888),
    FORMAT(XRGB2101010),
    FORMAT(XBGR2101010),
    FORMAT(RGBX1010102),
    FORMAT(BGRX1010102),
    FORMAT(ARGB2101010),
    FORMAT(ABGR2101010),
    FORMAT(RGBA1010102),
    FORMAT(BGRA1010102),
    FORMAT(YUYV),
    FORMAT(YVYU),
    FORMAT(UYVY),
    FORMAT(VYUY),
    FORMAT(AYUV),
    FORMAT(NV12),
    FORMAT(NV21),
    FORMAT(NV16),
    FORMAT(NV61),
    FORMAT(YUV410),
    FORMAT(YVU410),
    FORMAT(YUV411),
    FORMAT(YVU411),
    FORMAT(YUV420),
    FORMAT(YVU420),
    FORMAT(YUV422),
    FORMAT(YVU422),
    FORMAT(YUV444),
    FORMAT(YVU444),
    { 0xffffffff, NULL }
  };
#undef FORMAT

  for (i = 0; formats[i].name; i++)
    {
      if (formats[i].format == format)
        return formats[i].name;
    }
  return NULL;
}
#endif

static void
wl_shm_format (void          *data,
               struct wl_shm *wl_shm,
               uint32_t       format)
{
  GDK_NOTE (MISC, g_message ("supported pixel format %s", get_format_name (format)));
}

static const struct wl_shm_listener wl_shm_listener = {
  wl_shm_format
};

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static void
server_decoration_manager_default_mode (void                                          *data,
                                        struct org_kde_kwin_server_decoration_manager *manager,
                                        uint32_t                                       mode)
{
  g_assert (mode <= ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_SERVER);
  const char *modes[] = {
    [ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_NONE]   = "none",
    [ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_CLIENT] = "client",
    [ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_SERVER] = "server",
  };
  GdkWaylandDisplay *display_wayland = data;
  g_debug ("Compositor prefers decoration mode '%s'", modes[mode]);
  display_wayland->server_decoration_mode = mode;
}

static const struct org_kde_kwin_server_decoration_manager_listener server_decoration_listener = {
  .default_mode = server_decoration_manager_default_mode
};

gboolean
gdk_wayland_display_prefers_ssd (GdkDisplay *display)
{
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
  if (display_wayland->server_decoration_manager)
    return display_wayland->server_decoration_mode == ORG_KDE_KWIN_SERVER_DECORATION_MANAGER_MODE_SERVER;
  return FALSE;
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
366
static void
Matthias Clasen's avatar
Matthias Clasen committed
367 368 369 370 371
gdk_registry_handle_global (void               *data,
                            struct wl_registry *registry,
                            uint32_t            id,
                            const char         *interface,
                            uint32_t            version)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
372
{
373
  GdkWaylandDisplay *display_wayland = data;
374
  struct wl_output *output;
375
  gboolean handled = TRUE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
376

Matthias Clasen's avatar
Matthias Clasen committed
377 378 379
  GDK_NOTE (MISC,
            g_message ("add global %u, interface %s, version %u", id, interface, version));

Matthias Clasen's avatar
Matthias Clasen committed
380 381 382 383 384 385 386 387 388 389
  if (strcmp (interface, "wl_compositor") == 0)
    {
      display_wayland->compositor =
        wl_registry_bind (display_wayland->wl_registry, id, &wl_compositor_interface, MIN (version, 3));
      display_wayland->compositor_version = MIN (version, 3);
    }
  else if (strcmp (interface, "wl_shm") == 0)
    {
      display_wayland->shm =
        wl_registry_bind (display_wayland->wl_registry, id, &wl_shm_interface, 1);
390
      wl_shm_add_listener (display_wayland->shm, &wl_shm_listener, display_wayland);
Matthias Clasen's avatar
Matthias Clasen committed
391
    }
392
  else if (strcmp (interface, "zxdg_shell_v6") == 0)
Matthias Clasen's avatar
Matthias Clasen committed
393 394
    {
      display_wayland->xdg_shell =
395 396 397 398 399
        wl_registry_bind (display_wayland->wl_registry, id,
                          &zxdg_shell_v6_interface, 1);
      zxdg_shell_v6_add_listener (display_wayland->xdg_shell,
                                  &xdg_shell_listener,
                                  display_wayland);
Matthias Clasen's avatar
Matthias Clasen committed
400
    }
401
  else if (strcmp (interface, "gtk_shell1") == 0)
Matthias Clasen's avatar
Matthias Clasen committed
402
    {
403 404 405
      display_wayland->gtk_shell =
        wl_registry_bind(display_wayland->wl_registry, id,
                         &gtk_shell1_interface,
406
                         MIN (version, GTK_SHELL1_VERSION));
407 408
      _gdk_wayland_screen_set_has_gtk_shell (display_wayland->screen);
      display_wayland->gtk_shell_version = version;
Matthias Clasen's avatar
Matthias Clasen committed
409 410 411 412 413 414
    }
  else if (strcmp (interface, "wl_output") == 0)
    {
      output =
        wl_registry_bind (display_wayland->wl_registry, id, &wl_output_interface, MIN (version, 2));
      _gdk_wayland_screen_add_output (display_wayland->screen, id, output, MIN (version, 2));
415
      _gdk_wayland_display_async_roundtrip (display_wayland);
Matthias Clasen's avatar
Matthias Clasen committed
416 417 418
    }
  else if (strcmp (interface, "wl_seat") == 0)
    {
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
      static const char *required_device_manager_globals[] = {
        "wl_compositor",
        "wl_data_device_manager",
        NULL
      };

      if (has_required_globals (display_wayland,
                                required_device_manager_globals))
        _gdk_wayland_display_add_seat (display_wayland, id, version);
      else
        {
          SeatAddedClosure *closure;

          closure = g_new0 (SeatAddedClosure, 1);
          closure->base.handler = seat_added_closure_run;
          closure->base.required_globals = required_device_manager_globals;
          closure->id = id;
          closure->version = version;
          postpone_on_globals_closure (display_wayland, &closure->base);
        }
Matthias Clasen's avatar
Matthias Clasen committed
439 440 441
    }
  else if (strcmp (interface, "wl_data_device_manager") == 0)
    {
442
      display_wayland->data_device_manager_version = MIN (version, 3);
443
      display_wayland->data_device_manager =
444 445
        wl_registry_bind (display_wayland->wl_registry, id, &wl_data_device_manager_interface,
                          display_wayland->data_device_manager_version);
Matthias Clasen's avatar
Matthias Clasen committed
446 447 448 449 450 451
    }
  else if (strcmp (interface, "wl_subcompositor") == 0)
    {
      display_wayland->subcompositor =
        wl_registry_bind (display_wayland->wl_registry, id, &wl_subcompositor_interface, 1);
    }
452 453
  else if (strcmp (interface, "zwp_pointer_gestures_v1") == 0 &&
           version == GDK_ZWP_POINTER_GESTURES_V1_VERSION)
454 455
    {
      display_wayland->pointer_gestures =
456
        wl_registry_bind (display_wayland->wl_registry,
457
                          id, &zwp_pointer_gestures_v1_interface, version);
458
    }
459 460 461 462 463 464
  else if (strcmp (interface, "gtk_primary_selection_device_manager") == 0)
    {
      display_wayland->primary_selection_manager =
        wl_registry_bind(display_wayland->wl_registry, id,
                         &gtk_primary_selection_device_manager_interface, 1);
    }
465
  else if (strcmp (interface, "zwp_tablet_manager_v2") == 0)
466 467 468
    {
      display_wayland->tablet_manager =
        wl_registry_bind(display_wayland->wl_registry, id,
469
                         &zwp_tablet_manager_v2_interface, 1);
470
    }
471 472 473 474 475 476
  else if (strcmp (interface, "zxdg_exporter_v1") == 0)
    {
      display_wayland->xdg_exporter =
        wl_registry_bind (display_wayland->wl_registry, id,
                          &zxdg_exporter_v1_interface, 1);
    }
477 478 479 480 481 482
  else if (strcmp (interface, "zxdg_importer_v1") == 0)
    {
      display_wayland->xdg_importer =
        wl_registry_bind (display_wayland->wl_registry, id,
                          &zxdg_importer_v1_interface, 1);
    }
483 484 485 486 487 488
  else if (strcmp (interface, "zwp_keyboard_shortcuts_inhibit_manager_v1") == 0)
    {
      display_wayland->keyboard_shortcuts_inhibit =
        wl_registry_bind (display_wayland->wl_registry, id,
                          &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
    }
489 490 491 492 493 494 495 496 497
  else if (strcmp (interface, "org_kde_kwin_server_decoration_manager") == 0)
    {
      display_wayland->server_decoration_manager =
        wl_registry_bind (display_wayland->wl_registry, id,
                          &org_kde_kwin_server_decoration_manager_interface, 1);
      org_kde_kwin_server_decoration_manager_add_listener (display_wayland->server_decoration_manager,
                                                           &server_decoration_listener,
                                                           display_wayland);
    }
498 499 500 501 502 503 504 505
  else
    handled = FALSE;

  if (handled)
    g_hash_table_insert (display_wayland->known_globals,
                         GUINT_TO_POINTER (id), g_strdup (interface));

  process_on_globals_closures (display_wayland);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
506 507
}

508
static void
Matthias Clasen's avatar
Matthias Clasen committed
509 510 511
gdk_registry_handle_global_remove (void               *data,
                                   struct wl_registry *registry,
                                   uint32_t            id)
512
{
513
  GdkWaylandDisplay *display_wayland = data;
Matthias Clasen's avatar
Matthias Clasen committed
514
  GdkDisplay *display = GDK_DISPLAY (display_wayland);
515

Matthias Clasen's avatar
Matthias Clasen committed
516
  GDK_NOTE (MISC, g_message ("remove global %u", id));
Matthias Clasen's avatar
Matthias Clasen committed
517 518
  _gdk_wayland_device_manager_remove_seat (display->device_manager, id);
  _gdk_wayland_screen_remove_output (display_wayland->screen, id);
519

520 521
  g_hash_table_remove (display_wayland->known_globals, GUINT_TO_POINTER (id));

522
  /* FIXME: the object needs to be destroyed here, we're leaking */
523
}
Kristian Høgsberg's avatar
Kristian Høgsberg committed
524

525
static const struct wl_registry_listener registry_listener = {
526 527
    gdk_registry_handle_global,
    gdk_registry_handle_global_remove
528 529
};

530
static void
Matthias Clasen's avatar
Matthias Clasen committed
531
log_handler (const char *format, va_list args)
532
{
533
  g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
534 535
}

536 537 538 539 540 541 542 543 544 545 546 547 548
static void
load_cursor_theme_closure_run (GdkWaylandDisplay *display_wayland,
                               OnHasGlobalsClosure *closure)
{
  _gdk_wayland_display_load_cursor_theme (display_wayland);
}

static void
_gdk_wayland_display_prepare_cursor_themes (GdkWaylandDisplay *display_wayland)
{
  OnHasGlobalsClosure *closure;
  static const char *required_cursor_theme_globals[] = {
      "wl_shm",
Matthias Clasen's avatar
Matthias Clasen committed
549
      NULL
550 551 552 553 554 555 556 557
  };

  closure = g_new0 (OnHasGlobalsClosure, 1);
  closure->handler = load_cursor_theme_closure_run;
  closure->required_globals = required_cursor_theme_globals;
  postpone_on_globals_closure (display_wayland, closure);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
558
GdkDisplay *
559
_gdk_wayland_display_open (const gchar *display_name)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
560
{
561
  struct wl_display *wl_display;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
562
  GdkDisplay *display;
563
  GdkWaylandDisplay *display_wayland;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
564

Matthias Clasen's avatar
Matthias Clasen committed
565 566
  GDK_NOTE (MISC, g_message ("opening display %s", display_name ? display_name : ""));

567
  /* If this variable is unset then wayland initialisation will surely
568 569 570 571 572 573
   * fail, logging a fatal error in the process.  Save ourselves from
   * that.
   */
  if (g_getenv ("XDG_RUNTIME_DIR") == NULL)
    return NULL;

Matthias Clasen's avatar
Matthias Clasen committed
574
  wl_log_set_handler_client (log_handler);
575

Matthias Clasen's avatar
Matthias Clasen committed
576
  wl_display = wl_display_connect (display_name);
577 578 579
  if (!wl_display)
    return NULL;

580
  display = g_object_new (GDK_TYPE_WAYLAND_DISPLAY, NULL);
581
  display->device_manager = _gdk_wayland_device_manager_new (display);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
582

583
  display_wayland = GDK_WAYLAND_DISPLAY (display);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
584
  display_wayland->wl_display = wl_display;
585
  display_wayland->screen = _gdk_wayland_screen_new (display);
586
  display_wayland->event_source = _gdk_wayland_display_event_source_new (display);
587 588 589 590

  display_wayland->known_globals =
    g_hash_table_new_full (NULL, NULL, NULL, g_free);

591
  _gdk_wayland_display_init_cursors (display_wayland);
592
  _gdk_wayland_display_prepare_cursor_themes (display_wayland);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
593

Matthias Clasen's avatar
Matthias Clasen committed
594 595
  display_wayland->wl_registry = wl_display_get_registry (display_wayland->wl_display);
  wl_registry_add_listener (display_wayland->wl_registry, &registry_listener, display_wayland);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
596

597 598 599 600 601 602 603 604 605 606 607 608
  _gdk_wayland_display_async_roundtrip (display_wayland);

  /* Wait for initializing to complete. This means waiting for all
   * asynchrounous roundtrips that were triggered during initial roundtrip. */
  while (g_list_length (display_wayland->async_roundtrips) > 0)
    {
      if (wl_display_dispatch (display_wayland->wl_display) < 0)
        {
          g_object_unref (display);
          return NULL;
        }
    }
609

610 611 612 613 614 615 616 617 618 619
  /* Make sure we have xdg_shell at least */
  if (display_wayland->xdg_shell == NULL)
    {
      g_warning ("Wayland compositor does not support xdg_shell interface,"
                 " not using Wayland display");
      g_object_unref (display);

      return NULL;
    }

620 621
  display_wayland->selection = gdk_wayland_selection_new ();

Kristian Høgsberg's avatar
Kristian Høgsberg committed
622 623 624 625 626 627 628 629
  g_signal_emit_by_name (display, "opened");

  return display;
}

static void
gdk_wayland_display_dispose (GObject *object)
{
630
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
631

632
  _gdk_screen_close (display_wayland->screen);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
633 634 635 636 637 638 639 640

  if (display_wayland->event_source)
    {
      g_source_destroy (display_wayland->event_source);
      g_source_unref (display_wayland->event_source);
      display_wayland->event_source = NULL;
    }

641 642 643 644 645 646
  if (display_wayland->selection)
    {
      gdk_wayland_selection_free (display_wayland->selection);
      display_wayland->selection = NULL;
    }

647 648 649 650 651 652 653
  g_list_free_full (display_wayland->async_roundtrips, (GDestroyNotify) wl_callback_destroy);

  if (display_wayland->known_globals)
    {
      g_hash_table_destroy (display_wayland->known_globals);
      display_wayland->known_globals = NULL;
    }
654

655 656
  g_list_free_full (display_wayland->on_has_globals_closures, g_free);

657
  G_OBJECT_CLASS (gdk_wayland_display_parent_class)->dispose (object);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
658 659 660 661 662
}

static void
gdk_wayland_display_finalize (GObject *object)
{
663
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (object);
664
  guint i;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
665

666 667
  _gdk_wayland_display_finalize_cursors (display_wayland);

668
  g_object_unref (display_wayland->screen);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
669 670

  g_free (display_wayland->startup_notification_id);
671 672 673 674 675 676 677 678 679 680 681
  g_free (display_wayland->cursor_theme_name);
  xkb_context_unref (display_wayland->xkb_context);

  for (i = 0; i < GDK_WAYLAND_THEME_SCALES_COUNT; i++)
    {
      if (display_wayland->scaled_cursor_themes[i])
        {
          wl_cursor_theme_destroy (display_wayland->scaled_cursor_themes[i]);
          display_wayland->scaled_cursor_themes[i] = NULL;
        }
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
682

683 684
  g_ptr_array_free (display_wayland->monitors, TRUE);

685
  G_OBJECT_CLASS (gdk_wayland_display_parent_class)->finalize (object);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
686 687
}

688
static const gchar *
Kristian Høgsberg's avatar
Kristian Høgsberg committed
689 690
gdk_wayland_display_get_name (GdkDisplay *display)
{
691 692 693 694 695 696 697
  const gchar *name;

  name = g_getenv ("WAYLAND_DISPLAY");
  if (name == NULL)
    name = "wayland-0";

  return name;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
698 699 700 701 702 703 704
}

static GdkScreen *
gdk_wayland_display_get_default_screen (GdkDisplay *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

705
  return GDK_WAYLAND_DISPLAY (display)->screen;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
706 707
}

708 709 710
void
gdk_wayland_display_system_bell (GdkDisplay *display,
                                 GdkWindow  *window)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
711
{
712
  GdkWaylandDisplay *display_wayland;
713
  struct gtk_surface1 *gtk_surface;
714
  gint64 now_ms;
715

Kristian Høgsberg's avatar
Kristian Høgsberg committed
716
  g_return_if_fail (GDK_IS_DISPLAY (display));
717 718 719 720 721 722

  display_wayland = GDK_WAYLAND_DISPLAY (display);

  if (!display_wayland->gtk_shell)
    return;

723 724 725 726 727
  if (window)
    gtk_surface = gdk_wayland_window_get_gtk_surface (window);
  else
    gtk_surface = NULL;

728 729 730 731 732 733
  now_ms = g_get_monotonic_time () / 1000;
  if (now_ms - display_wayland->last_bell_time_ms < MIN_SYSTEM_BELL_DELAY_MS)
    return;

  display_wayland->last_bell_time_ms = now_ms;

734 735 736 737 738 739 740
  gtk_shell1_system_bell (display_wayland->gtk_shell, gtk_surface);
}

static void
gdk_wayland_display_beep (GdkDisplay *display)
{
  gdk_wayland_display_system_bell (display, NULL);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
741 742 743 744 745
}

static void
gdk_wayland_display_sync (GdkDisplay *display)
{
746
  GdkWaylandDisplay *display_wayland;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
747 748 749

  g_return_if_fail (GDK_IS_DISPLAY (display));

750
  display_wayland = GDK_WAYLAND_DISPLAY (display);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
751

Matthias Clasen's avatar
Matthias Clasen committed
752
  wl_display_roundtrip (display_wayland->wl_display);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
753 754 755 756 757 758 759 760
}

static void
gdk_wayland_display_flush (GdkDisplay *display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));

  if (!display->closed)
Matthias Clasen's avatar
Matthias Clasen committed
761
    wl_display_flush (GDK_WAYLAND_DISPLAY (display)->wl_display);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
762 763
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
static void
gdk_wayland_display_make_default (GdkDisplay *display)
{
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
  const gchar *startup_id;

  g_free (display_wayland->startup_notification_id);
  display_wayland->startup_notification_id = NULL;

  startup_id = g_getenv ("DESKTOP_STARTUP_ID");
  if (startup_id && *startup_id != '\0')
    {
      if (!g_utf8_validate (startup_id, -1, NULL))
        g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
      else
        display_wayland->startup_notification_id = g_strdup (startup_id);

      /* Clear the environment variable so it won't be inherited by
       * child processes and confuse things.
       */
      g_unsetenv ("DESKTOP_STARTUP_ID");
    }
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
static gboolean
gdk_wayland_display_has_pending (GdkDisplay *display)
{
  return FALSE;
}

static GdkWindow *
gdk_wayland_display_get_default_group (GdkDisplay *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

  return NULL;
}


static gboolean
gdk_wayland_display_supports_selection_notification (GdkDisplay *display)
{
806
  return FALSE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
}

static gboolean
gdk_wayland_display_request_selection_notification (GdkDisplay *display,
						    GdkAtom     selection)

{
    return FALSE;
}

static gboolean
gdk_wayland_display_supports_clipboard_persistence (GdkDisplay *display)
{
  return FALSE;
}

static void
gdk_wayland_display_store_clipboard (GdkDisplay    *display,
				     GdkWindow     *clipboard_window,
				     guint32        time_,
				     const GdkAtom *targets,
				     gint           n_targets)
{
}

static gboolean
gdk_wayland_display_supports_shapes (GdkDisplay *display)
{
835
  return FALSE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
836 837 838 839 840
}

static gboolean
gdk_wayland_display_supports_input_shapes (GdkDisplay *display)
{
841
  return TRUE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
842 843 844 845 846
}

static gboolean
gdk_wayland_display_supports_composite (GdkDisplay *display)
{
847
  return FALSE;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
}

static void
gdk_wayland_display_before_process_all_updates (GdkDisplay *display)
{
}

static void
gdk_wayland_display_after_process_all_updates (GdkDisplay *display)
{
  /* Post the damage here instead? */
}

static gulong
gdk_wayland_display_get_next_serial (GdkDisplay *display)
{
864 865
  static gulong serial = 0;
  return ++serial;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
866 867
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
/**
 * gdk_wayland_display_set_startup_notification_id:
 * @display: (type GdkWaylandDisplay): a #GdkDisplay
 * @startup_id: the startup notification ID (must be valid utf8)
 *
 * Sets the startup notification ID for a display.
 *
 * This is usually taken from the value of the DESKTOP_STARTUP_ID
 * environment variable, but in some cases (such as the application not
 * being launched using exec()) it can come from other sources.
 *
 * The startup ID is also what is used to signal that the startup is
 * complete (for example, when opening a window or when calling
 * gdk_notify_startup_complete()).
 *
 * Since: 3.22
 **/
void
gdk_wayland_display_set_startup_notification_id (GdkDisplay *display,
                                                 const char *startup_id)
{
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);

  g_free (display_wayland->startup_notification_id);
  display_wayland->startup_notification_id = g_strdup (startup_id);
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
895 896 897 898
static void
gdk_wayland_display_notify_startup_complete (GdkDisplay  *display,
					     const gchar *startup_id)
{
899 900 901 902 903 904 905 906 907 908 909 910
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
  gchar *free_this = NULL;

  if (startup_id == NULL)
    {
      startup_id = free_this = display_wayland->startup_notification_id;
      display_wayland->startup_notification_id = NULL;

      if (startup_id == NULL)
        return;
    }

911 912
  if (display_wayland->gtk_shell)
    gtk_shell1_set_startup_id (display_wayland->gtk_shell, startup_id);
913 914

  g_free (free_this);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
915 916
}

917
static GdkKeymap *
918
_gdk_wayland_display_get_keymap (GdkDisplay *display)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
919
{
920
  GdkDevice *core_keyboard = NULL;
921
  static GdkKeymap *tmp_keymap = NULL;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
922

923
  core_keyboard = gdk_seat_get_keyboard (gdk_display_get_default_seat (display));
924

925 926 927 928 929 930 931 932 933
  if (core_keyboard && tmp_keymap)
    {
      g_object_unref (tmp_keymap);
      tmp_keymap = NULL;
    }

  if (core_keyboard)
    return _gdk_wayland_device_get_keymap (core_keyboard);

934 935
  if (!tmp_keymap)
    tmp_keymap = _gdk_wayland_keymap_new ();
936 937

  return tmp_keymap;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951
}

static void
gdk_wayland_display_push_error_trap (GdkDisplay *display)
{
}

static gint
gdk_wayland_display_pop_error_trap (GdkDisplay *display,
				    gboolean    ignored)
{
  return 0;
}

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
static int
gdk_wayland_display_get_n_monitors (GdkDisplay *display)
{
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);

  return display_wayland->monitors->len;
}

static GdkMonitor *
gdk_wayland_display_get_monitor (GdkDisplay *display,
                                 int         monitor_num)
{
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);

  if (monitor_num < 0 || monitor_num >= display_wayland->monitors->len)
    return NULL;

  return (GdkMonitor *)display_wayland->monitors->pdata[monitor_num];
}

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
static GdkMonitor *
gdk_wayland_display_get_monitor_at_window (GdkDisplay *display,
                                           GdkWindow  *window)
{
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY (display);
  struct wl_output *output;
  int i;

  g_return_val_if_fail (GDK_IS_WAYLAND_WINDOW (window), NULL);

  output = gdk_wayland_window_get_wl_output (window);
  if (output == NULL)
    return NULL;

  for (i = 0; i < display_wayland->monitors->len; i++)
    {
      GdkMonitor *monitor = display_wayland->monitors->pdata[i];

      if (gdk_wayland_monitor_get_wl_output (monitor) == output)
        return monitor;
    }

  return NULL;
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
997
static void
Matthias Clasen's avatar
Matthias Clasen committed
998
gdk_wayland_display_class_init (GdkWaylandDisplayClass *class)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
999 1000 1001 1002 1003 1004 1005
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class);

  object_class->dispose = gdk_wayland_display_dispose;
  object_class->finalize = gdk_wayland_display_finalize;

1006
  display_class->window_type = gdk_wayland_window_get_type ();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1007 1008 1009 1010 1011
  display_class->get_name = gdk_wayland_display_get_name;
  display_class->get_default_screen = gdk_wayland_display_get_default_screen;
  display_class->beep = gdk_wayland_display_beep;
  display_class->sync = gdk_wayland_display_sync;
  display_class->flush = gdk_wayland_display_flush;
1012
  display_class->make_default = gdk_wayland_display_make_default;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  display_class->has_pending = gdk_wayland_display_has_pending;
  display_class->queue_events = _gdk_wayland_display_queue_events;
  display_class->get_default_group = gdk_wayland_display_get_default_group;
  display_class->supports_selection_notification = gdk_wayland_display_supports_selection_notification;
  display_class->request_selection_notification = gdk_wayland_display_request_selection_notification;
  display_class->supports_clipboard_persistence = gdk_wayland_display_supports_clipboard_persistence;
  display_class->store_clipboard = gdk_wayland_display_store_clipboard;
  display_class->supports_shapes = gdk_wayland_display_supports_shapes;
  display_class->supports_input_shapes = gdk_wayland_display_supports_input_shapes;
  display_class->supports_composite = gdk_wayland_display_supports_composite;
  display_class->get_app_launch_context = _gdk_wayland_display_get_app_launch_context;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1024 1025
  display_class->get_default_cursor_size = _gdk_wayland_display_get_default_cursor_size;
  display_class->get_maximal_cursor_size = _gdk_wayland_display_get_maximal_cursor_size;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1026 1027
  display_class->get_cursor_for_type = _gdk_wayland_display_get_cursor_for_type;
  display_class->get_cursor_for_name = _gdk_wayland_display_get_cursor_for_name;
1028
  display_class->get_cursor_for_surface = _gdk_wayland_display_get_cursor_for_surface;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1029 1030 1031 1032 1033 1034 1035
  display_class->supports_cursor_alpha = _gdk_wayland_display_supports_cursor_alpha;
  display_class->supports_cursor_color = _gdk_wayland_display_supports_cursor_color;
  display_class->before_process_all_updates = gdk_wayland_display_before_process_all_updates;
  display_class->after_process_all_updates = gdk_wayland_display_after_process_all_updates;
  display_class->get_next_serial = gdk_wayland_display_get_next_serial;
  display_class->notify_startup_complete = gdk_wayland_display_notify_startup_complete;
  display_class->create_window_impl = _gdk_wayland_display_create_window_impl;
1036
  display_class->get_keymap = _gdk_wayland_display_get_keymap;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1037 1038 1039 1040 1041 1042 1043 1044 1045
  display_class->push_error_trap = gdk_wayland_display_push_error_trap;
  display_class->pop_error_trap = gdk_wayland_display_pop_error_trap;
  display_class->get_selection_owner = _gdk_wayland_display_get_selection_owner;
  display_class->set_selection_owner = _gdk_wayland_display_set_selection_owner;
  display_class->send_selection_notify = _gdk_wayland_display_send_selection_notify;
  display_class->get_selection_property = _gdk_wayland_display_get_selection_property;
  display_class->convert_selection = _gdk_wayland_display_convert_selection;
  display_class->text_property_to_utf8_list = _gdk_wayland_display_text_property_to_utf8_list;
  display_class->utf8_to_string_target = _gdk_wayland_display_utf8_to_string_target;
1046 1047

  display_class->make_gl_context_current = gdk_wayland_display_make_gl_context_current;
1048 1049 1050

  display_class->get_n_monitors = gdk_wayland_display_get_n_monitors;
  display_class->get_monitor = gdk_wayland_display_get_monitor;
1051
  display_class->get_monitor_at_window = gdk_wayland_display_get_monitor_at_window;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1052 1053 1054
}

static void
1055
gdk_wayland_display_init (GdkWaylandDisplay *display)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1056
{
1057
  display->xkb_context = xkb_context_new (0);
1058 1059

  display->monitors = g_ptr_array_new_with_free_func (g_object_unref);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1060
}
1061

1062 1063 1064 1065 1066
void
gdk_wayland_display_set_cursor_theme (GdkDisplay  *display,
                                      const gchar *name,
                                      gint         size)
{
1067
  GdkWaylandDisplay *display_wayland = GDK_WAYLAND_DISPLAY(display);
1068
  struct wl_cursor_theme *theme;
1069
  int i;
1070

1071 1072
  g_assert (display_wayland);
  g_assert (display_wayland->shm);
1073

1074 1075
  if (g_strcmp0 (name, display_wayland->cursor_theme_name) == 0 &&
      display_wayland->cursor_theme_size == size)
1076 1077
    return;

1078
  theme = wl_cursor_theme_load (name, size, display_wayland->shm);
1079 1080
  if (theme == NULL)
    {
1081
      g_warning ("Failed to load cursor theme %s", name);
1082 1083 1084
      return;
    }

1085 1086
  for (i = 0; i < GDK_WAYLAND_THEME_SCALES_COUNT; i++)
    {
1087
      if (display_wayland->scaled_cursor_themes[i])
1088
        {
1089 1090
          wl_cursor_theme_destroy (display_wayland->scaled_cursor_themes[i]);
          display_wayland->scaled_cursor_themes[i] = NULL;
1091 1092
        }
    }
1093 1094 1095 1096 1097
  display_wayland->scaled_cursor_themes[0] = theme;
  if (display_wayland->cursor_theme_name != NULL)
    g_free (display_wayland->cursor_theme_name);
  display_wayland->cursor_theme_name = g_strdup (name);
  display_wayland->cursor_theme_size = size;
1098

1099
  _gdk_wayland_display_update_cursors (display_wayland);
1100 1101 1102
}

struct wl_cursor_theme *
1103
_gdk_wayland_display_get_scaled_cursor_theme (GdkWaylandDisplay *display_wayland,
1104 1105 1106 1107
                                              guint              scale)
{
  struct wl_cursor_theme *theme;

1108
  g_assert (display_wayland->cursor_theme_name);
1109 1110 1111
  g_assert (scale <= GDK_WAYLAND_MAX_THEME_SCALE);
  g_assert (scale >= 1);

1112
  theme = display_wayland->scaled_cursor_themes[scale - 1];
1113 1114
  if (!theme)
    {
1115 1116 1117
      theme = wl_cursor_theme_load (display_wayland->cursor_theme_name,
                                    display_wayland->cursor_theme_size * scale,
                                    display_wayland->shm);
1118 1119
      if (theme == NULL)
        {
1120
          g_warning ("Failed to load cursor theme %s with scale %u",
1121
                     display_wayland->cursor_theme_name, scale);
1122 1123
          return NULL;
        }
1124
      display_wayland->scaled_cursor_themes[scale - 1] = theme;
1125
    }
1126

1127
  return theme;
1128 1129
}

1130
static void
1131
_gdk_wayland_display_load_cursor_theme (GdkWaylandDisplay *display_wayland)
1132
{
1133
  guint size;
1134
  const gchar *name;
1135
  GValue v = G_VALUE_INIT;
1136

1137 1138
  g_assert (display_wayland);
  g_assert (display_wayland->shm);
1139

1140
  g_value_init (&v, G_TYPE_INT);
1141
  if (gdk_screen_get_setting (display_wayland->screen, "gtk-cursor-theme-size", &v))
1142 1143 1144 1145 1146
    size = g_value_get_int (&v);
  else
    size = 32;
  g_value_unset (&v);

1147
  g_value_init (&v, G_TYPE_STRING);