Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

files: follow the theme through inotify, no idle wakeups

oxydizer7dec20ab9a48
diff --git a/src/files/settings.zig b/src/files/settings.zig
index faaba6b..210da67 100644
--- a/src/files/settings.zig
+++ b/src/files/settings.zig
@@ -1,6 +1,8 @@
//! Follow the compositor's persisted theme without blocking the UI thread.
-//! Stat once per second; read only after changes, including atomic replacement.
-//! Shared by the Files, Images and PDF clients.
+//! `watch` arms inotify on the files' directories so followers sleep until a
+//! change; `poll` stats at most once per second and reads only after changes,
+//! including atomic replacement. Shared by the Files, Images, PDF and System
+//! Monitor clients.
const std = @import("std");
const c = @import("c.zig").api;
const anim = @import("ui").anim;
@@ -101,6 +103,28 @@ pub const Settings = struct {
if (self.theme_path) |p| a.free(p);
}
+ /// Adds inotify watches on the directories holding the config and theme
+ /// files, so atomic saves are seen too. A missing directory is replaced by
+ /// its nearest existing parent: call again after events, so a created
+ /// directory gets its own watch (re-adding a watch is harmless).
+ pub fn watch(self: *const Settings, fd: c_int) void {
+ for ([_]?[:0]const u8{ self.config_path, self.theme_path }) |maybe_path| {
+ var parent = std.fs.path.dirname(maybe_path orelse continue) orelse continue;
+ while (true) {
+ var buf: [4096]u8 = undefined;
+ const path = std.fmt.bufPrintZ(&buf, "{s}", .{parent}) catch break;
+ if (c.inotify_add_watch(fd, path, c.IN_CLOSE_WRITE | c.IN_MOVED_TO | c.IN_CREATE | c.IN_DELETE | c.IN_DELETE_SELF | c.IN_MOVE_SELF) >= 0) break;
+ parent = std.fs.path.dirname(parent) orelse break;
+ }
+ }
+ }
+
+ /// `poll` without its once-a-second pacing, for after `watch` saw events.
+ pub fn reload(self: *Settings, a: std.mem.Allocator, io: std.Io) ?ThemeUpdate {
+ self.last_second = -1;
+ return self.poll(a, io);
+ }
+
/// The merged `[theme]` of the config and `REDIWM_THEME` files, when
/// either changed. The caller must apply or deinit the returned update.
pub fn poll(self: *Settings, a: std.mem.Allocator, io: std.Io) ?ThemeUpdate {
diff --git a/src/files/worker.zig b/src/files/worker.zig
index 18c1af2..05f34bd 100644
--- a/src/files/worker.zig
+++ b/src/files/worker.zig
@@ -244,6 +244,14 @@ pub const Worker = struct {
defer if (inotify_fd >= 0) {
_ = c.close(inotify_fd);
};
+ // Its own descriptor: the browsed folder can be the config directory,
+ // and leaving it must not drop the theme watch.
+ const theme_fd = c.inotify_init1(c.IN_NONBLOCK | c.IN_CLOEXEC);
+ defer if (theme_fd >= 0) {
+ _ = c.close(theme_fd);
+ };
+ if (theme_fd >= 0) settings.watch(theme_fd);
+ var theme_changed = true;
var watched_dir: ?[]u8 = null;
defer if (watched_dir) |d| self.allocator.free(d);
@@ -273,7 +281,9 @@ pub const Worker = struct {
var opened_storage = opened;
opened_storage.deinit(self.allocator);
if (self.stop.load(.acquire)) break;
- if (settings.poll(self.allocator, self.io)) |t| {
+ const update = if (theme_fd < 0) settings.poll(self.allocator, self.io) else if (theme_changed) settings.reload(self.allocator, self.io) else null;
+ theme_changed = false;
+ if (update) |t| {
_ = c.pthread_mutex_lock(&self.mutex);
self.animation_settings = settings.animations;
if (self.theme) |old| old.deinit();
@@ -358,13 +368,20 @@ pub const Worker = struct {
}
if (inotify_fd >= 0) {
- var fds = [2]c.struct_pollfd{
+ var fds = [3]c.struct_pollfd{
.{ .fd = self.wake, .events = c.POLLIN, .revents = 0 },
.{ .fd = inotify_fd, .events = c.POLLIN, .revents = 0 },
+ .{ .fd = theme_fd, .events = c.POLLIN, .revents = 0 },
};
- // Requests and directory changes wake the poll; the timeout
- // only paces Settings.poll, which stats the theme once a second.
- const poll_res = c.poll(&fds, fds.len, 1000);
+ // Requests, directory and theme changes wake the poll. Without
+ // a theme watch the timeout paces Settings.poll's once-a-second stat.
+ const poll_res = c.poll(&fds, fds.len, if (theme_fd >= 0) -1 else 1000);
+ if (poll_res > 0 and (fds[2].revents & c.POLLIN != 0)) {
+ var buf: [4096]u8 align(@alignOf(c.struct_inotify_event)) = undefined;
+ while (c.read(theme_fd, &buf, buf.len) > 0) {}
+ settings.watch(theme_fd);
+ theme_changed = true;
+ }
if (poll_res > 0 and (fds[0].revents & c.POLLIN != 0)) {
var count: u64 = 0;
_ = c.read(self.wake, &count, @sizeOf(u64));
diff --git a/src/system_monitor/settings_watch.zig b/src/system_monitor/settings_watch.zig
index ee27d09..9350291 100644
--- a/src/system_monitor/settings_watch.zig
+++ b/src/system_monitor/settings_watch.zig
@@ -22,19 +22,10 @@ pub const Watch = struct {
_ = c.close(self.fd);
}
fn arm(self: *Watch) void {
- for ([_]?[:0]const u8{ self.settings.config_path, self.settings.theme_path }) |maybe_path| {
- var parent = std.fs.path.dirname(maybe_path orelse continue) orelse continue;
- while (true) {
- var buf: [4096]u8 = undefined;
- const path = std.fmt.bufPrintZ(&buf, "{s}", .{parent}) catch break;
- if (c.inotify_add_watch(self.fd, path, c.IN_CLOSE_WRITE | c.IN_MOVED_TO | c.IN_CREATE | c.IN_DELETE | c.IN_DELETE_SELF | c.IN_MOVE_SELF) >= 0) break;
- parent = std.fs.path.dirname(parent) orelse break;
- }
- }
+ self.settings.watch(self.fd);
}
fn apply(self: *Watch) bool {
- self.settings.last_second = -1;
- if (self.settings.poll(a, self.io)) |update| {
+ if (self.settings.reload(a, self.io)) |update| {
update.apply();
@import("ui").anim.applySettings(self.settings.animations);
return true;
diff --git a/tests/files_thumbnails.py b/tests/files_thumbnails.py
index de8a9ee..0ae82f7 100644
--- a/tests/files_thumbnails.py
+++ b/tests/files_thumbnails.py
@@ -259,7 +259,8 @@ def run():
before = context_switches(files.pid)
time.sleep(2.0)
switches = context_switches(files.pid) - before
- assert switches <= 4, f"idle Files context-switched {switches} times in 2 s"
+ # The theme is followed through inotify, so nothing ticks.
+ assert switches <= 1, f"idle Files context-switched {switches} times in 2 s"
print(f"Idle Files made {switches} context switches in 2 s.")
stop(files)
@@ -299,7 +300,8 @@ def run():
before = context_switches(files.pid)
time.sleep(2.0)
switches = context_switches(files.pid) - before
- assert switches <= 4, f"idle Files context-switched {switches} times in 2 s"
+ # The theme is followed through inotify, so nothing ticks.
+ assert switches <= 1, f"idle Files context-switched {switches} times in 2 s"
stop(files)
except Exception:
for log in tmp.glob("*.log"):