JoystickAndroid.cc 9.16 KB
Newer Older
1 2 3 4 5 6 7 8
#include "JoystickAndroid.h"

#include "QGCApplication.h"

#include <QQmlEngine>

int JoystickAndroid::_androidBtnListCount;
int *JoystickAndroid::_androidBtnList;
Gregory Dymarek's avatar
Gregory Dymarek committed
9 10
int JoystickAndroid::ACTION_DOWN;
int JoystickAndroid::ACTION_UP;
11 12
QMutex JoystickAndroid::m_mutex;

Gregory Dymarek's avatar
Gregory Dymarek committed
13
JoystickAndroid::JoystickAndroid(const QString& name, int axisCount, int buttonCount, int id, MultiVehicleManager* multiVehicleManager)
Gregory Dymarek's avatar
Gregory Dymarek committed
14
    : Joystick(name,axisCount,buttonCount,0,multiVehicleManager)
15 16 17 18 19
    , deviceId(id)
{
    int i;
    
    QAndroidJniEnvironment env;
Gregory Dymarek's avatar
Gregory Dymarek committed
20
    QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", id);
21

22 23 24 25 26
    //set button mapping (number->code)
    jintArray b = env->NewIntArray(_androidBtnListCount);
    env->SetIntArrayRegion(b,0,_androidBtnListCount,_androidBtnList);

    QAndroidJniObject btns = inputDevice.callObjectMethod("hasKeys", "([I)[Z", b);
27 28 29 30 31 32
    jbooleanArray jSupportedButtons = btns.object<jbooleanArray>();
    jboolean* supportedButtons = env->GetBooleanArrayElements(jSupportedButtons, nullptr);
    //create a mapping table (btnCode) that maps button number with button code
    btnValue = new bool[_buttonCount];
    btnCode = new int[_buttonCount];
    int c = 0;
33
    for (i=0;i<_androidBtnListCount;i++)
34
        if (supportedButtons[i]) {
35 36 37 38
            btnValue[c] = false;
            btnCode[c] = _androidBtnList[i];
            c++;
        }
39 40 41

    env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0);

42
    // set axis mapping (number->code)
43 44
    axisValue = new int[_axisCount];
    axisCode = new int[_axisCount];
45
    QAndroidJniObject rangeListNative = inputDevice.callObjectMethod("getMotionRanges", "()Ljava/util/List;");
46
    for (i = 0; i < _axisCount; i++) {
Gregory Dymarek's avatar
Gregory Dymarek committed
47 48
        QAndroidJniObject range = rangeListNative.callObjectMethod("get", "(I)Ljava/lang/Object;",i);
        axisCode[i] = range.callMethod<jint>("getAxis");
49 50 51 52 53 54 55
        // Don't allow two axis with the same code
        for (int j = 0; j < i; j++) {
            if (axisCode[i] == axisCode[j]) {
                axisCode[i] = -1;
                break;
            }
        }
Gregory Dymarek's avatar
Gregory Dymarek committed
56
        axisValue[i] = 0;
57 58
    }

Gregory Dymarek's avatar
Gregory Dymarek committed
59
    qCDebug(JoystickLog) << "axis:" <<_axisCount << "buttons:" <<_buttonCount;
60 61 62 63 64 65 66 67 68 69 70 71 72 73
    QtAndroidPrivate::registerGenericMotionEventListener(this);
    QtAndroidPrivate::registerKeyEventListener(this);
}

JoystickAndroid::~JoystickAndroid() {
    delete btnCode;
    delete axisCode;
    delete btnValue;
    delete axisValue;

    QtAndroidPrivate::unregisterGenericMotionEventListener(this);
    QtAndroidPrivate::unregisterKeyEventListener(this);
}

74

