forked from kpwn/uikittools-ng
-
Notifications
You must be signed in to change notification settings - Fork 14
/
uidisplay.m
686 lines (596 loc) · 18.6 KB
/
uidisplay.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <dlfcn.h>
#import <err.h>
#import <getopt.h>
#import <objc/runtime.h>
#if NLS
# include <libintl.h>
# define _(a) gettext(a)
# define PACKAGE "uikittools-ng"
#else
# define _(a) a
#endif
#ifndef LOCALEDIR
# define LOCALEDIR "/usr/share/locale"
#endif
#define OPTIONAL_ARGUMENT_IS_PRESENT \
((optarg == NULL && optind < argc && argv[optind][0] != '-') \
? (bool)(optarg = argv[optind++]) \
: (optarg != NULL))
typedef struct {
NSInteger style;
NSInteger time;
} DarkModeOverride;
@interface UISUserInterfaceStyleMode : NSObject
@property(assign, nonatomic) NSInteger modeValue;
- (void)setOverride:(DarkModeOverride)override;
@end
typedef struct {
int hour;
int minute;
} Time;
typedef struct {
Time fromTime;
Time toTime;
} Schedule;
typedef struct {
BOOL active;
BOOL enabled;
BOOL sunSchedulePermitted;
int mode;
Schedule schedule;
NSUInteger disableFlags;
BOOL available;
} Status;
@interface CBBlueLightClient : NSObject
- (void)setEnabled:(BOOL)arg1;
+ (BOOL)supportsBlueLightReduction;
- (BOOL)getBlueLightStatus:(Status *)arg1;
@end
@interface CBAdaptationClient : NSObject
- (void)setEnabled:(BOOL)arg1;
- (BOOL)getEnabled;
+ (BOOL)supportsAdaptation;
@end
BOOL _AXSAutoBrightnessEnabled();
void _AXSSetReduceWhitePointEnabled(BOOL enabled);
BOOL _AXSReduceWhitePointEnabled();
uint16_t kHIDPage_Consumer = 0x0C;
uint16_t kHIDUsage_Csmr_Power = 0x30;
void *IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
void *IOHIDEventCreateKeyboardEvent(CFAllocatorRef allocator,
uint64_t timeStamp, uint16_t usagePage,
uint16_t usage, Boolean down,
uint32_t flags);
void IOHIDEventSetSenderID(void *event, uint64_t senderID);
void IOHIDEventSystemClientDispatchEvent(void *client, void *event);
uint64_t mach_absolute_time();
int IOObjectRelease(void *object);
typedef enum { sOn, sOff, sUnspecified } state;
typedef enum {
iNope,
iEverything,
iAutoBrightness,
iBrightness,
iDarkMode,
iNightShift,
iTrueTone,
iReduceWhitePoint,
iHeight,
iWidth,
iPhysicalHeight,
iPhysicalWidth,
iScale
} infoType;
// clang-format off
void usage() {
printf(_("Usage: %s [-a state] [-b [+|-]num] [-d state] [-h] [-i [key]] [-j] [-l] [-n state] [-t state] [-w state]\n"), getprogname());
}
// clang-format on
state stateFromString(char *stringState, char *argName) {
if (strcmp("0", stringState) == 0 || strcmp("off", stringState) == 0) {
return sOff;
} else if (strcmp("1", stringState) == 0 ||
strcmp("on", stringState) == 0) {
return sOn;
} else {
errx(1, _("Invalid %s value: '%s', permitted values: 0, off, 1, on\n"),
argName, stringState);
}
}
char *stateAsString(state theState) {
switch (theState) {
case sOn:
return _("on");
case sOff:
return _("off");
case sUnspecified:
return _("not supported");
}
}
id stateAsJSONType(state theState) {
switch (theState) {
case sOn:
return @YES;
case sOff:
return @NO;
case sUnspecified:
return nil;
}
}
void setAutoBrightness(state newState) {
void *backBoardServices = dlopen(
"/System/Library/PrivateFrameworks/BackBoardServices.framework/"
"BackBoardServices",
RTLD_NOW);
void (*BKSDisplayBrightnessSetAutoBrightnessEnabled)(BOOL) = dlsym(
backBoardServices, "BKSDisplayBrightnessSetAutoBrightnessEnabled");
BKSDisplayBrightnessSetAutoBrightnessEnabled(newState == sOn);
dlclose(backBoardServices);
}
void setDarkMode(state newState) {
void *UIKitServices = dlopen(
"/System/Library/PrivateFrameworks/UIKitServices.framework/"
"UIKitServices",
RTLD_NOW);
if (objc_lookUpClass("UISUserInterfaceStyleMode") != nil) {
UISUserInterfaceStyleMode *styleMode =
[objc_getClass("UISUserInterfaceStyleMode") new];
BOOL(*UISUserInterfaceStyleModeValueIsAutomatic)
(NSInteger) =
dlsym(UIKitServices, "UISUserInterfaceStyleModeValueIsAutomatic");
if (UISUserInterfaceStyleModeValueIsAutomatic(styleMode.modeValue)) {
DarkModeOverride override = {.style = (newState == sOn) ? 2 : 1,
.time = 1};
[styleMode setOverride:override];
} else {
styleMode.modeValue = (newState == sOn) ? 2 : 1;
}
dlclose(UIKitServices);
} else {
errx(2, _("Dark Mode is only supported on iOS 13 and higher.\n"));
}
}
state getDarkMode() {
void *UIKitServices = dlopen(
"/System/Library/PrivateFrameworks/UIKitServices.framework/"
"UIKitServices",
RTLD_NOW);
if (objc_lookUpClass("UISUserInterfaceStyleMode") != nil) {
UISUserInterfaceStyleMode *styleMode =
[objc_getClass("UISUserInterfaceStyleMode") new];
dlclose(UIKitServices);
return styleMode.modeValue != 1 ? sOn : sOff;
} else {
return sUnspecified;
}
}
void setNightShift(state newState) {
void *coreBrightness = dlopen(
"/System/Library/PrivateFrameworks/CoreBrightness.framework/"
"CoreBrightness",
RTLD_NOW);
Class classCBBlueLightClient = objc_getClass("CBBlueLightClient");
if ([classCBBlueLightClient supportsBlueLightReduction]) {
[[classCBBlueLightClient new] setEnabled:(newState == sOn)];
} else {
dlclose(coreBrightness);
errx(2, _("Night Shift is not supported on this device.\n"));
}
dlclose(coreBrightness);
}
state getNightShift() {
void *coreBrightness = dlopen(
"/System/Library/PrivateFrameworks/CoreBrightness.framework/"
"CoreBrightness",
RTLD_NOW);
Class classCBBlueLightClient = objc_getClass("CBBlueLightClient");
state theState = sUnspecified;
if ([classCBBlueLightClient supportsBlueLightReduction]) {
Status status;
[[classCBBlueLightClient new] getBlueLightStatus:&status];
theState = status.enabled ? sOn : sOff;
}
dlclose(coreBrightness);
return theState;
}
void setTrueTone(state newState) {
void *coreBrightness = dlopen(
"/System/Library/PrivateFrameworks/CoreBrightness.framework/"
"CoreBrightness",
RTLD_NOW);
Class classCBAdaptationClient = objc_getClass("CBAdaptationClient");
if ([classCBAdaptationClient supportsAdaptation]) {
[[classCBAdaptationClient new] setEnabled:(newState == sOn)];
} else {
dlclose(coreBrightness);
errx(2, _("True Tone is not supported on this device.\n"));
}
dlclose(coreBrightness);
}
state getTrueTone() {
void *coreBrightness = dlopen(
"/System/Library/PrivateFrameworks/CoreBrightness.framework/"
"CoreBrightness",
RTLD_NOW);
Class classCBAdaptationClient = objc_getClass("CBAdaptationClient");
state theState = sUnspecified;
if ([classCBAdaptationClient supportsAdaptation]) {
theState = [[classCBAdaptationClient new] getEnabled] ? sOn : sOff;
}
dlclose(coreBrightness);
return theState;
}
void setBrightness(char *value) {
BOOL increase = NO;
BOOL decrease = NO;
if (value[0] == '+') {
increase = YES;
} else if (value[0] == '-') {
decrease = YES;
}
size_t length = strlen(value);
int number;
const char *errstr;
if (value[length - 1] == '%')
value[length - 1] = '\0';
if (increase || decrease) {
if (length < 2) {
errx(1, _("Invalid brightness value: %s\n"), value);
}
char *onlyValue = malloc(length - 1);
strncpy(onlyValue, value + 1, length - 1);
number = (int)strtonum(onlyValue, 0, 100, &errstr);
free(onlyValue);
} else {
number = (int)strtonum(value, 0, 100, &errstr);
}
if (errstr != NULL)
err(1, "%s", value);
if (number == 0 && (increase || decrease)) return;
float brightness = ((float)number) / 100;
void *backBoardServices = dlopen(
"/System/Library/PrivateFrameworks/BackBoardServices.framework/"
"BackBoardServices",
RTLD_NOW);
float (*BKSDisplayBrightnessGetCurrent)(void) =
dlsym(backBoardServices, "BKSDisplayBrightnessGetCurrent");
if (increase) {
brightness += BKSDisplayBrightnessGetCurrent();
if (brightness > 1.0) {
dlclose(backBoardServices);
errx(1, _("Unable to increase the brightness by %d, %.6g.\n"), number,
brightness * 100);
}
} else if (decrease) {
brightness = BKSDisplayBrightnessGetCurrent() - brightness;
if (brightness < 0.0) {
dlclose(backBoardServices);
errx(1, _("Unable to decrease the brightness by %d, %.6g.\n"), number,
brightness * 100);
}
}
void *(*BKSDisplayBrightnessTransactionCreate)(CFAllocatorRef allocator) =
dlsym(backBoardServices, "BKSDisplayBrightnessTransactionCreate");
void (*BKSDisplayBrightnessSet)(float brightness, long unknown) =
dlsym(backBoardServices, "BKSDisplayBrightnessSet");
void *transaction =
BKSDisplayBrightnessTransactionCreate(kCFAllocatorDefault);
BKSDisplayBrightnessSet(brightness, 1);
CFRelease(transaction);
dlclose(backBoardServices);
}
float getBrightness() {
void *backBoardServices = dlopen(
"/System/Library/PrivateFrameworks/BackBoardServices.framework/"
"BackBoardServices",
RTLD_NOW);
float (*BKSDisplayBrightnessGetCurrent)(void) =
dlsym(backBoardServices, "BKSDisplayBrightnessGetCurrent");
float brightness = BKSDisplayBrightnessGetCurrent() * 100;
dlclose(backBoardServices);
return brightness;
}
void lock() {
void *client = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
void *lockButtonDown = IOHIDEventCreateKeyboardEvent(
kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Consumer,
kHIDUsage_Csmr_Power, 1, 0);
IOHIDEventSetSenderID(lockButtonDown, 0x8000000817319372);
IOHIDEventSystemClientDispatchEvent(client, lockButtonDown);
IOObjectRelease(lockButtonDown);
void *lockButtonUp = IOHIDEventCreateKeyboardEvent(
kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Consumer,
kHIDUsage_Csmr_Power, 0, 0);
IOHIDEventSetSenderID(lockButtonUp, 0x8000000817319372);
IOHIDEventSystemClientDispatchEvent(client, lockButtonUp);
IOObjectRelease(lockButtonUp);
IOObjectRelease(client);
}
void printJSON(id notJSON) {
NSError *error;
NSData *jsonData;
jsonData = [NSJSONSerialization dataWithJSONObject:notJSON options:0 error:&error];
if (error)
errx(1, _("JSON formating failed: %s"), error.localizedDescription.UTF8String);
printf("%s\n", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding].UTF8String);
}
int main(int argc, char *argv[]) {
#if NLS
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
#endif
if (argc == 1) {
usage();
return 0;
}
// clang-format off
static struct option longOptions[] = {
{"help", no_argument, 0, 'h'},
{"info", optional_argument, 0, 'i'},
{"json", no_argument, 0, 'j'},
{"autobrightness", required_argument, 0, 'a'},
{"brightness", required_argument, 0, 'b'},
{"darkmode", required_argument, 0, 'd'},
{"lock", required_argument, 0, 'l'},
{"nightshift", required_argument, 0, 'n'},
{"truetone", required_argument, 0, 't'},
{"reducewhitepoint", required_argument, 0, 'w'},
{NULL, 0, NULL, 0}};
// clang-format on
BOOL json = NO;
infoType info = iNope;
int code = 0;
while ((code = getopt_long(argc, argv, "hi::ja:b:d:ln:t:w:", longOptions,
NULL)) != -1) {
switch (code) {
case 'h':
usage();
return 0;
case 'i': {
if (OPTIONAL_ARGUMENT_IS_PRESENT) {
if (strcmp(optarg, "autobrightness") == 0 || strcmp(optarg, "auto-brightness") == 0) {
info = iAutoBrightness;
} else if (strcmp(optarg, "brightness") == 0) {
info = iBrightness;
} else if (strcmp(optarg, "darkmode") == 0) {
info = iDarkMode;
} else if (strcmp(optarg, "nightshift") == 0) {
info = iNightShift;
} else if (strcmp(optarg, "truetone") == 0) {
info = iTrueTone;
} else if (strcmp(optarg, "reducewhitepoint") == 0 || strcmp(optarg, "whitepoint") == 0) {
info = iReduceWhitePoint;
} else if (strcmp(optarg, "height") == 0) {
info = iHeight;
} else if (strcmp(optarg, "width") == 0) {
info = iWidth;
} else if (strcmp(optarg, "physicalHeight") == 0) {
info = iPhysicalHeight;
} else if (strcmp(optarg, "physicalWidth") == 0) {
info = iPhysicalWidth;
} else if (strcmp(optarg, "scale") == 0) {
info = iScale;
} else {
errx(1, _("Unknown information type: %s\n"), optarg);
}
} else
info = iEverything;
break;
}
case 'j':
json = YES;
break;
case 'a': {
state newState = stateFromString(strdup(optarg), "Auto-Brightness");
setAutoBrightness(newState);
break;
}
case 'b':
setBrightness(strdup(optarg));
break;
case 'd': {
state newState = stateFromString(strdup(optarg), "Dark Mode");
setDarkMode(newState);
break;
}
case 'l':
lock();
break;
case 'n': {
state newState = stateFromString(strdup(optarg), "Night Shift");
setNightShift(newState);
break;
}
case 't': {
state newState = stateFromString(strdup(optarg), "True Tone");
setTrueTone(newState);
break;
}
case 'w': {
state newState = stateFromString(strdup(optarg), "Reduce White Point");
_AXSSetReduceWhitePointEnabled(newState == sOn);
break;
}
case '*':
usage();
return 1;
}
}
if (info != iNope) {
UIScreen *screen = [UIScreen mainScreen];
if (json) {
switch (info) {
case iAutoBrightness:
printJSON(@{
@"autoBrightness": @(_AXSAutoBrightnessEnabled()),
@"displayName": [NSString stringWithUTF8String:_("Auto-Brightness")]
});
break;
case iBrightness:
printJSON(@{
@"brightness": @(getBrightness()),
@"displayName": [NSString stringWithUTF8String:_("Brightness")]
});
break;
case iDarkMode:
printJSON(@{
@"darkMode": stateAsJSONType(getDarkMode()),
@"displayName": [NSString stringWithUTF8String:_("Dark Mode")]
});
break;
case iNightShift:
printJSON(@{
@"nightShift": stateAsJSONType(getNightShift()),
@"displayName": [NSString stringWithUTF8String:_("Night Shift")]
});
break;
case iTrueTone:
printJSON(@{
@"trueTone": stateAsJSONType(getNightShift()),
@"displayName": [NSString stringWithUTF8String:_("True Tone")]
});
break;
case iReduceWhitePoint:
printJSON(@{
@"trueTone": @(_AXSReduceWhitePointEnabled()),
@"displayName": [NSString stringWithUTF8String:_("Reduce White Point")]
});
break;
case iHeight:
printJSON(@{
@"height": @(CGRectGetHeight([screen bounds])),
@"displayName": [NSString stringWithUTF8String:_("Height")]
});
break;
case iWidth:
printJSON(@{
@"width": @(CGRectGetWidth([screen bounds])),
@"displayName": [NSString stringWithUTF8String:_("Width")]
});
break;
case iPhysicalHeight:
printJSON(@{
@"physicalHeight": @(CGRectGetHeight([screen nativeBounds])),
@"displayName": [NSString stringWithUTF8String:_("Physical Height")]
});
break;
case iPhysicalWidth:
printJSON(@{
@"physicalWidth": @(CGRectGetWidth([screen nativeBounds])),
@"displayName": [NSString stringWithUTF8String:_("Physical Width")]
});
break;
case iScale:
printJSON(@{
@"scale": @([screen scale]),
@"displayName": [NSString stringWithUTF8String:_("Scale")]
});
break;
default:
printJSON(@[
@{
@"autoBrightness": @(_AXSAutoBrightnessEnabled()),
@"displayName": [NSString stringWithUTF8String:_("Auto-Brightness")]
},
@{
@"brightness": @(getBrightness()),
@"displayName": [NSString stringWithUTF8String:_("Brightness")]
},
@{
@"darkMode": stateAsJSONType(getDarkMode()),
@"displayName": [NSString stringWithUTF8String:_("Dark Mode")]
},
@{
@"nightShift": stateAsJSONType(getNightShift()),
@"displayName": [NSString stringWithUTF8String:_("Night Shift")]
},
@{
@"trueTone": stateAsJSONType(getNightShift()),
@"displayName": [NSString stringWithUTF8String:_("True Tone")]
},
@{
@"reduceWhitePoint": @(_AXSReduceWhitePointEnabled()),
@"displayName": [NSString stringWithUTF8String:_("Reduce White Point")]
},
@{
@"height": @(CGRectGetHeight([screen bounds])),
@"displayName": [NSString stringWithUTF8String:_("Height")]
},
@{
@"width": @(CGRectGetWidth([screen bounds])),
@"displayName": [NSString stringWithUTF8String:_("Width")]
},
@{
@"physicalHeight": @(CGRectGetHeight([screen nativeBounds])),
@"displayName": [NSString stringWithUTF8String:_("Physical Height")]
},
@{
@"physicalWidth": @(CGRectGetWidth([screen nativeBounds])),
@"displayName": [NSString stringWithUTF8String:_("Physical Width")]
},
@{
@"scale": @([screen scale]),
@"displayName": [NSString stringWithUTF8String:_("Scale")]
}
]);
break;
}
}
else {
switch (info) {
case iAutoBrightness:
printf("%s: %s\n", _("Auto-Brightness"), stateAsString(_AXSAutoBrightnessEnabled() ? sOn : sOff));
break;
case iBrightness:
printf("%s: %.6g\n", _("Brightness"), getBrightness());
break;
case iDarkMode:
printf("%s: %s\n", _("Dark Mode"), stateAsString(getDarkMode()));
break;
case iNightShift:
printf("%s: %s\n", _("Night Shift"), stateAsString(getNightShift()));
break;
case iTrueTone:
printf("%s: %s\n", _("True Tone"), stateAsString(getTrueTone()));
break;
case iReduceWhitePoint:
printf("%s: %s\n", _("Reduce White Point"), stateAsString(_AXSReduceWhitePointEnabled() ? sOn : sOff));
break;
case iHeight:
printf("%s: %.6g\n", _("Height"), CGRectGetHeight([screen bounds]));
break;
case iWidth:
printf("%s: %.6g\n", _("Width"), CGRectGetWidth([screen bounds]));
break;
case iPhysicalHeight:
printf("%s: %.6g\n", _("Physical Height"), CGRectGetHeight([screen nativeBounds]));
break;
case iPhysicalWidth:
printf("%s: %.6g\n", _("Physical Width"), CGRectGetWidth([screen nativeBounds]));
break;
case iScale:
printf("%s: %.2g\n", _("Scale"), [screen scale]);
break;
default:
printf("%s: %.6g\n", _("Brightness"), getBrightness());
printf("%s: %s\n", _("Auto-Brightness"), stateAsString(_AXSAutoBrightnessEnabled() ? sOn : sOff));
printf("%s: %s\n", _("Dark Mode"), stateAsString(getDarkMode()));
printf("%s: %s\n", _("Night Shift"), stateAsString(getNightShift()));
printf("%s: %s\n", _("True Tone"), stateAsString(getTrueTone()));
printf("%s: %s\n", _("Reduce White Point"), stateAsString(_AXSReduceWhitePointEnabled() ? sOn : sOff));
printf("%s: %.6g\n", _("Height"), CGRectGetHeight([screen bounds]));
printf("%s: %.6g\n", _("Width"), CGRectGetWidth([screen bounds]));
printf("%s: %.6g\n", _("Physical Height"), CGRectGetHeight([screen nativeBounds]));
printf("%s: %.6g\n", _("Physical Width"), CGRectGetWidth([screen nativeBounds]));
printf("%s: %.2g\n", _("Scale"), [screen scale]);
break;
}
}
} else if (json) {
errx(1, _("-j/--json may only be used in combination with -i/--info"));
}
return 0;
}