Open device even if you were unable to grab it

This commit is contained in:
Denis 2023-11-05 21:36:16 +01:00
parent bc12d2f1e9
commit 93fd24feff
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03

View file

@ -24,6 +24,7 @@ static struct libevdev* ev_matches(const char* sysfs_entry, const uinput_filters
if (libevdev_new_from_fd(fd, &dev) != 0) {
fprintf(stderr, "Cannot initialize libevdev from this device (%s): skipping.\n", sysfs_entry);
close(fd);
return NULL;
}
@ -31,25 +32,36 @@ static struct libevdev* ev_matches(const char* sysfs_entry, const uinput_filters
if (strcmp(name, filters->name) != 0) {
fprintf(stderr, "The device name (%s) for device %s does not matches the expected one %s.\n", name, sysfs_entry, filters->name);
libevdev_free(dev);
close(fd);
return NULL;
}
const int grab_res = libevdev_grab(dev, LIBEVDEV_GRAB);
if (grab_res != 0) {
fprintf(stderr, "Unable to grab the device (%s): %d.\n", sysfs_entry, grab_res);
libevdev_free(dev);
return NULL;
//libevdev_free(dev);
//close(fd);
return dev;
}
return 0;
return dev;
}
static pthread_mutex_t input_acquire_mutex = PTHREAD_MUTEX_INITIALIZER;
static char* open_sysfs[] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
};
void *input_dev_thread_func(void *ptr) {
input_dev_t *in_dev = (input_dev_t*)ptr;
struct libevdev* dev = NULL;
int open_sysfs_idx = -1;
for (;;) {
const uint32_t flags = in_dev->crtl_flags;
@ -71,6 +83,12 @@ void *input_dev_thread_func(void *ptr) {
continue;
}
// clean up leftover from previous opening
if (open_sysfs_idx >= 0) {
free(open_sysfs[open_sysfs_idx]);
open_sysfs[open_sysfs_idx] = NULL;
}
char path[512] = "\0";
DIR *d;
@ -88,7 +106,30 @@ void *input_dev_thread_func(void *ptr) {
sprintf(path, "%s%s", input_path, dir->d_name);
if (ev_matches(path, in_dev->ev_filters) != NULL) {
// check if that has been already opened
// open_sysfs
int skip = 0;
for (int o = 0; o < (sizeof(open_sysfs) / sizeof(const char*)); ++o) {
if (strcmp(open_sysfs[o], path) == 0) {
skip = 1;
break;
}
}
if (skip) {
continue;
}
// try to open the device
dev = ev_matches(path, in_dev->ev_filters);
if (dev != NULL) {
open_sysfs_idx = 0;
while (open_sysfs[open_sysfs_idx] != NULL) {
open_sysfs[open_sysfs_idx] = malloc(sizeof(path));
memcpy(open_sysfs[open_sysfs_idx], path, 512);
++open_sysfs_idx;
}
printf("Opened device %s\n name: %s", path, libevdev_get_name(dev));
break;
}