-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayutil_nightshift.m
586 lines (478 loc) · 16.2 KB
/
displayutil_nightshift.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
/*
displayutil - displayutil_nightshift.m
enable / disable / schedule nightshift
History:
v. 1.0.0 (04/01/2021) - Initial version
v. 1.0.1 (04/15/2021) - Add support for nightshift schedules
Copyright (c) 2021 Sriranga R. Veeraraghavan <ranga@calalum.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#import <stdio.h>
#import <stdlib.h>
#import <Foundation/Foundation.h>
#import "CBBlueLightClient.h"
#import "displayutil_argutils.h"
#import "displayutil_nightshift.h"
/* strings to select nightshift mode */
const char *gStrModeNightShiftLong = "nightshift";
const char *gStrModeNightShiftShort = "ns";
const char *gStrModeNightShiftSchedule = "schedule";
const char *gStrModeNightShiftScheduleSunset = "sunset";
/* nightshift constants */
static const char *gStrNightShiftRange = "0.0 - 1.0";
static const char *gStrNightSiftScheduleSunset = "sunset to sunrise";
static const char *gStrNightShiftScheduleNone = "none";
static const char *gStrNightShiftScheduleRange = "[h]h:mm [h]h:mm";
/* error messages */
static const char *gStrErrNoNS = "nightshift not supported";
static const char *gStrErrNoNSClient = "cannot create a nightshift client";
static const char *gStrErrNSStatus = "cannot get nightshift status";
static const char *gStrErrNSStrength = "cannot get nightshift strength";
static const char *gStrErrInvalidNSStrength
= "nightshift strength must be 0.0-1.0";
static const char *gStrErrNSSetStrength
= "cannot change nightshift strength";
static const char *gStrErrNSSetState = "cannot update nightshift state";
static const char *gStrErrNSMode = "cannot change nightshift schedule";
static const char *gStrErrNSBadSched = "invalid schedule";
/* prototypes */
static bool isNightShiftAvailable(void);
static bool setNightShiftEnabled(bool status);
static bool isValidHourForNightShift(int hr);
static bool isValidMinForNightShift(int min);
/* private functions */
/* isNightShiftAvailable - check to see if nightshift is available */
static bool isNightShiftAvailable(void)
{
return [CBBlueLightClient supportsBlueLightReduction];
}
/* setNightShiftEnabled - set the nightshift status */
static bool setNightShiftEnabled(bool status)
{
CBBlueLightClient *blueLightClient = nil;
float nightShiftStrength = 0;
if (isNightShiftAvailable() != true)
{
fprintf(stderr, "error: %s\n", gStrErrNoNS);
return false;
}
blueLightClient = [[CBBlueLightClient alloc] init];
if (blueLightClient == nil)
{
fprintf(stderr, "error: %s\n", gStrErrNoNSClient);
return false;
}
/*
if nightshift is being enabled, but the current strength
is 0 (or less, which shouldn't happen), set the strength
to 1.0 (maximum)
*/
if (status == true)
{
if ([blueLightClient getStrength: &nightShiftStrength] == true &&
nightShiftStrength <= 0.0)
{
[blueLightClient setStrength: 1.0 commit: TRUE];
}
}
[blueLightClient setEnabled: status];
[blueLightClient release];
return true;
}
/* isValidHour - check if the specified number is a valid hour */
static bool isValidHourForNightShift(int hr)
{
return (hr >= 0 && hr <= 23);
}
/* isValidMin - check if the specified number is a valid minute */
static bool isValidMinForNightShift(int min)
{
return (min >= 0 && min <= 59);
}
/* public functions */
/* printNightShiftUsage - print usage message for nightshift mode */
void printNightShiftUsage(void)
{
/* basic usage */
fprintf(stderr,
"%s [%s|%s] [%s|%s|%s|%s]\n",
gPgmName,
gStrModeNightShiftShort,
gStrModeNightShiftLong,
gStrOn,
gStrEnable,
gStrOff,
gStrDisable);
/* set strength */
fprintf(stderr,
"%s [%s|%s] [%s]\n",
gPgmName,
gStrModeNightShiftShort,
gStrModeNightShiftLong,
gStrNightShiftRange);
/* schedule */
fprintf(stderr,
"%s [%s|%s] [%s [%s|%s|%s]]\n",
gPgmName,
gStrModeNightShiftShort,
gStrModeNightShiftLong,
gStrModeNightShiftSchedule,
gStrDisable,
gStrModeNightShiftScheduleSunset,
gStrNightShiftScheduleRange);
}
/* printNightShiftStatus - print the current status of nightshift */
bool printNightShiftStatus(nightShiftStatus_t status)
{
CBBlueLightClient *blueLightClient = nil;
CBBlueLightClient_StatusData_t blueLightStatus;
float nightShiftStrength = 0;
if (isNightShiftAvailable() != true)
{
fprintf(stderr,
"%s: error: %s\n",
gStrModeNightShiftLong,
gStrErrNoNS);
return false;
}
blueLightClient = [[CBBlueLightClient alloc] init];
if (blueLightClient == nil)
{
fprintf(stderr,
"%s: error: %s\n",
gStrModeNightShiftLong,
gStrErrNoNSClient);
return false;
}
if ([blueLightClient getBlueLightStatus: &blueLightStatus] != true)
{
fprintf(stderr,
"%s: error: %s\n",
gStrModeNightShiftLong,
gStrErrNSStatus);
[blueLightClient release];
return false;
}
if (status == nightShiftStatusAll ||
status == nightShiftStatusStrengthOnly)
{
if ([blueLightClient getStrength: &nightShiftStrength] != true)
{
fprintf(stderr,
"%s: error: %s\n",
gStrModeNightShiftLong,
gStrErrNSStrength);
[blueLightClient release];
return false;
}
}
if (status == nightShiftStatusAll)
{
fprintf(stdout,
"%s: %s",
gStrModeNightShiftLong,
(blueLightStatus.enabled == true ? gStrOn : gStrOff));
}
/*
check the nightshift mode and print out the nightshift schedule:
mode == 0: no schedule
mode == 1: sunset to sunrise
mode == 2: custom schedule
*/
if (status == nightShiftStatusAll ||
status == nightShiftStatusScheduleOnly)
{
if (blueLightStatus.mode != 0) {
if (blueLightStatus.sunsetToSunrise == true &&
blueLightStatus.mode == 1)
{
if (status == nightShiftStatusAll)
{
fprintf(stdout, ", schedule is from %s", gStrNightSiftScheduleSunset);
}
else
{
fprintf(stdout,
"%s: %s: %s\n",
gStrModeNightShiftSchedule,
gStrModeNightShiftLong,
gStrNightSiftScheduleSunset);
}
}
else
{
if (status == nightShiftStatusAll)
{
fprintf(stdout,
", schedule is from %d:%02d to %d:%02d",
blueLightStatus.schedule.from.hour,
blueLightStatus.schedule.from.minute,
blueLightStatus.schedule.to.hour,
blueLightStatus.schedule.to.minute);
}
else
{
fprintf(stdout,
"%s: %s: %d:%02d to %d:%02d\n",
gStrModeNightShiftLong,
gStrModeNightShiftSchedule,
blueLightStatus.schedule.from.hour,
blueLightStatus.schedule.from.minute,
blueLightStatus.schedule.to.hour,
blueLightStatus.schedule.to.minute);
}
}
}
else if (status == nightShiftStatusScheduleOnly)
{
fprintf(stdout,
"%s: %s: %s\n",
gStrModeNightShiftLong,
gStrModeNightShiftSchedule,
gStrNightShiftScheduleNone);
}
}
if (status == nightShiftStatusAll ||
status == nightShiftStatusStrengthOnly)
{
fprintf(stdout, ", strength: %0.2f\n", nightShiftStrength);
}
[blueLightClient release];
return true;
}
/* nightShiftEnable - enable nightshift */
bool nightShiftEnable(void)
{
return setNightShiftEnabled(true);
}
/* nightShiftDisable - disable nightshift */
bool nightShiftDisable(void)
{
return setNightShiftEnabled(false);
}
/* setNightShiftStrength - set nightshift's strength */
bool setNightShiftStrength(float strength)
{
CBBlueLightClient *blueLightClient = nil;
bool retcode = false;
if (strength < 0.0 || strength > 1.0)
{
fprintf(stderr, "error: %s\n", gStrErrInvalidNSStrength);
return retcode;
}
if (isNightShiftAvailable() != true)
{
fprintf(stderr, "error: %s\n", gStrErrNoNS);
return retcode;
}
blueLightClient = [[CBBlueLightClient alloc] init];
if (blueLightClient == nil)
{
fprintf(stderr, "error: %s\n", gStrErrNoNSClient);
return retcode;
}
if ([blueLightClient setStrength: strength commit: TRUE] != true)
{
fprintf(stderr, "error: %s\n", gStrErrNSSetStrength);
[blueLightClient release];
return retcode;
}
retcode = [blueLightClient setEnabled: (strength == 0.0 ? FALSE : TRUE)];
[blueLightClient release];
if (retcode != true)
{
fprintf(stderr, "error: %s\n", gStrErrNSSetState);
}
return retcode;
}
/* nightShiftScheduleDisable - disable the nightshift schedule */
bool nightShiftScheduleDisable(void)
{
CBBlueLightClient *blueLightClient = nil;
bool retcode = false;
if (isNightShiftAvailable() != true)
{
fprintf(stderr, "error: %s\n", gStrErrNoNS);
return retcode;
}
blueLightClient = [[CBBlueLightClient alloc] init];
if (blueLightClient == nil)
{
fprintf(stderr, "error: %s\n", gStrErrNoNSClient);
return retcode;
}
retcode = [blueLightClient setMode: CBBlueLightClientModeNoSchedule];
[blueLightClient release];
if (retcode != true)
{
fprintf(stderr, "error: %s\n", gStrErrNSMode);
}
return retcode;
}
/*
nightShiftScheduleSunsetSunrise - set the nightshift schedule to be
sunset to sunrise
*/
bool nightShiftScheduleSunsetSunrise(void)
{
CBBlueLightClient *blueLightClient = nil;
bool retcode = false;
if (isNightShiftAvailable() != true)
{
fprintf(stderr, "error: %s\n", gStrErrNoNS);
return retcode;
}
blueLightClient = [[CBBlueLightClient alloc] init];
if (blueLightClient == nil)
{
fprintf(stderr, "error: %s\n", gStrErrNoNSClient);
return retcode;
}
retcode = [blueLightClient setMode: CBBlueLightClientModeSunsetSunrise];
[blueLightClient release];
if (retcode != true)
{
fprintf(stderr, "error: %s\n", gStrErrNSMode);
}
return retcode;
}
/* nightShiftSchedule - set the nightshift schedule */
bool nightShiftSchedule(int startHr, int startMin, int endHr, int endMin)
{
CBBlueLightClient_Schedule_t schedule;
CBBlueLightClient *blueLightClient = nil;
bool retcode = false;
if (isNightShiftAvailable() != true)
{
fprintf(stderr, "error: %s\n", gStrErrNoNS);
return retcode;
}
if (isValidHourForNightShift(startHr) != true ||
isValidMinForNightShift(startMin) != true ||
isValidHourForNightShift(endHr) != true ||
isValidMinForNightShift(endMin) != true)
{
fprintf(stderr,
"error: %s: '%02d:%02d' to %02d:%02d'\n",
gStrErrNSBadSched,
startHr,
startMin,
endHr,
endMin);
return retcode;
}
schedule.from.hour = startHr;
schedule.from.minute = startMin;
schedule.to.hour = endHr;
schedule.to.minute = endMin;
blueLightClient = [[CBBlueLightClient alloc] init];
if (blueLightClient == nil)
{
fprintf(stderr, "error: %s\n", gStrErrNoNSClient);
return retcode;
}
if ([blueLightClient setSchedule: &schedule] != true)
{
fprintf(stderr, "error: %s\n", gStrErrNSMode);
[blueLightClient release];
return retcode;
}
retcode = [blueLightClient setMode: CBBlueLightClientModeCustomSchedule];
[blueLightClient release];
if (retcode != true)
{
fprintf(stderr, "error: %s\n", gStrErrNSMode);
}
return retcode;
}
/*
strToTimeComponents - if an argument is a valid time ([h]h:mm),
split it into its hour and minute components
*/
bool strToTimeComponents(const char *arg, int *hour, int *min)
{
NSString *argStr = nil, *hrStr = nil, *minStr = nil;
NSArray *argComponents = nil;
NSCharacterSet *notDigits = nil;
int hourRaw = 0, minRaw = 0;
if (arg == NULL || hour == NULL || min == NULL)
{
return false;
}
argStr = [[NSString alloc] initWithUTF8String: arg];
if (argStr == nil)
{
return false;
}
/*
if the argument is a valid time it should be in the form [h]h:mm,
so the array created by splitting the argument should only have
2 components.
*/
argComponents = [argStr componentsSeparatedByString:@":"];
if (argComponents == nil)
{
[argStr release];
return false;
}
/* invalid time if there are more than 2 substrings */
if ([argComponents count] != 2 ||
[[argComponents objectAtIndex: 0]
isKindOfClass:[NSString class]] != true ||
[[argComponents objectAtIndex: 1]
isKindOfClass:[NSString class]] != true)
{
[argComponents release];
[argStr release];
return false;
}
hrStr = [argComponents objectAtIndex: 0];
minStr = [argComponents objectAtIndex: 1];
/*
valid hour and min should contain only digits, the hour string should
be no longer than 2 numbers, and the min string should contains exactly
two numbers
based on: https://samplecodebank.blogspot.com/2013/06/NSCharacterSet-decimalDigitCharacterSet-example.html
*/
notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([hrStr isEqualToString: @""] ||
[hrStr length] > 2 ||
[hrStr rangeOfCharacterFromSet: notDigits].location != NSNotFound ||
[minStr isEqualToString: @""] ||
[minStr length] != 2 ||
[minStr rangeOfCharacterFromSet: notDigits].location != NSNotFound)
{
[notDigits release];
[argComponents release];
[argStr release];
return false;
}
hourRaw = [hrStr intValue];
minRaw = [minStr intValue];
[notDigits release];
[argComponents release];
[argStr release];
/* check if the hour and min are valid */
if (isValidHourForNightShift(hourRaw) != true ||
isValidMinForNightShift(minRaw) != true)
{
return false;
}
*hour = hourRaw;
*min = minRaw;
return true;
}