-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbacklightd.cpp
More file actions
122 lines (109 loc) · 2.85 KB
/
backlightd.cpp
File metadata and controls
122 lines (109 loc) · 2.85 KB
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
#include <errno.h>
#include <syslog.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/extensions/dpms.h>
#include <chrono>
#include <thread>
#include <csignal>
using namespace std;
using namespace std::chrono;
void signalHandler (int signum)
{
syslog(LOG_INFO,"backlightd: stopped");
exit(signum);
}
void backlight_on()
{
int fd = open("/sys/class/backlight/soc:backlight/bl_power", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/backlight/soc:backlight/bl_power");
exit(1);
}
if (write(fd, "0", 1) != 1) {
perror("Error writing to /sys/class/backlight/soc:backlight/bl_power");
exit(1);
}
close(fd);
syslog(LOG_INFO,"backlightd: set backlight on");
// fprintf(stderr, "Set backlight on\n");
}
void backlight_off()
{
int fd = open("/sys/class/backlight/soc:backlight/bl_power", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/backlight/soc:backlight/bl_power");
exit(1);
}
if (write(fd, "4", 1) != 1) {
perror("Error writing to /sys/class/backlight/soc:backlight/bl_power");
exit(1);
}
close(fd);
syslog(LOG_INFO,"backlightd: set backlight off");
// fprintf(stderr, "backlightd: set backlight off\n");
}
int main(int argc, char *argv[])
{
signal(SIGINT, signalHandler);
Display *dpy;
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
{
fprintf(stderr, "%s: unable to open display\n", argv[0]);
exit(EXIT_FAILURE);
}
BOOL prev = true;
BOOL onoff;
CARD16 state;
syslog(LOG_INFO,"backlightd: started");
backlight_on();
while(true)
{
DPMSInfo(dpy, &state, &onoff);
switch (onoff)
{
case false:
if (prev == false)
{
backlight_on();
prev=true;
}
break;
case true:
switch (state)
{
case DPMSModeOn:
if (prev == false)
{
backlight_on();
prev=true;
}
break;
case DPMSModeSuspend:
case DPMSModeStandby:
case DPMSModeOff:
if (prev == true)
{
backlight_off();
prev=false;
}
break;
default:
break;
}
break;
default:
break;
}
this_thread::sleep_for(milliseconds(200));
}
return 0;
}