jsfw/hid.c

489 lines
16 KiB
C
Raw Normal View History

2022-08-30 17:54:56 -05:00
#include "hid.h"
#include "const.h"
2022-10-07 18:36:53 -05:00
#include "server.h"
#include "util.h"
2022-08-30 17:54:56 -05:00
#include "vec.h"
2022-08-30 08:37:34 -05:00
#include <dirent.h>
#include <fcntl.h>
2022-08-31 11:59:06 -05:00
#include <linux/input-event-codes.h>
2022-08-30 08:37:34 -05:00
#include <linux/input.h>
2022-08-30 12:08:36 -05:00
#include <pthread.h>
2022-08-30 08:37:34 -05:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>
2022-08-30 12:08:36 -05:00
#include <unistd.h>
2022-08-29 17:27:03 -05:00
2022-10-12 12:33:18 -05:00
// List of ids of the currently known devices
2022-10-07 18:36:53 -05:00
static Vec known_devices;
// Queue of available devices, devices that can only be given to one client
static Vec available_devices;
// List of cloneable devices, devices that can be handed out to multiple clients
static Vec cloneable_devices;
2022-08-29 17:27:03 -05:00
// Mutex for devices
static pthread_mutex_t devices_mutex = PTHREAD_MUTEX_INITIALIZER;
2022-10-07 18:36:53 -05:00
// Condvar notified on devices update
static pthread_cond_t devices_cond = PTHREAD_COND_INITIALIZER;
// Mutex for devices
static pthread_mutex_t known_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
static ServerConfig *config;
2022-08-29 17:27:03 -05:00
// uniqs are just hexadecimal numbers with colons in between each byte
uniq_t parse_uniq(char uniq[17]) {
uniq_t res = 0;
2022-08-30 08:37:34 -05:00
for (int i = 0; i < 17; i++) {
2022-08-29 17:27:03 -05:00
char c = uniq[i];
2022-08-30 08:37:34 -05:00
int digit;
if (c >= '0' && c <= '9')
digit = c - '0';
else if (c >= 'a' && c <= 'f')
digit = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
digit = c - 'A' + 10;
else
continue;
2022-08-29 17:27:03 -05:00
res <<= 4;
res += digit;
}
return res;
}
// Finish setup of a partially initialized device (set device_info and mapping)
2022-08-30 08:37:34 -05:00
void setup_device(PhysicalDevice *dev) {
dev->device_info.code = DeviceInfo;
dev->device_info.abs_count = 0;
dev->device_info.rel_count = 0;
dev->device_info.key_count = 0;
2022-08-30 12:08:36 -05:00
for (int i = 0; i < ABS_CNT; i++)
2022-08-30 11:28:29 -05:00
dev->mapping.abs_indices[i] = -1;
2022-08-30 12:08:36 -05:00
for (int i = 0; i < REL_CNT; i++)
2022-08-30 17:54:56 -05:00
dev->mapping.rel_indices[i] = -1;
2022-08-30 12:08:36 -05:00
for (int i = 0; i < KEY_CNT; i++)
2022-08-30 11:28:29 -05:00
dev->mapping.key_indices[i] = -1;
2022-10-07 18:36:53 -05:00
uint8_t type_bits[EV_MAX] = {0};
uint8_t feat_bits[(KEY_MAX + 7) / 8] = {0};
ioctl(dev->event, EVIOCGBIT(0, EV_MAX), type_bits);
// Loop over all event types
for (int type = 0; type < EV_MAX; type++) {
2022-09-11 04:23:31 -05:00
// Ignore if the the device doesn't have any of this event type
if (!bit_set(type_bits, type)) {
continue;
}
2022-09-11 04:23:31 -05:00
// Clear feat_bits to only have the features of the current type
memset(feat_bits, 0, sizeof(feat_bits));
ioctl(dev->event, EVIOCGBIT(type, KEY_MAX), feat_bits);
// Loop over "instances" of type (i.e Each axis of a controller for EV_ABS)
for (int i = 0; i < KEY_MAX; i++) {
2022-09-11 04:23:31 -05:00
// "instances" don't have to be consecutive (this is why we do all this instead of just worrying
// about the count)
if (!bit_set(feat_bits, i)) {
continue;
}
if (type == EV_ABS) {
struct input_absinfo abs;
ioctl(dev->event, EVIOCGABS(i), &abs);
uint16_t index = dev->device_info.abs_count++;
dev->device_info.abs_min[index] = abs.minimum;
dev->device_info.abs_max[index] = abs.maximum;
dev->device_info.abs_fuzz[index] = abs.fuzz;
dev->device_info.abs_flat[index] = abs.flat;
dev->device_info.abs_res[index] = abs.resolution;
2022-09-11 04:23:31 -05:00
// Bidirectional mapping id <-> index
// We need this to avoid wasting space in packets because ids are sparse
dev->device_info.abs_id[index] = i;
dev->mapping.abs_indices[i] = index;
} else if (type == EV_REL) {
uint16_t index = dev->device_info.rel_count++;
dev->device_info.rel_id[index] = i;
dev->mapping.rel_indices[i] = index;
} else if (type == EV_KEY) {
uint16_t index = dev->device_info.key_count++;
dev->device_info.key_id[index] = i;
dev->mapping.key_indices[i] = index;
2022-08-30 08:37:34 -05:00
}
}
}
}
2022-10-12 12:33:18 -05:00
bool filter_event(int fd, char *event, ControllerFilter *filter, uniq_t uniq) {
2022-10-07 18:36:53 -05:00
if (filter->js) {
char device_path[64];
snprintf(device_path, 64, "/sys/class/input/%s/device", event);
2022-08-29 17:27:03 -05:00
DIR *device_dir = opendir(device_path);
struct dirent *device_dirent;
2022-08-29 17:27:03 -05:00
bool found = false;
while ((device_dirent = readdir(device_dir)) != NULL) {
if (device_dirent->d_type == DT_DIR && strncmp(device_dirent->d_name, "js", 2) == 0) {
found = true;
break;
}
2022-08-29 17:27:03 -05:00
}
closedir(device_dir);
2022-08-29 18:06:25 -05:00
if (!found) {
return false;
}
2022-08-29 17:27:03 -05:00
}
2022-08-30 08:37:34 -05:00
2022-10-12 12:33:18 -05:00
if (filter->name != NULL) {
char name[256] = {0};
ioctl(fd, EVIOCGNAME(256), name);
if (strcmp(name, filter->name) != 0) {
return false;
}
}
if (filter->uniq > 0 && uniq != filter->uniq) {
return false;
}
2022-10-07 18:36:53 -05:00
struct input_id ids;
ioctl(fd, EVIOCGID, &ids);
if (filter->vendor > 0 && filter->vendor != ids.vendor)
return false;
if (filter->product > 0 && filter->product != ids.product)
return false;
return true;
2022-08-29 17:27:03 -05:00
}
// Initialize vectors for polling
2022-10-12 12:33:18 -05:00
void poll_devices_init(void) {
2023-03-18 12:41:17 -05:00
known_devices = vec_of(uint64_t);
cloneable_devices = vec_of(Controller);
available_devices = vec_of(Controller);
2022-08-29 17:27:03 -05:00
}
2023-03-18 12:41:17 -05:00
// Find index of tag that matches any of the tags specified in the tags array (of length tag_count), returns -1 otherwise
static int match_tags(char *tag, char **tags, size_t tag_count) {
2023-03-18 10:31:33 -05:00
for (int i = 0; i < tag_count; i++) {
if (strcmp(tag, tags[i]) == 0) {
2023-03-18 12:41:17 -05:00
return i;
2023-03-18 10:31:33 -05:00
}
}
2023-03-18 12:41:17 -05:00
return -1;
2023-03-18 10:31:33 -05:00
}
// Block to get a device, this is thread safe
2022-10-12 12:33:18 -05:00
// stop: additional condition to check before doing anything,
// if the condition is ever found to be true the function will return immediately with a NULL pointer.
2023-03-18 12:41:17 -05:00
bool get_device(char **tags, size_t tag_count, bool *stop, Controller *res, uint8_t *ref_index) {
// Check if we can get one right away
2022-10-07 18:36:53 -05:00
pthread_mutex_lock(&devices_mutex);
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
while (1) {
2022-10-12 12:33:18 -05:00
if (*stop) {
pthread_mutex_unlock(&devices_mutex);
2023-03-18 12:41:17 -05:00
return false;
2022-10-12 12:33:18 -05:00
}
2022-10-07 18:36:53 -05:00
for (int i = 0; i < available_devices.len; i++) {
2023-03-18 12:41:17 -05:00
Controller *c = vec_get(&available_devices, i);
int index = match_tags(c->ctr.tag, tags, tag_count);
if (index >= 0) {
*ref_index = index;
*res = *c;
2022-10-07 18:36:53 -05:00
vec_remove(&available_devices, i, NULL);
pthread_mutex_unlock(&devices_mutex);
2023-03-18 12:41:17 -05:00
return true;
2022-10-07 18:36:53 -05:00
}
}
2022-08-30 08:37:34 -05:00
2022-10-07 18:36:53 -05:00
for (int i = 0; i < cloneable_devices.len; i++) {
2023-03-18 12:41:17 -05:00
Controller *c = vec_get(&cloneable_devices, i);
int index = match_tags(c->ctr.tag, tags, tag_count);
if (index >= 0) {
*ref_index = index;
*res = *c;
2022-10-07 18:36:53 -05:00
pthread_mutex_unlock(&devices_mutex);
2023-03-18 12:41:17 -05:00
return true;
2022-10-07 18:36:53 -05:00
}
}
2022-10-07 18:36:53 -05:00
// Wait on condvar until there's a device and we can unlock the mutex
pthread_cond_wait(&devices_cond, &devices_mutex);
2022-08-29 17:27:03 -05:00
}
2022-10-07 18:36:53 -05:00
}
2022-10-07 18:36:53 -05:00
// Return a device that isn't used anymore, this really only makes sense for non cloneable devices.
void return_device(Controller *c) {
// If device is cloneable there is nothing to return
if (c->ctr.duplicate) {
return;
}
pthread_mutex_lock(&devices_mutex);
2023-03-18 12:41:17 -05:00
vec_push(&available_devices, c);
2022-10-07 18:36:53 -05:00
// Signal that there are new devices
pthread_cond_broadcast(&devices_cond);
pthread_mutex_unlock(&devices_mutex);
2022-08-29 17:27:03 -05:00
}
2022-10-07 18:36:53 -05:00
// Forget about a broken device. This invalidates the reference to the controller
void forget_device(Controller *c) {
2023-03-18 12:41:17 -05:00
pthread_mutex_lock(&known_devices_mutex);
2022-10-07 18:36:53 -05:00
// If controller is cloneable we need to remove it from the cloneable list
if (c->ctr.duplicate) {
for (int i = 0; i < cloneable_devices.len; i++) {
Controller *d = *(Controller **)vec_get(&cloneable_devices, i);
2023-03-18 12:41:17 -05:00
if (d->dev.id == c->dev.id) {
2022-10-07 18:36:53 -05:00
vec_remove(&cloneable_devices, i, NULL);
break;
}
}
}
// Free the name if it was allocated
if (c->dev.name != NULL && c->dev.name != DEVICE_DEFAULT_NAME) {
2023-03-18 12:41:17 -05:00
printf("HID: Forgetting device '%s' (%lu)\n", c->dev.name, c->dev.id);
2022-10-07 18:36:53 -05:00
free(c->dev.name);
2022-08-29 18:06:25 -05:00
} else {
2023-03-18 12:41:17 -05:00
printf("HID: Forgetting device %lu\n", c->dev.id);
2022-08-29 18:06:25 -05:00
}
2022-10-07 18:36:53 -05:00
// try to close the file descriptor, they may be already closed if the device was unpugged.
2022-10-07 18:36:53 -05:00
close(c->dev.event);
close(c->dev.hidraw);
// Safely remove device from the known device list
2022-10-07 18:36:53 -05:00
for (int i = 0; i < known_devices.len; i++) {
2023-03-18 12:41:17 -05:00
uint64_t *id = vec_get(&known_devices, i);
if (*id == c->dev.id) {
2022-10-07 18:36:53 -05:00
vec_remove(&known_devices, i, NULL);
2022-08-29 17:27:03 -05:00
break;
}
}
2022-10-07 18:36:53 -05:00
pthread_mutex_unlock(&known_devices_mutex);
2022-08-29 17:27:03 -05:00
}
2023-03-18 12:41:17 -05:00
uint64_t parse_event_name(const char *event) { return atol(event + 5); }
// Find all available devices and pick up on new ones
2022-10-12 12:33:18 -05:00
void poll_devices(void) {
// loop over all entries of /sys/class/input
2022-08-30 08:37:34 -05:00
DIR *input_dir = opendir("/sys/class/input");
struct dirent *input;
2022-10-07 18:36:53 -05:00
2022-08-30 08:37:34 -05:00
while ((input = readdir(input_dir)) != NULL) {
// Ignore if the entry isn't a link or doesn't start with event
2022-08-30 08:37:34 -05:00
if (input->d_type != DT_LNK || strncmp(input->d_name, "event", 5) != 0) {
2022-08-29 17:27:03 -05:00
continue;
}
2022-08-30 08:37:34 -05:00
PhysicalDevice dev;
2022-10-07 18:36:53 -05:00
dev.hidraw = -1;
2022-10-12 12:33:18 -05:00
dev.uniq = 0;
2023-03-18 12:41:17 -05:00
dev.id = parse_event_name(input->d_name);
2022-08-29 17:27:03 -05:00
// Open /dev/input/eventXX
2022-10-07 18:36:53 -05:00
{
char event_path[64];
snprintf(event_path, 64, "/dev/input/%s", input->d_name);
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
dev.event = open(event_path, O_RDONLY);
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
if (dev.event < 0) { // Ignore device if we couldn't open
continue;
}
2022-08-29 17:27:03 -05:00
}
// Try to get the name, default to DEFAULT_NAME if impossible
2022-10-07 18:36:53 -05:00
char *name;
{
static char name_buf[256] = {0};
if (ioctl(dev.event, EVIOCGNAME(256), name_buf) >= 0) {
name = name_buf;
} else {
name = (char *)DEVICE_DEFAULT_NAME;
}
2022-09-11 04:23:31 -05:00
}
2022-08-29 17:27:03 -05:00
2022-10-12 12:33:18 -05:00
// Try to get uniq, drop device if we can't
{
char uniq_str[17] = {0};
ioctl(dev.event, EVIOCGUNIQ(17), uniq_str);
dev.uniq = parse_uniq(uniq_str);
}
2022-10-07 18:36:53 -05:00
// Used for linear searches
bool found;
// Filter devices according server config
ServerConfigController *ctr;
{
found = false;
for (int i = 0; i < config->controller_count; i++) {
ctr = &config->controllers[i];
2022-10-12 12:33:18 -05:00
if (filter_event(dev.event, input->d_name, &ctr->filter, dev.uniq)) {
2022-10-07 18:36:53 -05:00
found = true;
break;
}
}
if (!found) {
goto skip;
}
2022-09-11 04:23:31 -05:00
}
2022-08-29 17:27:03 -05:00
// Check if we already know of this device
2022-08-29 17:27:03 -05:00
{
2022-10-07 18:36:53 -05:00
found = false;
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
pthread_mutex_lock(&known_devices_mutex);
for (int i = 0; i < known_devices.len; i++) {
2023-03-18 12:41:17 -05:00
uint64_t *id = vec_get(&known_devices, i);
if (*id == dev.id) {
2022-10-07 18:36:53 -05:00
found = true;
2022-08-30 08:37:34 -05:00
break;
}
2022-08-29 17:27:03 -05:00
}
2022-10-07 18:36:53 -05:00
pthread_mutex_unlock(&known_devices_mutex);
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
if (found) { // Device isn't new
goto skip;
2022-08-29 17:27:03 -05:00
}
2022-10-07 18:36:53 -05:00
}
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
// Look for hidraw if the device should have one (Dualshock 4 only, with ps4_hidraw property set)
if (ctr->ps4_hidraw) {
// Attempt to find the path
char hidraw_path[64];
{
char hidraw_dir_path[256];
snprintf(hidraw_dir_path, 256, "/sys/class/input/%s/device/device/hidraw", input->d_name);
DIR *hidraw_dir = opendir(hidraw_dir_path);
struct dirent *hidraw = NULL;
while ((hidraw = readdir(hidraw_dir)) != NULL) {
if (strncmp(hidraw->d_name, "hidraw", 6) == 0) {
break;
}
}
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
if (hidraw == NULL) {
printf("HID: Couldn't get hidraw of %s", input->d_name);
goto skip;
}
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
snprintf(hidraw_path, 64, "/dev/%s", hidraw->d_name);
closedir(hidraw_dir);
}
// Try to open
dev.hidraw = open(hidraw_path, O_WRONLY);
if (dev.hidraw < 0) {
goto skip;
}
}
2022-08-29 17:27:03 -05:00
2022-10-07 18:36:53 -05:00
// Allocate for name (only now to avoid unecessary allocations)
if (name != DEVICE_DEFAULT_NAME) {
dev.name = malloc(256);
if (dev.name == NULL) {
dev.name = (char *)DEVICE_DEFAULT_NAME;
} else {
strcpy(dev.name, name);
}
}
2022-08-29 18:06:25 -05:00
2022-10-07 18:36:53 -05:00
// This code is only run if the device has passed all filters and requirements
{
setup_device(&dev);
Controller c = {.dev = dev, .ctr = *ctr};
pthread_mutex_lock(&known_devices_mutex);
2023-03-18 12:41:17 -05:00
vec_push(&known_devices, &c.dev.id);
2022-10-07 18:36:53 -05:00
pthread_mutex_unlock(&known_devices_mutex);
2023-03-18 12:41:17 -05:00
printf("HID: New device, %s [%s] (%s: %lu)\n", name, ctr->tag, input->d_name, dev.id);
2022-10-07 18:36:53 -05:00
if (ctr->duplicate) {
pthread_mutex_lock(&devices_mutex);
2023-03-18 12:41:17 -05:00
vec_push(&cloneable_devices, &c);
2022-10-07 18:36:53 -05:00
// Signal that there are new cloneable devices
pthread_cond_broadcast(&devices_cond);
pthread_mutex_unlock(&devices_mutex);
} else {
pthread_mutex_lock(&devices_mutex);
2023-03-18 12:41:17 -05:00
vec_push(&available_devices, &c);
2022-10-07 18:36:53 -05:00
// Signal that there are new devices
pthread_cond_broadcast(&devices_cond);
pthread_mutex_unlock(&devices_mutex);
}
}
// Continue here avoids running cleanup code
2022-08-29 17:27:03 -05:00
continue;
// close open file descriptor and continue
2022-08-30 08:37:34 -05:00
skip:
2022-08-29 17:27:03 -05:00
close(dev.event);
};
closedir(input_dir);
2022-10-07 18:36:53 -05:00
}
2022-10-07 18:36:53 -05:00
// "Execute" a MessageControllerState: set the led color, rumble and flash using the hidraw interface (Dualshock 4 only)
void apply_controller_state(Controller *c, MessageControllerState *state) {
if (c->ctr.ps4_hidraw && c->dev.hidraw < 0) {
2023-03-18 12:41:17 -05:00
printf("HID: Trying to apply controller state on incompatible device (%lu)\n", c->dev.id);
2022-10-07 18:36:53 -05:00
return;
2022-08-29 17:27:03 -05:00
}
2023-03-18 12:41:17 -05:00
printf("HID: (%lu) Controller state: #%02x%02x%02x flash: (%d, %d) rumble: (%d, %d)\n", c->dev.id, state->led[0],
2022-10-07 18:36:53 -05:00
state->led[1], state->led[2], state->flash_on, state->flash_off, state->small_rumble, state->big_rumble);
2022-08-30 08:37:34 -05:00
uint8_t buf[32] = {0x05, 0xff, 0x00, 0x00};
buf[4] = state->small_rumble;
buf[5] = state->big_rumble;
buf[6] = state->led[0];
buf[7] = state->led[1];
buf[8] = state->led[2];
buf[9] = state->flash_on;
buf[10] = state->flash_off;
2022-10-07 18:36:53 -05:00
write(c->dev.hidraw, buf, 32);
2022-08-30 12:08:36 -05:00
if (state->flash_on == 0 && state->flash_off == 0) {
// May not be necessary
2022-10-07 18:36:53 -05:00
fsync(c->dev.hidraw);
2022-09-11 04:23:31 -05:00
// Send a second time, to reenable the led
2022-10-07 18:36:53 -05:00
write(c->dev.hidraw, buf, 32);
2022-08-30 08:37:34 -05:00
};
}
// Body of the hid thread
2022-10-07 18:36:53 -05:00
void *hid_thread(void *arg) {
2022-08-30 17:54:56 -05:00
printf("HID: start\n");
2022-10-07 18:36:53 -05:00
config = arg;
2022-08-29 17:27:03 -05:00
poll_devices_init();
2022-08-30 08:37:34 -05:00
while (1) {
2022-08-29 17:27:03 -05:00
poll_devices();
2022-10-07 18:36:53 -05:00
nanosleep(&config->poll_interval, NULL);
2022-08-29 17:27:03 -05:00
}
2022-08-29 17:27:03 -05:00
return NULL;
2022-08-27 19:29:43 -05:00
}