title: How does one handle input from a character device/gamepad directly in Linux systems?
body: <p>I am developing a program in C that uses a <a href="https://rads.stackoverflow.com/amzn/click/com/B0034ZOAO0" rel="noreferrer" rel="nofollow noreferrer">USB SNES controller</a> for input on a RPM based distribution.  Is there a library anyone knows of that makes this easier to interact with, or some tool (joydev?) that allows to properly read input from the device?  I do not need an entire game engine; it is only for input from the character device.</p>
  
  <p>If there's a library that already does this for me, that would be excellent (I can just see what the library does on my own) and this can be closed with just a link; otherwise, if I have to do this myself, I have a few specific questions:</p>
  
  <h3>The questions:</h3>
  
  <ul>
  <li>Is there an already existing C library that handles all of my USB device interactions to a gamepad for me?  I'll gladly learn a new library.  (My google-fu has failed me here, I apologize if this is excessively obvious)</li>
  <li>What is the appropriate way to ensure you open the proper character device every time, as the event* name changes between sessions/initialization?</li>
  <li>What is the appropriate method to handle input from these devices from my application?  Just determine what each button press amounts to value-wise and perform actions based on that input when we poll over the character device?</li>
  </ul>
  
  <p>In brief pseduo-C, something like this?</p>
  
  <pre><code>struct gamepad {
      int fd;
  };
  
  void get_input(struct gamepad *gamepad)
  {
      char *buf;
      read(gamepad-&gt;fd, buf, 48);
      switch(buf)
      {
          /* insert cases about handling differing buttons */
      }
  }
  </code></pre>
  
  <h3>How the device presents itself:</h3>
  
  <p>I can clearly see the device registers properly as far as I can see:</p>
  
  <pre><code>$ dmesg | tail
  [198564.517068] usb 1-1: USB disconnect, device number 17
  [198566.154324] usb 1-1: new low-speed USB device number 18 using xhci_hcd
  [198566.323309] usb 1-1: New USB device found, idVendor=12bd, idProduct=d015
  [198566.323312] usb 1-1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
  [198566.323313] usb 1-1: Product: 2Axes 11Keys Game  Pad
  [198566.323792] usb 1-1: ep 0x81 - rounding interval to 64 microframes, ep desc says 80 microframes
  [198566.328692] input: 2Axes 11Keys Game  Pad as /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0/input/input20
  [198566.329036] hid-generic 0003:12BD:D015.0006: input,hidraw2: USB HID v1.10 Gamepad [2Axes 11Keys Game  Pad] on usb-0000:00:14.0-1/input0
  </code></pre>
  
  <p>And if I read from the device, I can see that it receives an interrupt and input from the device just be using hexdump:</p>
  
  <pre><code>$ ls -lattr /dev/input/by-id/usb-12bd_2Axes_11Keys_Game_Pad-event-joystick 
  lrwxrwxrwx. 1 root root 10 Jan 20 15:56 /dev/input/by-id/usb-12bd_2Axes_11Keys_Game_Pad-event-joystick -&gt; ../event17
  </code></pre>
  
  <p>When I press a key (not releasing) it appears to work as expected although I cannot decipher what is being returned from the buffer without context as of yet:</p>
  
  <pre><code>$ hexdump /dev/input/event17 
  0000000 f53a 569f 0000 0000 ac6c 000c 0000 0000
  0000010 0003 0000 007f 0000 f53a 569f 0000 0000
  0000020 ac6c 000c 0000 0000 0000 0000 0000 0000
  </code></pre>
  
  <p>On release of a button, you receive similar output:</p>
  
  <pre><code>0000030 f53c 569f 0000 0000 8be3 0007 0000 0000
  0000040 0003 0001 007f 0000 f53c 569f 0000 0000
  0000050 8be3 0007 0000 0000 0000 0000 0000 0000
  </code></pre>
  
  <p>That is the 'up' button being pressed and released above.</p>
  
  <h3>The research:</h3>
  
  <p>In an effort to determine how other libraries do this, I thought about doing an strace of <code>pygame</code> in python and seeing what devices it opens as well as how it reads input, but I'm still learning through how to use it.  I also saw some vague mentions concerning joydev, but, again, haven't learned how to use them.  I'm doing that currently and will post results if I learn anything of value.</p>
  
  <p>Beyond that, watching button presses via ASCII and hexdump, I note that they have a similar input based on button, but there appears to be what I figure is a count of interrupts for the USB bus at the end of the output above (0xf53a to 0xf53c).  This always appears to increment as well, and, for my purposes, can probably be discarded.</p>
  
  <p>There is also the possibility I'm just not mounting the device correctly because I'm missing some module or package (thinking, again, about joydev and what it is supposed to do).  I have not worked often with USB at all so this type of device handling is new to me.</p>
  
  <p>Searching around for a while, I didn't see anything that showed exactly what I was looking for, but I gladly accept redirects to other questions/topics for reading.</p>
  
  <p>Thank you in advance!</p>