Конфигурационный файл для upstart добавлен
[zenbook-als.git] / main.cpp
1 #include <string.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <signal.h>
8 #include <stdio.h>
9
10 using namespace std;
11
12 volatile bool active = true;
13 volatile bool goneActive = false;
14 volatile bool goneInactive = false;
15
16 void sigHandler(int sig)
17 {
18     if(sig == SIGUSR1) {
19         active = !active;
20
21         if(active) {
22             goneActive = true;
23             goneInactive = false;
24         } else {
25             goneInactive = true;
26             goneActive = false;
27         }
28     }
29 }
30
31 void enableALS(bool enable) {
32     int fd = open("/proc/acpi/call", O_RDWR);
33     if(fd == -1) {
34         fprintf(stderr, "Error opening /proc/acpi/call");
35     }
36
37     char *buf;
38     if(enable) {
39         buf = "\\_SB.PCI0.LPCB.EC0.TALS 0x1";
40     } else {
41         buf = "\\_SB.PCI0.LPCB.EC0.TALS 0x0";
42     }
43
44     write(fd, buf, strlen(buf) + 1);
45
46     close(fd);
47 }
48
49 void setScreenBacklight(int percent) {
50     char cmd[100];
51     snprintf(cmd, 100, "echo %d > /sys/class/backlight/acpi_video0/brightness", percent);
52     system(cmd);
53 }
54
55 void setKeyboardBacklight(int percent) {
56     int value = 0;
57
58     if(percent <= 25) value = 0;
59     else if(percent <= 50) value = 1;
60     else if(percent <= 75) value = 2;
61     else if(percent <= 100) value = 3;
62
63     char cmd[150];
64     snprintf(cmd, 150, "echo %d > /sys/class/leds/asus::kbd_backlight/brightness", value);
65     system(cmd);
66 }
67
68 int getAmbientLightPercent() {
69     int fd = open("/proc/acpi/call", O_RDWR);
70     if(fd == -1) {
71         fprintf(stderr, "Error opening /proc/acpi/call");
72     }
73
74     char *buf;
75     buf = "\\\_SB.ALS._ALI";
76
77     write(fd, buf, strlen(buf) + 1);
78
79     char strals[100];
80     int count = read(fd, strals, 100);
81     strals[count] = '\0';
82     close(fd);
83
84     // 0x32 (min illuminance), 0xC8, 0x190, 0x258, 0x320 (max illuminance).
85     int als;
86     sscanf(strals,"%x",&als);
87
88     float percent = 0;
89
90     switch(als) {
91     case 0x32:
92         percent = 10;
93         break;
94     case 0xC8:
95         percent = 25;
96         break;
97     case 0x190:
98         percent = 50;
99         break;
100     case 0x258:
101         percent = 75;
102         break;
103     case 0x320:
104         percent = 100;
105         break;
106     }
107
108     return percent;
109 }
110
111 int main()
112 {
113     struct sigaction sa;
114     sa.sa_handler = sigHandler;
115     sigemptyset(&sa.sa_mask);
116     sa.sa_flags = 0;
117     sigaction(SIGUSR1, &sa, NULL);
118
119     enableALS(true);
120
121     while(1) {
122
123         while(!active) {
124             if(goneInactive) {
125                 enableALS(false);
126                 goneInactive = false;
127             }
128
129             if(!goneInactive && !goneActive)
130                 sleep(60*60*2);
131
132             if(goneActive) {
133                 enableALS(true);
134                 goneActive = false;
135             }
136         }
137
138         float als = getAmbientLightPercent();
139         printf("%f\%\n", als);
140         if(als <= 10) {
141             setScreenBacklight(40);
142             setKeyboardBacklight(100);
143         } else if(als <= 25) {
144             setScreenBacklight(60);
145             setKeyboardBacklight(0);
146         } else if(als <= 50) {
147             setScreenBacklight(75);
148             setKeyboardBacklight(0);
149         } else if(als <= 75) {
150             setScreenBacklight(90);
151             setKeyboardBacklight(0);
152         } else if(als <= 100) {
153             setScreenBacklight(100);
154             setKeyboardBacklight(0);
155         }
156
157         sleep(3);
158     }
159
160     return 0;
161 }