Skip to content
JoystickSDL.cc 1.93 KiB
Newer Older
Gregory Dymarek's avatar
Gregory Dymarek committed
#include "JoystickSDL.h"

#include "QGCApplication.h"

#include <QQmlEngine>

JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int index, MultiVehicleManager* multiVehicleManager)
    : Joystick(name,axisCount,buttonCount,multiVehicleManager)
    , _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++) {
        QString name = SDL_JoystickName(i);

        if (!ret.contains(name)) {
            int axisCount, buttonCount;

            SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
            axisCount = SDL_JoystickNumAxes(sdlJoystick);
            buttonCount = SDL_JoystickNumButtons(sdlJoystick);
            SDL_JoystickClose(sdlJoystick);

            qCDebug(JoystickLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount;
            ret[name] = new JoystickSDL(name, axisCount, buttonCount, i, _multiVehicleManager);
        } else {
            qCDebug(JoystickLog) << "\tSkipping duplicate" << name;
        }
    }
    return ret;
}

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

    if (!sdlJoystick) {
Gregory Dymarek's avatar
Gregory Dymarek committed
        qCWarning(JoystickLog) << "SDL_JoystickOpen failed:" << SDL_GetError();
        return false;
Gregory Dymarek's avatar
Gregory Dymarek committed
void JoystickSDL::_close(void) {
Gregory Dymarek's avatar
Gregory Dymarek committed
    SDL_JoystickClose(sdlJoystick);
}

Gregory Dymarek's avatar
Gregory Dymarek committed
bool JoystickSDL::_update(void)
Gregory Dymarek's avatar
Gregory Dymarek committed
{
    SDL_JoystickUpdate();
    return true;
}

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

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