Commit 51814a3d authored by Gregory Dymarek's avatar Gregory Dymarek

Fixing issue with initialization

parent 8c0e81d9
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
# along with QGroundControl. If not, see <http://www.gnu.org/licenses/>. # along with QGroundControl. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------- # -------------------------------------------------
#QMAKE_CFLAGS_DEBUG += -g
#QMAKE_CXXFLAGS += -g
#QMAKE_CXXFLAGS_DEBUG += -g
exists($${OUT_PWD}/qgroundcontrol.pro) { exists($${OUT_PWD}/qgroundcontrol.pro) {
error("You must use shadow build (e.g. mkdir build; cd build; qmake ../qgroundcontrol.pro).") error("You must use shadow build (e.g. mkdir build; cd build; qmake ../qgroundcontrol.pro).")
} }
......
...@@ -8,46 +8,42 @@ int JoystickAndroid::_androidBtnListCount; ...@@ -8,46 +8,42 @@ int JoystickAndroid::_androidBtnListCount;
int *JoystickAndroid::_androidBtnList; int *JoystickAndroid::_androidBtnList;
QMutex JoystickAndroid::m_mutex; QMutex JoystickAndroid::m_mutex;
JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManager* multiVehicleManager) JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager)
: Joystick(name,0,0,multiVehicleManager) //buttonCount and axisCount is computed below : Joystick(name,axisCount,buttonCount,multiVehicleManager)
, deviceId(id) , deviceId(id)
{ {
int i; int i;
QAndroidJniEnvironment env; 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<jbooleanArray>(); jbooleanArray jSupportedButtons = btns.object<jbooleanArray>();
int btn_sz = env->GetArrayLength(jSupportedButtons);
jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr); jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr);
_buttonCount=0;
for (i=0;i<btn_sz;i++)
if (supportedButtons[i]) _buttonCount++;
//create a mapping table (btnCode) that maps button number with button code //create a mapping table (btnCode) that maps button number with button code
btnValue = new bool[_buttonCount]; btnValue = new bool[_buttonCount];
btnCode = new int[_buttonCount]; btnCode = new int[_buttonCount];
int c = 0; int c = 0;
for (i=0;i<btn_sz;i++) for (i=0;i<_androidBtnListCount;i++)
if (supportedButtons[i]) { if (supportedButtons[i]) {
btnValue[c] = false; btnValue[c] = false;
btnCode[c] = _androidBtnList[i]; btnCode[c] = _androidBtnList[i];
c++; c++;
} }
env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0); env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0);
//get number of axis //set axis mapping (number->code)
QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;");
_axisCount = rangeListNative.callMethod<jint>("size");
axisValue = new int[_axisCount]; axisValue = new int[_axisCount];
axisCode = new int[_axisCount]; axisCode = new int[_axisCount];
QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;");
for (i=0;i<_axisCount;i++) { for (i=0;i<_axisCount;i++) {
QAndroidJniObject range = rangeListNative.callObjectMethod("get", "()Landroid/view/InputDevice/MotionRange;"); QAndroidJniObject range = rangeListNative.callObjectMethod("get", "()Landroid/view/InputDevice/MotionRange;");
if (range.isValid()) if (range.isValid())
...@@ -55,7 +51,6 @@ JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManage ...@@ -55,7 +51,6 @@ JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManage
axisValue[i] = 0; axisValue[i] = 0;
} }
_axisCount = 4;
qDebug() << "axis:" <<_axisCount << "buttons:" <<_buttonCount; qDebug() << "axis:" <<_axisCount << "buttons:" <<_buttonCount;
QtAndroidPrivate::registerGenericMotionEventListener(this); QtAndroidPrivate::registerGenericMotionEventListener(this);
...@@ -63,7 +58,6 @@ JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManage ...@@ -63,7 +58,6 @@ JoystickAndroid::JoystickAndroid(const QString& name, int id, MultiVehicleManage
} }
JoystickAndroid::~JoystickAndroid() { JoystickAndroid::~JoystickAndroid() {
delete btnCode; delete btnCode;
delete axisCode; delete axisCode;
delete btnValue; delete btnValue;
...@@ -79,7 +73,7 @@ bool JoystickAndroid::handleKeyEvent(jobject event) { ...@@ -79,7 +73,7 @@ bool JoystickAndroid::handleKeyEvent(jobject event) {
const int _deviceId = ev.callMethod<jint>("getDeviceId", "()I"); const int _deviceId = ev.callMethod<jint>("getDeviceId", "()I");
if (_deviceId!=deviceId) return false; if (_deviceId!=deviceId) return false;
qDebug() << "handleKeyEvent!" << deviceId; //qDebug() << "handleKeyEvent!" << deviceId;
return true; return true;
} }
...@@ -89,7 +83,7 @@ bool JoystickAndroid::handleGenericMotionEvent(jobject event) { ...@@ -89,7 +83,7 @@ bool JoystickAndroid::handleGenericMotionEvent(jobject event) {
const int _deviceId = ev.callMethod<jint>("getDeviceId", "()I"); const int _deviceId = ev.callMethod<jint>("getDeviceId", "()I");
if (_deviceId!=deviceId) return false; if (_deviceId!=deviceId) return false;
qDebug() << "handleMotionEvent!" << deviceId; //qDebug() << "handleMotionEvent!" << deviceId;
return true; return true;
} }
...@@ -104,13 +98,13 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe ...@@ -104,13 +98,13 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
QAndroidJniEnvironment env; QAndroidJniEnvironment env;
QAndroidJniObject o = QAndroidJniObject::callStaticObjectMethod<jintArray>("android/view/InputDevice", "getDeviceIds"); QAndroidJniObject o = QAndroidJniObject::callStaticObjectMethod<jintArray>("android/view/InputDevice", "getDeviceIds");
jintArray jarr = o.object<jintArray>(); jintArray jarr = o.object<jintArray>();
size_t sz = env->GetArrayLength(jarr); int sz = env->GetArrayLength(jarr);
jint *buff = env->GetIntArrayElements(jarr, nullptr); jint *buff = env->GetIntArrayElements(jarr, nullptr);
int SOURCE_GAMEPAD = QAndroidJniObject::getStaticField<jint>("android/view/InputDevice", "SOURCE_GAMEPAD"); int SOURCE_GAMEPAD = QAndroidJniObject::getStaticField<jint>("android/view/InputDevice", "SOURCE_GAMEPAD");
int SOURCE_JOYSTICK = QAndroidJniObject::getStaticField<jint>("android/view/InputDevice", "SOURCE_JOYSTICK"); int SOURCE_JOYSTICK = QAndroidJniObject::getStaticField<jint>("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]); QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", buff[i]);
int sources = inputDevice.callMethod<jint>("getSources", "()I"); int sources = inputDevice.callMethod<jint>("getSources", "()I");
if (((sources & SOURCE_GAMEPAD) != SOURCE_GAMEPAD) //check if the input device is interesting to us if (((sources & SOURCE_GAMEPAD) != SOURCE_GAMEPAD) //check if the input device is interesting to us
...@@ -121,13 +115,29 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe ...@@ -121,13 +115,29 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
QString name = inputDevice.callObjectMethod("getName", "()Ljava/lang/String;").toString(); QString name = inputDevice.callObjectMethod("getName", "()Ljava/lang/String;").toString();
if (joystickFound) { //skipping { if (joystickFound) { //skipping {
qWarning() << "Skipping joystick:" << name; qWarning() << "Skipping joystick:" << name;
continue; continue;
} }
//get number of axis
QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;");
int axisCount = rangeListNative.callMethod<jint>("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<jbooleanArray>();
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(inputDevice, name, axisCount, buttonCount, buff[i], _multiVehicleManager);
ret[name] = new JoystickAndroid(name, buff[i], _multiVehicleManager);
joystickFound = true; joystickFound = true;
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
class JoystickAndroid : public Joystick, public QtAndroidPrivate::GenericMotionEventListener, public QtAndroidPrivate::KeyEventListener class JoystickAndroid : public Joystick, public QtAndroidPrivate::GenericMotionEventListener, public QtAndroidPrivate::KeyEventListener
{ {
public: 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(); ~JoystickAndroid();
static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager); static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager);
......
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