]> Pileus Git - ~andy/hwkeys/blob - hwkeys.c
Add initial hwkeys program.
[~andy/hwkeys] / hwkeys.c
1 #include <math.h>
2 #include <stdio.h>
3
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <linux/input.h>
9
10 static void screenlight(int up)
11 {
12         static const char *path = "/sys/class/backlight/gmux_backlight/brightness";
13         static int    prev, next, tmp;
14         static double val;
15         static FILE  *fd;
16
17         // Open brightness file
18         if (fd == NULL)
19                 fd = fopen(path, "r+");
20         if (fd == NULL) {
21                 printf("error opening file\n");
22                 return;
23         }
24
25         // Reset if someone else changed the value
26         fseek(fd, 0, SEEK_SET);
27         fscanf(fd, "%d", &tmp);
28         if (tmp != prev)
29                 val = pow(tmp/1024.0, 1/10.0);
30         //if (tmp != prev)
31         //      printf("convert: %d -> %d -> %f\n",
32         //                      prev, tmp, val);
33
34         // Update the current value
35         val += 0.01 * (up?1:-1);
36         if (val < 0) val = 0;
37         if (val > 1) val = 1;
38
39         // Write it back to the file
40         next = pow(val, 10) * 1024;
41         if (next == prev)
42                 next += (up?1:-1);
43         else
44                 prev = next;
45
46         //printf("saving:  %d\n", next);
47         fprintf(fd, "%d", next);
48         fflush(fd);
49 }
50
51 static void keylight(int up)
52 {
53         //static const char *path = "/sys/class/leds/smc::kbd_backlight/brightness";
54 }
55
56 int main(int argc, char **argv)
57 {
58         struct input_event ev;
59         int fd, len, debug=0;
60
61         //if ((fd = open("/dev/input/event4", O_RDONLY)) < 0) {
62         //if ((fd = open("/dev/input/event6", O_RDONLY)) < 0) {
63         if ((fd = open("/dev/input/event10", O_RDONLY)) < 0) {
64                 printf("Error\n");
65                 return 0;
66         }
67
68         while ((len = read(fd, &ev, sizeof(ev)))) {
69                 if (len != sizeof(ev)) {
70                         printf("Error\n");
71                         sleep(1);
72                         continue;
73                 }
74
75                 if (debug) {
76                         printf("%ld.%06lu: type=%04hx code=%04hx val=%08x\n",
77                                         ev.time.tv_sec,
78                                         ev.time.tv_usec,
79                                         ev.type, ev.code, ev.value);
80                         fflush(stdout);
81                 }
82
83                 if (ev.type != EV_KEY)
84                         continue;
85                 if (ev.value == 0) // key up
86                         continue;
87
88                 switch (ev.code) {
89                         case KEY_BRIGHTNESSDOWN:
90                                 screenlight(0);
91                                 break;
92                         case KEY_BRIGHTNESSUP:
93                                 screenlight(1);
94                                 break;
95                         case KEY_KBDILLUMDOWN:
96                                 keylight(0);
97                                 break;
98                         case KEY_KBDILLUMUP:
99                                 keylight(1);
100                                 break;
101                 }
102                 fflush(stdout);
103         }
104         return 0;
105 }