Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /dev/input/eventN number to event struct #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
tomlankhorst marked this conversation as resolved.
Show resolved Hide resolved
}

int init_devices(void)
{
struct device *dev;
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A serial device will certainly not have a /dev/input/eventN number.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be another identifier, such as /dev/ttyX?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, typically it would be something like /dev/ttySX or /dev/ttyUSBX.

if(open_dev_serial(dev) == -1) {
remove_device(dev);
} else {
Expand All @@ -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);
Expand All @@ -92,6 +106,8 @@ int init_devices(void)
return 0;
}



static struct device *add_device(void)
{
struct device *dev;
Expand Down
1 change: 1 addition & 0 deletions src/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
3 changes: 3 additions & 0 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "event.h"
#include "client.h"
#include "proto_unix.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,30 @@ enum {
};

struct event_motion {
int type;
struct {
int type;
int dev;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nameless structs are not valid C89. Also why make a struct here instead of just adding a dev field in the existing struct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to be consistent with the spnav_event union such that both type and dev become accessible without using a nested struct.

int x, y, z;
int rx, ry, rz;
unsigned int period;
int *data;
};

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;
Expand Down
10 changes: 6 additions & 4 deletions src/proto_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,28 @@ 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;

switch(ev->type) {
case EVENT_MOTION:
data[0] = UEV_TYPE_MOTION;
data[1] = ev->dev;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't do this. Shuffling the data fields, and increasing the length of the data, breaks the protocol.


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:
Expand Down