Rename osd.{h,c} to cycle.{h,c}

This commit is contained in:
tokyo4j 2025-11-29 02:09:24 +09:00 committed by Hiroaki Yamamoto
parent 35b3980a5b
commit 65cc2e40ba
12 changed files with 14 additions and 14 deletions

374
src/cycle/cycle.c Normal file
View file

@ -0,0 +1,374 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "cycle.h"
#include <assert.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#include "common/array.h"
#include "common/lab-scene-rect.h"
#include "common/scene-helpers.h"
#include "config/rcxml.h"
#include "labwc.h"
#include "node.h"
#include "output.h"
#include "scaled-buffer/scaled-font-buffer.h"
#include "scaled-buffer/scaled-icon-buffer.h"
#include "ssd.h"
#include "theme.h"
#include "view.h"
static void update_osd(struct server *server);
static void
destroy_osd_scenes(struct server *server)
{
struct output *output;
wl_list_for_each(output, &server->outputs, link) {
struct osd_item *item, *tmp;
wl_list_for_each_safe(item, tmp, &output->osd_scene.items, link) {
wl_list_remove(&item->link);
free(item);
}
if (output->osd_scene.tree) {
wlr_scene_node_destroy(&output->osd_scene.tree->node);
output->osd_scene.tree = NULL;
}
}
}
static void
osd_update_preview_outlines(struct view *view)
{
/* Create / Update preview outline tree */
struct server *server = view->server;
struct theme *theme = server->theme;
struct lab_scene_rect *rect = view->server->osd_state.preview_outline;
if (!rect) {
struct lab_scene_rect_options opts = {
.border_colors = (float *[3]) {
theme->osd_window_switcher_preview_border_color[0],
theme->osd_window_switcher_preview_border_color[1],
theme->osd_window_switcher_preview_border_color[2],
},
.nr_borders = 3,
.border_width = theme->osd_window_switcher_preview_border_width,
};
rect = lab_scene_rect_create(&server->scene->tree, &opts);
wlr_scene_node_place_above(&rect->tree->node, &server->menu_tree->node);
server->osd_state.preview_outline = rect;
}
struct wlr_box geo = ssd_max_extents(view);
lab_scene_rect_set_size(rect, geo.width, geo.height);
wlr_scene_node_set_position(&rect->tree->node, geo.x, geo.y);
}
/*
* Returns the view to select next in the window switcher.
* If !start_view, the second focusable view is returned.
*/
static struct view *
get_next_cycle_view(struct server *server, struct view *start_view,
enum lab_cycle_dir dir)
{
struct view *(*iter)(struct wl_list *head, struct view *view,
enum lab_view_criteria criteria);
bool forwards = dir == LAB_CYCLE_DIR_FORWARD;
iter = forwards ? view_next_no_head_stop : view_prev_no_head_stop;
enum lab_view_criteria criteria = rc.window_switcher.criteria;
/*
* Views are listed in stacking order, topmost first. Usually the
* topmost view is already focused, so when iterating in the forward
* direction we pre-select the view second from the top:
*
* View #1 (on top, currently focused)
* View #2 (pre-selected)
* View #3
* ...
*/
if (!start_view && forwards) {
start_view = iter(&server->views, NULL, criteria);
}
return iter(&server->views, start_view, criteria);
}
void
osd_on_view_destroy(struct view *view)
{
assert(view);
struct osd_state *osd_state = &view->server->osd_state;
if (view->server->input_mode != LAB_INPUT_STATE_WINDOW_SWITCHER) {
/* OSD not active, no need for clean up */
return;
}
if (osd_state->cycle_view == view) {
/*
* If we are the current OSD selected view, cycle
* to the next because we are dying.
*/
/* Also resets preview node */
osd_state->cycle_view = get_next_cycle_view(view->server,
osd_state->cycle_view, LAB_CYCLE_DIR_BACKWARD);
/*
* If we cycled back to ourselves, then we have no more windows.
* Just close the OSD for good.
*/
if (osd_state->cycle_view == view || !osd_state->cycle_view) {
/* osd_finish() additionally resets cycle_view to NULL */
osd_finish(view->server, /*switch_focus*/ false);
}
}
if (osd_state->cycle_view) {
/* Recreate the OSD to reflect the view has now gone. */
destroy_osd_scenes(view->server);
update_osd(view->server);
}
if (view->scene_tree) {
struct wlr_scene_node *node = &view->scene_tree->node;
if (osd_state->preview_anchor == node) {
/*
* If we are the anchor for the current OSD selected view,
* replace the anchor with the node before us.
*/
osd_state->preview_anchor = lab_wlr_scene_get_prev_node(node);
}
}
}
void
osd_on_cursor_release(struct server *server, struct wlr_scene_node *node)
{
assert(server->input_mode == LAB_INPUT_STATE_WINDOW_SWITCHER);
struct osd_item *item = node_osd_item_from_node(node);
server->osd_state.cycle_view = item->view;
osd_finish(server, /*switch_focus*/ true);
}
static void
restore_preview_node(struct server *server)
{
struct osd_state *osd_state = &server->osd_state;
if (osd_state->preview_node) {
wlr_scene_node_reparent(osd_state->preview_node,
osd_state->preview_parent);
if (osd_state->preview_anchor) {
wlr_scene_node_place_above(osd_state->preview_node,
osd_state->preview_anchor);
} else {
/* Selected view was the first node */
wlr_scene_node_lower_to_bottom(osd_state->preview_node);
}
/* Node was disabled / minimized before, disable again */
if (!osd_state->preview_was_enabled) {
wlr_scene_node_set_enabled(osd_state->preview_node, false);
}
if (osd_state->preview_was_shaded) {
struct view *view = node_view_from_node(osd_state->preview_node);
view_set_shade(view, true);
}
osd_state->preview_node = NULL;
osd_state->preview_parent = NULL;
osd_state->preview_anchor = NULL;
osd_state->preview_was_shaded = false;
}
}
void
osd_begin(struct server *server, enum lab_cycle_dir direction)
{
if (server->input_mode != LAB_INPUT_STATE_PASSTHROUGH) {
return;
}
server->osd_state.cycle_view = get_next_cycle_view(server,
server->osd_state.cycle_view, direction);
seat_focus_override_begin(&server->seat,
LAB_INPUT_STATE_WINDOW_SWITCHER, LAB_CURSOR_DEFAULT);
update_osd(server);
/* Update cursor, in case it is within the area covered by OSD */
cursor_update_focus(server);
}
void
osd_cycle(struct server *server, enum lab_cycle_dir direction)
{
assert(server->input_mode == LAB_INPUT_STATE_WINDOW_SWITCHER);
server->osd_state.cycle_view = get_next_cycle_view(server,
server->osd_state.cycle_view, direction);
update_osd(server);
}
void
osd_finish(struct server *server, bool switch_focus)
{
if (server->input_mode != LAB_INPUT_STATE_WINDOW_SWITCHER) {
return;
}
restore_preview_node(server);
/* FIXME: this sets focus to the old surface even with switch_focus=true */
seat_focus_override_end(&server->seat);
struct view *cycle_view = server->osd_state.cycle_view;
server->osd_state.preview_node = NULL;
server->osd_state.preview_anchor = NULL;
server->osd_state.cycle_view = NULL;
server->osd_state.preview_was_shaded = false;
destroy_osd_scenes(server);
if (server->osd_state.preview_outline) {
/* Destroy the whole multi_rect so we can easily react to new themes */
wlr_scene_node_destroy(&server->osd_state.preview_outline->tree->node);
server->osd_state.preview_outline = NULL;
}
/* Hiding OSD may need a cursor change */
cursor_update_focus(server);
if (switch_focus && cycle_view) {
if (rc.window_switcher.unshade) {
view_set_shade(cycle_view, false);
}
desktop_focus_view(cycle_view, /*raise*/ true);
}
}
static void
preview_cycled_view(struct view *view)
{
assert(view);
assert(view->scene_tree);
struct osd_state *osd_state = &view->server->osd_state;
/* Move previous selected node back to its original place */
restore_preview_node(view->server);
/* Store some pointers so we can reset the preview later on */
osd_state->preview_node = &view->scene_tree->node;
osd_state->preview_parent = view->scene_tree->node.parent;
/* Remember the sibling right before the selected node */
osd_state->preview_anchor = lab_wlr_scene_get_prev_node(
osd_state->preview_node);
while (osd_state->preview_anchor && !osd_state->preview_anchor->data) {
/* Ignore non-view nodes */
osd_state->preview_anchor = lab_wlr_scene_get_prev_node(
osd_state->preview_anchor);
}
/* Store node enabled / minimized state and force-enable if disabled */
osd_state->preview_was_enabled = osd_state->preview_node->enabled;
if (!osd_state->preview_was_enabled) {
wlr_scene_node_set_enabled(osd_state->preview_node, true);
}
if (rc.window_switcher.unshade && view->shaded) {
view_set_shade(view, false);
osd_state->preview_was_shaded = true;
}
/*
* FIXME: This abuses an implementation detail of the always-on-top tree.
* Create a permanent server->osd_preview_tree instead that can
* also be used as parent for the preview outlines.
*/
wlr_scene_node_reparent(osd_state->preview_node,
view->server->view_tree_always_on_top);
/* Finally raise selected node to the top */
wlr_scene_node_raise_to_top(osd_state->preview_node);
}
static void
update_osd_on_output(struct server *server, struct output *output,
struct osd_impl *osd_impl, struct wl_array *views)
{
if (!output_is_usable(output)) {
return;
}
if (!output->osd_scene.tree) {
osd_impl->create(output, views);
assert(output->osd_scene.tree);
}
osd_impl->update(output);
}
static void
update_osd(struct server *server)
{
struct wl_array views;
wl_array_init(&views);
view_array_append(server, &views, rc.window_switcher.criteria);
struct osd_impl *osd_impl = NULL;
switch (rc.window_switcher.style) {
case WINDOW_SWITCHER_CLASSIC:
osd_impl = &osd_classic_impl;
break;
case WINDOW_SWITCHER_THUMBNAIL:
osd_impl = &osd_thumbnail_impl;
break;
}
if (!wl_array_len(&views) || !server->osd_state.cycle_view) {
osd_finish(server, /*switch_focus*/ false);
goto out;
}
if (rc.window_switcher.show) {
/* Display the actual OSD */
switch (rc.window_switcher.output_criteria) {
case OSD_OUTPUT_ALL: {
struct output *output;
wl_list_for_each(output, &server->outputs, link) {
update_osd_on_output(server, output, osd_impl, &views);
}
break;
}
case OSD_OUTPUT_POINTER:
update_osd_on_output(server,
output_nearest_to_cursor(server), osd_impl, &views);
break;
case OSD_OUTPUT_KEYBOARD: {
struct output *output;
if (server->active_view) {
output = server->active_view->output;
} else {
/* Fallback to pointer, if there is no active_view */
output = output_nearest_to_cursor(server);
}
update_osd_on_output(server, output, osd_impl, &views);
break;
}
}
}
if (rc.window_switcher.preview) {
preview_cycled_view(server->osd_state.cycle_view);
}
/* Outline current window */
if (rc.window_switcher.outlines) {
if (view_is_focusable(server->osd_state.cycle_view)) {
osd_update_preview_outlines(server->osd_state.cycle_view);
}
}
out:
wl_array_release(&views);
}

