Commit 5a1cf05f authored by Robert Menzel's avatar Robert Menzel

added linux, QT joystick support which is not used yet and inactive as...

added linux, QT joystick support which is not used yet and inactive as ACGL_EXPERIMENTAL will not be set.
parent 99d04661
......@@ -8,6 +8,7 @@
#include <ACGL/ACGL.hh>
#include <string>
#include <linuxjoystick/Joystick.h>
namespace ACGL{
namespace HardwareSupport{
......@@ -113,13 +114,13 @@ private:
//
// GLFW specifics: replace this to support joysticks with other APIs
//
//#ifdef ACGL_COMPILE_WITH_GLFW
#ifdef ACGL_COMPILE_WITH_GLFW
const unsigned char *mGLFWButtons;
const float *mGLFWAxes;
unsigned char *mLastGLFWButtonState;
int mGLFWGamePadNumber;
//#endif
#endif
};
ACGL_SMARTPOINTER_TYPEDEFS(GamePad)
......
/*
* Based on http://andreasvolz.wordpress.com/2008/01/24/a-linux-joystick-class-for-c/
* by Andreas Volz, released under the MIT license.
* Ported to QT threads from Philip Trettner.
*
* Define ACGL_COMPILE_WITH_LINUX_JOYSTICK_SUPPORT to use this. Access the joystick/gamepads
* using the ACGL wrapper HardwareSupport::GamePad .
*
*
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.
*/
#pragma once
//
// This joystick interface uses QT just for threading and only works on linux
// as it uses native linux APIs.
// Only use ACGL_OWN_LINUX_JOYSTICK to test if this gets used.
//
// TODO: get away from QT and use native C++11 threads to minimize dependencies.
//
#if defined(ACGL_COMPILE_WITH_QT) && defined(__linux__) && defined(ACGL_EXPERIMENTAL)
#define ACGL_OWN_LINUX_JOYSTICK
#endif
#ifdef ACGL_OWN_LINUX_JOYSTICK
#include <linux/joystick.h>
#include <string>
#include <queue>
#include <QMutex>
#include <QThread>
// for more info about the Linux Joystick API read
// /usr/src/linux/Documentation/input/joystick-api.txt
struct EventJoystick
{
int32_t time;
int16_t value;
int8_t number;
bool synthetic;
};
// TODO: configurable joystick device; best a manager for autodetect...
class Joystick : QThread //: public sigc::trackable
{
public:
//sigc::signal <void, const EventJoystick&> signalAxis;
//sigc::signal <void, const EventJoystick&> signalButton;
Joystick ();
virtual ~Joystick ();
/* Open a joystick device.
* @param device A device (e.g. /dev/input/jsX).
*/
bool open (const std::string &device);
/* Close the joystick device.
*/
bool close ();
/*
* @return Number of available buttons.
* @return -1 Initializing not finished.
*/
int getNumberOfButtons ();
/*
* @return Number of available axis.
* @return -1 Initializing not finished.
*/
int getNumberOfAxes ();
/*
* @return Identifier string of the Joystick
*/
const std::string &getIdentifier ();
/// polls one event
/// Returns true if an event was found
bool pollEventButton(EventJoystick &_event);
bool pollEventAxis(EventJoystick &_event);
private: // intentionally not implemented
Joystick (const Joystick&);
Joystick& operator = (const Joystick&);
private:
std::queue<EventJoystick> mEventsBtn;
std::queue<EventJoystick> mEventsAxis;
QMutex mBtnMutex;
QMutex mAxisMutex;
struct js_event joy_event;
int m_fd;
bool m_init;
int m_axes;
int m_buttons;
std::string m_name;
bool m_run;
void run ();
};
#endif // ACGL_OWN_LINUX_JOYSTICK
......@@ -24,13 +24,18 @@ GamePad::GamePad( int _n )
{
assert( _n != 0 );
#ifndef ACGL_COMPILE_WITH_GLFW
assert( 0 && "compiled without any gamepad supporting library, gamepad will always report that no buttons are pressed" );
warning() << "compiled without any gamepad supporting library, gamepad will always report that no buttons are pressed" << endl;
#endif
mGamePadOK = false;
mLastGLFWButtonState = NULL;
mAxes = NULL;
mAxesMultiplier = NULL;
mMinSensitivity = 0.0f;
#ifdef ACGL_COMPILE_WITH_GLFW
mLastGLFWButtonState = NULL;
int numberOfJoysticksFound = 0;
for (int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; ++i) {
if (glfwJoystickPresent(i) == GL_TRUE) {
......@@ -129,7 +134,9 @@ GamePad::GamePad( int _n )
GamePad::~GamePad()
{
#ifdef ACGL_COMPILE_WITH_GLFW
delete[] mLastGLFWButtonState;
#endif
delete[] mAxes;
delete[] mAxesMultiplier;
}
......@@ -137,7 +144,12 @@ GamePad::~GamePad()
bool GamePad::isPressedRaw( unsigned int _button )
{
if ( (int)_button > mNumberOfButtons) return false;
#ifdef ACGL_COMPILE_WITH_GLFW
return ( mGLFWButtons[_button] == 1 );
#else
return false;
#endif
}
bool GamePad::isPressed( GamePadButton _button )
......@@ -155,7 +167,11 @@ bool GamePad::buttonStateChanged( unsigned int _button )
{
if ( (int)_button > mNumberOfButtons) return false;
#ifdef ACGL_COMPILE_WITH_GLFW
return (mGLFWButtons[_button] != mLastGLFWButtonState[_button]);
#else
return false;
#endif
}
......@@ -232,7 +248,15 @@ void GamePad::update()
void GamePad::printState()
{
if (!ok()) return;
#ifndef ACGL_COMPILE_WITH_GLFW
warning() << "compiled without any gamepad supporting library, gamepad will always report that no buttons are pressed" << endl;
return;
#endif
if (!ok()) {
debug() << "no gamepad found - restarting the application might be needed after plugging in a gamepad" << endl;
return;
}
#ifdef ACGL_COMPILE_WITH_GLFW
for (int i = 0; i < mNumberOfButtons; ++i) {
......
/*
* See header for details and license.
*/
#include "linuxjoystick/Joystick.h"
#ifdef ACGL_OWN_LINUX_JOYSTICK
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
Joystick::Joystick () :
m_init (false),
m_axes (0),
m_buttons (0),
m_run (true)
{
}
Joystick::~Joystick ()
{
this->close ();
}
bool Joystick::open (const string &device)
{
m_fd = ::open (device.c_str(), O_RDONLY);
if (m_fd == -1)
{
cerr << "Error opening joystick device!" << endl;
return false;
}
else
{
char buttons;
char axes;
char name[128];
// get number of buttons
ioctl (m_fd, JSIOCGBUTTONS, &buttons);
m_buttons = buttons;
// get number of axes
ioctl (m_fd, JSIOCGAXES, &axes);
m_axes = axes;
// get device name
if (ioctl(m_fd, JSIOCGNAME (sizeof(name)), name) < 0)
{
m_name = "Unknown";
}
else
{
m_name = name;
}
/* TODO: support those if needed
* #define JSIOCGVERSION // get driver version
* #define JSIOCSCORR // set correction values
* #define JSIOCGCORR // get correction values
*/
this->start();
//thread = Glib::Thread::create (sigc::mem_fun (*this, &Joystick::loop), false);
m_run = true;
}
return true;
}
bool Joystick::close ()
{
// end thread
m_run = false;
// reset some values
m_init = false;
m_axes = 0;
m_buttons = 0;
return !::close (m_fd);
}
void Joystick::run ()
{
this->setTerminationEnabled(true);
// wait for all synthetic event until the first real event comes
// then we've all available axis and buttons.
while (m_run && isRunning())
{
EventJoystick eventJoy;
ssize_t n = read (m_fd, &joy_event, sizeof(struct js_event));
if (!n)
std::cerr << "No joy_event could be read\n";
eventJoy.time = joy_event.time;
eventJoy.value = joy_event.value;
switch (joy_event.type)
{
case JS_EVENT_BUTTON:
if (!m_init) m_init = true;
eventJoy.number = joy_event.number;
eventJoy.synthetic = false;
{
QMutexLocker locker(&mBtnMutex);
mEventsBtn.push(eventJoy);
(void)locker;
}
//signalButton.emit (eventJoy);
break;
case JS_EVENT_AXIS:
if (!m_init) m_init = true;
eventJoy.number = joy_event.number;
eventJoy.synthetic = false;
{
QMutexLocker locker(&mAxisMutex);
mEventsAxis.push(eventJoy);
(void)locker;
}
//signalAxis.emit (eventJoy);
break;
case JS_EVENT_BUTTON | JS_EVENT_INIT:
if (m_init) // skip the synthetic events on driver start
{
eventJoy.number = joy_event.number & ~JS_EVENT_INIT;
eventJoy.synthetic = true;
{
QMutexLocker locker(&mBtnMutex);
mEventsBtn.push(eventJoy);
(void)locker;
}
//signalButton.emit (eventJoy);
}
break;
case JS_EVENT_AXIS | JS_EVENT_INIT:
if (m_init) // skip the synthetic events on driver start
{
eventJoy.number = joy_event.number & ~JS_EVENT_INIT;
eventJoy.synthetic = true;
{
QMutexLocker locker(&mAxisMutex);
mEventsAxis.push(eventJoy);
(void)locker;
}
//signalAxis.emit (eventJoy);
}
break;
default: // we should never reach this point
printf ("unknown event: %x\n", joy_event.type);
}
}
}
int Joystick::getNumberOfButtons ()
{
return m_buttons;
}
int Joystick::getNumberOfAxes ()
{
return m_axes;
}
const string &Joystick::getIdentifier ()
{
return m_name;
}
bool Joystick::pollEventButton(EventJoystick &_event)
{
if ( mEventsBtn.size() == 0 ) return false;
QMutexLocker locker(&mBtnMutex);
(void)locker;
if ( mEventsBtn.size() > 0 )
{
EventJoystick ev = mEventsBtn.front();
mEventsBtn.pop();
_event = ev;
return true;
}
return false;
}
bool Joystick::pollEventAxis(EventJoystick &_event)
{
if ( mEventsAxis.size() == 0 ) return false;
QMutexLocker locker(&mAxisMutex);
(void)locker;
if ( mEventsAxis.size() > 0 )
{
EventJoystick ev = mEventsAxis.front();
mEventsAxis.pop();
_event = ev;
return true;
}
return false;
}
#endif // ACGL_OWN_LINUX_JOYSTICK
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment