From 059a0c8988967e23807e4f78920816f679dbc068 Mon Sep 17 00:00:00 2001
From: wochap <gean.marroquin@gmail.com>
Date: Thu, 2 Jan 2025 19:06:36 -0500
Subject: [PATCH] implement scenefx

---
 Makefile     |   2 +-
 client.h     |  14 ++-
 config.def.h |  30 ++++-
 dwl.c        | 319 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 355 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index 3358bae..20b15bb 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@ DWLDEVCFLAGS = -g -pedantic -Wall -Wextra -Wdeclaration-after-statement \
 	-Wfloat-conversion
 
 # CFLAGS / LDFLAGS
-PKGS      = wlroots-0.18 wayland-server xkbcommon libinput $(XLIBS)
+PKGS      = scenefx wlroots-0.18 wayland-server xkbcommon libinput $(XLIBS)
 DWLCFLAGS = `$(PKG_CONFIG) --cflags $(PKGS)` $(DWLCPPFLAGS) $(DWLDEVCFLAGS) $(CFLAGS)
 LDLIBS    = `$(PKG_CONFIG) --libs $(PKGS)` -lm $(LIBS)
 
diff --git a/client.h b/client.h
index 42f225f..e3cef30 100644
--- a/client.h
+++ b/client.h
@@ -136,10 +136,10 @@ client_get_clip(Client *c, struct wlr_box *clip)
 {
 	struct wlr_box xdg_geom = {0};
 	*clip = (struct wlr_box){
-		.x = 0,
-		.y = 0,
-		.width = c->geom.width - c->bw,
-		.height = c->geom.height - c->bw,
+		.x = c->bw,
+		.y = c->bw,
+		.width = c->geom.width - c->bw * 2,
+		.height = c->geom.height - c->bw * 2,
 	};
 
 #ifdef XWAYLAND
@@ -328,6 +328,12 @@ static inline void
 client_set_border_color(Client *c, const float color[static 4])
 {
 	int i;
+
+	if (corner_radius > 0) {
+		wlr_scene_rect_set_color(c->round_border, color);
+		return;
+	}
+
 	for (i = 0; i < 4; i++)
 		wlr_scene_rect_set_color(c->border[i], color);
 }
diff --git a/config.def.h b/config.def.h
index 22d2171..d4e85c1 100644
--- a/config.def.h
+++ b/config.def.h
@@ -12,7 +12,35 @@ static const float bordercolor[]           = COLOR(0x444444ff);
 static const float focuscolor[]            = COLOR(0x005577ff);
 static const float urgentcolor[]           = COLOR(0xff0000ff);
 /* This conforms to the xdg-protocol. Set the alpha to zero to restore the old behavior */
-static const float fullscreen_bg[]         = {0.1f, 0.1f, 0.1f, 1.0f}; /* You can also use glsl colors */
+static const float fullscreen_bg[]         = {0.1f, 0.1f, 0.1f, 0.0f}; /* You can also use glsl colors */
+
+static const int opacity = 0; /* flag to enable opacity */
+static const float opacity_inactive = 0.5;
+static const float opacity_active = 1.0;
+
+static const int shadow = 1; /* flag to enable shadow */
+static const int shadow_only_floating = 0; /* only apply shadow to floating windows */
+static const float shadow_color[4] = COLOR(0x0000FFff);
+static const float shadow_color_focus[4] = COLOR(0xFF0000ff);
+static const int shadow_blur_sigma = 20;
+static const int shadow_blur_sigma_focus = 40;
+static const char *const shadow_ignore_list[] = { NULL }; /* list of app-id to ignore */
+
+static const int corner_radius = 8; /* 0 disables corner_radius */
+static const int corner_radius_inner = 8; /* 0 disables corner_radius */
+static const int corner_radius_only_floating = 0; /* only apply corner_radius and corner_radius_inner to floating windows */
+
+static const int blur = 1; /* flag to enable blur */
+static const int blur_optimized = 1;
+static const int blur_ignore_transparent = 1;
+static const struct blur_data blur_data = {
+	.radius = 5,
+	.num_passes = 3,
+	.noise = (float)0.02,
+	.brightness = (float)0.9,
+	.contrast = (float)0.9,
+	.saturation = (float)1.1,
+};
 
 /* tagging - TAGCOUNT must be no greater than 31 */
 #define TAGCOUNT (9)
diff --git a/dwl.c b/dwl.c
index 5bf995e..3c551dc 100644
--- a/dwl.c
+++ b/dwl.c
@@ -10,8 +10,13 @@
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <time.h>
+#include <scenefx/render/fx_renderer/fx_renderer.h>
+#include <scenefx/types/fx/blur_data.h>
+#include <scenefx/types/fx/corner_location.h>
+#include <scenefx/types/wlr_scene.h>
 #include <unistd.h>
 #include <wayland-server-core.h>
+#include <wayland-util.h>
 #include <wlr/backend.h>
 #include <wlr/backend/libinput.h>
 #include <wlr/render/allocator.h>
@@ -43,7 +48,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>
@@ -141,6 +145,12 @@ typedef struct {
 	uint32_t tags;
 	int isfloating, isurgent, isfullscreen;
 	uint32_t resize; /* configure serial of a pending resize */
+
+	float opacity;
+	int corner_radius;
+	struct wlr_scene_shadow *shadow;
+	int has_shadow_enabled;
+	struct wlr_scene_rect *round_border;
 } Client;
 
 typedef struct {
@@ -355,6 +365,11 @@ static Monitor *xytomon(double x, double y);
 static void xytonode(double x, double y, struct wlr_surface **psurface,
 		Client **pc, LayerSurface **pl, double *nx, double *ny);
 static void zoom(const Arg *arg);
+static void iter_xdg_scene_buffers(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data);
+static void iter_xdg_scene_buffers_opacity(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data);
+static void iter_xdg_scene_buffers_corner_radius(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data);
+static void output_configure_scene(struct wlr_scene_node *node, Client *c);
+static int in_shadow_ignore_list(const char *str);
 
 /* variables */
 static const char broken[] = "broken";
@@ -366,6 +381,8 @@ static struct wl_event_loop *event_loop;
 static struct wlr_backend *backend;
 static struct wlr_scene *scene;
 static struct wlr_scene_tree *layers[NUM_LAYERS];
+/* TODO: implement wlr_scene_optimized_blur */
+/* static struct wlr_scene_optimized_blur *blur_layer; */
 static struct wlr_scene_tree *drag_icon;
 /* Map from ZWLR_LAYER_SHELL_* constants to Lyr* enum */
 static const int layermap[] = { LyrBg, LyrBottom, LyrTop, LyrOverlay };
@@ -413,6 +430,8 @@ static struct wlr_box sgeom;
 static struct wl_list mons;
 static Monitor *selmon;
 
+static float transparent[4] = {0.1f, 0.1f, 0.1f, 0.0f};
+
 #ifdef XWAYLAND
 static void activatex11(struct wl_listener *listener, void *data);
 static void associatex11(struct wl_listener *listener, void *data);
@@ -1061,6 +1080,9 @@ createnotify(struct wl_listener *listener, void *data)
 	c->surface.xdg = toplevel->base;
 	c->bw = borderpx;
 
+	c->opacity = opacity;
+	c->corner_radius = corner_radius;
+
 	LISTEN(&toplevel->base->surface->events.commit, &c->commit, commitnotify);
 	LISTEN(&toplevel->base->surface->events.map, &c->map, mapnotify);
 	LISTEN(&toplevel->base->surface->events.unmap, &c->unmap, unmapnotify);
@@ -1369,8 +1391,20 @@ focusclient(Client *c, int lift)
 
 		/* Don't change border color if there is an exclusive focus or we are
 		 * handling a drag operation */
-		if (!exclusive_focus && !seat->drag)
+		if (!exclusive_focus && !seat->drag) {
 			client_set_border_color(c, focuscolor);
+
+			if (shadow) {
+				wlr_scene_shadow_set_blur_sigma(c->shadow, shadow_blur_sigma_focus);
+				if (c->has_shadow_enabled) {
+					wlr_scene_shadow_set_color(c->shadow, shadow_color_focus);
+				}
+			}
+			if (opacity) {
+				c->opacity = opacity_active;
+				wlr_scene_node_for_each_buffer(&c->scene_surface->node, iter_xdg_scene_buffers_opacity, c);
+			}
+		}
 	}
 
 	/* Deactivate old client if focus is changing */
@@ -1389,6 +1423,17 @@ focusclient(Client *c, int lift)
 		} else if (old_c && !client_is_unmanaged(old_c) && (!c || !client_wants_focus(c))) {
 			client_set_border_color(old_c, bordercolor);
 
+			if (shadow) {
+				wlr_scene_shadow_set_blur_sigma(old_c->shadow, shadow_blur_sigma);
+				if (old_c->has_shadow_enabled) {
+					wlr_scene_shadow_set_color(old_c->shadow, shadow_color);
+				}
+			}
+			if (opacity) {
+				old_c->opacity = opacity_inactive;
+				wlr_scene_node_for_each_buffer(&old_c->scene_surface->node, iter_xdg_scene_buffers_opacity, old_c);
+			}
+
 			client_activate_surface(old, 0);
 		}
 	}
@@ -1718,6 +1763,21 @@ mapnotify(struct wl_listener *listener, void *data)
 		c->border[i]->node.data = c;
 	}
 
+	wlr_scene_node_for_each_buffer(&c->scene_surface->node, iter_xdg_scene_buffers, c);
+
+	if (corner_radius > 0) {
+		c->round_border = wlr_scene_rect_create(c->scene, 0, 0, c->isurgent ? urgentcolor : bordercolor);
+		c->round_border->node.data = c;
+		/* Lower the border below the XDG scene tree */
+		wlr_scene_node_lower_to_bottom(&c->round_border->node);
+	}
+
+	if (shadow) {
+		c->shadow = wlr_scene_shadow_create(c->scene, 0, 0, c->corner_radius, shadow_blur_sigma, shadow_color);
+		/* Lower the shadow below the border */
+		wlr_scene_node_lower_to_bottom(&c->shadow->node);
+	}
+
 	/* Initialize client geometry with room for border */
 	client_set_tiled(c, WLR_EDGE_TOP | WLR_EDGE_BOTTOM | WLR_EDGE_LEFT | WLR_EDGE_RIGHT);
 	c->geom.width += 2 * c->bw;
@@ -1739,6 +1799,25 @@ mapnotify(struct wl_listener *listener, void *data)
 	}
 	printstatus();
 
