git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

scenefx/tinywl/tinywl.c (49.8K)

   1 #include <assert.h>
   2 #include <getopt.h>
   3 #include <stdbool.h>
   4 #include <stdlib.h>
   5 #include <stdio.h>
   6 #include <time.h>
   7 #include <scenefx/render/fx_renderer/fx_renderer.h>
   8 #include <scenefx/types/fx/clipped_region.h>
   9 #include <scenefx/types/wlr_scene.h>
  10 #include <unistd.h>
  11 #include <wayland-server-core.h>
  12 #include <wayland-util.h>
  13 #include <wlr/backend.h>
  14 #include <wlr/render/allocator.h>
  15 #include <wlr/render/wlr_renderer.h>
  16 #include <wlr/types/wlr_cursor.h>
  17 #include <wlr/types/wlr_compositor.h>
  18 #include <wlr/types/wlr_data_device.h>
  19 #include <wlr/types/wlr_input_device.h>
  20 #include <wlr/types/wlr_keyboard.h>
  21 #include <wlr/types/wlr_output.h>
  22 #include <wlr/types/wlr_output_layout.h>
  23 #include <wlr/types/wlr_pointer.h>
  24 #include <wlr/types/wlr_seat.h>
  25 #include <wlr/types/wlr_subcompositor.h>
  26 #include <wlr/types/wlr_xcursor_manager.h>
  27 #include <wlr/types/wlr_xdg_shell.h>
  28 #include <wlr/util/log.h>
  29 #include <xkbcommon/xkbcommon.h>
  30 
  31 #define BORDER_THICKNESS 3
  32 
  33 /* For brevity's sake, struct members are annotated where they are used. */
  34 enum tinywl_cursor_mode {
  35 	TINYWL_CURSOR_PASSTHROUGH,
  36 	TINYWL_CURSOR_MOVE,
  37 	TINYWL_CURSOR_RESIZE,
  38 };
  39 
  40 struct tinywl_server {
  41 	struct wl_display *wl_display;
  42 	struct wlr_backend *backend;
  43 	struct wlr_renderer *renderer;
  44 	struct wlr_allocator *allocator;
  45 	struct wlr_scene *scene;
  46 	struct wlr_scene_output_layout *scene_layout;
  47 	struct {
  48 		struct wlr_scene_tree *bottom_layer;
  49 		/** Used for optimized blur. Everything exclusively below gets blurred */
  50 		struct wlr_scene_optimized_blur *blur_layer;
  51 		struct wlr_scene_tree *toplevel_layer;
  52 	} layers;
  53 
  54 	struct wlr_xdg_shell *xdg_shell;
  55 	struct wl_listener new_xdg_toplevel;
  56 	struct wl_listener new_xdg_popup;
  57 	struct wl_list toplevels;
  58 
  59 	struct wlr_cursor *cursor;
  60 	struct wlr_xcursor_manager *cursor_mgr;
  61 	struct wl_listener cursor_motion;
  62 	struct wl_listener cursor_motion_absolute;
  63 	struct wl_listener cursor_button;
  64 	struct wl_listener cursor_axis;
  65 	struct wl_listener cursor_frame;
  66 
  67 	struct wlr_seat *seat;
  68 	struct wl_listener new_input;
  69 	struct wl_listener request_cursor;
  70 	struct wl_listener pointer_focus_change;
  71 	struct wl_listener request_set_selection;
  72 	struct wl_list keyboards;
  73 	enum tinywl_cursor_mode cursor_mode;
  74 	struct tinywl_toplevel *grabbed_toplevel;
  75 	double grab_x, grab_y;
  76 	struct wlr_box grab_geobox;
  77 	uint32_t resize_edges;
  78 
  79 	struct wlr_output_layout *output_layout;
  80 	struct wl_list outputs;
  81 	struct wl_listener new_output;
  82 };
  83 
  84 struct tinywl_output {
  85 	struct wl_list link;
  86 	struct tinywl_server *server;
  87 	struct wlr_output *wlr_output;
  88 	struct wl_listener frame;
  89 	struct wl_listener request_state;
  90 	struct wl_listener destroy;
  91 };
  92 
  93 struct tinywl_toplevel {
  94 	struct wl_list link;
  95 	struct tinywl_server *server;
  96 	struct wlr_xdg_toplevel *xdg_toplevel;
  97 	struct wlr_scene_tree *xdg_scene_tree;
  98 	struct wlr_scene_tree *scene_tree;
  99 	struct wl_listener map;
 100 	struct wl_listener unmap;
 101 	struct wl_listener commit;
 102 	struct wl_listener destroy;
 103 	struct wl_listener request_move;
 104 	struct wl_listener request_resize;
 105 	struct wl_listener request_maximize;
 106 	struct wl_listener request_fullscreen;
 107 
 108 	float opacity;
 109 	int corner_radius;
 110 	struct wlr_scene_blur *blur;
 111 	struct wlr_scene_shadow *shadow;
 112 	struct wlr_scene_rect *border;
 113 };
 114 
 115 struct tinywl_popup {
 116 	struct wlr_xdg_popup *xdg_popup;
 117 	struct wl_listener commit;
 118 	struct wl_listener destroy;
 119 };
 120 
 121 struct tinywl_keyboard {
 122 	struct wl_list link;
 123 	struct tinywl_server *server;
 124 	struct wlr_keyboard *wlr_keyboard;
 125 
 126 	struct wl_listener modifiers;
 127 	struct wl_listener key;
 128 	struct wl_listener destroy;
 129 };
 130 
 131 static void focus_toplevel(struct tinywl_toplevel *toplevel) {
 132 	/* Note: this function only deals with keyboard focus. */
 133 	if (toplevel == NULL) {
 134 		return;
 135 	}
 136 	struct tinywl_server *server = toplevel->server;
 137 	struct wlr_seat *seat = server->seat;
 138 	struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface;
 139 	struct wlr_surface *surface = toplevel->xdg_toplevel->base->surface;
 140 	if (prev_surface == surface) {
 141 		/* Don't re-focus an already focused surface. */
 142 		return;
 143 	}
 144 	if (prev_surface) {
 145 		/*
 146 		 * Deactivate the previously focused surface. This lets the client know
 147 		 * it no longer has focus and the client will repaint accordingly, e.g.
 148 		 * stop displaying a caret.
 149 		 */
 150 		struct wlr_xdg_toplevel *prev_toplevel =
 151 			wlr_xdg_toplevel_try_from_wlr_surface(prev_surface);
 152 		if (prev_toplevel != NULL) {
 153 			wlr_xdg_toplevel_set_activated(prev_toplevel, false);
 154 		}
 155 	}
 156 	struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
 157 	/* Move the toplevel to the front */
 158 	wlr_scene_node_raise_to_top(&toplevel->scene_tree->node);
 159 	wl_list_remove(&toplevel->link);
 160 	wl_list_insert(&server->toplevels, &toplevel->link);
 161 	/* Activate the new surface */
 162 	wlr_xdg_toplevel_set_activated(toplevel->xdg_toplevel, true);
 163 	/*
 164 	 * Tell the seat to have the keyboard enter this surface. wlroots will keep
 165 	 * track of this and automatically send key events to the appropriate
 166 	 * clients without additional work on your part.
 167 	 */
 168 	if (keyboard != NULL) {
 169 		wlr_seat_keyboard_notify_enter(seat, surface,
 170 			keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers);
 171 	}
 172 }
 173 
 174 static void keyboard_handle_modifiers(
 175 		struct wl_listener *listener, void *data) {
 176 	/* This event is raised when a modifier key, such as shift or alt, is
 177 	 * pressed. We simply communicate this to the client. */
 178 	struct tinywl_keyboard *keyboard =
 179 		wl_container_of(listener, keyboard, modifiers);
 180 	/*
 181 	 * A seat can only have one keyboard, but this is a limitation of the
 182 	 * Wayland protocol - not wlroots. We assign all connected keyboards to the
 183 	 * same seat. You can swap out the underlying wlr_keyboard like this and
 184 	 * wlr_seat handles this transparently.
 185 	 */
 186 	wlr_seat_set_keyboard(keyboard->server->seat, keyboard->wlr_keyboard);
 187 	/* Send modifiers to the client. */
 188 	wlr_seat_keyboard_notify_modifiers(keyboard->server->seat,
 189 		&keyboard->wlr_keyboard->modifiers);
 190 }
 191 
 192 static bool handle_keybinding(struct tinywl_server *server, xkb_keysym_t sym) {
 193 	/*
 194 	 * Here we handle compositor keybindings. This is when the compositor is
 195 	 * processing keys, rather than passing them on to the client for its own
 196 	 * processing.
 197 	 *
 198 	 * This function assumes Alt is held down.
 199 	 */
 200 	switch (sym) {
 201 	case XKB_KEY_Escape:
 202 		wl_display_terminate(server->wl_display);
 203 		break;
 204 	case XKB_KEY_F1:
 205 		/* Cycle to the next toplevel */
 206 		if (wl_list_length(&server->toplevels) < 2) {
 207 			break;
 208 		}
 209 		struct tinywl_toplevel *next_toplevel =
 210 			wl_container_of(server->toplevels.prev, next_toplevel, link);
 211 		focus_toplevel(next_toplevel);
 212 		break;
 213 	default:
 214 		return false;
 215 	}
 216 	return true;
 217 }
 218 
 219 static void keyboard_handle_key(
 220 		struct wl_listener *listener, void *data) {
 221 	/* This event is raised when a key is pressed or released. */
 222 	struct tinywl_keyboard *keyboard =
 223 		wl_container_of(listener, keyboard, key);
 224 	struct tinywl_server *server = keyboard->server;
 225 	struct wlr_keyboard_key_event *event = data;
 226 	struct wlr_seat *seat = server->seat;
 227 
 228 	/* Translate libinput keycode -> xkbcommon */
 229 	uint32_t keycode = event->keycode + 8;
 230 	/* Get a list of keysyms based on the keymap for this keyboard */
 231 	const xkb_keysym_t *syms;
 232 	int nsyms = xkb_state_key_get_syms(
 233 			keyboard->wlr_keyboard->xkb_state, keycode, &syms);
 234 
 235 	bool handled = false;
 236 	uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->wlr_keyboard);
 237 	if ((modifiers & WLR_MODIFIER_ALT) &&
 238 			event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
 239 		/* If alt is held down and this button was _pressed_, we attempt to
 240 		 * process it as a compositor keybinding. */
 241 		for (int i = 0; i < nsyms; i++) {
 242 			handled = handle_keybinding(server, syms[i]);
 243 		}
 244 	}
 245 
 246 	if (!handled) {
 247 		/* Otherwise, we pass it along to the client. */
 248 		wlr_seat_set_keyboard(seat, keyboard->wlr_keyboard);
 249 		wlr_seat_keyboard_notify_key(seat, event->time_msec,
 250 			event->keycode, event->state);
 251 	}
 252 }
 253 
 254 static void keyboard_handle_destroy(struct wl_listener *listener, void *data) {
 255 	/* This event is raised by the keyboard base wlr_input_device to signal
 256 	 * the destruction of the wlr_keyboard. It will no longer receive events
 257 	 * and should be destroyed.
 258 	 */
 259 	struct tinywl_keyboard *keyboard =
 260 		wl_container_of(listener, keyboard, destroy);
 261 	wl_list_remove(&keyboard->modifiers.link);
 262 	wl_list_remove(&keyboard->key.link);
 263 	wl_list_remove(&keyboard->destroy.link);
 264 	wl_list_remove(&keyboard->link);
 265 	free(keyboard);
 266 }
 267 
 268 static void server_new_keyboard(struct tinywl_server *server,
 269 		struct wlr_input_device *device) {
 270 	struct wlr_keyboard *wlr_keyboard = wlr_keyboard_from_input_device(device);
 271 
 272 	struct tinywl_keyboard *keyboard = calloc(1, sizeof(*keyboard));
 273 	keyboard->server = server;
 274 	keyboard->wlr_keyboard = wlr_keyboard;
 275 
 276 	/* We need to prepare an XKB keymap and assign it to the keyboard. This
 277 	 * assumes the defaults (e.g. layout = "us"). */
 278 	struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
 279 	struct xkb_keymap *keymap = xkb_keymap_new_from_names(context, NULL,
 280 		XKB_KEYMAP_COMPILE_NO_FLAGS);
 281 
 282 	wlr_keyboard_set_keymap(wlr_keyboard, keymap);
 283 	xkb_keymap_unref(keymap);
 284 	xkb_context_unref(context);
 285 	wlr_keyboard_set_repeat_info(wlr_keyboard, 25, 600);
 286 
 287 	/* Here we set up listeners for keyboard events. */
 288 	keyboard->modifiers.notify = keyboard_handle_modifiers;
 289 	wl_signal_add(&wlr_keyboard->events.modifiers, &keyboard->modifiers);
 290 	keyboard->key.notify = keyboard_handle_key;
 291 	wl_signal_add(&wlr_keyboard->events.key, &keyboard->key);
 292 	keyboard->destroy.notify = keyboard_handle_destroy;
 293 	wl_signal_add(&device->events.destroy, &keyboard->destroy);
 294 
 295 	wlr_seat_set_keyboard(server->seat, keyboard->wlr_keyboard);
 296 
 297 	/* And add the keyboard to our list of keyboards */
 298 	wl_list_insert(&server->keyboards, &keyboard->link);
 299 }
 300 
 301 static void server_new_pointer(struct tinywl_server *server,
 302 		struct wlr_input_device *device) {
 303 	/* We don't do anything special with pointers. All of our pointer handling
 304 	 * is proxied through wlr_cursor. On another compositor, you might take this
 305 	 * opportunity to do libinput configuration on the device to set
 306 	 * acceleration, etc. */
 307 	wlr_cursor_attach_input_device(server->cursor, device);
 308 }
 309 
 310 static void server_new_input(struct wl_listener *listener, void *data) {
 311 	/* This event is raised by the backend when a new input device becomes
 312 	 * available. */
 313 	struct tinywl_server *server =
 314 		wl_container_of(listener, server, new_input);
 315 	struct wlr_input_device *device = data;
 316 	switch (device->type) {
 317 	case WLR_INPUT_DEVICE_KEYBOARD:
 318 		server_new_keyboard(server, device);
 319 		break;
 320 	case WLR_INPUT_DEVICE_POINTER:
 321 		server_new_pointer(server, device);
 322 		break;
 323 	default:
 324 		break;
 325 	}
 326 	/* We need to let the wlr_seat know what our capabilities are, which is
 327 	 * communiciated to the client. In TinyWL we always have a cursor, even if
 328 	 * there are no pointer devices, so we always include that capability. */
 329 	uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
 330 	if (!wl_list_empty(&server->keyboards)) {
 331 		caps |= WL_SEAT_CAPABILITY_KEYBOARD;
 332 	}
 333 	wlr_seat_set_capabilities(server->seat, caps);
 334 }
 335 
 336 static void seat_request_cursor(struct wl_listener *listener, void *data) {
 337 	struct tinywl_server *server = wl_container_of(
 338 			listener, server, request_cursor);
 339 	/* This event is raised by the seat when a client provides a cursor image */
 340 	struct wlr_seat_pointer_request_set_cursor_event *event = data;
 341 	struct wlr_seat_client *focused_client =
 342 		server->seat->pointer_state.focused_client;
 343 	/* This can be sent by any client, so we check to make sure this one is
 344 	 * actually has pointer focus first. */
 345 	if (focused_client == event->seat_client) {
 346 		/* Once we've vetted the client, we can tell the cursor to use the
 347 		 * provided surface as the cursor image. It will set the hardware cursor
 348 		 * on the output that it's currently on and continue to do so as the
 349 		 * cursor moves between outputs. */
 350 		wlr_cursor_set_surface(server->cursor, event->surface,
 351 				event->hotspot_x, event->hotspot_y);
 352 	}
 353 }
 354 
 355 static void seat_pointer_focus_change(struct wl_listener *listener, void *data) {
 356 	struct tinywl_server *server = wl_container_of(
 357 			listener, server, pointer_focus_change);
 358 	/* This event is raised when the pointer focus is changed, including when the
 359 	 * client is closed. We set the cursor image to its default if target surface
 360 	 * is NULL */
 361 	struct wlr_seat_pointer_focus_change_event *event = data;
 362 	if (event->new_surface == NULL) {
 363 		wlr_cursor_set_xcursor(server->cursor, server->cursor_mgr, "default");
 364 	}
 365 }
 366 
 367 static void seat_request_set_selection(struct wl_listener *listener, void *data) {
 368 	/* This event is raised by the seat when a client wants to set the selection,
 369 	 * usually when the user copies something. wlroots allows compositors to
 370 	 * ignore such requests if they so choose, but in tinywl we always honor
 371 	 */
 372 	struct tinywl_server *server = wl_container_of(
 373 			listener, server, request_set_selection);
 374 	struct wlr_seat_request_set_selection_event *event = data;
 375 	wlr_seat_set_selection(server->seat, event->source, event->serial);
 376 }
 377 
 378 static struct tinywl_toplevel *desktop_toplevel_at(
 379 		struct tinywl_server *server, double lx, double ly,
 380 		struct wlr_surface **surface, double *sx, double *sy) {
 381 	/* This returns the topmost node in the scene at the given layout coords.
 382 	 * We only care about surface nodes as we are specifically looking for a
 383 	 * surface in the surface tree of a tinywl_toplevel. */
 384 	struct wlr_scene_node *node = wlr_scene_node_at(
 385 		&server->scene->tree.node, lx, ly, sx, sy);
 386 	if (node == NULL || node->type != WLR_SCENE_NODE_BUFFER) {
 387 		return NULL;
 388 	}
 389 	struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node);
 390 	struct wlr_scene_surface *scene_surface =
 391 		wlr_scene_surface_try_from_buffer(scene_buffer);
 392 	if (!scene_surface) {
 393 		return NULL;
 394 	}
 395 
 396 	*surface = scene_surface->surface;
 397 	/* Find the node corresponding to the tinywl_toplevel at the root of this
 398 	 * surface tree, it is the only one for which we set the data field. */
 399 	struct wlr_scene_tree *tree = node->parent;
 400 	while (tree != NULL && tree->node.data == NULL) {
 401 		tree = tree->node.parent;
 402 	}
 403 	return tree->node.data;
 404 }
 405 
 406 static void reset_cursor_mode(struct tinywl_server *server) {
 407 	/* Reset the cursor mode to passthrough. */
 408 	server->cursor_mode = TINYWL_CURSOR_PASSTHROUGH;
 409 	server->grabbed_toplevel = NULL;
 410 }
 411 
 412 static void process_cursor_move(struct tinywl_server *server) {
 413 	/* Move the grabbed toplevel to the new position. */
 414 	struct tinywl_toplevel *toplevel = server->grabbed_toplevel;
 415 	wlr_scene_node_set_position(&toplevel->scene_tree->node,
 416 		server->cursor->x - server->grab_x,
 417 		server->cursor->y - server->grab_y);
 418 }
 419 
 420 static void process_cursor_resize(struct tinywl_server *server) {
 421 	/*
 422 	 * Resizing the grabbed toplevel can be a little bit complicated, because we
 423 	 * could be resizing from any corner or edge. This not only resizes the
 424 	 * toplevel on one or two axes, but can also move the toplevel if you resize
 425 	 * from the top or left edges (or top-left corner).
 426 	 *
 427 	 * Note that some shortcuts are taken here. In a more fleshed-out
 428 	 * compositor, you'd wait for the client to prepare a buffer at the new
 429 	 * size, then commit any movement that was prepared.
 430 	 */
 431 	struct tinywl_toplevel *toplevel = server->grabbed_toplevel;
 432 	double border_x = server->cursor->x - server->grab_x;
 433 	double border_y = server->cursor->y - server->grab_y;
 434 	int new_left = server->grab_geobox.x;
 435 	int new_right = server->grab_geobox.x + server->grab_geobox.width;
 436 	int new_top = server->grab_geobox.y;
 437 	int new_bottom = server->grab_geobox.y + server->grab_geobox.height;
 438 
 439 	if (server->resize_edges & WLR_EDGE_TOP) {
 440 		new_top = border_y;
 441 		if (new_top >= new_bottom) {
 442 			new_top = new_bottom - 1;
 443 		}
 444 	} else if (server->resize_edges & WLR_EDGE_BOTTOM) {
 445 		new_bottom = border_y;
 446 		if (new_bottom <= new_top) {
 447 			new_bottom = new_top + 1;
 448 		}
 449 	}
 450 	if (server->resize_edges & WLR_EDGE_LEFT) {
 451 		new_left = border_x;
 452 		if (new_left >= new_right) {
 453 			new_left = new_right - 1;
 454 		}
 455 	} else if (server->resize_edges & WLR_EDGE_RIGHT) {
 456 		new_right = border_x;
 457 		if (new_right <= new_left) {
 458 			new_right = new_left + 1;
 459 		}
 460 	}
 461 
 462 	struct wlr_box *geo_box = &toplevel->xdg_toplevel->base->geometry;
 463 	wlr_scene_node_set_position(&toplevel->scene_tree->node,
 464 		new_left - geo_box->x, new_top - geo_box->y);
 465 
 466 	int new_width = new_right - new_left;
 467 	int new_height = new_bottom - new_top;
 468 	wlr_xdg_toplevel_set_size(toplevel->xdg_toplevel, new_width, new_height);
 469 
 470 	// Clip to geometry
 471 	struct wlr_box clip = {
 472 		.width = new_width,
 473 		.height = new_height,
 474 		.x = geo_box->x,
 475 		.y = geo_box->y,
 476 	};
 477 	wlr_scene_subsurface_tree_set_clip(&toplevel->scene_tree->node, &clip);
 478 }
 479 
 480 static void process_cursor_motion(struct tinywl_server *server, uint32_t time) {
 481 	/* If the mode is non-passthrough, delegate to those functions. */
 482 	if (server->cursor_mode == TINYWL_CURSOR_MOVE) {
 483 		process_cursor_move(server);
 484 		return;
 485 	} else if (server->cursor_mode == TINYWL_CURSOR_RESIZE) {
 486 		process_cursor_resize(server);
 487 		return;
 488 	}
 489 
 490 	/* Otherwise, find the toplevel under the pointer and send the event along. */
 491 	double sx, sy;
 492 	struct wlr_seat *seat = server->seat;
 493 	struct wlr_surface *surface = NULL;
 494 	struct tinywl_toplevel *toplevel = desktop_toplevel_at(server,
 495 			server->cursor->x, server->cursor->y, &surface, &sx, &sy);
 496 	if (!toplevel) {
 497 		/* If there's no toplevel under the cursor, set the cursor image to a
 498 		 * default. This is what makes the cursor image appear when you move it
 499 		 * around the screen, not over any toplevels. */
 500 		wlr_cursor_set_xcursor(server->cursor, server->cursor_mgr, "default");
 501 	}
 502 	if (surface) {
 503 		/*
 504 		 * Send pointer enter and motion events.
 505 		 *
 506 		 * The enter event gives the surface "pointer focus", which is distinct
 507 		 * from keyboard focus. You get pointer focus by moving the pointer over
 508 		 * a window.
 509 		 *
 510 		 * Note that wlroots will avoid sending duplicate enter/motion events if
 511 		 * the surface has already has pointer focus or if the client is already
 512 		 * aware of the coordinates passed.
 513 		 */
 514 		wlr_seat_pointer_notify_enter(seat, surface, sx, sy);
 515 		wlr_seat_pointer_notify_motion(seat, time, sx, sy);
 516 	} else {
 517 		/* Clear pointer focus so future button events and such are not sent to
 518 		 * the last client to have the cursor over it. */
 519 		wlr_seat_pointer_clear_focus(seat);
 520 	}
 521 }
 522 
 523 static void server_cursor_motion(struct wl_listener *listener, void *data) {
 524 	/* This event is forwarded by the cursor when a pointer emits a _relative_
 525 	 * pointer motion event (i.e. a delta) */
 526 	struct tinywl_server *server =
 527 		wl_container_of(listener, server, cursor_motion);
 528 	struct wlr_pointer_motion_event *event = data;
 529 	/* The cursor doesn't move unless we tell it to. The cursor automatically
 530 	 * handles constraining the motion to the output layout, as well as any
 531 	 * special configuration applied for the specific input device which
 532 	 * generated the event. You can pass NULL for the device if you want to move
 533 	 * the cursor around without any input. */
 534 	wlr_cursor_move(server->cursor, &event->pointer->base,
 535 			event->delta_x, event->delta_y);
 536 	process_cursor_motion(server, event->time_msec);
 537 }
 538 
 539 static void server_cursor_motion_absolute(
 540 		struct wl_listener *listener, void *data) {
 541 	/* This event is forwarded by the cursor when a pointer emits an _absolute_
 542 	 * motion event, from 0..1 on each axis. This happens, for example, when
 543 	 * wlroots is running under a Wayland window rather than KMS+DRM, and you
 544 	 * move the mouse over the window. You could enter the window from any edge,
 545 	 * so we have to warp the mouse there. There is also some hardware which
 546 	 * emits these events. */
 547 	struct tinywl_server *server =
 548 		wl_container_of(listener, server, cursor_motion_absolute);
 549 	struct wlr_pointer_motion_absolute_event *event = data;
 550 	wlr_cursor_warp_absolute(server->cursor, &event->pointer->base, event->x,
 551 		event->y);
 552 	process_cursor_motion(server, event->time_msec);
 553 }
 554 
 555 static void server_cursor_button(struct wl_listener *listener, void *data) {
 556 	/* This event is forwarded by the cursor when a pointer emits a button
 557 	 * event. */
 558 	struct tinywl_server *server =
 559 		wl_container_of(listener, server, cursor_button);
 560 	struct wlr_pointer_button_event *event = data;
 561 	/* Notify the client with pointer focus that a button press has occurred */
 562 	wlr_seat_pointer_notify_button(server->seat,
 563 			event->time_msec, event->button, event->state);
 564 	if (event->state == WL_POINTER_BUTTON_STATE_RELEASED) {
 565 		/* If you released any buttons, we exit interactive move/resize mode. */
 566 		reset_cursor_mode(server);
 567 	} else {
 568 		/* Focus that client if the button was _pressed_ */
 569 		double sx, sy;
 570 		struct wlr_surface *surface = NULL;
 571 		struct tinywl_toplevel *toplevel = desktop_toplevel_at(server,
 572 				server->cursor->x, server->cursor->y, &surface, &sx, &sy);
 573 		focus_toplevel(toplevel);
 574 	}
 575 }
 576 
 577 static void server_cursor_axis(struct wl_listener *listener, void *data) {
 578 	/* This event is forwarded by the cursor when a pointer emits an axis event,
 579 	 * for example when you move the scroll wheel. */
 580 	struct tinywl_server *server =
 581 		wl_container_of(listener, server, cursor_axis);
 582 	struct wlr_pointer_axis_event *event = data;
 583 	/* Notify the client with pointer focus of the axis event. */
 584 	wlr_seat_pointer_notify_axis(server->seat,
 585 			event->time_msec, event->orientation, event->delta,
 586 			event->delta_discrete, event->source, event->relative_direction);
 587 }
 588 
 589 static void server_cursor_frame(struct wl_listener *listener, void *data) {
 590 	/* This event is forwarded by the cursor when a pointer emits an frame
 591 	 * event. Frame events are sent after regular pointer events to group
 592 	 * multiple events together. For instance, two axis events may happen at the
 593 	 * same time, in which case a frame event won't be sent in between. */
 594 	struct tinywl_server *server =
 595 		wl_container_of(listener, server, cursor_frame);
 596 	/* Notify the client with pointer focus of the frame event. */
 597 	wlr_seat_pointer_notify_frame(server->seat);
 598 }
 599 
 600 static void xdg_toplevel_commit(struct wl_listener *listener, void *data) {
 601 	/* Called when a new surface state is committed. */
 602 	struct tinywl_toplevel *toplevel = wl_container_of(listener, toplevel, commit);
 603 
 604 	if (toplevel->xdg_toplevel->base->initial_commit) {
 605 		/* When an xdg_surface performs an initial commit, the compositor must
 606 		 * reply with a configure so the client can map the surface. tinywl
 607 		 * configures the xdg_toplevel with 0,0 size to let the client pick the
 608 		 * dimensions itself. */
 609 		wlr_xdg_toplevel_set_size(toplevel->xdg_toplevel, 0, 0);
 610 		return;
 611 	}
 612 
 613 	struct wlr_box *geometry = &toplevel->xdg_toplevel->base->geometry;
 614 	wlr_scene_subsurface_tree_set_clip(&toplevel->xdg_scene_tree->node, geometry);
 615 	wlr_scene_blur_set_size(toplevel->blur, geometry->width, geometry->height);
 616 
 617 	int border_width = geometry->width + (BORDER_THICKNESS * 2);
 618 	int border_height = geometry->height + (BORDER_THICKNESS * 2);
 619 
 620 	// technically we dont actually need the hole here since optimized blur would
 621 	// hide the border + shadow, but we do here to show compositors how to implement it
 622 
 623 	wlr_scene_rect_set_size(toplevel->border, border_width, border_height);
 624 	wlr_scene_rect_set_clipped_region(toplevel->border, (struct clipped_region) {
 625 			.corners = corner_radii_all(toplevel->corner_radius),
 626 			.area = { BORDER_THICKNESS, BORDER_THICKNESS, geometry->width, geometry->height }
 627 	});
 628 
 629 	int blur_sigma = toplevel->shadow->blur_sigma;
 630 	wlr_scene_shadow_set_size(toplevel->shadow,
 631 			border_width + (blur_sigma * 2),
 632 			border_height + (blur_sigma * 2));
 633 	wlr_scene_shadow_set_clipped_region(toplevel->shadow, (struct clipped_region) {
 634 			.corners = corner_radii_all(toplevel->corner_radius + BORDER_THICKNESS),
 635 			.area = { blur_sigma, blur_sigma, border_width, border_height }
 636 	});
 637 }
 638 
 639 static void output_configure_scene(struct wlr_scene_node *node,
 640 		struct tinywl_toplevel *toplevel) {
 641 	if (!node->enabled) {
 642 		return;
 643 	}
 644 
 645 	struct tinywl_toplevel *_toplevel = node->data;
 646 	if (_toplevel) {
 647 		toplevel = _toplevel;
 648 	}
 649 
 650 	if (node->type == WLR_SCENE_NODE_BUFFER) {
 651 		struct wlr_scene_buffer *buffer = wlr_scene_buffer_from_node(node);
 652 
 653 		struct wlr_scene_surface * scene_surface =
 654 			wlr_scene_surface_try_from_buffer(buffer);
 655 		if (!scene_surface) {
 656 			return;
 657 		}
 658 
 659 		struct wlr_xdg_surface *xdg_surface =
 660 			wlr_xdg_surface_try_from_wlr_surface(scene_surface->surface);
 661 
 662 		if (toplevel &&
 663 				xdg_surface &&
 664 				xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
 665 			wlr_scene_buffer_set_opacity(buffer, toplevel->opacity);
 666 
 667 			if (!wlr_subsurface_try_from_wlr_surface(xdg_surface->surface)) {
 668 				wlr_scene_buffer_set_corner_radii(
 669 						buffer, corner_radii_bottom(toplevel->corner_radius));
 670 			}
 671 		}
 672 	} else if (node->type == WLR_SCENE_NODE_TREE) {
 673 		struct wlr_scene_tree *tree = wl_container_of(node, tree, node);
 674 		struct wlr_scene_node *node;
 675 		wl_list_for_each(node, &tree->children, link) {
 676 			output_configure_scene(node, toplevel);
 677 		}
 678 	}
 679 }
 680 
 681 static void output_frame(struct wl_listener *listener, void *data) {
 682 	/* This function is called every time an output is ready to display a frame,
 683 	 * generally at the output's refresh rate (e.g. 60Hz). */
 684 	struct tinywl_output *output = wl_container_of(listener, output, frame);
 685 	struct wlr_scene *scene = output->server->scene;
 686 
 687 	struct wlr_scene_output *scene_output = wlr_scene_get_scene_output(
 688 		scene, output->wlr_output);
 689 
 690 	output_configure_scene(&scene_output->scene->tree.node, NULL);
 691 
 692 	/* Render the scene if needed and commit the output */
 693 	wlr_scene_output_commit(scene_output, NULL);
 694 
 695 	struct timespec now;
 696 	clock_gettime(CLOCK_MONOTONIC, &now);
 697 	wlr_scene_output_send_frame_done(scene_output, &now);
 698 }
 699 
 700 static void output_request_state(struct wl_listener *listener, void *data) {
 701 	/* This function is called when the backend requests a new state for
 702 	 * the output. For example, Wayland and X11 backends request a new mode
 703 	 * when the output window is resized. */
 704 	struct tinywl_output *output = wl_container_of(listener, output, request_state);
 705 	const struct wlr_output_event_request_state *event = data;
 706 	wlr_output_commit_state(output->wlr_output, event->state);
 707 
 708 	/*
 709 	 * Sets the blur node size to the outputs size. Assumes there's only one
 710 	 * output. More advanced compositors should either implement per output blur
 711 	 * nodes or set it to the size of all outputs.
 712 	 */
 713 	wlr_scene_optimized_blur_set_size(output->server->layers.blur_layer,
 714 			output->wlr_output->width, output->wlr_output->height);
 715 }
 716 
 717 static void output_destroy(struct wl_listener *listener, void *data) {
 718 	struct tinywl_output *output = wl_container_of(listener, output, destroy);
 719 
 720 	wl_list_remove(&output->frame.link);
 721 	wl_list_remove(&output->request_state.link);
 722 	wl_list_remove(&output->destroy.link);
 723 	wl_list_remove(&output->link);
 724 	free(output);
 725 }
 726 
 727 static void server_new_output(struct wl_listener *listener, void *data) {
 728 	/* This event is raised by the backend when a new output (aka a display or
 729 	 * monitor) becomes available. */
 730 	struct tinywl_server *server =
 731 		wl_container_of(listener, server, new_output);
 732 	struct wlr_output *wlr_output = data;
 733 
 734 	/* Configures the output created by the backend to use our allocator
 735 	 * and our renderer. Must be done once, before committing the output */
 736 	wlr_output_init_render(wlr_output, server->allocator, server->renderer);
 737 
 738 	/* The output may be disabled, switch it on. */
 739 	struct wlr_output_state state;
 740 	wlr_output_state_init(&state);
 741 	wlr_output_state_set_enabled(&state, true);
 742 
 743 	/* Some backends don't have modes. DRM+KMS does, and we need to set a mode
 744 	 * before we can use the output. The mode is a tuple of (width, height,
 745 	 * refresh rate), and each monitor supports only a specific set of modes. We
 746 	 * just pick the monitor's preferred mode, a more sophisticated compositor
 747 	 * would let the user configure it. */
 748 	struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output);
 749 	if (mode != NULL) {
 750 		wlr_output_state_set_mode(&state, mode);
 751 	}
 752 
 753 	/* Atomically applies the new output state. */
 754 	wlr_output_commit_state(wlr_output, &state);
 755 	wlr_output_state_finish(&state);
 756 
 757 	/* Allocates and configures our state for this output */
 758 	struct tinywl_output *output = calloc(1, sizeof(*output));
 759 	output->wlr_output = wlr_output;
 760 	output->server = server;
 761 
 762 	/* Sets up a listener for the frame event. */
 763 	output->frame.notify = output_frame;
 764 	wl_signal_add(&wlr_output->events.frame, &output->frame);
 765 
 766 	/* Sets up a listener for the state request event. */
 767 	output->request_state.notify = output_request_state;
 768 	wl_signal_add(&wlr_output->events.request_state, &output->request_state);
 769 
 770 	/* Sets up a listener for the destroy event. */
 771 	output->destroy.notify = output_destroy;
 772 	wl_signal_add(&wlr_output->events.destroy, &output->destroy);
 773 
 774 	wl_list_insert(&server->outputs, &output->link);
 775 
 776 	/* Adds this to the output layout. The add_auto function arranges outputs
 777 	 * from left-to-right in the order they appear. A more sophisticated
 778 	 * compositor would let the user configure the arrangement of outputs in the
 779 	 * layout.
 780 	 *
 781 	 * The output layout utility automatically adds a wl_output global to the
 782 	 * display, which Wayland clients can see to find out information about the
 783 	 * output (such as DPI, scale factor, manufacturer, etc).
 784 	 */
 785 	struct wlr_output_layout_output *l_output = wlr_output_layout_add_auto(server->output_layout,
 786 		wlr_output);
 787 	struct wlr_scene_output *scene_output = wlr_scene_output_create(server->scene, wlr_output);
 788 	wlr_scene_output_layout_add_output(server->scene_layout, l_output, scene_output);
 789 
 790 	/*
 791 	 * Sets the blur node size to the outputs size. Assumes there's only one
 792 	 * output. More advanced compositors should either implement per output blur
 793 	 * nodes or set it to the size of all outputs.
 794 	 */
 795 	wlr_scene_optimized_blur_set_size(output->server->layers.blur_layer,
 796 			output->wlr_output->width, output->wlr_output->height);
 797 }
 798 
 799 static void iter_xdg_scene_buffers(struct wlr_scene_buffer *buffer, int sx,
 800 								   int sy, void *user_data) {
 801 	struct tinywl_toplevel *toplevel = user_data;
 802 
 803 	struct wlr_scene_surface * scene_surface = wlr_scene_surface_try_from_buffer(buffer);
 804 	if (!scene_surface) {
 805 		return;
 806 	}
 807 
 808 	struct wlr_xdg_surface *xdg_surface =
 809 		wlr_xdg_surface_try_from_wlr_surface(scene_surface->surface);
 810 	if (toplevel &&
 811 			xdg_surface &&
 812 			xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
 813 		// TODO: Be able to set whole decoration_data instead of calling
 814 		// each individually?
 815 		wlr_scene_buffer_set_opacity(buffer, toplevel->opacity);
 816 
 817 		if (!wlr_subsurface_try_from_wlr_surface(xdg_surface->surface)) {
 818 			wlr_scene_buffer_set_corner_radii(buffer, corner_radii_bottom(toplevel->corner_radius));
 819 
 820 			wlr_scene_blur_set_transparency_mask_source(toplevel->blur, buffer);
 821 		}
 822 	}
 823 }
 824 
 825 static void xdg_toplevel_map(struct wl_listener *listener, void *data) {
 826 	/* Called when the surface is mapped, or ready to display on-screen. */
 827 	struct tinywl_toplevel *toplevel = wl_container_of(listener, toplevel, map);
 828 
 829 	wlr_scene_node_set_enabled(&toplevel->scene_tree->node, true);
 830 
 831 	wlr_scene_node_for_each_buffer(&toplevel->scene_tree->node,
 832 								   iter_xdg_scene_buffers, toplevel);
 833 
 834 	wl_list_insert(&toplevel->server->toplevels, &toplevel->link);
 835 
 836 	focus_toplevel(toplevel);
 837 }
 838 
 839 static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) {
 840 	/* Called when the surface is unmapped, and should no longer be shown. */
 841 	struct tinywl_toplevel *toplevel = wl_container_of(listener, toplevel, unmap);
 842 
 843 	wlr_scene_node_set_enabled(&toplevel->scene_tree->node, false);
 844 
 845 	/* Reset the cursor mode if the grabbed toplevel was unmapped. */
 846 	if (toplevel == toplevel->server->grabbed_toplevel) {
 847 		reset_cursor_mode(toplevel->server);
 848 	}
 849 
 850 	wl_list_remove(&toplevel->link);
 851 }
 852 
 853 static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) {
 854 	/* Called when the xdg_toplevel is destroyed. */
 855 	struct tinywl_toplevel *toplevel = wl_container_of(listener, toplevel, destroy);
 856 
 857 	wl_list_remove(&toplevel->map.link);
 858 	wl_list_remove(&toplevel->unmap.link);
 859 	wl_list_remove(&toplevel->commit.link);
 860 	wl_list_remove(&toplevel->destroy.link);
 861 	wl_list_remove(&toplevel->request_move.link);
 862 	wl_list_remove(&toplevel->request_resize.link);
 863 	wl_list_remove(&toplevel->request_maximize.link);
 864 	wl_list_remove(&toplevel->request_fullscreen.link);
 865 
 866 	wlr_scene_node_destroy(&toplevel->scene_tree->node);
 867 
 868 	free(toplevel);
 869 }
 870 
 871 static void begin_interactive(struct tinywl_toplevel *toplevel,
 872 		enum tinywl_cursor_mode mode, uint32_t edges) {
 873 	/* This function sets up an interactive move or resize operation, where the
 874 	 * compositor stops propegating pointer events to clients and instead
 875 	 * consumes them itself, to move or resize windows. */
 876 	struct tinywl_server *server = toplevel->server;
 877 
 878 	server->grabbed_toplevel = toplevel;
 879 	server->cursor_mode = mode;
 880 
 881 	if (mode == TINYWL_CURSOR_MOVE) {
 882 		server->grab_x = server->cursor->x - toplevel->scene_tree->node.x;
 883 		server->grab_y = server->cursor->y - toplevel->scene_tree->node.y;
 884 	} else {
 885 		struct wlr_box *geo_box = &toplevel->xdg_toplevel->base->geometry;
 886 
 887 		double border_x = (toplevel->scene_tree->node.x + geo_box->x) +
 888 			((edges & WLR_EDGE_RIGHT) ? geo_box->width : 0);
 889 		double border_y = (toplevel->scene_tree->node.y + geo_box->y) +
 890 			((edges & WLR_EDGE_BOTTOM) ? geo_box->height : 0);
 891 		server->grab_x = server->cursor->x - border_x;
 892 		server->grab_y = server->cursor->y - border_y;
 893 
 894 		server->grab_geobox = *geo_box;
 895 		server->grab_geobox.x += toplevel->scene_tree->node.x;
 896 		server->grab_geobox.y += toplevel->scene_tree->node.y;
 897 
 898 		server->resize_edges = edges;
 899 	}
 900 }
 901 
 902 static void xdg_toplevel_request_move(
 903 		struct wl_listener *listener, void *data) {
 904 	/* This event is raised when a client would like to begin an interactive
 905 	 * move, typically because the user clicked on their client-side
 906 	 * decorations. Note that a more sophisticated compositor should check the
 907 	 * provided serial against a list of button press serials sent to this
 908 	 * client, to prevent the client from requesting this whenever they want. */
 909 	struct tinywl_toplevel *toplevel = wl_container_of(listener, toplevel, request_move);
 910 	begin_interactive(toplevel, TINYWL_CURSOR_MOVE, 0);
 911 }
 912 
 913 static void xdg_toplevel_request_resize(
 914 		struct wl_listener *listener, void *data) {
 915 	/* This event is raised when a client would like to begin an interactive
 916 	 * resize, typically because the user clicked on their client-side
 917 	 * decorations. Note that a more sophisticated compositor should check the
 918 	 * provided serial against a list of button press serials sent to this
 919 	 * client, to prevent the client from requesting this whenever they want. */
 920 	struct wlr_xdg_toplevel_resize_event *event = data;
 921 	struct tinywl_toplevel *toplevel = wl_container_of(listener, toplevel, request_resize);
 922 	begin_interactive(toplevel, TINYWL_CURSOR_RESIZE, event->edges);
 923 }
 924 
 925 static void xdg_toplevel_request_maximize(
 926 		struct wl_listener *listener, void *data) {
 927 	/* This event is raised when a client would like to maximize itself,
 928 	 * typically because the user clicked on the maximize button on client-side
 929 	 * decorations. tinywl doesn't support maximization, but to conform to
 930 	 * xdg-shell protocol we still must send a configure.
 931 	 * wlr_xdg_surface_schedule_configure() is used to send an empty reply.
 932 	 * However, if the request was sent before an initial commit, we don't do
 933 	 * anything and let the client finish the initial surface setup. */
 934 	struct tinywl_toplevel *toplevel =
 935 		wl_container_of(listener, toplevel, request_maximize);
 936 	if (toplevel->xdg_toplevel->base->initialized) {
 937 		wlr_xdg_surface_schedule_configure(toplevel->xdg_toplevel->base);
 938 	}
 939 }
 940 
 941 static void xdg_toplevel_request_fullscreen(
 942 		struct wl_listener *listener, void *data) {
 943 	/* Just as with request_maximize, we must send a configure here. */
 944 	struct tinywl_toplevel *toplevel =
 945 		wl_container_of(listener, toplevel, request_fullscreen);
 946 	if (toplevel->xdg_toplevel->base->initialized) {
 947 		wlr_xdg_surface_schedule_configure(toplevel->xdg_toplevel->base);
 948 	}
 949 }
 950 
 951 static void server_new_xdg_toplevel(struct wl_listener *listener, void *data) {
 952 	/* This event is raised when a client creates a new toplevel (application window). */
 953 	struct tinywl_server *server = wl_container_of(listener, server, new_xdg_toplevel);
 954 	struct wlr_xdg_toplevel *xdg_toplevel = data;
 955 
 956 	/* Allocate a tinywl_toplevel for this surface */
 957 	struct tinywl_toplevel *toplevel = calloc(1, sizeof(*toplevel));
 958 	toplevel->server = server;
 959 	toplevel->xdg_toplevel = xdg_toplevel;
 960 	toplevel->scene_tree = wlr_scene_tree_create(toplevel->server->layers.toplevel_layer);
 961 	toplevel->xdg_scene_tree = wlr_scene_xdg_surface_create(
 962 			toplevel->scene_tree, xdg_toplevel->base);
 963 	toplevel->scene_tree->node.data = toplevel;
 964 	xdg_toplevel->base->data = toplevel->scene_tree;
 965 
 966 	/* Set the scene_nodes decoration data */
 967 	toplevel->opacity = 1;
 968 	toplevel->corner_radius = 20;
 969 
 970 	toplevel->blur = wlr_scene_blur_create(toplevel->scene_tree, 0, 0);
 971 	wlr_scene_blur_set_should_only_blur_bottom_layer(toplevel->blur, true);
 972 
 973 	toplevel->border = wlr_scene_rect_create(toplevel->scene_tree, 0, 0,
 974 			(float[4]){ 1.0f, 0.f, 0.f, 1.0f });
 975 	wlr_scene_rect_set_corner_radii(toplevel->border,
 976 			corner_radii_bottom(toplevel->corner_radius + BORDER_THICKNESS));
 977 	wlr_scene_node_set_position(&toplevel->border->node, -BORDER_THICKNESS, -BORDER_THICKNESS);
 978 
 979 	float blur_sigma = 20.0f;
 980 	toplevel->shadow = wlr_scene_shadow_create(toplevel->scene_tree,
 981 			0, 0, toplevel->corner_radius, blur_sigma, (float[4]){ 0.f, 1.0f, 0.f, 1.0f });
 982 	wlr_scene_node_set_position(&toplevel->shadow->node, -BORDER_THICKNESS - blur_sigma,
 983 			-BORDER_THICKNESS - blur_sigma);
 984 
 985 	// Lower the border below the XDG scene tree
 986 	wlr_scene_node_lower_to_bottom(&toplevel->border->node);
 987 	// Lower the shadow below the border
 988 	wlr_scene_node_lower_to_bottom(&toplevel->shadow->node);
 989 	// Lower the blur below the shadow
 990 	wlr_scene_node_lower_to_bottom(&toplevel->blur->node);
 991 
 992 	/* Listen to the various events it can emit */
 993 	toplevel->map.notify = xdg_toplevel_map;
 994 	wl_signal_add(&xdg_toplevel->base->surface->events.map, &toplevel->map);
 995 	toplevel->unmap.notify = xdg_toplevel_unmap;
 996 	wl_signal_add(&xdg_toplevel->base->surface->events.unmap, &toplevel->unmap);
 997 	toplevel->commit.notify = xdg_toplevel_commit;
 998 	wl_signal_add(&xdg_toplevel->base->surface->events.commit, &toplevel->commit);
 999 	toplevel->destroy.notify = xdg_toplevel_destroy;
1000 	wl_signal_add(&xdg_toplevel->events.destroy, &toplevel->destroy);
1001 
1002 	/* cotd */
1003 	toplevel->request_move.notify = xdg_toplevel_request_move;
1004 	wl_signal_add(&xdg_toplevel->events.request_move, &toplevel->request_move);
1005 	toplevel->request_resize.notify = xdg_toplevel_request_resize;
1006 	wl_signal_add(&xdg_toplevel->events.request_resize, &toplevel->request_resize);
1007 	toplevel->request_maximize.notify = xdg_toplevel_request_maximize;
1008 	wl_signal_add(&xdg_toplevel->events.request_maximize, &toplevel->request_maximize);
1009 	toplevel->request_fullscreen.notify = xdg_toplevel_request_fullscreen;
1010 	wl_signal_add(&xdg_toplevel->events.request_fullscreen, &toplevel->request_fullscreen);
1011 }
1012 
1013 static void xdg_popup_commit(struct wl_listener *listener, void *data) {
1014 	/* Called when a new surface state is committed. */
1015 	struct tinywl_popup *popup = wl_container_of(listener, popup, commit);
1016 
1017 	if (popup->xdg_popup->base->initial_commit) {
1018 		/* When an xdg_surface performs an initial commit, the compositor must
1019 		 * reply with a configure so the client can map the surface.
1020 		 * tinywl sends an empty configure. A more sophisticated compositor
1021 		 * might change an xdg_popup's geometry to ensure it's not positioned
1022 		 * off-screen, for example. */
1023 		wlr_xdg_surface_schedule_configure(popup->xdg_popup->base);
1024 	}
1025 }
1026 
1027 static void xdg_popup_destroy(struct wl_listener *listener, void *data) {
1028 	/* Called when the xdg_popup is destroyed. */
1029 	struct tinywl_popup *popup = wl_container_of(listener, popup, destroy);
1030 
1031 	wl_list_remove(&popup->commit.link);
1032 	wl_list_remove(&popup->destroy.link);
1033 
1034 	free(popup);
1035 }
1036 
1037 static void server_new_xdg_popup(struct wl_listener *listener, void *data) {
1038 	/* This event is raised when a client creates a new popup. */
1039 	struct wlr_xdg_popup *xdg_popup = data;
1040 
1041 	struct tinywl_popup *popup = calloc(1, sizeof(*popup));
1042 	popup->xdg_popup = xdg_popup;
1043 
1044 	/* We must add xdg popups to the scene graph so they get rendered. The
1045 	 * wlroots scene graph provides a helper for this, but to use it we must
1046 	 * provide the proper parent scene node of the xdg popup. To enable this,
1047 	 * we always set the user data field of xdg_surfaces to the corresponding
1048 	 * scene node. */
1049 	struct wlr_xdg_surface *parent = wlr_xdg_surface_try_from_wlr_surface(xdg_popup->parent);
1050 	assert(parent != NULL);
1051 	struct wlr_scene_tree *parent_tree = parent->data;
1052 	xdg_popup->base->data = wlr_scene_xdg_surface_create(parent_tree, xdg_popup->base);
1053 
1054 	popup->commit.notify = xdg_popup_commit;
1055 	wl_signal_add(&xdg_popup->base->surface->events.commit, &popup->commit);
1056 
1057 	popup->destroy.notify = xdg_popup_destroy;
1058 	wl_signal_add(&xdg_popup->events.destroy, &popup->destroy);
1059 }
1060 
1061 int main(int argc, char *argv[]) {
1062 	wlr_log_init(WLR_DEBUG, NULL);
1063 	char *startup_cmd = NULL;
1064 
1065 	int c;
1066 	while ((c = getopt(argc, argv, "s:h")) != -1) {
1067 		switch (c) {
1068 		case 's':
1069 			startup_cmd = optarg;
1070 			break;
1071 		default:
1072 			printf("Usage: %s [-s startup command]\n", argv[0]);
1073 			return 0;
1074 		}
1075 	}
1076 	if (optind < argc) {
1077 		printf("Usage: %s [-s startup command]\n", argv[0]);
1078 		return 0;
1079 	}
1080 
1081 	struct tinywl_server server = {0};
1082 	/* The Wayland display is managed by libwayland. It handles accepting
1083 	 * clients from the Unix socket, managing Wayland globals, and so on. */
1084 	server.wl_display = wl_display_create();
1085 	/* The backend is a wlroots feature which abstracts the underlying input and
1086 	 * output hardware. The autocreate option will choose the most suitable
1087 	 * backend based on the current environment, such as opening an X11 window
1088 	 * if an X11 server is running. */
1089 	server.backend = wlr_backend_autocreate(wl_display_get_event_loop(server.wl_display), NULL);
1090 	if (server.backend == NULL) {
1091 		wlr_log(WLR_ERROR, "failed to create wlr_backend");
1092 		return 1;
1093 	}
1094 
1095 	/* Autocreates a renderer, either Pixman, GLES2 or Vulkan for us. The user
1096 	 * can also specify a renderer using the WLR_RENDERER env var.
1097 	 * The renderer is responsible for defining the various pixel formats it
1098 	 * supports for shared memory, this configures that for clients. */
1099 	server.renderer = fx_renderer_create(server.backend);
1100 	if (server.renderer == NULL) {
1101 		wlr_log(WLR_ERROR, "failed to create wlr_renderer");
1102 		return 1;
1103 	}
1104 
1105 	wlr_renderer_init_wl_display(server.renderer, server.wl_display);
1106 
1107 	/* Autocreates an allocator for us.
1108 	 * The allocator is the bridge between the renderer and the backend. It
1109 	 * handles the buffer creation, allowing wlroots to render onto the
1110 	 * screen */
1111 	server.allocator = wlr_allocator_autocreate(server.backend,
1112 		server.renderer);
1113 	if (server.allocator == NULL) {
1114 		wlr_log(WLR_ERROR, "failed to create wlr_allocator");
1115 		return 1;
1116 	}
1117 
1118 	/* This creates some hands-off wlroots interfaces. The compositor is
1119 	 * necessary for clients to allocate surfaces, the subcompositor allows to
1120 	 * assign the role of subsurfaces to surfaces and the data device manager
1121 	 * handles the clipboard. Each of these wlroots interfaces has room for you
1122 	 * to dig your fingers in and play with their behavior if you want. Note that
1123 	 * the clients cannot set the selection directly without compositor approval,
1124 	 * see the handling of the request_set_selection event below.*/
1125 	wlr_compositor_create(server.wl_display, 5, server.renderer);
1126 	wlr_subcompositor_create(server.wl_display);
1127 	wlr_data_device_manager_create(server.wl_display);
1128 
1129 	/* Creates an output layout, which a wlroots utility for working with an
1130 	 * arrangement of screens in a physical layout. */
1131 	server.output_layout = wlr_output_layout_create(server.wl_display);
1132 
1133 	/* Configure a listener to be notified when new outputs are available on the
1134 	 * backend. */
1135 	wl_list_init(&server.outputs);
1136 	server.new_output.notify = server_new_output;
1137 	wl_signal_add(&server.backend->events.new_output, &server.new_output);
1138 
1139 	/* Create a scene graph. This is a wlroots abstraction that handles all
1140 	 * rendering and damage tracking. All the compositor author needs to do
1141 	 * is add things that should be rendered to the scene graph at the proper
1142 	 * positions and then call wlr_scene_output_commit() to render a frame if
1143 	 * necessary.
1144 	 */
1145 	server.scene = wlr_scene_create();
1146 	server.scene_layout = wlr_scene_attach_output_layout(server.scene, server.output_layout);
1147 
1148 	/* Create all of the basic scene layers */
1149 	server.layers.bottom_layer = wlr_scene_tree_create(&server.scene->tree);
1150 	/* Add a bottom rect to demonstrate optimized blur */
1151 	float bottom_rect_color[4] = { 1, 1, 1, 1 };
1152 	wlr_scene_rect_create(server.layers.bottom_layer, 200, 200, bottom_rect_color);
1153 	/* Set the size later */
1154 	server.layers.blur_layer = wlr_scene_optimized_blur_create(&server.scene->tree, 0, 0);
1155 	server.layers.toplevel_layer = wlr_scene_tree_create(&server.scene->tree);
1156 	/* Add a top rect that won't get blurred by optimized */
1157 	float top_rect_color[4] = { 1, 0, 0, 1 };
1158 	struct wlr_scene_rect *rect = wlr_scene_rect_create(server.layers.toplevel_layer,
1159 			200, 200, top_rect_color);
1160 	wlr_scene_rect_set_clipped_region(rect, (struct clipped_region) {
1161 			.corners = {12, 12, 0, 0},
1162 			.area = {
1163 				.x = 50,
1164 				.y = 50,
1165 				.width = 100,
1166 				.height = 100,
1167 			},
1168 	});
1169 	wlr_scene_node_set_position(&rect->node, 200, 200);
1170 
1171 	/* Set up xdg-shell version 3. The xdg-shell is a Wayland protocol which is
1172 	 * used for application windows. For more detail on shells, refer to
1173 	 * https://drewdevault.com/2018/07/29/Wayland-shells.html.
1174 	 */
1175 	wl_list_init(&server.toplevels);
1176 	server.xdg_shell = wlr_xdg_shell_create(server.wl_display, 3);
1177 	server.new_xdg_toplevel.notify = server_new_xdg_toplevel;
1178 	wl_signal_add(&server.xdg_shell->events.new_toplevel, &server.new_xdg_toplevel);
1179 	server.new_xdg_popup.notify = server_new_xdg_popup;
1180 	wl_signal_add(&server.xdg_shell->events.new_popup, &server.new_xdg_popup);
1181 
1182 	/*
1183 	 * Creates a cursor, which is a wlroots utility for tracking the cursor
1184 	 * image shown on screen.
1185 	 */
1186 	server.cursor = wlr_cursor_create();
1187 	wlr_cursor_attach_output_layout(server.cursor, server.output_layout);
1188 
1189 	/* Creates an xcursor manager, another wlroots utility which loads up
1190 	 * Xcursor themes to source cursor images from and makes sure that cursor
1191 	 * images are available at all scale factors on the screen (necessary for
1192 	 * HiDPI support). */
1193 	server.cursor_mgr = wlr_xcursor_manager_create(NULL, 24);
1194 
1195 	/*
1196 	 * wlr_cursor *only* displays an image on screen. It does not move around
1197 	 * when the pointer moves. However, we can attach input devices to it, and
1198 	 * it will generate aggregate events for all of them. In these events, we
1199 	 * can choose how we want to process them, forwarding them to clients and
1200 	 * moving the cursor around. More detail on this process is described in
1201 	 * https://drewdevault.com/2018/07/17/Input-handling-in-wlroots.html.
1202 	 *
1203 	 * And more comments are sprinkled throughout the notify functions above.
1204 	 */
1205 	server.cursor_mode = TINYWL_CURSOR_PASSTHROUGH;
1206 	server.cursor_motion.notify = server_cursor_motion;
1207 	wl_signal_add(&server.cursor->events.motion, &server.cursor_motion);
1208 	server.cursor_motion_absolute.notify = server_cursor_motion_absolute;
1209 	wl_signal_add(&server.cursor->events.motion_absolute,
1210 			&server.cursor_motion_absolute);
1211 	server.cursor_button.notify = server_cursor_button;
1212 	wl_signal_add(&server.cursor->events.button, &server.cursor_button);
1213 	server.cursor_axis.notify = server_cursor_axis;
1214 	wl_signal_add(&server.cursor->events.axis, &server.cursor_axis);
1215 	server.cursor_frame.notify = server_cursor_frame;
1216 	wl_signal_add(&server.cursor->events.frame, &server.cursor_frame);
1217 
1218 	/*
1219 	 * Configures a seat, which is a single "seat" at which a user sits and
1220 	 * operates the computer. This conceptually includes up to one keyboard,
1221 	 * pointer, touch, and drawing tablet device. We also rig up a listener to
1222 	 * let us know when new input devices are available on the backend.
1223 	 */
1224 	wl_list_init(&server.keyboards);
1225 	server.new_input.notify = server_new_input;
1226 	wl_signal_add(&server.backend->events.new_input, &server.new_input);
1227 	server.seat = wlr_seat_create(server.wl_display, "seat0");
1228 	server.request_cursor.notify = seat_request_cursor;
1229 	wl_signal_add(&server.seat->events.request_set_cursor,
1230 			&server.request_cursor);
1231 	server.pointer_focus_change.notify = seat_pointer_focus_change;
1232 	wl_signal_add(&server.seat->pointer_state.events.focus_change,
1233 			&server.pointer_focus_change);
1234 	server.request_set_selection.notify = seat_request_set_selection;
1235 	wl_signal_add(&server.seat->events.request_set_selection,
1236 			&server.request_set_selection);
1237 
1238 	/* Add a Unix socket to the Wayland display. */
1239 	const char *socket = wl_display_add_socket_auto(server.wl_display);
1240 	if (!socket) {
1241 		wlr_backend_destroy(server.backend);
1242 		return 1;
1243 	}
1244 
1245 	/* Start the backend. This will enumerate outputs and inputs, become the DRM
1246 	 * master, etc */
1247 	if (!wlr_backend_start(server.backend)) {
1248 		wlr_backend_destroy(server.backend);
1249 		wl_display_destroy(server.wl_display);
1250 		return 1;
1251 	}
1252 
1253 	/* Set the WAYLAND_DISPLAY environment variable to our socket and run the
1254 	 * startup command if requested. */
1255 	setenv("WAYLAND_DISPLAY", socket, true);
1256 	if (startup_cmd) {
1257 		if (fork() == 0) {
1258 			execl("/bin/sh", "/bin/sh", "-c", startup_cmd, (void *)NULL);
1259 		}
1260 	}
1261 	/* Run the Wayland event loop. This does not return until you exit the
1262 	 * compositor. Starting the backend rigged up all of the necessary event
1263 	 * loop configuration to listen to libinput events, DRM events, generate
1264 	 * frame events at the refresh rate, and so on. */
1265 	wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s",
1266 			socket);
1267 	wl_display_run(server.wl_display);
1268 
1269 
1270 	/* Once wl_display_run returns, we destroy all clients then shut down the
1271 	 * server. */
1272 	wl_display_destroy_clients(server.wl_display);
1273 
1274 	wl_list_remove(&server.new_xdg_toplevel.link);
1275 	wl_list_remove(&server.new_xdg_popup.link);
1276 
1277 	wl_list_remove(&server.cursor_motion.link);
1278 	wl_list_remove(&server.cursor_motion_absolute.link);
1279 	wl_list_remove(&server.cursor_button.link);
1280 	wl_list_remove(&server.cursor_axis.link);
1281 	wl_list_remove(&server.cursor_frame.link);
1282 
1283 	wl_list_remove(&server.new_input.link);
1284 	wl_list_remove(&server.request_cursor.link);
1285 	wl_list_remove(&server.pointer_focus_change.link);
1286 	wl_list_remove(&server.request_set_selection.link);
1287 
1288 	wl_list_remove(&server.new_output.link);
1289 
1290 	wlr_scene_node_destroy(&server.scene->tree.node);
1291 	wlr_xcursor_manager_destroy(server.cursor_mgr);
1292 	wlr_cursor_destroy(server.cursor);
1293 	wlr_allocator_destroy(server.allocator);
1294 	wlr_renderer_destroy(server.renderer);
1295 	wlr_backend_destroy(server.backend);
1296 	wl_display_destroy(server.wl_display);
1297 	return 0;
1298 }