JoystickSDL.cc 2.62 KB
Newer Older
1 2 3 4 5 6
#include "JoystickSDL.h"

#include "QGCApplication.h"

#include <QQmlEngine>

7 8
JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, MultiVehicleManager* multiVehicleManager)
    : Joystick(name,axisCount,buttonCount,hatCount,multiVehicleManager)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    , _index(index)
{
}

QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicleManager) {
    static QMap<QString, Joystick*> ret;

    if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE) < 0) {
        qWarning() << "Couldn't initialize SimpleDirectMediaLayer:" << SDL_GetError();
        return ret;
    }

    // Load available joysticks

    qCDebug(JoystickLog) << "Available joysticks";

    for (int i=0; i<SDL_NumJoysticks(); i++) {
26 27 28 29 30 31 32 33 34 35
        QString name = SDL_JoystickNameForIndex(i);

        if (SDL_IsGameController(i)) {
            qDebug() << name << "supports SDL GameController!";
        } else {
            qDebug() << name << "DOES NOT support SDL GameController!";
        }

        SDL_GameController* sdlController = SDL_GameControllerOpen(i);
        qDebug() << SDL_GetError();
36 37

        if (!ret.contains(name)) {
38
            int axisCount, buttonCount, hatCount;
39 40 41 42

            SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
            axisCount = SDL_JoystickNumAxes(sdlJoystick);
            buttonCount = SDL_JoystickNumButtons(sdlJoystick);
43
            hatCount = SDL_JoystickNumHats(sdlJoystick);
44 45
            SDL_JoystickClose(sdlJoystick);

46 47
            qCDebug(JoystickLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount << "hats:" << hatCount;
            ret[name] = new JoystickSDL(name, axisCount, buttonCount, hatCount, i, _multiVehicleManager);
48 49 50 51 52 53 54
        } else {
            qCDebug(JoystickLog) << "\tSkipping duplicate" << name;
        }
    }
    return ret;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
55
bool JoystickSDL::_open(void) {
56 57 58
    sdlJoystick = SDL_JoystickOpen(_index);

    if (!sdlJoystick) {
Gregory Dymarek's avatar
Gregory Dymarek committed
59 60
        qCWarning(JoystickLog) << "SDL_JoystickOpen failed:" << SDL_GetError();
        return false;
61 62 63 64 65
    }

    return true;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
66
void JoystickSDL::_close(void) {
67 68 69
    SDL_JoystickClose(sdlJoystick);
}

Gregory Dymarek's avatar
Gregory Dymarek committed
70
bool JoystickSDL::_update(void)
71 72 73 74 75
{
    SDL_JoystickUpdate();
    return true;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
76
bool JoystickSDL::_getButton(int i) {
77 78 79
    return !!SDL_JoystickGetButton(sdlJoystick, i);
}

Gregory Dymarek's avatar
Gregory Dymarek committed
80
int JoystickSDL::_getAxis(int i) {
81 82 83
    return SDL_JoystickGetAxis(sdlJoystick, i);
}

84 85 86 87 88 89 90 91 92
uint8_t JoystickSDL::_getHat(int hat,int i) {
    uint8_t hatButtons[] = {SDL_HAT_UP,SDL_HAT_DOWN,SDL_HAT_LEFT,SDL_HAT_RIGHT};

    if ( i < int(sizeof(hatButtons)) ) {
        return !!(SDL_JoystickGetHat(sdlJoystick, hat) & hatButtons[i]);
    }
    return 0;
}