forked from kfix/ddcctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddcctl.m
407 lines (353 loc) · 16 KB
/
ddcctl.m
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// ddcctl.m
// query and control monitors through their on-wire data channels and OSD microcontrollers
// http://en.wikipedia.org/wiki/Display_Data_Channel#DDC.2FCI
// http://en.wikipedia.org/wiki/Monitor_Control_Command_Set
//
// Copyright Joey Korkames 2016 http://github.com/kfix
// Licensed under GPLv3, full text at http://www.gnu.org/licenses/gpl-3.0.txt
// Now using argv[] instead of user-defaults to handle commandline arguments.
// Added optional use of an external app 'OSDisplay' to have a BezelUI like OSD.
// Have fun! Marc (Saman-VDR) 2016
#ifdef DEBUG
#define MyLog NSLog
#else
#define MyLog(...) (void)printf("%s\n",[[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#endif
#import <Foundation/Foundation.h>
#import <AppKit/NSScreen.h>
#import "DDC.h"
#ifdef BLACKLIST
NSUserDefaults *defaults;
int blacklistedDeviceWithNumber;
#endif
#ifdef OSD
bool useOsd;
#endif
NSString *EDIDString(char *string)
{
NSString *temp = [[NSString alloc] initWithBytes:string length:13 encoding:NSASCIIStringEncoding];
return ([temp rangeOfString:@"\n"].location != NSNotFound) ? [[temp componentsSeparatedByString:@"\n"] objectAtIndex:0] : temp;
}
/* Get current value for control from display */
uint getControl(CGDirectDisplayID cdisplay, uint control_id)
{
struct DDCReadCommand command;
command.control_id = control_id;
command.max_value = 0;
command.current_value = 0;
MyLog(@"D: querying VCP control: #%u =?", command.control_id);
if (!DDCRead(cdisplay, &command)) {
MyLog(@"E: DDC send command failed!");
MyLog(@"E: VCP control #%u (0x%02hhx) = current: %u, max: %u", command.control_id, command.control_id, command.current_value, command.max_value);
} else {
MyLog(@"I: VCP control #%u (0x%02hhx) = current: %u, max: %u", command.control_id, command.control_id, command.current_value, command.max_value);
}
return command.current_value;
}
/* Set new value for control from display */
void setControl(CGDirectDisplayID cdisplay, uint control_id, uint new_value)
{
struct DDCWriteCommand command;
command.control_id = control_id;
command.new_value = new_value;
MyLog(@"D: setting VCP control #%u => %u", command.control_id, command.new_value);
if (!DDCWrite(cdisplay, &command)){
MyLog(@"E: Failed to send DDC command!");
}
#ifdef OSD
if (useOsd) {
NSString *OSDisplay = @"/Applications/OSDisplay.app/Contents/MacOS/OSDisplay";
switch (control_id) {
case 16:
[NSTask launchedTaskWithLaunchPath:OSDisplay
arguments:[NSArray arrayWithObjects:
@"-l", [NSString stringWithFormat:@"%u", new_value],
@"-i", @"brightness", nil]];
break;
case 18:
[NSTask launchedTaskWithLaunchPath:OSDisplay
arguments:[NSArray arrayWithObjects:
@"-l", [NSString stringWithFormat:@"%u", new_value],
@"-i", @"contrast", nil]];
break;
default:
break;
}
}
#endif
}
/* Get current value to Set relative value for control from display */
void getSetControl(CGDirectDisplayID cdisplay, uint control_id, NSString *new_value, NSString *operator)
{
struct DDCReadCommand command;
command.control_id = control_id;
command.max_value = 0;
command.current_value = 0;
// read
MyLog(@"D: querying VCP control: #%u =?", command.control_id);
if (!DDCRead(cdisplay, &command)) {
MyLog(@"E: DDC send command failed!");
MyLog(@"E: VCP control #%u (0x%02hhx) = current: %u, max: %u", command.control_id, command.control_id, command.current_value, command.max_value);
} else {
MyLog(@"I: VCP control #%u (0x%02hhx) = current: %u, max: %u", command.control_id, command.control_id, command.current_value, command.max_value);
}
// calculate
NSString *formula = [NSString stringWithFormat:@"%u %@ %@", command.current_value, operator, new_value];
NSExpression *exp = [NSExpression expressionWithFormat:formula];
NSNumber *set_value = [exp expressionValueWithObject:nil context:nil];
// validate and write
if (set_value.intValue >= 0 && set_value.intValue <= command.max_value) {
MyLog(@"D: relative setting: %@ = %d", formula, set_value.intValue);
setControl(cdisplay, control_id, set_value.unsignedIntValue);
} else {
MyLog(@"D: relative setting: %@ = %d is out of range!", formula, set_value.intValue);
}
}
/* Main function */
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSPointerArray *_displayIDs = [NSPointerArray pointerArrayWithOptions:NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality];
for (NSScreen *screen in NSScreen.screens)
{
NSDictionary *description = [screen deviceDescription];
if ([description objectForKey:@"NSDeviceIsScreen"]) {
CGDirectDisplayID screenNumber = [[description objectForKey:@"NSScreenNumber"] unsignedIntValue];
if (CGDisplayIsBuiltin(screenNumber)) continue; // ignore MacBook screens because the lid can be closed and they don't use DDC.
[_displayIDs addPointer:(void *)(UInt64)screenNumber];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize displayPhysicalSize = CGDisplayScreenSize(screenNumber); // dspPhySz only valid if EDID present!
float displayScale = [screen backingScaleFactor];
double rotation = CGDisplayRotation(screenNumber);
if (displayScale > 1) {
MyLog(@"D: NSScreen #%u (%.0fx%.0f %g°) HiDPI",
screenNumber,
displayPixelSize.width,
displayPixelSize.height,
rotation);
}
else {
MyLog(@"D: NSScreen #%u (%.0fx%.0f %g°) %0.2f DPI",
screenNumber,
displayPixelSize.width,
displayPixelSize.height,
rotation,
(displayPixelSize.width / displayPhysicalSize.width) * 25.4f); // there being 25.4 mm in an inch
}
}
}
MyLog(@"I: found %lu external display%@", [_displayIDs count], [_displayIDs count] > 1 ? @"s" : @"");
// Defaults
NSString *screenName = @"";
NSUInteger displayId = -1;
NSUInteger command_interval = 100000;
BOOL dump_values = NO;
NSString *HelpString = @"Usage:\n"
@"ddcctl \t-d <1-..> [display#]\n"
@"\t-w 100000 [delay usecs between settings]\n"
@"\n"
@"----- Basic settings -----\n"
@"\t-b <1-..> [brightness]\n"
@"\t-c <1-..> [contrast]\n"
@"\t-rbc [reset brightness and contrast]\n"
#ifdef OSD
@"\t-O [osd: needs external app 'OSDisplay']\n"
#endif
@"\n"
@"----- Settings that don\'t always work -----\n"
@"\t-m <1|2> [mute speaker OFF/ON]\n"
@"\t-v <1-254> [speaker volume]\n"
@"\t-i <1-18> [select input source]\n"
@"\t-p <1|2-5> [power on | standby/off]\n"
@"\t-o [read-only orientation]\n"
@"\n"
@"----- Settings (testing) -----\n"
@"\t-rg <1-..> [red gain]\n"
@"\t-gg <1-..> [green gain]\n"
@"\t-bg <1-..> [blue gain]\n"
@"\t-rrgb [reset color]\n"
@"\n"
@"----- Setting grammar -----\n"
@"\t-X ? (query value of setting X)\n"
@"\t-X NN (put setting X to NN)\n"
@"\t-X <NN>- (decrease setting X by NN)\n"
@"\t-X <NN>+ (increase setting X by NN)";
// Commandline Arguments
NSMutableDictionary *actions = [[NSMutableDictionary alloc] init];
for (int i=1; i<argc; i++)
{
if (!strcmp(argv[i], "-d")) {
i++;
if (i >= argc) break;
displayId = atoi(argv[i]);
}
else if (!strcmp(argv[i], "-b")) {
i++;
if (i >= argc) break;
[actions setObject:@[@BRIGHTNESS, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"b"];
}
else if (!strcmp(argv[i], "-c")) {
i++;
if (i >= argc) break;
[actions setObject:@[@CONTRAST, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"c"];
}
else if (!strcmp(argv[i], "-rbc")) {
[actions setObject:@[@RESET_BRIGHTNESS_AND_CONTRAST, @"1"] forKey:@"rbc"];
}
else if (!strcmp(argv[i], "-rg")) {
i++;
if (i >= argc) break;
[actions setObject:@[@RED_GAIN, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"rg"];
}
else if (!strcmp(argv[i], "-gg")) {
i++;
if (i >= argc) break;
[actions setObject:@[@GREEN_GAIN, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"gg"];
}
else if (!strcmp(argv[i], "-bg")) {
i++;
if (i >= argc) break;
[actions setObject:@[@BLUE_GAIN, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"bg"];
}
else if (!strcmp(argv[i], "-rrgb")) {
[actions setObject:@[@RESET_COLOR, @"1"] forKey:@"rrgb"];
}
else if (!strcmp(argv[i], "-D")) {
dump_values = YES;
}
else if (!strcmp(argv[i], "-p")) {
i++;
if (i >= argc) break;
[actions setObject:@[@DPMS, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"p"];
}
else if (!strcmp(argv[i], "-o")) { // read only
[actions setObject:@[@ORIENTATION, @"?"] forKey:@"o"];
}
else if (!strcmp(argv[i], "-osd")) { // read only - returns '1' (OSD closed) or '2' (OSD active)
[actions setObject:@[@ON_SCREEN_DISPLAY, @"?"] forKey:@"osd"];
}
else if (!strcmp(argv[i], "-lang")) { // read only
[actions setObject:@[@OSD_LANGUAGE, @"?"] forKey:@"lang"];
}
else if (!strcmp(argv[i], "-reset")) {
[actions setObject:@[@RESET, @"1"] forKey:@"reset"];
}
else if (!strcmp(argv[i], "-preset_a")) {
i++;
if (i >= argc) break;
[actions setObject:@[@COLOR_PRESET_A, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"preset_a"];
}
else if (!strcmp(argv[i], "-preset_b")) {
i++;
if (i >= argc) break;
[actions setObject:@[@COLOR_PRESET_B, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"preset_b"];
}
else if (!strcmp(argv[i], "-preset_c")) {
i++;
if (i >= argc) break;
[actions setObject:@[@COLOR_PRESET_C, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"preset_c"];
}
else if (!strcmp(argv[i], "-i")) {
i++;
if (i >= argc) break;
[actions setObject:@[@INPUT_SOURCE, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"i"];
}
else if (!strcmp(argv[i], "-m")) {
i++;
if (i >= argc) break;
[actions setObject:@[@AUDIO_MUTE, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"m"];
}
else if (!strcmp(argv[i], "-v")) {
i++;
if (i >= argc) break;
[actions setObject:@[@AUDIO_SPEAKER_VOLUME, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"v"];
}
else if (!strcmp(argv[i], "-w")) {
i++;
if (i >= argc) break;
command_interval = atoi(argv[i]);
}
#ifdef OSD
else if (!strcmp(argv[i], "-O")) {
useOsd = YES;
}
#endif
#ifdef TEST
else if (!strcmp(argv[i], "-test")) {
i++;
if (i >= argc) break;
NSString *test = [[NSString alloc] initWithUTF8String:argv[i]];
i++;
if (i >= argc) break;
[actions setObject:@[test, [[NSString alloc] initWithUTF8String:argv[i]]] forKey:@"test"];
NSLog(@"TEST: %@ %@", test, [[NSString alloc] initWithUTF8String:argv[i]]);
}
#endif
else if (!strcmp(argv[i], "-h")) {
NSLog(@"ddcctl 0.1x - %@", HelpString);
return 0;
}
else {
NSLog(@"Unknown argument: %@", [[NSString alloc] initWithUTF8String:argv[i]]);
return -1;
}
}
// Let's go...
if (0 < displayId && displayId <= [_displayIDs count]) {
MyLog(@"I: polling display %lu's EDID", displayId);
CGDirectDisplayID cdisplay = (CGDirectDisplayID)[_displayIDs pointerAtIndex:displayId - 1];
struct EDID edid = {};
if (EDIDTest(cdisplay, &edid)) {
for (union descriptor *des = edid.descriptors; des < edid.descriptors + sizeof(edid.descriptors); des++) {
switch (des->text.type)
{
case 0xFF:
MyLog(@"I: got edid.serial: %@", EDIDString(des->text.data));
break;
case 0xFC:
screenName = EDIDString(des->text.data);
MyLog(@"I: got edid.name: %@", screenName);
break;
}
}
// Debugging
if (dump_values) {
for (uint i=0x00; i<=255; i++) {
getControl(cdisplay, i);
usleep(command_interval);
}
}
// Actions
[actions enumerateKeysAndObjectsUsingBlock:^(id argname, NSArray* valueArray, BOOL *stop) {
NSInteger control_id = [valueArray[0] intValue];
NSString *argval = valueArray[1];
MyLog(@"D: action: %@: %@", argname, argval);
if (control_id > -1) {
// this is a valid monitor control
NSString *argval_num = [argval stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-+"]]; // look for relative setting ops
if ([argval hasPrefix:@"+"] || [argval hasPrefix:@"-"]) { // +/-NN relative
getSetControl(cdisplay, control_id, argval_num, [argval substringToIndex:1]);
} else if ([argval hasSuffix:@"+"] || [argval hasSuffix:@"-"]) { // NN+/- relative
// read, calculate, then write
getSetControl(cdisplay, control_id, argval_num, [argval substringFromIndex:argval.length - 1]);
} else if ([argval hasPrefix:@"?"]) {
// read current setting
getControl(cdisplay, control_id);
} else if (argval_num == argval) {
// write fixed setting
setControl(cdisplay, control_id, [argval intValue]);
}
}
usleep(command_interval); // stagger comms to these wimpy I2C mcu's
}];
} else {
MyLog(@"E: Failed to poll display!");
return -1;
}
} else { // no display id given
NSLog(@"%@", HelpString);
}
}
return 0;
}