GLUT Joystick Support for Linux

I implemented joystick support for GLUT under Linux. The API is fully compatible to Win32 version of the GLUT. So you can use the same code to use joystick both on Linux and Win32 systems. Since the GLUT developlment for X Window System is not active now, I just put my modified GLUT. If there is license issue, let me know.

To compile GLUT, you can just type make. The resulting library is lib/glut/libglut.so.3. You can just copy this shared library and header file under include/glut.h to wherever you want.

To see if the modified GLUT works with joystick, you can run joy_test program under test/glut directory. After you turn on joystick from the menu which is appeared by pressing right mouse button, you can move the teapot with your joystick. You can also change the color of teapot by pressing joystick buttons.

Controlling joystick is pretty easy. You just need to use the following two functions:

void glutInitJoystick(const char *joystickDevice)

Open and initialize the joystickDevice . Usually joystickDevice would be /dev/input/js0 .
void glutJoystickFunc(GLUTjoystickCB joystickFunc, int pollInterval)

Register the joystick callback joystickFunc and set the polling interval for the callback to pollInterval.
Function signature of the callback is void joystickFunc(unsigned int buttonMask, int x, int y, int z).

buttonMask returns the current state of the buttons. You can get the particular button state by
(buttonMask & 0x1) << which
where which is the button number that you want to retrieve the state. Button numbering is starting from 0.

x, y, z return the value of the first, second, and third axis. Their range is signed short ([-32768-32767]). It's odd to call x, y, z, but it's what is used for Win32 implementation of the joystick support. It's not my fault!

Download GLUT with Joystick support


Last modified: Fri May 7 02:13:35 MDT 2004
tkh at cs.unm.edu