Files
dwl-patches/patches/scenefx/scenefx.patch
T
2026-10-29 18:15:34 +01:00

426 lines
18 KiB
Diff

From 10d093f65881d49ff5e919168451ab75f16adce0 Mon Sep 17 00:00:00 2001
From: Guido Cella <guido@guidocella.xyz>
Date: Thu, 29 Oct 2026 18:06:25 +0100
Subject: [PATCH] integrate scenefx
Add rounded corners, shadows and blur with scenefx.
The border is now a single rectangle with a hole for the window contents,
so its corners can be rounded concentrically with them. Buffers are rounded
when their corners coincide with the window's, which includes subsurfaces
covering it.
Tiled windows show the cached blur of the background layer, which is
redrawn only when it changes, while floating windows blur what's behind
them. Fullscreen windows have no effects so that fullscreen_bg keeps
hiding what's behind them.
scenefx has to be linked before wlroots so that its wlr_scene functions
override wlroots' ones, including those called by wlroots' scene surface
helpers, which scenefx doesn't reimplement.
---
Makefile | 4 +-
client.h | 4 +-
config.def.h | 8 +++
config.mk | 5 ++
dwl.c | 144 +++++++++++++++++++++++++++++++++++++++++++--------
5 files changed, 137 insertions(+), 28 deletions(-)
diff --git a/Makefile b/Makefile
index 8304245..aad87ba 100644
--- a/Makefile
+++ b/Makefile
@@ -13,8 +13,8 @@ DWLDEVCFLAGS = -g -Wpedantic -Wall -Wextra -Wdeclaration-after-statement \
# CFLAGS / LDFLAGS
PKGS = wayland-server xkbcommon libinput $(XLIBS)
-DWLCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(WLR_INCS) $(DWLCPPFLAGS) $(DWLDEVCFLAGS) $(CFLAGS)
-LDLIBS = `$(PKG_CONFIG) --libs $(PKGS)` $(WLR_LIBS) -lm $(LIBS)
+DWLCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(WLR_INCS) $(FX_INCS) $(DWLCPPFLAGS) $(DWLDEVCFLAGS) $(CFLAGS)
+LDLIBS = `$(PKG_CONFIG) --libs $(PKGS)` $(FX_LIBS) $(WLR_LIBS) -lm $(LIBS)
all: dwl
dwl: dwl.c client.h config.h ime.h util.h config.mk cursor-shape-v1-protocol.h \
diff --git a/client.h b/client.h
index ebe665b..d3f8091 100644
--- a/client.h
+++ b/client.h
@@ -300,9 +300,7 @@ client_send_close(Client *c)
static inline void
client_set_border_color(Client *c, const float color[static 4])
{
- int i;
- for (i = 0; i < 4; i++)
- wlr_scene_rect_set_color(c->border[i], color);
+ wlr_scene_rect_set_color(c->border, color);
}
static inline void
diff --git a/config.def.h b/config.def.h
index 4901e7a..71ffa98 100644
--- a/config.def.h
+++ b/config.def.h
@@ -13,6 +13,14 @@ static const float bordercolor[] = COLOR(0x444444ff);
static const float focuscolor[] = COLOR(0x005577ff);
static const float urgentcolor[] = COLOR(0xff0000ff);
static const float fullscreen_bg[] = {0.0f, 0.0f, 0.0f, 1.0f}; /* You can also use glsl colors */
+/* rounded corners, shadows and blur, see scenefx */
+static const int corner_radius = 8; /* radius of the corners of window contents, 0 disables them */
+static const int corner_radius_only_floating = 0; /* 1 means only floating windows have rounded corners */
+static const int shadow_sigma = 20; /* blur sigma and size of window shadows, 0 disables them */
+static const float shadow_color[] = COLOR(0x00000080);
+/* blur behind translucent windows. Set num_passes to 0 to disable it */
+static const struct blur_data blur_data = { .num_passes = 3, .radius = 5, .noise = 0.02f,
+ .brightness = 0.9f, .contrast = 0.9f, .saturation = 1.1f };
/* tagging - TAGCOUNT must be no greater than 31 */
#define TAGCOUNT (9)
diff --git a/config.mk b/config.mk
index 0da079c..9293d5e 100644
--- a/config.mk
+++ b/config.mk
@@ -24,6 +24,11 @@ WLR_LIBS = `$(PKG_CONFIG) --libs wlroots-0.20`
# -I$(PWD)/wlroots/0.20/include/wlroots-0.20
#WLR_LIBS = -Wl,-rpath,$(PWD)/wlroots/0.20/lib64 -L$(PWD)/wlroots/0.20/lib64 -lwlroots-0.20
+# scenefx's headers are included with -isystem to hide their -Wpedantic warnings,
+# and after WLR_INCS so they don't override an alternative wlroots installation.
+FX_INCS = -isystem `$(PKG_CONFIG) --variable=includedir scenefx-0.5`/scenefx-0.5
+FX_LIBS = `$(PKG_CONFIG) --libs scenefx-0.5`
+
XWAYLAND =
XLIBS =
# Uncomment to build XWayland support
diff --git a/dwl.c b/dwl.c
index 89e8caf..1a702c9 100644
--- a/dwl.c
+++ b/dwl.c
@@ -6,6 +6,9 @@
#include <limits.h>
#include <linux/input-event-codes.h>
#include <math.h>
+#include <scenefx/render/fx_renderer/fx_renderer.h>
+#include <scenefx/types/fx/clipped_region.h>
+#include <scenefx/types/wlr_scene.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -51,7 +54,6 @@
#include <wlr/types/wlr_primary_selection.h>
#include <wlr/types/wlr_primary_selection_v1.h>
#include <wlr/types/wlr_relative_pointer_v1.h>
-#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_screencopy_v1.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_server_decoration.h>
@@ -120,7 +122,9 @@ typedef struct {
Monitor *mon;
struct wlr_scene_tree *scene;
- struct wlr_scene_rect *border[4]; /* top, bottom, left, right */
+ struct wlr_scene_rect *border;
+ struct wlr_scene_shadow *shadow;
+ struct wlr_scene_blur *blur;
struct wlr_scene_tree *scene_surface;
struct wl_list link;
struct wl_list flink;
@@ -211,6 +215,7 @@ struct Monitor {
struct wlr_output *wlr_output;
struct wlr_scene_output *scene_output;
struct wlr_scene_rect *fullscreen_bg; /* See createmon() for info */
+ struct wlr_scene_optimized_blur *blur;
struct wl_listener frame;
struct wl_listener destroy;
struct wl_listener request_state;
@@ -284,6 +289,7 @@ static void commitlayersurfacenotify(struct wl_listener *listener, void *data);
static void commitlocksurface(struct wl_listener *listener, void *data);
static void commitnotify(struct wl_listener *listener, void *data);
static void commitpopup(struct wl_listener *listener, void *data);
+static int cornerradius(Client *c);
static void createdecoration(struct wl_listener *listener, void *data);
static void createidleinhibitor(struct wl_listener *listener, void *data);
static void createkeyboard(struct wlr_keyboard *keyboard);
@@ -346,6 +352,7 @@ static void requestdecorationmode(struct wl_listener *listener, void *data);
static void requeststartdrag(struct wl_listener *listener, void *data);
static void requestmonstate(struct wl_listener *listener, void *data);
static void resize(Client *c, struct wlr_box geo, int interact);
+static void roundbuffer(struct wlr_scene_buffer *buffer, int sx, int sy, void *data);
static void run(char *startup_cmd);
static void setcursor(struct wl_listener *listener, void *data);
static void setcursorshape(struct wl_listener *listener, void *data);
@@ -545,6 +552,9 @@ applygeom(Client *c)
{
struct wlr_box clip;
Client *p = client_get_parent(c);
+ int radius = cornerradius(c);
+ int outer = radius ? radius + (int)c->bw : 0;
+ int floating = c->isfloating || !c->mon->lt[c->mon->sellt]->arrange;
/* We move all floating clients to LyrTile while in floating layout
* to avoid "real" floating clients be always on top */
@@ -560,19 +570,37 @@ applygeom(Client *c)
wlr_scene_node_set_enabled(&c->scene_surface->node, 1);
}
- /* Update scene-graph, including borders */
+ /* Update scene-graph, including borders. The border is a rectangle with
+ * a hole for the content, so their rounded corners stay concentric. */
wlr_scene_node_set_position(&c->scene->node, c->geom.x, c->geom.y);
wlr_scene_node_set_position(&c->scene_surface->node, c->bw, c->bw);
- wlr_scene_rect_set_size(c->border[0], c->geom.width, c->bw);
- wlr_scene_rect_set_size(c->border[1], c->geom.width, c->bw);
- wlr_scene_rect_set_size(c->border[2], c->bw, c->geom.height - 2 * c->bw);
- wlr_scene_rect_set_size(c->border[3], c->bw, c->geom.height - 2 * c->bw);
- wlr_scene_node_set_position(&c->border[1]->node, 0, c->geom.height - c->bw);
- wlr_scene_node_set_position(&c->border[2]->node, 0, c->bw);
- wlr_scene_node_set_position(&c->border[3]->node, c->geom.width - c->bw, c->bw);
+ wlr_scene_rect_set_size(c->border, c->geom.width, c->geom.height);
+ wlr_scene_rect_set_corner_radius(c->border, outer);
+ wlr_scene_rect_set_clipped_region(c->border, (struct clipped_region){
+ .area = { c->bw, c->bw, c->geom.width - 2 * c->bw, c->geom.height - 2 * c->bw },
+ .corners = corner_radii_all(radius) });
+
+ wlr_scene_node_set_enabled(&c->shadow->node, shadow_sigma && !c->isfullscreen);
+ wlr_scene_shadow_set_size(c->shadow, c->geom.width + 2 * shadow_sigma,
+ c->geom.height + 2 * shadow_sigma);
+ wlr_scene_shadow_set_corner_radius(c->shadow, outer);
+ /* Don't draw the shadow behind translucent windows */
+ wlr_scene_shadow_set_clipped_region(c->shadow, (struct clipped_region){
+ .area = { shadow_sigma, shadow_sigma, c->geom.width, c->geom.height },
+ .corners = corner_radii_all(outer) });
+
+ /* The fullscreen background hides what's behind fullscreen windows */
+ wlr_scene_node_set_enabled(&c->blur->node, blur_data.num_passes && !c->isfullscreen);
+ wlr_scene_node_set_position(&c->blur->node, c->bw, c->bw);
+ wlr_scene_blur_set_size(c->blur, c->geom.width - 2 * c->bw, c->geom.height - 2 * c->bw);
+ wlr_scene_blur_set_corner_radius(c->blur, radius);
+ /* Only floating windows can be above other windows, tiled ones can use
+ * the cached blur of the background layer */
+ wlr_scene_blur_set_should_only_blur_bottom_layer(c->blur, !floating);
client_get_clip(c, &clip);
wlr_scene_subsurface_tree_set_clip(&c->scene_surface->node, &clip);
+ wlr_scene_node_for_each_buffer(&c->scene_surface->node, roundbuffer, c);
}
void
@@ -890,6 +918,7 @@ cleanupmon(struct wl_listener *listener, void *data)
closemon(m);
wlr_scene_node_destroy(&m->fullscreen_bg->node);
+ wlr_scene_node_destroy(&m->blur->node);
free(m);
}
@@ -982,6 +1011,10 @@ commitlayersurfacenotify(struct wl_listener *listener, void *data)
return;
}
+ /* The cached blur of the background layer must be redrawn when it changes */
+ if (scene_layer == layers[LyrBg] || l->scene->node.parent == layers[LyrBg])
+ wlr_scene_optimized_blur_mark_dirty(l->mon->blur);
+
if (layer_surface->current.committed == 0 && l->mapped == layer_surface->surface->mapped)
return;
l->mapped = layer_surface->surface->mapped;
@@ -1043,6 +1076,9 @@ commitnotify(struct wl_listener *listener, void *data)
transactioncommit(c, c->resize_serial && c->surface.xdg->current.configure_serial
>= c->resize_serial);
resize(c, c->geom, (c->isfloating && !c->isfullscreen));
+ /* Subsurfaces may have been added or resized */
+ if (client_surface(c)->mapped)
+ wlr_scene_node_for_each_buffer(&c->scene_surface->node, roundbuffer, c);
}
void
@@ -1075,6 +1111,15 @@ commitpopup(struct wl_listener *listener, void *data)
free(listener);
}
+int
+cornerradius(Client *c)
+{
+ if (c->isfullscreen || (corner_radius_only_floating && !c->isfloating
+ && c->mon && c->mon->lt[c->mon->sellt]->arrange))
+ return 0;
+ return corner_radius;
+}
+
void
createdecoration(struct wl_listener *listener, void *data)
{
@@ -1267,6 +1312,10 @@ createmon(struct wl_listener *listener, void *data)
m->fullscreen_bg = wlr_scene_rect_create(layers[LyrFS], 0, 0, fullscreen_bg);
wlr_scene_node_set_enabled(&m->fullscreen_bg->node, 0);
+ /* Caches the blur of everything below it, which tiled windows show */
+ m->blur = wlr_scene_optimized_blur_create(&scene->tree, 0, 0);
+ wlr_scene_node_place_above(&m->blur->node, &layers[LyrBg]->node);
+
/* Adds this to the output layout in the order it was configured.
*
* The output layout utility automatically adds a wl_output global to the
@@ -1720,7 +1769,7 @@ gpureset(struct wl_listener *listener, void *data)
struct wlr_renderer *old_drw = drw;
struct wlr_allocator *old_alloc = alloc;
struct Monitor *m;
- if (!(drw = wlr_renderer_autocreate(backend)))
+ if (!(drw = fx_renderer_create(backend)))
die("couldn't recreate renderer");
if (!(alloc = wlr_allocator_autocreate(backend, drw)))
@@ -1733,6 +1782,7 @@ gpureset(struct wl_listener *listener, void *data)
wl_list_for_each(m, &mons, link) {
wlr_output_init_render(m->wlr_output, alloc, drw);
+ wlr_scene_optimized_blur_mark_dirty(m->blur);
}
wlr_allocator_destroy(old_alloc);
@@ -1941,7 +1991,6 @@ mapnotify(struct wl_listener *listener, void *data)
Client *p = NULL;
Client *w, *c = wl_container_of(listener, c, map);
Monitor *m;
- int i;
struct wlr_ext_foreign_toplevel_handle_v1_state foreign_toplevel_state = {
.app_id = client_get_appid(c),
@@ -1969,11 +2018,14 @@ mapnotify(struct wl_listener *listener, void *data)
goto unset_fullscreen;
}
- for (i = 0; i < 4; i++) {
- c->border[i] = wlr_scene_rect_create(c->scene, 0, 0,
- c->isurgent ? urgentcolor : bordercolor);
- c->border[i]->node.data = c;
- }
+ c->border = wlr_scene_rect_create(c->scene, 0, 0,
+ c->isurgent ? urgentcolor : bordercolor);
+ c->border->node.data = c;
+ c->shadow = wlr_scene_shadow_create(c->scene, 0, 0, 0, shadow_sigma, shadow_color);
+ wlr_scene_node_set_position(&c->shadow->node, -shadow_sigma, -shadow_sigma);
+ wlr_scene_node_lower_to_bottom(&c->shadow->node);
+ c->blur = wlr_scene_blur_create(c->scene, 0, 0);
+ wlr_scene_node_lower_to_bottom(&c->blur->node);
/* Initialize client geometry with room for border */
client_set_tiled(c, WLR_EDGE_TOP | WLR_EDGE_BOTTOM | WLR_EDGE_LEFT |
@@ -2494,6 +2546,22 @@ resize(Client *c, struct wlr_box geo, int interact)
transactionschedule();
}
+void
+roundbuffer(struct wlr_scene_buffer *buffer, int sx, int sy, void *data)
+{
+ /* Round the corners of buffers which are at the corners of the window,
+ * including subsurfaces covering it */
+ Client *c = data;
+ int radius = cornerradius(c);
+ int left = sx == (int)c->bw, top = sy == (int)c->bw;
+ int right = sx + buffer->dst_width == c->current.width - (int)c->bw;
+ int bottom = sy + buffer->dst_height == c->current.height - (int)c->bw;
+
+ wlr_scene_buffer_set_corner_radii(buffer, corner_radii_new(
+ top && left ? radius : 0, top && right ? radius : 0,
+ bottom && right ? radius : 0, bottom && left ? radius : 0));
+}
+
void
run(char *startup_cmd)
{
@@ -2737,17 +2805,18 @@ setup(void)
/* Initialize the scene graph used to lay out windows */
scene = wlr_scene_create();
+ wlr_scene_set_blur_data(scene, blur_data.num_passes, (int)blur_data.radius, blur_data.noise,
+ blur_data.brightness, blur_data.contrast, blur_data.saturation);
root_bg = wlr_scene_rect_create(&scene->tree, 0, 0, rootcolor);
for (i = 0; i < NUM_LAYERS; i++)
layers[i] = wlr_scene_tree_create(&scene->tree);
drag_icon = wlr_scene_tree_create(&scene->tree);
wlr_scene_node_place_below(&drag_icon->node, &layers[LyrBlock]->node);
- /* Autocreates a renderer, either Pixman, GLES2 or Vulkan for us. The user
- * can also specify a renderer using the WLR_RENDERER env var.
+ /* Creates scenefx's GLES2 renderer, which is required to render its effects.
* The renderer is responsible for defining the various pixel formats it
* supports for shared memory, this configures that for clients. */
- if (!(drw = wlr_renderer_autocreate(backend)))
+ if (!(drw = fx_renderer_create(backend)))
die("couldn't create renderer");
wl_signal_add(&drw->events.lost, &gpu_reset);
@@ -2959,7 +3028,9 @@ snapshot(struct wlr_scene_node *node, struct wlr_scene_tree *parent, void *data)
* the buffers so that they can be shown after the client changes them */
struct wlr_scene_node *child, *copy;
struct wlr_scene_tree *tree;
- struct wlr_scene_rect *rect;
+ struct wlr_scene_rect *src_rect, *rect;
+ struct wlr_scene_shadow *src_shadow, *shadow;
+ struct wlr_scene_blur *src_blur, *blur;
struct wlr_scene_buffer *src, *buffer;
if (!node->enabled)
@@ -2973,8 +3044,29 @@ snapshot(struct wlr_scene_node *node, struct wlr_scene_tree *parent, void *data)
copy = &tree->node;
break;
case WLR_SCENE_NODE_RECT:
- rect = wlr_scene_rect_from_node(node);
- copy = &wlr_scene_rect_create(parent, rect->width, rect->height, rect->color)->node;
+ src_rect = wlr_scene_rect_from_node(node);
+ rect = wlr_scene_rect_create(parent, src_rect->width, src_rect->height, src_rect->color);
+ wlr_scene_rect_set_corner_radii(rect, src_rect->corners);
+ wlr_scene_rect_set_clipped_region(rect, src_rect->clipped_region);
+ copy = &rect->node;
+ break;
+ case WLR_SCENE_NODE_SHADOW:
+ src_shadow = wlr_scene_shadow_from_node(node);
+ shadow = wlr_scene_shadow_create(parent, src_shadow->width, src_shadow->height,
+ src_shadow->corner_radius, src_shadow->blur_sigma, src_shadow->color);
+ wlr_scene_shadow_set_clipped_region(shadow, src_shadow->clipped_region);
+ copy = &shadow->node;
+ break;
+ case WLR_SCENE_NODE_BLUR:
+ src_blur = wlr_scene_blur_from_node(node);
+ blur = wlr_scene_blur_create(parent, src_blur->width, src_blur->height);
+ wlr_scene_blur_set_corner_radii(blur, src_blur->corners);
+ wlr_scene_blur_set_clipped_region(blur, src_blur->clipped_region);
+ wlr_scene_blur_set_should_only_blur_bottom_layer(blur,
+ src_blur->should_only_blur_bottom_layer);
+ wlr_scene_blur_set_alpha(blur, src_blur->alpha);
+ wlr_scene_blur_set_strength(blur, src_blur->strength);
+ copy = &blur->node;
break;
case WLR_SCENE_NODE_BUFFER:
src = wlr_scene_buffer_from_node(node);
@@ -2989,6 +3081,7 @@ snapshot(struct wlr_scene_node *node, struct wlr_scene_tree *parent, void *data)
wlr_scene_buffer_set_color_range(buffer, src->color_range);
wlr_scene_buffer_set_source_box(buffer, &src->src_box);
wlr_scene_buffer_set_transform(buffer, src->transform);
+ wlr_scene_buffer_set_corner_radii(buffer, src->corners);
wlr_scene_buffer_set_buffer(buffer, src->buffer);
copy = &buffer->node;
break;
@@ -3222,6 +3315,8 @@ unmaplayersurfacenotify(struct wl_listener *listener, void *data)
wlr_scene_node_set_enabled(&l->scene->node, 0);
if (l->layer_surface->output && (l->mon = l->layer_surface->output->data))
arrangelayers(l->mon);
+ if (l->mon && l->scene->node.parent == layers[LyrBg])
+ wlr_scene_optimized_blur_mark_dirty(l->mon->blur);
if (l->layer_surface->surface == seat->keyboard_state.focused_surface)
focusclient(focustop(selmon), NULL, 1);
motionnotify(0, NULL, 0, 0, 0, 0);
@@ -3340,6 +3435,9 @@ updatemons(struct wl_listener *listener, void *data)
wlr_scene_node_set_position(&m->fullscreen_bg->node, m->m.x, m->m.y);
wlr_scene_rect_set_size(m->fullscreen_bg, m->m.width, m->m.height);
+ wlr_scene_node_set_position(&m->blur->node, m->m.x, m->m.y);
+ wlr_scene_optimized_blur_set_size(m->blur, m->m.width, m->m.height);
+
if (m->lock_surface) {
struct wlr_scene_tree *scene_tree = m->lock_surface->surface->data;
wlr_scene_node_set_position(&scene_tree->node, m->m.x, m->m.y);
--
2.56.0