Migrated layer shell to the scene graph API

This commit is contained in:
Keith Bowes 2022-02-28 09:07:17 -05:00
parent d7b799ab80
commit 5a3ac6055d
7 changed files with 415 additions and 304 deletions

View file

@ -15,9 +15,17 @@ struct wb_output {
struct wlr_output *wlr_output;
struct wb_server *server;
struct wl_list layers[4]; /* wb_layer_surface::link */
struct {
struct wlr_scene_node *shell_background;
struct wlr_scene_node *shell_bottom;
struct wlr_scene_node *shell_fullscreen;
struct wlr_scene_node *shell_overlay;
struct wlr_scene_node *shell_top;
} layers;
#if !WLR_CHECK_VERSION(0, 16, 0)
struct wlr_scene_rect *scene_rect;
#endif
struct wl_listener destroy;
struct wl_listener frame;

View file

@ -11,6 +11,8 @@ struct wb_server;
struct wb_seat {
struct wlr_seat *seat;
struct wlr_layer_surface_v1 *focused_layer;
struct wl_list keyboards;
struct wl_listener request_set_primary_selection;
@ -28,5 +30,6 @@ struct wb_keyboard {
struct wb_server;
struct wb_seat *wb_seat_create(struct wb_server *server);
void seat_set_focus_layer(struct wb_seat *seat, struct wlr_layer_surface_v1 *layer);
void wb_seat_destroy(struct wb_seat *seat);
#endif

View file

@ -6,278 +6,351 @@
* Copyright 2022 Sway Developers
*/
#include <wlr/types/wlr_layer_shell_v1.h>
#include <wlr/types/wlr_scene.h>
#include "waybox/xdg_shell.h"
static void apply_exclusive(struct wlr_box *usable_area,
uint32_t anchor, int32_t exclusive,
int32_t margin_top, int32_t margin_right,
int32_t margin_bottom, int32_t margin_left) {
if (exclusive <= 0) {
static void descriptor_destroy(struct wb_scene_descriptor *desc) {
if (!desc) {
return;
}
struct {
uint32_t anchors;
int *positive_axis;
int *negative_axis;
int margin;
} edges[] = {
{
.anchors =
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP,
.positive_axis = &usable_area->y,
.negative_axis = &usable_area->height,
.margin = margin_top,
},
{
.anchors =
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM,
.positive_axis = NULL,
.negative_axis = &usable_area->height,
.margin = margin_bottom,
},
{
.anchors =
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM,
.positive_axis = &usable_area->x,
.negative_axis = &usable_area->width,
.margin = margin_left,
},
{
.anchors =
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM,
.positive_axis = NULL,
.negative_axis = &usable_area->width,
.margin = margin_right,
},
};
for (size_t i = 0; i < sizeof(edges) / sizeof(edges[0]); ++i) {
if ((anchor & edges[i].anchors) == edges[i].anchors) {
if (edges[i].positive_axis) {
*edges[i].positive_axis += exclusive + edges[i].margin;
}
if (edges[i].negative_axis) {
*edges[i].negative_axis -= exclusive + edges[i].margin;
}
}
}
wl_list_remove(&desc->destroy.link);
free(desc);
}
static void arrange_layer(struct wlr_output *output,
struct wl_list *list /* struct *wb_layer_surface */,
struct wlr_box *usable_area, bool exclusive) {
struct wb_layer_surface *wb_surface;
struct wlr_box full_area = { 0 };
wlr_output_effective_resolution(output,
&full_area.width, &full_area.height);
wl_list_for_each_reverse(wb_surface, list, link) {
struct wlr_layer_surface_v1 *layer = wb_surface->layer_surface;
struct wlr_layer_surface_v1_state *state = &layer->current;
if (exclusive != (state->exclusive_zone > 0)) {
continue;
}
struct wlr_box bounds;
if (state->exclusive_zone == -1) {
bounds = full_area;
} else {
bounds = *usable_area;
}
struct wlr_box box = {
.width = state->desired_width,
.height = state->desired_height
};
/* Horizontal axis */
const uint32_t both_horiz = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
if ((state->anchor & both_horiz) && box.width == 0) {
box.x = bounds.x;
box.width = bounds.width;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) {
box.x = bounds.x;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) {
box.x = bounds.x + (bounds.width - box.width);
} else {
box.x = bounds.x + ((bounds.width / 2) - (box.width / 2));
}
/* Vertical axis */
const uint32_t both_vert = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
| ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
if ((state->anchor & both_vert) && box.height == 0) {
box.y = bounds.y;
box.height = bounds.height;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) {
box.y = bounds.y;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) {
box.y = bounds.y + (bounds.height - box.height);
} else {
box.y = bounds.y + ((bounds.height / 2) - (box.height / 2));
}
/* Margin */
if ((state->anchor & both_horiz) == both_horiz) {
box.x += state->margin.left;
box.width -= state->margin.left + state->margin.right;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) {
box.x += state->margin.left;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) {
box.x -= state->margin.right;
}
if ((state->anchor & both_vert) == both_vert) {
box.y += state->margin.top;
box.height -= state->margin.top + state->margin.bottom;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) {
box.y += state->margin.top;
} else if ((state->anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) {
box.y -= state->margin.bottom;
}
if (box.width < 0 || box.height < 0) {
wlr_layer_surface_v1_destroy(layer);
continue;
}
static void handle_descriptor_destroy(struct wl_listener *listener, void *data) {
struct wb_scene_descriptor *desc =
wl_container_of(listener, desc, destroy);
/* Apply */
wb_surface->geo = box;
apply_exclusive(usable_area, state->anchor, state->exclusive_zone,
state->margin.top, state->margin.right,
state->margin.bottom, state->margin.left);
wlr_layer_surface_v1_configure(layer, box.width, box.height);
descriptor_destroy(desc);
}
void assign_scene_descriptor(struct wlr_scene_node *node,
enum wb_scene_descriptor_type type, void *data) {
struct wb_scene_descriptor *desc =
calloc(1, sizeof(struct wb_scene_descriptor));
if (!desc) {
return;
}
desc->type = type;
desc->data = data;
desc->destroy.notify = handle_descriptor_destroy;
wl_signal_add(&node->events.destroy, &desc->destroy);
node->data = desc;
}
static void arrange_surface (struct wb_output *output, struct wlr_box *full_area,
struct wlr_box *usable_area, struct wlr_scene_node *scene_node) {
struct wlr_scene_node *node;
wl_list_for_each(node, &scene_node->state.children, state.link) {
struct wb_scene_descriptor *desc = node->data;
if (desc->type == WB_SCENE_DESC_LAYER_SHELL) {
#if WLR_CHECK_VERSION(0, 16, 0)
struct wb_layer_surface *surface = desc->data;
wlr_scene_layer_surface_v1_configure(surface->scene,
full_area, usable_area);
#endif
}
}
}
void arrange_layers(struct wb_output *output) {
struct wlr_box usable_area = { 0 };
struct wlr_box full_area;
wlr_output_effective_resolution(output->wlr_output,
&usable_area.width, &usable_area.height);
/* Arrange exclusive surfaces from top->bottom */
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY],
&usable_area, true);
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP],
&usable_area, true);
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM],
&usable_area, true);
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND],
&usable_area, true);
memcpy(&full_area, &usable_area, sizeof(struct wlr_box));
/* Arrange non-exlusive surfaces from top->bottom */
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY],
&usable_area, false);
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP],
&usable_area, false);
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM],
&usable_area, false);
arrange_layer(output->wlr_output,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND],
&usable_area, false);
/* 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,
};
size_t nlayers = sizeof(layers_above_shell) / sizeof(layers_above_shell[0]);
struct wb_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) {
if (layer->layer_surface->current.keyboard_interactive) {
topmost = layer;
break;
}
}
if (topmost != NULL) {
break;
}
}
/* TODO: Focus topmost layer */
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);
}
static void handle_output_destroy(struct wl_listener *listener, void *data) {
struct wb_layer_surface *layer =
wl_container_of(listener, layer, output_destroy);
layer->layer_surface->output = NULL;
wl_list_remove(&layer->output_destroy.link);
wlr_layer_surface_v1_destroy(layer->layer_surface);
#if WLR_CHECK_VERSION(0, 16, 0)
static struct wlr_scene_node *wb_layer_get_scene(struct wb_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;
}
/* Unreachable */
return NULL;
}
#endif
static struct wb_layer_surface *wb_layer_surface_create(
#if WLR_CHECK_VERSION(0, 16, 0)
struct wlr_scene_layer_surface_v1 *scene) {
#else
struct wlr_layer_surface_v1 *scene) {
#endif
struct wb_layer_surface *surface = calloc(1, sizeof(struct wb_layer_surface));
if (!surface) {
return NULL;
}
#if WLR_CHECK_VERSION(0, 16, 0)
surface->scene = scene;
#endif
return surface;
}
static void handle_surface_commit(struct wl_listener *listener, void *data) {
struct wb_layer_surface *layer =
wl_container_of(listener, layer, surface_commit);
struct wlr_layer_surface_v1 *layer_surface = layer->layer_surface;
struct wlr_output *wlr_output = layer_surface->output;
if (wlr_output != NULL) {
struct wb_output *output = wlr_output->data;
arrange_layers(output);
}
}
struct wb_layer_surface *surface =
wl_container_of(listener, surface, surface_commit);
static void handle_destroy(struct wl_listener *listener, void *data) {
struct wb_layer_surface *layer = wl_container_of(
listener, layer, destroy);
wl_list_remove(&layer->link);
wl_list_remove(&layer->destroy.link);
wl_list_remove(&layer->map.link);
wl_list_remove(&layer->surface_commit.link);
if (layer->layer_surface->output) {
wl_list_remove(&layer->output_destroy.link);
arrange_layers((struct wb_output *)layer->layer_surface->output->data);
if (!surface->output) {
return;
}
free(layer);
#if WLR_CHECK_VERSION(0, 16, 0)
struct wlr_layer_surface_v1 *layer_surface = surface->scene->layer_surface;
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_node *output_layer = wb_layer_get_scene(
surface->output, layer_type);
wlr_scene_node_reparent(surface->scene->node, output_layer);
}
if (committed || layer_surface->mapped != surface->mapped) {
surface->mapped = layer_surface->mapped;
arrange_layers(surface->output);
}
#endif
}
static void handle_map(struct wl_listener *listener, void *data) {
struct wlr_layer_surface_v1 *layer_surface = data;
wlr_surface_send_enter(layer_surface->surface, layer_surface->output);
struct wb_layer_surface *surface = wl_container_of(listener,
surface, map);
#if WLR_CHECK_VERSION(0, 16, 0)
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 wb_seat *seat = surface->server->seat;
/* 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);
}
#endif
}
void server_new_layer_surface(struct wl_listener *listener, void *data) {
struct wb_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->cursor->cursor->x, server->cursor->cursor->y);
layer_surface->output = output;
}
static void handle_unmap(struct wl_listener *listener, void *data) {
#if WLR_CHECK_VERSION(0, 16, 0)
struct wb_layer_surface *surface = wl_container_of(
listener, surface, unmap);
struct wb_seat *seat = surface->server->seat;
if (seat->focused_layer == surface->scene->layer_surface) {
seat_set_focus_layer(seat, seat->focused_layer);
}
#endif
}
struct wb_output *output = layer_surface->output->data;
struct wb_layer_surface *wb_surface =
calloc(1, sizeof(struct wb_layer_surface));
if (!wb_surface) {
static void wb_layer_surface_destroy(struct wb_layer_surface *surface) {
if (surface == NULL) {
return;
}
wb_surface->layer_surface = layer_surface;
layer_surface->data = wb_surface;
wb_surface->server = server;
wb_surface->surface_commit.notify = handle_surface_commit;
wl_list_remove(&surface->map.link);
wl_list_remove(&surface->unmap.link);
wl_list_remove(&surface->surface_commit.link);
wl_list_remove(&surface->destroy.link);
free(surface);
}
static void handle_destroy(struct wl_listener *listener, void *data) {
struct wb_layer_surface *surface =
wl_container_of(listener, surface, destroy);
if (surface->output) {
arrange_layers(surface->output);
}
wb_layer_surface_destroy(surface);
}
static void popup_handle_destroy(struct wl_listener *listener, void *data) {
struct wb_layer_popup *popup =
wl_container_of(listener, popup, destroy);
wl_list_remove(&popup->destroy.link);
wl_list_remove(&popup->new_popup.link);
free(popup);
}
static struct wb_layer_surface *popup_get_layer(
struct wb_layer_popup *popup) {
struct wlr_scene_node *current = popup->scene;
while (current) {
if (current->data) {
struct wb_scene_descriptor *desc = current->data;
if (desc->type == WB_SCENE_DESC_LAYER_SHELL) {
return desc->data;
}
}
current = current->parent;
}
return NULL;
}
static void popup_unconstrain(struct wb_layer_popup *popup) {
struct wb_layer_surface *surface = popup_get_layer(popup);
if (!surface) {
return;
}
struct wlr_xdg_popup *wlr_popup = popup->wlr_popup;
struct wb_output *output = surface->output;
int lx, ly;
wlr_scene_node_coords(popup->scene, &lx, &ly);
/* the output box expressed in the coordinate system of the toplevel parent
* of the popup */
struct wb_view *view = wl_container_of(output->server->views.next, view, link);
struct wlr_box output_toplevel_sx_box = {
.x = view->current_position.x - lx,
.y = view->current_position.y - ly,
.width = output->wlr_output->width,
.height = output->wlr_output->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 wb_layer_popup *create_popup(struct wlr_xdg_popup *wlr_popup,
struct wlr_scene_node *parent) {
struct wb_layer_popup *popup =
calloc(1, sizeof(struct wb_layer_popup));
if (popup == NULL) {
return NULL;
}
popup->wlr_popup = wlr_popup;
popup->scene = wlr_scene_xdg_surface_create(parent,
wlr_popup->base);
if (!popup->scene) {
free(popup);
return NULL;
}
assign_scene_descriptor(popup->scene, WB_SCENE_DESC_LAYER_SHELL_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_unconstrain(popup);
return NULL;
}
static void popup_handle_new_popup(struct wl_listener *listener, void *data) {
struct wb_layer_popup *layer_popup =
wl_container_of(listener, layer_popup, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
create_popup(wlr_popup, layer_popup->scene);
}
static void handle_new_popup(struct wl_listener *listener, void *data) {
struct wb_layer_surface *wb_layer_surface =
wl_container_of(listener, wb_layer_surface, new_popup);
#if WLR_CHECK_VERSION(0, 16, 0)
struct wlr_xdg_popup *wlr_popup = data;
create_popup(wlr_popup, wb_layer_surface->scene->node);
#endif
}
void handle_layer_shell_surface(struct wl_listener *listener, void *data) {
struct wlr_layer_surface_v1 *layer_surface = data;
if (layer_surface->output == NULL)
return;
struct wb_output *output = layer_surface->output->data;
if (!layer_surface->output) {
/* Assign last active output */
layer_surface->output = output->wlr_output;
}
#if WLR_CHECK_VERSION(0, 16, 0)
enum zwlr_layer_shell_v1_layer layer_type = layer_surface->pending.layer;
struct wlr_scene_node *output_layer = wb_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) {
return;
}
struct wb_layer_surface *surface =
wb_layer_surface_create(scene_surface);
#else
struct wb_layer_surface *surface =
wb_layer_surface_create(layer_surface);
#endif
if (!surface) {
wlr_layer_surface_v1_destroy(layer_surface);
return;
}
#if WLR_CHECK_VERSION(0, 16, 0)
assign_scene_descriptor(scene_surface->node,
WB_SCENE_DESC_LAYER_SHELL, surface);
#endif
surface->output = output;
surface->server = output->server;
surface->surface_commit.notify = handle_surface_commit;
wl_signal_add(&layer_surface->surface->events.commit,
&wb_surface->surface_commit);
wb_surface->output_destroy.notify = handle_output_destroy;
wl_signal_add(&layer_surface->output->events.destroy,
&wb_surface->output_destroy);
wb_surface->destroy.notify = handle_destroy;
wl_signal_add(&layer_surface->events.destroy, &wb_surface->destroy);
wb_surface->map.notify = handle_map;
wl_signal_add(&layer_surface->events.map, &wb_surface->map);
/* TODO: popups */
&surface->surface_commit);
surface->map.notify = handle_map;
wl_signal_add(&layer_surface->events.map, &surface->map);
surface->unmap.notify = handle_unmap;
wl_signal_add(&layer_surface->events.unmap, &surface->unmap);
surface->destroy.notify = handle_destroy;
wl_signal_add(&layer_surface->events.destroy, &surface->destroy);
surface->new_popup.notify = handle_new_popup;
wl_signal_add(&layer_surface->events.new_popup, &surface->new_popup);
/* TODO: Listen for subsurfaces */
wl_list_insert(&output->layers[layer_surface->pending.layer], &wb_surface->link);
/* Temporarily set the layer's current state to pending
* So that we can easily arrange it */
struct wlr_layer_surface_v1_state old_state = layer_surface->current;
@ -288,8 +361,7 @@ void server_new_layer_surface(struct wl_listener *listener, void *data) {
void init_layer_shell(struct wb_server *server) {
server->layer_shell = wlr_layer_shell_v1_create(server->wl_display);
return;
server->new_layer_surface.notify = server_new_layer_surface;
server->new_layer_surface.notify = handle_layer_shell_surface;
wl_signal_add(&server->layer_shell->events.new_surface,
&server->new_layer_surface);
}

View file

@ -5,18 +5,51 @@
struct wb_server;
struct wb_layer_surface {
struct wlr_layer_surface_v1 *layer_surface;
struct wb_output *output;
struct wb_server *server;
struct wl_list link;
#if WLR_CHECK_VERSION(0, 16, 0)
struct wlr_scene_layer_surface_v1 *scene;
#endif
bool mapped;
struct wl_listener destroy;
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener surface_commit;
struct wl_listener output_destroy;
struct wl_listener new_popup;
};
struct wlr_box geo;
struct wb_layer_popup {
struct wlr_xdg_popup *wlr_popup;
struct wlr_scene_node *scene;
struct wl_listener destroy;
struct wl_listener new_popup;
};
struct wb_layer_subsurface {
struct wlr_scene_node *scene;
struct wl_listener destroy;
};
enum wb_scene_descriptor_type {
WB_SCENE_DESC_NODE,
WB_SCENE_DESC_LAYER_SHELL,
WB_SCENE_DESC_LAYER_SHELL_POPUP,
};
struct wb_scene_descriptor {
enum wb_scene_descriptor_type type;
void *data;
struct wl_listener destroy;
};
void init_layer_shell(struct wb_server *server);
void assign_scene_descriptor(struct wlr_scene_node *node,
enum wb_scene_descriptor_type type, void *data);
#endif

View file

@ -1,40 +1,5 @@
#include "waybox/output.h"
#if !WLR_CHECK_VERSION(0, 16, 0)
static void render_layer_surface(struct wlr_surface *surface,
int sx, int sy, void *data) {
struct wb_layer_surface *layer_surface = data;
struct wlr_texture *texture = wlr_surface_get_texture(surface);
if (texture == NULL) {
return;
}
struct wlr_output *output = layer_surface->layer_surface->output;
double ox = 0, oy = 0;
wlr_output_layout_output_coords(
layer_surface->server->output_layout, output, &ox, &oy);
ox += layer_surface->geo.x + sx, oy += layer_surface->geo.y + sy;
float matrix[9];
struct wlr_box box;
memcpy(&box, &layer_surface->geo, sizeof(struct wlr_box));
wlr_render_texture_with_matrix(layer_surface->server->renderer,
texture, matrix, 1);
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
wlr_surface_send_frame_done(surface, &now);
}
static void render_layer(
struct wb_output *output, struct wl_list *layer_surfaces) {
struct wb_layer_surface *layer_surface;
wl_list_for_each(layer_surface, layer_surfaces, link) {
struct wlr_layer_surface_v1 *wlr_layer_surface_v1 =
layer_surface->layer_surface;
wlr_surface_for_each_surface(wlr_layer_surface_v1->surface,
render_layer_surface, layer_surface);
}
}
#endif
void output_frame_notify(struct wl_listener *listener, void *data) {
struct wb_output *output = wl_container_of(listener, output, frame);
struct wlr_scene *scene = output->server->scene;
@ -42,21 +7,14 @@ void output_frame_notify(struct wl_listener *listener, void *data) {
struct wlr_scene_output *scene_output = wlr_scene_get_scene_output(
scene, output->wlr_output);
wlr_scene_rect_set_size(output->scene_rect, output->wlr_output->width, output->wlr_output->height);
#if !WLR_CHECK_VERSION(0, 16, 0)
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);
wlr_scene_rect_set_size(output->scene_rect,
output->wlr_output->width, output->wlr_output->height);
#endif
/* Render the scene if needed and commit the output */
wlr_scene_output_commit(scene_output);
#if !WLR_CHECK_VERSION(0, 16, 0)
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
#endif
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
wlr_scene_output_send_frame_done(scene_output, &now);
@ -69,6 +27,14 @@ void output_destroy_notify(struct wl_listener *listener, void *data) {
wl_list_remove(&output->destroy.link);
wl_list_remove(&output->frame.link);
/* Frees the layers */
size_t num_layers = sizeof(output->layers) / sizeof(struct wlr_scene_node *);
for (size_t i = 0; i < num_layers; i++) {
struct wlr_scene_node *node =
((struct wlr_scene_node **) &output->layers)[i];
wlr_scene_node_destroy(node);
}
free(output);
}
@ -98,15 +64,22 @@ void new_output_notify(struct wl_listener *listener, void *data) {
output->server = server;
output->wlr_output = wlr_output;
wlr_output->data = output;
wl_list_insert(&server->outputs, &output->link);
float color[4] = {0.3, 0.3, 0.3, 1.0};
#if !WLR_CHECK_VERSION(0, 16, 0)
/* Allows setting the traditional background color. However, it
* interferes with the wlr_scene layer shell helper in 0.16.0+. */
float color[4] = {0.1875, 0.1875, 0.1875, 1.0};
output->scene_rect = wlr_scene_rect_create(&server->scene->node, 0, 0, color);
#endif
wl_list_init(&output->layers[0]);
wl_list_init(&output->layers[1]);
wl_list_init(&output->layers[2]);
wl_list_init(&output->layers[3]);
/* Initializes the layers */
size_t num_layers = sizeof(output->layers) / sizeof(struct wlr_scene_node *);
for (size_t i = 0; i < num_layers; i++) {
((struct wlr_scene_node **) &output->layers)[i] =
&wlr_scene_tree_create(&server->scene->node)->node;
}
wl_list_insert(&server->outputs, &output->link);
output->destroy.notify = output_destroy_notify;
wl_signal_add(&wlr_output->events.destroy, &output->destroy);

View file

@ -269,6 +269,27 @@ static void new_input_notify(struct wl_listener *listener, void *data) {
wlr_seat_set_capabilities(server->seat->seat, caps);
}
void seat_focus_surface(struct wb_seat *seat, struct wlr_surface *surface) {
if (!surface) {
wlr_seat_keyboard_notify_clear_focus(seat->seat);
return;
}
struct wlr_keyboard *kb = (struct wlr_keyboard *) seat->keyboards.next;
wlr_seat_keyboard_notify_enter(seat->seat, surface, kb->keycodes,
kb->num_keycodes, &kb->modifiers);
}
void seat_set_focus_layer(struct wb_seat *seat, struct wlr_layer_surface_v1 *layer) {
if (!layer) {
seat->focused_layer = NULL;
return;
}
seat_focus_surface(seat, layer->surface);
if (layer->current.layer >= ZWLR_LAYER_SHELL_V1_LAYER_TOP) {
seat->focused_layer = layer;
}
}
static void handle_request_set_primary_selection(struct wl_listener *listener,
void *data) {
struct wb_seat *seat =

View file

@ -45,7 +45,7 @@ void focus_view(struct wb_view *view, struct wlr_surface *surface) {
* stop displaying a caret.
*/
struct wlr_xdg_surface *previous = wlr_xdg_surface_from_wlr_surface(
seat->keyboard_state.focused_surface);
prev_surface);
#if WLR_CHECK_VERSION(0, 16, 0)
wlr_xdg_toplevel_set_activated(previous->toplevel, false);
#else
@ -91,8 +91,6 @@ static void xdg_toplevel_map(struct wl_listener *listener, void *data) {
if (view->xdg_toplevel->base->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL)
return;
wl_list_insert(&view->server->views, &view->link);
struct wb_config *config = view->server->config;
struct wlr_box geo_box = {0};
struct wlr_box usable_area = get_usable_area(view);
@ -141,8 +139,6 @@ static void xdg_toplevel_unmap(struct wl_listener *listener, void *data) {
next_view->xdg_toplevel->app_id);
focus_view(next_view, next_view->xdg_toplevel->base->surface);
}
wl_list_remove(&view->link);
}
static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) {
@ -158,6 +154,7 @@ static void xdg_toplevel_destroy(struct wl_listener *listener, void *data) {
wl_list_remove(&view->request_maximize.link);
wl_list_remove(&view->request_move.link);
wl_list_remove(&view->request_resize.link);
wl_list_remove(&view->link);
}
free(view);
@ -305,11 +302,13 @@ static void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
* we always set the user data field of xdg_surfaces to the corresponding
* scene node. */
if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) {
struct wlr_xdg_surface *parent = wlr_xdg_surface_from_wlr_surface(
xdg_surface->popup->parent);
struct wlr_scene_node *parent_node = parent->data;
xdg_surface->data = wlr_scene_xdg_surface_create(
parent_node, xdg_surface);
if (wlr_surface_is_xdg_surface(xdg_surface->popup->parent)) {
struct wlr_xdg_surface *parent = wlr_xdg_surface_from_wlr_surface(
xdg_surface->popup->parent);
struct wlr_scene_node *parent_node = parent->data;
xdg_surface->data = wlr_scene_xdg_surface_create(
parent_node, xdg_surface);
}
/* The scene graph doesn't currently unconstrain popups, so keep going */
/* return; */
}
@ -350,6 +349,8 @@ static void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
wl_signal_add(&toplevel->events.request_move, &view->request_move);
view->request_resize.notify = xdg_toplevel_request_resize;
wl_signal_add(&toplevel->events.request_resize, &view->request_resize);
wl_list_insert(&view->server->views, &view->link);
}
}