JoystickManager.cc 5.95 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
    #include "JoystickSDL.h"
18
    #define __sdljoystick__
19 20 21 22
#endif

#ifdef __android__
    #include "JoystickAndroid.h"
23 24 25 26 27 28 29
#endif

QGC_LOGGING_CATEGORY(JoystickManagerLog, "JoystickManagerLog")

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

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

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";
44 45 46 47
}

void JoystickManager::setToolbox(QGCToolbox *toolbox)
{
Jacob Walser's avatar
Jacob Walser committed
48
    QGCTool::setToolbox(toolbox);
49 50 51 52

    _multiVehicleManager = _toolbox->multiVehicleManager();

    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
53
}
54

Gregory Dymarek's avatar
Gregory Dymarek committed
55
void JoystickManager::init() {
Jacob Walser's avatar
Jacob Walser committed
56
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
57 58 59 60 61
    if (JoystickSDL::init()) {
        _setActiveJoystickFromSettings();
        connect(&_joystickCheckTimer, &QTimer::timeout, this, &JoystickManager::_updateAvailableJoysticks);
        _joystickCheckTimer.start(250);
    }
Jacob Walser's avatar
Jacob Walser committed
62
#elif defined(__android__)
63 64
    _setActiveJoystickFromSettings();
    //TODO: Investigate Android events for Joystick hot plugging & run _joystickCheckTimer if possible
Jacob Walser's avatar
Jacob Walser committed
65
#endif
Jacob Walser's avatar
Jacob Walser committed
66 67 68 69 70
}

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

72
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
73 74
    // Get the latest joystick mapping
    newMap = JoystickSDL::discover(_multiVehicleManager);
75
#elif defined(__android__)
76
    newMap = JoystickAndroid::discover(_multiVehicleManager);
77
#endif
78

79 80 81 82 83
    if (_activeJoystick && !newMap.contains(_activeJoystick->name())) {
        qCDebug(JoystickManagerLog) << "Active joystick removed";
        setActiveJoystick(NULL);
    }

Jacob Walser's avatar
Jacob Walser committed
84 85 86 87 88 89
    // 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();
90
            i.value()->stopPolling();
91 92
            i.value()->wait(1000);
            i.value()->deleteLater();
Jacob Walser's avatar
Jacob Walser committed
93
        }
94
    }
95

Jacob Walser's avatar
Jacob Walser committed
96 97
    _name2JoystickMap = newMap;
    emit availableJoysticksChanged();
98 99

    if (!_name2JoystickMap.count()) {
Jacob Walser's avatar
Jacob Walser committed
100
        setActiveJoystick(NULL);
101 102
        return;
    }
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    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
125 126

    if (joystick != NULL && !_name2JoystickMap.contains(joystick->name())) {
127 128 129
        qCWarning(JoystickManagerLog) << "Set active not in map" << joystick->name();
        return;
    }
Jacob Walser's avatar
Jacob Walser committed
130 131 132 133 134

    if (_activeJoystick == joystick) {
        return;
    }

135 136 137 138 139 140
    if (_activeJoystick) {
        _activeJoystick->stopPolling();
    }
    
    _activeJoystick = joystick;
    
Jacob Walser's avatar
Jacob Walser committed
141 142 143 144 145 146
    if (_activeJoystick != NULL) {
        qCDebug(JoystickManagerLog) << "Set active:" << _activeJoystick->name();

        settings.beginGroup(_settingsGroup);
        settings.setValue(_settingsKeyActiveJoystick, _activeJoystick->name());
    }
147

148
    emit activeJoystickChanged(_activeJoystick);
149
    emit activeJoystickNameChanged(_activeJoystick?_activeJoystick->name():"");
150 151 152 153 154 155
}

QVariantList JoystickManager::joysticks(void)
{
    QVariantList list;
    
156
    foreach (const QString &name, _name2JoystickMap.keys()) {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        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
182

Gregory Dymarek's avatar
Gregory Dymarek committed
183 184 185
/*
 * TODO: move this to the right place: JoystickSDL.cc and JoystickAndroid.cc respectively and call through Joystick.cc
 */
Jacob Walser's avatar
Jacob Walser committed
186 187
void JoystickManager::_updateAvailableJoysticks(void)
{
Jacob Walser's avatar
Jacob Walser committed
188
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
189 190
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
Jacob Walser's avatar
Jacob Walser committed
191
        switch(event.type) {
Jacob Walser's avatar
Jacob Walser committed
192 193 194 195 196 197 198 199 200 201 202
        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
203
        default:
Jacob Walser's avatar
Jacob Walser committed
204 205 206
            break;
        }
    }
Jacob Walser's avatar
Jacob Walser committed
207 208
#elif defined(__android__)
    /*
209
     * TODO: Investigate Android events for Joystick hot plugging
Jacob Walser's avatar
Jacob Walser committed
210 211
     */
#endif
Jacob Walser's avatar
Jacob Walser committed
212
}