-
Notifications
You must be signed in to change notification settings - Fork 4
/
dev_evdev.c
253 lines (218 loc) · 6.27 KB
/
dev_evdev.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "dev_evdev.h"
#include <libevdev-1.0/libevdev/libevdev.h>
static const char *input_path = "/dev/input/";
static const char *hidden_input_path = "/dev/input/.hidden/";
static pthread_mutex_t input_acquire_mutex = PTHREAD_MUTEX_INITIALIZER;
struct open_resource {
char* sysfs_path;
int fd;
};
static struct open_resource open_paths[] = {
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
{
.fd = -1,
.sysfs_path = NULL
},
};
static bool ev_matches(
const uinput_filters_t* const in_filters,
struct libevdev *in_evdev
) {
if (in_evdev == NULL) {
return NULL;
}
const char* name = libevdev_get_name(in_evdev);
if ((name != NULL) && (strcmp(name, in_filters->name) != 0)) {
return false;
}
// TODO: if more filters are implemented write them here
return true;
}
void dev_evdev_close(struct libevdev* out_evdev) {
if (out_evdev == NULL) {
return;
}
const int input_acquire_lock_result = pthread_mutex_lock(&input_acquire_mutex);
if (input_acquire_lock_result != 0) {
fprintf(stderr, "Cannot lock input mutex: %d -- this will probably have no consequences\n", input_acquire_lock_result);
return;
}
int fd = libevdev_get_fd(out_evdev);
for (int i = 0; i < sizeof(open_paths) / sizeof(open_paths[0]); ++i) {
if (open_paths[i].fd == fd) {
if (open_paths[i].sysfs_path != NULL) {
free(open_paths[i].sysfs_path);
open_paths[i].sysfs_path = NULL;
}
open_paths[i].fd = -1;
}
}
// free the memory
libevdev_free(out_evdev);
pthread_mutex_unlock(&input_acquire_mutex);
}
#define MAX_PATH_LEN 512
int dev_evdev_open(
const uinput_filters_t *const in_filters,
struct libevdev* *const out_evdev
) {
int res = -ENOENT;
int open_sysfs_idx = -1;
struct stat st = {0};
if (stat(hidden_input_path, &st) == -1) {
printf("Directory %s does not exits -- creating it\n", hidden_input_path);
if (mkdir(hidden_input_path, 0700) == 0) {
printf("Directory %s creates successfully", hidden_input_path);
} else {
fprintf(stderr, "Error creating %s directory: %d\n", hidden_input_path, errno);
}
}
const int mutex_lock_res = pthread_mutex_lock(&input_acquire_mutex);
if (mutex_lock_res != 0) {
fprintf(stderr, "Cannot lock input mutex: %d\n", mutex_lock_res);
res = mutex_lock_res;
goto dev_evdev_open_mutex_err;
}
char *const path = malloc(MAX_PATH_LEN);
if (path == NULL) {
res = -ENOMEM;
goto dev_evdev_open_err;
}
char *const hidden_path = malloc(MAX_PATH_LEN);
if (path == NULL) {
free(path);
res = -ENOMEM;
goto dev_evdev_open_err;
}
DIR *d;
struct dirent *dir;
d = opendir(input_path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_name[0] == '.') {
continue;
} else if (dir->d_name[0] == 'b') { // by-id
continue;
} else if (dir->d_name[0] == 'j') { // js-0
continue;
}
snprintf(hidden_path, MAX_PATH_LEN - 1, "%s%s", hidden_input_path, dir->d_name);
snprintf(path, MAX_PATH_LEN - 1, "%s%s", input_path, dir->d_name);
//printf("Testing for device %s\n", path);
// try to open the device, if it cannot be opened to go the next
int fd = open(path, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Cannot open %s, device skipped.\n", path);
continue;
}
// check if that has been already opened
// open_sysfs
int skip = 0;
for (int o = 0; o < (sizeof(open_paths) / sizeof(open_paths[0])); ++o) {
if ((open_paths[o].fd != -1) && (open_paths[o].sysfs_path != NULL) && (strcmp(open_paths[o].sysfs_path, path) == 0)) {
close(fd);
skip = 1;
break;
} else if ((open_paths[o].sysfs_path == NULL) && (open_paths[o].fd == -1)) {
open_sysfs_idx = o;
}
}
if ((skip) || (open_sysfs_idx == -1)) {
continue;
}
if (libevdev_new_from_fd(fd, out_evdev) != 0) {
close(fd);
continue;
}
// try to open the device
if (!ev_matches(in_filters, *out_evdev)) {
libevdev_free(*out_evdev);
close(fd);
continue;
}
if (rename(path, hidden_path) != 0) {
fprintf(stderr, "Unable to move the device: %d\n", errno);
libevdev_free(*out_evdev);
close(fd);
continue;
} else {
if (chmod(hidden_path, 000) != 0) {
fprintf(stderr, "Unable to perform chmod 000 to opened device\n");
}
}
// register the device as being opened already
open_paths[open_sysfs_idx].sysfs_path = path;
open_paths[open_sysfs_idx].fd = fd;
// the device has been found
res = 0;
break;
}
closedir(d);
}
dev_evdev_open_err:
if ((path != NULL) && (res != 0)) {
free(path);
}
pthread_mutex_unlock(&input_acquire_mutex);
dev_evdev_open_mutex_err:
return res;
}