title: How to capture legitimately and programmatically a second mouse or a trackball under linux, X?
body: <p>I am designing and implementing an experimental GUI application. I wish to allow my users to ergonomically use both hands simultaneously to interact with the application. As an example, imagine GIMP and a second mouse (a trackball) in your left (other) hand: you can navigate, pan, rotate or zoom the picture with your left hand, while drawing/editing point-accurate dots, lines or figures with your right hand at the same time. Back in 2000 ~ 2002 I did some experiments with two mice: one on PS2 (USB) interface and another one on RS232 serial interface. Nowadays, many years later, I am revisiting that ancient idea, with modern software and contemporary (USB) input devices.</p>
  
  <p>In an ideal case, my goal is to attach a second mouse (or a trackball) to a GNU/ Linux system with X and then be able to:</p>
  
  <ul>
  <li>(1.) Keep up using the primary mouse as the X default master pointer.</li>
  <li>(2.) Programmatically access / read events from the second mouse.</li>
  <li>(3.) Get the events from the second mouse separately (in a
  discernible way) from the primary mouse.</li>
  <li>(4.) Be able to "consume" the events from the second mouse, so that whenever my program is started, the second mouse shall not affect the master pointer.</li>
  <li>(5.) Preferably, the second mouse shall be captured only when the GUI
  window of my program has the focus.</li>
  <li>(6.) Preferably, the complete solution shall not depend on other packages or libraries, like gpm for example. However, it is not a problem if (any) udev rules are required, as the end user would need to install my program anyway.</li>
  <li>(7.) Preferably the solution shall not need any extra privileges or
  permissions like adding the user to "input" group, for example.</li>
  </ul>
  
  <p>I have spent 6+ hours searching Internet for hints, but regrettably I did not go too far beyond some "dirty" partial solution, with several disadvantages:</p>
  
  <ul>
  <li>(A.) Add the user to "input" group.</li>
  <li>(B.) Capture the events from the second mouse as in the example program below.</li>
  <li>(C.) Open point: look further as how to "consume" the events from the second mouse?</li>
  <li>(D.) Cope with global capturing (not only the window with focus) and other issues?</li>
  </ul>
  
  <p>...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;linux/input.h&gt;
  
  int main(int argc, char **argv)
  {
    int ret=1; /* Program's return value; 0=success. */
    int fd; /* File descriptor (handler) */
    struct input_event ev;
  
    if (argc != 2) {
      printf("Usage:\n rdinpdev &lt;device&gt;\n"
        "Print events from an input device.\n"
      );
    }
    else {
      fd = open(argv[1], O_RDONLY);
      printf("open returned %d\n",fd);
      if(fd &gt;= 0) {
        ret=0;
        while(1) {
          read(fd, &amp;ev, sizeof(struct input_event));
          printf("value %d, type %d, code %d\n",ev.value,ev.type,ev.code);
        }
      }
    }
    return ret;
  }
  </code></pre>