This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdisplay-brightness.c
124 lines (99 loc) · 4.17 KB
/
display-brightness.c
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
/*
Set Mac Display Backlight Brightness
Usage:
gcc -std=c99 -o dbrightness display-brightness.c -framework IOKit -framework ApplicationServices
./dbrightness 0.8
*/
#include <IOKit/graphics/IOGraphicsLib.h>
#include <ApplicationServices/ApplicationServices.h>
// According to: https://stackoverflow.com/questions/20025868/cgdisplayioserviceport-is-deprecated-in-os-x-10-9-how-to-replace
// ...CGDisplayIOServicePort is deprecated. However, the discussion from: https://github.com/glfw/glfw/blob/e0a6772e5e4c672179fc69a90bcda3369792ed1f/src/cocoa_monitor.m
// ...suggests that GLFW's cocoa_monitor offers the implementation we need. Eun's solution appears to work well. It is pasted below.
// Returns the io_service_t corresponding to a CG display ID, or 0 on failure.
// The io_service_t should be released with IOObjectRelease when not needed.
//
static io_service_t IOServicePortFromCGDisplayID(CGDirectDisplayID displayID)
{
io_iterator_t iter;
io_service_t serv, servicePort = 0;
CFMutableDictionaryRef matching = IOServiceMatching("IODisplayConnect");
// releases matching for us
kern_return_t err = IOServiceGetMatchingServices(kIOMasterPortDefault,
matching,
&iter);
if ( err )
return 0;
while ( (serv = IOIteratorNext(iter)) != 0 )
{
CFDictionaryRef displayInfo;
CFNumberRef vendorIDRef;
CFNumberRef productIDRef;
CFNumberRef serialNumberRef;
displayInfo = IODisplayCreateInfoDictionary( serv, kIODisplayOnlyPreferredName );
Boolean success;
success = CFDictionaryGetValueIfPresent( displayInfo, CFSTR(kDisplayVendorID), (const void**) & vendorIDRef );
success &= CFDictionaryGetValueIfPresent( displayInfo, CFSTR(kDisplayProductID), (const void**) & productIDRef );
if ( !success )
{
CFRelease(displayInfo);
continue;
}
SInt32 vendorID;
CFNumberGetValue( vendorIDRef, kCFNumberSInt32Type, &vendorID );
SInt32 productID;
CFNumberGetValue( productIDRef, kCFNumberSInt32Type, &productID );
// If a serial number is found, use it.
// Otherwise serial number will be nil (= 0) which will match with the output of 'CGDisplaySerialNumber'
SInt32 serialNumber = 0;
if ( CFDictionaryGetValueIfPresent(displayInfo, CFSTR(kDisplaySerialNumber), (const void**) & serialNumberRef) )
{
CFNumberGetValue( serialNumberRef, kCFNumberSInt32Type, &serialNumber );
}
// If the vendor and product id along with the serial don't match
// then we are not looking at the correct monitor.
// NOTE: The serial number is important in cases where two monitors
// are the exact same.
if( CGDisplayVendorNumber(displayID) != vendorID ||
CGDisplayModelNumber(displayID) != productID ||
CGDisplaySerialNumber(displayID) != serialNumber )
{
CFRelease(displayInfo);
continue;
}
servicePort = serv;
CFRelease(displayInfo);
break;
}
IOObjectRelease(iter);
return servicePort;
}
float getDisplayBrightness(void) {
CGDisplayErr dErr;
io_service_t service;
CGDirectDisplayID targetDisplay;
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
float brightness = HUGE_VALF;
targetDisplay = CGMainDisplayID();
service = IOServicePortFromCGDisplayID(targetDisplay);
dErr = IODisplayGetFloatParameter(service, kNilOptions, key, &brightness);
return brightness;
}
void setDisplayBrightness(float brightness) {
CGDisplayErr dErr;
io_service_t service;
CGDirectDisplayID targetDisplay;
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
targetDisplay = CGMainDisplayID();
service = IOServicePortFromCGDisplayID(targetDisplay);
dErr = IODisplaySetFloatParameter(service, kNilOptions, key, brightness);
}
int main(int argc, char **argv) {
// ./dbrightness 0.523
float brightness;
if (argc > 1 && sscanf(argv[1], "%f", &brightness) == 1) {
setDisplayBrightness(brightness);
} else {
printf("%f", getDisplayBrightness());
}
exit(0);
}