-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard_monitor.cpp
More file actions
54 lines (51 loc) · 1.4 KB
/
keyboard_monitor.cpp
File metadata and controls
54 lines (51 loc) · 1.4 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
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<linux/input.h>
#include<string.h>
#include<stdio.h>
using namespace std;
static const char *const evval[3] = {
"RELEASED",
"PRESSED ",
"REPEATED"
};
int main(void){
const char *dev = {"/dev/input/by-path/platform-i8042-serio-0-event-kbd"};
char DellXps13_kbdbacklight[] = {"cd /sys/devices/platform/dell-laptop/leds/dell::kbd_backlight"};
struct input_event ev;
ssize_t n;
int fd;
fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
return EXIT_FAILURE;
}
while (1) {
n = read(fd, &ev, sizeof ev);
if(n == (ssize_t)-1){
if (errno == EINTR){
continue;
}else{
break;
}
}else if(n != sizeof ev){
errno = EIO;
break;
}
if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2){
printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);
if(ev.value == 1 || ev.value == 2){
system("echo 2 > /sys/devices/platform/dell-laptop/leds/dell::kbd_backlight/brightness");
continue;
}else{
sleep(1);
system("echo 0 > /sys/devices/platform/dell-laptop/leds/dell::kbd_backlight/brightness");
}
}
}
fflush(stdout);
fprintf(stderr, "%s.\n", strerror(errno));
return EXIT_FAILURE;
}