#ifndef C8_SERVER_H
#define C8_SERVER_H

#include <cairo.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <wayland-server-core.h>
#include <wlr/backend.h>
#include <wlr/render/allocator.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_session_lock_v1.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include <wlr/types/wlr_xdg_decoration_v1.h>
#include <wlr/types/wlr_server_decoration.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <xkbcommon/xkbcommon.h>
#include <c8ui/c8ui.h>
#include "math3d.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define C8_DEFAULT_FONT "Selawik, sans-serif"
#define C8_CURSOR_SIZE 24 // px at scale 1, as XCURSOR_SIZE
#define C8_CURSOR_MIN 16
#define C8_CURSOR_MAX 128

enum c8_pattern {
	C8_PATTERN_NONE,
	C8_PATTERN_CIRCLES,
	C8_PATTERN_LINES,
	C8_PATTERN_SQUARES,
	C8_PATTERN_COUNT,
};

/* What the screens show with a second one plugged in ([display] project,
 * the Second screen flyout); AUTO is unset: XR on glasses, else
 * every display on and placed side by side */
enum c8_project {
	C8_PROJECT_AUTO,
	C8_PROJECT_PC, // the built-in panel only
	C8_PROJECT_DUPLICATE,
	C8_PROJECT_SECOND, // the other screen only
	C8_PROJECT_XR, // XR glasses: apps in the space around you
	C8_PROJECT_COUNT,
};

/* What a scene node belongs to, stored in wlr_scene_node.data. */
enum c8_node_type {
	C8_NODE_VIEW,
	C8_NODE_LAYER,
	C8_NODE_POPUP,
	C8_NODE_UI,
	C8_NODE_SHELL, // a surface of the shell client
};

struct c8_node_desc {
	enum c8_node_type type;
	void *data;
};

enum c8_action_type {
	C8_ACTION_NONE,
	C8_ACTION_EXEC,
	C8_ACTION_START,
	C8_ACTION_SEARCH,
	C8_ACTION_CLOSE,
	C8_ACTION_SNAP_LEFT,
	C8_ACTION_SNAP_RIGHT,
	C8_ACTION_UNSNAP,
	C8_ACTION_SPLIT_LEFT, // move the split between two apps a quarter
	C8_ACTION_SPLIT_RIGHT,
	C8_ACTION_SWITCHER,
	C8_ACTION_APP_WINDOWS,
	C8_ACTION_CHARMS,
	C8_ACTION_SETTINGS,
	C8_ACTION_PC_SETTINGS,
	C8_ACTION_APPS,
	C8_ACTION_QUIT,
	C8_ACTION_SCREENSHOT, // to the clipboard
	C8_ACTION_SCREENSHOT_WINDOW, // the app in front, to the clipboard
	C8_ACTION_SCREENSHOT_SAVE, // to Pictures/Screenshots
	C8_ACTION_SNIP, // pick a part of the screens (snip.c)
	C8_ACTION_PROJECT, // the Second screen flyout
	C8_ACTION_GLASSES_RECENTRE, // XR: forward is where you look
	C8_ACTION_NIGHT_LIGHT, // warmer colours on or off, now
	/* Media keys: the shell does them and shows the level */
	C8_ACTION_VOLUME_UP,
	C8_ACTION_VOLUME_DOWN,
	C8_ACTION_MUTE,
	C8_ACTION_MIC_MUTE,
	C8_ACTION_BRIGHTNESS_UP,
	C8_ACTION_BRIGHTNESS_DOWN,
	C8_ACTION_PLAY_PAUSE,
	C8_ACTION_NEXT_TRACK,
	C8_ACTION_PREVIOUS_TRACK,
	C8_ACTION_STOP_MEDIA,
	C8_ACTION_COUNT, /* keep last: settings.c checks names up to it */
};

struct c8_binding {
	struct wl_list link;
	uint32_t modifiers;
	xkb_keysym_t sym;
	enum c8_action_type action;
	char *arg;
};

/* An [output NAME] section; unset fields leave the display's own */
struct c8_output_config {
	struct wl_list link; // c8_config.outputs
	char *name;
	int enabled; // -1: unset
	bool has_mode;
	int width, height, refresh; // refresh in mHz, 0: any
	bool has_pos;
	int x, y;
	float scale; // 0: unset
	int transform; // enum wl_output_transform, -1: unset
};

struct c8_config {
	char *dir;
	char *terminal;
	char *font;
	char *icon_theme;
	char *cursor_theme; // NULL: XCURSOR_THEME or libXcursor's default
	int cursor_size;
	char *notify_corner; // top-right, top-left, bottom-right, bottom-left
	char *clock; // auto (the locale's way), 12 or 24
	bool xwayland;
	char *xkb_layout, *xkb_variant, *xkb_options;
	int repeat_rate, repeat_delay; // keys per second, ms
	struct {
		bool tap, natural_scroll, disable_while_typing;
		double speed; // -1..1
	} touchpad;
	struct {
		bool natural_scroll, left_handed;
		double speed;
	} mouse;
	struct wl_list outputs; // c8_output_config.link
	char **autostart;
	int autostart_len;
	struct wl_list bindings;
	/* Power commands for the user menu on Start; empty hides the item */
	char *lock_cmd, *sleep_cmd, *restart_cmd, *shutdown_cmd;
	int screen_off_after, sleep_after; // minutes idle, 0 for never
	char *locker; // the ext-session-lock client we run
	bool lock_when_off; // lock as the screens go off for idleness
	enum c8_project project; // [display]
	/* [nightlight], see nightlight.c */
	struct {
		bool enabled;
		int temperature; // kelvin at its warmest
		enum c8_night_schedule {
			C8_NIGHT_TIMES, // from..to
			C8_NIGHT_SUNSET, // sunset to sunrise at the place; none: times
			C8_NIGHT_ALWAYS,
		} schedule;
		int from, to; // minutes into the day
		bool has_place;
		double latitude, longitude;
	} night;
	/* [glasses], see glasses.c */
	struct {
		double fov; // diagonal, degrees
		double distance; // metres to the shell slate
		double ipd; // millimetres between the eyes (SBS)
		int screen_w, screen_h; // the shell's own screen in the glasses
		int latency; // ms to predict the head ahead by
		double scanout; // ms to light a frame top to bottom, -1 = a refresh
		double tilt; // degrees the sensor's ahead is above the eyes'
		double neck_up, neck_ahead; // m from the neck's pivot to the eyes
		bool outlines; // the space drawn as lines, for recordings
		bool steady; // still heads steadied, see imu.c
		double start_distance; // metres to Start when it opens
		char *background; // a 360 picture round you, or NULL
		/* where its horizon is, % down it (one that isn't all the
		 * sphere: wider than 2:1) */
		double background_horizon;
		/* Who draws XR (xrruntime.c): Monado through OpenXR, our own
		 * direct path, or Monado when it is installed */
		enum c8_xr_runtime { C8_XR_RUNTIME_AUTO, C8_XR_RUNTIME_MONADO,
			C8_XR_RUNTIME_BUILTIN } runtime;
		bool sbs_switch; // put them in 3D for Monado, glassesmode.c
	} glasses;

	/* Appearance, [appearance] in config.ini */
	uint32_t accent;
	uint32_t background;
	enum c8_pattern pattern;
	bool more_tiles;
	bool live_tiles;
	bool has_appearance; // config.ini has an [appearance] section
};

enum c8_shell_role {
	C8_SHELL_START,
	C8_SHELL_PANEL,
	C8_SHELL_SPLASH,
	C8_SHELL_OSD,
	C8_SHELL_BAR, // XR's charms bar, see hologram.c
};

/* A surface of the shell client, see shell.c */
struct c8_shell_surface {
	struct wl_list link; // c8_server.shell.surfaces
	struct c8_server *server;
	struct wl_resource *resource;
	struct wlr_surface *surface;
	enum c8_shell_role role;
	struct c8_node_desc desc;
	struct wlr_scene_tree *tree;
	struct wlr_scene_tree *surface_tree;
	struct wlr_box rect, pending_rect; // panels: layout coordinates
	bool rect_pending;
	int configured_w, configured_h;
	bool grab_keyboard;
	struct wl_listener surface_destroy;
};

