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.
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!
Andy said this on July 2, 2008 at 12:57 pm |
[…] Related post: programming joysticks with linux […]
Getting the Xbox controller to work with linux, including rumble effect. « Scary Reasoner said this on July 19, 2008 at 3:00 am |
[…] Programming joysticks with linux, part 2 part 1 is here. […]
Programming joysticks with linux, part 2 « Scary Reasoner said this on July 19, 2008 at 7:50 pm |
[…] also Part 1, part 2, and some info on getting xbox 360 rumble effect working with […]
Programming joysticks with linux part 3, Get Ready to Rumble!!! « Scary Reasoner said this on July 19, 2008 at 11:25 pm |
Extremely helpful. I am trying to use a joystick to control my robot.
Pratik Agarwal said this on July 6, 2010 at 11:54 pm |
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.
a99d said this on June 8, 2011 at 8:43 pm |
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:
scaryreasoner said this on June 8, 2011 at 9:25 pm |
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.
Drew Noakes said this on January 26, 2013 at 9:31 pm
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 🙂
Drew Noakes said this on January 26, 2013 at 9:37 pm
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.
Joel Peláez Jorge said this on November 1, 2011 at 9:13 pm |
Cool. Got a link to your code somewhere?
scaryreasoner said this on November 1, 2011 at 10:08 pm |
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.
Joel Peláez Jorge said this on November 2, 2011 at 2:32 pm |
And have a pkg-config .pc file…
Joel Peláez Jorge said this on November 2, 2011 at 2:36 pm |
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.
scaryreasoner said this on November 2, 2011 at 7:52 pm |
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”
darkmega0 said this on November 4, 2011 at 3:14 am
Thank you for providing the source files. They work. :hattip:
-Anisha-
anishakaul said this on November 8, 2011 at 5:04 am |
Glad you found it to be helpful. If you’re working with xbox 360 controllers, the rumble effect can be made to work as well.
scaryreasoner said this on November 8, 2011 at 5:26 am |
Thank you for the post and thank you so much for the code 😉
Morg4n said this on January 14, 2013 at 8:08 pm |