/*
* Shell surfaces: c8ui windows that get their role from c8_shell_v1
* instead of xdg-shell, so the compositor stacks and places them as the
* shell's.
*/
#include <stdlib.h>
#include "shell.h"
struct shell_surface {
struct c8ui_window *win;
struct c8_shell_surface_v1 *role;
enum c8_shell_v1_role kind;
shell_surface_fn on_dismiss;
void *dismiss_data;
int width, height;
bool hidden;
};
static void handle_configure(void *data, struct c8_shell_surface_v1 *role,
int32_t width, int32_t height) {
struct shell_surface *s = data;
s->width = width;
s->height = height;
if (s->kind != C8_SHELL_V1_ROLE_START) {
c8ui_window_suspend(s->win, false); // see shell_surface_set_rect
}
if (s->kind == C8_SHELL_V1_ROLE_START) {
/* Start covers the output: panels go by it */
bool changed = width != shell.output_width ||
height != shell.output_height;
shell.output_width = width;
shell.output_height = height;
if (changed) {
notify_output_changed();
}
}
if (!s->hidden) {
c8ui_window_configure(s->win, width, height);
}
}
static void handle_dismissed(void *data, struct c8_shell_surface_v1 *role) {
struct shell_surface *s = data;
if (s->on_dismiss) {
s->on_dismiss(s, s->dismiss_data);
}
}
static const struct c8_shell_surface_v1_listener role_listener = {
.configure = handle_configure,
.dismissed = handle_dismissed,
};
struct shell_surface *shell_surface_new(enum c8_shell_v1_role role) {
struct shell_surface *s = calloc(1, sizeof(*s));
s->kind = role;
s->win = c8ui_window_new_bare(shell.app);
s->role = c8_shell_v1_get_surface(shell.proto,
c8ui_window_surface(s->win), role);
c8_shell_surface_v1_add_listener(s->role, &role_listener, s);
c8ui_window_set_background(s->win, 0, 0);
return s;
}
void shell_surface_destroy(struct shell_surface *s) {
if (s == NULL) {
return;
}
c8_shell_surface_v1_destroy(s->role);
c8ui_window_destroy(s->win);
free(s);
}
struct c8ui_root *shell_surface_root(struct shell_surface *s) {
return c8ui_window_root(s->win);
}
struct c8ui_window *shell_surface_window(struct shell_surface *s) {
return s->win;
}
void shell_surface_set_rect(struct shell_surface *s, int x, int y, int width,
int height) {
c8_shell_surface_v1_set_rect(s->role, x, y, width, height);
bool resized = width != s->width || height != s->height;
bool was_hidden = s->hidden;
s->hidden = false;
if (was_hidden && !resized) {
/* Draws, and the commit carries the rect */
c8ui_window_configure(s->win, width, height);
return;
}
/* Carries the rect; a new size is answered with configure. Until
* then nothing is drawn: a frame of what changed at the old size
* (a card added over XR's charms bar) pushed the rest out of it,
* and the bar blinked. The compositor keeps showing the last one. */
if (resized && !was_hidden) {
c8ui_window_suspend(s->win, true);
}
wl_surface_commit(c8ui_window_surface(s->win));
}
void shell_surface_grab_keyboard(struct shell_surface *s, bool grab) {
c8_shell_surface_v1_grab_keyboard(s->role, grab);
}
void shell_surface_on_dismiss(struct shell_surface *s, shell_surface_fn fn,
void *data) {
s->on_dismiss = fn;
s->dismiss_data = data;
}
void shell_surface_hide(struct shell_surface *s) {
s->hidden = true;
c8ui_window_unmap(s->win);
}
/* Full-output roles: shows a hidden surface at the size last configured */
void shell_surface_show(struct shell_surface *s) {
if (!s->hidden) {
return;
}
s->hidden = false;
if (s->width > 0 && s->height > 0) {
c8ui_window_configure(s->win, s->width, s->height);
}
}