6
src/cycle/meson.build Normal file
View file

@ -0,0 +1,6 @@
labwc_sources += files(
'cycle.c',
'osd-classic.c',
'osd-field.c',
'osd-thumbnail.c',
)

240
src/cycle/osd-classic.c Normal file
View file

@ -0,0 +1,240 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#include "common/array.h"
#include "common/buf.h"
#include "common/font.h"
#include "common/lab-scene-rect.h"
#include "common/list.h"
#include "common/string-helpers.h"
#include "config/rcxml.h"
#include "cycle.h"
#include "labwc.h"
#include "node.h"
#include "output.h"
#include "scaled-buffer/scaled-font-buffer.h"
#include "scaled-buffer/scaled-icon-buffer.h"
#include "theme.h"
#include "workspaces.h"
struct osd_classic_item {
struct osd_item base;
struct wlr_scene_tree *normal_tree, *active_tree;
};
static void
create_fields_scene(struct server *server, struct view *view,
struct wlr_scene_tree *parent, const float *text_color,
const float *bg_color, int field_widths_sum, int x, int y)
{
struct theme *theme = server->theme;
struct window_switcher_classic_theme *switcher_theme =
&theme->osd_window_switcher_classic;
struct window_switcher_field *field;
wl_list_for_each(field, &rc.window_switcher.fields, link) {
int field_width = field_widths_sum * field->width / 100.0;
struct wlr_scene_node *node = NULL;
int height = -1;
if (field->content == LAB_FIELD_ICON) {
int icon_size = MIN(field_width,
switcher_theme->item_icon_size);
struct scaled_icon_buffer *icon_buffer =
scaled_icon_buffer_create(parent,
server, icon_size, icon_size);
scaled_icon_buffer_set_view(icon_buffer, view);
node = &icon_buffer->scene_buffer->node;
height = icon_size;
} else {
struct buf buf = BUF_INIT;
osd_field_get_content(field, &buf, view);
if (!string_null_or_empty(buf.data)) {
struct scaled_font_buffer *font_buffer =
scaled_font_buffer_create(parent);
scaled_font_buffer_update(font_buffer,
buf.data, field_width,
&rc.font_osd, text_color, bg_color);
node = &font_buffer->scene_buffer->node;
height = font_height(&rc.font_osd);
}
buf_reset(&buf);
}
if (node) {
int item_height = switcher_theme->item_height;
wlr_scene_node_set_position(node,
x, y + (item_height - height) / 2);
}
x += field_width + switcher_theme->item_padding_x;
}
}
static void
osd_classic_create(struct output *output, struct wl_array *views)
{
assert(!output->osd_scene.tree && wl_list_empty(&output->osd_scene.items));
struct server *server = output->server;
struct theme *theme = server->theme;
struct window_switcher_classic_theme *switcher_theme =
&theme->osd_window_switcher_classic;
int padding = theme->osd_border_width + switcher_theme->padding;
bool show_workspace = wl_list_length(&rc.workspace_config.workspaces) > 1;
const char *workspace_name = server->workspaces.current->name;
struct wlr_box output_box;
wlr_output_layout_get_box(server->output_layout, output->wlr_output,
&output_box);
int w = switcher_theme->width;
if (switcher_theme->width_is_percent) {
w = output_box.width * switcher_theme->width / 100;
}
int h = wl_array_len(views) * switcher_theme->item_height + 2 * padding;
if (show_workspace) {
/* workspace indicator */
h += switcher_theme->item_height;
}
output->osd_scene.tree = wlr_scene_tree_create(output->osd_tree);
float *text_color = theme->osd_label_text_color;
float *bg_color = theme->osd_bg_color;
/* Draw background */
struct lab_scene_rect_options bg_opts = {
.border_colors = (float *[1]) {theme->osd_border_color},
.nr_borders = 1,
.border_width = theme->osd_border_width,
.bg_color = bg_color,
.width = w,
.height = h,
};
lab_scene_rect_create(output->osd_scene.tree, &bg_opts);
int y = padding;
/* Draw workspace indicator */
if (show_workspace) {
struct font font = rc.font_osd;
font.weight = PANGO_WEIGHT_BOLD;
/* Center workspace indicator on the x axis */
int x = (w - font_width(&font, workspace_name)) / 2;
if (x < 0) {
wlr_log(WLR_ERROR,
"not enough space for workspace name in osd");
goto error;
}
struct scaled_font_buffer *font_buffer =
scaled_font_buffer_create(output->osd_scene.tree);
wlr_scene_node_set_position(&font_buffer->scene_buffer->node,
x, y + (switcher_theme->item_height - font_height(&font)) / 2);
scaled_font_buffer_update(font_buffer, workspace_name, 0,
&font, text_color, bg_color);
y += switcher_theme->item_height;
}
struct buf buf = BUF_INIT;
int nr_fields = wl_list_length(&rc.window_switcher.fields);
/* This is the width of the area available for text fields */
int field_widths_sum = w - 2 * padding
- 2 * switcher_theme->item_active_border_width
- (nr_fields + 1) * switcher_theme->item_padding_x;
if (field_widths_sum <= 0) {
wlr_log(WLR_ERROR, "Not enough spaces for osd contents");
goto error;
}
/* Draw text for each node */
struct view **view;
wl_array_for_each(view, views) {
struct osd_classic_item *item = znew(*item);
wl_list_append(&output->osd_scene.items, &item->base.link);
item->base.view = *view;
item->base.tree = wlr_scene_tree_create(output->osd_scene.tree);
node_descriptor_create(&item->base.tree->node,
LAB_NODE_OSD_ITEM, NULL, item);
/*
* OSD border
* +---------------------------------+
* | |
* | item border |
* |+-------------------------------+|
* || ||
* ||padding between each field ||
* ||| field-1 | field-2 | field-n |||
* || ||
* || ||
* |+-------------------------------+|
* | |
* | |
* +---------------------------------+
*/
int x = padding
+ switcher_theme->item_active_border_width
+ switcher_theme->item_padding_x;
item->normal_tree = wlr_scene_tree_create(item->base.tree);
item->active_tree = wlr_scene_tree_create(item->base.tree);
wlr_scene_node_set_enabled(&item->active_tree->node, false);
float *active_bg_color = switcher_theme->item_active_bg_color;
float *active_border_color = switcher_theme->item_active_border_color;
/* Highlight around selected window's item */
struct lab_scene_rect_options highlight_opts = {
.border_colors = (float *[1]) {active_border_color},
.bg_color = active_bg_color,
.nr_borders = 1,
.border_width = switcher_theme->item_active_border_width,
.width = w - 2 * padding,
.height = switcher_theme->item_height,
};
struct lab_scene_rect *highlight_rect = lab_scene_rect_create(
item->active_tree, &highlight_opts);
wlr_scene_node_set_position(&highlight_rect->tree->node, padding, y);
/* hitbox for mouse clicks */
struct wlr_scene_rect *hitbox = wlr_scene_rect_create(item->base.tree,
w - 2 * padding, switcher_theme->item_height, (float[4]) {0});
wlr_scene_node_set_position(&hitbox->node, padding, y);
create_fields_scene(server, *view, item->normal_tree,
text_color, bg_color, field_widths_sum, x, y);
create_fields_scene(server, *view, item->active_tree,
text_color, active_bg_color, field_widths_sum, x, y);
y += switcher_theme->item_height;
}
buf_reset(&buf);
error:;
/* Center OSD */
wlr_scene_node_set_position(&output->osd_scene.tree->node,
output_box.x + (output_box.width - w) / 2,
output_box.y + (output_box.height - h) / 2);
}
static void
osd_classic_update(struct output *output)
{
struct osd_classic_item *item;
wl_list_for_each(item, &output->osd_scene.items, base.link) {
bool active = item->base.view == output->server->osd_state.cycle_view;
wlr_scene_node_set_enabled(&item->normal_tree->node, !active);
wlr_scene_node_set_enabled(&item->active_tree->node, active);
}
}
struct osd_impl osd_classic_impl = {
.create = osd_classic_create,
.update = osd_classic_update,
};

