#ifndef C8_SHELL_H
#define C8_SHELL_H

#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-wayland.h>
#include "cont8nuum-shell-v1-client-protocol.h"
#include "stealth.h" // stealth mode: what is shown of the user and the PC

/* An open app window, as the compositor tells us */
struct window {
	struct wl_list link; // shell.windows, most recently used first
	struct c8_shell_window_v1 *proto;
	char *title, *app_id;
	uint32_t pid; // 0 if not known
	bool focused;
	struct c8ui_icon *icon; // for the switcher, loaded when first shown
	bool icon_tried;
	/* The switcher's picture of it, asked for each time it opens */
	cairo_surface_t *preview;
	int capture_fd; // -1, or the picture on its way
	uint32_t capture_serial;
	/* The tile look last sent for the title bar */
	bool styled;
	uint32_t style_rgb;
	char *style_icon;
};

/* main.c: asks c8 for a picture of the window, at most max_w x max_h
 * logical pixels; panels_preview_ready says when w->preview is new */
void window_capture(struct window *w, int max_w, int max_h);

struct appearance {
	uint32_t accent, background;
	char *pattern;
	bool more_tiles, live_tiles;
	char *font, *icon_theme;
	char *notification_corner;
	char *clock; // auto, 12 or 24
	/* Second screen: what's on and what can be picked (project.c) */
	char *project, *project_choices;
	bool night_light; // warming the screens now
	/* In the glasses: Start floats see-through, a launch is a slate */
	bool xr;
};

struct shell {
	struct c8ui_app *app;
	struct c8_shell_v1 *proto;
	bool start_visible;
	struct appearance appearance;
	uint32_t power; // enum c8_shell_v1_power
	struct wl_list windows;
	int output_width, output_height; // from Start's configure
	char *config_dir; // where start.ini lives
	bool respawned; // tile providers are already running
	bool locked; // the lock screen is up
};

extern struct shell shell;

/* surface.c: a c8ui window with a shell role */
struct shell_surface;
typedef void (*shell_surface_fn)(struct shell_surface *surface, void *data);
struct shell_surface *shell_surface_new(enum c8_shell_v1_role role);
void shell_surface_destroy(struct shell_surface *surface);
struct c8ui_root *shell_surface_root(struct shell_surface *surface);
struct c8ui_window *shell_surface_window(struct shell_surface *surface);
/* Panels: where, in output coordinates; applied with the next commit */
void shell_surface_set_rect(struct shell_surface *surface, int x, int y,
	int width, int height);
void shell_surface_grab_keyboard(struct shell_surface *surface, bool grab);
void shell_surface_on_dismiss(struct shell_surface *surface,
	shell_surface_fn fn, void *data);
/* Hides it until the next configure; panels show again on set_rect */
void shell_surface_hide(struct shell_surface *surface);
/* Full-output roles: shows it again at the size last configured */
void shell_surface_show(struct shell_surface *surface);

/* tiles.c: the pinned tiles, their groups, start.ini and live content */

#define TILE_GAP 8
#define START_MARGIN_X 116
#define START_TILES_Y 172
#define GROUP_GAP 72
#define BAND_CELLS 4
#define APPBAR_HEIGHT 88

enum tile_size {
	TILE_SMALL,
	TILE_MEDIUM,
	TILE_WIDE,
	TILE_LARGE,
};

struct rect {
	int x, y, width, height;
};

/* The Metro palette: a tile's colours, and the accents the compositor
 * offers */
static const uint32_t metro_palette[] = {
	0xa4c400, 0x60a917, 0x008a00, 0x00aba9, 0x1ba1e2,
	0x0050ef, 0x6a00ff, 0xaa00ff, 0xf472d0, 0xd80073,
	0xa20025, 0xe51400, 0xfa6800, 0xf0a30a, 0xe3c800,
	0x825a2c, 0x6d8764, 0x647687, 0x76608a, 0x87794e,
};
#define METRO_PALETTE_LEN \
	((int)(sizeof(metro_palette) / sizeof(metro_palette[0])))

