neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

nested.c (31841B)


      1 /* nested backend: swc as a client of another compositor.
      2  * one xdg_toplevel is the screen, the parent seat feeds ours */
      3 
      4 #include "nested.h"
      5 #include "backend.h"
      6 #include "compositor.h"
      7 #include "internal.h"
      8 #include "keyboard.h"
      9 #include "launch/protocol.h"
     10 #include "output.h"
     11 #include "pointer.h"
     12 #include "screen.h"
     13 #include "seat.h"
     14 #include "swc.h"
     15 #include "util.h"
     16 #include "view.h"
     17 
     18 #include <errno.h>
     19 #include <limits.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <string.h>
     23 #include <strings.h>
     24 #include <sys/mman.h>
     25 #include <unistd.h>
     26 #include <wayland-client.h>
     27 #include <wld/wayland.h>
     28 #include <wld/wld.h>
     29 #include <xkbcommon/xkbcommon.h>
     30 
     31 #include "cursor-shape-v1-client-protocol.h"
     32 #include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
     33 #include "xdg-shell-client-protocol.h"
     34 
     35 #define NESTED_DEFAULT_WIDTH 1280
     36 #define NESTED_DEFAULT_HEIGHT 720
     37 #define NESTED_CURSOR_SIZE 64
     38 #define NESTED_OUTPUT_NAME "NESTED-1"
     39 #define NESTED_TITLE "swc"
     40 #define NESTED_CAPTURE_KEY "ctrl+alt+g"
     41 #define NESTED_FULLSCREEN_KEY "ctrl+alt+f"
     42 
     43 /* wl_surface.damage_buffer and wl_pointer.frame first appear here */
     44 #define COMPOSITOR_VERSION 4
     45 #define SEAT_VERSION 5
     46 #define OUTPUT_VERSION 2
     47 
     48 /* a key the parent window keeps for itself, never passed to clients */
     49 struct chord {
     50 	xkb_keysym_t keysym;
     51 	uint32_t modifiers;
     52 	char description[64];
     53 };
     54 
     55 static struct {
     56 	struct swc_backend backend;
     57 
     58 	struct wl_display *display;
     59 	struct wl_registry *registry;
     60 	struct wl_compositor *compositor;
     61 	uint32_t compositor_version;
     62 	struct xdg_wm_base *wm_base;
     63 	struct wl_output *output;
     64 	struct wl_seat *seat;
     65 	uint32_t seat_version;
     66 	struct wl_keyboard *keyboard;
     67 	struct wl_pointer *pointer;
     68 	struct zwp_keyboard_shortcuts_inhibit_manager_v1 *inhibit_manager;
     69 	struct zwp_keyboard_shortcuts_inhibitor_v1 *inhibitor;
     70 	struct wp_cursor_shape_manager_v1 *shape_manager;
     71 	struct wp_cursor_shape_device_v1 *shape_device;
     72 	struct wl_event_source *source;
     73 
     74 	struct wl_surface *surface;
     75 	struct xdg_surface *xdg_surface;
     76 	struct xdg_toplevel *toplevel;
     77 	struct wl_callback *frame;
     78 	struct screen *screen;
     79 	bool configured;
     80 	bool captured;
     81 	bool fullscreen;
     82 
     83 	/* the parent talks in logical units, swc in buffer pixels. every
     84 	 * size and coordinate crossing this file is converted by scale */
     85 	int32_t scale;
     86 	uint32_t width, height;
     87 	int32_t pending_width, pending_height;
     88 
     89 	struct {
     90 		struct wl_surface *surface;
     91 		struct wld_buffer *buffer;
     92 		int32_t hotspot_x, hotspot_y;
     93 		uint32_t enter_serial;
     94 		bool inside;
     95 		/* where the parent's pointer is, kept while released so a
     96 		 * capture puts ours there */
     97 		wl_fixed_t x, y;
     98 		/* the click that captured, its release goes nowhere either */
     99 		uint32_t swallowed;
    100 	} cursor;
    101 
    102 	struct {
    103 		enum wl_pointer_axis_source source;
    104 		int32_t discrete[2];
    105 	} axis;
    106 
    107 	/* parent keymap that arrived before the swc seat existed */
    108 	struct xkb_keymap *keymap;
    109 
    110 	struct chord capture_key, fullscreen_key;
    111 
    112 	/* the parent's modifier state, kept here while released so the nested
    113 	 * clients see none of it, and handed to swc on capture */
    114 	struct {
    115 		uint32_t depressed, latched, locked, group;
    116 	} mods;
    117 } nested;
    118 
    119 bool
    120 nested_requested(void)
    121 {
    122 	const char *backend = getenv("SWC_BACKEND");
    123 
    124 	if (backend) {
    125 		return strcmp(backend, "nested") == 0;
    126 	}
    127 	if (getenv(SWC_LAUNCH_SOCKET_ENV)) {
    128 		return false;
    129 	}
    130 	return getenv("WAYLAND_DISPLAY") != NULL;
    131 }
    132 
    133 static int32_t
    134 env_int(const char *name, int32_t fallback)
    135 {
    136 	const char *value = getenv(name);
    137 	long number;
    138 	char *end;
    139 
    140 	if (!value || !*value) {
    141 		return fallback;
    142 	}
    143 	number = strtol(value, &end, 10);
    144 	return *end == '\0' && number > 0 && number <= INT32_MAX
    145 	           ? (int32_t)number
    146 	           : fallback;
    147 }
    148 
    149 /* primary plane */
    150 
    151 static void
    152 handle_frame_done(void *data, struct wl_callback *callback, uint32_t time)
    153 {
    154 	wl_callback_destroy(callback);
    155 	nested.frame = NULL;
    156 	/* the clock of the parent is not ours, and clients compare this
    157 	 * against the timestamps swc puts on input events */
    158 	view_frame(&nested.screen->planes.primary.view, get_time());
    159 }
    160 
    161 static const struct wl_callback_listener frame_listener = {
    162     .done = handle_frame_done,
    163 };
    164 
    165 static bool
    166 plane_update(struct view *view)
    167 {
    168 	return true;
    169 }
    170 
    171 static int
    172 plane_attach(struct view *view, struct wld_buffer *buffer)
    173 {
    174 	union wld_object object;
    175 
    176 	if (!buffer) {
    177 		return 0;
    178 	}
    179 	if (!wld_export(buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) {
    180 		ERROR("nested: could not export the screen buffer\n");
    181 		return -1;
    182 	}
    183 
    184 	if (nested.frame) {
    185 		wl_callback_destroy(nested.frame);
    186 	}
    187 	nested.frame = wl_surface_frame(nested.surface);
    188 	wl_callback_add_listener(nested.frame, &frame_listener, NULL);
    189 
    190 	wl_surface_set_buffer_scale(nested.surface, nested.scale);
    191 	wl_surface_attach(nested.surface, object.ptr, 0, 0);
    192 	if (nested.compositor_version >= COMPOSITOR_VERSION) {
    193 		wl_surface_damage_buffer(nested.surface, 0, 0, INT32_MAX, INT32_MAX);
    194 	} else {
    195 		wl_surface_damage(nested.surface, 0, 0, INT32_MAX, INT32_MAX);
    196 	}
    197 	wl_surface_commit(nested.surface);
    198 
    199 	return 0;
    200 }
    201 
    202 static bool
    203 plane_move(struct view *view, int32_t x, int32_t y)
    204 {
    205 	view_set_position(view, x, y);
    206 	return true;
    207 }
    208 
    209 static const struct view_impl plane_impl = {
    210     .update = plane_update,
    211     .attach = plane_attach,
    212     .move = plane_move,
    213 };
    214 
    215 static struct wld_surface *
    216 create_target(struct screen *screen)
    217 {
    218 	const struct swc_rectangle *geometry = &screen->base.geometry;
    219 
    220 	return wld_wayland_create_surface(nested.backend.context, geometry->width,
    221 	                                  geometry->height, WLD_FORMAT_XRGB8888,
    222 	                                  WLD_FLAG_MAP, nested.surface);
    223 }
    224 
    225 /* cursor: show our cursor image as the parent cursor, no lag, no second cursor */
    226 
    227 static void
    228 cursor_commit(void)
    229 {
    230 	union wld_object object;
    231 
    232 	if (!nested.pointer || !nested.cursor.inside) {
    233 		return;
    234 	}
    235 	/* released, the window is the parent's like any other and shows the
    236 	 * parent's own cursor. a parent without cursor-shape gets ours */
    237 	if (!nested.captured && nested.shape_device) {
    238 		wp_cursor_shape_device_v1_set_shape(nested.shape_device, nested.cursor.enter_serial,
    239 		                                    WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
    240 		return;
    241 	}
    242 	if (!nested.cursor.buffer ||
    243 	    !wld_export(nested.cursor.buffer, WLD_WAYLAND_OBJECT_BUFFER, &object)) {
    244 		wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial, NULL,
    245 		                      0, 0);
    246 		return;
    247 	}
    248 
    249 	wl_surface_set_buffer_scale(nested.cursor.surface, nested.scale);
    250 	wl_surface_attach(nested.cursor.surface, object.ptr, 0, 0);
    251 	wl_surface_damage(nested.cursor.surface, 0, 0, INT32_MAX, INT32_MAX);
    252 	wl_surface_commit(nested.cursor.surface);
    253 	wl_pointer_set_cursor(nested.pointer, nested.cursor.enter_serial,
    254 	                      nested.cursor.surface,
    255 	                      nested.cursor.hotspot_x / nested.scale,
    256 	                      nested.cursor.hotspot_y / nested.scale);
    257 }
    258 
    259 static void
    260 set_cursor(struct wld_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y)
    261 {
    262 	nested.cursor.buffer = buffer;
    263 	nested.cursor.hotspot_x = hotspot_x;
    264 	nested.cursor.hotspot_y = hotspot_y;
    265 	cursor_commit();
    266 }
    267 
    268 /* input capture like a vm window: a click or the chord captures,
    269  * the chord releases. while captured the parent stops using its own
    270  * shortcuts. a parent without the inhibit protocol keeps them */
    271 
    272 static void
    273 update_title(void)
    274 {
    275 	char title[2 * sizeof(nested.capture_key.description) + 64];
    276 
    277 	snprintf(title, sizeof(title), NESTED_TITLE " (%s %s input, %s fullscreen)",
    278 	         nested.capture_key.description,
    279 	         nested.captured ? "releases" : "captures",
    280 	         nested.fullscreen_key.description);
    281 	xdg_toplevel_set_title(nested.toplevel, title);
    282 }
    283 
    284 static void
    285 capture(void)
    286 {
    287 	if (nested.captured) {
    288 		return;
    289 	}
    290 	nested.captured = true;
    291 	if (nested.inhibit_manager && nested.seat) {
    292 		nested.inhibitor =
    293 		    zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(
    294 		        nested.inhibit_manager, nested.surface, nested.seat);
    295 	}
    296 	if (swc.seat) {
    297 		keyboard_handle_modifiers(swc.seat->keyboard, nested.mods.depressed,
    298 		                          nested.mods.latched, nested.mods.locked,
    299 		                          nested.mods.group);
    300 	}
    301 	if (swc.seat && nested.cursor.inside) {
    302 		pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
    303 		                               nested.cursor.x * nested.scale,
    304 		                               nested.cursor.y * nested.scale);
    305 		pointer_handle_frame(swc.seat->pointer);
    306 	}
    307 	cursor_commit();
    308 	update_title();
    309 }
    310 
    311 static void
    312 release(void)
    313 {
    314 	if (!nested.captured) {
    315 		return;
    316 	}
    317 	nested.captured = false;
    318 	if (nested.inhibitor) {
    319 		zwp_keyboard_shortcuts_inhibitor_v1_destroy(nested.inhibitor);
    320 		nested.inhibitor = NULL;
    321 	}
    322 	/* the chord that got us here is still held, and its release goes to
    323 	 * whatever the parent focuses next */
    324 	if (swc.seat) {
    325 		keyboard_reset(swc.seat->keyboard);
    326 	}
    327 	cursor_commit();
    328 	if (nested.toplevel) {
    329 		update_title();
    330 	}
    331 }
    332 
    333 static uint32_t
    334 modifier_from_name(const char *name)
    335 {
    336 	static const struct {
    337 		const char *name;
    338 		uint32_t modifier;
    339 	} names[] = {
    340 	    {"ctrl", SWC_MOD_CTRL},  {"control", SWC_MOD_CTRL},
    341 	    {"alt", SWC_MOD_ALT},    {"mod1", SWC_MOD_ALT},
    342 	    {"super", SWC_MOD_LOGO}, {"logo", SWC_MOD_LOGO},
    343 	    {"mod4", SWC_MOD_LOGO},  {"shift", SWC_MOD_SHIFT},
    344 	};
    345 	size_t i;
    346 
    347 	for (i = 0; i < ARRAY_LENGTH(names); i++) {
    348 		if (strcasecmp(name, names[i].name) == 0) {
    349 			return names[i].modifier;
    350 		}
    351 	}
    352 	return 0;
    353 }
    354 
    355 /* "ctrl+alt+g": all but the last token are modifiers.
    356  * the environment variable wins, "none" turns it off */
    357 static void
    358 parse_chord(struct chord *chord, const char *variable, const char *fallback)
    359 {
    360 	const char *spec = getenv(variable);
    361 	char buffer[sizeof(chord->description)];
    362 	char *token, *next, *save;
    363 
    364 	if (!spec || !*spec) {
    365 		spec = fallback;
    366 	}
    367 	snprintf(buffer, sizeof(buffer), "%s", spec);
    368 	snprintf(chord->description, sizeof(chord->description), "%s", spec);
    369 	chord->modifiers = 0;
    370 	chord->keysym = XKB_KEY_NoSymbol;
    371 
    372 	for (token = strtok_r(buffer, "+", &save); token; token = next) {
    373 		next = strtok_r(NULL, "+", &save);
    374 		if (next) {
    375 			chord->modifiers |= modifier_from_name(token);
    376 		} else {
    377 			chord->keysym =
    378 			    xkb_keysym_from_name(token, XKB_KEYSYM_CASE_INSENSITIVE);
    379 		}
    380 	}
    381 }
    382 
    383 /* match the keysym ignoring its level */
    384 static uint32_t
    385 held_modifiers(struct xkb_keymap *map)
    386 {
    387 	static const struct {
    388 		const char *name;
    389 		uint32_t mod;
    390 	} names[] = {
    391 	    {XKB_MOD_NAME_CTRL, SWC_MOD_CTRL},
    392 	    {XKB_MOD_NAME_ALT, SWC_MOD_ALT},
    393 	    {XKB_MOD_NAME_LOGO, SWC_MOD_LOGO},
    394 	    {XKB_MOD_NAME_SHIFT, SWC_MOD_SHIFT},
    395 	};
    396 	struct xkb_state *state = xkb_state_new(map);
    397 	uint32_t mods = 0;
    398 	size_t i;
    399 
    400 	if (!state) {
    401 		return 0;
    402 	}
    403 	xkb_state_update_mask(state, nested.mods.depressed, nested.mods.latched,
    404 	                      nested.mods.locked, 0, 0, nested.mods.group);
    405 	for (i = 0; i < ARRAY_LENGTH(names); i++) {
    406 		if (xkb_state_mod_name_is_active(state, names[i].name,
    407 		                                 XKB_STATE_MODS_EFFECTIVE) > 0) {
    408 			mods |= names[i].mod;
    409 		}
    410 	}
    411 	xkb_state_unref(state);
    412 	return mods;
    413 }
    414 
    415 /* match the keysym ignoring its level */
    416 static bool
    417 is_chord(const struct chord *chord, struct keyboard *keyboard, uint32_t key)
    418 {
    419 	struct xkb_keymap *map = keyboard->xkb.keymap.map;
    420 	const xkb_keysym_t *syms;
    421 
    422 	if (chord->keysym == XKB_KEY_NoSymbol ||
    423 	    (held_modifiers(map) & chord->modifiers) != chord->modifiers) {
    424 		return false;
    425 	}
    426 	if (xkb_keymap_key_get_syms_by_level(map, XKB_KEY(key), nested.mods.group, 0,
    427 	                                     &syms) < 1) {
    428 		return false;
    429 	}
    430 	return xkb_keysym_to_lower(syms[0]) == xkb_keysym_to_lower(chord->keysym);
    431 }
    432 
    433 static void
    434 toggle_fullscreen(void)
    435 {
    436 	if (nested.fullscreen) {
    437 		xdg_toplevel_unset_fullscreen(nested.toplevel);
    438 	} else {
    439 		xdg_toplevel_set_fullscreen(nested.toplevel, NULL);
    440 	}
    441 }
    442 
    443 /* keyboard */
    444 
    445 static void
    446 apply_keymap(void)
    447 {
    448 	if (!nested.keymap || !swc.seat) {
    449 		return;
    450 	}
    451 	if (!keyboard_set_keymap(swc.seat->keyboard, nested.keymap)) {
    452 		WARNING("nested: could not adopt the parent keymap\n");
    453 	}
    454 	xkb_keymap_unref(nested.keymap);
    455 	nested.keymap = NULL;
    456 }
    457 
    458 /* raw key codes mean the same only with the same keymap on both sides */
    459 static void
    460 handle_keyboard_keymap(void *data, struct wl_keyboard *keyboard,
    461                        uint32_t format, int32_t fd, uint32_t size)
    462 {
    463 	struct xkb_context *context;
    464 	struct xkb_keymap *keymap = NULL;
    465 	char *string;
    466 
    467 	if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
    468 		close(fd);
    469 		return;
    470 	}
    471 	string = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
    472 	close(fd);
    473 	if (string == MAP_FAILED) {
    474 		return;
    475 	}
    476 	if ((context = xkb_context_new(XKB_CONTEXT_NO_FLAGS))) {
    477 		keymap = xkb_keymap_new_from_buffer(context, string,
    478 		                                    strnlen(string, size),
    479 		                                    XKB_KEYMAP_FORMAT_TEXT_V1,
    480 		                                    XKB_KEYMAP_COMPILE_NO_FLAGS);
    481 		xkb_context_unref(context);
    482 	}
    483 	munmap(string, size);
    484 	if (!keymap) {
    485 		WARNING("nested: could not compile the parent keymap\n");
    486 		return;
    487 	}
    488 
    489 	if (nested.keymap) {
    490 		xkb_keymap_unref(nested.keymap);
    491 	}
    492 	nested.keymap = keymap;
    493 	apply_keymap();
    494 }
    495 
    496 static void
    497 handle_keyboard_enter(void *data, struct wl_keyboard *keyboard,
    498                       uint32_t serial, struct wl_surface *surface,
    499                       struct wl_array *keys)
    500 {
    501 }
    502 
    503 /* keys held when focus leaves are never released, so reset them */
    504 static void
    505 handle_keyboard_leave(void *data, struct wl_keyboard *keyboard,
    506                       uint32_t serial, struct wl_surface *surface)
    507 {
    508 	if (swc.seat) {
    509 		keyboard_reset(swc.seat->keyboard);
    510 	}
    511 }
    512 
    513 static void
    514 handle_keyboard_key(void *data, struct wl_keyboard *keyboard, uint32_t serial,
    515                     uint32_t time, uint32_t key, uint32_t state)
    516 {
    517 	if (!swc.seat) {
    518 		return;
    519 	}
    520 	if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
    521 		if (is_chord(&nested.capture_key, swc.seat->keyboard, key)) {
    522 			if (nested.captured) {
    523 				release();
    524 			} else {
    525 				capture();
    526 			}
    527 			return;
    528 		}
    529 		if (is_chord(&nested.fullscreen_key, swc.seat->keyboard, key)) {
    530 			toggle_fullscreen();
    531 			return;
    532 		}
    533 	}
    534 	/* released, keys are the parent's. the chords above still see the
    535 	 * modifiers, which keep being tracked */
    536 	if (!nested.captured) {
    537 		return;
    538 	}
    539 	keyboard_handle_key(swc.seat->keyboard, get_time(), key, state);
    540 }
    541 
    542 /* locks, layout group and modifiers held at focus in */
    543 static void
    544 handle_keyboard_modifiers(void *data, struct wl_keyboard *keyboard,
    545                           uint32_t serial, uint32_t depressed,
    546                           uint32_t latched, uint32_t locked, uint32_t group)
    547 {
    548 	nested.mods.depressed = depressed;
    549 	nested.mods.latched = latched;
    550 	nested.mods.locked = locked;
    551 	nested.mods.group = group;
    552 	if (swc.seat && nested.captured) {
    553 		keyboard_handle_modifiers(swc.seat->keyboard, depressed, latched,
    554 		                          locked, group);
    555 	}
    556 }
    557 
    558 static void
    559 handle_keyboard_repeat_info(void *data, struct wl_keyboard *keyboard,
    560                             int32_t rate, int32_t delay)
    561 {
    562 }
    563 
    564 static const struct wl_keyboard_listener keyboard_listener = {
    565     .keymap = handle_keyboard_keymap,
    566     .enter = handle_keyboard_enter,
    567     .leave = handle_keyboard_leave,
    568     .key = handle_keyboard_key,
    569     .modifiers = handle_keyboard_modifiers,
    570     .repeat_info = handle_keyboard_repeat_info,
    571 };
    572 
    573 /* pointer */
    574 
    575 /* seats older than SEAT_VERSION send no pointer.frame, clients wait for one */
    576 static void
    577 frame_legacy(void)
    578 {
    579 	if (nested.seat_version < SEAT_VERSION) {
    580 		pointer_handle_frame(swc.seat->pointer);
    581 	}
    582 }
    583 
    584 static void
    585 handle_pointer_enter(void *data, struct wl_pointer *pointer, uint32_t serial,
    586                      struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
    587 {
    588 	nested.cursor.inside = true;
    589 	nested.cursor.enter_serial = serial;
    590 	nested.cursor.x = x;
    591 	nested.cursor.y = y;
    592 	cursor_commit();
    593 	if (!swc.seat || !nested.captured) {
    594 		return;
    595 	}
    596 	pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
    597 	                               x * nested.scale, y * nested.scale);
    598 	frame_legacy();
    599 }
    600 
    601 static void
    602 handle_pointer_leave(void *data, struct wl_pointer *pointer, uint32_t serial,
    603                      struct wl_surface *surface)
    604 {
    605 	nested.cursor.inside = false;
    606 }
    607 
    608 static void
    609 handle_pointer_motion(void *data, struct wl_pointer *pointer, uint32_t time,
    610                       wl_fixed_t x, wl_fixed_t y)
    611 {
    612 	nested.cursor.x = x;
    613 	nested.cursor.y = y;
    614 	if (!swc.seat || !nested.captured) {
    615 		return;
    616 	}
    617 	pointer_handle_absolute_motion(swc.seat->pointer, get_time(),
    618 	                               x * nested.scale, y * nested.scale);
    619 	frame_legacy();
    620 }
    621 
    622 static void
    623 handle_pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial,
    624                       uint32_t time, uint32_t button, uint32_t state)
    625 {
    626 	if (!swc.seat) {
    627 		return;
    628 	}
    629 	/* released, a click only captures, as in a virtual machine window */
    630 	if (!nested.captured) {
    631 		if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
    632 			nested.cursor.swallowed = button;
    633 			capture();
    634 		}
    635 		return;
    636 	}
    637 	if (button == nested.cursor.swallowed) {
    638 		nested.cursor.swallowed = 0;
    639 		if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
    640 			return;
    641 		}
    642 	}
    643 	pointer_handle_button(swc.seat->pointer, get_time(), button, state);
    644 	frame_legacy();
    645 }
    646 
    647 static void
    648 handle_pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time,
    649                     uint32_t axis, wl_fixed_t value)
    650 {
    651 	int32_t discrete = 0;
    652 
    653 	if (!swc.seat || !nested.captured) {
    654 		return;
    655 	}
    656 	if (axis < ARRAY_LENGTH(nested.axis.discrete)) {
    657 		discrete = nested.axis.discrete[axis];
    658 		nested.axis.discrete[axis] = 0;
    659 	}
    660 	pointer_handle_axis(swc.seat->pointer, get_time(), axis,
    661 	                    nested.axis.source, value, discrete * 120);
    662 	frame_legacy();
    663 }
    664 
    665 static void
    666 handle_pointer_frame(void *data, struct wl_pointer *pointer)
    667 {
    668 	nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL;
    669 	if (swc.seat && nested.captured) {
    670 		pointer_handle_frame(swc.seat->pointer);
    671 	}
    672 }
    673 
    674 static void
    675 handle_pointer_axis_source(void *data, struct wl_pointer *pointer,
    676                            uint32_t source)
    677 {
    678 	nested.axis.source = source;
    679 }
    680 
    681 static void
    682 handle_pointer_axis_stop(void *data, struct wl_pointer *pointer, uint32_t time,
    683                          uint32_t axis)
    684 {
    685 }
    686 
    687 static void
    688 handle_pointer_axis_discrete(void *data, struct wl_pointer *pointer,
    689                              uint32_t axis, int32_t discrete)
    690 {
    691 	if (axis < ARRAY_LENGTH(nested.axis.discrete)) {
    692 		nested.axis.discrete[axis] = discrete;
    693 	}
    694 }
    695 
    696 static const struct wl_pointer_listener pointer_listener = {
    697     .enter = handle_pointer_enter,
    698     .leave = handle_pointer_leave,
    699     .motion = handle_pointer_motion,
    700     .button = handle_pointer_button,
    701     .axis = handle_pointer_axis,
    702     .frame = handle_pointer_frame,
    703     .axis_source = handle_pointer_axis_source,
    704     .axis_stop = handle_pointer_axis_stop,
    705     .axis_discrete = handle_pointer_axis_discrete,
    706 };
    707 
    708 /* seat */
    709 
    710 static void
    711 handle_seat_capabilities(void *data, struct wl_seat *seat,
    712                          uint32_t capabilities)
    713 {
    714 	bool has_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
    715 	bool has_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
    716 
    717 	if (has_keyboard && !nested.keyboard) {
    718 		nested.keyboard = wl_seat_get_keyboard(seat);
    719 		wl_keyboard_add_listener(nested.keyboard, &keyboard_listener, NULL);
    720 	} else if (!has_keyboard && nested.keyboard) {
    721 		wl_keyboard_destroy(nested.keyboard);
    722 		nested.keyboard = NULL;
    723 	}
    724 
    725 	if (has_pointer && !nested.pointer) {
    726 		nested.pointer = wl_seat_get_pointer(seat);
    727 		wl_pointer_add_listener(nested.pointer, &pointer_listener, NULL);
    728 		if (nested.shape_manager)
    729 			nested.shape_device = wp_cursor_shape_manager_v1_get_pointer(
    730 			    nested.shape_manager, nested.pointer);
    731 	} else if (!has_pointer && nested.pointer) {
    732 		if (nested.shape_device) {
    733 			wp_cursor_shape_device_v1_destroy(nested.shape_device);
    734 			nested.shape_device = NULL;
    735 		}
    736 		wl_pointer_destroy(nested.pointer);
    737 		nested.pointer = NULL;
    738 		nested.cursor.inside = false;
    739 	}
    740 }
    741 
    742 static void
    743 handle_seat_name(void *data, struct wl_seat *seat, const char *name)
    744 {
    745 }
    746 
    747 static const struct wl_seat_listener seat_listener = {
    748     .capabilities = handle_seat_capabilities,
    749     .name = handle_seat_name,
    750 };
    751 
    752 /* output: only its scale matters */
    753 
    754 static void
    755 handle_output_geometry(void *data, struct wl_output *output, int32_t x,
    756                        int32_t y, int32_t physical_width,
    757                        int32_t physical_height, int32_t subpixel,
    758                        const char *make, const char *model, int32_t transform)
    759 {
    760 }
    761 
    762 static void
    763 handle_output_mode(void *data, struct wl_output *output, uint32_t flags,
    764                    int32_t width, int32_t height, int32_t refresh)
    765 {
    766 }
    767 
    768 static void
    769 handle_output_done(void *data, struct wl_output *output)
    770 {
    771 }
    772 
    773 static void
    774 handle_output_scale(void *data, struct wl_output *output, int32_t factor)
    775 {
    776 	/* SWC_NESTED_SCALE pins the scale. 1 on a dense display makes the
    777 	 * parent magnify us, which is what a session with no scaling of its
    778 	 * own wants */
    779 	if (factor > 0 && !nested.surface && !getenv("SWC_NESTED_SCALE")) {
    780 		nested.scale = factor;
    781 	}
    782 }
    783 
    784 static const struct wl_output_listener output_listener = {
    785     .geometry = handle_output_geometry,
    786     .mode = handle_output_mode,
    787     .done = handle_output_done,
    788     .scale = handle_output_scale,
    789 };
    790 
    791 /* shell surface */
    792 
    793 static void
    794 handle_wm_base_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
    795 {
    796 	xdg_wm_base_pong(wm_base, serial);
    797 }
    798 
    799 static const struct xdg_wm_base_listener wm_base_listener = {
    800     .ping = handle_wm_base_ping,
    801 };
    802 
    803 static void
    804 resize(uint32_t width, uint32_t height)
    805 {
    806 	if (width == nested.width && height == nested.height) {
    807 		return;
    808 	}
    809 	nested.width = width;
    810 	nested.height = height;
    811 	if (nested.screen) {
    812 		screen_resize(nested.screen, width, height);
    813 	}
    814 }
    815 
    816 static void
    817 handle_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
    818                              uint32_t serial)
    819 {
    820 	xdg_surface_ack_configure(xdg_surface, serial);
    821 	nested.configured = true;
    822 
    823 	if (nested.pending_width > 0 && nested.pending_height > 0) {
    824 		resize((uint32_t)(nested.pending_width * nested.scale),
    825 		       (uint32_t)(nested.pending_height * nested.scale));
    826 	}
    827 	nested.pending_width = 0;
    828 	nested.pending_height = 0;
    829 }
    830 
    831 static const struct xdg_surface_listener xdg_surface_listener = {
    832     .configure = handle_xdg_surface_configure,
    833 };
    834 
    835 /* a 0 here means the parent leaves the size to us, so keep ours */
    836 static void
    837 handle_toplevel_configure(void *data, struct xdg_toplevel *toplevel,
    838                           int32_t width, int32_t height,
    839                           struct wl_array *states)
    840 {
    841 	uint32_t *state;
    842 
    843 	nested.pending_width = width;
    844 	nested.pending_height = height;
    845 	nested.fullscreen = false;
    846 	wl_array_for_each(state, states) {
    847 		if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) {
    848 			nested.fullscreen = true;
    849 		}
    850 	}
    851 }
    852 
    853 static void
    854 handle_toplevel_close(void *data, struct xdg_toplevel *toplevel)
    855 {
    856 	wl_display_terminate(swc.display);
    857 }
    858 
    859 static const struct xdg_toplevel_listener toplevel_listener = {
    860     .configure = handle_toplevel_configure,
    861     .close = handle_toplevel_close,
    862 };
    863 
    864 static bool
    865 create_screens(struct wl_list *screens)
    866 {
    867 	struct output *output;
    868 	unsigned width = NESTED_DEFAULT_WIDTH, height = NESTED_DEFAULT_HEIGHT;
    869 	const char *size = getenv("SWC_NESTED_SIZE");
    870 
    871 	if (size && (sscanf(size, "%ux%u", &width, &height) != 2 || width == 0 ||
    872 	             height == 0)) {
    873 		WARNING("nested: ignoring malformed SWC_NESTED_SIZE '%s'\n", size);
    874 		width = NESTED_DEFAULT_WIDTH;
    875 		height = NESTED_DEFAULT_HEIGHT;
    876 	}
    877 	nested.width = width * (uint32_t)nested.scale;
    878 	nested.height = height * (uint32_t)nested.scale;
    879 
    880 	nested.surface = wl_compositor_create_surface(nested.compositor);
    881 	nested.cursor.surface = wl_compositor_create_surface(nested.compositor);
    882 	if (!nested.surface || !nested.cursor.surface) {
    883 		return false;
    884 	}
    885 	nested.xdg_surface =
    886 	    xdg_wm_base_get_xdg_surface(nested.wm_base, nested.surface);
    887 	xdg_surface_add_listener(nested.xdg_surface, &xdg_surface_listener, NULL);
    888 	nested.toplevel = xdg_surface_get_toplevel(nested.xdg_surface);
    889 	xdg_toplevel_add_listener(nested.toplevel, &toplevel_listener, NULL);
    890 	update_title();
    891 	xdg_toplevel_set_app_id(nested.toplevel, NESTED_TITLE);
    892 	wl_surface_commit(nested.surface);
    893 
    894 	/* attaching a buffer before the first configure is a protocol
    895 	 * error, and a tiling parent answers with the size it really gives
    896 	 * us, which has to be known before the screen exists */
    897 	while (!nested.configured) {
    898 		if (wl_display_dispatch(nested.display) < 0) {
    899 			ERROR("nested: lost the parent before the first configure\n");
    900 			return false;
    901 		}
    902 	}
    903 
    904 	output = output_new_fb(nested.width, nested.height, NESTED_OUTPUT_NAME);
    905 	if (!output) {
    906 		return false;
    907 	}
    908 	nested.screen = screen_new_virtual(output, &plane_impl);
    909 	if (!nested.screen) {
    910 		output_destroy(output);
    911 		return false;
    912 	}
    913 	output->screen = nested.screen;
    914 	wl_list_insert(screens, &nested.screen->link);
    915 
    916 	return true;
    917 }
    918 
    919 /* registry */
    920 
    921 static void
    922 handle_global(void *data, struct wl_registry *registry, uint32_t name,
    923               const char *interface, uint32_t version)
    924 {
    925 	if (strcmp(interface, wl_compositor_interface.name) == 0) {
    926 		nested.compositor_version = MIN(version, COMPOSITOR_VERSION);
    927 		nested.compositor =
    928 		    wl_registry_bind(registry, name, &wl_compositor_interface,
    929 		                     nested.compositor_version);
    930 	} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
    931 		nested.wm_base =
    932 		    wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
    933 		xdg_wm_base_add_listener(nested.wm_base, &wm_base_listener, NULL);
    934 	} else if (strcmp(interface, wl_seat_interface.name) == 0 && !nested.seat) {
    935 		nested.seat_version = MIN(version, SEAT_VERSION);
    936 		nested.seat = wl_registry_bind(registry, name, &wl_seat_interface,
    937 		                               nested.seat_version);
    938 		wl_seat_add_listener(nested.seat, &seat_listener, NULL);
    939 	} else if (strcmp(interface, wl_output_interface.name) == 0 &&
    940 	           !nested.output) {
    941 		nested.output = wl_registry_bind(registry, name, &wl_output_interface,
    942 		                                 MIN(version, OUTPUT_VERSION));
    943 		wl_output_add_listener(nested.output, &output_listener, NULL);
    944 	} else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
    945 		nested.shape_manager = wl_registry_bind(
    946 		    registry, name, &wp_cursor_shape_manager_v1_interface, 1);
    947 	} else if (strcmp(interface,
    948 	                  zwp_keyboard_shortcuts_inhibit_manager_v1_interface
    949 	                      .name) == 0) {
    950 		nested.inhibit_manager = wl_registry_bind(
    951 		    registry, name,
    952 		    &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
    953 	}
    954 }
    955 
    956 static void
    957 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
    958 {
    959 }
    960 
    961 static const struct wl_registry_listener registry_listener = {
    962     .global = handle_global,
    963     .global_remove = handle_global_remove,
    964 };
    965 
    966 /* the parent connection is in our event loop. a check source drains
    967  * replies already read, which nothing else wakes the loop for */
    968 static int
    969 handle_display(int fd, uint32_t mask, void *data)
    970 {
    971 	int count;
    972 
    973 	if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
    974 		ERROR("nested: the parent compositor went away\n");
    975 		wl_display_terminate(swc.display);
    976 		return 0;
    977 	}
    978 
    979 	count = mask & WL_EVENT_READABLE
    980 	            ? wl_display_dispatch(nested.display)
    981 	            : wl_display_dispatch_pending(nested.display);
    982 	if (count < 0 ||
    983 	    (wl_display_flush(nested.display) < 0 && errno != EAGAIN)) {
    984 		ERROR("nested: parent connection failed: %s\n", strerror(errno));
    985 		wl_display_terminate(swc.display);
    986 		return 0;
    987 	}
    988 	return count;
    989 }
    990 
    991 /* the seat exists only now, well after the parent sent its keymap */
    992 static void
    993 start(void)
    994 {
    995 	apply_keymap();
    996 	swc_activate();
    997 }
    998 
    999 static void
   1000 finalize(void)
   1001 {
   1002 	release();
   1003 	if (nested.keymap) {
   1004 		xkb_keymap_unref(nested.keymap);
   1005 	}
   1006 	if (nested.source) {
   1007 		wl_event_source_remove(nested.source);
   1008 	}
   1009 	if (nested.frame) {
   1010 		wl_callback_destroy(nested.frame);
   1011 	}
   1012 	if (nested.toplevel) {
   1013 		xdg_toplevel_destroy(nested.toplevel);
   1014 	}
   1015 	if (nested.xdg_surface) {
   1016 		xdg_surface_destroy(nested.xdg_surface);
   1017 	}
   1018 	if (nested.surface) {
   1019 		wl_surface_destroy(nested.surface);
   1020 	}
   1021 	if (nested.cursor.surface) {
   1022 		wl_surface_destroy(nested.cursor.surface);
   1023 	}
   1024 	if (nested.backend.renderer) {
   1025 		wld_destroy_renderer(nested.backend.renderer);
   1026 	}
   1027 	if (nested.backend.context) {
   1028 		wld_destroy_context(nested.backend.context);
   1029 	}
   1030 	if (nested.shape_device) {
   1031 		wp_cursor_shape_device_v1_destroy(nested.shape_device);
   1032 	}
   1033 	if (nested.pointer) {
   1034 		wl_pointer_destroy(nested.pointer);
   1035 	}
   1036 	if (nested.keyboard) {
   1037 		wl_keyboard_destroy(nested.keyboard);
   1038 	}
   1039 	if (nested.seat) {
   1040 		wl_seat_destroy(nested.seat);
   1041 	}
   1042 	if (nested.output) {
   1043 		wl_output_destroy(nested.output);
   1044 	}
   1045 	if (nested.inhibit_manager) {
   1046 		zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(
   1047 		    nested.inhibit_manager);
   1048 	}
   1049 	if (nested.shape_manager) {
   1050 		wp_cursor_shape_manager_v1_destroy(nested.shape_manager);
   1051 	}
   1052 	if (nested.wm_base) {
   1053 		xdg_wm_base_destroy(nested.wm_base);
   1054 	}
   1055 	if (nested.compositor) {
   1056 		wl_compositor_destroy(nested.compositor);
   1057 	}
   1058 	if (nested.registry) {
   1059 		wl_registry_destroy(nested.registry);
   1060 	}
   1061 	if (nested.display) {
   1062 		wl_display_disconnect(nested.display);
   1063 	}
   1064 	memset(&nested, 0, sizeof(nested));
   1065 }
   1066 
   1067 bool
   1068 nested_initialize(void)
   1069 {
   1070 	int32_t cursor_size;
   1071 
   1072 	nested.scale = env_int("SWC_NESTED_SCALE", 1);
   1073 	nested.axis.source = WL_POINTER_AXIS_SOURCE_WHEEL;
   1074 	parse_chord(&nested.capture_key, "SWC_NESTED_CAPTURE_KEY", NESTED_CAPTURE_KEY);
   1075 	parse_chord(&nested.fullscreen_key, "SWC_NESTED_FULLSCREEN_KEY",
   1076 	            NESTED_FULLSCREEN_KEY);
   1077 
   1078 	if (!(nested.display = wl_display_connect(NULL))) {
   1079 		ERROR("nested: could not connect to the parent compositor\n");
   1080 		goto error;
   1081 	}
   1082 	nested.registry = wl_display_get_registry(nested.display);
   1083 	wl_registry_add_listener(nested.registry, &registry_listener, NULL);
   1084 	/* the first roundtrip binds the globals, the second collects what
   1085 	 * they announce on bind: the output scale and the seat capabilities */
   1086 	if (wl_display_roundtrip(nested.display) < 0 ||
   1087 	    wl_display_roundtrip(nested.display) < 0) {
   1088 		ERROR("nested: parent connection failed during setup\n");
   1089 		goto error;
   1090 	}
   1091 	if (!nested.compositor || !nested.wm_base) {
   1092 		ERROR("nested: the parent lacks wl_compositor or xdg_wm_base\n");
   1093 		goto error;
   1094 	}
   1095 
   1096 	nested.backend.context =
   1097 	    wld_wayland_create_context(nested.display, WLD_SHM, WLD_NONE);
   1098 	if (!nested.backend.context) {
   1099 		ERROR("nested: could not create a wld context on the parent\n");
   1100 		goto error;
   1101 	}
   1102 	nested.backend.renderer = wld_create_renderer(nested.backend.context);
   1103 	if (!nested.backend.renderer) {
   1104 		ERROR("nested: could not create a wld renderer\n");
   1105 		goto error;
   1106 	}
   1107 
   1108 	/* the size of a buffer must be a multiple of its buffer scale */
   1109 	cursor_size = NESTED_CURSOR_SIZE - NESTED_CURSOR_SIZE % nested.scale;
   1110 	nested.backend.cursor_width = (uint32_t)cursor_size;
   1111 	nested.backend.cursor_height = (uint32_t)cursor_size;
   1112 	nested.backend.virtual_input = true;
   1113 	nested.backend.create_screens = create_screens;
   1114 	nested.backend.create_target = create_target;
   1115 	nested.backend.set_cursor = set_cursor;
   1116 	nested.backend.start = start;
   1117 	nested.backend.finalize = finalize;
   1118 
   1119 	nested.source = wl_event_loop_add_fd(swc.event_loop,
   1120 	                                     wl_display_get_fd(nested.display),
   1121 	                                     WL_EVENT_READABLE, handle_display,
   1122 	                                     NULL);
   1123 	if (!nested.source) {
   1124 		ERROR("nested: could not watch the parent connection\n");
   1125 		goto error;
   1126 	}
   1127 	wl_event_source_check(nested.source);
   1128 
   1129 	swc.backend = &nested.backend;
   1130 	return true;
   1131 
   1132 error:
   1133 	finalize();
   1134 	return false;
   1135 }