cycle: refactor to aggregate type definitions into cycle.h

We declared `cycle_state` struct in `labwc.h` and `cycle_osd_scene`
struct in `output.h`, which was unclean in terms of separation of
concerns.

So this commit firstly moves `cycle_state` to `cycle.h`, then replaces
`cycle_osd_scene` in `output.h` with `cycle_osd_output` in `cycle.h`
which is dynamically allocated in a similar manner to
`session_lock_output`. This ensures that all states about alt-tabbing
are stored in `server->cycle`.

Also, this commit fixes a rare memory leak in `output->cycle_osd.items`
when an output is destroyed while alt-tabbing, by freeing it when the
osd tree is destroyed.
This commit is contained in:
tokyo4j 2025-12-29 02:50:45 +09:00 committed by Johan Malm
parent 276d4e61f9
commit dfe428ae14
8 changed files with 95 additions and 72 deletions

View file

@ -117,9 +117,9 @@ create_label(struct wlr_scene_tree *parent, struct view *view,
static struct cycle_osd_thumbnail_item *
create_item_scene(struct wlr_scene_tree *parent, struct view *view,
struct output *output)
struct cycle_osd_output *osd_output)
{
struct server *server = output->server;
struct server *server = osd_output->output->server;
struct theme *theme = server->theme;
struct window_switcher_thumbnail_theme *switcher_theme =
&theme->osd_window_switcher_thumbnail;
@ -137,7 +137,7 @@ create_item_scene(struct wlr_scene_tree *parent, struct view *view,
}
struct cycle_osd_thumbnail_item *item = znew(*item);
wl_list_append(&output->cycle_osd.items, &item->base.link);
wl_list_append(&osd_output->items, &item->base.link);
struct wlr_scene_tree *tree = wlr_scene_tree_create(parent);
node_descriptor_create(&tree->node, LAB_NODE_CYCLE_OSD_ITEM, NULL, item);
item->base.tree = tree;
@ -159,7 +159,7 @@ create_item_scene(struct wlr_scene_tree *parent, struct view *view,
switcher_theme->item_height, (float[4]) {0});
/* thumbnail */
struct wlr_buffer *thumb_buffer = render_thumb(output, view);
struct wlr_buffer *thumb_buffer = render_thumb(osd_output->output, view);
if (thumb_buffer) {
struct wlr_scene_buffer *thumb_scene_buffer =
wlr_scene_buffer_create(tree, thumb_buffer);
@ -226,17 +226,16 @@ get_items_geometry(struct output *output, struct theme *theme,
}
static void
cycle_osd_thumbnail_create(struct output *output)
cycle_osd_thumbnail_init(struct cycle_osd_output *osd_output)
{
assert(!output->cycle_osd.tree && wl_list_empty(&output->cycle_osd.items));
struct output *output = osd_output->output;
struct server *server = output->server;
struct theme *theme = server->theme;
struct window_switcher_thumbnail_theme *switcher_theme =
&theme->osd_window_switcher_thumbnail;
int padding = theme->osd_border_width + switcher_theme->padding;
output->cycle_osd.tree = wlr_scene_tree_create(output->cycle_osd_tree);
osd_output->tree = wlr_scene_tree_create(output->cycle_osd_tree);
int nr_views = wl_list_length(&server->cycle.views);
assert(nr_views > 0);
@ -248,7 +247,7 @@ cycle_osd_thumbnail_create(struct output *output)
int index = 0;
wl_list_for_each(view, &server->cycle.views, cycle_link) {
struct cycle_osd_thumbnail_item *item = create_item_scene(
output->cycle_osd.tree, view, output);
osd_output->tree, view, osd_output);
if (!item) {
break;
}
@ -268,7 +267,7 @@ cycle_osd_thumbnail_create(struct output *output)
.height = nr_rows * switcher_theme->item_height + 2 * padding,
};
struct lab_scene_rect *bg =
lab_scene_rect_create(output->cycle_osd.tree, &bg_opts);
lab_scene_rect_create(osd_output->tree, &bg_opts);
wlr_scene_node_lower_to_bottom(&bg->tree->node);
/* center */
@ -277,15 +276,16 @@ cycle_osd_thumbnail_create(struct output *output)
&output_box);
int lx = output_box.x + (output_box.width - bg_opts.width) / 2;
int ly = output_box.y + (output_box.height - bg_opts.height) / 2;
wlr_scene_node_set_position(&output->cycle_osd.tree->node, lx, ly);
wlr_scene_node_set_position(&osd_output->tree->node, lx, ly);
}
static void
cycle_osd_thumbnail_update(struct output *output)
cycle_osd_thumbnail_update(struct cycle_osd_output *osd_output)
{
struct server *server = osd_output->output->server;
struct cycle_osd_thumbnail_item *item;
wl_list_for_each(item, &output->cycle_osd.items, base.link) {
bool active = (item->base.view == output->server->cycle.selected_view);
wl_list_for_each(item, &osd_output->items, base.link) {
bool active = (item->base.view == server->cycle.selected_view);
wlr_scene_node_set_enabled(&item->active_bg->tree->node, active);
wlr_scene_node_set_enabled(
&item->active_label->scene_buffer->node, active);
@ -295,6 +295,6 @@ cycle_osd_thumbnail_update(struct output *output)
}
struct cycle_osd_impl cycle_osd_thumbnail_impl = {
.create = cycle_osd_thumbnail_create,
.init = cycle_osd_thumbnail_init,
.update = cycle_osd_thumbnail_update,
};