static inline int64_t now_ms(void) {
	return g_get_monotonic_time() / 1000;
}

static inline double ease_out(double t) {
	t = t < 0 ? 0 : t > 1 ? 1 : t;
	return 1 - (1 - t) * (1 - t) * (1 - t);
}

static inline double ease_in_out(double t) {
	t = t < 0 ? 0 : t > 1 ? 1 : t;
	return t < 0.5 ? 4 * t * t * t : 1 - pow(-2 * t + 2, 3) / 2;
}

static inline bool in_rect(const struct rect *r, double x, double y) {
	return x >= r->x && y >= r->y && x < r->x + r->width &&
		y < r->y + r->height;
}

/* Replaces *dst with a copy of src; empty counts as none */
static inline void set_str(char **dst, const char *src) {
	free(*dst);
	*dst = src && src[0] ? strdup(src) : NULL;
}

struct group;

struct tile {
	struct wl_list link; // group.tiles
	struct group *group;
	char *id, *name, *exec, *icon, *provider;
	enum tile_size size;
	uint32_t color;
	bool has_color;
	bool live_enabled, label;
	/* Made by an app's pin request (live.c): it may take it off again */
	bool secondary;
	struct c8ui_icon *icon_image;
	bool icon_loaded;
	/* A picture filling the face instead of the colour and icon
	 * (image = in start.ini; wide_image = for the wide size) */
	char *image, *wide_image;
	cairo_surface_t *image_s, *wide_s;
	bool image_loaded, wide_loaded;

	/* Cell in the group's grid, from the last layout */
	int col, row;
	/* An old start.ini's cell, for ordering it when loaded */
	int pos_col, pos_row;
	bool has_pos;

	struct rect box; // layout target, in page coordinates
	double cur_x, cur_y; // shown, eased towards box
	bool placed;

	/* Rotating through live content */
	int slide, prev_slide;
	double flip; // 0..1, 1 = settled
	/* How many slides it has, kept until what it comes from changes
	 * (tile_slide_count) */
	int slides;
	unsigned slides_stamp;
	/* The two slides of a flip, drawn once and moved after */
	cairo_surface_t *flip_from, *flip_to;
	bool flip_moved; // only the flip went on: the face needs putting together again
	int64_t flip_start, next_rotate;

	bool selected, focused, pressed;
	struct c8ui_tilt tilt; // sinks in while pressed: press_tile in start.c
	double alpha;

	/* The face, drawn once per change */
	cairo_surface_t *face;
	int face_w, face_h;
	double face_scale;
	bool face_dirty;
};

struct group {
	struct wl_list link; // tile_groups
	char *name;
	struct wl_list tiles;
	int x, width;
};

extern struct wl_list tile_groups;

#define for_each_tile(group, tile) \
	wl_list_for_each(group, &tile_groups, link) \
		wl_list_for_each(tile, &(group)->tiles, link)

void tiles_load(void);
void tiles_save(void);
void tiles_launch_providers(void);
struct group *group_create(const char *name);
/* Before next, or last if next is NULL */
struct group *group_create_before(struct group *next, const char *name);
void group_set_name(struct group *group, const char *name);
void groups_prune(void);
struct tile *tile_find(const char *id);
/* The tile whose command is exactly exec, or NULL */
struct tile *tile_find_exec(const char *exec);
struct tile *tile_create(struct group *group, const char *id);
void tile_destroy(struct tile *tile);
void tile_size_cells(enum tile_size size, int *w, int *h);
int tile_cell(void);
uint32_t tile_color(struct tile *tile);
bool tile_is_live(struct tile *tile);
/* Lays the groups out side by side from START_MARGIN_X, the tiles of
 * each flowing in order. Returns the page width. */
int tiles_layout(int rows);
/* Where in group's order tile would land nearest to left, top (page
 * coordinates), as laid out last */