75 76 77 78
QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVehicleManager) {
    bool joystickFound = false;
    static QMap<QString, Joystick*> ret;

Gregory Dymarek's avatar
Gregory Dymarek committed
79
    _initStatic(); //it's enough to run it once, should be in a static constructor
80 81 82 83 84 85

    QMutexLocker lock(&m_mutex);

    QAndroidJniEnvironment env;
    QAndroidJniObject o = QAndroidJniObject::callStaticObjectMethod<jintArray>("android/view/InputDevice", "getDeviceIds");
    jintArray jarr = o.object<jintArray>();
86
    int sz = env->GetArrayLength(jarr);
87 88 89 90 91
    jint *buff = env->GetIntArrayElements(jarr, nullptr);

    int SOURCE_GAMEPAD = QAndroidJniObject::getStaticField<jint>("android/view/InputDevice", "SOURCE_GAMEPAD");
    int SOURCE_JOYSTICK = QAndroidJniObject::getStaticField<jint>("android/view/InputDevice", "SOURCE_JOYSTICK");

92
    for (int i = 0; i < sz; ++i) {
93 94 95 96 97 98 99 100 101 102
        QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", buff[i]);
        int sources = inputDevice.callMethod<jint>("getSources", "()I");
        if (((sources & SOURCE_GAMEPAD) != SOURCE_GAMEPAD) //check if the input device is interesting to us
                && ((sources & SOURCE_JOYSTICK) != SOURCE_JOYSTICK)) continue;

        //get id and name
        QString id = inputDevice.callObjectMethod("getDescriptor", "()Ljava/lang/String;").toString();
        QString name = inputDevice.callObjectMethod("getName", "()Ljava/lang/String;").toString();


103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        if (joystickFound) { //skipping {
            qWarning() << "Skipping joystick:" << name;
            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;
Gregory Dymarek's avatar
Gregory Dymarek committed
119 120
        for (int j=0;j<_androidBtnListCount;j++)
            if (supportedButtons[j]) buttonCount++;
121 122
        env->ReleaseBooleanArrayElements(jSupportedButtons, supportedButtons, 0);

Gregory Dymarek's avatar
Gregory Dymarek committed
123
        qCDebug(JoystickLog) << "\t" << name << "id:" << buff[i] << "axes:" << axisCount << "buttons:" << buttonCount;
124

Gregory Dymarek's avatar
Gregory Dymarek committed
125
        ret[name] = new JoystickAndroid(name, axisCount, buttonCount, buff[i], _multiVehicleManager);
126 127 128 129 130 131 132 133 134
        joystickFound = true;
    }

    env->ReleaseIntArrayElements(jarr, buff, 0);


    return ret;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

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;
}


Gregory Dymarek's avatar
Gregory Dymarek committed
169
bool JoystickAndroid::_open(void) {
170 171 172
    return true;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
173
void JoystickAndroid::_close(void) {
174 175
}

Gregory Dymarek's avatar
Gregory Dymarek committed
176
bool JoystickAndroid::_update(void)
177 178 179 180
{
    return true;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
181
bool JoystickAndroid::_getButton(int i) {
Gregory Dymarek's avatar
Gregory Dymarek committed
182
    return btnValue[ i ];
183 184
}

Gregory Dymarek's avatar
Gregory Dymarek committed
185
int JoystickAndroid::_getAxis(int i) {
Gregory Dymarek's avatar
Gregory Dymarek committed
186
    return axisValue[ i ];
187 188
}

Gregory Dymarek's avatar
Gregory Dymarek committed
189 190 191 192 193 194
uint8_t JoystickAndroid::_getHat(int hat,int i) {
    Q_UNUSED(hat);
    Q_UNUSED(i);

    return 0;
}
195 196

//helper method
Gregory Dymarek's avatar
Gregory Dymarek committed
197
void JoystickAndroid::_initStatic() {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    //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

    //int JoystickAndroid::_androidBtnListCount;
    _androidBtnListCount = 31;
    static int ret[31]; //there are 31 buttons in total accordingy to the API
    int i;
    //int *JoystickAndroid::
    _androidBtnList = ret;

    for (i=1;i<=16;i++) {
        QString name = "KEYCODE_BUTTON_"+QString::number(i);
        ret[i-1] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", name.toStdString().c_str());
    }
    i--;

    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_A");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_B");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_C");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_L1");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_L2");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_R1");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_R2");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_MODE");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_SELECT");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_START");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_THUMBL");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_THUMBR");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_X");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_Y");
    ret[i++] = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "KEYCODE_BUTTON_Z");

Gregory Dymarek's avatar
Gregory Dymarek committed
230 231
    ACTION_DOWN = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "ACTION_DOWN");
    ACTION_UP = QAndroidJniObject::getStaticField<jint>("android/view/KeyEvent", "ACTION_UP");
232 233
}