aboutsummaryrefslogtreecommitdiff
path: root/daemons/devicesd.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/devicesd.c
parenta1d62dd085ac70fc90a05095220156897bdd8f82 (diff)
downloadbar-eb51ef01a4bd8bd7bea61a0a5fd524f66cf276cd.tar.gz
bar-eb51ef01a4bd8bd7bea61a0a5fd524f66cf276cd.zip
add additional daemons
Diffstat (limited to 'daemons/devicesd.c')
-rw-r--r--daemons/devicesd.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/daemons/devicesd.c b/daemons/devicesd.c
new file mode 100644
index 0000000..6b30930
--- /dev/null
+++ b/daemons/devicesd.c
@@ -0,0 +1,75 @@
+// devicesd — USB device hotplug daemon for bar
+//
+// Uses IOKit to listen for USB device connect/disconnect events and
+// posts a Darwin notification.
+//
+// Compile:
+// cc -o bar-devicesd daemons/devicesd.c \
+// -framework IOKit -framework CoreFoundation
+
+#include <IOKit/IOKitLib.h>
+#include <notify.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static volatile int keep_running = 1;
+static IONotificationPortRef notifyPort = NULL;
+
+static void handle_signal(int sig) {
+ (void)sig;
+ keep_running = 0;
+}
+
+static void device_event(void *refcon, io_iterator_t iterator) {
+ (void)refcon;
+ io_object_t obj;
+ while ((obj = IOIteratorNext(iterator))) {
+ IOObjectRelease(obj);
+ }
+ notify_post("bar.touch.<block-name>");
+}
+
+int main(void) {
+ signal(SIGINT, handle_signal);
+ signal(SIGTERM, handle_signal);
+
+ notifyPort = IONotificationPortCreate(kIOMasterPortDefault);
+ CFRunLoopSourceRef source = IONotificationPortGetRunLoopSource(notifyPort);
+ CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
+
+ // Watch for USB devices
+ CFMutableDictionaryRef matching = IOServiceMatching("IOUSBDevice");
+ if (matching) {
+ io_iterator_t iter;
+
+ IOServiceAddMatchingNotification(
+ notifyPort, kIOFirstMatchNotification, matching,
+ device_event, NULL, &iter);
+ device_event(NULL, iter); // drain initial set
+
+ IOServiceAddMatchingNotification(
+ notifyPort, kIOTerminatedNotification, matching,
+ device_event, NULL, &iter);
+ }
+
+ // Also watch for Bluetooth devices
+ CFMutableDictionaryRef btMatching = IOServiceMatching("IOBluetoothHCIController");
+ if (btMatching) {
+ io_iterator_t iter;
+ IOServiceAddMatchingNotification(
+ notifyPort, kIOFirstMatchNotification, btMatching,
+ device_event, NULL, &iter);
+ device_event(NULL, iter);
+ }
+
+ while (keep_running) {
+ pause();
+ }
+
+ if (notifyPort) {
+ IONotificationPortDestroy(notifyPort);
+ }
+ return 0;
+}