370
src/cycle/osd-field.c Normal file
View file

@ -0,0 +1,370 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <ctype.h>
#include <wlr/util/log.h>
#include "common/buf.h"
#include "common/mem.h"
#include "config/rcxml.h"
#include "cycle.h"
#include "view.h"
#include "workspaces.h"
#include "labwc.h"
#include "desktop-entry.h"
#include "output.h"
/* includes '%', terminating 's' and NULL byte, 8 is enough for %-9999s */
#define LAB_FIELD_SINGLE_FMT_MAX_LEN 8
static_assert(LAB_FIELD_SINGLE_FMT_MAX_LEN <= 255, "fmt_position is a unsigned char");
/* forward declares */
typedef void field_conversion_type(struct buf *buf, struct view *view, const char *format);
struct field_converter {
const char fmt_char;
field_conversion_type *fn;
};
/* Internal helpers */
static const char *
get_identifier(struct view *view, bool trim)
{
const char *identifier = view->app_id;
/* remove the first two nodes of 'org.' strings */
if (trim && !strncmp(identifier, "org.", 4)) {
const char *p = identifier + 4;
p = strchr(p, '.');
if (p) {
return ++p;
}
}
return identifier;
}
static const char *
get_desktop_name(struct view *view)
{
#if HAVE_LIBSFDO
const char *name = desktop_entry_name_lookup(view->server, view->app_id);
if (name) {
return name;
}
#endif
return get_identifier(view, /* trim */ true);
}
static const char *
get_type(struct view *view, bool short_form)
{
switch (view->type) {
case LAB_XDG_SHELL_VIEW:
return short_form ? "[W]" : "[xdg-shell]";
#if HAVE_XWAYLAND
case LAB_XWAYLAND_VIEW:
return short_form ? "[X]" : "[xwayland]";
#endif
}
return "???";
}
static const char *
get_title_if_different(struct view *view)
{
const char *identifier = get_identifier(view, /*trim*/ false);
const char *title = view->title;
return !strcmp(identifier, title) ? NULL : title;
}
/* Field handlers */
static void
field_set_type(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: B (backend) */
buf_add(buf, get_type(view, /*short_form*/ false));
}
static void
field_set_type_short(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: b (backend) */
buf_add(buf, get_type(view, /*short_form*/ true));
}
static void
field_set_workspace(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: W */
buf_add(buf, view->workspace->name);
}
static void
field_set_workspace_short(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: w */
if (wl_list_length(&rc.workspace_config.workspaces) > 1) {
buf_add(buf, view->workspace->name);
}
}
static void
field_set_win_state(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: s */
if (view->minimized) {
buf_add(buf, "m");
} else if (view->shaded) {
buf_add(buf, "s");
} else if (view->maximized) {
buf_add(buf, "M");
} else if (view->fullscreen) {
buf_add(buf, "F");
} else {
buf_add(buf, " ");
}
}
static void
field_set_win_state_all(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: S */
buf_add(buf, view->minimized ? "m" : " ");
buf_add(buf, view->shaded ? "s" : " ");
buf_add(buf, view->maximized ? "M" : " ");
buf_add(buf, view->fullscreen ? "F" : " ");
/* TODO: add always-on-top and omnipresent ? */
}
static void
field_set_output(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: O */
if (output_is_usable(view->output)) {
buf_add(buf, view->output->wlr_output->name);
}
}
static void
field_set_output_short(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: o */
if (wl_list_length(&view->server->outputs) > 1 &&
output_is_usable(view->output)) {
buf_add(buf, view->output->wlr_output->name);
}
}
static void
field_set_identifier(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: I */
buf_add(buf, get_identifier(view, /*trim*/ false));
}
static void
field_set_identifier_trimmed(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: i */
buf_add(buf, get_identifier(view, /*trim*/ true));
}
static void
field_set_desktop_entry_name(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: n */
buf_add(buf, get_desktop_name(view));
}
static void
field_set_title(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: T */
buf_add(buf, view->title);
}
static void
field_set_title_short(struct buf *buf, struct view *view, const char *format)
{
/* custom type conversion-specifier: t */
buf_add(buf, get_title_if_different(view));
}
static const struct field_converter field_converter[LAB_FIELD_COUNT] = {
[LAB_FIELD_TYPE] = { 'B', field_set_type },
[LAB_FIELD_TYPE_SHORT] = { 'b', field_set_type_short },
[LAB_FIELD_WIN_STATE_ALL] = { 'S', field_set_win_state_all },
[LAB_FIELD_WIN_STATE] = { 's', field_set_win_state },
[LAB_FIELD_IDENTIFIER] = { 'I', field_set_identifier },
[LAB_FIELD_TRIMMED_IDENTIFIER] = { 'i', field_set_identifier_trimmed },
[LAB_FIELD_DESKTOP_ENTRY_NAME] = { 'n', field_set_desktop_entry_name},
[LAB_FIELD_WORKSPACE] = { 'W', field_set_workspace },
[LAB_FIELD_WORKSPACE_SHORT] = { 'w', field_set_workspace_short },
[LAB_FIELD_OUTPUT] = { 'O', field_set_output },
[LAB_FIELD_OUTPUT_SHORT] = { 'o', field_set_output_short },
[LAB_FIELD_TITLE] = { 'T', field_set_title },
[LAB_FIELD_TITLE_SHORT] = { 't', field_set_title_short },
/* fmt_char can never be matched so prevents LAB_FIELD_CUSTOM recursion */
[LAB_FIELD_CUSTOM] = { '\0', osd_field_set_custom },
};
void
osd_field_set_custom(struct buf *buf, struct view *view, const char *format)
{
if (!format) {
wlr_log(WLR_ERROR, "Missing format for custom window switcher field");
return;
}
char fmt[LAB_FIELD_SINGLE_FMT_MAX_LEN];
unsigned char fmt_position = 0;
struct buf field_result = BUF_INIT;
char converted_field[4096];
for (const char *p = format; *p; p++) {
if (!fmt_position) {
if (*p == '%') {
fmt[fmt_position++] = *p;
} else {
/*
* Just relay anything not part of a
* format string to the output buffer.
*/
buf_add_char(buf, *p);
}
continue;
}
/* Allow string formatting */
/* TODO: add . for manual truncating? */
/*
* Remove the || *p == '#' section as not used/needed
* change (*p >= '0' && *p <= '9') to isdigit(*p)
* changes by droc12345
*/
if (*p == '-' || isdigit(*p)) {
if (fmt_position >= LAB_FIELD_SINGLE_FMT_MAX_LEN - 2) {
/* Leave space for terminating 's' and NULL byte */
wlr_log(WLR_ERROR,
"single format string length exceeded: '%s'", p);
} else {
fmt[fmt_position++] = *p;
}
continue;
}
/* Handlers */
for (unsigned char i = 0; i < LAB_FIELD_COUNT; i++) {
if (*p != field_converter[i].fmt_char) {
continue;
}
/* Generate the actual content*/
field_converter[i].fn(&field_result, view, /*format*/ NULL);
/* Throw it at snprintf to allow formatting / padding */
fmt[fmt_position++] = 's';
fmt[fmt_position++] = '\0';
snprintf(converted_field, sizeof(converted_field),
fmt, field_result.data);
/* And finally write it to the output buffer */
buf_add(buf, converted_field);
goto reset_format;
}
wlr_log(WLR_ERROR,
"invalid format character found for osd %s: '%c'",
format, *p);
reset_format:
/* Reset format string and tmp field result buffer */
buf_clear(&field_result);
fmt_position = 0;
}
buf_reset(&field_result);
}
void
osd_field_arg_from_xml_node(struct window_switcher_field *field,
const char *nodename, const char *content)
{
if (!strcmp(nodename, "content")) {
if (!strcmp(content, "type")) {
field->content = LAB_FIELD_TYPE;
} else if (!strcmp(content, "type_short")) {
field->content = LAB_FIELD_TYPE_SHORT;
} else if (!strcmp(content, "app_id")) {
wlr_log(WLR_ERROR, "window-switcher field 'app_id' is deprecated");
field->content = LAB_FIELD_IDENTIFIER;
} else if (!strcmp(content, "identifier")) {
field->content = LAB_FIELD_IDENTIFIER;
} else if (!strcmp(content, "trimmed_identifier")) {
field->content = LAB_FIELD_TRIMMED_IDENTIFIER;
} else if (!strcmp(content, "icon")) {
field->content = LAB_FIELD_ICON;
} else if (!strcmp(content, "desktop_entry_name")) {
field->content = LAB_FIELD_DESKTOP_ENTRY_NAME;
} else if (!strcmp(content, "title")) {
field->content = LAB_FIELD_TITLE;
} else if (!strcmp(content, "workspace")) {
field->content = LAB_FIELD_WORKSPACE;
} else if (!strcmp(content, "state")) {
field->content = LAB_FIELD_WIN_STATE;
} else if (!strcmp(content, "output")) {
field->content = LAB_FIELD_OUTPUT;
} else if (!strcmp(content, "custom")) {
field->content = LAB_FIELD_CUSTOM;
} else {
wlr_log(WLR_ERROR, "bad windowSwitcher field '%s'", content);
}
} else if (!strcmp(nodename, "format")) {
zfree(field->format);
field->format = xstrdup(content);
} else if (!strcmp(nodename, "width") && !strchr(content, '%')) {
wlr_log(WLR_ERROR, "Invalid osd field width: %s, misses trailing %%", content);
} else if (!strcmp(nodename, "width")) {
field->width = atoi(content);
} else {
wlr_log(WLR_ERROR, "Unexpected data in field parser: %s=\"%s\"",
nodename, content);
}
}
bool
osd_field_is_valid(struct window_switcher_field *field)
{
if (field->content == LAB_FIELD_NONE) {
wlr_log(WLR_ERROR, "Invalid OSD field: no content set");
return false;
}
if (field->content == LAB_FIELD_CUSTOM && !field->format) {
wlr_log(WLR_ERROR, "Invalid OSD field: custom without format");
return false;
}
if (!field->width) {
wlr_log(WLR_ERROR, "Invalid OSD field: no width");
return false;
}
return true;
}
void
osd_field_get_content(struct window_switcher_field *field,
struct buf *buf, struct view *view)
{
if (field->content == LAB_FIELD_NONE) {
wlr_log(WLR_ERROR, "Invalid window switcher field type");
return;
}
assert(field->content < LAB_FIELD_COUNT && field_converter[field->content].fn);
field_converter[field->content].fn(buf, view, field->format);
}
void
osd_field_free(struct window_switcher_field *field)
{
zfree(field->format);
zfree(field);
}

