1 #include <GLES2/gl2.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <wayland-client.h>
6 #include <wayland-egl.h>
7 #include <wlr/render/egl.h>
8 #include "xdg-shell-client-protocol.h"
9 #include "xdg-decoration-unstable-v1-client-protocol.h"
10
11 /**
12 * Usage: toplevel-decoration [mode]
13 * Creates an xdg-toplevel supporting decoration negotiation. If `mode` is
14 * specified, the client will prefer this decoration mode.
15 */
16
17 static int width = 500, height = 300;
18
19 static struct wl_compositor *compositor = NULL;
20 static struct xdg_wm_base *wm_base = NULL;
21 static struct zxdg_decoration_manager_v1 *decoration_manager = NULL;
22
23 struct wlr_egl egl;
24 struct wl_egl_window *egl_window;
25 struct wlr_egl_surface *egl_surface;
26
27 struct zxdg_toplevel_decoration_v1 *decoration;
28 enum zxdg_toplevel_decoration_v1_mode client_preferred_mode, current_mode;
29
get_mode_name(enum zxdg_toplevel_decoration_v1_mode mode)30 static const char *get_mode_name(enum zxdg_toplevel_decoration_v1_mode mode) {
31 switch (mode) {
32 case ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE:
33 return "client-side decorations";
34 case ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE:
35 return "server-side decorations";
36 }
37 abort();
38 }
39
request_preferred_mode(void)40 static void request_preferred_mode(void) {
41 enum zxdg_toplevel_decoration_v1_mode mode = client_preferred_mode;
42 if (mode == 0) {
43 printf("Requesting compositor preferred mode\n");
44 zxdg_toplevel_decoration_v1_unset_mode(decoration);
45 return;
46 }
47 if (mode == current_mode) {
48 return;
49 }
50
51 printf("Requesting %s\n", get_mode_name(mode));
52 zxdg_toplevel_decoration_v1_set_mode(decoration, mode);
53 }
54
draw(void)55 static void draw(void) {
56 eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context);
57
58 float color[] = {1.0, 1.0, 0.0, 1.0};
59 if (current_mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE) {
60 color[0] = 0.0;
61 }
62
63 glViewport(0, 0, width, height);
64 glClearColor(color[0], color[1], color[2], 1.0);
65 glClear(GL_COLOR_BUFFER_BIT);
66
67 eglSwapBuffers(egl.display, egl_surface);
68 }
69
xdg_surface_handle_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)70 static void xdg_surface_handle_configure(void *data,
71 struct xdg_surface *xdg_surface, uint32_t serial) {
72 xdg_surface_ack_configure(xdg_surface, serial);
73 wl_egl_window_resize(egl_window, width, height, 0, 0);
74 draw();
75 }
76
77 static const struct xdg_surface_listener xdg_surface_listener = {
78 .configure = xdg_surface_handle_configure,
79 };
80
xdg_toplevel_handle_configure(void * data,struct xdg_toplevel * xdg_toplevel,int32_t w,int32_t h,struct wl_array * states)81 static void xdg_toplevel_handle_configure(void *data,
82 struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h,
83 struct wl_array *states) {
84 width = w;
85 height = h;
86 }
87
88 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
89 .configure = xdg_toplevel_handle_configure,
90 };
91
decoration_handle_configure(void * data,struct zxdg_toplevel_decoration_v1 * decoration,enum zxdg_toplevel_decoration_v1_mode mode)92 static void decoration_handle_configure(void *data,
93 struct zxdg_toplevel_decoration_v1 *decoration,
94 enum zxdg_toplevel_decoration_v1_mode mode) {
95 printf("Using %s\n", get_mode_name(mode));
96 current_mode = mode;
97 }
98
99 static const struct zxdg_toplevel_decoration_v1_listener decoration_listener = {
100 .configure = decoration_handle_configure,
101 };
102
pointer_handle_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t sx,wl_fixed_t sy)103 static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
104 uint32_t serial, struct wl_surface *surface,
105 wl_fixed_t sx, wl_fixed_t sy) {
106 // No-op
107 }
108
pointer_handle_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)109 static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
110 uint32_t serial, struct wl_surface *surface) {
111 // No-op
112 }
113
pointer_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t sx,wl_fixed_t sy)114 static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
115 uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {
116 // No-op
117 }
118
pointer_handle_button(void * data,struct wl_pointer * wl_pointer,uint32_t serial,uint32_t time,uint32_t button,enum wl_pointer_button_state state)119 static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
120 uint32_t serial, uint32_t time, uint32_t button,
121 enum wl_pointer_button_state state) {
122 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
123 // Toggle mode
124 if (client_preferred_mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE) {
125 client_preferred_mode = ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
126 } else {
127 client_preferred_mode = ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE;
128 }
129 request_preferred_mode();
130 }
131 }
132
pointer_handle_axis(void * data,struct wl_pointer * wl_pointer,uint32_t time,enum wl_pointer_axis axis,wl_fixed_t value)133 static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
134 uint32_t time, enum wl_pointer_axis axis, wl_fixed_t value) {
135 // No-op
136 }
137
138 static const struct wl_pointer_listener pointer_listener = {
139 .enter = pointer_handle_enter,
140 .leave = pointer_handle_leave,
141 .motion = pointer_handle_motion,
142 .button = pointer_handle_button,
143 .axis = pointer_handle_axis,
144 };
145
seat_handle_capabilities(void * data,struct wl_seat * seat,enum wl_seat_capability caps)146 static void seat_handle_capabilities(void *data, struct wl_seat *seat,
147 enum wl_seat_capability caps) {
148 if (caps & WL_SEAT_CAPABILITY_POINTER) {
149 struct wl_pointer *pointer = wl_seat_get_pointer(seat);
150 wl_pointer_add_listener(pointer, &pointer_listener, NULL);
151 }
152 }
153
154 static const struct wl_seat_listener seat_listener = {
155 .capabilities = seat_handle_capabilities,
156 };
157
handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)158 static void handle_global(void *data, struct wl_registry *registry,
159 uint32_t name, const char *interface, uint32_t version) {
160 if (strcmp(interface, wl_compositor_interface.name) == 0) {
161 compositor = wl_registry_bind(registry, name, &wl_compositor_interface,
162 1);
163 } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
164 wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
165 } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
166 decoration_manager = wl_registry_bind(registry, name,
167 &zxdg_decoration_manager_v1_interface, 1);
168 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
169 struct wl_seat *seat =
170 wl_registry_bind(registry, name, &wl_seat_interface, 1);
171 wl_seat_add_listener(seat, &seat_listener, NULL);
172 }
173 }
174
handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)175 static void handle_global_remove(void *data, struct wl_registry *registry,
176 uint32_t name) {
177 // Who cares?
178 }
179
180 static const struct wl_registry_listener registry_listener = {
181 .global = handle_global,
182 .global_remove = handle_global_remove,
183 };
184
main(int argc,char ** argv)185 int main(int argc, char **argv) {
186 if (argc == 2) {
187 char *mode = argv[1];
188 if (strcmp(mode, "client") == 0) {
189 client_preferred_mode = ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
190 } else if (strcmp(mode, "server") == 0) {
191 client_preferred_mode = ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE;
192 } else {
193 fprintf(stderr, "Invalid decoration mode\n");
194 return EXIT_FAILURE;
195 }
196 }
197
198 struct wl_display *display = wl_display_connect(NULL);
199 if (display == NULL) {
200 fprintf(stderr, "Failed to create display\n");
201 return EXIT_FAILURE;
202 }
203
204 struct wl_registry *registry = wl_display_get_registry(display);
205 wl_registry_add_listener(registry, ®istry_listener, NULL);
206 wl_display_roundtrip(display);
207
208 if (compositor == NULL) {
209 fprintf(stderr, "wl-compositor not available\n");
210 return EXIT_FAILURE;
211 }
212 if (wm_base == NULL) {
213 fprintf(stderr, "xdg-shell not available\n");
214 return EXIT_FAILURE;
215 }
216 if (decoration_manager == NULL) {
217 fprintf(stderr, "xdg-decoration not available\n");
218 return EXIT_FAILURE;
219 }
220
221 wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL,
222 WL_SHM_FORMAT_ARGB8888);
223
224 struct wl_surface *surface = wl_compositor_create_surface(compositor);
225 struct xdg_surface *xdg_surface =
226 xdg_wm_base_get_xdg_surface(wm_base, surface);
227 struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
228 decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(
229 decoration_manager, xdg_toplevel);
230
231 wl_display_roundtrip(display);
232 request_preferred_mode();
233
234 xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
235 xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
236 zxdg_toplevel_decoration_v1_add_listener(decoration, &decoration_listener,
237 NULL);
238 wl_surface_commit(surface);
239
240 egl_window = wl_egl_window_create(surface, width, height);
241 egl_surface = wlr_egl_create_surface(&egl, egl_window);
242
243 wl_display_roundtrip(display);
244
245 draw();
246
247 while (wl_display_dispatch(display) != -1) {
248 // This space is intentionally left blank
249 }
250
251 return EXIT_SUCCESS;
252 }
253