sway/sway/desktop/layer_shell.c

448 lines
14 KiB
C
Raw Normal View History

#include <stdbool.h>
2018-03-28 19:47:22 +00:00
#include <stdlib.h>
#include <string.h>
#include <wayland-server-core.h>
2018-09-14 17:21:44 +00:00
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_subcompositor.h>
#include <wlr/types/wlr_xdg_shell.h>
#include "log.h"
2024-01-18 15:04:51 +00:00
#include "sway/scene_descriptor.h"
#include "sway/desktop/transaction.h"
#include "sway/input/cursor.h"
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
2018-03-28 19:47:22 +00:00
#include "sway/layers.h"
#include "sway/output.h"
#include "sway/server.h"
#include "sway/tree/arrange.h"
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 11:00:10 +00:00
#include "sway/tree/workspace.h"
2018-03-28 19:47:22 +00:00
struct wlr_layer_surface_v1 *toplevel_layer_surface_from_surface(
struct wlr_surface *surface) {
struct wlr_layer_surface_v1 *layer;
do {
if (!surface) {
return NULL;
}
// Topmost layer surface
if ((layer = wlr_layer_surface_v1_try_from_wlr_surface(surface))) {
return layer;
}
// Layer subsurface
if (wlr_subsurface_try_from_wlr_surface(surface)) {
surface = wlr_surface_get_root_surface(surface);
continue;
}
// Layer surface popup
struct wlr_xdg_surface *xdg_surface = NULL;
if ((xdg_surface = wlr_xdg_surface_try_from_wlr_surface(surface)) &&
xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP && xdg_surface->popup != NULL) {
if (!xdg_surface->popup->parent) {
return NULL;
}
surface = wlr_surface_get_root_surface(xdg_surface->popup->parent);
continue;
}
// Return early if the surface is not a layer/xdg_popup/sub surface
return NULL;
} while (true);
}
2024-01-18 15:04:51 +00:00
static void arrange_surface(struct sway_output *output, const struct wlr_box *full_area,
struct wlr_box *usable_area, struct wlr_scene_tree *tree) {
struct wlr_scene_node *node;
wl_list_for_each(node, &tree->children, link) {
struct sway_layer_surface *surface = scene_descriptor_try_get(node,
SWAY_SCENE_DESC_LAYER_SHELL);
// surface could be null during destruction
if (!surface) {
continue;
}
2024-01-18 15:04:51 +00:00
if (!surface->scene->layer_surface->initialized) {
continue;
}
2024-01-18 15:04:51 +00:00
wlr_scene_layer_surface_v1_configure(surface->scene, full_area, usable_area);
}
}
void arrange_layers(struct sway_output *output) {
struct wlr_box usable_area = { 0 };
wlr_output_effective_resolution(output->wlr_output,
&usable_area.width, &usable_area.height);
2024-01-18 15:04:51 +00:00
const struct wlr_box full_area = usable_area;
2024-01-18 15:04:51 +00:00
arrange_surface(output, &full_area, &usable_area, output->layers.shell_background);
arrange_surface(output, &full_area, &usable_area, output->layers.shell_bottom);
arrange_surface(output, &full_area, &usable_area, output->layers.shell_top);
arrange_surface(output, &full_area, &usable_area, output->layers.shell_overlay);
if (!wlr_box_equal(&usable_area, &output->usable_area)) {
sway_log(SWAY_DEBUG, "Usable area changed, rearranging output");
2024-01-18 15:04:51 +00:00
output->usable_area = usable_area;
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 11:00:10 +00:00
arrange_output(output);
} else {
arrange_popups(root->layers.popup);
2018-03-29 22:24:29 +00:00
}
2024-01-18 15:04:51 +00:00
}
2024-01-18 15:04:51 +00:00
static struct wlr_scene_tree *sway_layer_get_scene(struct sway_output *output,
enum zwlr_layer_shell_v1_layer type) {
switch (type) {
case ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND:
return output->layers.shell_background;
case ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM:
return output->layers.shell_bottom;
case ZWLR_LAYER_SHELL_V1_LAYER_TOP:
return output->layers.shell_top;
case ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY:
return output->layers.shell_overlay;
}
2024-01-18 15:04:51 +00:00
sway_assert(false, "unreachable");
return NULL;
}
static struct sway_layer_surface *sway_layer_surface_create(
struct wlr_scene_layer_surface_v1 *scene) {
struct sway_layer_surface *surface = calloc(1, sizeof(*surface));
if (!surface) {
sway_log(SWAY_ERROR, "Could not allocate a scene_layer surface");
return NULL;
}
2024-01-18 15:04:51 +00:00
struct wlr_scene_tree *popups = wlr_scene_tree_create(root->layers.popup);
if (!popups) {
sway_log(SWAY_ERROR, "Could not allocate a scene_layer popup node");
free(surface);
return NULL;
}
surface->desc.relative = &scene->tree->node;
if (!scene_descriptor_assign(&popups->node,
SWAY_SCENE_DESC_POPUP, &surface->desc)) {
sway_log(SWAY_ERROR, "Failed to allocate a popup scene descriptor");
wlr_scene_node_destroy(&popups->node);
free(surface);
return NULL;
}
2024-01-18 15:04:51 +00:00
surface->tree = scene->tree;
surface->scene = scene;
surface->layer_surface = scene->layer_surface;
surface->popups = popups;
return surface;
2018-03-28 19:47:22 +00:00
}
Fix seat_set_focus_layer crash when disabling output This fixes the following crash: #0 0x00007f7daac3af25 in raise () at /usr/lib/libc.so.6 #1 0x00007f7daac24897 in abort () at /usr/lib/libc.so.6 #2 0x00007f7daac24767 in _nl_load_domain.cold () at /usr/lib/libc.so.6 #3 0x00007f7daac33526 in () at /usr/lib/libc.so.6 #4 0x0000555bfbc35029 in seat_set_focus_layer (seat=0x555bfd76d860, layer=0x555bfdda2ff0) at ../sway/input/seat.c:1164 __PRETTY_FUNCTION__ = "seat_set_focus_layer" __PRETTY_FUNCTION__ = "seat_set_focus_layer" #5 0x0000555bfbc35029 in seat_set_focus_layer (seat=seat@entry=0x555bfd76d860, layer=0x555bfdda2ff0) at ../sway/input/seat.c:1164 __PRETTY_FUNCTION__ = "seat_set_focus_layer" #6 0x0000555bfbc25899 in handle_output_destroy (listener=0x555bfdb90688, data=<optimized out>) at ../sway/desktop/layer_shell.c:263 layer = 0x555bfdd6b040 sway_layer = 0x555bfdb90610 seat = 0x555bfd76d860 client = 0x555bfdb76d70 set_focus = <optimized out> #7 0x0000555bfbc5b669 in wl_signal_emit (data=0x555bfd795930, signal=0x555bfd795ae0) at /usr/include/wayland-server-core.h:472 l = <optimized out> next = 0x555bfdb6a3e8 __PRETTY_FUNCTION__ = "output_disable" index = <optimized out> #8 0x0000555bfbc5b669 in output_disable (output=output@entry=0x555bfd795930) at ../sway/tree/output.c:263 __PRETTY_FUNCTION__ = "output_disable" index = <optimized out> #9 0x0000555bfbc3b890 in apply_output_config (oc=0x555bfd7d73d0, output=output@entry=0x555bfd795930) at ../sway/config/output.c:321 wlr_output = 0x555bfd7afaf0 scale = <optimized out> output_box = <optimized out> #10 0x0000555bfbc28309 in handle_output_manager_apply (listener=0x555bfbc7f148 <server+488>, data=0x555bfdca6eb0) at ../sway/desktop/output.c:936 wlr_output = <optimized out> output = 0x555bfd795930 oc = <optimized out> server = 0x555bfbc7ef60 <server> config = 0x555bfdca6eb0 config_head = 0x555bfdb79350 ok = true #11 0x00007f7dab4fbf7c in wlr_signal_emit_safe (signal=<optimized out>, data=0x555bfdca6eb0) at ../subprojects/wlroots/util/signal.c:29 pos = 0x555bfbc7f148 <server+488> l = 0x555bfbc7f148 <server+488> cursor = {link = {prev = 0x555bfbc7f148 <server+488>, next = 0x7fff238a8390}, notify = 0x7f7dab4fbef0 <handle_noop>} end = {link = {prev = 0x7fff238a8370, next = 0x555bfd7419f8}, notify = 0x7f7dab4fbef0 <handle_noop>} #12 0x00007f7daa45469a in ffi_call_unix64 () at /usr/lib/libffi.so.6 #13 0x00007f7daa453fb6 in ffi_call () at /usr/lib/libffi.so.6 #14 0x00007f7daae6f82f in () at /usr/lib/libwayland-server.so.0 #15 0x00007f7daae6c193 in () at /usr/lib/libwayland-server.so.0 #16 0x00007f7daae6d7f2 in wl_event_loop_dispatch () at /usr/lib/libwayland-server.so.0 #17 0x00007f7daae6c39c in wl_display_run () at /usr/lib/libwayland-server.so.0 This crash happens because focus can only be set on mapped surfaces.
2020-02-06 14:11:24 +00:00
static struct sway_layer_surface *find_mapped_layer_by_client(
2024-01-18 15:04:51 +00:00
struct wl_client *client, struct sway_output *ignore_output) {
for (int i = 0; i < root->outputs->length; ++i) {
struct sway_output *output = root->outputs->items[i];
2024-01-18 15:04:51 +00:00
if (output == ignore_output) {
continue;
}
// For now we'll only check the overlay layer
2024-01-18 15:04:51 +00:00
struct wlr_scene_node *node;
wl_list_for_each (node, &output->layers.shell_overlay->children, link) {
struct sway_layer_surface *surface = scene_descriptor_try_get(node,
SWAY_SCENE_DESC_LAYER_SHELL);
if (!surface) {
continue;
}
2024-01-18 15:04:51 +00:00
struct wlr_layer_surface_v1 *layer_surface = surface->layer_surface;
struct wl_resource *resource = layer_surface->resource;
Fix seat_set_focus_layer crash when disabling output This fixes the following crash: #0 0x00007f7daac3af25 in raise () at /usr/lib/libc.so.6 #1 0x00007f7daac24897 in abort () at /usr/lib/libc.so.6 #2 0x00007f7daac24767 in _nl_load_domain.cold () at /usr/lib/libc.so.6 #3 0x00007f7daac33526 in () at /usr/lib/libc.so.6 #4 0x0000555bfbc35029 in seat_set_focus_layer (seat=0x555bfd76d860, layer=0x555bfdda2ff0) at ../sway/input/seat.c:1164 __PRETTY_FUNCTION__ = "seat_set_focus_layer" __PRETTY_FUNCTION__ = "seat_set_focus_layer" #5 0x0000555bfbc35029 in seat_set_focus_layer (seat=seat@entry=0x555bfd76d860, layer=0x555bfdda2ff0) at ../sway/input/seat.c:1164 __PRETTY_FUNCTION__ = "seat_set_focus_layer" #6 0x0000555bfbc25899 in handle_output_destroy (listener=0x555bfdb90688, data=<optimized out>) at ../sway/desktop/layer_shell.c:263 layer = 0x555bfdd6b040 sway_layer = 0x555bfdb90610 seat = 0x555bfd76d860 client = 0x555bfdb76d70 set_focus = <optimized out> #7 0x0000555bfbc5b669 in wl_signal_emit (data=0x555bfd795930, signal=0x555bfd795ae0) at /usr/include/wayland-server-core.h:472 l = <optimized out> next = 0x555bfdb6a3e8 __PRETTY_FUNCTION__ = "output_disable" index = <optimized out> #8 0x0000555bfbc5b669 in output_disable (output=output@entry=0x555bfd795930) at ../sway/tree/output.c:263 __PRETTY_FUNCTION__ = "output_disable" index = <optimized out> #9 0x0000555bfbc3b890 in apply_output_config (oc=0x555bfd7d73d0, output=output@entry=0x555bfd795930) at ../sway/config/output.c:321 wlr_output = 0x555bfd7afaf0 scale = <optimized out> output_box = <optimized out> #10 0x0000555bfbc28309 in handle_output_manager_apply (listener=0x555bfbc7f148 <server+488>, data=0x555bfdca6eb0) at ../sway/desktop/output.c:936 wlr_output = <optimized out> output = 0x555bfd795930 oc = <optimized out> server = 0x555bfbc7ef60 <server> config = 0x555bfdca6eb0 config_head = 0x555bfdb79350 ok = true #11 0x00007f7dab4fbf7c in wlr_signal_emit_safe (signal=<optimized out>, data=0x555bfdca6eb0) at ../subprojects/wlroots/util/signal.c:29 pos = 0x555bfbc7f148 <server+488> l = 0x555bfbc7f148 <server+488> cursor = {link = {prev = 0x555bfbc7f148 <server+488>, next = 0x7fff238a8390}, notify = 0x7f7dab4fbef0 <handle_noop>} end = {link = {prev = 0x7fff238a8370, next = 0x555bfd7419f8}, notify = 0x7f7dab4fbef0 <handle_noop>} #12 0x00007f7daa45469a in ffi_call_unix64 () at /usr/lib/libffi.so.6 #13 0x00007f7daa453fb6 in ffi_call () at /usr/lib/libffi.so.6 #14 0x00007f7daae6f82f in () at /usr/lib/libwayland-server.so.0 #15 0x00007f7daae6c193 in () at /usr/lib/libwayland-server.so.0 #16 0x00007f7daae6d7f2 in wl_event_loop_dispatch () at /usr/lib/libwayland-server.so.0 #17 0x00007f7daae6c39c in wl_display_run () at /usr/lib/libwayland-server.so.0 This crash happens because focus can only be set on mapped surfaces.
2020-02-06 14:11:24 +00:00
if (wl_resource_get_client(resource) == client
2024-01-18 15:04:51 +00:00
&& layer_surface->surface->mapped) {
return surface;
}
}
}
return NULL;
}
2018-03-28 19:47:22 +00:00
static void handle_output_destroy(struct wl_listener *listener, void *data) {
2024-01-18 15:04:51 +00:00
struct sway_layer_surface *layer =
wl_container_of(listener, layer, output_destroy);
layer->output = NULL;
wlr_scene_node_destroy(&layer->scene->tree->node);
}
static void handle_node_destroy(struct wl_listener *listener, void *data) {
struct sway_layer_surface *layer =
wl_container_of(listener, layer, node_destroy);
// destroy the scene descriptor straight away if it exists, otherwise
// we will try to reflow still considering the destroyed node.
scene_descriptor_destroy(&layer->tree->node, SWAY_SCENE_DESC_LAYER_SHELL);
// 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.
struct sway_seat *seat = input_manager_get_default_seat();
struct wl_client *client =
2024-01-18 15:04:51 +00:00
wl_resource_get_client(layer->layer_surface->resource);
if (!server.session_lock.lock) {
struct sway_layer_surface *consider_layer =
find_mapped_layer_by_client(client, layer->output);
if (consider_layer) {
seat_set_focus_layer(seat, consider_layer->layer_surface);
}
}
2024-01-18 15:04:51 +00:00
if (layer->output) {
arrange_layers(layer->output);
transaction_commit_dirty();
}
wlr_scene_node_destroy(&layer->popups->node);
wl_list_remove(&layer->map.link);
wl_list_remove(&layer->unmap.link);
wl_list_remove(&layer->surface_commit.link);
wl_list_remove(&layer->node_destroy.link);
wl_list_remove(&layer->output_destroy.link);
free(layer);
2018-03-28 19:47:22 +00:00
}
static void handle_surface_commit(struct wl_listener *listener, void *data) {
2024-01-18 15:04:51 +00:00
struct sway_layer_surface *surface =
wl_container_of(listener, surface, surface_commit);
struct wlr_layer_surface_v1 *layer_surface = surface->layer_surface;
if (!layer_surface->initialized) {
return;
}
2024-01-18 15:04:51 +00:00
uint32_t committed = layer_surface->current.committed;
if (committed & WLR_LAYER_SURFACE_V1_STATE_LAYER) {
enum zwlr_layer_shell_v1_layer layer_type = layer_surface->current.layer;
struct wlr_scene_tree *output_layer = sway_layer_get_scene(
surface->output, layer_type);
wlr_scene_node_reparent(&surface->scene->tree->node, output_layer);
}
2024-01-18 15:04:51 +00:00
if (layer_surface->initial_commit || committed || layer_surface->surface->mapped != surface->mapped) {
surface->mapped = layer_surface->surface->mapped;
arrange_layers(surface->output);
transaction_commit_dirty();
}
}
2024-01-18 15:04:51 +00:00
static void handle_map(struct wl_listener *listener, void *data) {
struct sway_layer_surface *surface = wl_container_of(listener,
surface, map);
struct wlr_layer_surface_v1 *layer_surface =
surface->scene->layer_surface;
// focus on new surface
if (layer_surface->current.keyboard_interactive &&
(layer_surface->current.layer == ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY ||
layer_surface->current.layer == ZWLR_LAYER_SHELL_V1_LAYER_TOP)) {
struct sway_seat *seat;
wl_list_for_each(seat, &server.input->seats, link) {
// but only if the currently focused layer has a lower precedence
if (!seat->focused_layer ||
seat->focused_layer->current.layer >= layer_surface->current.layer) {
seat_set_focus_layer(seat, layer_surface);
}
}
arrange_layers(surface->output);
}
cursor_rebase_all();
2018-03-28 19:47:22 +00:00
}
static void handle_unmap(struct wl_listener *listener, void *data) {
2024-01-18 15:04:51 +00:00
struct sway_layer_surface *surface = wl_container_of(
listener, surface, unmap);
struct sway_seat *seat;
wl_list_for_each(seat, &server.input->seats, link) {
2024-01-18 15:04:51 +00:00
if (seat->focused_layer == surface->layer_surface) {
seat_set_focus_layer(seat, NULL);
}
}
cursor_rebase_all();
}
2024-01-18 15:04:51 +00:00
static void popup_handle_destroy(struct wl_listener *listener, void *data) {
struct sway_layer_popup *popup =
wl_container_of(listener, popup, destroy);
2019-07-05 18:45:11 +00:00
2024-01-18 15:04:51 +00:00
wl_list_remove(&popup->destroy.link);
wl_list_remove(&popup->new_popup.link);
wl_list_remove(&popup->commit.link);
free(popup);
2019-07-05 18:45:11 +00:00
}
2023-07-11 12:09:14 +00:00
static void popup_unconstrain(struct sway_layer_popup *popup) {
struct wlr_xdg_popup *wlr_popup = popup->wlr_popup;
2024-01-18 15:04:51 +00:00
struct sway_output *output = popup->toplevel->output;
// if a client tries to create a popup while we are in the process of destroying
// its output, don't crash.
if (!output) {
return;
}
2023-07-11 12:09:14 +00:00
2024-01-18 15:04:51 +00:00
int lx, ly;
wlr_scene_node_coords(&popup->toplevel->scene->tree->node, &lx, &ly);
2023-07-11 12:09:14 +00:00
// the output box expressed in the coordinate system of the toplevel parent
// of the popup
struct wlr_box output_toplevel_sx_box = {
2024-01-18 15:04:51 +00:00
.x = output->lx - lx,
.y = output->ly - ly,
2023-07-11 12:09:14 +00:00
.width = output->width,
.height = output->height,
};
wlr_xdg_popup_unconstrain_from_box(wlr_popup, &output_toplevel_sx_box);
}
2019-07-05 18:45:11 +00:00
static void popup_handle_commit(struct wl_listener *listener, void *data) {
struct sway_layer_popup *popup = wl_container_of(listener, popup, commit);
2023-07-11 12:09:14 +00:00
if (popup->wlr_popup->base->initial_commit) {
popup_unconstrain(popup);
}
2019-07-05 18:45:11 +00:00
}
static void popup_handle_new_popup(struct wl_listener *listener, void *data);
static struct sway_layer_popup *create_popup(struct wlr_xdg_popup *wlr_popup,
2024-01-18 15:04:51 +00:00
struct sway_layer_surface *toplevel, struct wlr_scene_tree *parent) {
struct sway_layer_popup *popup = calloc(1, sizeof(*popup));
2019-07-05 18:45:11 +00:00
if (popup == NULL) {
return NULL;
}
2024-01-18 15:04:51 +00:00
popup->toplevel = toplevel;
2019-07-05 18:45:11 +00:00
popup->wlr_popup = wlr_popup;
2024-01-18 15:04:51 +00:00
popup->scene = wlr_scene_xdg_surface_create(parent,
wlr_popup->base);
if (!popup->scene) {
free(popup);
return NULL;
}
2019-07-05 18:45:11 +00:00
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);
2024-01-18 15:04:51 +00:00
popup->commit.notify = popup_handle_commit;
wl_signal_add(&wlr_popup->base->surface->events.commit, &popup->commit);
2019-07-05 18:45:11 +00:00
return popup;
}
static void popup_handle_new_popup(struct wl_listener *listener, void *data) {
struct sway_layer_popup *sway_layer_popup =
wl_container_of(listener, sway_layer_popup, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
2024-01-18 15:04:51 +00:00
create_popup(wlr_popup, sway_layer_popup->toplevel, sway_layer_popup->scene);
2019-07-05 18:45:11 +00:00
}
static void handle_new_popup(struct wl_listener *listener, void *data) {
struct sway_layer_surface *sway_layer_surface =
wl_container_of(listener, sway_layer_surface, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
2024-01-18 15:04:51 +00:00
create_popup(wlr_popup, sway_layer_surface, sway_layer_surface->popups);
}
2018-03-28 19:47:22 +00:00
void handle_layer_shell_surface(struct wl_listener *listener, void *data) {
2018-09-14 17:21:44 +00:00
struct wlr_layer_surface_v1 *layer_surface = data;
2020-06-04 13:43:42 +00:00
sway_log(SWAY_DEBUG, "new layer surface: namespace %s layer %d anchor %" PRIu32
" size %" PRIu32 "x%" PRIu32 " margin %" PRIu32 ",%" PRIu32 ",%" PRIu32 ",%" PRIu32 ",",
layer_surface->namespace,
layer_surface->pending.layer,
layer_surface->pending.anchor,
layer_surface->pending.desired_width,
layer_surface->pending.desired_height,
layer_surface->pending.margin.top,
layer_surface->pending.margin.right,
layer_surface->pending.margin.bottom,
layer_surface->pending.margin.left);
2018-03-28 19:47:22 +00:00
2018-04-22 17:27:34 +00:00
if (!layer_surface->output) {
// Assign last active output
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 11:00:10 +00:00
struct sway_output *output = NULL;
struct sway_seat *seat = input_manager_get_default_seat();
2018-04-22 17:27:34 +00:00
if (seat) {
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 11:00:10 +00:00
struct sway_workspace *ws = seat_get_focused_workspace(seat);
if (ws != NULL) {
output = ws->output;
}
2018-04-22 17:27:34 +00:00
}
if (!output || output == root->fallback_output) {
if (!root->outputs->length) {
sway_log(SWAY_ERROR,
"no output to auto-assign layer surface '%s' to",
layer_surface->namespace);
wlr_layer_surface_v1_destroy(layer_surface);
2018-04-22 17:27:34 +00:00
return;
}
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 11:00:10 +00:00
output = root->outputs->items[0];
2018-04-22 17:27:34 +00:00
}
Implement type safe arguments and demote sway_container This commit changes the meaning of sway_container so that it only refers to layout containers and view containers. Workspaces, outputs and the root are no longer known as containers. Instead, root, outputs, workspaces and containers are all a type of node, and containers come in two types: layout containers and view containers. In addition to the above, this implements type safe variables. This means we use specific types such as sway_output and sway_workspace instead of generic containers or nodes. However, it's worth noting that in a few places places (eg. seat focus and transactions) referring to them in a generic way is unavoidable which is why we still use nodes in some places. If you want a TL;DR, look at node.h, as well as the struct definitions for root, output, workspace and container. Note that sway_output now contains a workspaces list, and workspaces now contain a tiling and floating list, and containers now contain a pointer back to the workspace. There are now functions for seat_get_focused_workspace and seat_get_focused_container. The latter will return NULL if a workspace itself is focused. Most other seat functions like seat_get_focus and seat_set_focus now accept and return nodes. In the config->handler_context struct, current_container has been replaced with three pointers: node, container and workspace. node is the same as what current_container was, while workspace is the workspace that the node resides on and container is the actual container, which may be NULL if a workspace itself is focused. The global root_container variable has been replaced with one simply called root, which is a pointer to the sway_root instance. The way outputs are created, enabled, disabled and destroyed has changed. Previously we'd wrap the sway_output in a container when it is enabled, but as we don't have containers any more it needs a different approach. The output_create and output_destroy functions previously created/destroyed the container, but now they create/destroy the sway_output. There is a new function output_disable to disable an output without destroying it. Containers have a new view property. If this is populated then the container is a view container, otherwise it's a layout container. Like before, this property is immutable for the life of the container. Containers have both a `sway_container *parent` and `sway_workspace *workspace`. As we use specific types now, parent cannot point to a workspace so it'll be NULL for containers which are direct children of the workspace. The workspace property is set for all containers, except those which are hidden in the scratchpad as they have no workspace. In some cases we need to refer to workspaces in a container-like way. For example, workspaces have layout and children, but when using specific types this makes it difficult. Likewise, it's difficult for a container to get its parent's layout when the parent could be another container or a workspace. To make it easier, some helper functions have been created: container_parent_layout and container_get_siblings. container_remove_child has been renamed to container_detach and container_replace_child has been renamed to container_replace. `container_handle_fullscreen_reparent(con, old_parent)` has had the old_parent removed. We now unfullscreen the workspace when detaching the container, so this function is simplified and only needs one argument now. container_notify_subtree_changed has been renamed to container_update_representation. This is more descriptive of its purpose. I also wanted to be able to call it with whatever container was changed rather than the container's parent, which makes bubbling up to the workspace easier. There are now state structs per node thing. ie. sway_output_state, sway_workspace_state and sway_container_state. The focus, move and layout commands have been completely refactored to work with the specific types. I considered making these a separate PR, but I'd be backporting my changes only to replace them again, and it's easier just to test everything at once.
2018-08-30 11:00:10 +00:00
layer_surface->output = output->wlr_output;
2018-04-22 17:27:34 +00:00
}
2024-01-18 15:04:51 +00:00
struct sway_output *output = layer_surface->output->data;
enum zwlr_layer_shell_v1_layer layer_type = layer_surface->pending.layer;
struct wlr_scene_tree *output_layer = sway_layer_get_scene(
output, layer_type);
struct wlr_scene_layer_surface_v1 *scene_surface =
wlr_scene_layer_surface_v1_create(output_layer, layer_surface);
if (!scene_surface) {
sway_log(SWAY_ERROR, "Could not allocate a layer_surface_v1");
return;
}
2024-01-18 15:04:51 +00:00
struct sway_layer_surface *surface =
sway_layer_surface_create(scene_surface);
if (!surface) {
wlr_layer_surface_v1_destroy(layer_surface);
2024-01-18 15:04:51 +00:00
sway_log(SWAY_ERROR, "Could not allocate a sway_layer_surface");
return;
}
2018-03-28 19:47:22 +00:00
2024-01-18 15:04:51 +00:00
if (!scene_descriptor_assign(&scene_surface->tree->node,
SWAY_SCENE_DESC_LAYER_SHELL, surface)) {
sway_log(SWAY_ERROR, "Failed to allocate a layer surface descriptor");
// destroying the layer_surface will also destroy its corresponding
// scene node
wlr_layer_surface_v1_destroy(layer_surface);
return;
}
2024-01-18 15:04:51 +00:00
surface->output = output;
surface->surface_commit.notify = handle_surface_commit;
wl_signal_add(&layer_surface->surface->events.commit,
&surface->surface_commit);
surface->map.notify = handle_map;
wl_signal_add(&layer_surface->surface->events.map, &surface->map);
surface->unmap.notify = handle_unmap;
wl_signal_add(&layer_surface->surface->events.unmap, &surface->unmap);
surface->new_popup.notify = handle_new_popup;
wl_signal_add(&layer_surface->events.new_popup, &surface->new_popup);
surface->output_destroy.notify = handle_output_destroy;
wl_signal_add(&output->events.disable, &surface->output_destroy);
surface->node_destroy.notify = handle_node_destroy;
wl_signal_add(&scene_surface->tree->node.events.destroy, &surface->node_destroy);
2018-03-28 19:47:22 +00:00
}