JoystickManager.cc 4.23 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11

#include "JoystickManager.h"
12
#include "QGCApplication.h"
13 14 15

#include <QQmlEngine>

16 17 18 19 20 21
#ifndef __mobile__
    #ifdef Q_OS_MAC
        #include <SDL.h>
    #else
        #include <SDL/SDL.h>
    #endif
22 23 24 25 26 27 28
#endif

QGC_LOGGING_CATEGORY(JoystickManagerLog, "JoystickManagerLog")

const char * JoystickManager::_settingsGroup =              "JoystickManager";
const char * JoystickManager::_settingsKeyActiveJoystick =  "ActiveJoystick";

29 30
JoystickManager::JoystickManager(QGCApplication* app)
    : QGCTool(app)
31
    , _activeJoystick(NULL)
32
    , _multiVehicleManager(NULL)
33 34
{
    
35 36 37 38 39 40 41 42 43 44
}

void JoystickManager::setToolbox(QGCToolbox *toolbox)
{
   QGCTool::setToolbox(toolbox);

    _multiVehicleManager = _toolbox->multiVehicleManager();

    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);

45
#ifndef __mobile__
46 47 48 49
    if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE) < 0) {
        qWarning() << "Couldn't initialize SimpleDirectMediaLayer:" << SDL_GetError();
        return;
    }
50

51
    // Load available joysticks
52

53
    qCDebug(JoystickManagerLog) << "Available joysticks";
54

55 56
    for (int i=0; i<SDL_NumJoysticks(); i++) {
        QString name = SDL_JoystickName(i);
57

58 59
        if (!_name2JoystickMap.contains(name)) {
            int axisCount, buttonCount;
60

61 62 63 64 65 66
            SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
            axisCount = SDL_JoystickNumAxes(sdlJoystick);
            buttonCount = SDL_JoystickNumButtons(sdlJoystick);
            SDL_JoystickClose(sdlJoystick);

            qCDebug(JoystickManagerLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount;
67
            _name2JoystickMap[name] = new Joystick(name, axisCount, buttonCount, i, _multiVehicleManager);
68 69 70 71
        } else {
            qCDebug(JoystickManagerLog) << "\tSkipping duplicate" << name;
        }
    }
72
#endif
73

74 75 76 77
    if (!_name2JoystickMap.count()) {
        qCDebug(JoystickManagerLog) << "\tnone found";
        return;
    }
78

79 80 81 82 83 84
    _setActiveJoystickFromSettings();
}


void JoystickManager::_setActiveJoystickFromSettings(void)
{
85
#ifndef __mobile__
86 87 88 89 90 91 92 93 94 95 96
    QSettings settings;
    
    settings.beginGroup(_settingsGroup);
    QString name = settings.value(_settingsKeyActiveJoystick).toString();
    
    if (name.isEmpty()) {
        name = _name2JoystickMap.first()->name();
    }
    
    setActiveJoystick(_name2JoystickMap.value(name, _name2JoystickMap.first()));
    settings.setValue(_settingsKeyActiveJoystick, _activeJoystick->name());
97
#endif
98 99 100 101 102 103 104 105 106
}

Joystick* JoystickManager::activeJoystick(void)
{
    return _activeJoystick;
}

void JoystickManager::setActiveJoystick(Joystick* joystick)
{
107 108 109
#ifdef __mobile__
    Q_UNUSED(joystick)
#else
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    QSettings settings;
    
    if (!_name2JoystickMap.contains(joystick->name())) {
        qCWarning(JoystickManagerLog) << "Set active not in map" << joystick->name();
        return;
    }
    
    if (_activeJoystick) {
        _activeJoystick->stopPolling();
    }
    
    _activeJoystick = joystick;
    
    settings.beginGroup(_settingsGroup);
    settings.setValue(_settingsKeyActiveJoystick, _activeJoystick->name());
    
    emit activeJoystickChanged(_activeJoystick);
    emit activeJoystickNameChanged(_activeJoystick->name());
128
#endif
129 130 131 132 133 134
}

QVariantList JoystickManager::joysticks(void)
{
    QVariantList list;
    
135
    foreach (const QString &name, _name2JoystickMap.keys()) {
136 137 138 139 140 141 142 143 144 145 146 147 148
        list += QVariant::fromValue(_name2JoystickMap[name]);
    }
    
    return list;
}

QStringList JoystickManager::joystickNames(void)
{
    return _name2JoystickMap.keys();
}

QString JoystickManager::activeJoystickName(void)
{
149 150 151
#ifdef __mobile__
    return QString();
#else
152
    return _activeJoystick ? _activeJoystick->name() : QString();
153
#endif
154 155 156 157 158 159 160 161 162 163 164
}

void JoystickManager::setActiveJoystickName(const QString& name)
{
    if (!_name2JoystickMap.contains(name)) {
        qCWarning(JoystickManagerLog) << "Set active not in map" << name;
        return;
    }
    
    setActiveJoystick(_name2JoystickMap[name]);
}