JoystickManager.cc 5.82 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
#ifndef __mobile__
17 18 19 20 21
    #include "JoystickSDL.h"
    #define __sdljoystick__
#endif

#ifdef __android__
22 23 24 25
    /*
     * Android Joystick not yet supported
     * #include "JoystickAndroid.h"
     */
26 27 28 29 30 31 32
#endif

QGC_LOGGING_CATEGORY(JoystickManagerLog, "JoystickManagerLog")

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

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

JoystickManager::~JoystickManager() {
    QMap<QString, Joystick*>::iterator i;
    for (i = _name2JoystickMap.begin(); i != _name2JoystickMap.end(); ++i) {
        qDebug() << "Releasing joystick:" << i.key();
        delete i.value();
    }
    qDebug() << "Done";
47 48 49 50
}

void JoystickManager::setToolbox(QGCToolbox *toolbox)
{
Jacob Walser's avatar
Jacob Walser committed
51
    QGCTool::setToolbox(toolbox);
52 53 54 55

    _multiVehicleManager = _toolbox->multiVehicleManager();

    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
56
}
57

Jacob Walser's avatar
Jacob Walser committed
58
void JoystickManager::init()
59
{
Jacob Walser's avatar
Jacob Walser committed
60
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
61 62 63 64 65
    if (JoystickSDL::init()) {
        _setActiveJoystickFromSettings();
        connect(&_joystickCheckTimer, &QTimer::timeout, this, &JoystickManager::_updateAvailableJoysticks);
        _joystickCheckTimer.start(250);
    }
Jacob Walser's avatar
Jacob Walser committed
66 67 68 69 70
#elif defined(__android__)
    /*
     * Android Joystick not yet supported
     */
#endif
Jacob Walser's avatar
Jacob Walser committed
71 72 73 74 75 76
}

void JoystickManager::_setActiveJoystickFromSettings(void)
{
    QMap<QString,Joystick*> newMap;

77
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
78 79
    // Get the latest joystick mapping
    newMap = JoystickSDL::discover(_multiVehicleManager);
80
#elif defined(__android__)
81 82 83 84
    /*
     * Android Joystick not yet supported
     * _name2JoystickMap = JoystickAndroid::discover(_multiVehicleManager);
     */
85
#endif
86

87 88 89 90 91
    if (_activeJoystick && !newMap.contains(_activeJoystick->name())) {
        qCDebug(JoystickManagerLog) << "Active joystick removed";
        setActiveJoystick(NULL);
    }

Jacob Walser's avatar
Jacob Walser committed
92 93 94 95 96 97
    // Check to see if our current mapping contains any joysticks that are not in the new mapping
    // If so, those joysticks have been unplugged, and need to be cleaned up
    QMap<QString, Joystick*>::iterator i;
    for (i = _name2JoystickMap.begin(); i != _name2JoystickMap.end(); ++i) {
        if (!newMap.contains(i.key())) {
            qCDebug(JoystickManagerLog) << "Releasing joystick:" << i.key();
98
            i.value()->stopPolling();
99 100
            i.value()->wait(1000);
            i.value()->deleteLater();
Jacob Walser's avatar
Jacob Walser committed
101
        }
102
    }
103

Jacob Walser's avatar
Jacob Walser committed
104 105
    _name2JoystickMap = newMap;
    emit availableJoysticksChanged();
106

Jacob Walser's avatar
Jacob Walser committed
107 108 109 110
    if (!_name2JoystickMap.count()) {
        setActiveJoystick(NULL);
        return;
    }
Jacob Walser's avatar
Jacob Walser committed
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    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());
}

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

void JoystickManager::setActiveJoystick(Joystick* joystick)
{
    QSettings settings;
Jacob Walser's avatar
Jacob Walser committed
133 134

    if (joystick != NULL && !_name2JoystickMap.contains(joystick->name())) {
135 136 137
        qCWarning(JoystickManagerLog) << "Set active not in map" << joystick->name();
        return;
    }
Jacob Walser's avatar
Jacob Walser committed
138 139 140 141 142

    if (_activeJoystick == joystick) {
        return;
    }

143 144 145 146 147 148
    if (_activeJoystick) {
        _activeJoystick->stopPolling();
    }
    
    _activeJoystick = joystick;
    
Jacob Walser's avatar
Jacob Walser committed
149 150 151 152 153 154
    if (_activeJoystick != NULL) {
        qCDebug(JoystickManagerLog) << "Set active:" << _activeJoystick->name();

        settings.beginGroup(_settingsGroup);
        settings.setValue(_settingsKeyActiveJoystick, _activeJoystick->name());
    }
155 156 157

    emit activeJoystickChanged(_activeJoystick);
    emit activeJoystickNameChanged(_activeJoystick?_activeJoystick->name():"");
158 159 160 161 162 163
}

QVariantList JoystickManager::joysticks(void)
{
    QVariantList list;
    
164
    foreach (const QString &name, _name2JoystickMap.keys()) {
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        list += QVariant::fromValue(_name2JoystickMap[name]);
    }
    
    return list;
}

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

QString JoystickManager::activeJoystickName(void)
{
    return _activeJoystick ? _activeJoystick->name() : QString();
}

void JoystickManager::setActiveJoystickName(const QString& name)
{
    if (!_name2JoystickMap.contains(name)) {
        qCWarning(JoystickManagerLog) << "Set active not in map" << name;
        return;
    }
    
    setActiveJoystick(_name2JoystickMap[name]);
}
Jacob Walser's avatar
Jacob Walser committed
190 191 192

void JoystickManager::_updateAvailableJoysticks(void)
{
Jacob Walser's avatar
Jacob Walser committed
193
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
194 195
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
Jacob Walser's avatar
Jacob Walser committed
196
        switch(event.type) {
Jacob Walser's avatar
Jacob Walser committed
197 198 199 200 201 202 203 204 205 206 207
        case SDL_QUIT:
            qCDebug(JoystickManagerLog) << "SDL ERROR:" << SDL_GetError();
            break;
        case SDL_JOYDEVICEADDED:
            qCDebug(JoystickManagerLog) << "Joystick added:" << event.jdevice.which;
            _setActiveJoystickFromSettings();
            break;
        case SDL_JOYDEVICEREMOVED:
            qCDebug(JoystickManagerLog) << "Joystick removed:" << event.jdevice.which;
            _setActiveJoystickFromSettings();
            break;
Jacob Walser's avatar
Jacob Walser committed
208
        default:
Jacob Walser's avatar
Jacob Walser committed
209 210 211
            break;
        }
    }
Jacob Walser's avatar
Jacob Walser committed
212 213 214 215 216
#elif defined(__android__)
    /*
     * Android Joystick not yet supported
     */
#endif
Jacob Walser's avatar
Jacob Walser committed
217
}