/* The shell client and what it has made */
struct c8_shell {
	struct wl_client *client;
	struct wl_resource *resource; // its c8_shell_v1
	struct wl_global *global;
	struct wl_listener client_destroy;
	struct wl_event_source *respawn;
	struct wl_event_source *icons; // the icon loader has some ready
	int64_t started;
	int quick_exits; // in a row, for backing off
	bool spawned; // once, so later runs are respawns
	struct c8_shell_surface *start;
	struct wl_list surfaces; // c8_shell_surface.link
	/* Start shown but kept hidden until the shell draws it fresh, so its
	 * last frame (tiles in place) doesn't flash before the sweep */
	bool start_reveal_pending;
	struct wl_event_source *start_reveal;
	/* In the glasses Start sweeps away as it closes: shown a little
	 * longer (the shell animates it), then hidden by this timer */
	struct wl_event_source *start_close;
	/* The snap last told (snap event), so arranging only sends changes;
	 * primary is the app that had the rest then (never dereferenced) */
	struct {
		struct c8_view *view;
		struct c8_view *primary;
		int side;
		struct wlr_box rest;
	} snap;
};

/* The title bar's look for a window, as the shell picked it */
struct c8_view_style {
	bool set;
	uint32_t color;
	char *icon_name;
	/* Loaded in the background (shell.c), as a picture big enough for
	 * every place it's drawn: NULL until it's here, and after if there
	 * is none by that name (icon_loaded says which) */
	cairo_surface_t *icon;
	struct c8ui_icon_request *icon_request;
	bool icon_loaded;
};

struct c8_view;

/* The split moving (tween.c): while two apps change places or sizes
 * each one's last picture is held and stretched from its old box to
 * its new one, then fades into the app redrawn at its new size */
struct c8_tween_cover {
	struct c8_view *view; // NULL once it's gone
	struct wlr_box from, to;
	struct wlr_scene_buffer *frame; // its held picture; NULL: cropped live
	bool anchor_right; // cropped live, its content keeps the right edge
	bool hidden; // laid out but not shown (behind the shell's pick)
};

/* An app's picture left behind by the layout, under the moving ones */
struct c8_tween_leave {
	struct wlr_scene_buffer *frame;
	bool fade; // it closed
};

#define C8_TWEEN_LEAVE_MAX 4

struct c8_tween {
	struct c8_tween_cover covers[2];
	int count;
	struct c8_tween_leave leave[C8_TWEEN_LEAVE_MAX];
	int leave_count;
	struct wlr_box div_from, div_to; // empty: no divider there
	int64_t start; // ms, of the move or the fade
	bool active; // moving
	bool fading; // moved: the pictures fading into the apps
	struct wlr_scene_tree *tree; // the pictures, over the apps
	struct wl_event_source *timer;
	/* The layout last arranged, to move from */
	struct c8_view *last_view[2];
	struct wlr_box last_box[2], last_div, last_area;
	int last_count;
	bool last_split;
	/* An app let go of by its title bar, and where */
	struct c8_view *drop_view;
	struct wlr_box drop_box;
};

/* Window chrome: the title bar that drops down at the top edge, dragging
 * apps by it to snap them, and dragging the divider between two apps */
struct c8_chrome {
	struct wlr_scene_tree *tree;
	struct wlr_scene_buffer *bar, *card, *preview;
	struct c8_view *view; // the bar's app, or the one being dragged
	struct wlr_box bar_box; // layout coordinates
	int hover, pressed; // bar buttons
	int grab; // chrome.c's enum chrome_grab
	double grab_x, grab_y;
	int zone; // where a dragged app would go
	bool pinned; // pulled down by a swipe: stays until a press elsewhere
};

/* What the window management code needs from a shell (xdg or X11) */
struct c8_view_impl {
	struct wlr_surface *(*get_surface)(struct c8_view *view);
	const char *(*get_title)(struct c8_view *view);
	const char *(*get_app_id)(struct c8_view *view);
	struct c8_view *(*get_parent)(struct c8_view *view);
	bool (*wants_fullscreen)(struct c8_view *view);
	/* Fill box (layout coordinates) */
	void (*configure)(struct c8_view *view, struct wlr_box box, bool fullscreen);
	/* Floating: centre at the window's own size within area */
	void (*place)(struct c8_view *view, struct wlr_box area);
	void (*activate)(struct c8_view *view, bool activated);
	void (*close)(struct c8_view *view);
};

/* The slate's controls under a window in XR, see slatebar.c */
#define C8_SLATEBAR_H 40 // px of the slate
#define C8_SLATEBAR_GAP 12 // between them and the window (the handle's room)
#define C8_SLATEBAR_HANDLE 28 // the resize handle round the corner

enum c8_slatebar_part {
	C8_SLATEBAR_NONE,
	C8_SLATEBAR_TITLE, // drag to carry it
	C8_SLATEBAR_FOLLOW,
	C8_SLATEBAR_MOVE,
	C8_SLATEBAR_MINIMISE,
	C8_SLATEBAR_MAXIMISE, // the theatre, and back
	C8_SLATEBAR_STEREO, // Show as 3D: flat, side by side, top and bottom
	C8_SLATEBAR_CLOSE,
	C8_SLATEBAR_CORNER, // the handle: drag to resize
};

struct c8_slatebar_look {
	int width;
	int hover, pressed; // enum c8_slatebar_part
	bool focused, follow, icon, theatre;
	int stereo; // the window's 3D layout, as view->holo.stereo
	uint32_t accent;
	char *title;
};

struct c8_view {
	struct wl_list link; // c8_server.views, most recently focused first
	struct c8_server *server;
	const struct c8_view_impl *impl;
	struct wlr_xdg_toplevel *toplevel; // NULL for X11 windows
	struct wlr_scene_tree *tree;
	struct c8_node_desc desc;
	struct wlr_xdg_toplevel_decoration_v1 *decoration;
	struct wlr_box geo;
	bool mapped, floating;
	/* The title as stealth mode shows it (c8_view_title) */
	char *title_from, *title_shown;
	unsigned title_gen;
	struct wl_resource *shell_window; // c8_shell_window_v1, if announced
	bool shell_focused; // as last told
	struct c8_view_style style;
	/* In other tools' window lists, see protocols.c */
	struct {
		struct wlr_ext_foreign_toplevel_handle_v1 *ext;
		struct wlr_foreign_toplevel_handle_v1 *wlr;
		struct wl_listener activate, close, minimize;
		/* Sharing the window alone (protocols.c): a scene of its own
		 * holding only its surfaces, and the capture source over it */
		struct wlr_scene *capture_scene;
		struct wlr_ext_image_capture_source_v1 *capture;
		struct wl_listener capture_gone;
	} listed;

	/* Its slate in XR, see hologram.c */
	struct {
		int slot; // 1 + its slot of the layout, 0 none
		bool placed; // pose set (dialogs take their window's)
		struct pose pose; // centre, facing you, in the recentred world
		int width, height; // the window's size there
		bool follow; // Follow me: it drifts after your gaze
		/* Show as 3D, chosen on its slate bar: its picture holds both
		 * eyes' (the stereo protocol's layouts: 0 flat, 1 side by
		 * side, 2 top and bottom), for apps that don't say so
		 * themselves (games, emulators); remembered per app */
		int stereo;
		bool kept; // placed by you (or from where you had it): kept on close
		bool summon; // flying to where you look (it was picked)
		/* Put away (it and its dialogs): not drawn, an icon over the
		 * charms bar until it's picked again */
		bool minimised;
		/* Fullscreen: a big slate ahead, where it was kept for after */
		bool theatre;
		struct pose before_pose;
		int before_width, before_height;
		bool before_follow;
		/* Maximise's say over the theatre: 1 in, -1 out, 0 as the
		 * app asks (its next fullscreen request clears it) */
		int theatre_ask;
		/* Being resized: its top left corner stays here until the app
		 * has caught up (it may answer late) */
		struct vec3 anchor;
		int64_t anchor_until; // ms
		/* Its slate bar's picture and what it showed, see slatebar.c */
		struct wlr_texture *bar;
		struct c8_slatebar_look bar_look;
		/* Opened out of view: its icon at the view's edge for a while
		 * (ms it opened, 0 none), known once it's placed */
		int64_t beacon_at;
		bool beacon_pending;
		struct wlr_texture *beacon; // that icon, see slatebar.c
		uint32_t beacon_color;
		bool beacon_icon; // drawn with its icon (not a letter)
		int64_t unseen_frame; // ms: out of view, last told to draw then
		/* ms it was minimised or restored: it shrinks away towards the
		 * bar, or comes back up from it (glasses.c `away`) */
		int64_t minimised_at, restored_at;
	} holo;

