aboutsummaryrefslogtreecommitdiff
path: root/daemons/brightnessd.c
blob: 333dbf4cc29c3730b6a90ec602804168bf98bdbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// brightnessd — display brightness daemon for bar
//
// Uses IOKit to listen for display brightness changes and posts
// a Darwin notification.
//
// Compile:
//   cc -o bar-brightnessd daemons/brightnessd.c \
//       -framework IOKit -framework CoreFoundation

#include <IOKit/IOKitLib.h>
#include <IOKit/graphics/IOGraphicsLib.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 brightness_changed(void *refcon, io_iterator_t iterator) {
	(void)refcon;
	// Drain the iterator
	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 brightness-related display changes
	CFMutableDictionaryRef matching = IOServiceMatching("AppleBacklightDisplay");
	if (!matching) {
		// Fallback: try Intel backlight
		matching = IOServiceMatching("AppleIntelPanel");
	}
	if (!matching) {
		matching = IOServiceMatching("IODisplay");
	}

	if (matching) {
		io_iterator_t iter;
		IOServiceAddMatchingNotification(
			notifyPort, kIOFirstMatchNotification, matching,
			brightness_changed, NULL, &iter);
		brightness_changed(NULL, iter); // drain initial

		// Also watch for termination to catch display disconnect
		IOServiceAddMatchingNotification(
			notifyPort, kIOTerminatedNotification, matching,
			brightness_changed, NULL, &iter);
	}

	while (keep_running) {
		pause();
	}

	if (notifyPort) {
		IONotificationPortDestroy(notifyPort);
	}
	return 0;
}