-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
100 lines (74 loc) · 1.91 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <linux/input.h>
#include <fcntl.h>
char *get_apple_magic_keyboard() {
struct dirent **namelist;
int n = scandir("/dev/input/by-id/", &namelist, NULL, alphasort);
if (n == -1) {
return NULL;
}
char *magic_keyboard_path = NULL;
while (n--) {
const char *name = namelist[n]->d_name;
if (strstr(name, "Magic_Keyboard") != NULL && magic_keyboard_path == NULL) {
magic_keyboard_path = strdup(name);
}
free(namelist[n]);
}
free(namelist);
if (!magic_keyboard_path) {
return NULL;
}
char *buffer = malloc(300);
if (!buffer) {
fprintf(stderr, "malloc() failed.\n");
exit(1);
}
if (0 > snprintf(buffer, 300, "/dev/input/by-id/%s", magic_keyboard_path)) {
fprintf(stderr, "snprintf() failed.\n");
exit(1);
}
free(magic_keyboard_path);
printf("Found apple magic keyboard at: %s\n", buffer);
return buffer;
}
#define DELAY() do { usleep(100 * 1E3); } while (0)
int main(void) {
while (true) {
char *device_path = get_apple_magic_keyboard();
if (!device_path) {
DELAY();
continue;
}
printf("Found apple magic keyboard at %s\n", device_path);
int keyboard_fd = open(device_path, O_RDONLY);
if (0 > keyboard_fd) {
free(device_path);
fprintf(stderr, "Failed to open device_path.\n");
DELAY();
continue;
}
printf("Successfully opened device path.\n");
while (true) {
struct input_event event;
int bytesRead = read(keyboard_fd, &event, sizeof(event));
if (0 > bytesRead) {
fprintf(stderr, "Failed to read bytes from device path.\n");
break;
}
if (event.type == EV_KEY && event.value == 1) {
if (event.code == 0x36) {
system("/usr/bin/curl http://10.10.10.10:4444/toggle-usb-switch --max-time 3 -o /dev/null 1> /dev/null 2>/dev/null &");
}
}
}
free(device_path);
DELAY();
}
return EXIT_SUCCESS;
}