Quote:
Originally Posted by danielrex15
can u help me with a detailed code for this.
|
I will not do your homework. But I will tell you this: there are two ways to read from a mouse device in linux:
- The old mousedev way. This involves device files such as /dev/input/mouseN for reading from the Nth mouse or /dev/input/mice for reading from all mice at the same time.
- The new evdev way. Here you read from /dev/input/eventN where N corresponds to your input event handler for your mouse (you can view this through “cat /proc/bus/input/devices”).
If you want to use the old way, you’ll have to look at some code which uses it (such as gpm) and make sense of it.
If you want to use the new way, your code can build on this boilerplate:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>
#define MOUSEFILE "/dev/input/event1"
int main()
{
int fd;
struct input_event ie;
if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
perror("opening device");
exit(EXIT_FAILURE);
}
while(read(fd, &ie, sizeof(struct input_event))) {
printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
}
return 0;
}
Now, in the real world, you might pass the device path as an argument. Or you might pass the device description as an argument and have your program find the device path through
/proc/bus/input/devices. You will obviously need to know what the types and codes and values mean. These are mostly macros in the
linux/input.h header file. For example, for mice you will need to know
Code:
EV_SYN — synthetic events (e.g. 0 0 0) which separate real events
EV_KEY — key/button events
EV_REL — relative motion
EV_ABS — absolute motion
You will also need to know things like the keycodes for buttons (there are in fact
ioctls which you can use to get the keyvalue mask of the device):
Code:
BTN_LEFT
BTN_RIGHT
BTN_MIDDLE
You will need to know the relative and absolute axes. You will also need to know what the
value field means. E.g., for button presses, it is usually 1 and for button releases, it is 0. You will also need to use look at the timing of each event, and use it to interpret double or triple clicks, etc.
If you want some concrete code to look at, look you might take a look at the Xorg
evdev input driver or
library. There are a bunch more if you search the web for “evdev mouse” or similar. For example,
here is a small program which finds the keys and axes supported by a device.