Commit 1c19920f authored by Gregory Dymarek's avatar Gregory Dymarek

Implementing handlers

parent 3c6a4bc1
...@@ -6,19 +6,18 @@ ...@@ -6,19 +6,18 @@
int JoystickAndroid::_androidBtnListCount; int JoystickAndroid::_androidBtnListCount;
int *JoystickAndroid::_androidBtnList; int *JoystickAndroid::_androidBtnList;
int JoystickAndroid::ACTION_DOWN;
int JoystickAndroid::ACTION_UP;
QMutex JoystickAndroid::m_mutex; 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) : 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);
//set button mapping (number->code) //set button mapping (number->code)
jintArray b = env->NewIntArray(_androidBtnListCount); jintArray b = env->NewIntArray(_androidBtnListCount);
...@@ -44,15 +43,14 @@ JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QSt ...@@ -44,15 +43,14 @@ JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QSt
axisValue = new int[_axisCount]; axisValue = new int[_axisCount];
axisCode = new int[_axisCount]; axisCode = new int[_axisCount];
QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;"); 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", "(I)Ljava/lang/Object;",i);
if (range.isValid()) axisCode[i] = range.callMethod<jint>("getAxis");
axisCode[i] = range.callMethod<jint>("getAxis");
axisValue[i] = 0; axisValue[i] = 0;
} }
qDebug() << "axis:" <<_axisCount << "buttons:" <<_buttonCount; qCDebug(JoystickLog) << "axis:" <<_axisCount << "buttons:" <<_buttonCount;
QtAndroidPrivate::registerGenericMotionEventListener(this); QtAndroidPrivate::registerGenericMotionEventListener(this);
QtAndroidPrivate::registerKeyEventListener(this); QtAndroidPrivate::registerKeyEventListener(this);
} }
...@@ -67,31 +65,11 @@ JoystickAndroid::~JoystickAndroid() { ...@@ -67,31 +65,11 @@ JoystickAndroid::~JoystickAndroid() {
QtAndroidPrivate::unregisterKeyEventListener(this); QtAndroidPrivate::unregisterKeyEventListener(this);
} }
bool JoystickAndroid::handleKeyEvent(jobject event) {
QJNIObjectPrivate ev(event);
QMutexLocker lock(&m_mutex);
const int _deviceId = ev.callMethod<jint>("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<jint>("getDeviceId", "()I");
if (_deviceId!=deviceId) return false;
//qDebug() << "handleMotionEvent!" << deviceId;
return true;
}
QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVehicleManager) { QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVehicleManager) {
bool joystickFound = false; bool joystickFound = false;
static QMap<QString, Joystick*> ret; static QMap<QString, Joystick*> 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); QMutexLocker lock(&m_mutex);
...@@ -131,13 +109,13 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe ...@@ -131,13 +109,13 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
jbooleanArray jSupportedButtons = btns.object<jbooleanArray>(); jbooleanArray jSupportedButtons = btns.object<jbooleanArray>();
jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr); jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr);
int buttonCount = 0; int buttonCount = 0;
for (i=0;i<_androidBtnListCount;i++) for (int j=0;j<_androidBtnListCount;j++)
if (supportedButtons[i]) buttonCount++; if (supportedButtons[j]) buttonCount++;
env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0); 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; joystickFound = true;
} }
...@@ -147,6 +125,40 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe ...@@ -147,6 +125,40 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
return ret; return ret;
} }
bool JoystickAndroid::handleKeyEvent(jobject event) {
QJNIObjectPrivate ev(event);
QMutexLocker lock(&m_mutex);
const int _deviceId = ev.callMethod<jint>("getDeviceId", "()I");
if (_deviceId!=deviceId) return false;
const int action = ev.callMethod<jint>("getAction", "()I");
const int keyCode = ev.callMethod<jint>("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<jint>("getDeviceId", "()I");
if (_deviceId!=deviceId) return false;
for (int i=0;i<_axisCount;i++) {
const float v = ev.callMethod<jfloat>("getAxisValue", "(I)F",axisCode[i]);
axisValue[i] = (int)(v*32767.f);
}
return true;
}
bool JoystickAndroid::open(void) { bool JoystickAndroid::open(void) {
return true; return true;
} }
...@@ -169,7 +181,7 @@ int JoystickAndroid::getAxis(int i) { ...@@ -169,7 +181,7 @@ int JoystickAndroid::getAxis(int i) {
//helper method //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 //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 //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() { ...@@ -202,8 +214,7 @@ void JoystickAndroid::_buttonList() {
ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_Y"); ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_Y");
ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_Z"); ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_Z");
for (int j=0;j<_androidBtnListCount;j++) ACTION_DOWN = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "ACTION_DOWN");
qDebug() << "\tpossible button: "+QString::number(_androidBtnList[j]); ACTION_UP = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "ACTION_UP");
} }
...@@ -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 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(); ~JoystickAndroid();
static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager); static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager);
...@@ -36,10 +36,11 @@ private: ...@@ -36,10 +36,11 @@ private:
bool *btnValue; bool *btnValue;
int *axisValue; int *axisValue;
static void _buttonList(); static void _initStatic();
static int * _androidBtnList; //list of all possible android buttons static int * _androidBtnList; //list of all possible android buttons
static int _androidBtnListCount; static int _androidBtnListCount;
static int ACTION_DOWN, ACTION_UP;
static QMutex m_mutex; static QMutex m_mutex;
int deviceId; int deviceId;
......
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