From 3c6a4bc17ab8e798c55cc11e1dfa9372ebc2f1ba Mon Sep 17 00:00:00 2001 From: Gregory Dymarek Date: Sat, 23 Apr 2016 08:36:04 +0100 Subject: [PATCH] Fixing issue with initialization --- qgroundcontrol.pro | 4 ++ src/Joystick/JoystickAndroid.cc | 74 +++++++++++++++++++-------------- src/Joystick/JoystickAndroid.h | 2 +- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/qgroundcontrol.pro b/qgroundcontrol.pro index c312b105b..0db9bfc5b 100644 --- a/qgroundcontrol.pro +++ b/qgroundcontrol.pro @@ -17,6 +17,10 @@ # along with QGroundControl. If not, see . # ------------------------------------------------- +#QMAKE_CFLAGS_DEBUG += -g +#QMAKE_CXXFLAGS += -g +#QMAKE_CXXFLAGS_DEBUG += -g + exists($${OUT_PWD}/qgroundcontrol.pro) { error("You must use shadow build (e.g. mkdir build; cd build; qmake ../qgroundcontrol.pro).") } diff --git a/src/Joystick/JoystickAndroid.cc b/src/Joystick/JoystickAndroid.cc index 14bdcf9b8..3036ce86d 100644 --- a/src/Joystick/JoystickAndroid.cc +++ b/src/Joystick/JoystickAndroid.cc @@ -8,46 +8,42 @@ int JoystickAndroid::_androidBtnListCount; int *JoystickAndroid::_androidBtnList; QMutex JoystickAndroid::m_mutex; -JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManager* multiVehicleManager) - : Joystick(name,0,0,multiVehicleManager) //buttonCount and axisCount is computed below +JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager) + : Joystick(name,axisCount,buttonCount,multiVehicleManager) , deviceId(id) { int i; QAndroidJniEnvironment env; - QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", id); + //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); - //get number of buttons - jintArray a = env->NewIntArray(_androidBtnListCount); - env->SetIntArrayRegion(a,0,_androidBtnListCount,_androidBtnList); - QAndroidJniObject btns = inputDevice.callObjectMethod("hasKeys", "([I)[Z", a); + //set button mapping (number->code) + jintArray b = env->NewIntArray(_androidBtnListCount); + env->SetIntArrayRegion(b,0,_androidBtnListCount,_androidBtnList); + + QAndroidJniObject btns = inputDevice.callObjectMethod("hasKeys", "([I)[Z", b); jbooleanArray jSupportedButtons = btns.object(); - int btn_sz = env->GetArrayLength(jSupportedButtons); jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr); - _buttonCount=0; - for (i=0;iReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0); - //get number of axis - QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;"); - _axisCount = rangeListNative.callMethod("size"); + //set axis mapping (number->code) 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()) @@ -55,7 +51,6 @@ JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManage axisValue[i] = 0; } - _axisCount = 4; qDebug() << "axis:" <<_axisCount << "buttons:" <<_buttonCount; QtAndroidPrivate::registerGenericMotionEventListener(this); @@ -63,7 +58,6 @@ JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManage } JoystickAndroid::~JoystickAndroid() { - delete btnCode; delete axisCode; delete btnValue; @@ -79,7 +73,7 @@ bool JoystickAndroid::handleKeyEvent(jobject event) { const int _deviceId = ev.callMethod("getDeviceId", "()I"); if (_deviceId!=deviceId) return false; - qDebug() << "handleKeyEvent!" << deviceId; + //qDebug() << "handleKeyEvent!" << deviceId; return true; } @@ -89,7 +83,7 @@ bool JoystickAndroid::handleGenericMotionEvent(jobject event) { const int _deviceId = ev.callMethod("getDeviceId", "()I"); if (_deviceId!=deviceId) return false; - qDebug() << "handleMotionEvent!" << deviceId; + //qDebug() << "handleMotionEvent!" << deviceId; return true; } @@ -104,13 +98,13 @@ QMap JoystickAndroid::discover(MultiVehicleManager* _multiVe QAndroidJniEnvironment env; QAndroidJniObject o = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDeviceIds"); jintArray jarr = o.object(); - size_t sz = env->GetArrayLength(jarr); + int sz = env->GetArrayLength(jarr); jint *buff = env->GetIntArrayElements(jarr, nullptr); int SOURCE_GAMEPAD = QAndroidJniObject::getStaticField("android/view/InputDevice", "SOURCE_GAMEPAD"); int SOURCE_JOYSTICK = QAndroidJniObject::getStaticField("android/view/InputDevice", "SOURCE_JOYSTICK"); - for (size_t i = 0; i < sz; ++i) { + for (int i = 0; i < sz; ++i) { QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", buff[i]); int sources = inputDevice.callMethod("getSources", "()I"); if (((sources & SOURCE_GAMEPAD) != SOURCE_GAMEPAD) //check if the input device is interesting to us @@ -121,13 +115,29 @@ QMap JoystickAndroid::discover(MultiVehicleManager* _multiVe QString name = inputDevice.callObjectMethod("getName", "()Ljava/lang/String;").toString(); - if (joystickFound) { //skipping { - qWarning() << "Skipping joystick:" << name; - continue; - } + if (joystickFound) { //skipping { + qWarning() << "Skipping joystick:" << name; + continue; + } + + //get number of axis + QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;"); + int axisCount = rangeListNative.callMethod("size"); + + //get number of buttons + jintArray a = env->NewIntArray(_androidBtnListCount); + env->SetIntArrayRegion(a,0,_androidBtnListCount,_androidBtnList); + QAndroidJniObject btns = inputDevice.callObjectMethod("hasKeys", "([I)[Z", a); + jbooleanArray jSupportedButtons = btns.object(); + jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr); + int buttonCount = 0; + for (i=0;i<_androidBtnListCount;i++) + if (supportedButtons[i]) buttonCount++; + env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0); + + qDebug() << "\t" << name << "id:" << buff[i] << "axes:" << axisCount << "buttons:" << buttonCount; - qDebug() << "\t" << name << "id:" << buff[i]; - ret[name] = new JoystickAndroid(name, buff[i], _multiVehicleManager); + ret[name] = new JoystickAndroid(inputDevice, name, axisCount, buttonCount, buff[i], _multiVehicleManager); joystickFound = true; } diff --git a/src/Joystick/JoystickAndroid.h b/src/Joystick/JoystickAndroid.h index 48f83ec29..7afca4169 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 QString& name, int id, MultiVehicleManager* multiVehicleManager); + JoystickAndroid(const QAndroidJniObject &inputDevice, const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager); ~JoystickAndroid(); static QMap discover(MultiVehicleManager* _multiVehicleManager); -- 2.22.0