tags:

views:

320

answers:

2

So the question I have today has to do with interfacing with a USB mouse plugged into a linux machine. However, I don't want the mouse to have any traditional effect on the X environment -- I just want to be able utilize the encoders embedded within it through raw input. So here's my question.

How can I obtain low level but meaningful data from alternate mouse devices within c++ in linux? Specifically, I would like to know relative position or at least encoder counts along both the x and y axes.

A: 

You should use the HID (Human Interface Device) protocol

http://www.usb.org/developers/devclass_docs/HID1_11.pdf

http://en.wikipedia.org/wiki/Human_interface_device

Lliane
+1  A: 

I've done something similar for a USB barcode reader that presents as a HID keyboard.

In recent kernels, there will be an event device for the mouse in /dev/input/event*. If you open that and grab that with the EVIOCGRAB ioctl(), it won't send mouse events to any other app. You can then read events straight off the mouse - see the evdev interface documentation in Documentation/input/input.txt in the linux source code distro.

When you read from the event device, you'll get a whole number of input events, in the following form:

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

(struct input_event and the following macros are all defined in linux/input.h).

The events you're interested in will have input_event.type == EV_REL (relative movement event), the input_event.code member will be something like REL_X (indicating X-axis - see the linux/input.h file for the full list), and input_event.value will be the displacement.

There is no need to implement the HID protocol yourself, as another answer suggests.

caf