	struct wl_listener map, unmap, commit, destroy;
	struct wl_listener request_fullscreen, request_maximize, request_move,
		request_resize, set_title, set_app_id;
};

struct c8_popup {
	struct wlr_xdg_popup *popup;
	struct wl_listener commit, reposition, destroy;
};

struct c8_layer {
	struct wl_list link; // c8_server.layers
	struct c8_server *server;
	struct wlr_layer_surface_v1 *layer_surface;
	struct wlr_scene_layer_surface_v1 *scene;
	struct c8_node_desc desc;
	bool mapped;
	struct wl_listener map, unmap, commit, destroy, new_popup;
};

struct c8_output {
	struct wl_list link;
	struct c8_server *server;
	struct wlr_output *wlr_output;
	struct wlr_scene_output *scene_output;
	bool idle_off; // turned off for idleness, not by the user
	bool builtin; // the laptop's own panel: eDP, LVDS or DSI
	bool glasses; // XR glasses (XREAL), see output.c
	bool capture_global; // its wl_output is the portal's only, glasses.c
	/* What it shows: the layout like any display, or (out of the layout)
	 * another's picture, see glasses.c */
	enum c8_output_role {
		C8_OUTPUT_NORMAL,
		C8_OUTPUT_VIRTUAL, // the shell's screen in XR: no display
		C8_OUTPUT_MIRROR, // Duplicate: the primary's picture, letterboxed
		C8_OUTPUT_GLASSES, // XR: slates in space, drawn in GL
		/* XR through Monado, which has the glasses: what they show,
		 * drawn only for a capture (sharing), see glasses.c */
		C8_OUTPUT_VIEW,
	} role;
	bool sink_dirty; // a mirror or glasses: the picture changed
	bool lock_drawn; // has shown the lock cover since locking
	bool leasing; // the glasses, offered to Monado: not ours to draw
	/* Night light: what its ramps were made for (0: none of ours), a
	 * gamma-control client having them instead, a refusal said once */
	int night_key;
	bool gamma_client, night_failed;
	struct wl_listener frame, request_state, destroy, present;
	struct wl_listener needs_frame; // the view's only: a capture wants one
	/* The last frame shown: when (CLOCK_MONOTONIC ns) and the refresh
	 * then, for the glasses to time their poses by */
	int64_t presented_ns, refresh_ns;
};

/* Shows another's picture (a duplicate, the glasses): no part of the layout */
static inline bool c8_output_is_sink(struct c8_output *output) {
	return output->role == C8_OUTPUT_MIRROR ||
		output->role == C8_OUTPUT_GLASSES || output->role == C8_OUTPUT_VIEW;
}

/* Ours, not a display: XR's shell screen and the view for capturing */
static inline bool c8_output_is_virtual(struct c8_output *output) {
	return output->role == C8_OUTPUT_VIRTUAL || output->role == C8_OUTPUT_VIEW;
}

struct c8_keyboard {
	struct wl_list link;
	struct c8_server *server;
	struct wlr_keyboard *wlr_keyboard;
	bool is_virtual; // the client's keymap, not ours
	struct wl_listener modifiers, key, destroy;
};

struct c8_pointer {
	struct wl_list link; // c8_server.pointers
	struct c8_server *server;
	struct wlr_input_device *device; // a mouse, touchpad, touchscreen or pen
	struct wl_listener destroy;
};

struct c8_server {
	struct wl_display *display;
	unsigned stealth_gen; // counts stealth mode's changes (c8_view_title)
	struct wl_event_loop *loop;
	struct wlr_backend *backend;
	struct wlr_session *session;
	struct wlr_renderer *renderer;
	struct wlr_allocator *allocator;
	struct wlr_compositor *compositor;
	struct wlr_scene *scene;
	struct wlr_scene_output_layout *scene_layout;
	struct wlr_output_layout *output_layout;
	struct wl_list outputs;
	struct wl_listener new_output, layout_change;
	struct wlr_output_manager_v1 *output_manager;
	struct wl_listener output_manager_apply, output_manager_test;
	float scale;
	bool terminating; // tearing down: the scene may already be gone

	struct wlr_xwayland *xwayland;
	struct wl_listener xwayland_ready, xwayland_new_surface,
		xwayland_pointer_focus;

	struct {
		struct wlr_scene_tree *background, *bottom, *views, *border, *unmanaged,
			*start, *top, *shell, *overlay, *splash, *drag, *snip, *lock;
	} layers;

	/* Drag and drop between apps (drag.c) */
	struct {
		struct wlr_drag *drag; // the one going on, or NULL
		struct wlr_scene_tree *icon;
		struct wl_listener request_start, start, destroy;
	} dnd;

	struct wlr_xdg_shell *xdg_shell;
	struct wl_listener new_toplevel, new_popup;
	struct wl_list views;
	struct c8_view *snapped;
	uint32_t focus_serial; // the display serial when focus last moved
	bool snap_left;
	double snap_fraction; // of the usable width, for the snapped app
	struct wlr_box divider_box; // empty with nothing snapped
	struct wlr_box rest_box; // the other side of a snapped app; empty too
	struct c8_tween tween;
	struct c8_chrome chrome;
	const char *cursor_name; // ours, while internal_cursor
	const char *shape_name; // the app's cursor-shape, NULL for its own surface
	struct wlr_scene_rect *divider;
	struct wlr_scene_rect *focus_border[4]; // on the focused tiled app's edge

	struct wlr_layer_shell_v1 *layer_shell;
	struct wl_listener new_layer_surface;
	struct wl_list layers_list;
	struct wlr_layer_surface_v1 *focused_layer;
	struct wlr_box usable;

	struct wlr_xdg_decoration_manager_v1 *decoration_manager;
	struct wl_listener new_decoration;
	/* KDE's older protocol: the only one GTK3 listens to */
	struct wlr_server_decoration_manager *kde_decoration_manager;

	struct wlr_seat *seat;
	struct wl_list keyboards;
	struct wl_list pointers;
	struct wlr_keyboard_shortcuts_inhibit_manager_v1 *shortcuts_inhibit;
	struct wl_listener new_input, request_cursor, request_set_selection,
		request_set_primary_selection, new_virtual_keyboard,
		new_virtual_pointer;
	struct wlr_cursor *cursor;
	struct wlr_xcursor_manager *xcursor_manager;
	struct wl_listener cursor_motion, cursor_motion_absolute, cursor_button,
		cursor_axis, cursor_frame;
	/* Touchscreens and pens (touch.c) */
	struct {
		struct wl_listener down, up, motion, cancel, frame;
		struct wl_listener tool_axis, tool_proximity, tool_tip, tool_button;
		int32_t pointer_id; // the finger acting as the mouse, -1 for none
		bool pointer_moved; // a pointer frame is due
		/* Fingers on apps that take touch: the surface's origin */
		struct {
			int32_t id;
			bool down;
			double ox, oy;
		} points[10];
		/* The pen over an app that does tablets itself (tablet-v2) */
		struct wlr_surface *pen_surface;
		struct wl_listener pen_surface_destroy;
		bool pen_down;
		double pen_ox, pen_oy; // the surface's origin while the tip is down
		/* A finger that landed on a screen edge, maybe a swipe in */
		struct {
			int32_t id; // -1 for none
			int edge; // touch.c's enum edge
			bool fired; // it did its thing; the rest of it is ours
			struct wlr_input_device *device;
			double x, y; // where it landed, 0..1 of the screen
			double lx, ly; // the same in layout coordinates
			uint32_t time;
		} edge;
		/* Every finger on the screen, wherever it goes: two landing
		 * together and moving as one are a swipe, as three are on the
		 * touchpad */
		struct {
			int32_t id;
			bool down;
			bool spent; // part of a swipe: ignored until lifted
			double lx, ly;
			uint32_t time; // when it landed
		} fingers[10];
		bool maybe_swipe; // two landed together: watching them move
		double maybe_x, maybe_y, maybe_spread; // their middle and gap then
		bool swiping; // the swipe is going to the app under it
		double swipe_x, swipe_y; // the fingers' middle as last sent
		uint32_t swipe_time;
	} touch;
	bool super_alone;
	bool internal_cursor;
	/* The surface a button went down on keeps the pointer until it's up
	 * (compared only, never used); its origin in layout coordinates */
	struct {
		struct wlr_surface *surface;
		double ox, oy;
	} press;
	/* Alt+Tab or Super+` held down keeps stepping, like a held key */
	struct {
		struct wl_event_source *timer;
		struct wlr_keyboard *keyboard; // NULL: nothing held
		uint32_t keycode;
		enum c8_action_type action;
		uint32_t modifiers; // the binding's, Shift aside
	} step_repeat;

