Linux : Reading the Mouse events datas from /dev/input/mouse0

A simple program to check the /dev/input/mouse0 datas

Tested on the raspberry PI with GPM installed

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>

#define MOUSEFILE "/dev/input/mouse0"
//
int main()
{
    int fd;
    struct input_event ie;
    //
    unsigned char button,bLeft,bMiddle,bRight;
    char x,y;
    int absolute_x,absolute_y;

    if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
        printf("Device open ERROR\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("Device open OK\n");
    }
    //
    printf("right-click to set absolute x,y coordinates origin (0,0)\n");
    while(read(fd, &ie, sizeof(struct input_event)))
    {
        unsigned char *ptr = (unsigned char*)&ie;
        int i;       
        //
        button=ptr[0];
        bLeft = button & 0x1;
        bMiddle = ( button & 0x4 ) > 0;
        bRight = ( button & 0x2 ) > 0;
        x=(char) ptr[1];y=(char) ptr[2];
        printf("bLEFT:%d, bMIDDLE: %d, bRIGHT: %d, rx: %d  ry=%d\n",bLeft,bMiddle,bRight, x,y);
        //
        absolute_x+=x;
        absolute_y-=y;
        if (bRight==1)
        {
            absolute_x=0;
            absolute_y=0;
            printf("Absolute x,y coords origin recorded\n");
        }
        //
        printf("Absolute coords from TOP_LEFT= %i %i\n",absolute_x,absolute_y);
        //
        // comment to disable the display of raw event structure datas
        //
        for(i=0; i<sizeof(ie); i++)
        {
            printf("%02X ", *ptr++);
        }
        printf("\n");
    }

return 0;
}

The read() command is blocking for now.
The following C code does almost the same thing, with non-blocking option enabled this time:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/input.h>

#define MOUSEFILE "/dev/input/mouse0\0"
//
int main()
{
    int fd;
    struct input_event ie;
    unsigned char *ptr = (unsigned char*)&ie;
    //
    unsigned char button,bLeft,bMiddle,bRight;
    char x,y;                                                            // the relX , relY datas
    int absolute_x,absolute_y;

    if((fd = open(MOUSEFILE, O_RDONLY | O_NONBLOCK )) == -1)
    {
        printf("NonBlocking %s open ERROR\n",MOUSEFILE);
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("NonBlocking %s open OK\n",MOUSEFILE);
    }
    //
    printf("--------------------------------------------------------\n");
    printf("right-click to set absolute x,y coordinates origin (0,0)\n");
    printf("left+right click to EXIT\n");
    printf("--------------------------------------------------------\n");
    //
    while(1)
    {       
        if(read(fd, &ie, sizeof(struct input_event))!=-1)
        {
            //
            button=ptr[0];
            bLeft = button & 0x1;
            bMiddle = ( button & 0x4 ) > 0;
            bRight = ( button & 0x2 ) > 0;
            x=(char) ptr[1];y=(char) ptr[2];
            if(bLeft&bRight) { printf("\nright+left ,EXIT\n");break;}
            // computes absolute x,y coordinates
            absolute_x+=x;
            absolute_y-=y;
            // set absolute reference ?
            if (bRight==1)
            {
                absolute_x=0;
                absolute_y=0;
                printf("Absolute x,y coords origin recorded\n");
            }
            // show it!
            printf("X%i Y%i\n",absolute_x,absolute_y);
            fflush(stdout);
        }
    }
    //
    close(fd);
return 0;
}

 

A mouse only sends relative movement, not absolute position. You have to keep track of it yourself, and when you receive a mouse-button event you have to check your own coordinates for the position.

 


I am using this code to read mouse events from the dev/input/event* in linux .

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/input.h>
#include <fcntl.h>

#define MOUSEFILE "/dev/input/event4"

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;
}

It gives me the results in the format :

time 1342517261.840285 type 2 code 0 value -1

‘time’ is the timestamp, it returns the time at which the event happened.

‘code’ is event code, for example REL_X or KEY_BACKSPACE, complete list is in include/linux/input.h.

‘value’ is the value the event carries. Either a relative change for EV_REL, absolute new value for EV_ABS (joysticks …), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.

when i click , i get the event but i don’t get the position of the mouse on the screen , what is the way to get the position of the mouse on screen .


Edit 1:So as it turns out that i have to use the relative co-ordinates to get the mouse co-ordinates .I believe this is a common requirement so there might be libraries/pre-existing code that you can use to get the co-ordinates. Any info on this topic will be very useful .


Edit2 : SOLUTION

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/input.h>
#include <fcntl.h>
#include <X11/Xlib.h>

#define MOUSEFILE "/dev/input/event4"

int main()
{
  int fd;
  struct input_event ie;
  Display *dpy;
  Window root, child;
  int rootX, rootY, winX, winY;
  unsigned int mask;

  dpy = XOpenDisplay(NULL);
  XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
              &rootX,&rootY,&winX,&winY,&mask);

  if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
    perror("opening device");
    exit(EXIT_FAILURE);
  }

  while(read(fd, &ie, sizeof(struct input_event))) {
    if (ie.type == 2) {
      if (ie.code == 0) {
          XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                        &rootX,&rootY,&winX,&winY,&mask);
          //rootX += ie.value;
          }
      else if (ie.code == 1) {
          XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
                        &rootX,&rootY,&winX,&winY,&mask);
         // rootY += ie.value;
          }
      printf("time%ld.%06ld\tx %d\ty %d\n",
         ie.time.tv_sec, ie.time.tv_usec, rootX, rootY);
    } else
      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;
}

you need to compile with X11 lib: gcc main.c -lX11

XQueryPointer seems more convenient solution . Thanks , @perreal for the guidance .

P.S. Never worked for me CK, but keep it for later clue.

Tip: to check the mouse event use following command.

# cat /proc/bus/input/devices | grep mouse
H: Handlers=mouse0 event15

 

Ref.:

http://blog.eihis.com/2014/06/26/linux-reading-the-mouse-events-datas/