2018-12-31 00:12:33 +01:00
|
|
|
/*
|
|
|
|
|
* Cage: A Wayland kiosk.
|
|
|
|
|
*
|
2021-12-24 08:58:19 +01:00
|
|
|
* Copyright (C) 2018-2021 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
|
|
|
|
|
|
2021-12-26 15:36:39 +01:00
|
|
|
#include <assert.h>
|
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_output.h>
|
2021-08-09 20:06:57 +02:00
|
|
|
#include <wlr/types/wlr_scene.h>
|
2018-12-31 00:12:33 +01:00
|
|
|
|
|
|
|
|
#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-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
|
2020-02-17 19:49:47 +01:00
|
|
|
view_is_transient_for(struct cg_view *child, struct cg_view *parent)
|
|
|
|
|
{
|
2019-01-30 20:25:29 +01:00
|
|
|
return child->impl->is_transient_for(child, parent);
|
2019-01-30 17:04:15 +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;
|
2021-08-09 20:06:57 +02:00
|
|
|
|
2024-08-24 15:44:46 +02:00
|
|
|
if (view->scene_tree) {
|
|
|
|
|
wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);
|
|
|
|
|
}
|
2021-08-09 20:06:57 +02:00
|
|
|
|
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;
|
2021-08-09 20:06:57 +02:00
|
|
|
|
2024-08-24 15:44:46 +02:00
|
|
|
if (view->scene_tree) {
|
|
|
|
|
wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);
|
|
|
|
|
}
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 15:50:31 +01:00
|
|
|
void
|
|
|
|
|
view_position(struct cg_view *view)
|
|
|
|
|
{
|
2022-10-12 04:05:33 -04:00
|
|
|
struct wlr_box layout_box;
|
|
|
|
|
wlr_output_layout_get_box(view->server->output_layout, NULL, &layout_box);
|
2020-01-11 17:06:39 +01:00
|
|
|
|
2022-10-12 04:05:33 -04:00
|
|
|
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 {
|
2022-10-12 04:05:33 -04:00
|
|
|
view_center(view, &layout_box);
|
2019-01-10 15:50:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 15:37:38 +02:00
|
|
|
void
|
|
|
|
|
view_position_all(struct cg_server *server)
|
|
|
|
|
{
|
|
|
|
|
struct cg_view *view;
|
|
|
|
|
wl_list_for_each (view, &server->views, link) {
|
|
|
|
|
view_position(view);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2022-12-14 13:56:15 -05:00
|
|
|
wlr_scene_node_destroy(&view->scene_tree->node);
|
2021-08-09 20:06:57 +02:00
|
|
|
|
2021-12-24 08:58:19 +01:00
|
|
|
view->wlr_surface->data = NULL;
|
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)
|
|
|
|
|
{
|
2022-12-14 13:56:15 -05:00
|
|
|
view->scene_tree = wlr_scene_subsurface_tree_create(&view->server->scene->tree, surface);
|
|
|
|
|
if (!view->scene_tree) {
|
2021-08-09 20:06:57 +02:00
|
|
|
wl_resource_post_no_memory(surface->resource);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-14 13:56:15 -05:00
|
|
|
view->scene_tree->node.data = view;
|
2019-02-13 18:14:03 +01:00
|
|
|
|
2021-12-24 08:58:19 +01:00
|
|
|
view->wlr_surface = surface;
|
|
|
|
|
surface->data = view;
|
|
|
|
|
|
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-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);
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 17:01:16 +01:00
|
|
|
void
|
2020-02-17 19:49:47 +01:00
|
|
|
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;
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct cg_view *
|
2021-12-24 08:58:19 +01:00
|
|
|
view_from_wlr_surface(struct wlr_surface *surface)
|
2018-12-31 00:12:33 +01:00
|
|
|
{
|
2021-12-26 15:36:39 +01:00
|
|
|
assert(surface);
|
2021-12-24 08:58:19 +01:00
|
|
|
return surface->data;
|
2018-12-31 00:12:33 +01:00
|
|
|
}
|