aboutsummaryrefslogtreecommitdiff
path: root/daemons/volume/volumed.c
blob: 3681b60900b7d65246b11c259808dfc8136621f7 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// bar-volumed — volume daemon for bar (no permissions needed)
//
// Uses CoreAudio to listen for system volume/mute changes instead of
// a CGEvent tap, so no Accessibility permission is required.
//
// When the volume changes, it posts a Darwin notification that tells
// bar to refresh the "volume" block.
//
// ── Setup ──
//
// 1. Add a volume block to Config (no interval needed):
//      Block(name: "volume", placement: .right, command: "<your-volume-script>")
//
// 2. Compile and run the daemon:
//      cc -o ~/.local/bin/bar-volumed daemons/volume/volumed.c -framework CoreAudio
//      ~/.local/bin/bar-volumed &
//
// 3. Or install as a luanch agent:
//      cp daemons/volume/house.panini.bar.volumed.plist ~/Library/LaunchAgents/
//      # Make sure to update the ProgramArguments with the correct path to the program
//      launchctl load ~/Library/LaunchAgents/house.panini.bar.volumed.plist
//
// Compile:
//   cc -o bar-volumed daemons/volume/volumed.c -framework CoreAudio

#include <CoreAudio/CoreAudio.h>
#include <notify.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static volatile int keep_running = 1;

static void handle_signal(int sig) {
	(void)sig;
	keep_running = 0;
}

// ── Volume change callback ───────────────────────────────────────────

static OSStatus volume_changed(
	AudioObjectID object,
	UInt32 num_addresses,
	const AudioObjectPropertyAddress addresses[],
	void *context
) {
	(void)object; (void)num_addresses; (void)addresses; (void)context;
	notify_post("bar.touch.volume");
	return noErr;
}

// ── Device change callback ───────────────────────────────────────────
// Re-registers the volume listener when the output device changes
// (e.g. headphones plugged in).

static AudioDeviceID current_device = kAudioObjectUnknown;
static void register_volume_listener(AudioDeviceID device);

static OSStatus device_changed(
	AudioObjectID object,
	UInt32 num_addresses,
	const AudioObjectPropertyAddress addresses[],
	void *context
) {
	(void)object; (void)num_addresses; (void)addresses; (void)context;

	AudioObjectPropertyAddress addr = {
		kAudioHardwarePropertyDefaultOutputDevice,
		kAudioObjectPropertyScopeGlobal,
		kAudioObjectPropertyElementMain
	};
	AudioDeviceID new_device = kAudioObjectUnknown;
	UInt32 size = sizeof(new_device);
	AudioObjectGetPropertyData(
		kAudioObjectSystemObject, &addr, 0, NULL, &size, &new_device);

	if (new_device != current_device && new_device != kAudioObjectUnknown) {
		register_volume_listener(new_device);
	}
	return noErr;
}

// ── Register listener on a device ────────────────────────────────────

static void register_volume_listener(AudioDeviceID device) {
	// Remove old listener if any
	if (current_device != kAudioObjectUnknown) {
		AudioObjectPropertyAddress old_addr = {
			kAudioDevicePropertyVolumeScalar,
			kAudioDevicePropertyScopeOutput,
			1  // channel 1
		};
		AudioObjectRemovePropertyListener(
			current_device, &old_addr, volume_changed, NULL);
	}

	current_device = device;

	// Get the preferred stereo channel (usually 1)
	UInt32 channel = 1;
	AudioObjectPropertyAddress chan = {
		kAudioDevicePropertyPreferredChannelsForStereo,
		kAudioDevicePropertyScopeOutput,
		kAudioObjectPropertyElementMain
	};
	UInt32 channels[2] = {0, 0};
	UInt32 size = sizeof(channels);
	if (AudioObjectGetPropertyData(device, &chan, 0, NULL, &size, &channels) == noErr) {
		channel = channels[0];
	}

	// Listen for volume changes on the main output channel
	AudioObjectPropertyAddress vol_addr = {
		kAudioDevicePropertyVolumeScalar,
		kAudioDevicePropertyScopeOutput,
		channel
	};
	AudioObjectAddPropertyListener(device, &vol_addr, volume_changed, NULL);

	// Also listen for mute changes
	AudioObjectPropertyAddress mute_addr = {
		kAudioDevicePropertyMute,
		kAudioDevicePropertyScopeOutput,
		kAudioObjectPropertyElementMain
	};
	AudioObjectAddPropertyListener(device, &mute_addr, volume_changed, NULL);
}

// ── Main ─────────────────────────────────────────────────────────────

int main(void) {
	signal(SIGINT, handle_signal);
	signal(SIGTERM, handle_signal);

	// Listen for output device changes
	AudioObjectPropertyAddress dev_addr = {
		kAudioHardwarePropertyDefaultOutputDevice,
		kAudioObjectPropertyScopeGlobal,
		kAudioObjectPropertyElementMain
	};
	AudioObjectAddPropertyListener(
		kAudioObjectSystemObject, &dev_addr, device_changed, NULL);

	// Register on the current device
	UInt32 size = sizeof(current_device);
	AudioObjectGetPropertyData(
		kAudioObjectSystemObject, &dev_addr, 0, NULL, &size, &current_device);

	if (current_device != kAudioObjectUnknown) {
		register_volume_listener(current_device);
	}

	// Run until SIGINT/SIGTERM
	while (keep_running) {
		pause();
	}

	// Cleanup
	if (current_device != kAudioObjectUnknown) {
		AudioObjectPropertyAddress vol_addr = {
			kAudioDevicePropertyVolumeScalar,
			kAudioDevicePropertyScopeOutput,
			1  // channel 1
		};
		AudioObjectRemovePropertyListener(
			current_device, &vol_addr, volume_changed, NULL);
	}

	return 0;
}