A Linux Joystick Class for C++

24 01 2008

For a project I needed to access a joystick in Linux from a C++ application. I wasn’t happy with existing solutions (e.g. SDL) and thought it’s bad to use the plain C API. So here is a easy to use C++ class. It needs glibmm and libsigc++ to compile. The licence is BSD. Have fun with it.

Here are the files:
Joystick.h
Joystick.cpp

See how easy it is to use the class:

Joystick joystick;

EventJoystick m_eventButton;
EventJoystick m_eventAxis;

joystick.open ("/dev/input/js0");

int numButtons = joystick.getNumberOfButtons ();
int numAxis = joystick.getNumberOfAxes ();
cout << "Joystick identifier: " << joystick.getIdentifier () << endl;

m_axisDispatcher.signalDispatch.connect (sigc::mem_fun (this, &ControlManager::onJoystickAxis));
m_buttonDispatcher.signalDispatch.connect (sigc::mem_fun (this, &ControlManager::onJoystickButton));

joystick.signalAxis.connect (sigc::mem_fun (this, &ControlManager::dispatchAxis));
joystick.signalButton.connect (sigc::mem_fun (this, &ControlManager::dispatchButton));
void ControlManager::dispatchAxis (const EventJoystick &event)
{
  // dispatch axis
}

void ControlManager::dispatchButton (const EventJoystick &event)
{
  // dispatch button
}

void ControlManager::onJoystickAxis (int missedEvents)
{
  // dispatched callback
}

void ControlManager::onJoystickButton (int missedEvents)
{
  // dispatched callback
}

Copyright (C) 2008 Andreas Volz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies of the Software and its Copyright notices. In addition publicly
documented acknowledgment must be given that this software has been used if no
source code of this software is made available publicly. This includes
acknowledgments in either Copyright notices, Manuals, Publicity and Marketing
documents or any documentation provided with any product containing this
software. This License does not apply to any software that links to the
libraries provided by this software (statically or dynamically), but only to
the software provided.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Advertisements

Actions

Information

4 responses

26 07 2010
cidcamp

Hi, thank you for the help.

I think It may be useful for me, but I have a problem:

Where can I paste this code? In my main?

But the hard problem is that ‘m_axisDispatcher’ is not declared. Can answer to me how to declare it.

Thank you very much, and sorry for my spanish english.

26 07 2010
andreasvolz

This isn’t a complete example. Only some pasted code from my application.

You should take a look how to use sigc++. This is essential for the usage of this class.

The Joystick class runs in an extra thread. The dispatcher is needed if you like to dispatch the joystick signals to a main loop (like GTK+ or so).

If you’ve still problems mail me at linux [at] brachttal [dot] net and I’ll help you with an easy example.

27 07 2010
cidcamp

Thank you very much for your interest.

I have found an easier example for Qt Develop, I’m using now QtCreator to develop, so this is perfect for me:

10 12 2012
Using the Linux Joystick API | Jacob Zelek

[…] this time but I think I’d go with the approach taken in this article. I may also take this approach, if I’m willing to use […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: