aboutsummaryrefslogtreecommitdiff
path: root/daemons/spaced.c
diff options
context:
space:
mode:
authorTommaso Negri <me@tommasongr.com>2026-08-11 18:19:02 +0200
committerTommaso Negri <me@tommasongr.com>2026-08-11 18:19:02 +0200
commiteb51ef01a4bd8bd7bea61a0a5fd524f66cf276cd (patch)
treed750764614bf1e5125c4a09ba6c72084b6a5772e /daemons/spaced.c
parenta1d62dd085ac70fc90a05095220156897bdd8f82 (diff)
downloadbar-eb51ef01a4bd8bd7bea61a0a5fd524f66cf276cd.tar.gz
bar-eb51ef01a4bd8bd7bea61a0a5fd524f66cf276cd.zip
add additional daemons
Diffstat (limited to 'daemons/spaced.c')
-rw-r--r--daemons/spaced.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/daemons/spaced.c b/daemons/spaced.c
new file mode 100644
index 0000000..0729266
--- /dev/null
+++ b/daemons/spaced.c
@@ -0,0 +1,75 @@
+// spaced — desktop space daemon for bar
+//
+// Monitors the active desktop space and posts a Darwin notification
+// when the space changes. Uses a lightweight 0.5s poll of a CoreGraphics
+// private API.
+//
+// Compile:
+// cc -o bar-spaced daemons/spaced.c -framework CoreFoundation
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <dlfcn.h>
+#include <notify.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static volatile int keep_running = 1;
+
+// Private CoreGraphics API
+typedef CFStringRef (*CGSCopyActiveMenuBarDisplayIdentifierFunc)(void);
+
+static CGSCopyActiveMenuBarDisplayIdentifierFunc copyIdentifier = NULL;
+
+static void handle_signal(int sig) {
+ (void)sig;
+ keep_running = 0;
+}
+
+int main(void) {
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+
+ // Load the private CGS API at runtime
+ void *handle = dlopen(
+ "/System/Library/PrivateFrameworks/CoreGraphics.framework/CoreGraphics",
+ RTLD_LAZY | RTLD_LOCAL);
+
+ if (!handle) {
+ fprintf(stderr, "bar-spaced: could not load CoreGraphics framework\n");
+ return 1;
+ }
+
+ copyIdentifier = (CGSCopyActiveMenuBarDisplayIdentifierFunc)
+ dlsym(handle, "CGSCopyActiveMenuBarDisplayIdentifier");
+
+ if (!copyIdentifier) {
+ fprintf(stderr, "bar-spaced: could not find CGS API\n");
+ dlclose(handle);
+ return 1;
+ }
+
+ CFStringRef last = NULL;
+ unsigned int pollCount = 0;
+
+ while (keep_running) {
+ CFStringRef current = copyIdentifier();
+
+ if (current) {
+ if (!last || !CFEqual(current, last)) {
+ notify_post("bar.touch.<block-name>");
+ if (last) CFRelease(last);
+ last = CFStringCreateCopy(NULL, current);
+ }
+ CFRelease(current);
+ }
+
+ usleep(500000); // 0.5 second intervals
+ pollCount++;
+ }
+
+ if (last) CFRelease(last);
+ dlclose(handle);
+ return 0;
+}