+	if (corner_radius > 0) {
+		int radius = c->corner_radius + c->bw;
+		if ((corner_radius_only_floating && !c->isfloating) || c->isfullscreen) {
+			radius = 0;
+		}
+		wlr_scene_rect_set_corner_radius(c->round_border, radius);
+	}
+
+	if (shadow) {
+		const float *color = focustop(c->mon) == c ? shadow_color_focus : shadow_color;
+		int has_shadow_enabled = 1;
+		if ((shadow_only_floating && !c->isfloating) || in_shadow_ignore_list(client_get_appid(c)) || c->isfullscreen) {
+			color = transparent;
+			has_shadow_enabled = 0;
+		}
+		wlr_scene_shadow_set_color(c->shadow, color);
+		c->has_shadow_enabled = has_shadow_enabled;
+	}
+
 unset_fullscreen:
 	m = c->mon ? c->mon : xytomon(c->geom.x, c->geom.y);
 	wl_list_for_each(w, &clients, link) {
@@ -2113,6 +2192,8 @@ rendermon(struct wl_listener *listener, void *data)
 			goto skip;
 	}
 
+	output_configure_scene(&m->scene_output->scene->tree.node, NULL);
+
 	/*
 	 * HACK: The "correct" way to set the gamma is to commit it together with
 	 * the rest of the state in one go, but to do that we would need to rewrite
@@ -2181,6 +2262,8 @@ resize(Client *c, struct wlr_box geo, int interact)
 {
 	struct wlr_box *bbox;
 	struct wlr_box clip;
+	int blur_sigma;
+	int i;
 
 	if (!c->mon || !c->scene)
 		return;
@@ -2207,6 +2290,20 @@ resize(Client *c, struct wlr_box geo, int interact)
 			c->geom.height - 2 * c->bw);
 	client_get_clip(c, &clip);
 	wlr_scene_subsurface_tree_set_clip(&c->scene_surface->node, &clip);
+
+	if (corner_radius > 0) {
+		wlr_scene_node_set_position(&c->round_border->node, 0, 0);
+		wlr_scene_rect_set_size(c->round_border, c->geom.width, c->geom.height);
+		/* hide original border */
+		for (i = 0; i < 4; i++)
+			wlr_scene_rect_set_color(c->border[i], transparent);
+	}
+
+	if (shadow) {
+		blur_sigma = (int)round(c->shadow->blur_sigma);
+		wlr_scene_node_set_position(&c->shadow->node, -blur_sigma, -blur_sigma);
+		wlr_scene_shadow_set_size(c->shadow, c->geom.width + (blur_sigma) * 2, c->geom.height + (blur_sigma) * 2);
+	}
 }
 
 void
