-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.c
53 lines (42 loc) · 1.02 KB
/
lights.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>
#define DEVICE_FILE "/dev/input/event5"
#define LED_BRIGHTNESS "/sys/devices/soc/leds-qpnp-18/leds/button-backlight/brightness"
void lights() {
FILE *f = fopen(LED_BRIGHTNESS, "w");
if (f != NULL) {
fprintf(f, "40");
fclose(f);
sleep(1.5);
f = fopen(LED_BRIGHTNESS, "w");
if (f != NULL) {
fprintf(f, "0");
fclose(f);
}
}
}
int main() {
int fd = inotify_init();
if (fd < 0) {
printf("Error: inotify_init failed\n");
return 1;
}
int wd = inotify_add_watch(fd, DEVICE_FILE, IN_MODIFY);
if (wd < 0) {
printf("Error: inotify_add_watch failed\n");
return 1;
}
char buffer[sizeof(struct inotify_event) + 255];
ssize_t len;
while (1) {
len = read(fd, buffer, sizeof(buffer));
if (len > 0) {
lights();
}
}
inotify_rm_watch(fd, wd);
close(fd);
return 0;
}