	/* Raw motion, locking and confining, gestures: see pointer.c */
	struct {
		struct wlr_relative_pointer_manager_v1 *relative;
		struct wlr_pointer_constraints_v1 *constraints;
		struct wlr_pointer_constraint_v1 *active;
		struct wlr_pointer_gestures_v1 *gestures;
		struct wlr_tablet_manager_v2 *tablets;
		struct wl_listener new_constraint, swipe_begin, swipe_update,
			swipe_end, pinch_begin, pinch_update, pinch_end, hold_begin,
			hold_end;
	} pointer;

	struct c8_config config;
	struct c8_shell shell;
	bool start_visible; // Start covers the apps
	struct wlr_scene_rect *desktop; // behind the apps

	/* The settings app, see settings.c */
	struct wl_global *settings_global;
	/* Says which clients are sandboxed (Flatpak): see global_filter */
	struct wlr_security_context_manager_v1 *security_context;
	struct wl_client *settings_client;
	struct wl_listener settings_client_destroy;
	struct wl_list settings_handles;
	/* c8-sync, which keeps the settings in step with the online
	 * account: the other client of that protocol, run in the background */
	struct wl_client *sync_client;
	struct wl_listener sync_client_destroy;
	struct wl_event_source *sync_respawn;
	int sync_tries;

	/* The session and idleness, see session.c */
	bool session_systemd;
	struct wlr_idle_notifier_v1 *idle_notifier;
	struct wlr_idle_inhibit_manager_v1 *idle_inhibit;
	struct wl_listener new_idle_inhibitor;
	int idle_inhibitors;
	struct wl_event_source *screen_off_timer, *sleep_timer;
	bool screens_off;
	char project_sent[128]; // the Second screen state the shell last got

	struct {
		struct wl_listener request_activate, set_gamma, capture_request;
	} protocols;

	/* The screen lock, see lock.c */
	struct {
		struct wlr_session_lock_manager_v1 *manager;
		struct wlr_session_lock_v1 *active; // the locker's lock object
		bool locked; // stays true if the locker dies without unlocking
		bool locked_sent;
		struct wlr_scene_rect *cover;
		struct wl_list surfaces; // lock.c's lock_surface.link
		struct wl_client *client;
		int tries; // lockers started since one last unlocked
		struct wl_event_source *retry;
		struct wl_listener new_lock, new_surface, unlock, destroy,
			client_destroy;
	} lock;

	const char *socket;

	/* Mirrors and glasses, see glasses.c */
	struct {
		struct wlr_backend *backend; // headless, for the virtual output
		struct c8_output *shell; // the virtual output, in XR
		/* It as made: announced (new_output) only once the backend runs */
		struct wlr_output *shell_made;
		/* With Monado: the view (C8_OUTPUT_VIEW) as made, and while
		 * it's being made (new_output can come from inside that) */
		struct wlr_output *view_made;
		bool view_coming;
		struct c8_output *view; // it, once announced
		/* Monado's frames of it, asked for while a capture wants them */
		bool view_streaming;
		/* The runtime's name for its headset ("HTC Vive"), "" until
		 * known: the view's description in the share picker */
		char headset[256];
		int view_unwanted; // its frames since a capture last wanted one
		/* Print waiting on Monado's picture: which action, and the timer
		 * that gives up on it */
		int shot_action; // 0: none
		struct wl_event_source *shot_timer;
		struct c8_output *source; // whose picture the sinks show
		struct wlr_buffer *picture; // its last frame, locked
		/* of it, made when first drawn; NULL when it's a client's
		 * buffer, whose own texture is drawn (see picture_texture) */
		struct wlr_texture *picture_texture;
		/* Scanned out, the frame may be a client's buffer covering only
		 * part of the source (a lone toast): which part of it, where */
		struct wlr_fbox picture_src; // empty: all of it
		struct wlr_box picture_dst; // in the source's pixels
		struct wlr_box picture_shown; // of it, what has anything on
		int picture_w, picture_h; // the source's size
		uint64_t picture_seq; // counts the pictures taken
		bool updating; // making or dropping the virtual output
		/* Asked again meanwhile (the glasses' lease ending as Monado
		 * is stopped): once more when this one's done */
		bool update_again;
		bool audio; // sound moved to the glasses, see xr_audio
		struct c8_xr_gl *gl; // shaders, made the first time they're used
		struct c8_tracker *tracker; // where the head points, see tracker.c
		struct quat centre; // the world turned so forward is this
		double centre_pitch; // and how far up the slate goes, radians
		/* Holograms, see hologram.c */
		uint64_t slots; // taken slots of the layout
		struct pose head; // as last drawn, recentred
		double m_per_px, fov_x, fov_y; // a slate pixel; one eye's view
		struct pose shell_pose; // the shell's screen, where it's placed
		/* and as drawn: in front of any window nearer than that, its
		 * pixels smaller by as much, so it looks the same size and in
		 * 3D isn't further away than what it covers */
		struct pose shell_drawn;
		double shell_m;
		/* Start's tiles' width (the shell says): the shell's screen is
		 * widened to it while Start is up */
		int start_width;
		bool shell_placed;
		struct vec3 aim; // the pointer's ray, recentred
		bool aim_set;
		struct c8_view *aim_view; // what it lands on, or
		bool aim_shell; // the shell's screen, or neither
		double aim_t; // m along the ray to it
		int64_t scrolled_ms; // the last wheel or touchpad scroll
		struct c8_view *grab_view; // pressed on, while held
		bool grab_shell;
		struct c8_view *move_view; // carried by Super + drag
		double move_dyaw, move_dpitch; // its centre from the ray
		/* The launch splash's slate: where the next window opens */
		struct pose splash_pose;
		bool splash_placed;
		int64_t splash_at; // ms it showed or went
		float dim; // the windows' brightness, eased towards its goal
		/* The charms bar: comes up below you as you look down, or
		 * where you look after the charms binding (pinned) */
		struct pose bar_pose;
		bool bar_shown, bar_pinned;
		bool bar_grabbed; // it has the keyboard (the switcher, a card)
		bool bar_grab_pinned; // and was pinned for it, not already up
		bool bar_turning; // turning after you, until it's in front again
		int bar_h; // its height last frame: it grows upwards
		float bar_t; // 0 gone to 1 up, linear in time (BAR_IN_MS)
		float bar_fade; // bar_t eased: how opaque it's drawn
		int64_t bar_t_ms; // when bar_t last moved
		struct pose bar_drawn; // bar_pose, lower while it rises in
		double bar_sink_from; // looking lower than this sinks it (bar_frame)
		bool aim_bar, grab_bar;
		/* Slate bars (slatebar.c): the window whose bar the ray is on
		 * and which part, the one pointed at last (its bar shows), and
		 * a button pressed, acting if released over it */
		struct c8_view *aim_chrome;
		int aim_part;
		struct c8_view *chrome_view;
		struct c8_view *press_view;
		int press_part;
		/* Minimised windows' icons over the charms bar: the one the ray
		 * is on, and one pressed (it comes back if released there) */
		struct c8_view *aim_mini, *press_mini;
		struct wlr_texture *label; // the pointed one's title, slatebar.c
		char *label_text;
		/* Move (or the title dragged) carries move_view; Move's
		 * carrying lasts until the next click (sticky) */
		bool move_sticky;
		/* The corner dragged: the window's size follows the ray from
		 * where it began */
		struct c8_view *size_view;
		double size_px, size_py;
		int size_w, size_h;
		bool swallow_release; // the click that ended a sticky move
		/* A theatre's bar shows only with the ray at its top edge */
		struct c8_view *reveal_view;
		/* The HUD (toasts, the level box): its bottom middle eased
		 * after the bottom middle of the view, see hologram.c */
		bool hud_placed;
		double hud_yaw, hud_pitch;
		struct pose hud_pose;
		double hud_m;
		bool aim_hud, grab_hud;
		/* The Monado path (xrruntime.c): the runtime and our overlay
		 * in it, and the glasses leased to it (their output is gone,
		 * but XR goes on) */
		struct c8_xr_monado *runtime;
		bool leased;
	} xr;

	/* lease.c: headsets leased to a VR runtime */
	struct {
		struct wlr_drm_lease_v1_manager *manager; // NULL: none to lease
		struct wl_listener request;
	} lease;

