Skip to content

Commit

Permalink
Joystick: Fixup Joystick Threading
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Oct 8, 2024
1 parent 98cb3d4 commit 32fefac
Show file tree
Hide file tree
Showing 12 changed files with 932 additions and 870 deletions.
1,169 changes: 625 additions & 544 deletions src/Joystick/Joystick.cc

Large diffs are not rendered by default.

552 changes: 269 additions & 283 deletions src/Joystick/Joystick.h

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions src/Joystick/JoystickAndroid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "JoystickAndroid.h"
#include "JoystickManager.h"
#include "MultiVehicleManager.h"
#include "QGCLoggingCategory.h"

#include <QtCore/QJniEnvironment>
Expand All @@ -32,8 +31,8 @@ static void clear_jni_exception()
}
}

JoystickAndroid::JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager)
: Joystick(name,axisCount,buttonCount,0,multiVehicleManager)
JoystickAndroid::JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id)
: Joystick(name,axisCount,buttonCount,0)
, deviceId(id)
{
int i;
Expand Down Expand Up @@ -95,7 +94,7 @@ JoystickAndroid::~JoystickAndroid() {
}


QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVehicleManager) {
QMap<QString, Joystick*> JoystickAndroid::discover() {
static QMap<QString, Joystick*> ret;

QMutexLocker lock(&m_mutex);
Expand Down Expand Up @@ -144,7 +143,7 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe

qCDebug(JoystickLog) << "\t" << name << "id:" << buff[i] << "axes:" << axisCount << "buttons:" << buttonCount;

ret[name] = new JoystickAndroid(name, axisCount, buttonCount, buff[i], _multiVehicleManager);
ret[name] = new JoystickAndroid(name, axisCount, buttonCount, buff[i]);
}

for (auto i = ret.begin(); i != ret.end();) {
Expand Down Expand Up @@ -205,15 +204,18 @@ bool JoystickAndroid::_update(void)
return true;
}

bool JoystickAndroid::_getButton(int i) {
bool JoystickAndroid::_getButton(int i) const
{
return btnValue[ i ];
}

int JoystickAndroid::_getAxis(int i) {
int JoystickAndroid::_getAxis(int i) const
{
return axisValue[ i ];
}

int JoystickAndroid::_getAndroidHatAxis(int axisHatCode) {
int JoystickAndroid::_getAndroidHatAxis(int axisHatCode) const
{
for(int i = 0; i < _axisCount; i++) {
if (axisCode[i] == axisHatCode) {
return _getAxis(i);
Expand All @@ -222,7 +224,8 @@ int JoystickAndroid::_getAndroidHatAxis(int axisHatCode) {
return 0;
}

bool JoystickAndroid::_getHat(int hat,int i) {
bool JoystickAndroid::_getHat(int hat,int i) const
{
// Android supports only one hat button
if (hat != 0) {
return false;
Expand Down
19 changes: 9 additions & 10 deletions src/Joystick/JoystickAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,33 @@
#include "Joystick.h"
#include <QtCore/private/qandroidextras_p.h>

class MultiVehicleManager;
class JoystickManager;

class JoystickAndroid : public Joystick, public QtAndroidPrivate::GenericMotionEventListener, public QtAndroidPrivate::KeyEventListener
{
public:
JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager);
JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id);

~JoystickAndroid();

static bool init(JoystickManager *manager);

static void setNativeMethods();

static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager);
static QMap<QString, Joystick*> discover();

private:
bool handleKeyEvent(jobject event);
bool handleGenericMotionEvent(jobject event);
int _getAndroidHatAxis(int axisHatCode);
int _getAndroidHatAxis(int axisHatCode) const;

virtual bool _open ();
virtual void _close ();
virtual bool _update ();
bool _open () final;
void _close () final;
bool _update () final;

virtual bool _getButton (int i);
virtual int _getAxis (int i);
virtual bool _getHat (int hat,int i);
bool _getButton (int i) const final;
int _getAxis (int i) const final;
bool _getHat (int hat,int i) const final;

int *btnCode;
int *axisCode;
Expand Down
8 changes: 2 additions & 6 deletions src/Joystick/JoystickManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


#include "JoystickManager.h"
#include "MultiVehicleManager.h"
#include "Joystick.h"
#if defined(QGC_SDL_JOYSTICK)
#include "JoystickSDL.h"
Expand All @@ -27,7 +26,6 @@ QGC_LOGGING_CATEGORY(JoystickManagerLog, "JoystickManagerLog")
JoystickManager::JoystickManager(QGCApplication* app, QGCToolbox* toolbox)
: QGCTool(app, toolbox)
, _activeJoystick(nullptr)
, _multiVehicleManager(nullptr)
{
// qCDebug(JoystickManagerLog) << Q_FUNC_INFO << this;
}
Expand All @@ -48,8 +46,6 @@ void JoystickManager::setToolbox(QGCToolbox *toolbox)
{
QGCTool::setToolbox(toolbox);

_multiVehicleManager = _toolbox->multiVehicleManager();

QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
qmlRegisterUncreatableType<JoystickManager>("QGroundControl.JoystickManager", 1, 0, "JoystickManager", "Reference only");
qmlRegisterUncreatableType<Joystick> ("QGroundControl.JoystickManager", 1, 0, "Joystick", "Reference only");
Expand Down Expand Up @@ -78,9 +74,9 @@ void JoystickManager::_setActiveJoystickFromSettings(void)

#ifdef QGC_SDL_JOYSTICK
// Get the latest joystick mapping
newMap = JoystickSDL::discover(_multiVehicleManager);
newMap = JoystickSDL::discover();
#elif defined(Q_OS_ANDROID)
newMap = JoystickAndroid::discover(_multiVehicleManager);
newMap = JoystickAndroid::discover();
#endif

if (_activeJoystick && !newMap.contains(_activeJoystick->name())) {
Expand Down
2 changes: 0 additions & 2 deletions src/Joystick/JoystickManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

Q_DECLARE_LOGGING_CATEGORY(JoystickManagerLog)

class MultiVehicleManager;
class Joystick;

/// Joystick Manager
Expand Down Expand Up @@ -75,7 +74,6 @@ private slots:
private:
Joystick* _activeJoystick;
QMap<QString, Joystick*> _name2JoystickMap;
MultiVehicleManager* _multiVehicleManager;

int _joystickCheckTimerCounter;
QTimer _joystickCheckTimer;
Expand Down
15 changes: 7 additions & 8 deletions src/Joystick/JoystickSDL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
****************************************************************************/

#include "JoystickSDL.h"
#include "MultiVehicleManager.h"
#include "QGCLoggingCategory.h"

#include <QtCore/QTextStream>
#include <QtCore/QFile>
#include <QtCore/QIODevice>

JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, bool isGameController, MultiVehicleManager* multiVehicleManager)
: Joystick(name,axisCount,buttonCount,hatCount,multiVehicleManager)
JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, bool isGameController)
: Joystick(name,axisCount,buttonCount,hatCount)
, _isGameController(isGameController)
, _index(index)
{
Expand All @@ -41,7 +40,7 @@ bool JoystickSDL::init(void) {
return true;
}

QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicleManager) {
QMap<QString, Joystick*> JoystickSDL::discover() {
static QMap<QString, Joystick*> ret;

QMap<QString,Joystick*> newRet;
Expand Down Expand Up @@ -83,7 +82,7 @@ QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicl
name = QString("%1 %2").arg(originalName).arg(duplicateIdx++);
}

newRet[name] = new JoystickSDL(name, qMax(0,axisCount), qMax(0,buttonCount), qMax(0,hatCount), i, isGameController, _multiVehicleManager);
newRet[name] = new JoystickSDL(name, qMax(0,axisCount), qMax(0,buttonCount), qMax(0,hatCount), i, isGameController);
} else {
newRet[name] = ret[name];
JoystickSDL *j = static_cast<JoystickSDL*>(newRet[name]);
Expand Down Expand Up @@ -166,23 +165,23 @@ bool JoystickSDL::_update(void)
return true;
}

bool JoystickSDL::_getButton(int i) {
bool JoystickSDL::_getButton(int i) const {
if (_isGameController) {
return SDL_GameControllerGetButton(sdlController, SDL_GameControllerButton(i)) == 1;
} else {
return SDL_JoystickGetButton(sdlJoystick, i) == 1;
}
}

int JoystickSDL::_getAxis(int i) {
int JoystickSDL::_getAxis(int i) const {
if (_isGameController) {
return SDL_GameControllerGetAxis(sdlController, SDL_GameControllerAxis(i));
} else {
return SDL_JoystickGetAxis(sdlJoystick, i);
}
}

bool JoystickSDL::_getHat(int hat, int i) {
bool JoystickSDL::_getHat(int hat, int i) const {
uint8_t hatButtons[] = {SDL_HAT_UP,SDL_HAT_DOWN,SDL_HAT_LEFT,SDL_HAT_RIGHT};
if (i < int(sizeof(hatButtons))) {
return (SDL_JoystickGetHat(sdlJoystick, hat) & hatButtons[i]) != 0;
Expand Down
10 changes: 5 additions & 5 deletions src/Joystick/JoystickSDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class MultiVehicleManager;
class JoystickSDL : public Joystick
{
public:
JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, bool isGameController, MultiVehicleManager* multiVehicleManager);
JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, bool isGameController);
~JoystickSDL();

static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager);
static QMap<QString, Joystick*> discover();
static bool init(void);

int index(void) const { return _index; }
Expand All @@ -43,9 +43,9 @@ class JoystickSDL : public Joystick
void _close () final;
bool _update () final;

bool _getButton (int i) final;
int _getAxis (int i) final;
bool _getHat (int hat,int i) final;
bool _getButton (int i) const final;
int _getAxis (int i) const final;
bool _getHat (int hat,int i) const final;

SDL_Joystick* sdlJoystick;
SDL_GameController* sdlController;
Expand Down
2 changes: 2 additions & 0 deletions src/MAVLink/QGCMAVLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ QGCMAVLink::QGCMAVLink(QObject *parent)
: QObject(parent)
{
// qCDebug(StatusTextHandlerLog) << Q_FUNC_INFO << this;

(void) qRegisterMetaType<GRIPPER_ACTIONS>("GRIPPER_ACTIONS");
}

QGCMAVLink::~QGCMAVLink()
Expand Down
1 change: 1 addition & 0 deletions src/MAVLink/QGCMAVLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ class QGCMAVLink : public QObject

static mavlink_status_t* getChannelStatus(mavlink_channel_t channel) { return mavlink_get_channel_status(static_cast<uint8_t>(channel)); }
};
Q_DECLARE_METATYPE(GRIPPER_ACTIONS)
2 changes: 0 additions & 2 deletions src/Utilities/QGCLoggingCategory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ QGC_LOGGING_CATEGORY(RTKGPSLog, "RTKGPSLog")
QGC_LOGGING_CATEGORY(GuidedActionsControllerLog, "GuidedActionsControllerLog")
QGC_LOGGING_CATEGORY(LocalizationLog, "LocalizationLog")
QGC_LOGGING_CATEGORY(VideoAllLog, kVideoAllLogCategory)
QGC_LOGGING_CATEGORY(JoystickLog, "JoystickLog")


QGCLoggingCategoryRegister* _instance = nullptr;

Expand Down
1 change: 0 additions & 1 deletion src/Utilities/QGCLoggingCategory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Q_DECLARE_LOGGING_CATEGORY(RTKGPSLog)
Q_DECLARE_LOGGING_CATEGORY(GuidedActionsControllerLog)
Q_DECLARE_LOGGING_CATEGORY(LocalizationLog)
Q_DECLARE_LOGGING_CATEGORY(VideoAllLog) // turns on all individual QGC video logs
Q_DECLARE_LOGGING_CATEGORY(JoystickLog)

/// @def QGC_LOGGING_CATEGORY
/// This is a QGC specific replacement for Q_LOGGING_CATEGORY. It will register the category name into a
Expand Down

0 comments on commit 32fefac

Please sign in to comment.