title: capture mouse with Xlib
body: <p>I want to write a simple Xlib program changing the mouse behavior (to give an example, invert vertical movement). I have a problem with capturing the events.</p>
  
  <p>I would like the code to</p>
  
  <ul>
  <li>capture changes in the controllers position (I move mouse upward, <code>MotionEvent</code>)</li>
  <li>calculate new cursor position (<code>new_x -= difference_x</code>)</li>
  <li>set new cursor position ( move pointer down, <code>XWarpPointer</code>, prevent event generation here)</li>
  </ul>
  
  <p>The code below should capture a motion event every time the mouse is moved, but it generates the event only when the pointer moves from one window to another... How to capture all the movement events?</p>
  
  <pre><code>#include "X11/Xlib.h"
  #include "stdio.h"
  
  int main(int argc, char *argv[])
  {
      Display *display;
      Window root_window;
      XEvent event;
  
      display = XOpenDisplay(0);
      root_window = XRootWindow(display, 0);
      XSelectInput(display, root_window, PointerMotionMask );
  
      while(1) {
          XNextEvent( display, &amp;event );
          switch( event.type ) {
              case MotionNotify:
                  printf("x %d y %d\n", event.xmotion.x, event.xmotion.y );
                  break;
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Related:</p>
  
  <p><a href="https://stackoverflow.com/questions/2792954/x11-how-do-i-really-grab-the-mouse-pointer">X11: How do I REALLY grab the mouse pointer?</a></p>