Posted Feb 23, 2009 at 9:05:00 PM
Subject: Programming Gamepads in Linux
I'm new to Linux and using it for the past 10 days and I'm quite excited about it. But I'm not new to programming and decide to start with some basic C programming. Now I found something that's really interesting and exciting. So I'm sharing with you all and want to get some ideas and tips.
I like to play lot of games and I have 2 gamepads, Saitek P2600 and Frontech. And these work well on Windows and I searched for drivers to make them work on Linux. Fortunately I didnt find one. Yes fortunately, because that lead me to program them myself.
God bless Linux filesystem and handling all devices as files. When I connected my gamepad I noticed 4 new files got added to the /dev folder, one of them was hidrawX(it was hidraw3 when I tried). I assumed it to mean Human Interface Device - Raw mode or something like that. Then I tried to display the content of this hidraw file using the cat command and noticed that the device was constantly sending some signals(by writing to the file). And I was not able to recognize anything as they were just some symbols. But when I pressed any key on the gamepad, the symbols changed. So I decided to decipher them and wrote a simple program to read a file which will read a byte at a time and display the value as a number rather than an ASCII character. When I analysed the output numbers they formed a repeating pattern of N bytes(7 bytes for my saitek and 8 bytes for my frontech). So then changed the code to display N bytes at a time as B1-B2-B3...-BN. Now when I pressed a key on the gamepad one of the N bytes changed to a different value and when I released it returned back to the original value.
So I wrote 2 C programs, one(readrawsignals.c) to read the raw signal and display them as decimal numbers to analyse the signals and the other(gamepadkeys.c) which takes a config file as input(the config file has the meaning of individual bytes of the N bytes read and gamepad key mapping) and displays which key is pressed in a more meaningful way.
I dont find an option to attach files in the forum. So let me know how I can share them with you all.
Now I would like to know few things to proceed with further development. (1) Is there a way to auto recognize when a new device is attached and to identify the device filename of the new device? (2) Currently the C programs run in console mode and display a message saying "Button X pressed" or "Left Stick(140, 127)". I want to send keyboard and mouse messages to the system when a key is pressed to make the app more useful instead of just simply printing a message. This way we'll be able to use the gamepad for any games not just the ones that support gamepads. (3) The app should be able to run in the background with out a GUI and should have an icon created near the clock to bring it up to load config or change settings.
My email Id is, email@example.com. If you are interested send me a mail and I'll send you the code.