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

#include "QGCApplication.h"

#include <QQmlEngine>
6
#include <QTextStream>
7

8
JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, bool isGameController, MultiVehicleManager* multiVehicleManager)
9
    : Joystick(name,axisCount,buttonCount,hatCount,multiVehicleManager)
10
    , _isGameController(isGameController)
11 12 13 14 15 16 17
    , _index(index)
{
}

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

18
    if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_NOPARACHUTE) < 0) {
19 20 21 22
        qWarning() << "Couldn't initialize SimpleDirectMediaLayer:" << SDL_GetError();
        return ret;
    }

23 24
    _loadGameControllerMappings();

25 26 27 28 29
    // Load available joysticks

    qCDebug(JoystickLog) << "Available joysticks";

    for (int i=0; i<SDL_NumJoysticks(); i++) {
30 31
        QString name = SDL_JoystickNameForIndex(i);

32
        if (!ret.contains(name)) {
33
            int axisCount, buttonCount, hatCount;
34
            bool isGameController;
35 36

            SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
37 38 39 40 41 42 43 44 45 46 47 48
            if (SDL_IsGameController(i)) {
                isGameController = true;
                axisCount = SDL_CONTROLLER_AXIS_MAX;
                buttonCount = SDL_CONTROLLER_BUTTON_MAX;
                hatCount = 0;
            } else {
                isGameController = false;
                axisCount = SDL_JoystickNumAxes(sdlJoystick);
                buttonCount = SDL_JoystickNumButtons(sdlJoystick);
                hatCount = SDL_JoystickNumHats(sdlJoystick);
            }

49 50
            SDL_JoystickClose(sdlJoystick);

51
            qCDebug(JoystickLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount << "hats:" << hatCount << "isGC:" << isGameController;
52
            ret[name] = new JoystickSDL(name, axisCount, buttonCount, hatCount, i, isGameController, _multiVehicleManager);
53 54 55 56 57 58 59
        } else {
            qCDebug(JoystickLog) << "\tSkipping duplicate" << name;
        }
    }
    return ret;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
void JoystickSDL::_loadGameControllerMappings(void) {
    QFile file(":/db/mapping/joystick/gamecontrollerdb.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qWarning() << "Couldn't load GameController mapping database.";
        return;
    }

    QTextStream s(&file);

    while (!s.atEnd()) {
        SDL_GameControllerAddMapping(s.readLine().toStdString().c_str());
    }
}

Gregory Dymarek's avatar
Gregory Dymarek committed
75
bool JoystickSDL::_open(void) {
76 77 78 79 80 81
    if ( _isGameController ) {
        sdlController = SDL_GameControllerOpen(_index);
        sdlJoystick = SDL_GameControllerGetJoystick(sdlController);
    } else {
        sdlJoystick = SDL_JoystickOpen(_index);
    }
82 83

    if (!sdlJoystick) {
Gregory Dymarek's avatar
Gregory Dymarek committed
84 85
        qCWarning(JoystickLog) << "SDL_JoystickOpen failed:" << SDL_GetError();
        return false;
86 87 88 89 90
    }

    return true;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
91
void JoystickSDL::_close(void) {
92 93 94
    SDL_JoystickClose(sdlJoystick);
}

Gregory Dymarek's avatar
Gregory Dymarek committed
95
bool JoystickSDL::_update(void)
96 97
{
    SDL_JoystickUpdate();
98
    SDL_GameControllerUpdate();
99 100 101
    return true;
}

Gregory Dymarek's avatar
Gregory Dymarek committed
102
bool JoystickSDL::_getButton(int i) {
103 104 105 106 107
    if ( _isGameController ) {
        return !!SDL_GameControllerGetButton(sdlController, SDL_GameControllerButton(i));
    } else {
        return !!SDL_JoystickGetButton(sdlJoystick, i);
    }
108 109
}

Gregory Dymarek's avatar
Gregory Dymarek committed
110
int JoystickSDL::_getAxis(int i) {
111 112 113 114 115
    if ( _isGameController ) {
        return SDL_GameControllerGetAxis(sdlController, SDL_GameControllerAxis(i));
    } else {
        return SDL_JoystickGetAxis(sdlJoystick, i);
    }
116 117
}

118 119 120 121 122 123 124 125 126
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;
}