	/* stereo.c: surfaces holding 3D or all-round pictures */
	struct {
		struct wl_global *global;
		struct wl_list surfaces; // c8_stereo_surface.link, newest sphere first
		uint32_t shows; // as the clients were last told
	} stereo;
};

/* util.c */
void c8_fonts_init(void);
#define C8_XR_NAME "XR-1" // the XR primary, a headless output
#define C8_XR_VIEW_NAME "XR-VIEW" // what the glasses show, for sharing

/* A listener on a signal, the two lines every one takes */
static inline void c8_listen(struct wl_listener *listener,
		struct wl_signal *signal, wl_notify_func_t notify) {
	listener->notify = notify;
	wl_signal_add(signal, listener);
}

int64_t c8_now_ms(void);
int64_t c8_now_ns(void);
void c8_cross_path(cairo_t *cr, double cx, double cy, double r);

#ifndef MAX // GLib has them too, where it's included
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
void c8_exec(const char *cmd);
/* With XDG_ACTIVATION_TOKEN set to token (if not NULL) */
void c8_exec_token(const char *cmd, const char *token);
char *c8_strdup(const char *s);
void c8_set_str(char **dst, const char *src);
void c8_color_to_float(uint32_t rgb, float alpha, float out[4]);
typedef void (*c8_draw_fn)(cairo_t *cr, int width, int height, void *data);
void c8_scene_buffer_draw(struct wlr_scene_buffer *node, int width, int height,
	float scale, c8_draw_fn draw, void *data);
struct wlr_buffer *c8_buffer_from_argb(const uint8_t *data, int width,
	int height);
/* Runs argv[0] with a private Wayland connection (WAYLAND_SOCKET), as its
 * own session so it outlives us cleanly; NULL if that fails */
struct wl_client *c8_spawn_client(struct c8_server *server,
	char *const argv[]);
/* The same as our own child, to stop by pid (util.c); stdin_out, if
 * given, gets the write end of a pipe that is its stdin */
/* pass_fd (if >= 0) goes to the child as the number in env pass_env */
struct wl_client *c8_spawn_client_child(struct c8_server *server,
	char *const argv[], char *const env[], pid_t *pid, int *stdin_out,
	int pass_fd, const char *pass_env);
struct wlr_scene_buffer *c8_scene_buffer_create(struct wlr_scene_tree *parent,
	struct c8_node_desc *desc);
int c8_text(cairo_t *cr, const char *font, double size, int weight,
	const char *text, double x, double y, int max_width, int max_lines);
int c8_text_width(cairo_t *cr, const char *font, double size, int weight,
	const char *text);
bool c8_state_path(const char *name, char *path, size_t size, bool make);

extern const uint32_t c8_palette[];
extern const int c8_palette_len;
extern const uint32_t c8_backgrounds[];
extern const int c8_backgrounds_len;
extern struct c8_node_desc c8_ui_desc;

/* config.c */
void c8_config_init(struct c8_server *server, const char *dir);
void c8_config_finish(struct c8_server *server);
/* Read config.ini again. The previous settings are moved to *old, for
 * comparing; free them with c8_config_free_file(). */
/* Reads an ini file: handler gets every key = value with its section,
 * and each section as it starts (key NULL). False if it can't be opened. */
typedef void (*c8_ini_handler)(void *data, const char *section,
	const char *key, const char *value);
bool c8_ini_parse(void *data, const char *path, c8_ini_handler handler);
void c8_config_reload(struct c8_server *server, struct c8_config *old);
void c8_config_free_file(struct c8_config *config);
bool c8_parse_combo(const char *combo, uint32_t *mods, xkb_keysym_t *sym);
void c8_format_combo(uint32_t mods, xkb_keysym_t sym, char *buf, size_t len);
const char *c8_action_name(enum c8_action_type action);
struct c8_output_config *c8_output_config_find(struct c8_config *config,
	const char *name);
const char *c8_transform_name(int transform);
/* Edits to config.ini that keep its comments and layout */
enum c8_config_op_type {
	C8_CONFIG_SET,
	C8_CONFIG_ADD,
	C8_CONFIG_REMOVE,
};
struct c8_config_op {
	enum c8_config_op_type type;
	char *section, *key, *value;
};
bool c8_config_patch(struct c8_server *server, const struct c8_config_op *ops,
	int count);
const char *c8_pattern_name(enum c8_pattern pattern);

/* output.c */
void c8_output_init(struct c8_server *server);
void c8_output_finish(struct c8_server *server);
struct wlr_output *c8_primary_output(struct c8_server *server);
void c8_output_box(struct c8_server *server, struct wlr_box *box);
void c8_schedule_tick(struct c8_server *server);
/* Make the outputs match their [output] sections and [display] project */
void c8_output_apply_config(struct c8_server *server);
const char *c8_project_name(enum c8_project project);
bool c8_project_parse(const char *name, enum c8_project *out);
/* What the screens show now: AUTO resolved, NONE (-1) with one screen */
#define C8_PROJECT_NONE -1
int c8_project_now(struct c8_server *server);
/* The choices the flyout offers now, comma separated; "" with one screen */
void c8_project_choices(struct c8_server *server, char *buf, size_t len);

/* glasses.c: mirrors (Duplicate) and the glasses (XR) */
void c8_xr_init(struct c8_server *server);
void c8_xr_finish(struct c8_server *server);
/* A window closing in XR: its last picture shrinks and fades away where
 * it was (glasses.c keeps its buffers that long). Before its geometry
 * goes. */
void c8_xr_window_closing(struct c8_view *view);
/* Make the virtual output and the roles match the Project choice */
void c8_xr_update(struct c8_server *server);
/* The shell's screen to the size it should be now ([glasses] screen,
 * wider while Start is up) */
void c8_xr_shell_size(struct c8_server *server);
/* A frame of an output whose picture others show / of one that shows it */
void c8_xr_source_frame(struct c8_output *output);
void c8_xr_sink_frame(struct c8_output *output);
/* What the glasses show now, for a screenshot; NULL if not in XR */
cairo_surface_t *c8_xr_screenshot(struct c8_server *server);
cairo_surface_t *c8_buffer_read(struct c8_server *server,
	struct wlr_buffer *buffer, int width, int height); // screenshot.c
void c8_xr_output_gone(struct c8_output *output);
/* The shell's screen in XR, from our own headless backend */
bool c8_xr_is_virtual(struct c8_server *server, struct wlr_output *output);
bool c8_xr_is_view(struct c8_server *server, struct wlr_output *output);
void c8_xr_headset_named(struct c8_server *server, const char *name);
/* screenshot.c: a buffer to draw a capture into, linear where it can be */
struct wlr_buffer *c8_capture_buffer(struct c8_server *server, int w, int h);
void c8_xr_view_needs_frame(struct wl_listener *listener, void *data);
void c8_xr_recentre(struct c8_server *server);
/* From the tracker: it came or went; settled = a fresh fix */
void c8_xr_tracking_changed(struct c8_server *server, bool settled);

/* hologram.c: windows as slates in XR */
bool c8_xr_active(struct c8_server *server);
/* fullscreen: it's the theatre (asked for fullscreen, or Maximise) */
bool c8_holo_window_box(struct c8_view *view, struct wlr_box *box,
	bool *fullscreen);
/* A window was picked (the switcher, a tile, a toast, activation): out
 * of view, it flies to where you look */
void c8_holo_summon(struct c8_view *view);
/* The shell's screen as drawn (pose and a pixel's metres) */
struct pose c8_holo_shell_pose(struct c8_server *server, double *m);
/* The window shown as the theatre, focused and not under Start */
struct c8_view *c8_holo_theatre(struct c8_server *server);
bool c8_holo_area(struct c8_view *view, struct wlr_box *box);
struct pose c8_holo_pose(struct c8_view *view);
void c8_holo_view_gone(struct c8_view *view);
void c8_holo_recentred(struct c8_server *server);
void c8_holo_start_shown(struct c8_server *server);
void c8_holo_start_hidden(struct c8_server *server);
bool c8_holo_splash_box(struct c8_server *server, struct wlr_box *box);
void c8_holo_splash_shown(struct c8_server *server, bool shown);
bool c8_holo_bar_box(struct c8_server *server, struct wlr_box *box);
bool c8_holo_hud_box(struct c8_server *server, struct wlr_box *box);
struct pose c8_holo_hud_pose(struct c8_server *server, double *m);
void c8_holo_view_mapped(struct c8_view *view);
bool c8_holo_beacon(struct c8_view *view, double *yaw, double *pitch,
	float *alpha);