int tiles_drop_index(struct group *group, struct tile *tile, int rows,
	double left, double top);
/* Into group at index (the end if past it) */
void tile_move(struct tile *tile, struct group *group, int index);
void tile_draw(cairo_t *cr, struct c8ui_widget *w, struct tile *tile,
	double x, double y);
int tile_slide_count(struct tile *tile);
void tile_reset_rotation(struct tile *tile, int64_t now, int index);
bool tile_tick(struct tile *tile, int64_t now);
void tiles_style_windows(void);
/* The tile for an app id or desktop entry, by id or command */
/* The app's tile: by id, else one running its program (the app id's,
 * or its desktop entry's); NULL if none */
struct tile *tile_for_app(const char *app_id);
/* The Icon of the app's desktop entry, or NULL */
const char *app_entry_icon(const char *app_id);
uint32_t app_color(const char *app_id);
/* Its icon's name: its tile's, its desktop entry's, else the app id */
const char *app_icon_name(const char *app_id);
const char *tile_size_name(enum tile_size size);
/* An app's own tile (live.c's RequestPin, once the user agreed): a new tile
 * in the last group, with a command that carries the app's arguments */
struct tile *tile_pin_secondary(const char *id, const char *name,
	const char *exec, const char *icon, bool has_color, uint32_t color,
	enum tile_size size);

/* live.c: live tile content, from org.cont8nuum.Tiles1 on the session bus */

struct live_update {
	char *tag; // "" for none
	GHashTable *fields; // name -> value
	int64_t expires; // wall-clock seconds, 0 for never
	/* Scheduled ones wait in live.pending: when they start showing, and
	 * how long they then last (expires is worked out from it) */
	int64_t starts; // wall-clock seconds
	long ttl; // seconds, 0 for never
	GHashTable *images; // path or URI -> cairo_surface_t, NULL if unreadable
};

struct live {
	char *id;
	GPtrArray *updates; // struct live_update, oldest first
	GPtrArray *pending; // scheduled for later, soonest first
	char *badge;
};

void live_init(void);
/* wake.c: org.cont8nuum.Wake1, apps started at times they booked */
void wake_init(void);
struct live *live_find(const char *id);
/* The field for this tile size: "wide.text1" before "text1" */
const char *live_field(struct live_update *u, enum tile_size size,
	const char *key);
cairo_surface_t *live_image(struct live_update *u, const char *ref);
/* A picture from an absolute path or file:// URI, NULL if it won't load */
cairo_surface_t *live_load_image(const char *ref);
/* Forget a tile's loaded icon and pictures and draw its face again */
void tile_restyle(struct tile *tile);
/* Tiles were pinned, unpinned or resized: tell the bus */
void live_tiles_changed(void);
/* Tells the tile's provider still running, if any, to go, and starts a
 * new one */
void live_restart_provider(struct tile *tile);
void live_start_visible(void);
/* Writes tiles.ini now, if it is waiting to be */
void live_flush(void);
/* Goes up whenever any tile's updates change */
unsigned live_stamp(void);
void live_activated(struct tile *tile);

/* templates.c: drawing live content, one slide at a time */
bool tile_draw_slide(cairo_t *cr, const char *family, struct tile *tile,
	int index, int w, int h);
/* The name to show on a slide, NULL for none */
const char *tile_label(struct tile *tile, int index);
uint32_t tile_slide_color(struct tile *tile, int index);
/* Slides whose background is Start's (stats) */
bool tile_slide_clear(struct tile *tile, int index);

/* start.c */
void start_init(void);
void start_visible_changed(void);
void start_windows_changed(void);
void start_power_changed(void);
void start_appearance_changed(void);
void start_action(enum c8_shell_v1_action action);
/* A tile's look or live content changed */
void start_tile_changed(struct tile *tile);

/* Start's search, with text typed in already (or NULL) */
void start_open_search(const char *text);
/* Start's side of All apps: closes menus and search before it opens */
void start_open_apps(void);
/* Launches it with the splash growing out of from, on screen */
void start_activate_tile(struct tile *tile, struct rect from);
void start_animate(void);
void start_redraw(void);
/* Start's widget, or NULL before it exists */
struct c8ui_widget *start_widget(void);
/* Pinning from All apps: the new tile is laid out and saved */
void start_pinned(struct tile *tile);
void start_unpin(struct tile *tile);

/* apps.c: All apps, below Start */
struct app;
void apps_init(void);
void apps_resize(int width, int height);
void apps_restyle(void);
void apps_draw(cairo_t *cr, struct c8ui_widget *w);
void apps_draw_arrow(cairo_t *cr, double cx, double cy, bool up);
/* Advances the slide and the scrolling; true while still moving */
bool apps_tick(int64_t now, double dt);
bool apps_active(void);
/* A swipe sideways: the next tab (dir 1) or the one before; false if
 * there is none that way */
bool apps_swipe(int dir);
/* 0 is Start, 1 All apps; in between while sliding */
double apps_progress(void);
bool apps_dragging(void);
void apps_show(bool show);
/* Back to Start at once, for when Start is hidden */
void apps_reset(void);
void apps_rest(void); // let go of the drawn rows
/* Touchpad: the page follows the fingers, then springs into place */
void apps_drag(double delta);
/* The same for a finger on the screen, which may rest without lifting */
void apps_drag_finger(double delta);
void apps_drag_end(void);
void apps_scroll(double delta);
/* A finger pans the list as it moves, then lets it coast at v px/ms */
void apps_pan(double delta);
void apps_fling(double v);
bool apps_press(double x, double y, uint32_t button);
bool apps_key(const struct c8ui_event *ev);

/* The left button let go over the widget: what a click on it is */
static inline bool left_click(struct c8ui_widget *w,
		const struct c8ui_event *ev) {
	return ev->type == C8UI_EVENT_RELEASE &&
		ev->button == C8UI_BUTTON_LEFT &&
		c8ui_rect_contains(&w->rect, ev->x, ev->y);
}

/* A flicked page coasting to a stop: moves *pos by *v px/ms over dt ms,
 * slowing as it goes, and stops at min or max. True while it moves. */
static inline bool fling_step(double *v, double *pos, double dt, double min,
		double max) {
	if (*v == 0) {
		return false;
	}
	*pos += *v * dt;
	*v *= exp(-dt / 325.0);
	if (*pos <= min || *pos >= max) {
		*pos = *pos < min ? min : *pos > max ? max : *pos;
		*v = 0;
	} else if (fabs(*v) < 0.02) {
		*v = 0;
	}
	return *v != 0;
}

/* Start, All apps and search all scroll sideways the same way: the
 * target kept within what there is to show */
static inline void scroll_clamp(double *target, double content,
		double width) {
	double max = content - width;
	*target = *target > max ? max : *target;
	*target = *target < 0 ? 0 : *target;
}

/* And moved just far enough to show box with Start's margin beside it */
static inline void scroll_reveal(double *target, const struct rect *box,
		double content, double width) {
	if (box->x - START_MARGIN_X < *target) {
		*target = box->x - START_MARGIN_X;
	} else if (box->x + box->width + START_MARGIN_X > *target + width) {
		*target = box->x + box->width + START_MARGIN_X - width;
	}
	scroll_clamp(target, content, width);
}

/* A frame of it: a fling coasts on, and the page eases after its target.
 * True while anything moves. */
static inline bool scroll_step(double *fling, double *scroll, double *target,
		double dt, double content, double width) {
	bool moving = false;
	if (fling_step(fling, target, dt, 0, fmax(content - width, 0))) {
		*scroll = *target;
		moving = true;
	}
	double ds = *target - *scroll;
	if (fabs(ds) > 0.5) {
		*scroll += ds * (1 - exp(-dt / 70.0));
		moving = true;
	} else {
		*scroll = *target;
	}
	return moving;
}

/* Moved by delta, which ends a fling; with: the page goes along at once
 * (a finger panning it), else it eases there (a wheel) */
static inline void scroll_by(double *fling, double *scroll, double *target,
		double delta, double content, double width, bool with) {
	*fling = 0;
	*target += delta;
	scroll_clamp(target, content, width);
	if (with) {
		*scroll = *target;
	}
}

/* How far box `to` is from `from` going (dx, dy), for the arrow keys
 * moving the focus: what's along the way counts once, what's off to the
 * side twice. INFINITY when it isn't that way at all. */
static inline double focus_distance(const struct rect *from,
		const struct rect *to, int dx, int dy) {
	double x = (to->x + to->width / 2.0) - (from->x + from->width / 2.0);
	double y = (to->y + to->height / 2.0) - (from->y + from->height / 2.0);
	double along = x * dx + y * dy;
	if (along <= 1) {
		return INFINITY;
	}
	return along + (fabs(x * dy) + fabs(y * dx)) * 2;
}

/* The way an arrow key points */
static inline bool arrow_dir(uint32_t sym, int *dx, int *dy) {
	*dx = sym == XKB_KEY_Left ? -1 : sym == XKB_KEY_Right ? 1 : 0;
	*dy = sym == XKB_KEY_Up ? -1 : sym == XKB_KEY_Down ? 1 : 0;
	return *dx != 0 || *dy != 0;
}

/* An entry as All apps draws it, ENTRY_W by ENTRY_H, at x, y */
#define ENTRY_W 272
#define ENTRY_H 40
#define ENTRY_PITCH 48
void apps_draw_entry(cairo_t *cr, const char *family, struct app *app,
	double x, double y, double alpha);
/* Rescans now if anything was installed or removed */
void apps_rescan(void);
/* Apps (or unpinned live tile widgets) matching a casefolded query, best
 * first. The pointers last until the next rescan. */
int apps_search(const char *query, bool widgets, struct app **out, int max);
bool apps_same(struct app *app, struct tile *tile);
/* Launches an app, or pins a widget; from is where it is on screen */
void apps_activate_at(struct app *app, struct rect from);
/* The command a desktop entry runs, as All apps would (NULL if it can't
 * be shown); the caller frees it */
char *apps_command(const char *desktop_path);

/* defaults.c: the kinds of thing PC settings gives a default app to */
#include "default-apps.h" // the kinds, shared with PC settings
int defaults_count(void);
const struct c8_default_role *defaults_role(int i);
/* Launches a command through the compositor; `@default ROLE` is turned
 * into the default app for that role first */
void shell_launch(const char *exec);

/* search.c: typing on Start or All apps turns it into results */
void search_init(void);
bool search_active(void);
/* text: what was typed, or NULL to open it empty */
void search_begin(const char *text);
void search_end(void);
void search_resize(int width, int height);
void search_restyle(void);
void search_draw(cairo_t *cr, struct c8ui_widget *w, int64_t now);
bool search_tick(int64_t now, double dt);
bool search_key(const struct c8ui_event *ev);
bool search_press(double x, double y, uint32_t button);
/* Dragging out a selection in the search box */
void search_motion(double x, double y);
void search_release(void);
void search_scroll(double delta);
void search_pan(double delta);
void search_fling(double v);
/* How well a casefolded text matches a casefolded query: 0 not at all,
 * 1 inside a word, 2 at the start of a word, 3 at the start, 4 all of it */
int search_score(const char *text, const char *query);
/* Fuzzily, for names: the query's letters in order, gaps allowed. text
 * and query casefolded; orig is the name as written (for camelCase), or
 * NULL. 0 for no match, else higher is better. */
int search_fuzzy(const char *text, const char *orig, const char *query);

