Programming joysticks with linux

Got the idea today to add joystick support to wordwarvi, and after work, went out and bought a Logitech Dual Action game pad with rumble effect. Was quite easy to program for, I got home about 6:00, and by 6:30, had a little program able to read from the joystick, made a trip to the grocery store to buy some food, came back, cooked it, ate it, then continued programming, and by 9:00, pretty much had my game running with joystick support. Didn’t try to do any rumble effects, and it’s a bit twitchy. Not sure how much of that is just the game not being suited to joystick control, and how much is just my programming needing to be tweaked to modulate the input to smooth things out, and how much is my ineptitude at wielding a joystick.

Here is my joystick code:

It’s quite easy to program, once you plug in the joystick to a USB port, device nodes appear (courtesy of udev) in /dev/input/js0, etc. You just open that up, and do a read (blocking or nonblocking, whichever you like) which gives you an event structure which contains the event type which tells you whether the event is a button press or a stick movement, a number (which indicates what button or axis was pressed or moved, e.g. 0 means stick 1, x axis, (or button 0, depending on the event type). There’s also a value, which contains, er, the value — for button presses, 1 indicates the button was pressed, 0 indicates it was released. Axis movements are signed shorts, ranging from -32768 to 32767. This is easy to figure out what’s what, you just use the “jstest” program.

jstest --normal /dev/input/js0

Then you fiddle with the joystick and watch what jstest prints out. It becomes pretty obvious what the data looks like.

You should also read the kernel documentation. Search google for “joystick-api.txt“, or download the kernel source from kernel.org, and look in Documentation/input/joystick-api.txt.

Very surprising how easy it is to read the joystick from a program.

~ by scaryreasoner on February 22, 2008.

18 Responses to “Programming joysticks with linux”

  1. That’s just great! I tried programming a joystick with SDL, but it sounds like it is just as easy without any libs. I will defiunitly have a go at this. Thanks for the advice!

  2. […] Related post: programming joysticks with linux […]

  3. […] Programming joysticks with linux, part 2 part 1 is here. […]

  4. […] also Part 1, part 2, and some info on getting xbox 360 rumble effect working with […]

  5. Extremely helpful. I am trying to use a joystick to control my robot.

  6. hola que tal saludos interesante, tu publicasion trate de clonar tu programa, para empezar a ver que pasaba pero me tope con este error:

    joystick.c: In function ‘open_joystick’:
    joystick.c:21: error: number of arguments doesn’t match prototype
    joystick.h:34: error: prototype declaration
    make[2]: *** [build/Debug/GNU-Linux-x86/joystick.o] Error 1
    make[1]: *** [.build-conf] Error

    Si pudieras ayuda a resolver el problema, e lo agradecere.

    • Not sure, those line numbers (21 in joystick.c and 34 in joystick.h) do not seem to match up with the error messages very well.

      Latest version seems to work ok for me:

      [scameron@zuul wordwarvi]$ gcc --version
      gcc (GCC) 4.4.5 20110214 (Red Hat 4.4.5-6)
      Copyright (C) 2010 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions.  There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      
      [scameron@zuul wordwarvi]$ gcc -Wall -c joystick.c
      [scameron@zuul wordwarvi]$ 
      [scameron@zull wordwarvi]$ cvs status joystick.c joystick.h
      smcameron@wordwarvi.cvs.sourceforge.net's password: 
      ===================================================================
      File: joystick.c       	Status: Up-to-date
      
         Working revision:	1.5
         Repository revision:	1.5	/cvsroot/wordwarvi/wordwarvi/joystick.c,v
         Sticky Tag:		(none)
         Sticky Date:		(none)
         Sticky Options:	(none)
      
      ===================================================================
      File: joystick.h       	Status: Up-to-date
      
         Working revision:	1.4
         Repository revision:	1.4	/cvsroot/wordwarvi/wordwarvi/joystick.h,v
         Sticky Tag:		(none)
         Sticky Date:		(none)
         Sticky Options:	(none)
      
      [scameron@zuul wordwarvi]$
      
      • I experienced the same issue as a99d. It’s because in your blog post you link to version 1.1 of joystick.c. Perhaps you could remove the version number from that URL?

        Thanks for sharing your code.

      • The commented out test program needs a minor tweak to successfully compile as well. Pass a zero to open_joystick. To get rid of a warning, return 0 when the infinite loop ends 🙂

  7. I created a tarball package of the library, using autoconf and automake.
    It’s ready for use ./configure && make && make install.
    The structure is for my implementation for my joystick library based in your code, even has a test program, etc.

  8. So, I don’t upload the code tarball, I’m still working on it. Would be ready in a mouth. I will upload in sourceforge.net, but I created a your version code with autotools, I can send your email in tarball.

    • And have a pkg-config .pc file…

    • No need to email, I just wondered if you had anything on sourceforge or github or some place else, but it sounds like you’re still working on it. Glad you found my little example helpful.

      • Well, I will upload the tarball in sourceforge.net, the first version will is based in your code only, before I will add new code, the project will name “joysticklib”

  9. Thank you for providing the source files. They work. :hattip:

    -Anisha-

  10. Thank you for the post and thank you so much for the code 😉

Leave a comment