/* Minimised: a window (and its dialogs) put away with its slate bar's
 * button, until it's picked again from anywhere (c8_focus_view restores
 * it). Each shows as its icon in a row over the charms bar. */
void c8_holo_minimise(struct c8_view *root);
void c8_holo_restore(struct c8_view *view);
void c8_holo_restore_all(struct c8_server *server);
bool c8_holo_minimised(struct c8_view *view);
#define C8_MINI_MAX 24 // icons in the row
#define C8_MINI_PX 64 // an icon's tile, in the bar's pixels
#define C8_MINI_GAP 8 // between them
#define C8_MINI_ABOVE 12 // from the top of the bar's surface
/* Those windows, the first minimised first; how many */
int c8_holo_minimised_views(struct c8_server *server, struct c8_view **views,
	int max);
/* The middle of icon i of n, pixels right of the bar's middle */
static inline double c8_mini_x(int i, int n) {
	return (i - (n - 1) / 2.0) * (C8_MINI_PX + C8_MINI_GAP);
}
/* A slate pixel of the bar, which is nearer than the windows */
double c8_holo_bar_m(struct c8_server *server);
/* The charms binding: true when the glasses took it */
bool c8_holo_charms(struct c8_server *server);
/* The bar took the keyboard (the switcher is on it) or let it go */
void c8_holo_bar_grab(struct c8_server *server, bool grab);
/* How far the bar is from your eyes, m: within reach, nearer than any
 * window */
#define C8_BAR_DIST 0.8
/* How much the bar curves round you: the radius in m. As far as the bar
 * is, so its axis runs through your eyes and every part of it is as
 * near and faces you; at 1.2 m its ends came only 5 cm nearer and it
 * looked flat */
#define C8_BAR_CURVE_R C8_BAR_DIST
bool c8_holo_pointer_motion(struct c8_server *server, double dx, double dy,
	uint32_t time);
bool c8_holo_button(struct c8_server *server, uint32_t button, bool pressed);
void c8_holo_button_done(struct c8_server *server);
bool c8_holo_axis(struct c8_server *server, double delta);
/* On a window's slate or nowhere: no cursor image, glasses.c draws a dot */
bool c8_holo_cursor_hidden(struct c8_server *server);
void c8_holo_frame(struct c8_server *server, struct pose head,
	double m_per_px, double fov_x, double fov_y);

/* slatebar.c: the controls under a window's slate in XR */
/* How wide the row of tiles is under a window w wide */
int c8_slatebar_width(int w);
enum c8_slatebar_part c8_slatebar_part_at(int w, int h, double px,
	double py);
struct wlr_texture *c8_slatebar_texture(struct c8_view *view,
	const struct c8_slatebar_look *look);
void c8_slatebar_drop(struct c8_view *view);
#define C8_BEACON_PX 72 // a beacon's icon tile, in slate pixels
struct wlr_texture *c8_slatebar_beacon(struct c8_view *view);
/* A line of text on a dark tile (a minimised window's title over its
 * icon); one is kept, made again when the text changes */
struct wlr_texture *c8_slatebar_label(struct c8_server *server,
	const char *text);
void c8_slatebar_label_drop(struct c8_server *server);
void c8_slatebar_beacon_drop(struct c8_view *view);
/* hologram.c: whether a window shows one, and how it looks */
bool c8_holo_chrome_shown(struct c8_view *view);
struct c8_slatebar_look c8_holo_chrome_look(struct c8_view *view);

/* placement.c: where each app's window was in XR, kept in
 * ~/.local/state/cont8nuum/holograms.ini by app id */
struct c8_placement {
	bool has_pose; // else only its 3D layout is known
	double yaw, pitch, distance; // radians, metres, in the recentred world
	int width, height;
	bool follow;
	int stereo; // view->holo.stereo
};
bool c8_placement_get(const char *app_id, struct c8_placement *out);
void c8_placement_put(struct c8_server *server, const char *app_id,
	const struct c8_placement *p);
/* Its 3D layout only, keeping where it was (if known) */
void c8_placement_put_stereo(struct c8_server *server, const char *app_id,
	int stereo);
void c8_placement_flush(void);

/* tracker.c: where the head points */
struct c8_tracker;
struct c8_tracker *c8_tracker_create(struct c8_server *server);
void c8_tracker_destroy(struct c8_tracker *tracker);
/* The head's pose at time (CLOCK_MONOTONIC ns), predicted when ahead;
 * false while there's no tracking (the pose is then the identity) */
/* Takes in what the sensors sent since last time: before a frame's
 * poses are asked for. Tracking may come or go from here
 * (c8_xr_tracking_changed), so not inside a render pass. */
void c8_tracker_update(struct c8_tracker *tracker);
bool c8_tracker_pose(struct c8_tracker *tracker, int64_t when_ns,
	struct pose *out);
/* Tracking moves the picture all the time: frames go on without damage */
/* While glasses are in XR: look for their sensors */
void c8_tracker_set_wanted(struct c8_tracker *tracker, bool wanted);
/* The head's pose from elsewhere (an OpenXR runtime's, xrclient.c),
 * given as is; NULL goes back to the IMU */
void c8_tracker_set_external(struct c8_tracker *tracker,
	const struct pose *pose);

/* imu.c: the glasses' motion sensors, fused (3DoF): see imu.h */

/* session.c */
void c8_session_start(struct c8_server *server);
void c8_session_stop(struct c8_server *server);
/* Passes the named variables (space-separated) on to D-Bus and systemd */
void c8_session_env_changed(struct c8_server *server, const char *vars);
void c8_idle_init(struct c8_server *server);
void c8_idle_finish(struct c8_server *server);
void c8_idle_activity(struct c8_server *server);
void c8_idle_configure(struct c8_server *server);

/* screenshot.c */
void c8_screenshot(struct c8_server *server, enum c8_action_type action);
cairo_surface_t *c8_screenshot_capture(struct c8_server *server,
	struct wlr_box *area, double *scale);
cairo_surface_t *c8_screenshot_crop(cairo_surface_t *full, struct wlr_box area,
	double scale, struct wlr_box box);
/* Takes image: to the clipboard, with a toast that can save it */
void c8_screenshot_snipped(struct c8_server *server, cairo_surface_t *image);
/* The window's own surfaces scaled to fit max_w x max_h (no larger than
 * its logical size) as premultiplied ARGB8888, rows of 4 x *width bytes */
bool c8_view_capture(struct c8_view *view, int max_w, int max_h, void *data,
	int *width, int *height);

/* snip.c */
void c8_snip_start(struct c8_server *server);
void c8_snip_cancel(struct c8_server *server);
bool c8_snip_active(struct c8_server *server);
/* While active these get the pointer and key presses */
const char *c8_snip_motion(struct c8_server *server, double x, double y);
/* Also eats the release of the press that finished it */
bool c8_snip_button(struct c8_server *server, double x, double y,
	uint32_t button, bool pressed);
void c8_snip_key(struct c8_server *server, xkb_keysym_t sym);

/* protocols.c */
/* nightlight.c */
#define C8_NIGHT_MIN_K 1500
#define C8_NIGHT_MAX_K 6000
void c8_nightlight_init(struct c8_server *server);
void c8_nightlight_finish(struct c8_server *server);
void c8_nightlight_configure(struct c8_server *server);
void c8_nightlight_outputs(struct c8_server *server);
void c8_nightlight_gamma_client(struct c8_server *server,
	struct wlr_output *wlr_output, bool has);
bool c8_nightlight_on(struct c8_server *server);
void c8_nightlight_set(struct c8_server *server, bool on);
void c8_nightlight_toggle(struct c8_server *server);
const char *c8_night_schedule_name(enum c8_night_schedule schedule);
bool c8_night_schedule_parse(const char *s, enum c8_night_schedule *out);
bool c8_parse_time(const char *s, int *minutes);

void c8_protocols_init(struct c8_server *server);
void c8_protocols_finish(struct c8_server *server);

/* lease.c: DRM leasing for VR runtimes */
void c8_lease_init(struct c8_server *server);
void c8_lease_finish(struct c8_server *server);
/* A headset (non-desktop) display: offered, never shown by us */
bool c8_lease_offer(struct c8_server *server, struct wlr_output *output);
bool c8_lease_global_visible(struct c8_server *server,
	const struct wl_client *client, const struct wl_global *global);

