From 81bf59d7f7ea4486c254ea772e9066215e8a2794 Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Thu, 19 Sep 2019 09:31:52 +0200 Subject: [PATCH] Add /dev/input/eventN number to event struct --- src/dev.c | 16 ++++++++++++++++ src/dev.h | 1 + src/event.c | 3 +++ src/event.h | 15 ++++++++++++--- src/proto_unix.c | 10 ++++++---- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/dev.c b/src/dev.c index 466ed42..22463f1 100644 --- a/src/dev.c +++ b/src/dev.c @@ -33,6 +33,18 @@ static int match_usbdev(const struct usb_device_info *devinfo); static struct device *dev_list = NULL; +static int device_evt_num(const char* path) +{ + const char needle[] = "/dev/input/event"; + char* pos = strstr(path, needle); + + if (pos == NULL) return 0; + + pos += strlen(needle); + + return (int)strtol(pos, NULL, 10); +} + int init_devices(void) { struct device *dev; @@ -44,6 +56,7 @@ int init_devices(void) if(!dev_path_in_use(cfg.serial_dev)) { dev = add_device(); strcpy(dev->path, cfg.serial_dev); + dev->evt_num = device_evt_num(cfg.serial_dev); if(open_dev_serial(dev) == -1) { remove_device(dev); } else { @@ -69,6 +82,7 @@ int init_devices(void) dev = add_device(); strcpy(dev->path, usbdev->devfiles[i]); + dev->evt_num = device_evt_num(usbdev->devfiles[i]); if(open_dev_usb(dev) == -1) { remove_device(dev); @@ -92,6 +106,8 @@ int init_devices(void) return 0; } + + static struct device *add_device(void) { struct device *dev; diff --git a/src/dev.h b/src/dev.h index 37a7001..9507d1d 100644 --- a/src/dev.h +++ b/src/dev.h @@ -30,6 +30,7 @@ struct device { void *data; char name[MAX_DEV_NAME]; char path[PATH_MAX]; + int evt_num; int num_axes, num_buttons; int *minval, *maxval; /* input value range (default: -500, 500) */ diff --git a/src/event.c b/src/event.c index 2593f54..37f091c 100644 --- a/src/event.c +++ b/src/event.c @@ -19,6 +19,7 @@ along with this program. If not, see . #include "config.h" #include #include +#include #include "event.h" #include "client.h" #include "proto_unix.h" @@ -146,6 +147,7 @@ void process_input(struct device *dev, struct dev_input *inp) break; } dev_ev->event.type = EVENT_MOTION; + dev_ev->event.dev = dev_ev->dev->evt_num; dev_ev->event.motion.data = (int*)&dev_ev->event.motion.x; dev_ev->event.motion.data[inp->idx] = sign * inp->val; dev_ev->pending = 1; @@ -179,6 +181,7 @@ void process_input(struct device *dev, struct dev_input *inp) struct dev_event dev_button_event; dev_button_event.dev = dev; dev_button_event.event.type = EVENT_BUTTON; + dev_button_event.event.dev = dev_ev->dev->evt_num; dev_button_event.event.button.press = inp->val; dev_button_event.event.button.bnum = inp->idx; dispatch_event(&dev_button_event); diff --git a/src/event.h b/src/event.h index 9cf382d..d2de54e 100644 --- a/src/event.h +++ b/src/event.h @@ -29,7 +29,10 @@ enum { }; struct event_motion { - int type; + struct { + int type; + int dev; + }; int x, y, z; int rx, ry, rz; unsigned int period; @@ -37,13 +40,19 @@ struct event_motion { }; struct event_button { - int type; + struct { + int type; + int dev; + }; int press; int bnum; }; typedef union spnav_event { - int type; + struct { + int type; + int dev; + }; struct event_motion motion; struct event_button button; } spnav_event; diff --git a/src/proto_unix.c b/src/proto_unix.c index 998f234..8412fcd 100644 --- a/src/proto_unix.c +++ b/src/proto_unix.c @@ -93,7 +93,7 @@ int get_unix_socket(void) void send_uevent(spnav_event *ev, struct client *c) { - int i, data[8] = {0}; + int i, data[9] = {0}; float motion_mul; if(lsock == -1) return; @@ -101,18 +101,20 @@ void send_uevent(spnav_event *ev, struct client *c) switch(ev->type) { case EVENT_MOTION: data[0] = UEV_TYPE_MOTION; + data[1] = ev->dev; motion_mul = get_client_sensitivity(c); for(i=0; i<6; i++) { float val = (float)ev->motion.data[i] * motion_mul; - data[i + 1] = (int)val; + data[i + 2] = (int)val; } - data[7] = ev->motion.period; + data[8] = ev->motion.period; break; case EVENT_BUTTON: data[0] = ev->button.press ? UEV_TYPE_PRESS : UEV_TYPE_RELEASE; - data[1] = ev->button.bnum; + data[1] = ev->dev; + data[2] = ev->button.bnum; break; default: