commit 49ebfc3ccffc37ccb3289e5886097c19db56fe16
parent 5b23c4895c02776e5fbc10c5aafd69edfb5db99d
Author: shrub <maybeshrub@gmail.com>
Date: Tue, 28 Apr 2026 17:57:02 +0000
decor: add configurable window decorations api
Diffstat:
7 files changed, 527 insertions(+), 24 deletions(-)
diff --git a/libswc/compositor.c b/libswc/compositor.c
@@ -29,6 +29,7 @@
#include "compositor.h"
#include "data_device_manager.h"
+#include "decor.h"
#include "drm.h"
#include "event.h"
#include "internal.h"
@@ -354,6 +355,7 @@ repaint_view(struct target *target, struct compositor_view *view,
}
pixman_region32_fini(&decor_region);
pixman_region32_fini(&content_region);
+ decor_repaint(swc.drm->renderer, target_geom, view, damage);
}
}
@@ -1300,6 +1302,7 @@ compositor_create_view(struct surface *surface)
view->decor.right = 0;
view->decor.bottom = 0;
view->decor.left = 0;
+ decor_view_initialize(view);
view->decor.damaged = false;
pixman_region32_init(&view->clip);
wl_signal_init(&view->destroy_signal);
@@ -1316,6 +1319,7 @@ compositor_view_destroy(struct compositor_view *view)
compositor_view_hide(view);
surface_set_view(view->surface, NULL);
view_finalize(&view->base);
+ decor_view_finalize(view);
pixman_region32_fini(&view->clip);
wl_list_remove(&view->link);
free(view);
@@ -1467,24 +1471,23 @@ compositor_view_set_border_color(struct compositor_view *view,
}
void
-compositor_view_set_decor(struct compositor_view *view, uint32_t color,
- uint32_t top, uint32_t right, uint32_t bottom,
- uint32_t left)
+compositor_view_set_decor(struct compositor_view *view,
+ const struct swc_decor *decor)
{
- if (view->decor.color == color && view->decor.top == top &&
- view->decor.right == right && view->decor.bottom == bottom &&
- view->decor.left == left) {
+ decor_view_set(view, decor);
+ update_extents(view);
+ update(&view->base);
+}
+
+void
+compositor_view_damage_decor(struct compositor_view *view)
+{
+ if (!view->decor.top && !view->decor.right && !view->decor.bottom &&
+ !view->decor.left) {
return;
}
- view->decor.color = color;
- view->decor.top = top;
- view->decor.right = right;
- view->decor.bottom = bottom;
- view->decor.left = left;
- view->decor.damaged = true;
-
- update_extents(view);
+ decor_view_damage(view);
update(&view->base);
}
@@ -1799,6 +1802,9 @@ compositor_initialize(void)
compositor.updating = false;
compositor.zoom = 1.0f;
compositor.zoom_buffer = NULL;
+ if (!decor_initialize()) {
+ return false;
+ }
pixman_region32_init(&compositor.damage);
pixman_region32_init(&compositor.opaque);
wl_list_init(&compositor.views);
@@ -1834,6 +1840,7 @@ compositor_finalize(void)
wld_buffer_unreference(compositor.zoom_buffer);
compositor.zoom_buffer = NULL;
}
+ decor_finalize();
pixman_region32_fini(&compositor.damage);
pixman_region32_fini(&compositor.opaque);
wl_global_destroy(compositor.global);
@@ -1971,6 +1978,36 @@ compositor_render_to_shm(struct screen *screen)
pixman_region32_fini(&out_border);
pixman_region32_fini(&in_border);
}
+
+ if (view->decor.top || view->decor.right || view->decor.bottom ||
+ view->decor.left) {
+ pixman_region32_t decor_region, content_region;
+ const struct swc_rectangle *geom = &view->base.geometry;
+ const struct swc_rectangle *target_geom = &screen->base.geometry;
+ int32_t x = geom->x - (int32_t)view->decor.left;
+ int32_t y = geom->y - (int32_t)view->decor.top;
+ uint32_t decor_width =
+ geom->width + view->decor.left + view->decor.right;
+ uint32_t decor_height =
+ geom->height + view->decor.top + view->decor.bottom;
+
+ pixman_region32_init_rect(&decor_region, x, y, decor_width,
+ decor_height);
+ pixman_region32_init_rect(&content_region, geom->x, geom->y,
+ geom->width, geom->height);
+ pixman_region32_subtract(&decor_region, &decor_region, &content_region);
+ pixman_region32_intersect(&decor_region, &decor_region, &damage);
+ pixman_region32_subtract(&decor_region, &decor_region, &view->clip);
+ if (pixman_region32_not_empty(&decor_region)) {
+ pixman_region32_translate(&decor_region, -target_geom->x,
+ -target_geom->y);
+ wld_fill_region(swc.shm->renderer, view->decor.color,
+ &decor_region);
+ }
+ pixman_region32_fini(&decor_region);
+ pixman_region32_fini(&content_region);
+ decor_repaint(swc.shm->renderer, target_geom, view, &damage);
+ }
}
draw_overlays(swc.shm->renderer, &screen->base.geometry);
diff --git a/libswc/compositor.h b/libswc/compositor.h
@@ -32,6 +32,7 @@
struct screen;
struct wld_buffer;
+struct wld_font;
struct swc_compositor {
struct pointer_handler *const pointer_handler;
@@ -91,6 +92,10 @@ struct compositor_view {
struct {
uint32_t color;
uint32_t top, right, bottom, left;
+ struct swc_decor_text text;
+ char *string;
+ char *font_name;
+ struct wld_font *font;
bool damaged;
} decor;
@@ -129,9 +134,10 @@ void
compositor_view_set_border_width(struct compositor_view *view,
uint32_t outwidth, uint32_t inwidth);
void
-compositor_view_set_decor(struct compositor_view *view, uint32_t color,
- uint32_t top, uint32_t right, uint32_t bottom,
- uint32_t left);
+compositor_view_set_decor(struct compositor_view *view,
+ const struct swc_decor *decor);
+void
+compositor_view_damage_decor(struct compositor_view *view);
/**
* get the current composited buffer for a screen for screenshotss.
diff --git a/libswc/decor.c b/libswc/decor.c
@@ -0,0 +1,361 @@
+#include "decor.h"
+
+#include "compositor.h"
+#include "swc.h"
+#include "window.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <wld/wld.h>
+
+#define DEFAULT_DECOR_FONT "sans-serif:size=10"
+
+static struct wld_font_context *font_context;
+
+static uint32_t
+decor_title_length(struct wld_font *font, const char *title, uint32_t max_width)
+{
+ uint32_t len = 0;
+ struct wld_extents extents;
+
+ if (!title || !max_width) {
+ return 0;
+ }
+
+ while (title[len]) {
+ uint32_t next = len + 1;
+
+ while ((title[next] & 0xc0) == 0x80) {
+ next++;
+ }
+
+ wld_font_text_extents_n(font, title, (int32_t)next, &extents);
+ if (extents.advance > max_width) {
+ break;
+ }
+ len = next;
+ }
+
+ return len;
+}
+
+static uint32_t
+utf8_next_len(const char *text, uint32_t offset)
+{
+ uint32_t next = offset + 1;
+
+ while ((text[next] & 0xc0) == 0x80) {
+ next++;
+ }
+
+ return next - offset;
+}
+
+static uint32_t
+decor_title_stacked_length(struct wld_font *font, const char *title,
+ uint32_t max_height, uint32_t *glyph_count)
+{
+ uint32_t len = 0, count = 0;
+
+ if (!title || !font->height) {
+ *glyph_count = 0;
+ return 0;
+ }
+
+ while (title[len] && (count + 1) * font->height <= max_height) {
+ len += utf8_next_len(title, len);
+ count++;
+ }
+
+ *glyph_count = count;
+ return len;
+}
+
+static bool
+streq(const char *a, const char *b)
+{
+ if (!a || !b) {
+ return a == b;
+ }
+ return strcmp(a, b) == 0;
+}
+
+static void
+close_decor_font(struct compositor_view *view)
+{
+ if (view->decor.font) {
+ wld_font_close(view->decor.font);
+ view->decor.font = NULL;
+ }
+ free(view->decor.font_name);
+ view->decor.font_name = NULL;
+}
+
+static void
+close_decor_string(struct compositor_view *view)
+{
+ free(view->decor.string);
+ view->decor.string = NULL;
+}
+
+bool
+decor_initialize(void)
+{
+ font_context = wld_font_create_context();
+ return font_context != NULL;
+}
+
+void
+decor_finalize(void)
+{
+ if (font_context) {
+ wld_font_destroy_context(font_context);
+ font_context = NULL;
+ }
+}
+
+void
+decor_view_initialize(struct compositor_view *view)
+{
+ memset(&view->decor.text, 0, sizeof(view->decor.text));
+ view->decor.string = NULL;
+ view->decor.font_name = NULL;
+ view->decor.font = NULL;
+}
+
+void
+decor_view_finalize(struct compositor_view *view)
+{
+ close_decor_string(view);
+ close_decor_font(view);
+}
+
+void
+decor_repaint(struct wld_renderer *renderer,
+ const struct swc_rectangle *target_geom,
+ struct compositor_view *view, pixman_region32_t *damage)
+{
+ const struct swc_rectangle *geom = &view->base.geometry;
+ const struct swc_decor_text *text = &view->decor.text;
+ struct wld_font *font = view->decor.font;
+ const char *title = view->decor.string;
+ uint32_t title_len, max_width, decor_size;
+ int32_t x, y, base_x, base_y, advance, available_width;
+ struct wld_extents extents;
+ pixman_region32_t title_region;
+
+ if (!title || !font || !text->enabled) {
+ return;
+ }
+
+ switch (text->edge) {
+ case SWC_DECOR_EDGE_TOP:
+ if (!view->decor.top) {
+ return;
+ }
+ base_x = geom->x - (int32_t)view->decor.left;
+ base_y = geom->y - (int32_t)view->decor.top;
+ max_width = geom->width + view->decor.left + view->decor.right;
+ decor_size = view->decor.top;
+ break;
+ case SWC_DECOR_EDGE_BOTTOM:
+ if (!view->decor.bottom) {
+ return;
+ }
+ base_x = geom->x - (int32_t)view->decor.left;
+ base_y = geom->y + (int32_t)geom->height;
+ max_width = geom->width + view->decor.left + view->decor.right;
+ decor_size = view->decor.bottom;
+ break;
+ case SWC_DECOR_EDGE_LEFT:
+ if (!view->decor.left) {
+ return;
+ }
+ base_x = geom->x - (int32_t)view->decor.left;
+ base_y = geom->y - (int32_t)view->decor.top;
+ max_width = view->decor.left;
+ decor_size = geom->height + view->decor.top + view->decor.bottom;
+ break;
+ case SWC_DECOR_EDGE_RIGHT:
+ if (!view->decor.right) {
+ return;
+ }
+ base_x = geom->x + (int32_t)geom->width;
+ base_y = geom->y - (int32_t)view->decor.top;
+ max_width = view->decor.right;
+ decor_size = geom->height + view->decor.top + view->decor.bottom;
+ break;
+ default:
+ return;
+ }
+
+ x = base_x;
+ y = base_y;
+
+ pixman_region32_init_rect(&title_region, x, y, max_width, decor_size);
+ pixman_region32_intersect(&title_region, &title_region, damage);
+ pixman_region32_subtract(&title_region, &title_region, &view->clip);
+ if (!pixman_region32_not_empty(&title_region)) {
+ pixman_region32_fini(&title_region);
+ return;
+ }
+ pixman_region32_fini(&title_region);
+
+ if (max_width <= text->padding * 2 || decor_size < font->height) {
+ return;
+ }
+ max_width -= text->padding * 2;
+
+ if (text->edge == SWC_DECOR_EDGE_LEFT || text->edge == SWC_DECOR_EDGE_RIGHT) {
+ uint32_t glyph_count, glyph_offset = 0, available_height, total_height;
+
+ if (decor_size <= text->padding * 2) {
+ return;
+ }
+
+ available_height = decor_size - text->padding * 2;
+ title_len = decor_title_stacked_length(font, title, available_height,
+ &glyph_count);
+ if (!title_len) {
+ return;
+ }
+
+ total_height = glyph_count * font->height;
+ y += (int32_t)text->padding;
+ switch (text->align) {
+ case SWC_DECOR_ALIGN_START:
+ break;
+ case SWC_DECOR_ALIGN_CENTER:
+ y += (int32_t)((available_height - total_height) / 2);
+ break;
+ case SWC_DECOR_ALIGN_END:
+ y += (int32_t)(available_height - total_height);
+ break;
+ }
+
+ while (glyph_offset < title_len) {
+ uint32_t glyph_len = utf8_next_len(title, glyph_offset);
+ int32_t glyph_x;
+
+ wld_font_text_extents_n(font, title + glyph_offset,
+ (int32_t)glyph_len, &extents);
+ advance = extents.advance > 0 ? extents.advance : 0;
+ glyph_x = x + (int32_t)text->padding;
+ if ((uint32_t)advance < max_width) {
+ glyph_x += ((int32_t)max_width - advance) / 2;
+ }
+
+ wld_draw_text(renderer, font, text->color,
+ glyph_x + text->offset_x - target_geom->x,
+ y + (int32_t)font->ascent + text->offset_y -
+ target_geom->y,
+ title + glyph_offset, glyph_len, NULL);
+
+ glyph_offset += glyph_len;
+ y += (int32_t)font->height;
+ }
+
+ return;
+ }
+
+ title_len = decor_title_length(font, title, max_width);
+ if (!title_len) {
+ return;
+ }
+ wld_font_text_extents_n(font, title, (int32_t)title_len, &extents);
+ advance = extents.advance > 0 ? extents.advance : 0;
+ available_width = (int32_t)max_width;
+
+ switch (text->align) {
+ case SWC_DECOR_ALIGN_START:
+ x += text->padding;
+ break;
+ case SWC_DECOR_ALIGN_CENTER:
+ x += (int32_t)text->padding + (available_width - advance) / 2;
+ break;
+ case SWC_DECOR_ALIGN_END:
+ x += (int32_t)text->padding + (int32_t)max_width - advance;
+ break;
+ }
+
+ y += (int32_t)((decor_size - font->height) / 2) + (int32_t)font->ascent;
+
+ x += text->offset_x;
+ y += text->offset_y;
+
+ wld_draw_text(renderer, font, text->color, x - target_geom->x,
+ y - target_geom->y, title, title_len, NULL);
+}
+
+void
+decor_view_set(struct compositor_view *view, const struct swc_decor *decor)
+{
+ const char *font_name = NULL;
+ struct wld_font *font = NULL;
+ struct swc_decor_text text = { 0 };
+ char *owned_string = NULL;
+ char *owned_font_name = NULL;
+ uint32_t color = 0, top = 0, right = 0, bottom = 0, left = 0;
+
+ if (!decor) {
+ goto apply;
+ }
+
+ color = decor->color;
+ top = decor->top;
+ right = decor->right;
+ bottom = decor->bottom;
+ left = decor->left;
+ text = decor->title;
+ if (text.enabled) {
+ font_name = text.font ? text.font : DEFAULT_DECOR_FONT;
+ }
+
+ if (view->decor.color == color && view->decor.top == top &&
+ view->decor.right == right && view->decor.bottom == bottom &&
+ view->decor.left == left &&
+ view->decor.text.enabled == text.enabled &&
+ view->decor.text.edge == text.edge &&
+ view->decor.text.align == text.align &&
+ streq(view->decor.string, text.string) &&
+ view->decor.text.color == text.color &&
+ view->decor.text.padding == text.padding &&
+ view->decor.text.offset_x == text.offset_x &&
+ view->decor.text.offset_y == text.offset_y &&
+ streq(view->decor.font_name, font_name)) {
+ return;
+ }
+
+ if (text.string) {
+ owned_string = strdup(text.string);
+ }
+
+ if (font_name) {
+ owned_font_name = strdup(font_name);
+ if (owned_font_name && font_context) {
+ font = wld_font_open_name(font_context, font_name);
+ }
+ }
+
+apply:
+ view->decor.color = color;
+ view->decor.top = top;
+ view->decor.right = right;
+ view->decor.bottom = bottom;
+ view->decor.left = left;
+ view->decor.text = text;
+ view->decor.text.string = NULL;
+ view->decor.text.font = NULL;
+ close_decor_string(view);
+ view->decor.string = owned_string;
+ close_decor_font(view);
+ view->decor.font_name = owned_font_name;
+ view->decor.font = font;
+ view->decor.damaged = true;
+}
+
+void
+decor_view_damage(struct compositor_view *view)
+{
+ view->decor.damaged = true;
+}
diff --git a/libswc/decor.h b/libswc/decor.h
@@ -0,0 +1,31 @@
+#ifndef SWC_DECOR_H
+#define SWC_DECOR_H
+
+#include <stdbool.h>
+#include <pixman.h>
+
+struct compositor_view;
+struct swc_decor;
+struct swc_rectangle;
+struct wld_renderer;
+
+bool
+decor_initialize(void);
+void
+decor_finalize(void);
+
+void
+decor_view_initialize(struct compositor_view *view);
+void
+decor_view_finalize(struct compositor_view *view);
+
+void
+decor_repaint(struct wld_renderer *renderer,
+ const struct swc_rectangle *target_geom,
+ struct compositor_view *view, pixman_region32_t *damage);
+void
+decor_view_set(struct compositor_view *view, const struct swc_decor *decor);
+void
+decor_view_damage(struct compositor_view *view);
+
+#endif
diff --git a/libswc/meson.build b/libswc/meson.build
@@ -6,6 +6,7 @@ libswc_src = files(
'data.c',
'data_device.c',
'data_device_manager.c',
+ 'decor.c',
'dmabuf.c',
'drm.c',
'input.c',
diff --git a/libswc/swc.h b/libswc/swc.h
@@ -390,15 +390,83 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color,
uint32_t inner_border_width, uint32_t outer_border_color,
uint32_t outer_border_width);
+/*window decor things*/
+
/**
- * Set decor around the window content
+ * Select which decor edge a text slot is rendered on
+ */
+enum swc_decor_edge {
+ SWC_DECOR_EDGE_TOP,
+ SWC_DECOR_EDGE_RIGHT,
+ SWC_DECOR_EDGE_BOTTOM,
+ SWC_DECOR_EDGE_LEFT,
+};
+
+/**
+ * choose text alignment within the selected decor edge.
+ *
+ * for top and bottom edges, alignment is horizontal
+ * for left and right edges, alignment is vertical
+ */
+enum swc_decor_align {
+ SWC_DECOR_ALIGN_START,
+ SWC_DECOR_ALIGN_CENTER,
+ SWC_DECOR_ALIGN_END,
+};
+
+/**
+ * describe a window decor text slot.
+ *
+ * if enabled is false, no text is drawn.
+ *
+ * the wm supplies some string to be rendered in this decor slot. swc
+ * copies the string when swc_window_set_decor() is called, so caller doesnt
+ * need to keep it after the call returns.
+ *
+ * for top and bottom edges, text is drawn horizontally
+ * for left and right edges, text is drawn as stacked glyphs, one glyph
+ * per row.
+ *
+ * the font field accepts some fontconfig pattern such as "sans-serif:size=10".
+ * if font is NULL, a default font is used (dont be a loser, pick a cool font)
+ */
+struct swc_decor_text {
+ bool enabled;
+ enum swc_decor_edge edge;
+ enum swc_decor_align align;
+ const char *string;
+ uint32_t color;
+ uint32_t padding;
+ int32_t offset_x, offset_y;
+ const char *font;
+};
+
+/**
+ * describe decor around a window's edges.
+ *
+ * The edge sizes extend outward from the window content geometry.
+ * if you put 0 it won't be visible
+ *
+ * the title field controls an optional text slot rendered on one edge
+ */
+struct swc_decor {
+ uint32_t color;
+ uint32_t top, right, bottom, left;
+ struct swc_decor_text title;
+};
+
+/**
+ * set window decor around the window.
*
* the dimensions are independent for each edge and extend outward from the
- * window content geometry. passing all dimensions as 0 disables this decor.
+ * window content geometry.
+ *
+ * swc copies any decor text string needed by the configuration
+ *
+ * passing NULL disables window dcor
*/
void
-swc_window_set_decor(struct swc_window *window, uint32_t color, uint32_t top,
- uint32_t right, uint32_t bottom, uint32_t left);
+swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor);
/**
* Begin an interactive move of the specified window.
diff --git a/libswc/window.c b/libswc/window.c
@@ -368,12 +368,11 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color,
}
EXPORT void
-swc_window_set_decor(struct swc_window *window, uint32_t color, uint32_t top,
- uint32_t right, uint32_t bottom, uint32_t left)
+swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor)
{
struct compositor_view *view = INTERNAL(window)->view;
- compositor_view_set_decor(view, color, top, right, bottom, left);
+ compositor_view_set_decor(view, decor);
}
EXPORT void