hikari/src/xwayland_view.c

436 lines
12 KiB
C

#ifdef HAVE_XWAYLAND
#include <hikari/xwayland_view.h>
#include <assert.h>
#include <stdlib.h>
#include <wlr/types/wlr_cursor.h>
#include <wlr/types/wlr_output_damage.h>
#include <wlr/xwayland.h>
#include <hikari/configuration.h>
#include <hikari/geometry.h>
#include <hikari/output.h>
#include <hikari/render_data.h>
#include <hikari/server.h>
#include <hikari/sheet.h>
#include <hikari/view.h>
#include <hikari/workspace.h>
static uint32_t
resize(struct hikari_view *view, int width, int height)
{
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
struct wlr_box *geometry = hikari_view_geometry(view);
struct hikari_output *output = view->output;
wlr_xwayland_surface_configure(xwayland_view->surface,
output->geometry.x + geometry->x,
output->geometry.y + geometry->y,
width,
height);
return 0;
}
static void
move_resize(struct hikari_view *view, int x, int y, int width, int height)
{
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
struct hikari_output *output = view->output;
wlr_xwayland_surface_configure(xwayland_view->surface,
output->geometry.x + x,
output->geometry.y + y,
width,
height);
}
static void
move(struct hikari_view *view, int x, int y)
{
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
struct wlr_box *geometry = hikari_view_geometry(view);
struct hikari_output *output = view->output;
wlr_xwayland_surface_configure(xwayland_view->surface,
output->geometry.x + x,
output->geometry.y + y,
geometry->width,
geometry->height);
}
static void
commit_handler(struct wl_listener *listener, void *data)
{
struct hikari_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, commit);
struct hikari_view *view = (struct hikari_view *)xwayland_view;
struct wlr_box *geometry = hikari_view_geometry(view);
if (hikari_view_is_dirty(view)) {
hikari_view_commit_pending_operation(
view, &view->pending_operation.geometry);
} else {
struct wlr_xwayland_surface *surface = xwayland_view->surface;
struct hikari_output *output = view->output;
if (surface->width != geometry->width ||
surface->height != geometry->height || surface->x != geometry->x ||
surface->y != geometry->y) {
if (!hikari_view_is_hidden(view)) {
hikari_indicator_damage(&hikari_server.indicator, view);
hikari_view_damage_whole(view);
}
geometry->x = surface->x;
geometry->y = surface->y;
geometry->width = surface->width;
geometry->height = surface->height;
hikari_view_refresh_geometry(view, geometry);
if (!hikari_view_is_hidden(view)) {
hikari_view_damage_whole(view);
}
} else if (view->output->enabled && !hikari_view_is_hidden(view)) {
pixman_region32_t damage;
pixman_region32_init(&damage);
wlr_surface_get_effective_damage(surface->surface, &damage);
pixman_region32_translate(&damage, geometry->x, geometry->y);
wlr_output_damage_add(output->damage, &damage);
pixman_region32_fini(&damage);
}
}
}
static void
set_title_handler(struct wl_listener *listener, void *data)
{
struct hikari_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, set_title);
struct hikari_view *view = (struct hikari_view *)xwayland_view;
hikari_view_set_title(view, xwayland_view->surface->title);
}
static void
first_map(struct hikari_xwayland_view *xwayland_view, bool *focus)
{
struct hikari_view *view = (struct hikari_view *)xwayland_view;
struct hikari_output *output = hikari_server.workspace->output;
struct wlr_box *geometry = &view->geometry;
view->border.state = HIKARI_BORDER_INACTIVE;
struct hikari_sheet *sheet;
struct hikari_group *group;
int x;
int y;
const char *app_id = xwayland_view->surface->class;
hikari_view_set_id(view, app_id);
hikari_configuration_resolve_view_autoconf(
hikari_configuration, app_id, view, &sheet, &group, &x, &y, focus);
if (x == -1) {
x = hikari_server.cursor->x - output->geometry.x;
}
if (y == -1) {
y = hikari_server.cursor->y - output->geometry.y;
}
geometry->width = xwayland_view->surface->width;
geometry->height = xwayland_view->surface->height;
xwayland_view->commit.notify = commit_handler;
wl_signal_add(
&xwayland_view->surface->surface->events.commit, &xwayland_view->commit);
hikari_view_set_title(view, xwayland_view->surface->title);
hikari_view_manage(view, sheet, group);
hikari_geometry_constrain_absolute(geometry, &output->usable_area, x, y);
wlr_xwayland_surface_configure(xwayland_view->surface,
output->geometry.x + view->geometry.x,
output->geometry.y + view->geometry.y,
geometry->width,
geometry->height);
hikari_view_refresh_geometry(view, geometry);
}
static void
map(struct hikari_view *view, bool focus)
{
#if !defined(NDEBUG)
printf("XWAYLAND MAP %p\n", view);
#endif
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
view->surface = xwayland_view->surface->surface;
view->surface->data = (struct hikari_view_interface *)view;
hikari_view_show(view);
if (focus) {
hikari_view_center_cursor(view);
}
hikari_server_cursor_focus();
}
static void
map_handler(struct wl_listener *listener, void *data)
{
struct hikari_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, map);
struct hikari_view *view = (struct hikari_view *)xwayland_view;
bool focus = false;
if (hikari_view_is_unmanaged(view)) {
first_map(xwayland_view, &focus);
}
map(view, focus);
}
static void
unmap(struct hikari_view *view)
{
#if !defined(NDEBUG)
printf("XWAYLAND UNMAP %p\n", view);
#endif
if (!hikari_view_is_hidden(view)) {
hikari_view_hide(view);
hikari_server_cursor_focus();
}
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
wl_list_remove(&xwayland_view->commit.link);
view->surface = NULL;
}
static void
unmap_handler(struct wl_listener *listener, void *data)
{
struct hikari_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, unmap);
unmap((struct hikari_view *)xwayland_view);
}
static void
destroy_handler(struct wl_listener *listener, void *data)
{
struct hikari_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, destroy);
#if !defined(NDEBUG)
printf("XWAYLAND DESTROY %p\n", xwayland_view);
#endif
struct hikari_view *view = (struct hikari_view *)xwayland_view;
if (hikari_view_is_mapped(view)) {
unmap(view);
}
hikari_view_fini(&xwayland_view->view);
wl_list_remove(&xwayland_view->map.link);
wl_list_remove(&xwayland_view->unmap.link);
wl_list_remove(&xwayland_view->destroy.link);
wl_list_remove(&xwayland_view->request_configure.link);
wl_list_remove(&xwayland_view->set_title.link);
hikari_free(xwayland_view);
}
static void
request_configure_handler(struct wl_listener *listener, void *data)
{
struct hikari_xwayland_view *xwayland_view =
wl_container_of(listener, xwayland_view, request_configure);
struct wlr_xwayland_surface *xwayland_surface = xwayland_view->surface;
struct wlr_xwayland_surface_configure_event *event = data;
#if !defined(NDEBUG)
printf("XWAYLAND CONFIGURE %p %d %d\n", xwayland_view, event->x, event->y);
#endif
struct wlr_box geometry = {
.x = event->x, .y = event->y, .width = event->width, .height = event->height
};
struct hikari_sheet *sheet = xwayland_view->view.sheet;
struct wlr_box *usable_area;
if (sheet != NULL) {
usable_area = &xwayland_view->view.output->usable_area;
} else {
usable_area = &hikari_server.workspace->output->usable_area;
}
hikari_geometry_constrain_absolute(
&geometry, usable_area, event->x, event->y);
wlr_xwayland_surface_configure(xwayland_surface,
geometry.x,
geometry.y,
geometry.width,
geometry.height);
}
static void
activate(struct hikari_view *view, bool active)
{
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
struct wlr_xwayland_surface *xwayland_surface = xwayland_view->surface;
wlr_xwayland_surface_activate(xwayland_surface, active);
wlr_xwayland_set_seat(hikari_server.xwayland, hikari_server.seat);
}
static void
quit(struct hikari_view *view)
{
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
wlr_xwayland_surface_close(xwayland_view->surface);
}
static struct wlr_surface *
surface_at(struct hikari_view_interface *view_interface,
double ox,
double oy,
double *sx,
double *sy)
{
struct hikari_view *view = (struct hikari_view *)view_interface;
struct wlr_box *geometry = hikari_view_geometry(view);
double x = ox - geometry->x;
double y = oy - geometry->y;
return wlr_surface_surface_at(view->surface, x, y, sx, sy);
}
static void
focus(struct hikari_view_interface *view_interface)
{
struct hikari_view *view = (struct hikari_view *)view_interface;
hikari_workspace_focus_view(view->sheet->workspace, view);
}
static void
for_each_surface(struct hikari_view_interface *view_interface,
void (*func)(struct wlr_surface *, int, int, void *),
void *data)
{
struct hikari_view *view = (struct hikari_view *)view_interface;
wlr_surface_for_each_surface(view->surface, func, data);
}
static void
constraints(struct hikari_view *view,
int *min_width,
int *min_height,
int *max_width,
int *max_height)
{
struct hikari_xwayland_view *xwayland_view =
(struct hikari_xwayland_view *)view;
struct hikari_output *output = view->output;
struct wlr_xwayland_surface *surface = xwayland_view->surface;
struct wlr_xwayland_surface_size_hints *size_hints = surface->size_hints;
if (size_hints != NULL) {
*min_width = size_hints->min_width > 0 ? size_hints->min_width : 0;
*max_width = size_hints->max_width > 0 ? size_hints->max_width
: output->geometry.width;
*min_height = size_hints->min_height > 0 ? size_hints->min_height : 0;
*max_height = size_hints->max_height > 0 ? size_hints->max_height
: output->geometry.height;
} else {
*min_width = 0;
*max_width = output->geometry.width;
*min_height = 0;
*max_height = output->geometry.height;
}
}
void
hikari_xwayland_view_init(struct hikari_xwayland_view *xwayland_view,
struct wlr_xwayland_surface *xwayland_surface,
struct hikari_workspace *workspace)
{
struct hikari_view *view = &xwayland_view->view;
hikari_view_init(view, HIKARI_XWAYLAND_VIEW, workspace);
view->view_interface.surface_at = surface_at;
view->view_interface.focus = focus;
view->view_interface.for_each_surface = for_each_surface;
wlr_xwayland_surface_ping(xwayland_surface);
#if !defined(NDEBUG)
printf("XWAYLAND NEW %p\n", xwayland_view);
#endif
xwayland_view->surface = xwayland_surface;
xwayland_view->map.notify = map_handler;
wl_signal_add(&xwayland_surface->events.map, &xwayland_view->map);
xwayland_view->unmap.notify = unmap_handler;
wl_signal_add(&xwayland_surface->events.unmap, &xwayland_view->unmap);
xwayland_view->destroy.notify = destroy_handler;
wl_signal_add(&xwayland_surface->events.destroy, &xwayland_view->destroy);
xwayland_view->request_configure.notify = request_configure_handler;
wl_signal_add(&xwayland_surface->events.request_configure,
&xwayland_view->request_configure);
xwayland_view->set_title.notify = set_title_handler;
wl_signal_add(&xwayland_surface->events.set_title, &xwayland_view->set_title);
view->activate = activate;
view->resize = resize;
view->move = move;
view->move_resize = move_resize;
view->quit = quit;
view->constraints = constraints;
}
#endif