title: How to read low level mouse click position in linux .
body: <p>I am using this code to read mouse events from the dev/input/event* in linux . </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;linux/input.h&gt;
  #include &lt;fcntl.h&gt;
  
  #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, &amp;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;
  }
  </code></pre>
  
  <p>It gives me the results in the format : </p>
  
  <blockquote>
    <p>time 1342517261.840285    type 2  code 0  value -1</p>
  </blockquote>
  
  <p>'time' is the timestamp, it returns the time at which the event happened.</p>
  
  <p>'code' is event code, for example REL_X or KEY_BACKSPACE, complete
  list is in include/linux/input.h.</p>
  
  <p>'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.</p>
  
  <p><strong>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 .</strong></p>
  
  <hr>
  
  <p>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 .   </p>
  
  <hr>
  
  <p><strong>Edit2 : SOLUTION</strong> </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  #include &lt;linux/input.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;X11/Xlib.h&gt;
  
  #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),&amp;root,&amp;child,
                &amp;rootX,&amp;rootY,&amp;winX,&amp;winY,&amp;mask);
  
    if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
      perror("opening device");
      exit(EXIT_FAILURE);
    }
  
    while(read(fd, &amp;ie, sizeof(struct input_event))) {
      if (ie.type == 2) {
        if (ie.code == 0) {
            XQueryPointer(dpy,DefaultRootWindow(dpy),&amp;root,&amp;child,
                          &amp;rootX,&amp;rootY,&amp;winX,&amp;winY,&amp;mask);
            //rootX += ie.value;
            }
        else if (ie.code == 1) {
            XQueryPointer(dpy,DefaultRootWindow(dpy),&amp;root,&amp;child,
                          &amp;rootX,&amp;rootY,&amp;winX,&amp;winY,&amp;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;
  }
  </code></pre>
  
  <p>XQueryPointer seems more convenient solution . Thanks , @perreal for the guidance . </p>