aboutsummaryrefslogtreecommitdiffstats
path: root/SSVLSystemAlertVolume.c
blob: 8e5019580dcb41a576d9ca7bcfb9dfc6b34435a2 (plain)
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
/*
 * Copyright (c) 2022  Teddy Wing
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

// Version: 0.1.0

#include "SSVLSystemAlertVolume.h"

// This property appears to be private, as it isn't defined in Apple's MacOSX
// SDK framework headers.
//
// The value comes from the Sound preference pane. Disassembling the
// `/System/Library/PreferencePanes/Sound.prefPane/Contents/MacOS/Sound` binary
// shows that functions accessing the system alert volume do so via the
// '0x7373766c' property. Converting that to a FourCharCode yields 'ssvl'.
const AudioServicesPropertyID SSVLAudioServicesPropertySystemAlertVolume = 'ssvl';

OSStatus SSVLGetSystemVolume(Float32 *volume) {
	UInt32 volume_size = sizeof(*volume);

	OSStatus result = AudioServicesGetProperty(
		SSVLAudioServicesPropertySystemAlertVolume,
		0,
		NULL,
		&volume_size,
		volume
	);

	if (*volume != 0) {
		*volume = log(*volume) + 1.0;
	}
	else {
		*volume = 0;
	}

	return result;
}

OSStatus SSVLSetSystemVolume(Float32 volume) {
	volume = exp(volume - 1.0);

	return AudioServicesSetProperty(
		SSVLAudioServicesPropertySystemAlertVolume,
		0,
		NULL,
		sizeof(volume),
		&volume
	);
}