diyac/layer.c
2024-04-01 15:38:57 +02:00

432 lines
16 KiB
C

#define _POSIX_C_SOURCE 200112L
#include <wlr/util/log.h>
#include <stdlib.h>
#include <assert.h>
#include "layer.h"
#include "node.h"
#include "output.h"
#include "seat.h"
#include "cursor.h"
static void popup_handle_new_popup(struct wl_listener *listener, void *data);
static struct diyac_popup *create_layer_popup(struct wlr_xdg_popup *wlr_popup, struct wlr_scene_tree *parent);
static void process_keyboard_interactivity(struct diyac_layer_surface *layer)
{
struct wlr_layer_surface_v1 *layer_surface = layer->scene_layer_surface->layer_surface;
struct diyac_seat *seat = &layer->server->seat;
if (layer_surface->current.keyboard_interactive && layer_surface->current.layer >= ZWLR_LAYER_SHELL_V1_LAYER_TOP)
{
/*
* Give keyboard focus to surface if
* - keyboard-interactivity is 'exclusive' or 'on-demand'; and
* - surface is in top/overlay layers; and
* - currently focused layer has a lower precedence
*
* In other words, when dealing with two surfaces with
* exclusive/on-demand keyboard-interactivity (firstly the
* currently focused 'focused_layer' and secondly the
* 'layer_surface' for which we're just responding to a
* map/commit event), the following logic applies:
*
* | focused_layer | layer_surface | who gets keyboard focus |
* |---------------|---------------|-------------------------|
* | overlay | top | focused_layer |
* | overlay | overlay | layer_surface |
* | top | top | layer_surface |
* | top | overlay | layer_surface |
*/
if (!seat->focused_layer || seat->focused_layer->current.layer <= layer_surface->current.layer)
{
diyac_seat_focus_layer(seat, layer_surface);
}
}
else if (seat->focused_layer && !seat->focused_layer->current.keyboard_interactive)
{
/*
* Clear focus if keyboard-interactivity has been set to
* ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE
*/
diyac_seat_focus_layer(seat, NULL);
}
}
static void layer_surface_commit(struct wl_listener *listener, void *data)
{
struct diyac_layer_surface *layer =
wl_container_of(listener, layer, surface_commit);
struct wlr_layer_surface_v1 *layer_surface =
layer->scene_layer_surface->layer_surface;
struct wlr_output *wlr_output =
layer->scene_layer_surface->layer_surface->output;
if (!wlr_output)
{
return;
}
uint32_t committed = layer_surface->current.committed;
struct diyac_output *output = (struct diyac_output *)wlr_output->data;
/* Process layer change */
if (committed & WLR_LAYER_SURFACE_V1_STATE_LAYER)
{
wlr_scene_node_reparent(&layer->scene_layer_surface->tree->node,
output->layer_tree[layer_surface->current.layer]);
}
/* Process keyboard-interactivity change */
if (committed & WLR_LAYER_SURFACE_V1_STATE_KEYBOARD_INTERACTIVITY)
{
process_keyboard_interactivity(layer);
}
if (committed || layer->mapped != layer_surface->surface->mapped)
{
layer->mapped = layer_surface->surface->mapped;
diyac_output_update_usable_area(output);
/*
* Update cursor focus here to ensure we
* enter a new/moved/resized layer surface.
*/
// cursor_update_focus(layer->server);
diyac_cursor_focus(layer->server);
}
}
static void
layer_surface_unmap(struct wl_listener *listener, void *data)
{
struct diyac_layer_surface *layer = wl_container_of(listener, layer, unmap);
struct wlr_layer_surface_v1 *layer_surface =
layer->scene_layer_surface->layer_surface;
if (layer_surface->output)
{
diyac_output_update_usable_area(layer_surface->output->data);
}
struct diyac_seat *seat = &layer->server->seat;
if (seat->focused_layer == layer_surface) {
diyac_seat_focus_layer(seat, NULL);
}
}
static void layer_surface_map(struct wl_listener *listener, void *data)
{
struct diyac_layer_surface *layer = wl_container_of(listener, layer, map);
struct wlr_output *wlr_output =
layer->scene_layer_surface->layer_surface->output;
if (wlr_output)
{
diyac_output_update_usable_area(wlr_output->data);
}
/*
* Since moving to the wlroots scene-graph API, there is no need to
* call wlr_surface_send_enter() from here since that will be done
* automatically based on the position of the surface and outputs in
* the scene. See wlr_scene_surface_create() documentation.
*/
process_keyboard_interactivity(layer);
}
static void layer_surface_node_destroy(struct wl_listener *listener, void *data)
{
struct diyac_layer_surface *layer =
wl_container_of(listener, layer, node_destroy);
/*
* TODO: Determine if this layer is being used by an exclusive client.
* If it is, try and find another layer owned by this client to pass
* focus to.
*/
wl_list_remove(&layer->map.link);
wl_list_remove(&layer->unmap.link);
wl_list_remove(&layer->surface_commit.link);
wl_list_remove(&layer->new_popup.link);
wl_list_remove(&layer->output_destroy.link);
wl_list_remove(&layer->node_destroy.link);
free(layer);
}
static void layer_surface_output_destroy(struct wl_listener *listener, void *data)
{
struct diyac_layer_surface *layer =
wl_container_of(listener, layer, output_destroy);
layer->scene_layer_surface->layer_surface->output = NULL;
wlr_layer_surface_v1_destroy(layer->scene_layer_surface->layer_surface);
}
static void
popup_handle_destroy(struct wl_listener *listener, void *data)
{
struct diyac_popup *popup = wl_container_of(listener, popup, destroy);
wl_list_remove(&popup->destroy.link);
wl_list_remove(&popup->new_popup.link);
/* Usually already removed unless there was no commit at all */
if (popup->commit.notify)
{
wl_list_remove(&popup->commit.link);
}
free(popup);
}
static void popup_handle_commit(struct wl_listener *listener, void *data)
{
struct diyac_popup *popup =
wl_container_of(listener, popup, commit);
struct wlr_box popup_box ;
wlr_xdg_surface_get_geometry(popup->wlr_popup->base, &popup_box);
wlr_xdg_popup_unconstrain_from_box(popup->wlr_popup, &popup->output_toplevel_sx_box);
if (!wlr_box_empty(&popup_box))
//if (popup->wlr_popup->base->initial_commit)
{
/* Prevent getting called over and over again */
wl_list_remove(&popup->commit.link);
popup->commit.notify = NULL;
}
}
/* This popup's parent is a layer popup */
static void popup_handle_new_popup(struct wl_listener *listener, void *data)
{
struct diyac_popup *popup =
wl_container_of(listener, popup, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
struct diyac_popup *new_popup = create_layer_popup(wlr_popup,
popup->scene_tree);
new_popup->output_toplevel_sx_box = popup->output_toplevel_sx_box;
}
static struct diyac_popup *create_layer_popup(struct wlr_xdg_popup *wlr_popup, struct wlr_scene_tree *parent)
{
wlr_log(WLR_INFO,
"create_layer_popup: popup create");
struct diyac_popup *popup = calloc(1, sizeof(*popup));
popup->wlr_popup = wlr_popup;
popup->scene_tree =
wlr_scene_xdg_surface_create(parent, wlr_popup->base);
if (!popup->scene_tree)
{
free(popup);
return NULL;
}
diyac_node_descriptor_create(&popup->scene_tree->node,
DIYAC_NODE_LAYER_POPUP, popup);
popup->destroy.notify = popup_handle_destroy;
wl_signal_add(&wlr_popup->base->events.destroy, &popup->destroy);
popup->new_popup.notify = popup_handle_new_popup;
wl_signal_add(&wlr_popup->base->events.new_popup, &popup->new_popup);
popup->commit.notify = popup_handle_commit;
wl_signal_add(&wlr_popup->base->surface->events.commit, &popup->commit);
return popup;
}
/*
* We move popups from the bottom to the top layer so that they are
* rendered above views.
*/
static void move_popup_to_top_layer(struct diyac_layer_surface *toplevel,
struct diyac_popup *popup)
{
wlr_log(WLR_INFO,
"move_popup_to_top_layer: popup notif");
struct diyac_server *server = toplevel->server;
struct wlr_output *wlr_output =
toplevel->scene_layer_surface->layer_surface->output;
struct diyac_output *output = (struct diyac_output *)wlr_output->data;
struct wlr_box box = {0};
wlr_output_layout_get_box(server->output_layout, wlr_output, &box);
int lx = toplevel->scene_layer_surface->tree->node.x + box.x;
int ly = toplevel->scene_layer_surface->tree->node.y + box.y;
struct wlr_scene_node *node = &popup->scene_tree->node;
wlr_scene_node_reparent(node, output->scenes.popup);
/* FIXME: verify the whole tree should be repositioned */
wlr_scene_node_set_position(&output->scenes.popup->node, lx, ly);
}
/* This popup's parent is a layer popup */
static void layer_popup_new(struct wl_listener *listener, void *data)
{
wlr_log(WLR_INFO,
"layer_popup_new: popup notif");
struct diyac_layer_surface *toplevel =
wl_container_of(listener, toplevel, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
struct diyac_server *server = toplevel->server;
struct wlr_scene_layer_surface_v1 *surface = toplevel->scene_layer_surface;
struct diyac_output *output = surface->layer_surface->output->data;
int lx, ly;
wlr_scene_node_coords(&surface->tree->node, &lx, &ly);
struct wlr_box output_box = {0};
wlr_output_layout_get_box(server->output_layout,
output->wlr_output, &output_box);
/*
* Output geometry expressed in the coordinate system of the toplevel
* parent of popup. We store this struct the lab_layer_popup struct
* to make it easier to unconstrain children when we move popups from
* the bottom to the top layer.
*/
struct wlr_box output_toplevel_sx_box = {
.x = output_box.x - lx,
.y = output_box.y - ly,
.width = output_box.width,
.height = output_box.height,
};
struct diyac_popup *popup = create_layer_popup(wlr_popup, surface->tree);
popup->output_toplevel_sx_box = output_toplevel_sx_box;
if (surface->layer_surface->current.layer <= ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM)
{
move_popup_to_top_layer(toplevel, popup);
}
}
void diyac_new_layer_surface(struct wl_listener *listener, void *data)
{
struct diyac_server *server = wl_container_of(listener, server, new_layer_surface);
struct wlr_layer_surface_v1 *layer_surface = data;
if (!layer_surface->output)
{
struct wlr_output *output = wlr_output_layout_output_at(
server->output_layout, server->seat.cursor->x,
server->seat.cursor->y);
if (!output)
{
wlr_log(WLR_INFO,
"No output available to assign layer surface");
wlr_layer_surface_v1_destroy(layer_surface);
return;
}
layer_surface->output = output;
}
struct diyac_layer_surface *surface = calloc(1, sizeof(*surface));
struct diyac_output *output = layer_surface->output->data;
struct wlr_scene_tree *selected_layer =
output->layer_tree[layer_surface->current.layer];
surface->scene_layer_surface = wlr_scene_layer_surface_v1_create(
selected_layer, layer_surface);
if (!surface->scene_layer_surface)
{
wlr_layer_surface_v1_destroy(layer_surface);
wlr_log(WLR_ERROR, "could not create layer surface");
return;
}
diyac_node_descriptor_create(&surface->scene_layer_surface->tree->node,
DIYAC_NODE_LAYER_SURFACE, surface);
surface->server = server;
surface->scene_layer_surface->layer_surface = layer_surface;
surface->surface_commit.notify = layer_surface_commit;
wl_signal_add(&layer_surface->surface->events.commit,
&surface->surface_commit);
surface->map.notify = layer_surface_map;
wl_signal_add(&layer_surface->surface->events.map, &surface->map);
surface->unmap.notify = layer_surface_unmap;
wl_signal_add(&layer_surface->surface->events.unmap, &surface->unmap);
surface->new_popup.notify = layer_popup_new;
wl_signal_add(&layer_surface->events.new_popup, &surface->new_popup);
surface->output_destroy.notify = layer_surface_output_destroy;
wl_signal_add(&layer_surface->output->events.destroy,
&surface->output_destroy);
surface->node_destroy.notify = layer_surface_node_destroy;
wl_signal_add(&surface->scene_layer_surface->tree->node.events.destroy,
&surface->node_destroy);
/*
* Temporarily set the layer's current state to pending so that
* it can easily be arranged.
*/
struct wlr_layer_surface_v1_state old_state = layer_surface->current;
layer_surface->current = layer_surface->pending;
diyac_output_update_usable_area(output);
layer_surface->current = old_state;
}
static void arrange_one_layer(const struct wlr_box *full_area, struct wlr_box *usable_area,
struct wlr_scene_tree *tree, bool exclusive)
{
struct wlr_scene_node *node;
wl_list_for_each(node, &tree->children, link)
{
struct diyac_layer_surface *surface = diyac_layer_surface_from_node(node);
struct wlr_scene_layer_surface_v1 *scene = surface->scene_layer_surface;
if (!!scene->layer_surface->current.exclusive_zone != exclusive)
{
continue;
}
wlr_scene_layer_surface_v1_configure(scene, full_area, usable_area);
}
}
/*
* To ensure outputs/views are left in a consistent state, this
* function should be called ONLY from output_update_usable_area()
* or output_update_all_usable_areas().
*/
void diyac_layers_arrange(struct diyac_output *output)
{
assert(output);
struct wlr_box full_area = {0};
wlr_output_effective_resolution(output->wlr_output,
&full_area.width, &full_area.height);
struct wlr_box usable_area = full_area;
struct diyac_server *server = output->server;
struct wlr_scene_output *scene_output =
wlr_scene_get_scene_output(server->scene, output->wlr_output);
if (!scene_output)
{
wlr_log(WLR_DEBUG, "no wlr_scene_output");
return;
}
for (int i = LAYER_TREE_SZ - 1; i >= 0; i--)
{
struct wlr_scene_tree *layer = output->layer_tree[i];
/*
* Process exclusive-zone clients before non-exclusive-zone
* clients, so that the latter give way to the former regardless
* of the order in which they were launched.
*
* Also start calculating the usable_area for exclusive-zone
* clients from the Overlay layer down to the Background layer
* to ensure that higher layers have a higher preference for
* placement.
*
* The 'exclusive' boolean also matches -1 which means that
* the layershell client wants to use the full screen rather
* than the usable area.
*/
arrange_one_layer(&full_area, &usable_area, layer, /* exclusive */ true);
}
for (size_t i = 0; i < LAYER_TREE_SZ; i++)
{
struct wlr_scene_tree *layer = output->layer_tree[i];
arrange_one_layer(&full_area, &usable_area, layer, /* exclusive */ false);
/* Set node position to account for output layout change */
wlr_scene_node_set_position(&layer->node, scene_output->x, scene_output->y);
}
output->usable_area = usable_area;
}