/* xrruntime.c: Monado drives the glasses, c8 is an overlay in it */
struct c8_xr_monado;
void c8_xr_runtime_init(struct c8_server *server);
void c8_xr_runtime_finish(struct c8_server *server);
/* From c8_xr_update: true while Monado has the glasses (offered, or
 * leased: glasses NULL), and then nothing of ours draws them */
bool c8_xr_runtime_update(struct c8_server *server, bool xr,
	struct c8_output *glasses);
void c8_xr_runtime_output_gone(struct c8_server *server,
	struct wlr_output *output);
bool c8_xr_runtime_is_client(struct c8_server *server,
	const struct wl_client *client);
bool c8_xr_runtime_has_client(struct c8_server *server);
/* To Monado's renderer (our Monado commit): 'S' frames until 's',
 * '1' one frame. False: no Monado, or one without it. Frames come to
 * c8_xr_share_frame (glasses.c), R8G8B8X8 */
bool c8_xr_runtime_share(struct c8_server *server, char cmd);
void c8_xr_share_frame(struct c8_server *server, const uint8_t *pixels,
	int width, int height, int stride);
/* screenshot.c: a screenshot's second half, with its picture (XR: from
 * Monado, which comes later) */
void c8_screenshot_finish(struct c8_server *server, int action,
	cairo_surface_t *image, bool xr);
/* glassesmode.c: their own 3D mode on (was_2d: only if it was off) or
 * back off, if we turned it on */
void c8_glasses_set_sbs(struct c8_server *server, bool sbs, bool was_2d);

/* xrclient.c: the OpenXR overlay session */
struct c8_xrclient;
struct c8_xrclient *c8_xrclient_create(struct c8_server *server);
void c8_xrclient_destroy(struct c8_xrclient *xr);
bool c8_xrclient_lost(struct c8_xrclient *xr); // ended: make another
bool c8_xrclient_ready(struct c8_xrclient *xr); // connected, session made
/* One eye of a runtime's frame: its pose in the runtime's space and the
 * angles to its edges (radians, left and down negative) */
struct c8_xr_view {
	struct pose pose;
	double left, right, up, down;
};
/* glasses.c: draws them into the framebuffer bound, side by side with
 * gap clear columns between; their fields are overscan times what the
 * glasses show (tangents) */
/* Returns whether what is clear in it is to show the background from
 * the runtime's layers (sky_layer: it has them, c8_xr_sky) */
/* Windows and the shell as the runtime's layers, back to front: begin
 * binds a framebuffer w x h for key (a window, Start, ...): C8_XR_DRAW,
 * or C8_XR_SAME (the image drawn last time for that sig still stands:
 * nothing bound, nothing to draw; sig 0 never is), or C8_XR_NONE (draw
 * it with the rest). end gives the slate's middle (c8's world) and size
 * in metres. curve > 0: the image is the slate unrolled, to bend round
 * an upright cylinder of that radius in front of its middle (a cylinder
 * layer); a runtime without them says no in begin. */
enum c8_xr_slate_begin { C8_XR_NONE, C8_XR_DRAW, C8_XR_SAME };
struct c8_xr_slates {
	void *data;
	enum c8_xr_slate_begin (*begin)(void *data, const void *key, int w,
		int h, double curve, uint64_t sig);
	void (*end)(void *data, const void *key, struct pose middle,
		double w_m, double h_m, double curve);
};
bool c8_xr_draw_views(struct c8_server *server,
	const struct c8_xr_view views[2], int view_w, int view_h, int gap,
	double overscan, bool over_game, bool sky_layer,
	const struct c8_xr_slates *slates, bool *drawn);
/* The background as equirectangular layers: the picture w x h, a band
 * from half (radians) above the horizon to half below; fill: the edge's
 * colours as a whole sphere behind it. A new serial: draw them again. */
struct c8_xr_sky {
	unsigned serial;
	int w, h;
	double half;
	bool fill;
};
bool c8_xr_sky(struct c8_server *server, struct c8_xr_sky *out);
void c8_xr_sky_draw(struct c8_server *server, bool fill, int w, int h);
float c8_xr_sky_veil(struct c8_server *server);
void c8_xr_windows_done(struct c8_server *server);
void c8_xr_dump_runtime(struct c8_server *server);

/* stereo.c: cont8nuum-stereo-v1. The numbers are the protocol's. */
struct c8_stereo_surface {
	struct wl_list link; // c8_server.stereo.surfaces
	struct c8_server *server;
	struct wl_resource *resource;
	struct wlr_surface *surface; // NULL once it's gone
	struct wl_listener surface_destroy;
	uint32_t layout; // 0 mono, 1 side by side, 2 top and bottom
	bool right_first;
	uint32_t projection; // 0 flat, 1 sphere, 2 half sphere
};
#define C8_STEREO_SHOWS_STEREO 1
#define C8_STEREO_SHOWS_SURROUND 2
void c8_stereo_init(struct c8_server *server);
struct c8_stereo_surface *c8_stereo_for(struct c8_server *server,
	struct wlr_surface *surface);
/* What the glasses do with such surfaces now: the clients are told */
void c8_stereo_shows(struct c8_server *server, uint32_t shows);
/* The sphere to put round the viewer, and the window it is of */
struct c8_stereo_surface *c8_stereo_surround(struct c8_server *server,
	struct c8_view **view);
/* A token for something the user launched, so an app already running
 * that gets handed the launch can bring its window forward */
const char *c8_launch_token(struct c8_server *server);
void c8_toplevel_mapped(struct c8_view *view);
void c8_toplevel_unmapped(struct c8_view *view);
void c8_toplevel_changed(struct c8_view *view);
void c8_toplevels_focus_changed(struct c8_server *server);

/* xdgoutput.c: xdg-output, also for outputs out of the layout (the
 * glasses in XR, shown only to the screen-sharing portal) */
void c8_xdg_output_init(struct c8_server *server);
bool c8_output_global_visible(struct c8_server *server,
	const struct wl_client *client, const struct wl_global *global);

/* lock.c */
void c8_lock_init(struct c8_server *server);
void c8_lock_finish(struct c8_server *server);
/* Starts the locker, unless locked or locking already */
void c8_lock(struct c8_server *server);
bool c8_locked(struct c8_server *server);
bool c8_lock_is_client(struct c8_server *server,
	const struct wl_client *client);
struct wlr_surface *c8_lock_keyboard_surface(struct c8_server *server);
bool c8_lock_owns_surface(struct c8_server *server,
	struct wlr_surface *surface);
void c8_lock_output_changed(struct c8_server *server);
void c8_lock_output_frame(struct c8_server *server, struct c8_output *output);

/* settings.c */
void c8_settings_init(struct c8_server *server);
bool c8_settings_is_launch(const char *cmd);
void c8_settings_launch(struct c8_server *server, const char *cmd);
/* Starts c8-sync (a real session only, or with C8_SYNC set) */
void c8_sync_start(struct c8_server *server);
/* An appearance setting from the shell: saved, applied, passed on */
/* Change one config.ini value as PC settings would (checked, saved,
 * applied, told to PC settings) */
bool c8_settings_set(struct c8_server *server, const char *section,
	const char *key, const char *value);
bool c8_settings_set_appearance(struct c8_server *server, const char *key,
	const char *value);

/* input.c */
void c8_input_init(struct c8_server *server);
void c8_input_finish(struct c8_server *server);
void c8_launch(struct c8_server *server, const char *cmd);
void c8_input_configure_keyboards(struct c8_server *server);
void c8_input_configure_pointers(struct c8_server *server);
void c8_cursor_rebase(struct c8_server *server);
/* [general] cursor_theme/cursor_size changed: reload and pass them on */
void c8_cursor_theme_changed(struct c8_server *server);
/* The surface under a layout point, NULL for none or the compositor's UI */
/* Anything of ours or a client's at a layout point */
bool c8_node_exists_at(struct c8_server *server, double lx, double ly);
struct wlr_surface *c8_surface_at(struct c8_server *server, double lx,
	double ly, double *sx, double *sy, struct c8_node_desc **desc_out);
/* The cursor moved: hover, hot corners (if moved), chrome, focus */
void c8_pointer_motion(struct c8_server *server, uint32_t time, bool moved);
void c8_pointer_button(struct c8_server *server, uint32_t time_msec,
	uint32_t button, enum wl_pointer_button_state state);

