2021-09-24 21:45:48 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2020-09-30 17:18:20 +01:00
|
|
|
/*
|
|
|
|
|
* layers.c - layer-shell implementation
|
|
|
|
|
*
|
2022-02-26 21:15:52 +00:00
|
|
|
* Based on
|
2020-10-28 20:49:26 +00:00
|
|
|
* - https://git.sr.ht/~sircmpwm/wio
|
|
|
|
|
* - https://github.com/swaywm/sway
|
2020-09-30 17:18:20 +01:00
|
|
|
* Copyright (C) 2019 Drew DeVault and Sway developers
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <wayland-server.h>
|
|
|
|
|
#include <wlr/types/wlr_layer_shell_v1.h>
|
2021-07-23 21:15:55 +01:00
|
|
|
#include <wlr/util/log.h>
|
2020-09-30 17:18:20 +01:00
|
|
|
#include "layers.h"
|
|
|
|
|
#include "labwc.h"
|
2022-02-26 21:15:52 +00:00
|
|
|
#include "node-descriptor.h"
|
2020-09-30 17:18:20 +01:00
|
|
|
|
2022-02-20 13:15:58 +00:00
|
|
|
void
|
|
|
|
|
arrange_layers(struct output *output)
|
2020-09-30 17:18:20 +01:00
|
|
|
{
|
|
|
|
|
struct wlr_box full_area = { 0 };
|
2022-02-20 13:15:58 +00:00
|
|
|
wlr_output_effective_resolution(output->wlr_output,
|
2022-02-26 21:15:52 +00:00
|
|
|
&full_area.width, &full_area.height);
|
2022-02-20 13:15:58 +00:00
|
|
|
struct wlr_box usable_area = full_area;
|
|
|
|
|
|
2022-03-02 20:21:57 +00:00
|
|
|
struct 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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 15:00:20 +00:00
|
|
|
for (int i = 0; i < LAB_NR_LAYERS; i++) {
|
2022-02-20 13:15:58 +00:00
|
|
|
struct lab_layer_surface *lab_layer_surface;
|
|
|
|
|
wl_list_for_each(lab_layer_surface, &output->layers[i], link) {
|
|
|
|
|
struct wlr_scene_layer_surface_v1 *scene_layer_surface =
|
|
|
|
|
lab_layer_surface->scene_layer_surface;
|
|
|
|
|
wlr_scene_layer_surface_v1_configure(
|
|
|
|
|
scene_layer_surface, &full_area, &usable_area);
|
2020-09-30 17:18:20 +01:00
|
|
|
}
|
2022-03-02 20:21:57 +00:00
|
|
|
|
|
|
|
|
wlr_scene_node_set_position(&output->layer_tree[i]->node,
|
|
|
|
|
scene_output->x, scene_output->y);
|
2022-02-20 13:15:58 +00:00
|
|
|
}
|
2020-09-30 17:18:20 +01:00
|
|
|
|
2022-02-20 14:53:21 +00:00
|
|
|
memcpy(&output->usable_area, &usable_area, sizeof(struct wlr_box));
|
2020-09-30 17:18:20 +01:00
|
|
|
|
|
|
|
|
/* Find topmost keyboard interactive layer, if such a layer exists */
|
|
|
|
|
uint32_t layers_above_shell[] = {
|
|
|
|
|
ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY,
|
|
|
|
|
ZWLR_LAYER_SHELL_V1_LAYER_TOP,
|
|
|
|
|
};
|
2022-03-02 20:23:20 +00:00
|
|
|
size_t nlayers = sizeof(layers_above_shell)
|
|
|
|
|
/ sizeof(layers_above_shell[0]);
|
2020-09-30 17:18:20 +01:00
|
|
|
struct lab_layer_surface *layer, *topmost = NULL;
|
|
|
|
|
for (size_t i = 0; i < nlayers; ++i) {
|
|
|
|
|
wl_list_for_each_reverse (layer,
|
|
|
|
|
&output->layers[layers_above_shell[i]], link) {
|
2022-03-02 20:23:20 +00:00
|
|
|
struct wlr_layer_surface_v1 *layer_surface =
|
|
|
|
|
layer->scene_layer_surface->layer_surface;
|
|
|
|
|
if (layer_surface->current.keyboard_interactive) {
|
2020-09-30 17:18:20 +01:00
|
|
|
topmost = layer;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-28 20:49:26 +00:00
|
|
|
if (topmost) {
|
2020-09-30 17:18:20 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-31 15:09:13 +00:00
|
|
|
struct seat *seat = &output->server->seat;
|
2020-10-28 20:49:26 +00:00
|
|
|
if (topmost) {
|
2022-02-20 13:15:58 +00:00
|
|
|
seat_set_focus_layer(seat,
|
|
|
|
|
topmost->scene_layer_surface->layer_surface);
|
2020-10-31 15:09:13 +00:00
|
|
|
} else if (seat->focused_layer &&
|
|
|
|
|
!seat->focused_layer->current.keyboard_interactive) {
|
|
|
|
|
seat_set_focus_layer(seat, NULL);
|
2020-10-28 20:49:26 +00:00
|
|
|
}
|
2022-02-20 14:53:21 +00:00
|
|
|
/* FIXME: should we call a desktop_arrange_all_views() here? */
|
2020-10-28 20:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-30 17:18:20 +01:00
|
|
|
static void
|
2020-10-28 21:28:37 +00:00
|
|
|
output_destroy_notify(struct wl_listener *listener, void *data)
|
2020-09-30 17:18:20 +01:00
|
|
|
{
|
|
|
|
|
struct lab_layer_surface *layer =
|
|
|
|
|
wl_container_of(listener, layer, output_destroy);
|
|
|
|
|
wl_list_remove(&layer->output_destroy.link);
|
2022-02-20 13:15:58 +00:00
|
|
|
wlr_layer_surface_v1_destroy(layer->scene_layer_surface->layer_surface);
|
2020-09-30 17:18:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-10-28 21:28:37 +00:00
|
|
|
surface_commit_notify(struct wl_listener *listener, void *data)
|
2020-09-30 17:18:20 +01:00
|
|
|
{
|
|
|
|
|
struct lab_layer_surface *layer =
|
|
|
|
|
wl_container_of(listener, layer, surface_commit);
|
2022-02-20 13:15:58 +00:00
|
|
|
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;
|
2021-10-15 19:14:07 +01:00
|
|
|
|
|
|
|
|
if (!wlr_output) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layer_surface->current.committed
|
|
|
|
|
|| layer->mapped != layer_surface->mapped) {
|
|
|
|
|
layer->mapped = layer_surface->mapped;
|
|
|
|
|
struct output *output =
|
|
|
|
|
output_from_wlr_output(layer->server, wlr_output);
|
|
|
|
|
arrange_layers(output);
|
|
|
|
|
}
|
2020-09-30 17:18:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-31 15:09:13 +00:00
|
|
|
static void
|
|
|
|
|
unmap(struct lab_layer_surface *layer)
|
|
|
|
|
{
|
|
|
|
|
struct seat *seat = &layer->server->seat;
|
2022-02-20 13:15:58 +00:00
|
|
|
if (seat->focused_layer == layer->scene_layer_surface->layer_surface) {
|
2020-10-31 15:09:13 +00:00
|
|
|
seat_set_focus_layer(seat, NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 17:18:20 +01:00
|
|
|
static void
|
2020-10-28 21:28:37 +00:00
|
|
|
destroy_notify(struct wl_listener *listener, void *data)
|
2020-09-30 17:18:20 +01:00
|
|
|
{
|
|
|
|
|
struct lab_layer_surface *layer = wl_container_of(
|
|
|
|
|
listener, layer, destroy);
|
2022-02-20 13:15:58 +00:00
|
|
|
if (layer->scene_layer_surface->layer_surface->mapped) {
|
2020-10-31 15:09:13 +00:00
|
|
|
unmap(layer);
|
|
|
|
|
}
|
2022-02-20 13:15:58 +00:00
|
|
|
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_list_remove(&layer->link);
|
|
|
|
|
wl_list_remove(&layer->destroy.link);
|
|
|
|
|
wl_list_remove(&layer->map.link);
|
2022-02-26 21:15:52 +00:00
|
|
|
wl_list_remove(&layer->unmap.link);
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_list_remove(&layer->surface_commit.link);
|
2022-02-20 13:15:58 +00:00
|
|
|
if (layer->scene_layer_surface->layer_surface->output) {
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_list_remove(&layer->output_destroy.link);
|
2022-02-26 21:15:52 +00:00
|
|
|
struct output *output = output_from_wlr_output(layer->server,
|
|
|
|
|
layer->scene_layer_surface->layer_surface->output);
|
2020-10-28 20:49:26 +00:00
|
|
|
arrange_layers(output);
|
2020-09-30 17:18:20 +01:00
|
|
|
}
|
|
|
|
|
free(layer);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 15:09:13 +00:00
|
|
|
static void
|
|
|
|
|
unmap_notify(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
2022-02-26 21:15:52 +00:00
|
|
|
return;
|
|
|
|
|
struct lab_layer_surface *lab_layer_surface =
|
|
|
|
|
wl_container_of(listener, lab_layer_surface, unmap);
|
|
|
|
|
unmap(lab_layer_surface);
|
2020-10-31 15:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-30 17:18:20 +01:00
|
|
|
static void
|
2020-10-28 21:28:37 +00:00
|
|
|
map_notify(struct wl_listener *listener, void *data)
|
2020-09-30 17:18:20 +01:00
|
|
|
{
|
2022-02-26 21:15:52 +00:00
|
|
|
return;
|
|
|
|
|
struct wlr_layer_surface_v1 *layer_surface = data;
|
|
|
|
|
wlr_surface_send_enter(layer_surface->surface, layer_surface->output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct
|
|
|
|
|
lab_layer_surface *popup_get_layer(struct lab_layer_popup *popup)
|
|
|
|
|
{
|
|
|
|
|
struct wlr_scene_node *node = popup->scene_node;
|
|
|
|
|
while (node) {
|
|
|
|
|
if (node->data) {
|
|
|
|
|
struct node_descriptor *desc = node->data;
|
|
|
|
|
if (desc->type == LAB_NODE_DESC_LAYER_SURFACE) {
|
|
|
|
|
return desc->data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
node = node->parent;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
popup_handle_destroy(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct lab_layer_popup *popup =
|
|
|
|
|
wl_container_of(listener, popup, destroy);
|
|
|
|
|
wl_list_remove(&popup->destroy.link);
|
|
|
|
|
wl_list_remove(&popup->new_popup.link);
|
|
|
|
|
free(popup);
|
2020-09-30 17:18:20 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-26 21:15:52 +00:00
|
|
|
static void
|
|
|
|
|
popup_unconstrain(struct lab_layer_popup *popup)
|
|
|
|
|
{
|
|
|
|
|
struct lab_layer_surface *layer = popup_get_layer(popup);
|
|
|
|
|
if (!layer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
struct wlr_xdg_popup *wlr_popup = popup->wlr_popup;
|
|
|
|
|
struct output *output =
|
|
|
|
|
layer->scene_layer_surface->layer_surface->output->data;
|
|
|
|
|
|
|
|
|
|
struct wlr_box output_box = { 0 };
|
|
|
|
|
wlr_output_effective_resolution(output->wlr_output, &output_box.width,
|
|
|
|
|
&output_box.height);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Output geometry expressed in the coordinate system of the toplevel
|
|
|
|
|
* parent of popup
|
|
|
|
|
*/
|
|
|
|
|
struct wlr_box output_toplevel_sx_box = {
|
|
|
|
|
.x = -layer->geo.x,
|
|
|
|
|
.y = -layer->geo.y,
|
|
|
|
|
.width = output_box.width,
|
|
|
|
|
.height = output_box.height,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
wlr_xdg_popup_unconstrain_from_box(wlr_popup, &output_toplevel_sx_box);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void popup_handle_new_popup(struct wl_listener *listener, void *data);
|
|
|
|
|
|
|
|
|
|
static struct lab_layer_popup *
|
|
|
|
|
create_popup(struct wlr_xdg_popup *wlr_popup, struct wlr_scene_node *parent)
|
|
|
|
|
{
|
|
|
|
|
struct lab_layer_popup *popup =
|
|
|
|
|
calloc(1, sizeof(struct lab_layer_popup));
|
|
|
|
|
if (!popup) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
popup->wlr_popup = wlr_popup;
|
|
|
|
|
popup->scene_node =
|
|
|
|
|
wlr_scene_xdg_surface_create(parent, wlr_popup->base);
|
|
|
|
|
if (!popup->scene_node) {
|
|
|
|
|
free(popup);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
node_descriptor_create(popup->scene_node,
|
|
|
|
|
LAB_NODE_DESC_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);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* FIXME: should we put popups in ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY
|
|
|
|
|
* or a dedicated output->layer_popup_tree - so that for example
|
|
|
|
|
* a panel in the bottom layer displays any popup above views.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
popup_unconstrain(popup);
|
|
|
|
|
return popup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
popup_handle_new_popup(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct lab_layer_popup *lab_layer_popup =
|
|
|
|
|
wl_container_of(listener, lab_layer_popup, new_popup);
|
|
|
|
|
struct wlr_xdg_popup *wlr_popup = data;
|
|
|
|
|
create_popup(wlr_popup, lab_layer_popup->scene_node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
new_popup_notify(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct lab_layer_surface *lab_layer_surface =
|
|
|
|
|
wl_container_of(listener, lab_layer_surface, new_popup);
|
|
|
|
|
struct wlr_xdg_popup *wlr_popup = data;
|
|
|
|
|
create_popup(wlr_popup, lab_layer_surface->scene_layer_surface->node);
|
|
|
|
|
}
|
2021-10-20 22:32:46 +01:00
|
|
|
|
2020-10-28 20:49:26 +00:00
|
|
|
static void
|
|
|
|
|
new_layer_surface_notify(struct wl_listener *listener, void *data)
|
2020-09-30 17:18:20 +01:00
|
|
|
{
|
|
|
|
|
struct server *server = wl_container_of(
|
|
|
|
|
listener, server, new_layer_surface);
|
|
|
|
|
struct wlr_layer_surface_v1 *layer_surface = data;
|
2020-10-28 20:49:26 +00:00
|
|
|
|
2020-09-30 17:18:20 +01:00
|
|
|
if (!layer_surface->output) {
|
|
|
|
|
struct wlr_output *output = wlr_output_layout_output_at(
|
2020-10-02 21:19:56 +01:00
|
|
|
server->output_layout, server->seat.cursor->x,
|
|
|
|
|
server->seat.cursor->y);
|
2020-09-30 17:18:20 +01:00
|
|
|
layer_surface->output = output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct lab_layer_surface *surface =
|
|
|
|
|
calloc(1, sizeof(struct lab_layer_surface));
|
|
|
|
|
if (!surface) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 21:28:37 +00:00
|
|
|
surface->surface_commit.notify = surface_commit_notify;
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_signal_add(&layer_surface->surface->events.commit,
|
|
|
|
|
&surface->surface_commit);
|
2020-10-28 21:28:37 +00:00
|
|
|
|
|
|
|
|
surface->destroy.notify = destroy_notify;
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_signal_add(&layer_surface->events.destroy, &surface->destroy);
|
2020-10-28 21:28:37 +00:00
|
|
|
|
|
|
|
|
surface->map.notify = map_notify;
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_signal_add(&layer_surface->events.map, &surface->map);
|
|
|
|
|
|
2020-10-31 15:09:13 +00:00
|
|
|
surface->unmap.notify = unmap_notify;
|
|
|
|
|
wl_signal_add(&layer_surface->events.unmap, &surface->unmap);
|
|
|
|
|
|
2022-02-26 21:15:52 +00:00
|
|
|
surface->new_popup.notify = new_popup_notify;
|
|
|
|
|
wl_signal_add(&layer_surface->events.new_popup, &surface->new_popup);
|
2022-02-20 13:15:58 +00:00
|
|
|
|
|
|
|
|
struct output *output = layer_surface->output->data;
|
2021-10-20 22:32:46 +01:00
|
|
|
|
2022-02-20 13:15:58 +00:00
|
|
|
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->node, layer_surface);
|
2022-02-26 21:15:52 +00:00
|
|
|
node_descriptor_create(surface->scene_layer_surface->node,
|
|
|
|
|
LAB_NODE_DESC_LAYER_SURFACE, surface);
|
2021-10-20 22:32:46 +01:00
|
|
|
|
|
|
|
|
surface->server = server;
|
2022-02-20 13:15:58 +00:00
|
|
|
surface->scene_layer_surface->layer_surface = layer_surface;
|
|
|
|
|
|
2021-10-20 22:32:46 +01:00
|
|
|
surface->output_destroy.notify = output_destroy_notify;
|
|
|
|
|
wl_signal_add(&layer_surface->output->events.destroy,
|
|
|
|
|
&surface->output_destroy);
|
|
|
|
|
|
|
|
|
|
if (!output) {
|
|
|
|
|
wlr_log(WLR_ERROR, "no output for layer");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 20:51:12 +01:00
|
|
|
wl_list_insert(&output->layers[layer_surface->pending.layer],
|
2020-09-30 17:18:20 +01:00
|
|
|
&surface->link);
|
|
|
|
|
/*
|
2021-09-24 20:51:12 +01:00
|
|
|
* Temporarily set the layer's current state to pending so that
|
2020-09-30 17:18:20 +01:00
|
|
|
* it can easily be arranged.
|
|
|
|
|
*/
|
|
|
|
|
struct wlr_layer_surface_v1_state old_state = layer_surface->current;
|
2021-09-24 20:51:12 +01:00
|
|
|
layer_surface->current = layer_surface->pending;
|
2020-09-30 17:18:20 +01:00
|
|
|
arrange_layers(output);
|
|
|
|
|
layer_surface->current = old_state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
layers_init(struct server *server)
|
|
|
|
|
{
|
|
|
|
|
server->layer_shell = wlr_layer_shell_v1_create(server->wl_display);
|
2020-10-28 20:49:26 +00:00
|
|
|
server->new_layer_surface.notify = new_layer_surface_notify;
|
2020-09-30 17:18:20 +01:00
|
|
|
wl_signal_add(&server->layer_shell->events.new_surface,
|
|
|
|
|
&server->new_layer_surface);
|
|
|
|
|
}
|