/* files.c: the user's files by name, for search */
struct file_hit {
	/* path: until the next search; name: until the index next changes */
	const char *path, *name;
	const char *folder; // files.c's own
	bool dir;
	int depth;
	int64_t used;
	int score;
};
/* changed runs when a new index is in; the old hits are gone by then */
void files_init(void (*changed)(void));
/* Builds it again in the background if it is getting old */
void files_refresh(void);
int files_search(const char *query, struct file_hit *out, int max);

/* splash.c: the launch splash */
void splash_init(void);
void splash_show(struct rect from, uint32_t color, const char *icon);
void splash_end(void);

/* network.c: Wi-Fi networks for the Networks flyout */
struct wifi_net {
	char *ssid;
	char *ap; // the access point used
	char *conn; // its saved connection, or NULL
	int strength; // 0..100
	bool active, secured, enterprise, sae, wep;
};
typedef void (*network_fn)(void *data);
typedef void (*network_result_fn)(bool ok, const char *error, void *data);
/* fn runs when the list changed; it scans now and then while watched */
void network_watch(network_fn fn, void *data);
void network_unwatch(void);
const struct wifi_net *network_list(int *count, bool *enabled,
	bool *has_device);
void network_set_wifi(bool on);
/* key: NULL to use what's saved */
void network_connect(const struct wifi_net *net, const char *key,
	bool autoconnect, network_result_fn fn, void *data);
void network_cancel(void);
void network_disconnect(void);

/* media.c: volume, brightness and player keys, and the level box */
void media_init(void);
void media_action(enum c8_shell_v1_action action);
/* Levels are 0..100 */
typedef void (*media_level_fn)(int level, bool muted, void *data);
void media_volume_get(media_level_fn fn, void *data);
void media_volume_set(int level);
void media_volume_mute(bool muted);
/* The apps playing sound (pactl; none without it), an app's streams as
 * one */
#define MEDIA_APPS_MAX 32
#define MEDIA_APP_STREAMS 16
struct media_app {
	char name[64]; // as it calls itself
	char app_id[128], icon[128], binary[64]; // where its icon may be; or ""
	int level; // of its loudest stream
	bool muted; // all of them
	unsigned streams[MEDIA_APP_STREAMS];
	int nstreams;
};
typedef void (*media_apps_fn)(const struct media_app *apps, int count,
	void *data);
void media_apps_get(media_apps_fn fn, void *data);
void media_app_set(const struct media_app *app, int level); // unmutes
void media_app_mute(const struct media_app *app, bool muted);
int media_brightness_get(void); // -1: no backlight
void media_brightness_set(int level);
/* The level box's icons, about 24 px, around cx, cy */
void media_draw_speaker(cairo_t *cr, double cx, double cy, bool muted);
void media_draw_sun(cairo_t *cr, double cx, double cy);

/* panels.c: charms, clock, Settings flyout, switcher */
void panels_init(void);
void panels_action(enum c8_shell_v1_action action);
void panels_switcher(int direction, uint32_t modifiers);
/* snapped was snapped to side (-1 left, 1 right): the other windows'
 * pictures go in the rect, to pick one for it. side 0 ends it. */
void panels_snap(struct window *snapped, int side, int x, int y, int width,
	int height);
void panels_preview_ready(struct window *w);
void panels_close(void);
void panels_windows_changed(void);
void panels_appearance_changed(void);
/* The Settings flyout, for the app in front if it has its own */
void panels_open_settings(void);
/* The Settings flyout with body over the quick settings; NULL for Start's
 * Personalise */
void panels_open_settings_with(struct c8ui_widget *body);
/* XR: the Settings card (pages Settings and Networks) opened, or put
 * away if it's up; false without the charms bar */
bool panels_settings_card(void);
/* False when the pages of the Settings card are headed already (its
 * pivot, over XR's charms bar): leave out a "Settings" heading */
bool panels_headed(void);
/* Another flyout in the Settings one's place (Networks, an app's search).
 * key, if given, sees keys before the panel's own (Escape closes). */
/* project.c: the Second screen flyout */
void project_open(void);
void project_changed(void);
void project_closed(void);