@@ -2307,6 +2404,29 @@ setfloating(Client *c, int floating)
 {
 	Client *p = client_get_parent(c);
 	c->isfloating = floating;
+
+	if (corner_radius > 0 && c->round_border != NULL) {
+		int radius = c->corner_radius + c->bw;
+		if ((corner_radius_only_floating && !c->isfloating) || c->isfullscreen) {
+			radius = 0;
+		}
+		wlr_scene_rect_set_corner_radius(c->round_border, radius);
+	}
+	if (corner_radius_inner > 0 && c->round_border != NULL) {
+		wlr_scene_node_for_each_buffer(&c->scene_surface->node, iter_xdg_scene_buffers_corner_radius, c);
+	}
+
+	if (shadow && c->shadow != NULL) {
+		const float *color = focustop(c->mon) == c ? shadow_color_focus : shadow_color;
+		int has_shadow_enabled = 1;
+		if ((shadow_only_floating && !c->isfloating) || in_shadow_ignore_list(client_get_appid(c)) || c->isfullscreen) {
+			color = transparent;
+			has_shadow_enabled = 0;
+		}
+		wlr_scene_shadow_set_color(c->shadow, color);
+		c->has_shadow_enabled = has_shadow_enabled;
+	}
+
 	/* If in floating layout do not change the client's layer */
 	if (!c->mon || !client_surface(c)->mapped || !c->mon->lt[c->mon->sellt]->arrange)
 		return;
@@ -2336,6 +2456,29 @@ setfullscreen(Client *c, int fullscreen)
 		 * client positions are set by the user and cannot be recalculated */
 		resize(c, c->prev, 0);
 	}
