2018-12-31 00:12:33 +01:00
|
|
|
/*
|
|
|
|
|
* Cage: A Wayland kiosk.
|
|
|
|
|
*
|
2019-12-26 12:00:51 +01:00
|
|
|
* Copyright (C) 2018-2020 Jente Hidskes
|
2018-12-31 00:12:33 +01:00
|
|
|
*
|
|
|
|
|
* See the LICENSE file accompanying this file.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-24 14:14:15 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
|
2018-12-31 00:12:33 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdlib.h>
|
2019-01-24 14:14:15 +01:00
|
|
|
#include <string.h>
|
2019-12-20 17:16:53 +01:00
|
|
|
#include <wayland-server-core.h>
|
2018-12-31 00:12:33 +01:00
|
|
|
#include <wlr/types/wlr_box.h>
|
|
|
|
|
#include <wlr/types/wlr_output.h>
|
|
|
|
|
#include <wlr/types/wlr_surface.h>
|
|
|
|
|
|
|
|
|
|
#include "output.h"
|
|
|
|
|
#include "seat.h"
|
|
|
|
|
#include "server.h"
|
|
|
|
|
#include "view.h"
|
2019-01-30 17:19:40 +01:00
|
|
|
#if CAGE_HAS_XWAYLAND
|
|
|
|
|
#include "xwayland.h"
|
|
|
|
|
#endif
|
2018-12-31 00:12:33 +01:00
|
|
|
|
2019-02-13 18:14:03 +01:00
|
|
|
static void
|
|
|
|
|
view_child_handle_commit(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct cg_view_child *child = wl_container_of(listener, child, commit);
|
2019-12-26 12:03:38 +01:00
|
|
|
view_damage_part(child->view);
|
2019-02-13 18:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subsurface_create(struct cg_view *view, struct wlr_subsurface *wlr_subsurface);
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
view_child_handle_new_subsurface(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct cg_view_child *child = wl_container_of(listener, child, new_subsurface);
|
|
|
|
|
struct wlr_subsurface *wlr_subsurface = data;
|
|
|
|
|
subsurface_create(child->view, wlr_subsurface);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
view_child_finish(struct cg_view_child *child)
|
|
|
|
|
{
|
|
|
|
|
if (!child) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 18:50:29 +01:00
|
|
|
view_damage_whole(child->view);
|
|
|
|
|
|
2019-02-13 18:14:03 +01:00
|
|
|
wl_list_remove(&child->link);
|
|
|
|
|
wl_list_remove(&child->commit.link);
|
|
|
|
|
wl_list_remove(&child->new_subsurface.link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
view_child_init(struct cg_view_child *child, struct cg_view *view, struct wlr_surface *wlr_surface)
|
|
|
|
|
{
|
|
|
|
|
child->view = view;
|
|
|
|
|
child->wlr_surface = wlr_surface;
|
|
|
|
|
|
|
|
|
|
child->commit.notify = view_child_handle_commit;
|
|
|
|
|
wl_signal_add(&wlr_surface->events.commit, &child->commit);
|
|
|
|
|
child->new_subsurface.notify = view_child_handle_new_subsurface;
|
|
|
|
|
wl_signal_add(&wlr_surface->events.new_subsurface, &child->new_subsurface);
|
|
|
|
|
|
|
|
|
|
wl_list_insert(&view->children, &child->link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
subsurface_destroy(struct cg_view_child *child)
|
|
|
|
|
{
|
|
|
|
|
if (!child) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct cg_subsurface *subsurface = (struct cg_subsurface *) child;
|
|
|
|
|
wl_list_remove(&subsurface->destroy.link);
|
|
|
|
|
view_child_finish(&subsurface->view_child);
|
|
|
|
|
free(subsurface);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
subsurface_handle_destroy(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct cg_subsurface *subsurface = wl_container_of(listener, subsurface, destroy);
|
|
|
|
|
struct cg_view_child *view_child = (struct cg_view_child *) subsurface;
|
|
|
|
|
subsurface_destroy(view_child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
subsurface_create(struct cg_view *view, struct wlr_subsurface *wlr_subsurface)
|
|
|
|
|
{
|
|
|
|
|
struct cg_subsurface *subsurface = calloc(1, sizeof(struct cg_subsurface));
|
|
|
|
|
if (!subsurface) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view_child_init(&subsurface->view_child, view, wlr_subsurface->surface);
|
|
|
|
|
subsurface->view_child.destroy = subsurface_destroy;
|
|
|
|
|
subsurface->wlr_subsurface = wlr_subsurface;
|
|
|
|
|
|
|
|
|
|
subsurface->destroy.notify = subsurface_handle_destroy;
|
|
|
|
|
wl_signal_add(&wlr_subsurface->events.destroy, &subsurface->destroy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_new_subsurface(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct cg_view *view = wl_container_of(listener, view, new_subsurface);
|
|
|
|
|
struct wlr_subsurface *wlr_subsurface = data;
|
|
|
|
|
subsurface_create(view, wlr_subsurface);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 14:14:15 +01:00
|
|
|
char *
|
|
|
|
|
view_get_title(struct cg_view *view)
|
|
|
|
|
{
|
2019-01-30 17:01:16 +01:00
|
|
|
const char *title = view->impl->get_title(view);
|
2019-01-31 16:20:48 +01:00
|
|
|
if (!title) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-01-24 14:14:15 +01:00
|
|
|
return strndup(title, strlen(title));
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 17:04:15 +01:00
|
|
|
bool
|
|
|
|
|
view_is_primary(struct cg_view *view)
|
|
|
|
|
{
|
|
|
|
|
return view->impl->is_primary(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2019-01-30 20:25:29 +01:00
|
|
|
view_is_transient_for(struct cg_view *child, struct cg_view *parent) {
|
|
|
|
|
return child->impl->is_transient_for(child, parent);
|
2019-01-30 17:04:15 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-20 13:42:36 +01:00
|
|
|
void
|
2019-12-26 12:03:38 +01:00
|
|
|
view_damage_part(struct cg_view *view)
|
2019-01-20 13:42:36 +01:00
|
|
|
{
|
2019-12-21 16:10:11 +01:00
|
|
|
struct cg_output *output;
|
|
|
|
|
wl_list_for_each(output, &view->server->outputs, link) {
|
2019-12-26 12:00:51 +01:00
|
|
|
output_damage_surface(output, view->wlr_surface, view->lx, view->ly, false);
|
2019-12-21 16:10:11 +01:00
|
|
|
}
|
2019-01-20 13:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-13 18:50:08 +01:00
|
|
|
void
|
|
|
|
|
view_damage_whole(struct cg_view *view)
|
|
|
|
|
{
|
2019-12-21 16:10:11 +01:00
|
|
|
struct cg_output *output;
|
|
|
|
|
wl_list_for_each(output, &view->server->outputs, link) {
|
2019-12-26 12:00:51 +01:00
|
|
|
output_damage_surface(output, view->wlr_surface, view->lx, view->ly, true);
|
2019-12-21 16:10:11 +01:00
|
|
|
}
|
2019-02-13 18:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:12:33 +01:00
|
|
|
void
|
|
|
|
|
view_activate(struct cg_view *view, bool activate)
|
|
|
|
|
{
|
2019-01-30 17:01:16 +01:00
|
|
|
view->impl->activate(view, activate);
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-11 17:06:39 +01:00
|
|
|
static bool
|
|
|
|
|
view_extends_output_layout(struct cg_view *view, struct wlr_box *layout_box)
|
2018-12-31 00:12:33 +01:00
|
|
|
{
|
2020-01-11 17:06:39 +01:00
|
|
|
int width, height;
|
|
|
|
|
view->impl->get_geometry(view, &width, &height);
|
|
|
|
|
|
|
|
|
|
return (layout_box->height < height || layout_box->width < width);
|
|
|
|
|
}
|
2019-12-29 13:17:30 +01:00
|
|
|
|
2020-01-11 17:06:39 +01:00
|
|
|
static void
|
|
|
|
|
view_maximize(struct cg_view *view, struct wlr_box *layout_box)
|
|
|
|
|
{
|
2019-12-29 16:07:14 +01:00
|
|
|
view->lx = layout_box->x;
|
|
|
|
|
view->ly = layout_box->y;
|
2019-12-29 13:17:30 +01:00
|
|
|
view->impl->maximize(view, layout_box->width, layout_box->height);
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 15:57:23 +01:00
|
|
|
static void
|
2020-01-11 17:06:39 +01:00
|
|
|
view_center(struct cg_view *view, struct wlr_box *layout_box)
|
2018-12-31 00:12:33 +01:00
|
|
|
{
|
2018-12-31 20:44:20 +01:00
|
|
|
int width, height;
|
2019-01-30 17:01:16 +01:00
|
|
|
view->impl->get_geometry(view, &width, &height);
|
2018-12-31 00:12:33 +01:00
|
|
|
|
2019-12-29 16:07:14 +01:00
|
|
|
view->lx = (layout_box->width - width) / 2;
|
|
|
|
|
view->ly = (layout_box->height - height) / 2;
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 15:50:31 +01:00
|
|
|
void
|
|
|
|
|
view_position(struct cg_view *view)
|
|
|
|
|
{
|
2020-01-11 17:06:39 +01:00
|
|
|
struct wlr_box *layout_box = wlr_output_layout_get_box(view->server->output_layout, NULL);
|
|
|
|
|
|
|
|
|
|
if (view_is_primary(view) || view_extends_output_layout(view, layout_box)) {
|
|
|
|
|
view_maximize(view, layout_box);
|
2019-01-10 15:50:31 +01:00
|
|
|
} else {
|
2020-01-11 17:06:39 +01:00
|
|
|
view_center(view, layout_box);
|
2019-01-10 15:50:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 17:04:15 +01:00
|
|
|
void
|
|
|
|
|
view_for_each_surface(struct cg_view *view, wlr_surface_iterator_func_t iterator, void *data)
|
|
|
|
|
{
|
|
|
|
|
view->impl->for_each_surface(view, iterator, data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 14:39:56 +01:00
|
|
|
void
|
|
|
|
|
view_for_each_popup(struct cg_view *view, wlr_surface_iterator_func_t iterator, void *data)
|
|
|
|
|
{
|
|
|
|
|
if (!view->impl->for_each_popup) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
view->impl->for_each_popup(view, iterator, data);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 19:52:47 +01:00
|
|
|
void
|
|
|
|
|
view_unmap(struct cg_view *view)
|
|
|
|
|
{
|
|
|
|
|
wl_list_remove(&view->link);
|
2019-02-13 18:14:03 +01:00
|
|
|
|
|
|
|
|
wl_list_remove(&view->new_subsurface.link);
|
|
|
|
|
|
|
|
|
|
struct cg_view_child *child, *tmp;
|
|
|
|
|
wl_list_for_each_safe(child, tmp, &view->children, link) {
|
|
|
|
|
child->destroy(child);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 19:52:47 +01:00
|
|
|
view->wlr_surface = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-31 00:12:33 +01:00
|
|
|
void
|
|
|
|
|
view_map(struct cg_view *view, struct wlr_surface *surface)
|
|
|
|
|
{
|
|
|
|
|
view->wlr_surface = surface;
|
|
|
|
|
|
2019-02-13 18:14:03 +01:00
|
|
|
struct wlr_subsurface *subsurface;
|
|
|
|
|
wl_list_for_each(subsurface, &view->wlr_surface->subsurfaces, parent_link) {
|
|
|
|
|
subsurface_create(view, subsurface);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view->new_subsurface.notify = handle_new_subsurface;
|
|
|
|
|
wl_signal_add(&view->wlr_surface->events.new_subsurface, &view->new_subsurface);
|
|
|
|
|
|
2019-01-17 22:01:57 +01:00
|
|
|
#if CAGE_HAS_XWAYLAND
|
|
|
|
|
/* We shouldn't position override-redirect windows. They set
|
2019-01-31 18:31:14 +01:00
|
|
|
their own (x,y) coordinates in handle_wayland_surface_map. */
|
2019-01-31 18:29:03 +01:00
|
|
|
if (view->type != CAGE_XWAYLAND_VIEW || xwayland_view_should_manage(view))
|
2019-01-17 22:01:57 +01:00
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
view_position(view);
|
|
|
|
|
}
|
2018-12-31 00:12:33 +01:00
|
|
|
|
2018-12-31 19:52:47 +01:00
|
|
|
wl_list_insert(&view->server->views, &view->link);
|
2018-12-31 00:12:33 +01:00
|
|
|
seat_set_focus(view->server->seat, view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
view_destroy(struct cg_view *view)
|
|
|
|
|
{
|
|
|
|
|
struct cg_server *server = view->server;
|
2019-01-30 17:19:40 +01:00
|
|
|
bool ever_been_mapped = true;
|
|
|
|
|
|
|
|
|
|
#if CAGE_HAS_XWAYLAND
|
|
|
|
|
if (view->type == CAGE_XWAYLAND_VIEW) {
|
|
|
|
|
struct cg_xwayland_view *xwayland_view = xwayland_view_from_view(view);
|
|
|
|
|
ever_been_mapped = xwayland_view->ever_been_mapped;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-12-31 00:12:33 +01:00
|
|
|
|
2019-01-04 17:24:42 +01:00
|
|
|
if (view->wlr_surface != NULL) {
|
|
|
|
|
view_unmap(view);
|
|
|
|
|
}
|
2019-01-30 17:01:16 +01:00
|
|
|
|
|
|
|
|
view->impl->destroy(view);
|
2018-12-31 00:12:33 +01:00
|
|
|
|
view: test whether XWayland surfaces have been mapped
This is to work around "misbehaving" (for lack of a better term)
clients. At the moment, Firefox Nightly and Google Chrome/Chromium are
known to be suchs client:
When XWayland support is enabled, Firefox first creates an XWayland
surface, closes this (before mapping it) and then opens an XDG toplevel
surface. Cage tries to manage the first XWayland surface, but when it
closes, Cage has no surfaces left and hence closes as well.
Hence, Cage terminates before it picks up on Firefox's XDG toplevel, and
Firefox (rightly) prints it cannot read the Wayland pipe.
In Chromium's case, it simply opens an XWayland surface which it
immediately closes, before opening the "real" XWayland surface.
The workaround is to track whether an XWayland surface has been mapped
and, if it hasn't, to not exit when we have no views left.
Firefox's behavior and the workaround are discussed in #18.
This commit fixes #18 and is part of the fix for #19.
2019-01-18 16:37:15 +01:00
|
|
|
/* If there is a previous view in the list, focus that. */
|
|
|
|
|
bool empty = wl_list_empty(&server->views);
|
|
|
|
|
if (!empty) {
|
2019-01-04 17:25:32 +01:00
|
|
|
struct cg_view *prev = wl_container_of(server->views.next, prev, link);
|
|
|
|
|
seat_set_focus(server->seat, prev);
|
2019-01-30 17:19:40 +01:00
|
|
|
} else if (ever_been_mapped) {
|
view: test whether XWayland surfaces have been mapped
This is to work around "misbehaving" (for lack of a better term)
clients. At the moment, Firefox Nightly and Google Chrome/Chromium are
known to be suchs client:
When XWayland support is enabled, Firefox first creates an XWayland
surface, closes this (before mapping it) and then opens an XDG toplevel
surface. Cage tries to manage the first XWayland surface, but when it
closes, Cage has no surfaces left and hence closes as well.
Hence, Cage terminates before it picks up on Firefox's XDG toplevel, and
Firefox (rightly) prints it cannot read the Wayland pipe.
In Chromium's case, it simply opens an XWayland surface which it
immediately closes, before opening the "real" XWayland surface.
The workaround is to track whether an XWayland surface has been mapped
and, if it hasn't, to not exit when we have no views left.
Firefox's behavior and the workaround are discussed in #18.
This commit fixes #18 and is part of the fix for #19.
2019-01-18 16:37:15 +01:00
|
|
|
/* The list is empty and the last view has been
|
|
|
|
|
mapped, so we can safely exit. */
|
|
|
|
|
wl_display_terminate(server->wl_display);
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 17:01:16 +01:00
|
|
|
void
|
|
|
|
|
view_init(struct cg_view *view, struct cg_server *server, enum cg_view_type type,
|
|
|
|
|
const struct cg_view_impl *impl)
|
2018-12-31 00:12:33 +01:00
|
|
|
{
|
|
|
|
|
view->server = server;
|
2019-01-30 17:01:16 +01:00
|
|
|
view->type = type;
|
|
|
|
|
view->impl = impl;
|
2019-02-13 18:14:03 +01:00
|
|
|
|
|
|
|
|
wl_list_init(&view->children);
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct cg_view *
|
2019-01-30 16:10:48 +01:00
|
|
|
view_from_wlr_surface(struct cg_server *server, struct wlr_surface *surface)
|
2018-12-31 00:12:33 +01:00
|
|
|
{
|
|
|
|
|
struct cg_view *view;
|
|
|
|
|
wl_list_for_each(view, &server->views, link) {
|
|
|
|
|
if (view->wlr_surface == surface) {
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-01-30 17:04:15 +01:00
|
|
|
|
|
|
|
|
struct wlr_surface *
|
|
|
|
|
view_wlr_surface_at(struct cg_view *view, double sx, double sy, double *sub_x, double *sub_y)
|
|
|
|
|
{
|
|
|
|
|
return view->impl->wlr_surface_at(view, sx, sy, sub_x, sub_y);
|
|
|
|
|
}
|