/* drag.c */
void c8_drag_init(struct c8_server *server);
void c8_drag_finish(struct c8_server *server);
/* Puts the drag icon at a layout point */
void c8_drag_move(struct c8_server *server, double lx, double ly);
/* A drag is carried by the pointer */
bool c8_drag_pointer(struct c8_server *server);
/* A finger moved: true if it carries the drag (and was handled) */
bool c8_drag_touch_motion(struct c8_server *server, uint32_t time, int32_t id,
	double lx, double ly);

/* im.c: input methods (fcitx5, an on-screen keyboard) for apps */
struct wlr_keyboard_key_event;
void c8_im_init(struct c8_server *server);
void c8_im_finish(struct c8_server *server);
void c8_im_focus(struct c8_server *server, struct wlr_surface *surface);
bool c8_im_key(struct c8_server *server, struct wlr_keyboard *kb,
	struct wlr_keyboard_key_event *event);
bool c8_im_modifiers(struct c8_server *server, struct wlr_keyboard *kb);

/* pointer.c */
struct wlr_pointer_motion_event;
void c8_pointer_init(struct c8_server *server);
void c8_pointer_finish(struct c8_server *server);
void c8_pointer_constraint_update(struct c8_server *server,
	struct wlr_surface *surface);
bool c8_pointer_relative(struct c8_server *server,
	struct wlr_pointer_motion_event *event, double *dx, double *dy);

/* touch.c */
void c8_touch_init(struct c8_server *server);
void c8_touch_finish(struct c8_server *server);
/* Puts touchscreens and pens on the built-in screen; after hotplug */
void c8_touch_map(struct c8_server *server);
void c8_run_action(struct c8_server *server, enum c8_action_type action,
	const char *arg);

/* view.c */
void c8_view_init(struct c8_server *server);
void c8_view_finish(struct c8_server *server);
void c8_view_snap_to(struct c8_view *view, int side, double fraction);
void c8_view_set_split(struct c8_server *server, double lx);
/* Move the split between two apps a quarter of the width left (-1) or
 * right (1); at either end the app left with it all is alone, as unsnap */
void c8_view_split_step(struct c8_server *server, int direction);
int c8_view_snap_min_width(void);

/* tween.c */
void c8_tween_init(struct c8_server *server);
/* The flat layout just arranged: the tiled apps (one or two) and their
 * boxes, the divider (empty without one) and whether an app is
 * snapped. Moves the apps from the last layout to this one when
 * something moved; true while they move (the focus border waits) */
bool c8_tween_layout(struct c8_server *server, struct c8_view **views,
	const struct wlr_box *boxes, int count, const struct wlr_box *divider,
	bool split, struct c8_view *hidden);
/* After arranging's stacking: the pictures over the apps, and the apps
 * they stand for out of sight */
void c8_tween_restack(struct c8_server *server);
/* The next layout moves view from where it was let go (from) */
void c8_tween_drop(struct c8_server *server, struct c8_view *view,
	const struct wlr_box *from);
/* view stops being shown mid-move (the shell's pick opened over it) */
void c8_tween_hide(struct c8_server *server, struct c8_view *view);
/* Puts the apps where they go at once and forgets the last layout */
void c8_tween_stop(struct c8_server *server);
void c8_tween_view_gone(struct c8_view *view);
void c8_view_minimize(struct c8_view *view);
struct c8_view *c8_view_group_latest(struct c8_view *view);
void c8_view_cycle_app(struct c8_server *server, int direction);
struct c8_view *c8_tiled_view_at(struct c8_server *server, double lx, double ly);

/* chrome.c */
void c8_chrome_init(struct c8_server *server);
bool c8_chrome_grabbing(struct c8_server *server);
/* Each returns whether the chrome took the pointer; *cursor is then the
 * cursor image to show */
bool c8_chrome_pointer_motion(struct c8_server *server, double x, double y,
	const char **cursor);
bool c8_chrome_pointer_button(struct c8_server *server, double x, double y,
	uint32_t button, bool pressed);
void c8_chrome_hide(struct c8_server *server);
void c8_chrome_view_gone(struct c8_server *server, struct c8_view *view);
/* Its colour or icon is other than what the bar was drawn with */
void c8_chrome_view_restyled(struct c8_server *server, struct c8_view *view);
/* Touch: whether a swipe down at x would pull a bar down; pull it down and
 * keep it; carry its app off by the finger; a touch at x, y closes a kept
 * bar unless it is on it */
bool c8_chrome_can_pull(struct c8_server *server, double x);
bool c8_chrome_pull(struct c8_server *server, double x);
bool c8_chrome_pull_drag(struct c8_server *server, double x, double y);
void c8_chrome_touch_at(struct c8_server *server, double x, double y);
void c8_views_arrange(struct c8_server *server);
void c8_focus_view(struct c8_view *view);
/* Focus for a window picked from elsewhere (a switcher, a toast, another
 * app): in XR it also comes to where you look. */
void c8_view_activate(struct c8_view *view);
/* Focus for a press on it: nothing to do when it's in front already */
void c8_view_raise(struct c8_view *view);
void c8_focus_restore(struct c8_server *server);
struct c8_view *c8_top_view(struct c8_server *server);
void c8_view_close(struct c8_view *view);
void c8_view_snap(struct c8_server *server, int side);
void c8_view_mapped(struct c8_view *view);
void c8_view_unmapped(struct c8_view *view);
void c8_view_props_changed(struct c8_view *view);
const char *c8_view_title(struct c8_view *view);
const char *c8_view_app_id(struct c8_view *view);
const char *c8_view_label(struct c8_view *view); // the title, else the app id
void c8_view_title_free(struct c8_view *view);
void c8_views_stealth_changed(struct c8_server *server);
struct c8_view *c8_view_parent(struct c8_view *view);
/* The window a dialog (or a dialog of a dialog) belongs to */
struct c8_view *c8_view_root(struct c8_view *view);
void c8_view_keyboard_enter(struct c8_server *server, struct wlr_surface *surface);

/* xwayland.c */
void c8_xwayland_init(struct c8_server *server);
void c8_xwayland_finish(struct c8_server *server);
/* The root window's cursor, from the current cursor theme */
void c8_xwayland_set_cursor(struct c8_server *server);

/* layer.c */
void c8_layer_init(struct c8_server *server);
void c8_layer_finish(struct c8_server *server);
void c8_layers_arrange(struct c8_server *server);

/* shell.c */
void c8_shell_init(struct c8_server *server);
void c8_shell_finish(struct c8_server *server);
/* Runs c8-shell; once the environment for apps is set up */
void c8_shell_start(struct c8_server *server);
bool c8_shell_is_client(struct c8_server *server, const struct wl_client *client);
/* Start, as the compositor decides it */
void c8_start_show(struct c8_server *server, bool show);
void c8_shell_action(struct c8_server *server, uint32_t action);
void c8_shell_switcher(struct c8_server *server, int direction, uint32_t mods);
/* After arranging: tells the shell when an app is snapped and others
 * could be picked for the rest of the screen */
void c8_shell_snap_changed(struct c8_server *server);
/* A shell panel is up: hot corners and chrome wait */
bool c8_shell_panel_open(struct c8_server *server);
/* A press outside the panels closes them; true if it was taken */
bool c8_shell_dismiss(struct c8_server *server, struct wlr_surface *pressed);
/* The surface the shell wants the keyboard on, or NULL */
struct wlr_surface *c8_shell_keyboard_surface(struct c8_server *server);
void c8_shell_appearance_changed(struct c8_server *server);
void c8_shell_power_changed(struct c8_server *server);
void c8_shell_output_changed(struct c8_server *server);
/* The launch splash while it shows, for the glasses to draw */
struct c8_shell_surface *c8_shell_splash(struct c8_server *server);
struct c8_shell_surface *c8_shell_bar(struct c8_server *server);
/* A toast or the level box that shows */
bool c8_shell_osd_shown(struct c8_shell_surface *ss);
void c8_shell_bar_dismiss(struct c8_server *server);
void c8_shell_window_mapped(struct c8_view *view);
void c8_shell_window_unmapped(struct c8_view *view);
void c8_shell_window_changed(struct c8_view *view);
void c8_shell_focus_changed(struct c8_server *server);
void c8_shell_locked_changed(struct c8_server *server);
/* The tile glyph on the title bar, if the shell gave the window one */
void c8_view_style_draw_icon(struct c8_view *view, cairo_t *cr, double x,
	double y, double size);

#endif
