LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   reading mouse device (https://www.linuxquestions.org/questions/linux-newbie-8/reading-mouse-device-615178/)

danielrex15 01-21-2008 08:20 AM

reading mouse device
 
hi,
i am new to linux .as my assignment ,i have been asked to read from the usb mouse pointing device. To begin with, I am not aware of the particular device in the "/dev/" directory that I need to do a read on.
I also tried to read from "/dev/input/mice" and found that it returned only 3 bytes. I went thru some of the resources onlince and found that it needs to retur struct input_event (defined in /include/linux/). So, I am not aware of how to interpret this 3 bytes that i get from reading.

osor 01-21-2008 12:27 PM

This post should get you started on your homework.

danielrex15 01-21-2008 11:28 PM

mouse device
 
hi,
thank you very much for your help.........can u help me with a detailed code for this.
thanks in advance

osor 01-22-2008 01:20 PM

Quote:

Originally Posted by danielrex15 (Post 3030950)
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:
  1. 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.
  2. 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.

danielrex15 01-23-2008 05:54 AM

I Have Finished My Homework......only One Part Left Detecting Whether A Mouse Is Attached Or Not....how Can We Do That Any Clue.


Thanks In Advance
Daniel Rex

osor 01-23-2008 11:27 AM

If you just want to see all your mouse devices, you can (as I mentioned before) just use /proc/bus/input/devices. If you want something special to occur when you plug in a mouse (e.g., the running of a script) you can use udev.


All times are GMT -5. The time now is 04:53 AM.