-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightnessd.c
1354 lines (1192 loc) · 59.2 KB
/
brightnessd.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
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2015 Christian Storm <Christian.Storm at tngtech dot com>
*
* 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.
*
*/
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <stdbool.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/screensaver.h>
#include <xcb/dpms.h>
#include <xcb/randr.h>
#ifdef DEBUGLOG
#define DEBUG(...) do { \
(void)fprintf(stderr, "%s", gs_color.green); \
(void)fprintf(stderr, "["PROGNAME"::DEBUG]" __VA_ARGS__); \
(void)fprintf(stderr, "%s", gs_color.reset); \
} while (0)
#else
#define DEBUG(...) do {} while (0)
#endif
#ifdef TRACELOG
#define TRACE(...) do { \
(void)fprintf(stderr, "%s", gs_color.gray); \
(void)fprintf(stderr, "["PROGNAME"::TRACE]" __VA_ARGS__); \
(void)fprintf(stderr, "%s", gs_color.reset); \
} while (0)
#else
#define TRACE(...) do {} while (0)
#endif
#define ERROR(...) do { \
(void)fprintf(stderr, "%s", gs_color.red); \
(void)fprintf(stderr, "["PROGNAME"] " __VA_ARGS__); \
(void)fprintf(stderr, "%s", gs_color.reset); \
} while (0)
#define WARN(...) do { \
(void)fprintf(stderr, "%s", gs_color.yellow); \
(void)fprintf(stderr, "["PROGNAME"] " __VA_ARGS__); \
(void)fprintf(stderr, "%s", gs_color.reset); \
} while (0)
#ifndef USE_SYSFS_BACKLIGHT_CONTROL
#define CC_IGNORE_WARNING_CAST_ALIGN \
_Pragma("clang diagnostic push" ) \
_Pragma("clang diagnostic ignored \"-Wcast-align\"") \
_Pragma("GCC diagnostic push" ) \
_Pragma("GCC diagnostic ignored \"-Wcast-align\"" )
#define CC_RESTORE_WARNINGS \
_Pragma("clang diagnostic push") \
_Pragma("GCC diagnostic push" )
#endif
#define NO_BRIGHTNESS -1
#define BRN_PRIORSCRSVR_UNDEFINED 0xff
#define RET_OK 0
///////////////////////////////////////////////////////////////////////////////
// configuration
///////////////////////////////////////////////////////////////////////////////
static uint8_t DIM_PERCENT_INTERVAL = 20;
static uint8_t DIM_PERCENT_TIMEOUT = 40;
///////////////////////////////////////////////////////////////////////////////
// types
///////////////////////////////////////////////////////////////////////////////
typedef enum {
STATE_DPMS_STANDBY,
STATE_DPMS_SUSPEND,
STATE_DPMS_OFF,
STATE_SCREENSAVER_ON_TIMEOUT,
STATE_SCREENSAVER_ON_INTERVAL,
STATE_SCREENSAVER_OFF,
STATE_SCREENSAVER_CYCLE,
STATE_SCREENSAVER_DISABLED,
STATE_UNKNOWN,
} state_t;
static struct Tcolor {
char* yellow;
char* red;
char* gray;
char* green;
char* reset;
} gs_color = {
.yellow = "\x1b[33m",
.red = "\x1b[1;31m",
.gray = "\x1b[1;30m",
.green = "\x1b[32m",
.reset = "\x1b[0m"
};
static struct Tglobalstate {
xcb_window_t screensaver_window;
uint16_t screensaver_timeout;
uint16_t screensaver_interval;
uint16_t dpms_standby_timeout;
uint16_t dpms_suspend_timeout;
uint16_t dpms_off_timeout;
uint16_t dpms_power_level;
uint32_t screensaver_idlesecuser;
uint32_t screensaver_idlesecserver;
uint8_t state;
uint8_t screensaver_blanking;
uint8_t screensaver_allow_exposures;
uint8_t screensaver_state;
uint8_t screensaver_kind;
uint8_t dpms_state;
char _padding[2];
} gs_globalstate;
static struct Teventstate {
uint8_t brn_cur_perc;
uint8_t brn_old_perc;
uint8_t brn_priorscrsvr_perc;
bool brn_interval_set;
} gs_eventstate = {
.brn_cur_perc = 0,
.brn_old_perc = 0,
.brn_priorscrsvr_perc = BRN_PRIORSCRSVR_UNDEFINED,
.brn_interval_set = false,
};
static struct Txcb {
xcb_connection_t *connection;
xcb_screen_t *screen;
xcb_window_t window;
xcb_pixmap_t pixmap;
xcb_atom_t backlight_atom;
xcb_atom_t backlight_new_atom;
xcb_atom_t backlight_legacy_atom;
int screen_nr;
xcb_intern_atom_reply_t *screensaver_id_atom;
uint8_t screensaver_id;
char _padding[7];
} gs_xcb = {
.connection = NULL,
.screen = NULL,
.screen_nr = 0,
.window = 0,
.pixmap = 0,
.screensaver_id_atom = NULL,
.screensaver_id = 0,
.backlight_atom = 0,
.backlight_new_atom = 0,
.backlight_legacy_atom = 0
};
typedef enum {
OPERATION_GETBRIGHTNESS,
OPERATION_SETBRIGHTNESS,
OPERATION_INCBRIGHTNESS,
OPERATION_DECBRIGHTNESS
} operations_t;
typedef enum {
OPERATION_SHUTDOWN_CONN,
OPERATION_SHUTDOWN_DEREGEVENT
} setup_operations_t;
///////////////////////////////////////////////////////////////////////////////
// forward declarations
///////////////////////////////////////////////////////////////////////////////
static void print_usage(void);
static void signal_handler(const int sig) __attribute__((noreturn));
static inline bool operation_handler(const operations_t operation, struct Txcb *pxcb, const uint8_t brn_percent, uint8_t *brn_cur_perc, uint8_t *brn_new_perc) __attribute__((always_inline));
void shutdown(const setup_operations_t operation);
bool query_state(struct Tglobalstate *state, const struct Txcb *pxcb);
bool query_state_screensaver(struct Tglobalstate *pglobalstate, const struct Txcb *pxcb);
bool query_state_dpms(struct Tglobalstate *pglobalstate, const struct Txcb *pxcb);
static int parse_uint8_t(char* input, uint8_t* output);
static int parse_args(int len, char** args);
#ifndef USE_SYSFS_BACKLIGHT_CONTROL
bool _operation_handler_randr(const operations_t operation, struct Txcb *pxcb, const uint8_t brn_percent, uint8_t *brn_cur_perc, uint8_t *brn_new_perc);
int32_t _get_brightness_randr(struct Txcb *pxcb, const xcb_randr_output_t output, const xcb_atom_t *backlight_atom);
int32_t get_brightness_randr(struct Txcb *pxcb, const xcb_randr_output_t output);
int8_t set_brightness_randr(const struct Txcb *pxcb, xcb_randr_output_t output, int32_t value);
#endif
#ifdef USE_SYSFS_BACKLIGHT_CONTROL
bool _operation_handler_file(const operations_t operation, const uint8_t brn_percent, uint8_t *brn_cur_perc, uint8_t *brn_new_perc);
int32_t get_brightness_file(const char* filename);
int8_t set_brightness_file(const char* filename, const int32_t value_abs);
static inline bool is_file_accessible(const char* filename, const int mode) __attribute__((always_inline));
#endif
///////////////////////////////////////////////////////////////////////////////
// is_file_accessible()
///////////////////////////////////////////////////////////////////////////////
/** Test whether a given file is accessible with a given access mode.
@param filename the absolute path to the file to be tested
@param mode R_OK, W_OK, X_OK, or F_OK
@return true on success or false on failure
@see man 3P access
*/
#ifdef USE_SYSFS_BACKLIGHT_CONTROL
static inline bool is_file_accessible(const char* filename, const int mode) {
if (access(filename, mode) == -1) {
ERROR("Error: cannot access file %s: %s", filename, strerror(errno) );
return false;
}
return true;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// shutdown()
///////////////////////////////////////////////////////////////////////////////
/** Perform cleanup and shutdown operations.
@param operation which shutdown operation to perform
@see setup_operations_t
*/
void shutdown(const setup_operations_t operation) {
switch(operation) {
case OPERATION_SHUTDOWN_CONN:
if (xcb_connection_has_error(gs_xcb.connection) > 0) {
ERROR("Error: xcb connection error while releasing xcb connection\n");
return;
}
DEBUG("[shutdown] releasing xcb connection\n");
(void)xcb_screensaver_unset_attributes(gs_xcb.connection, gs_xcb.screen->root);
if (gs_xcb.pixmap != 0) {
(void)xcb_free_pixmap(gs_xcb.connection, gs_xcb.pixmap);
}
if (gs_xcb.screensaver_id_atom) {
xcb_delete_property(gs_xcb.connection, gs_xcb.screen->root, gs_xcb.screensaver_id_atom->atom);
free(gs_xcb.screensaver_id_atom);
}
(void)xcb_flush(gs_xcb.connection);
xcb_disconnect(gs_xcb.connection);
(void)unsetenv("XSS_WINDOW");
(void)unsetenv("XSCREENSAVER_WINDOW");
return;
case OPERATION_SHUTDOWN_DEREGEVENT:
if (xcb_connection_has_error(gs_xcb.connection) > 0) {
ERROR("Error: xcb connection error while de-registering from screensaver events\n");
return;
}
DEBUG("[shutdown] unsubscribing from screensaver events\n");
(void)xcb_screensaver_select_input(gs_xcb.connection, gs_xcb.screen->root, 0);
return;
}
}
// callables for atexit() registration wrapping shutdown() with the appropriate operation arguments
static void shutdown_connection() { shutdown(OPERATION_SHUTDOWN_CONN); }
static void shutdown_deregister_events() { shutdown(OPERATION_SHUTDOWN_DEREGEVENT); }
///////////////////////////////////////////////////////////////////////////////
// query_state_screensaver()
///////////////////////////////////////////////////////////////////////////////
/** Query the current screensaver state.
@param pglobalstate state container struct
@param pxcb xcb container struct
@return true on successful screensaver query, false otherwise
@see Tglobalstate
@see Txcb
*/
bool query_state_screensaver(struct Tglobalstate *pglobalstate, const struct Txcb *pxcb) {
xcb_screensaver_query_info_cookie_t screensaver_query_info_cookie;
xcb_screensaver_query_info_reply_t *screensaver_query_info_reply;
screensaver_query_info_cookie = xcb_screensaver_query_info(pxcb->connection, pxcb->screen->root);
screensaver_query_info_reply = xcb_screensaver_query_info_reply(pxcb->connection, screensaver_query_info_cookie, NULL);
if (!screensaver_query_info_reply) { return false; }
pglobalstate->screensaver_idlesecuser = screensaver_query_info_reply->ms_since_user_input / 1000;
pglobalstate->screensaver_idlesecserver = screensaver_query_info_reply->ms_until_server / 1000;
pglobalstate->screensaver_state = screensaver_query_info_reply->state;
pglobalstate->screensaver_kind = screensaver_query_info_reply->kind;
pglobalstate->screensaver_window = screensaver_query_info_reply->saver_window;
free(screensaver_query_info_reply);
xcb_get_screen_saver_reply_t *get_screensaver_reply;
xcb_get_screen_saver_cookie_t get_screen_saver_cookie;
get_screen_saver_cookie = xcb_get_screen_saver(pxcb->connection);
get_screensaver_reply = xcb_get_screen_saver_reply(pxcb->connection, get_screen_saver_cookie, NULL);
if (!get_screensaver_reply) { return false; }
pglobalstate->screensaver_timeout = get_screensaver_reply->timeout;
pglobalstate->screensaver_interval = get_screensaver_reply->interval;
pglobalstate->screensaver_blanking = get_screensaver_reply->prefer_blanking;
pglobalstate->screensaver_allow_exposures = get_screensaver_reply->allow_exposures;
free(get_screensaver_reply);
TRACE("[query_state] scrsvr :: timeout=%us interval=%us idlesecUser=%ds idlesecSrv=%ds\n",
pglobalstate->screensaver_timeout,
pglobalstate->screensaver_interval,
pglobalstate->screensaver_idlesecuser,
pglobalstate->screensaver_idlesecserver
);
TRACE("[query_state] scrsvr :: blank=%s allow_exposure=%s kind=%s%s%s\n",
pglobalstate->screensaver_blanking ? "yes" : "no",
pglobalstate->screensaver_allow_exposures ? "yes" : "no",
pglobalstate->screensaver_kind == 0 ? "blanked" : "",
pglobalstate->screensaver_kind == 1 ? "internal" : "",
pglobalstate->screensaver_kind == 2 ? "external" : ""
);
if (!pglobalstate->screensaver_blanking) {
WARN("Warning: screensaver's prefer blanking mode is not enabled, blanking won't kick in!\n");
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
// query_state_dpms()
///////////////////////////////////////////////////////////////////////////////
/** Query the current dpms state.
@param pglobalstate state container struct
@param pxcb xcb container struct
@return true on successful dpms query, false otherwise
@see Tglobalstate
@see Txcb
*/
bool query_state_dpms(struct Tglobalstate *pglobalstate, const struct Txcb *pxcb) {
xcb_dpms_get_timeouts_cookie_t dpms_get_timeouts_cookie;
xcb_dpms_get_timeouts_reply_t *dpms_get_timeouts_reply;
xcb_generic_error_t *error = NULL;
dpms_get_timeouts_cookie = xcb_dpms_get_timeouts_unchecked(pxcb->connection);
dpms_get_timeouts_reply = xcb_dpms_get_timeouts_reply(pxcb->connection, dpms_get_timeouts_cookie, &error);
if (!dpms_get_timeouts_reply) { return false; }
pglobalstate->dpms_standby_timeout = dpms_get_timeouts_reply->standby_timeout;
pglobalstate->dpms_suspend_timeout = dpms_get_timeouts_reply->suspend_timeout;
pglobalstate->dpms_off_timeout = dpms_get_timeouts_reply->off_timeout;
free(dpms_get_timeouts_reply);
xcb_dpms_info_cookie_t dpms_info_cookie;
xcb_dpms_info_reply_t *dpms_info_reply;
dpms_info_cookie = xcb_dpms_info(pxcb->connection);
dpms_info_reply = xcb_dpms_info_reply(pxcb->connection, dpms_info_cookie, &error);
if (!dpms_info_reply) { return false; }
pglobalstate->dpms_state = dpms_info_reply->state;
pglobalstate->dpms_power_level = dpms_info_reply->power_level;
free(dpms_info_reply);
if (pglobalstate->dpms_standby_timeout == 0) {
WARN("Warning: dpms's standby timeout is 0 (=disabled), won't go into dpms standby mode!\n");
}
if (pglobalstate->dpms_standby_timeout == 0) {
WARN("Warning: dpms's suspend timeout is 0 (=disabled), won't go into dpms suspend mode!\n");
}
if (pglobalstate->dpms_standby_timeout == 0) {
WARN("Warning: dpms's off timeout is 0 (=disabled), won't go into dpms off mode!\n");
}
TRACE("[query_state] dpms :: status=%s standby=%us suspend=%us off=%us\n",
pglobalstate->dpms_state ? "on" : "off",
pglobalstate->dpms_standby_timeout,
pglobalstate->dpms_suspend_timeout,
pglobalstate->dpms_off_timeout
);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// query_state()
///////////////////////////////////////////////////////////////////////////////
/** Aggregate the current screensaver and dpms state.
@param pglobalstate state container struct
@param pxcb xcb container struct
@return true on successful query aggregation, false otherwise
@see Tglobalstate
@see Txcb
@see query_state_dpms
@see query_state_screensaver
*/
bool query_state(struct Tglobalstate *pglobalstate, const struct Txcb *pxcb) {
if (xcb_connection_has_error(pxcb->connection) > 0) {
ERROR("Error: xcb connection error while querying screensaver and dpms state\n");
return false;
}
if (!query_state_dpms(pglobalstate, pxcb) ) { return false; }
if (!query_state_screensaver(pglobalstate, pxcb)) { return false; }
#define SET_STATE(STATE) \
do { \
pglobalstate->state = STATE; \
TRACE("[query_state] state :: "#STATE"\n"); \
} while (0)
switch (pglobalstate->screensaver_state) {
case XCB_SCREENSAVER_STATE_OFF:
SET_STATE(STATE_SCREENSAVER_OFF); break;
case XCB_SCREENSAVER_STATE_ON:
switch (pglobalstate->dpms_power_level) {
case XCB_DPMS_DPMS_MODE_ON:
if (pglobalstate->screensaver_idlesecuser == pglobalstate->screensaver_timeout) {
SET_STATE(STATE_SCREENSAVER_ON_TIMEOUT);
} else {
SET_STATE(STATE_SCREENSAVER_ON_INTERVAL);
}
break;
case XCB_DPMS_DPMS_MODE_STANDBY:
SET_STATE(STATE_DPMS_STANDBY); break;
case XCB_DPMS_DPMS_MODE_SUSPEND:
SET_STATE(STATE_DPMS_SUSPEND); break;
case XCB_DPMS_DPMS_MODE_OFF:
SET_STATE(STATE_DPMS_OFF); break;
default:
SET_STATE(STATE_UNKNOWN); break;
}
break;
case XCB_SCREENSAVER_STATE_CYCLE:
SET_STATE(STATE_SCREENSAVER_CYCLE); break;
case XCB_SCREENSAVER_STATE_DISABLED:
SET_STATE(STATE_SCREENSAVER_DISABLED); break;
default:
SET_STATE(STATE_UNKNOWN); break;
}
#undef SET_STATE
return true;
}
///////////////////////////////////////////////////////////////////////////////
// _get_brightness_randr()
///////////////////////////////////////////////////////////////////////////////
/** Helper function returning the device-specific brightness of a given output.
@param pxcb xcb container struct
@param output the output to get the brightness for
@param backlight_atom the backlight property atom to read the brightness from
@return the *absolute* brightness value in the output's device-specific range, or NO_BRIGHTNESS on error
@see Txcb
@see NO_BRIGHTNESS
@see get_brightness_randr
*/
#ifndef USE_SYSFS_BACKLIGHT_CONTROL
int32_t _get_brightness_randr(struct Txcb *pxcb, const xcb_randr_output_t output, const xcb_atom_t *backlight_atom) {
xcb_randr_get_output_property_reply_t *output_poperty_reply = NULL;
xcb_randr_get_output_property_cookie_t output_poperty_cookie;
xcb_generic_error_t *error = NULL;
pxcb->backlight_atom = *backlight_atom;
if (pxcb->backlight_atom != XCB_ATOM_NONE) {
output_poperty_cookie = xcb_randr_get_output_property(pxcb->connection, output, pxcb->backlight_atom, XCB_ATOM_NONE, 0, 4, 0, 0);
output_poperty_reply = xcb_randr_get_output_property_reply(pxcb->connection, output_poperty_cookie, &error);
if (error != NULL || output_poperty_reply == NULL) {
TRACE("[get_brightness_randr] error %u while querying brightness of output %d on backlight %d\n", error->error_code, output, pxcb->backlight_atom);
return NO_BRIGHTNESS;
}
if (output_poperty_reply->type != XCB_ATOM_INTEGER || output_poperty_reply->num_items != 1 || output_poperty_reply->format != 32) {
free(output_poperty_reply);
return NO_BRIGHTNESS;
}
CC_IGNORE_WARNING_CAST_ALIGN
int32_t value_abs = *((int32_t *) xcb_randr_get_output_property_data(output_poperty_reply));
CC_RESTORE_WARNINGS
free(output_poperty_reply);
TRACE("[get_brightness_randr] brightness_abs=%d [output: %d][backlight: %d]\n", value_abs, output, pxcb->backlight_atom);
return value_abs;
}
TRACE("[get_brightness_randr] backlight is XCB_ATOM_NONE [output: %d][backlight: %d]\n", output, pxcb->backlight_atom);
return NO_BRIGHTNESS;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// get_brightness_randr()
///////////////////////////////////////////////////////////////////////////////
/** Get the brightness of a given output as device-specific absolute value.
@param pxcb xcb container struct
@param output the output to get the brightness for
@return the *absolute* brightness value in the output's device-specific range, or NO_BRIGHTNESS on error
@see Txcb
@see NO_BRIGHTNESS
@see _get_brightness_randr
*/
#ifndef USE_SYSFS_BACKLIGHT_CONTROL
int32_t get_brightness_randr(struct Txcb *pxcb, const xcb_randr_output_t output) {
int32_t value = _get_brightness_randr(pxcb, output, &pxcb->backlight_new_atom);
if (value == NO_BRIGHTNESS) {
value = _get_brightness_randr(pxcb, output, &pxcb->backlight_legacy_atom);
}
return value;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// set_brightness_randr()
///////////////////////////////////////////////////////////////////////////////
/** Set the brightness of a given output to a device-specific absolute value.
@param pxcb xcb container struct
@param output the output to set the brightness for
@param value_abs the *absolute* brightness value in the output's device-specific range
@return RET_OK, or NO_BRIGHTNESS on error
@see Txcb
@see NO_BRIGHTNESS
@see RET_OK
*/
#ifndef USE_SYSFS_BACKLIGHT_CONTROL
int8_t set_brightness_randr(const struct Txcb *pxcb, const xcb_randr_output_t output, int32_t value_abs) {
xcb_void_cookie_t xcb_void_cookie;
xcb_generic_error_t *xcb_generic_error;
TRACE("[set_brightness_randr] setting brightness_abs to %d [output: %d]\n", value_abs, output);
xcb_void_cookie = xcb_randr_change_output_property(pxcb->connection, output, pxcb->backlight_atom, XCB_ATOM_INTEGER, 32, XCB_PROP_MODE_REPLACE, 1, (unsigned char *)&value_abs);
if ( (xcb_generic_error = xcb_request_check(pxcb->connection, xcb_void_cookie)) ) {
ERROR("Error: cannot set brightness. Exiting.\n");
return NO_BRIGHTNESS;
}
return RET_OK;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// get_brightness_file()
///////////////////////////////////////////////////////////////////////////////
/** Get the brightness of an output from a file as device-specific absolute value.
@param filename the absolute path to the file the brightness value is read from
@return the *absolute* brightness value in the output's device-specific range, or NO_BRIGHTNESS on error
@see NO_BRIGHTNESS
*/
#ifdef USE_SYSFS_BACKLIGHT_CONTROL
int32_t get_brightness_file(const char* filename) {
FILE *file;
int32_t brightness;
if ((file = fopen(filename, "r"))) {
if (EOF == fscanf(file, "%u", &brightness)) {
ERROR("Error: cannot read file %s (%s)\n", filename, strerror(errno));
return NO_BRIGHTNESS;
}
if (fclose(file) != 0) {
ERROR("Error: cannot close file %s (%s)\n", filename, strerror(errno));
return NO_BRIGHTNESS;
}
TRACE("[get_brightness_file] brightness_abs=%d\n", brightness);
return brightness;
}
ERROR("Error: cannot open file %s for reading (%s)\n", filename, strerror(errno));
return NO_BRIGHTNESS;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// set_brightness_file()
///////////////////////////////////////////////////////////////////////////////
/** Set the brightness of an output by a file to a device-specific absolute value.
@param filename the absolute path to the file the brightness value is read from
@param value_abs the *absolute* brightness value in the output's device-specific range
@return RET_OK, or NO_BRIGHTNESS on error
@see NO_BRIGHTNESS
@see RET_OK
*/
#ifdef USE_SYSFS_BACKLIGHT_CONTROL
int8_t set_brightness_file(const char* filename, const int32_t value_abs) {
FILE *file;
if ((file = fopen(filename, "w"))) {
int8_t retcode = RET_OK;
if (fprintf(file, "%u", value_abs) < 0) {
ERROR("Error: cannot write file %s (%s)\n", filename, strerror(errno));
retcode = NO_BRIGHTNESS;
}
if (fclose(file) != 0) {
ERROR("Error: cannot close file %s (%s)\n", filename, strerror(errno));
retcode = NO_BRIGHTNESS;
}
return retcode;
}
ERROR("Error: cannot open file %s for writing (%s)", filename, strerror(errno));
return NO_BRIGHTNESS;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// _operation_handler_randr()
///////////////////////////////////////////////////////////////////////////////
/** Provides set/get/increase/decrease brightness operations using xrandr.
@param operation the brightness operation to perform
@param pxcb the global xcb container struct
@param brn_percent brightness percentage to set/increase/decrease depending on `operation`
@param brn_cur_perc the current brightness as percentage
@param brn_new_perc the new brightness as percentage
@return true if operation could be performed, false on an unrecoverable error
@see operations_t
@see Txcb
*/
#ifndef USE_SYSFS_BACKLIGHT_CONTROL
bool _operation_handler_randr(const operations_t operation, struct Txcb *pxcb, const uint8_t brn_percent, uint8_t *brn_cur_perc, uint8_t *brn_new_perc) {
bool output_found = false;
xcb_generic_error_t *error;
xcb_randr_output_t *outputs;
xcb_randr_get_screen_resources_reply_t *resources_reply;
xcb_randr_get_screen_resources_cookie_t resources_cookie;
resources_cookie = xcb_randr_get_screen_resources(pxcb->connection, pxcb->screen->root);
resources_reply = xcb_randr_get_screen_resources_reply(pxcb->connection, resources_cookie, &error);
if (error != NULL || resources_reply == NULL) {
ERROR("Error: randr Get Screen Resources returned error %d\n", error ? error->error_code : -1);
return false;
}
outputs = xcb_randr_get_screen_resources_outputs(resources_reply);
for (uint16_t o = 0; o < resources_reply->num_outputs; o++) {
int32_t brn_cur_abs = get_brightness_randr(pxcb, outputs[o]);
if (brn_cur_abs != NO_BRIGHTNESS) {
output_found = true;
xcb_randr_query_output_property_cookie_t prop_cookie;
xcb_randr_query_output_property_reply_t *prop_reply;
prop_cookie = xcb_randr_query_output_property(pxcb->connection, outputs[o], pxcb->backlight_atom);
prop_reply = xcb_randr_query_output_property_reply(pxcb->connection, prop_cookie, &error);
if (error != NULL || prop_reply == NULL) {
TRACE("[operation_handler] error %u while querying output property, continuing to next display\n", error->error_code);
continue;
}
if (prop_reply->range && xcb_randr_query_output_property_valid_values_length(prop_reply) == 2 ) {
int32_t *values = xcb_randr_query_output_property_valid_values(prop_reply);
free(prop_reply);
int32_t brn_min_abs = values[0];
int32_t brn_max_abs = values[1];
int32_t brn_new_abs = brn_percent * (brn_max_abs - brn_min_abs) / 100;
*brn_cur_perc = (uint8_t) ((brn_cur_abs - brn_min_abs) * 100 / (brn_max_abs - brn_min_abs));
*brn_new_perc = *brn_cur_perc;
switch (operation) {
case OPERATION_GETBRIGHTNESS:
TRACE("[operation_handler] OPERATION_GETBRIGHTNESS\n");
TRACE("[operation_handler] min_abs:%d <= cur_abs:%d <= max_abs:%d\n", brn_min_abs, brn_cur_abs, brn_max_abs);
free(resources_reply);
return true;
case OPERATION_SETBRIGHTNESS:
brn_new_abs = brn_min_abs + brn_new_abs;
TRACE("[operation_handler] OPERATION_SETBRIGHTNESS -> %d (abs)\n", brn_new_abs);
break;
case OPERATION_INCBRIGHTNESS:
brn_new_abs = brn_cur_abs + brn_new_abs;
TRACE("[operation_handler] OPERATION_INCBRIGHTNESS -> %d (abs)\n", brn_new_abs);
break;
case OPERATION_DECBRIGHTNESS:
brn_new_abs = brn_cur_abs - brn_new_abs;
TRACE("[operation_handler] OPERATION_DECBRIGHTNESS -> %d (abs)\n", brn_new_abs);
}
if (brn_new_abs > brn_max_abs) { brn_new_abs = brn_max_abs; }
if (brn_new_abs < brn_min_abs) { brn_new_abs = brn_min_abs; }
*brn_new_perc = (uint8_t) (brn_new_abs * 100 / (brn_max_abs - brn_min_abs));
TRACE("[operation_handler] min_abs:%d <= cur_abs:%d -> new_abs:%d <= max_abs:%d\n", brn_min_abs, brn_cur_abs, brn_new_abs, brn_max_abs);
TRACE("[operation_handler] cur_perc:%d -> new_perc:%d\n", *brn_cur_perc, *brn_new_perc);
(void)set_brightness_randr(pxcb, outputs[o], brn_new_abs);
xcb_flush(pxcb->connection);
} else {
free(prop_reply);
}
}
}
free(resources_reply);
if (!output_found) {
ERROR("Error: Couldn't get brightness for any output.\n");
}
return output_found;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// _operation_handler_file()
///////////////////////////////////////////////////////////////////////////////
/** Provides set/get/increase/decrease brightness operations using sysfs files.
@param operation the brightness operation to perform
@param brn_percent brightness percentage to set/increase/decrease depending on `operation`
@param brn_cur_perc the current brightness as percentage
@param brn_new_perc the new brightness as percentage
@return true if operation could be performed, false on an unrecoverable error
@see operations_t
*/
#ifdef USE_SYSFS_BACKLIGHT_CONTROL
bool _operation_handler_file(const operations_t operation, const uint8_t brn_percent, uint8_t *brn_cur_perc, uint8_t *brn_new_perc) {
int32_t brn_min_abs = 0;
int32_t brn_max_abs = 0;
int32_t brn_cur_abs = 0;
if ( NO_BRIGHTNESS == (brn_cur_abs = get_brightness_file(SYSFS_BACKLIGHT_PATH "brightness")) ) {
ERROR("Error: Couldn't get current brightness for output.\n");
return false;
}
if ( NO_BRIGHTNESS == (brn_max_abs = get_brightness_file(SYSFS_BACKLIGHT_PATH "max_brightness")) ) {
ERROR("Error: Couldn't get maximal brightness for output.\n");
return false;
}
int32_t brn_new_abs = brn_percent * (brn_max_abs - brn_min_abs) / 100;
*brn_cur_perc = (uint8_t) ((brn_cur_abs - brn_min_abs) * 100 / (brn_max_abs - brn_min_abs));
*brn_new_perc = *brn_cur_perc;
switch (operation) {
case OPERATION_GETBRIGHTNESS:
TRACE("[operation_handler] OPERATION_GETBRIGHTNESS\n");
TRACE("[operation_handler] min_abs:%d <= cur_abs:%d <= max_abs:%d\n", brn_min_abs, brn_cur_abs, brn_max_abs);
return true;
case OPERATION_SETBRIGHTNESS:
brn_new_abs = brn_min_abs + brn_new_abs;
TRACE("[operation_handler] OPERATION_SETBRIGHTNESS -> %d (abs)\n", brn_new_abs);
break;
case OPERATION_INCBRIGHTNESS:
brn_new_abs = brn_cur_abs + brn_new_abs;
TRACE("[operation_handler] OPERATION_INCBRIGHTNESS -> %d (abs)\n", brn_new_abs);
break;
case OPERATION_DECBRIGHTNESS:
brn_new_abs = brn_cur_abs - brn_new_abs;
TRACE("[operation_handler] OPERATION_DECBRIGHTNESS -> %d (abs)\n", brn_new_abs);
}
if (brn_new_abs > brn_max_abs) { brn_new_abs = brn_max_abs; }
if (brn_new_abs < brn_min_abs) { brn_new_abs = brn_min_abs; }
*brn_new_perc = (uint8_t) (brn_new_abs * 100 / (brn_max_abs - brn_min_abs));
TRACE("[operation_handler] min_abs:%d <= cur_abs:%d -> new_abs:%d <= max_abs:%d\n", brn_min_abs, brn_cur_abs, brn_new_abs, brn_max_abs);
TRACE("[operation_handler] cur_perc:%d -> new_perc:%d\n", *brn_cur_perc, *brn_new_perc);
(void)set_brightness_file(SYSFS_BACKLIGHT_PATH "brightness", brn_new_abs);
return true;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// operation_handler()
///////////////////////////////////////////////////////////////////////////////
/** Wrapper function calling the sysfs files backend or the xrandr backend variant.
@param operation the brightness operation to perform
@param pxcb the global xcb container struct
@param brn_percent brightness percentage to set/increase/decrease depending on `operation`
@param brn_cur_perc the current brightness as percentage
@param brn_new_perc the new brightness as percentage
@return true if operation could be performed, false on an unrecoverable error
@see _operation_handler_file
@see _operation_handler_randr
*/
static inline bool operation_handler(const operations_t operation, struct Txcb *pxcb, const uint8_t brn_percent, uint8_t *brn_cur_perc, uint8_t *brn_new_perc){
#ifdef USE_SYSFS_BACKLIGHT_CONTROL
(void)pxcb;
return _operation_handler_file(operation, brn_percent, brn_cur_perc, brn_new_perc);
#else
return _operation_handler_randr(operation, pxcb, brn_percent, brn_cur_perc, brn_new_perc);
#endif
}
///////////////////////////////////////////////////////////////////////////////
// signal_handler()
///////////////////////////////////////////////////////////////////////////////
/** Signal Handler initiating a proper shutdown when being interrupted or killed.
@param sig the signal number received
*/
static void signal_handler(const int sig) {
switch(sig){
case SIGTERM:
case SIGINT:
case SIGQUIT:
DEBUG("[signal_handler] received SIG_TERM/SIG_QUIT, exiting\n");
exit(EXIT_SUCCESS);
}
DEBUG("[signal_handler] received unhandled signal %d.\n", sig);
exit(EXIT_FAILURE);
}
///////////////////////////////////////////////////////////////////////////////
// _event_loop_scrsvr_on_timeout()
///////////////////////////////////////////////////////////////////////////////
/** Helper function to `event_loop()` handling the screensaver `timeout` event.
@param pxcb the global xcb container struct
@param peventstate event loop brightness state container struct
@return RET_OK on success, failure exit code on error (e.g, EXIT_FAILURE)
@see event_loop
@see RET_OK
*/
static uint8_t _event_loop_scrsvr_on_timeout(struct Txcb *pxcb, struct Teventstate *peventstate) {
if (!operation_handler(OPERATION_GETBRIGHTNESS, pxcb, 0, &peventstate->brn_priorscrsvr_perc , &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to get brightness on screensaver timeout. Exiting.\n");
return EXIT_FAILURE;
}
if (peventstate->brn_cur_perc == 0) {
DEBUG("[eventloop] brightness is 0%% on timeout, setting 100%% brightness\n");
if (!operation_handler(OPERATION_SETBRIGHTNESS, pxcb, 100, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to set initial brightness to 100%% on screensaver timeout. Exiting.\n");
return EXIT_FAILURE;
}
DEBUG("[eventloop] backlight reports brightness %d%% set\n", peventstate->brn_cur_perc);
if (peventstate->brn_cur_perc == 0) {
DEBUG("[eventloop] brightness is still 0%%, setting 100%% brightness\n");
if (!operation_handler(OPERATION_SETBRIGHTNESS, pxcb, 100, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to set brightness to 100%% on screensaver timeout again. Exiting.\n");
return EXIT_FAILURE;
}
DEBUG("[eventloop] backlight reports brightness %d%% set\n", peventstate->brn_cur_perc);
}
}
if (peventstate->brn_cur_perc < DIM_PERCENT_TIMEOUT) {
DEBUG("[eventloop] current brightness %d%% is below target brightness of %d%%, doing nothing.\n", peventstate->brn_cur_perc, DIM_PERCENT_TIMEOUT);
return RET_OK;
}
if (!operation_handler(OPERATION_SETBRIGHTNESS, pxcb, DIM_PERCENT_TIMEOUT, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to decrease brightness on screensaver timeout. Exiting.\n");
return EXIT_FAILURE;
}
DEBUG("[eventloop] brightness %d%% -> %d%%\n", peventstate->brn_priorscrsvr_perc, peventstate->brn_cur_perc);
return RET_OK;
}
///////////////////////////////////////////////////////////////////////////////
// _event_loop_scrsvr_on_interval()
///////////////////////////////////////////////////////////////////////////////
/** Helper function to `event_loop()` handling the screensaver `interval` event.
@param pxcb the global xcb container struct
@param peventstate event loop brightness state container struct
@return RET_OK on success, failure exit code on error (e.g, EXIT_FAILURE)
@see event_loop
@see RET_OK
*/
static uint8_t _event_loop_scrsvr_on_interval(struct Txcb *pxcb, struct Teventstate *peventstate) {
if (!peventstate->brn_interval_set) {
peventstate->brn_interval_set = true;
if (peventstate->brn_cur_perc < DIM_PERCENT_INTERVAL) {
DEBUG("[eventloop] current brightness %d%% is below target brightness of %d%%, doing nothing.\n", peventstate->brn_cur_perc, DIM_PERCENT_INTERVAL);
return RET_OK;
}
if (!operation_handler(OPERATION_SETBRIGHTNESS, pxcb, DIM_PERCENT_INTERVAL, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to decrease brightness on screensaver interval. Exiting.\n");
return EXIT_FAILURE;
}
DEBUG("[eventloop] brightness %d%% -> %d%%\n", peventstate->brn_old_perc, peventstate->brn_cur_perc);
return RET_OK;
}
DEBUG("[eventloop] brightness already set to %d%%\n", peventstate->brn_cur_perc);
return RET_OK;
}
///////////////////////////////////////////////////////////////////////////////
// _event_loop_scrsvr_off()
///////////////////////////////////////////////////////////////////////////////
/** Helper function to `event_loop()` handling the screensaver turn off event.
@param pxcb the global xcb container struct
@param peventstate event loop brightness state container struct
@return RET_OK on success, failure exit code on error (e.g, EXIT_FAILURE)
@see event_loop
@see RET_OK
*/
static uint8_t _event_loop_scrsvr_off(struct Txcb *pxcb, struct Teventstate *peventstate) {
peventstate->brn_interval_set = false;
if (peventstate->brn_priorscrsvr_perc == BRN_PRIORSCRSVR_UNDEFINED) {
DEBUG("[eventloop] event: OFF received without being called on timeout or interval, not setting brightness\n");
DEBUG("[eventloop] getting current brightness\n");
if (!operation_handler(OPERATION_GETBRIGHTNESS, pxcb, 0, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to get brightness while setting screensaver OFF. Exiting.\n");
return EXIT_FAILURE;
}
return RET_OK;
}
if (!operation_handler(OPERATION_GETBRIGHTNESS, pxcb, 0, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
ERROR("Error: Failed to get brightness while setting screensaver OFF. Exiting.\n");
return EXIT_FAILURE;
}
if (peventstate->brn_cur_perc == 0) {
DEBUG("[eventloop] brightness is 0%% on setting OFF screensaver, setting to sane 100%% brightness\n");
peventstate->brn_priorscrsvr_perc = 100;
}
if (operation_handler(OPERATION_SETBRIGHTNESS, pxcb, peventstate->brn_priorscrsvr_perc, &peventstate->brn_old_perc, &peventstate->brn_cur_perc)) {
DEBUG("[eventloop] set to previous brightness %d%% from %d%%\n", peventstate->brn_priorscrsvr_perc, peventstate->brn_old_perc);
} else {
ERROR("Error: Failed to set prior brightness while setting screensaver OFF. Exiting.\n");
return EXIT_FAILURE;
}
peventstate->brn_priorscrsvr_perc = BRN_PRIORSCRSVR_UNDEFINED;
return RET_OK;
}
///////////////////////////////////////////////////////////////////////////////
// event_loop()
///////////////////////////////////////////////////////////////////////////////
/** The event loop handling screensaver-related events.
The event loop is an indefinite loop only interrupted by errors or signals,
hence the return code is propagated to exit() upon returning.
For brevity, it calls several helper functions to do the actual work, namely
* _event_loop_scrsvr_on_timeout called when getting the `timeout` event
* _event_loop_scrsvr_on_interval called when getting the `interval` event
* _event_loop_scrsvr_off called when the screensaver should turn off
@param pglobalstate state container struct
@param pxcb xcb container struct
@param peventstate event loop brightness state container struct
@return failure code on error (e.g, EXIT_FAILURE) propagated to exit()
@see Tglobalstate
@see Txcb
@see Teventstate
@see signal_handler
@see _event_loop_scrsvr_on_timeout
@see _event_loop_scrsvr_on_interval
@see _event_loop_scrsvr_off
*/
static uint8_t event_loop(struct Tglobalstate *pglobalstate, struct Txcb *pxcb, struct Teventstate *peventstate) {
xcb_generic_event_t *event_generic;
uint8_t result;
while (true) {
if (xcb_connection_has_error(pxcb->connection)) {
ERROR("Error: xcb connection error while waiting for events\n");
return EXIT_FAILURE;
}
event_generic = xcb_wait_for_event(pxcb->connection);
if (XCB_EVENT_RESPONSE_TYPE(event_generic) != pxcb->screensaver_id) {
free(event_generic);
continue;
}
free(event_generic);
if (!query_state(pglobalstate, pxcb)) {
ERROR("Error: cannot query screensaver/dpms settings. Exiting.\n");
return EXIT_FAILURE;
}
switch (pglobalstate->state) {
case STATE_SCREENSAVER_ON_TIMEOUT:
DEBUG("[eventloop] handling event: ON (timeout) [idle=%ds]\n", pglobalstate->screensaver_idlesecuser);