I'm working with a mouse USB driver and I really don't know how to difference and use information send by /dev/input/mouse0
and /dev/input/mice
After few search on Google, I've found a few code about using /dev/input/mice
int fd, bytes;
unsigned char data[4];
const char *pDevice = "/dev/input/mice";
// Open Mouse
fd = open(pDevice, O_RDWR);
if(fd == -1)
{
printf("ERROR Opening %s\n", pDevice);
return -1;
}
int left, middle, right;
signed char x, y;
// Read Mouse
bytes = read(fd, data, sizeof(data));
if(bytes > 0)
{
left = data[0] & 0x1;
right = data[0] & 0x2;
middle = data[0] & 0x4;
x = data[1];
y = data[2];
printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", x, y, left, middle, right);
if(left)
return left;
if(right)
return right;
if(middle)
return middle;
}
return 0;
This program works but didn't get any information about the wheel scroll
I was able to found a program which use /dev/input/event0
int fd;
struct input_event ev
const char* pFile = "/dev/input/event0";
fd = open(pFile, O_RDONLY);
if(fd == -1)
{
printf("ERROR Opening %s\n", pFile);
return -1;
}
read(fd, &ev, sizeof(ev));
std::cout << "type : " << ev.type << " code : " << ev.code << " value : " << ev.value << std::endl;
close(fd);
return 0;
But there is a problem for diagonal movement because this file saved only 1 coordinate at same time, and X coordinate have priority on Y; so it's pretty annoying when you want to move your mouse on Y axis
However, I don't find any code to use /dev/input/mouse0
, so what information are saved in this file?
So my questions are :
1. What information are saved in /dev/input/mouse0
?
2. How can I read all mouse information without opening 2 files, and without priority problem (whell scrolling + X/Y axis movement)?
Best Answer
The Linux Input Subsystem userspace API documentation answers your questions. In general, all files in
/dev/input/
are provided by event handlers that distribute device events to userspace.What are /dev/input/mouse0 and /dev/input/mice?
From the introduction (important parts highlighted by me)
...this could also be reason why you don't see mouse wheel events. Since it is "a hack" for legacy programs, you shouldn't use it if you don't have to.
How can I read all mouse information [...]?
Use the corresponding
/dev/input/eventX
file which is provided by evdev (the event handler, not to be confused with the xorg-driver evdev). That's what your second code snippet obviously does....but what about the priority problem?
There is no priority problem. According to 2.2. Event codes:
Because all consecutive events until a
SYN_REPORT
event can be considered to have happened at the same time, the order in which they are reported doesn't matter.For example (using this program I found on github):
As you can see, not only are concurrent events separated by
SYN_REPORT
events, they also have the same time stamp.By the way, you can also use
evtest
to see the events generated by a device. You can find its source code here if you are interested in how it works.