From 1c19920f4f736f623c696e5ff633caf212532ead Mon Sep 17 00:00:00 2001 From: Gregory Dymarek Date: Sat, 23 Apr 2016 10:17:24 +0100 Subject: [PATCH] Implementing handlers --- src/Joystick/JoystickAndroid.cc | 89 +++++++++++++++++++++++------------------ src/Joystick/JoystickAndroid.h | 5 ++- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/Joystick/JoystickAndroid.cc b/src/Joystick/JoystickAndroid.cc index 3036ce8..f9eef36 100644 --- a/src/Joystick/JoystickAndroid.cc +++ b/src/Joystick/JoystickAndroid.cc @@ -6,19 +6,18 @@ int JoystickAndroid::_androidBtnListCount; int *JoystickAndroid::_androidBtnList; +int JoystickAndroid::ACTION_DOWN; +int JoystickAndroid::ACTION_UP; QMutex JoystickAndroid::m_mutex; -JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager) +JoystickAndroid::JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager) : Joystick(name,axisCount,buttonCount,multiVehicleManager) , deviceId(id) { int i; QAndroidJniEnvironment env; - - //if we define inputDevice in here this will fail for some reason, hence passing through argument - //QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", id); - + QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", id); //set button mapping (number->code) jintArray b = env->NewIntArray(_androidBtnListCount); @@ -44,15 +43,14 @@ JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QSt axisValue = new int[_axisCount]; axisCode = new int[_axisCount]; QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;"); - for (i=0;i<_axisCount;i++) { - QAndroidJniObject range = rangeListNative.callObjectMethod("get", "()Landroid/view/InputDevice/MotionRange;"); - if (range.isValid()) - axisCode[i] = range.callMethod("getAxis"); + for (i=0;i<_axisCount;i++) { + QAndroidJniObject range = rangeListNative.callObjectMethod("get", "(I)Ljava/lang/Object;",i); + axisCode[i] = range.callMethod("getAxis"); axisValue[i] = 0; } - qDebug() << "axis:" <<_axisCount << "buttons:" <<_buttonCount; + qCDebug(JoystickLog) << "axis:" <<_axisCount << "buttons:" <<_buttonCount; QtAndroidPrivate::registerGenericMotionEventListener(this); QtAndroidPrivate::registerKeyEventListener(this); } @@ -67,31 +65,11 @@ JoystickAndroid::~JoystickAndroid() { QtAndroidPrivate::unregisterKeyEventListener(this); } -bool JoystickAndroid::handleKeyEvent(jobject event) { - QJNIObjectPrivate ev(event); - QMutexLocker lock(&m_mutex); - const int _deviceId = ev.callMethod("getDeviceId", "()I"); - if (_deviceId!=deviceId) return false; - - //qDebug() << "handleKeyEvent!" << deviceId; - return true; -} - -bool JoystickAndroid::handleGenericMotionEvent(jobject event) { - QJNIObjectPrivate ev(event); - QMutexLocker lock(&m_mutex); - const int _deviceId = ev.callMethod("getDeviceId", "()I"); - if (_deviceId!=deviceId) return false; - - //qDebug() << "handleMotionEvent!" << deviceId; - return true; -} - QMap JoystickAndroid::discover(MultiVehicleManager* _multiVehicleManager) { bool joystickFound = false; static QMap ret; - _buttonList(); //it's enough to run it once, should be in a static constructor + _initStatic(); //it's enough to run it once, should be in a static constructor QMutexLocker lock(&m_mutex); @@ -131,13 +109,13 @@ QMap JoystickAndroid::discover(MultiVehicleManager* _multiVe jbooleanArray jSupportedButtons = btns.object(); jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr); int buttonCount = 0; - for (i=0;i<_androidBtnListCount;i++) - if (supportedButtons[i]) buttonCount++; + for (int j=0;j<_androidBtnListCount;j++) + if (supportedButtons[j]) buttonCount++; env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0); - qDebug() << "\t" << name << "id:" << buff[i] << "axes:" << axisCount << "buttons:" << buttonCount; + qCDebug(JoystickLog) << "\t" << name << "id:" << buff[i] << "axes:" << axisCount << "buttons:" << buttonCount; - ret[name] = new JoystickAndroid(inputDevice, name, axisCount, buttonCount, buff[i], _multiVehicleManager); + ret[name] = new JoystickAndroid(name, axisCount, buttonCount, buff[i], _multiVehicleManager); joystickFound = true; } @@ -147,6 +125,40 @@ QMap JoystickAndroid::discover(MultiVehicleManager* _multiVe return ret; } + +bool JoystickAndroid::handleKeyEvent(jobject event) { + QJNIObjectPrivate ev(event); + QMutexLocker lock(&m_mutex); + const int _deviceId = ev.callMethod("getDeviceId", "()I"); + if (_deviceId!=deviceId) return false; + + const int action = ev.callMethod("getAction", "()I"); + const int keyCode = ev.callMethod("getKeyCode", "()I"); + + for (int i=0;i<_buttonCount;i++) { + if (btnCode[i]==keyCode) { + if (action==ACTION_DOWN) btnValue[i] = true; + if (action==ACTION_UP) btnValue[i] = false; + return true; + } + } + return false; +} + +bool JoystickAndroid::handleGenericMotionEvent(jobject event) { + QJNIObjectPrivate ev(event); + QMutexLocker lock(&m_mutex); + const int _deviceId = ev.callMethod("getDeviceId", "()I"); + if (_deviceId!=deviceId) return false; + + for (int i=0;i<_axisCount;i++) { + const float v = ev.callMethod("getAxisValue", "(I)F",axisCode[i]); + axisValue[i] = (int)(v*32767.f); + } + return true; +} + + bool JoystickAndroid::open(void) { return true; } @@ -169,7 +181,7 @@ int JoystickAndroid::getAxis(int i) { //helper method -void JoystickAndroid::_buttonList() { +void JoystickAndroid::_initStatic() { //this gets list of all possible buttons - this is needed to check how many buttons our gamepad supports //instead of the whole logic below we could have just a simple array of hardcoded int values as these 'should' not change @@ -202,8 +214,7 @@ void JoystickAndroid::_buttonList() { ret[i++] = QAndroidJniObject::getStaticField("android/view/KeyEvent", "KEYCODE_BUTTON_Y"); ret[i++] = QAndroidJniObject::getStaticField("android/view/KeyEvent", "KEYCODE_BUTTON_Z"); - for (int j=0;j<_androidBtnListCount;j++) - qDebug() << "\tpossible button: "+QString::number(_androidBtnList[j]); - + ACTION_DOWN = QAndroidJniObject::getStaticField("android/view/KeyEvent", "ACTION_DOWN"); + ACTION_UP = QAndroidJniObject::getStaticField("android/view/KeyEvent", "ACTION_UP"); } diff --git a/src/Joystick/JoystickAndroid.h b/src/Joystick/JoystickAndroid.h index 7afca41..75ea813 100644 --- a/src/Joystick/JoystickAndroid.h +++ b/src/Joystick/JoystickAndroid.h @@ -15,7 +15,7 @@ class JoystickAndroid : public Joystick, public QtAndroidPrivate::GenericMotionEventListener, public QtAndroidPrivate::KeyEventListener { public: - JoystickAndroid(const QAndroidJniObject &inputDevice, const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager); + JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager); ~JoystickAndroid(); static QMap discover(MultiVehicleManager* _multiVehicleManager); @@ -36,10 +36,11 @@ private: bool *btnValue; int *axisValue; - static void _buttonList(); + static void _initStatic(); static int * _androidBtnList; //list of all possible android buttons static int _androidBtnListCount; + static int ACTION_DOWN, ACTION_UP; static QMutex m_mutex; int deviceId;