+
+	if (corner_radius > 0 && c->round_border != NULL) {
+		int radius = c->corner_radius + c->bw;
+		if ((corner_radius_only_floating && !c->isfloating) || c->isfullscreen) {
+			radius = 0;
+		}
+		wlr_scene_rect_set_corner_radius(c->round_border, radius);
+	}
+	if (corner_radius_inner > 0 && c->round_border != NULL) {
+		wlr_scene_node_for_each_buffer(&c->scene_surface->node, iter_xdg_scene_buffers_corner_radius, c);
+	}
+
+	if (shadow && c->shadow != NULL) {
+		const float *color = focustop(c->mon) == c ? shadow_color_focus : shadow_color;
+		int has_shadow_enabled = 1;
+		if ((shadow_only_floating && !c->isfloating) || in_shadow_ignore_list(client_get_appid(c)) || c->isfullscreen) {
+			color = transparent;
+			has_shadow_enabled = 0;
+		}
+		wlr_scene_shadow_set_color(c->shadow, color);
+		c->has_shadow_enabled = has_shadow_enabled;
+	}
+
 	arrange(c->mon);
 	printstatus();
 }
@@ -2457,11 +2600,17 @@ setup(void)
 	drag_icon = wlr_scene_tree_create(&scene->tree);
 	wlr_scene_node_place_below(&drag_icon->node, &layers[LyrBlock]->node);
 
+	if (blur) {
+		/* TODO: implement wlr_scene_optimized_blur_create */
+		/* blur_layer = wlr_scene_optimized_blur_create(&scene->tree, 0, 0); */
+		wlr_scene_set_blur_data(scene, blur_data);
+	}
+
 	/* Autocreates a renderer, either Pixman, GLES2 or Vulkan for us. The user
 	 * can also specify a renderer using the WLR_RENDERER env var.
 	 * 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");
 	LISTEN_STATIC(&drw->events.lost, gpureset);
 
@@ -2871,6 +3020,14 @@ 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);
 
+		/*
+		 * Sets the blur node size to the outputs size. Assumes there's only one
+		 * output. More advanced compositors should either implement per output blur
+		 * nodes or set it to the size of all outputs.
+		 */
+		/* TODO: implement wlr_scene_optimized_blur_set_size */
+		/* wlr_scene_optimized_blur_set_size(blur_layer, m->wlr_output->width, m->wlr_output->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);
@@ -2940,8 +3097,16 @@ urgent(struct wl_listener *listener, void *data)
 	c->isurgent = 1;
 	printstatus();
 
-	if (client_surface(c)->mapped)
+	if (client_surface(c)->mapped) {
 		client_set_border_color(c, urgentcolor);
+
+		if (shadow && c->shadow != NULL) {
+			wlr_scene_shadow_set_blur_sigma(c->shadow, shadow_blur_sigma_focus);
+			if (c->has_shadow_enabled) {
+				wlr_scene_shadow_set_color(c->shadow, shadow_color_focus);
+			}
+		}
+	}
 }
 
 void
@@ -3053,6 +3218,152 @@ zoom(const Arg *arg)
 	arrange(selmon);
 }
 
+void
+iter_xdg_scene_buffers(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data)
+{
+	Client *c = user_data;
+	struct wlr_scene_surface * scene_surface = wlr_scene_surface_try_from_buffer(buffer);
+	struct wlr_xdg_surface *xdg_surface;
+
+	if (!scene_surface) {
+		return;
+	}
+
+	xdg_surface = wlr_xdg_surface_try_from_wlr_surface(scene_surface->surface);
+
+	if (c && xdg_surface && xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
+		/* TODO: Be able to set whole decoration_data instead of calling */
+		/* each individually? */
+		if (opacity) {
+			wlr_scene_buffer_set_opacity(buffer, c->opacity);
+		}
+
+		if (!wlr_subsurface_try_from_wlr_surface(xdg_surface->surface)) {
+			if (corner_radius_inner > 0) {
+				int radius = corner_radius_inner;
+				if ((corner_radius_only_floating && !c->isfloating) || c->isfullscreen) {
+					radius = 0;
+				}
+				wlr_scene_buffer_set_corner_radius(buffer, radius, CORNER_LOCATION_ALL);
+			}
+
+			if (blur) {
+				wlr_scene_buffer_set_backdrop_blur(buffer, 1);
+				wlr_scene_buffer_set_backdrop_blur_optimized(buffer, blur_optimized);
+				wlr_scene_buffer_set_backdrop_blur_ignore_transparent(buffer, blur_ignore_transparent);
+			}
+		}
+	}
+}
+
+void
+iter_xdg_scene_buffers_opacity(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data)
+{
+	Client *c = user_data;
+	struct wlr_scene_surface * scene_surface = wlr_scene_surface_try_from_buffer(buffer);
+	struct wlr_xdg_surface *xdg_surface;
+
+	if (!scene_surface) {
+		return;
+	}
+
+	xdg_surface = wlr_xdg_surface_try_from_wlr_surface(scene_surface->surface);
+
+	if (c && xdg_surface && xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
+		/* TODO: Be able to set whole decoration_data instead of calling */
+		/* each individually? */
+		if (opacity) {
+			wlr_scene_buffer_set_opacity(buffer, c->opacity);
+		}
+	}
+}
+
+void
+iter_xdg_scene_buffers_corner_radius(struct wlr_scene_buffer *buffer, int sx, int sy, void *user_data)
+{
+	Client *c = user_data;
+	struct wlr_scene_surface * scene_surface = wlr_scene_surface_try_from_buffer(buffer);
+	struct wlr_xdg_surface *xdg_surface;
+
+	if (!scene_surface) {
+		return;
+	}
+
+	xdg_surface = wlr_xdg_surface_try_from_wlr_surface(scene_surface->surface);
+
+	if (c && xdg_surface && xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
+		/* TODO: Be able to set whole decoration_data instead of calling */
+		/* each individually? */
+		if (corner_radius_inner > 0) {
+			int radius = corner_radius_inner;
+			if ((corner_radius_only_floating && !c->isfloating) || c->isfullscreen) {
+				radius = 0;
+			}
+			wlr_scene_buffer_set_corner_radius(buffer, radius, CORNER_LOCATION_ALL);
+		}
+	}
+}
+
+void
+output_configure_scene(struct wlr_scene_node *node, Client *c)
+{
+	Client *_c;
+	struct wlr_xdg_surface *xdg_surface;
+	struct wlr_scene_node *_node;
+
+	if (!node->enabled) {
+		return;
+	}
+
+	_c = node->data;
+	if (_c) {
+		c = _c;
+	}
+
+	if (node->type == WLR_SCENE_NODE_BUFFER) {
+		struct wlr_scene_buffer *buffer = wlr_scene_buffer_from_node(node);
+
+		struct wlr_scene_surface *scene_surface = wlr_scene_surface_try_from_buffer(buffer);
+		if (!scene_surface) {
+			return;
+		}
+
+		xdg_surface = wlr_xdg_surface_try_from_wlr_surface(scene_surface->surface);
+
+		if (c && xdg_surface && xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
+			if (opacity) {
+				wlr_scene_buffer_set_opacity(buffer, c->opacity);
+			}
+
+			if (!wlr_subsurface_try_from_wlr_surface(xdg_surface->surface)) {
+				if (corner_radius_inner > 0) {
+					int radius = corner_radius_inner;
+					if ((corner_radius_only_floating && !c->isfloating) || c->isfullscreen) {
+						radius = 0;
+					}
+					wlr_scene_buffer_set_corner_radius(buffer, radius, CORNER_LOCATION_ALL);
+				}
+			}
+		}
+	} else if (node->type == WLR_SCENE_NODE_TREE) {
+		struct wlr_scene_tree *tree = wl_container_of(node, tree, node);
+		wl_list_for_each(_node, &tree->children, link) {
+			output_configure_scene(_node, c);
+		}
+	}
+}
+
+int
+in_shadow_ignore_list(const char *str)
+{
+	for (int i = 0; shadow_ignore_list[i] != NULL; i++) {
+		if (strcmp(shadow_ignore_list[i], str) == 0) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
 #ifdef XWAYLAND
 void
 activatex11(struct wl_listener *listener, void *data)