/* XR: a small card in front of you, sized to page */
void panels_show_card(struct c8ui_widget *page);
/* XR: content in a card of that size above the charms bar, with the
 * keyboard, rising in; it closes as any panel does (Escape, a press off
 * the bar, the bar going as you look up) */
void panels_show_bar_card(struct c8ui_widget *content, int width,
	int height);
void panels_card_height(int height);
/* The button that opened what's showing, so pressing it again puts it
 * away: panels_mark just after opening it, panels_is to ask. Anything
 * opening after clears it. */
enum opener {
	OPENER_NONE,
	OPENER_NETWORK,
	OPENER_VOLUME,
	OPENER_BRIGHTNESS,
};
void panels_mark(int key);
bool panels_is(int key);
#define BAR_LINE 3 // the accent along the charms bar's top
#define BAR_ABOVE_GAP 12 // between what's above the bar and the bar
void panels_show_flyout(struct c8ui_widget *page, c8ui_key_fn key,
	void *data);

/* context.c: the Search and Settings charms of the app in front, through
 * org.cont8nuum.Charms1; Start's search and the usual flyout otherwise */
void context_init(void);
void context_search(void);
void context_settings(void);
/* The name of the app in front, as All apps shows it; NULL for none.
 * g_free it. */
char *context_front_title(void);
/* The panel closed */
void context_closed(void);

/* quick.c: the Settings flyout's tiles (in XR those not on the bar or
 * the card's pivot), the charms bar's two (Volume, Brightness), and the
 * Networks flyout */
struct c8ui_widget *quick_tiles_new(void);
struct c8ui_widget *quick_row_new(int each, int height, int *width);
/* The Settings card's Networks page (XR): airplane mode beside the
 * Wi-Fi list; it watches NetworkManager while it lives */
struct c8ui_widget *quick_networks_new(void);
void quick_status_changed(void);
void quick_level_close(void);

/* profiles.c: power modes (power-profiles-daemon). profiles_list gives
 * the ones offered, 0 when there's nothing to choose */
void profiles_init(void);
int profiles_list(const char **names, const char **labels, int max);
bool profiles_active(const char *name);
void profiles_set(const char *name);

/* notify.c: org.freedesktop.Notifications, shown as toasts */
void notify_init(void);
void notify_appearance_changed(void);
void notify_locked_changed(void);
void notify_output_changed(void);
int notify_row_top(void); // XR: the toasts' row's top
/* The Settings flyout's "Hide for 1 hour"; 0 shows them again */
void notify_hide_for(int hours);
/* A toast with two buttons that asks the user something (shown even while
 * notifications are hidden, 20 s). fn runs once: yes when they pressed the
 * first, no for the second, dismissing it or letting it go. */
typedef void (*notify_ask_fn)(bool yes, void *data);
void notify_ask(const char *summary, const char *body, const char *icon,
	const char *yes, const char *no, notify_ask_fn fn, void *data);
bool notify_hidden(void);

/* tray.c: apps' tray icons (StatusNotifierItem), drawn under the charms
 * clock. tray_changed runs when the ones shown change */
typedef void (*tray_fn)(void *data);
struct tray_item;
void tray_init(void);
void tray_listen(tray_fn fn, void *data);
int tray_count(void);
struct tray_item *tray_nth(int n);
const char *tray_title(struct tray_item *item);
void tray_draw(struct tray_item *item, cairo_t *cr, double cx, double cy,
	double size);
/* A C8UI_BUTTON_*: left activates, right (or an item that is only a menu)
 * opens its menu in a flyout, anything else is the second action */
void tray_click(struct tray_item *item, int button, int x, int y);

/* logind.c: locking for `loginctl lock-session` and before sleep */
void logind_init(void);

/* loginpic.c: the lock picture's copy for the login screen */
void login_picture_changed(void);
void login_picture_init(void);

/* drives.c: a toast when a drive is plugged in */
void drives_init(void);
void logind_locked_changed(void);

#endif