300
src/cycle/osd-thumbnail.c Normal file
View file

@ -0,0 +1,300 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <wlr/render/allocator.h>
#include <wlr/render/swapchain.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include "config/rcxml.h"
#include "common/array.h"
#include "common/box.h"
#include "common/buf.h"
#include "common/lab-scene-rect.h"
#include "common/list.h"
#include "cycle.h"
#include "labwc.h"
#include "node.h"
#include "output.h"
#include "scaled-buffer/scaled-font-buffer.h"
#include "scaled-buffer/scaled-icon-buffer.h"
#include "theme.h"
#include "view.h"
struct osd_thumbnail_item {
struct osd_item base;
struct scaled_font_buffer *normal_label;
struct scaled_font_buffer *active_label;
struct lab_scene_rect *active_bg;
};
static void
render_node(struct server *server, struct wlr_render_pass *pass,
struct wlr_scene_node *node, int x, int y)
{
switch (node->type) {
case WLR_SCENE_NODE_TREE: {
struct wlr_scene_tree *tree = wlr_scene_tree_from_node(node);
struct wlr_scene_node *child;
wl_list_for_each(child, &tree->children, link) {
render_node(server, pass, child, x + node->x, y + node->y);
}
break;
}
case WLR_SCENE_NODE_BUFFER: {
struct wlr_scene_buffer *scene_buffer =
wlr_scene_buffer_from_node(node);
if (!scene_buffer->buffer) {
break;
}
struct wlr_texture *texture = wlr_texture_from_buffer(
server->renderer, scene_buffer->buffer);
if (!texture) {
break;
}
wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options){
.texture = texture,
.src_box = scene_buffer->src_box,
.dst_box = {
.x = x,
.y = y,
.width = scene_buffer->dst_width,
.height = scene_buffer->dst_height,
},
.transform = scene_buffer->transform,
});
wlr_texture_destroy(texture);
break;
}
case WLR_SCENE_NODE_RECT:
/* should be unreached */
wlr_log(WLR_ERROR, "ignoring rect");
break;
}
}
static struct wlr_buffer *
render_thumb(struct output *output, struct view *view)
{
if (!view->content_tree) {
/*
* Defensive. Could possibly occur if view was unmapped
* with OSD already displayed.
*/
return NULL;
}
struct server *server = output->server;
struct wlr_buffer *buffer = wlr_allocator_create_buffer(server->allocator,
view->current.width, view->current.height,
&output->wlr_output->swapchain->format);
struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(
server->renderer, buffer, NULL);
render_node(server, pass, &view->content_tree->node, 0, 0);
if (!wlr_render_pass_submit(pass)) {
wlr_log(WLR_ERROR, "failed to submit render pass");
wlr_buffer_drop(buffer);
return NULL;
}
return buffer;
}
static struct scaled_font_buffer *
create_label(struct wlr_scene_tree *parent, struct view *view,
struct window_switcher_thumbnail_theme *switcher_theme,
const float *text_color, const float *bg_color, int y)
{
struct buf buf = BUF_INIT;
osd_field_set_custom(&buf, view,
rc.window_switcher.thumbnail_label_format);
struct scaled_font_buffer *buffer =
scaled_font_buffer_create(parent);
scaled_font_buffer_update(buffer, buf.data,
switcher_theme->item_width - 2 * switcher_theme->item_padding,
&rc.font_osd, text_color, bg_color);
buf_reset(&buf);
wlr_scene_node_set_position(&buffer->scene_buffer->node,
(switcher_theme->item_width - buffer->width) / 2, y);
return buffer;
}
static struct osd_thumbnail_item *
create_item_scene(struct wlr_scene_tree *parent, struct view *view,
struct 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->border_width + switcher_theme->item_padding;
int title_y = switcher_theme->item_height - padding - switcher_theme->title_height;
struct wlr_box thumb_bounds = {
.x = padding,
.y = padding,
.width = switcher_theme->item_width - 2 * padding,
.height = title_y - 2 * padding,
};
if (thumb_bounds.width <= 0 || thumb_bounds.height <= 0) {
wlr_log(WLR_ERROR, "too small thumbnail area");
return NULL;
}
struct osd_thumbnail_item *item = znew(*item);
wl_list_append(&output->osd_scene.items, &item->base.link);
struct wlr_scene_tree *tree = wlr_scene_tree_create(parent);
node_descriptor_create(&tree->node, LAB_NODE_OSD_ITEM, NULL, item);
item->base.tree = tree;
item->base.view = view;
/* background for selected item */
struct lab_scene_rect_options opts = {
.border_colors = (float *[1]) { switcher_theme->item_active_border_color },
.nr_borders = 1,
.border_width = switcher_theme->item_active_border_width,
.bg_color = switcher_theme->item_active_bg_color,
.width = switcher_theme->item_width,
.height = switcher_theme->item_height,
};
item->active_bg = lab_scene_rect_create(tree, &opts);
/* hitbox for mouse clicks */
wlr_scene_rect_create(tree, switcher_theme->item_width,
switcher_theme->item_height, (float[4]) {0});
/* thumbnail */
struct wlr_buffer *thumb_buffer = render_thumb(output, view);
if (thumb_buffer) {
struct wlr_scene_buffer *thumb_scene_buffer =
wlr_scene_buffer_create(tree, thumb_buffer);
wlr_buffer_drop(thumb_buffer);
struct wlr_box thumb_box = box_fit_within(
thumb_buffer->width, thumb_buffer->height,
&thumb_bounds);
wlr_scene_buffer_set_dest_size(thumb_scene_buffer,
thumb_box.width, thumb_box.height);
wlr_scene_node_set_position(&thumb_scene_buffer->node,
thumb_box.x, thumb_box.y);
}
/* title */
item->normal_label = create_label(tree, view,
switcher_theme, theme->osd_label_text_color,
theme->osd_bg_color, title_y);
item->active_label = create_label(tree, view,
switcher_theme, theme->osd_label_text_color,
switcher_theme->item_active_bg_color, title_y);
/* icon */
int icon_size = switcher_theme->item_icon_size;
struct scaled_icon_buffer *icon_buffer =
scaled_icon_buffer_create(tree, server, icon_size, icon_size);
scaled_icon_buffer_set_view(icon_buffer, view);
int x = (switcher_theme->item_width - icon_size) / 2;
int y = title_y - padding - icon_size + 10; /* slide by 10px */
wlr_scene_node_set_position(&icon_buffer->scene_buffer->node, x, y);
return item;
}
static void
get_items_geometry(struct output *output, struct theme *theme,
int nr_thumbs, int *nr_rows, int *nr_cols)
{
struct window_switcher_thumbnail_theme *switcher_theme =
&theme->osd_window_switcher_thumbnail;
int output_width, output_height;
wlr_output_effective_resolution(output->wlr_output,
&output_width, &output_height);
int padding = theme->osd_border_width + switcher_theme->padding;
int max_bg_width = switcher_theme->max_width;
if (switcher_theme->max_width_is_percent) {
max_bg_width = output_width * switcher_theme->max_width / 100;
}
*nr_rows = 1;
*nr_cols = nr_thumbs;
while (1) {
assert(*nr_rows <= nr_thumbs);
int bg_width = *nr_cols * switcher_theme->item_width + 2 * padding;
if (bg_width < max_bg_width) {
break;
}
if (*nr_rows >= nr_thumbs) {
break;
}
(*nr_rows)++;
*nr_cols = ceilf((float)nr_thumbs / *nr_rows);
}
}
static void
osd_thumbnail_create(struct output *output, struct wl_array *views)
{
assert(!output->osd_scene.tree && wl_list_empty(&output->osd_scene.items));
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->osd_scene.tree = wlr_scene_tree_create(output->osd_tree);
int nr_views = wl_array_len(views);
assert(nr_views > 0);
int nr_rows, nr_cols;
get_items_geometry(output, theme, nr_views, &nr_rows, &nr_cols);
/* items */
struct view **view;
int index = 0;
wl_array_for_each(view, views) {
struct osd_thumbnail_item *item = create_item_scene(
output->osd_scene.tree, *view, output);
if (!item) {
break;
}
int x = (index % nr_cols) * switcher_theme->item_width + padding;
int y = (index / nr_cols) * switcher_theme->item_height + padding;
wlr_scene_node_set_position(&item->base.tree->node, x, y);
index++;
}
/* background */
struct lab_scene_rect_options bg_opts = {
.border_colors = (float *[1]) { theme->osd_border_color },
.nr_borders = 1,
.border_width = theme->osd_border_width,
.bg_color = theme->osd_bg_color,
.width = nr_cols * switcher_theme->item_width + 2 * padding,
.height = nr_rows * switcher_theme->item_height + 2 * padding,
};
struct lab_scene_rect *bg =
lab_scene_rect_create(output->osd_scene.tree, &bg_opts);
wlr_scene_node_lower_to_bottom(&bg->tree->node);
/* center */
struct wlr_box output_box;
wlr_output_layout_get_box(server->output_layout, output->wlr_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->osd_scene.tree->node, lx, ly);
}
static void
osd_thumbnail_update(struct output *output)
{
struct osd_thumbnail_item *item;
wl_list_for_each(item, &output->osd_scene.items, base.link) {
bool active = (item->base.view == output->server->osd_state.cycle_view);
wlr_scene_node_set_enabled(&item->active_bg->tree->node, active);
wlr_scene_node_set_enabled(
&item->active_label->scene_buffer->node, active);
wlr_scene_node_set_enabled(
&item->normal_label->scene_buffer->node, !active);
}
}
struct osd_impl osd_thumbnail_impl = {
.create = osd_thumbnail_create,
.update = osd_thumbnail_update,
};