JoystickManager.cc 6.25 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * 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 33
    , _activeJoystick(nullptr)
    , _multiVehicleManager(nullptr)
34
{
35 36 37 38 39
}

JoystickManager::~JoystickManager() {
    QMap<QString, Joystick*>::iterator i;
    for (i = _name2JoystickMap.begin(); i != _name2JoystickMap.end(); ++i) {
Don Gagne's avatar
Don Gagne committed
40
        qCDebug(JoystickManagerLog) << "Releasing joystick:" << i.key();
41 42 43
        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__
57 58
    if (!JoystickSDL::init()) {
        return;
Jacob Walser's avatar
Jacob Walser committed
59
    }
60
    _setActiveJoystickFromSettings();
Jacob Walser's avatar
Jacob Walser committed
61
#elif defined(__android__)
62
    if (!JoystickAndroid::init(this)) {
63
        return;
64
    }
65
    connect(this, &JoystickManager::updateAvailableJoysticksSignal, this, &JoystickManager::restartJoystickCheckTimer);
Jacob Walser's avatar
Jacob Walser committed
66
#endif
67
    connect(&_joystickCheckTimer, &QTimer::timeout, this, &JoystickManager::_updateAvailableJoysticks);
68
    _joystickCheckTimerCounter = 5;
69
    _joystickCheckTimer.start(1000);
Jacob Walser's avatar
Jacob Walser committed
70 71 72 73 74
}

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

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

83 84
    if (_activeJoystick && !newMap.contains(_activeJoystick->name())) {
        qCDebug(JoystickManagerLog) << "Active joystick removed";
85
        setActiveJoystick(nullptr);
86 87
    }

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

Jacob Walser's avatar
Jacob Walser committed
100 101
    _name2JoystickMap = newMap;
    emit availableJoysticksChanged();
102 103

    if (!_name2JoystickMap.count()) {
104
        setActiveJoystick(nullptr);
105 106
        return;
    }
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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
129

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

    if (_activeJoystick == joystick) {
        return;
    }

139 140 141 142 143 144
    if (_activeJoystick) {
        _activeJoystick->stopPolling();
    }
    
    _activeJoystick = joystick;
    
145
    if (_activeJoystick != nullptr) {
Jacob Walser's avatar
Jacob Walser committed
146 147 148 149 150
        qCDebug(JoystickManagerLog) << "Set active:" << _activeJoystick->name();

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

152
    emit activeJoystickChanged(_activeJoystick);
153
    emit activeJoystickNameChanged(_activeJoystick?_activeJoystick->name():"");
154 155 156 157 158 159
}

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

Gregory Dymarek's avatar
Gregory Dymarek committed
187 188 189
/*
 * TODO: move this to the right place: JoystickSDL.cc and JoystickAndroid.cc respectively and call through Joystick.cc
 */
190
void JoystickManager::_updateAvailableJoysticks()
Jacob Walser's avatar
Jacob Walser committed
191
{
Jacob Walser's avatar
Jacob Walser committed
192
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
193 194
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
Jacob Walser's avatar
Jacob Walser committed
195
        switch(event.type) {
Jacob Walser's avatar
Jacob Walser committed
196 197 198 199 200 201 202 203 204 205 206
        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
207
        default:
Jacob Walser's avatar
Jacob Walser committed
208 209 210
            break;
        }
    }
Jacob Walser's avatar
Jacob Walser committed
211
#elif defined(__android__)
212
    _joystickCheckTimerCounter--;
213
    _setActiveJoystickFromSettings();
214 215 216
    if (_joystickCheckTimerCounter <= 0) {
        _joystickCheckTimer.stop();
    }
Jacob Walser's avatar
Jacob Walser committed
217
#endif
Jacob Walser's avatar
Jacob Walser committed
218
}
219 220 221 222 223 224

void JoystickManager::restartJoystickCheckTimer()
{
    _joystickCheckTimerCounter = 5;
    _joystickCheckTimer.start(1000);
}