-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_event_file.c
69 lines (51 loc) · 1.78 KB
/
find_event_file.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
#include "find_event_file.h"
#define INPUT_DIR "/dev/input/"
int is_char_device(const struct dirent *file) {
struct stat filestat;
char file_path[512];
int err;
snprintf(file_path, sizeof(file_path), "%s%s", INPUT_DIR, file->d_name);
err = stat(file_path, &filestat);
if (err)
return 0;
// Check if the file supports character events
return S_ISCHR(filestat.st_mode);
}
char *get_keyboard_event_file(void) {
char *keyboard_file = NULL;
int num, i;
struct dirent **event_files;
char file_path[512];
num = scandir(INPUT_DIR, &event_files, &is_char_device, &alphasort);
if (num < 0)
return NULL;
for (i = 0; i < num; ++i) {
int fd;
int32_t event_bitmap = 0;
int32_t kbd_bitmap = KEY_A | KEY_B | KEY_C | KEY_Z;
snprintf(file_path, sizeof(file_path), "%s%s", INPUT_DIR, event_files[i]->d_name);
if ((fd = open(file_path, O_RDONLY)) == -1) {
continue;
}
// Get all the features supported by the device
ioctl(fd, EVIOCGBIT(0, sizeof(event_bitmap)), &event_bitmap);
if ((EV_KEY & event_bitmap) == EV_KEY) {
// The device supports key events
// Get the features supported that are related to key events
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(event_bitmap)), &event_bitmap);
if ((kbd_bitmap & event_bitmap) == kbd_bitmap) {
// The device supports A, B, C, Z keys, so it probably is a keyboard
keyboard_file = strdup(file_path);
close(fd);
break;
}
}
close(fd);
}
// Cleanup scandir results
for (i = 0; i < num; ++i) {
free(event_files[i]);
}
free(event_files);
return keyboard_file;
}