aboutsummaryrefslogtreecommitdiff
path: root/cube/cube.cpp
diff options
context:
space:
mode:
authorManuel Stoeckl <code@mstoeckl.com>2019-06-26 20:55:53 -0400
committerTony-LunarG <tony@lunarg.com>2019-11-25 14:25:34 -0700
commit6f6e3371c92ac63bf29d5010b2933659e3f8dbef (patch)
treeaca380bb4c6828786f6764103ef227317decce5d /cube/cube.cpp
parent39b3005ba560da72e2c71cab9c793af2c2bed511 (diff)
downloadusermoji-6f6e3371c92ac63bf29d5010b2933659e3f8dbef.tar.xz
cube: Port Wayland impl from wl-shell to xdg-shell
This change ports vkcube in both C and C++ versions to use the stable xdg-shell protocol for Wayland compositors. The original wl-shell protocol has been deprecated and is being removed from major compositor libraries; wlroots has already dropped support. New cmake modules have been added to look for Wayland-Protocols (containing XML descriptions of all common protocols) and `wayland-scanner`, the tool used to convert the XML files to a usable C interface. The change also adds support for the xdg-decoration protocol, which for some compositors is provided and needed to let them know that they should draw a titlebar, borders with resize controls, and other standard features. Change-Id: I39bedda93a7c5d0aeeb59c68023552723b413567
Diffstat (limited to 'cube/cube.cpp')
-rw-r--r--cube/cube.cpp101
1 files changed, 84 insertions, 17 deletions
diff --git a/cube/cube.cpp b/cube/cube.cpp
index 87b24f9f..abe80a88 100644
--- a/cube/cube.cpp
+++ b/cube/cube.cpp
@@ -22,6 +22,8 @@
#include <X11/Xutil.h>
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
#include <linux/input.h>
+#include "xdg-shell-client-header.h"
+#include "xdg-decoration-client-header.h"
#endif
#include <cassert>
@@ -289,8 +291,12 @@ struct Demo {
wl_registry *registry;
wl_compositor *compositor;
wl_surface *window;
- wl_shell *shell;
- wl_shell_surface *shell_surface;
+ xdg_wm_base *wm_base;
+ zxdg_decoration_manager_v1 *xdg_decoration_mgr;
+ zxdg_toplevel_decoration_v1 *toplevel_decoration;
+ xdg_surface *window_surface;
+ bool xdg_surface_has_been_configured;
+ xdg_toplevel *window_toplevel;
wl_seat *seat;
wl_pointer *pointer;
wl_keyboard *keyboard;
@@ -418,7 +424,7 @@ static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uin
uint32_t state) {
Demo *demo = (Demo *)data;
if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
- wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
+ xdg_toplevel_move(demo->window_toplevel, demo->seat, serial);
}
}
@@ -486,16 +492,24 @@ static const wl_seat_listener seat_listener = {
seat_handle_capabilities,
};
+static void wm_base_ping(void *data, xdg_wm_base *xdg_wm_base, uint32_t serial) { xdg_wm_base_pong(xdg_wm_base, serial); }
+
+static const struct xdg_wm_base_listener wm_base_listener = {wm_base_ping};
+
static void registry_handle_global(void *data, wl_registry *registry, uint32_t id, const char *interface, uint32_t version) {
Demo *demo = (Demo *)data;
// pickup wayland objects when they appear
- if (strcmp(interface, "wl_compositor") == 0) {
+ if (strcmp(interface, wl_compositor_interface.name) == 0) {
demo->compositor = (wl_compositor *)wl_registry_bind(registry, id, &wl_compositor_interface, 1);
- } else if (strcmp(interface, "wl_shell") == 0) {
- demo->shell = (wl_shell *)wl_registry_bind(registry, id, &wl_shell_interface, 1);
- } else if (strcmp(interface, "wl_seat") == 0) {
+ } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
+ demo->wm_base = (xdg_wm_base *)wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
+ xdg_wm_base_add_listener(demo->wm_base, &wm_base_listener, nullptr);
+ } else if (strcmp(interface, wl_seat_interface.name) == 0) {
demo->seat = (wl_seat *)wl_registry_bind(registry, id, &wl_seat_interface, 1);
wl_seat_add_listener(demo->seat, &seat_listener, demo);
+ } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
+ demo->xdg_decoration_mgr =
+ (zxdg_decoration_manager_v1 *)wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
}
}
@@ -525,8 +539,12 @@ Demo::Demo()
registry{nullptr},
compositor{nullptr},
window{nullptr},
- shell{nullptr},
- shell_surface{nullptr},
+ wm_base{nullptr},
+ xdg_decoration_mgr{nullptr},
+ toplevel_decoration{nullptr},
+ window_surface{nullptr},
+ xdg_surface_has_been_configured{false},
+ window_toplevel{nullptr},
seat{nullptr},
pointer{nullptr},
keyboard{nullptr},
@@ -669,9 +687,14 @@ void Demo::cleanup() {
wl_keyboard_destroy(keyboard);
wl_pointer_destroy(pointer);
wl_seat_destroy(seat);
- wl_shell_surface_destroy(shell_surface);
+ xdg_toplevel_destroy(window_toplevel);
+ xdg_surface_destroy(window_surface);
wl_surface_destroy(window);
- wl_shell_destroy(shell);
+ xdg_wm_base_destroy(wm_base);
+ if (xdg_decoration_mgr) {
+ zxdg_toplevel_decoration_v1_destroy(toplevel_decoration);
+ zxdg_decoration_manager_v1_destroy(xdg_decoration_mgr);
+ }
wl_compositor_destroy(compositor);
wl_registry_destroy(registry);
wl_display_disconnect(display);
@@ -2690,7 +2713,39 @@ void Demo::run() {
}
}
+static void handle_surface_configure(void *data, xdg_surface *xdg_surface, uint32_t serial) {
+ Demo *demo = (Demo *)data;
+ xdg_surface_ack_configure(xdg_surface, serial);
+ if (demo->xdg_surface_has_been_configured) {
+ demo->resize();
+ }
+ demo->xdg_surface_has_been_configured = true;
+}
+
+static const xdg_surface_listener surface_listener = {handle_surface_configure};
+
+static void handle_toplevel_configure(void *data, xdg_toplevel *xdg_toplevel, int32_t width, int32_t height,
+ struct wl_array *states) {
+ Demo *demo = (Demo *)data;
+ demo->width = width;
+ demo->height = height;
+ // This will be followed by a surface configure
+}
+
+static void handle_toplevel_close(void *data, xdg_toplevel *xdg_toplevel) {
+ Demo *demo = (Demo *)data;
+ demo->quit = true;
+}
+
+static const xdg_toplevel_listener toplevel_listener = {handle_toplevel_configure, handle_toplevel_close};
+
void Demo::create_window() {
+ if (!wm_base) {
+ printf("Compositor did not provide the standard protocol xdg-wm-base\n");
+ fflush(stdout);
+ exit(1);
+ }
+
window = wl_compositor_create_surface(compositor);
if (!window) {
printf("Can not create wayland_surface from compositor!\n");
@@ -2698,16 +2753,28 @@ void Demo::create_window() {
exit(1);
}
- shell_surface = wl_shell_get_shell_surface(shell, window);
- if (!shell_surface) {
- printf("Can not get shell_surface from wayland_surface!\n");
+ window_surface = xdg_wm_base_get_xdg_surface(wm_base, window);
+ if (!window_surface) {
+ printf("Can not get xdg_surface from wayland_surface!\n");
fflush(stdout);
exit(1);
}
+ window_toplevel = xdg_surface_get_toplevel(window_surface);
+ if (!window_toplevel) {
+ printf("Can not allocate xdg_toplevel for xdg_surface!\n");
+ fflush(stdout);
+ exit(1);
+ }
+ xdg_surface_add_listener(window_surface, &surface_listener, this);
+ xdg_toplevel_add_listener(window_toplevel, &toplevel_listener, this);
+ xdg_toplevel_set_title(window_toplevel, APP_SHORT_NAME);
+ if (xdg_decoration_mgr) {
+ // if supported, let the compositor render titlebars for us
+ toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(xdg_decoration_mgr, window_toplevel);
+ zxdg_toplevel_decoration_v1_set_mode(toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
+ }
- wl_shell_surface_add_listener(shell_surface, &shell_surface_listener, this);
- wl_shell_surface_set_toplevel(shell_surface);
- wl_shell_surface_set_title(shell_surface, APP_SHORT_NAME);
+ wl_surface_commit(window);
}
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
void Demo::run() {