JoystickManager.cc 5.65 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 51 52 53 54 55
}

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

    _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 61 62 63 64 65 66 67 68 69 70
    if (JoystickSDL::init()) {
        _setActiveJoystickFromSettings();
        connect(&_joystickCheckTimer, &QTimer::timeout, this, &JoystickManager::_updateAvailableJoysticks);
        _joystickCheckTimer.start(250);
    }
}

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

71
#ifdef __sdljoystick__
Jacob Walser's avatar
Jacob Walser committed
72 73
    // Get the latest joystick mapping
    newMap = JoystickSDL::discover(_multiVehicleManager);
74
#elif defined(__android__)
75 76 77 78
    /*
     * Android Joystick not yet supported
     * _name2JoystickMap = JoystickAndroid::discover(_multiVehicleManager);
     */
79
#endif
80

Jacob Walser's avatar
Jacob Walser committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    // 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();
            if (_activeJoystick && !newMap.contains(_activeJoystick->name())) {
                qCDebug(JoystickManagerLog) << "\twas active";
                _activeJoystick->stopPolling();
                _activeJoystick = NULL;
            }
            // FIXME: Don't leave this hanging! We need to free the object.
            //i.value()->deleteLater();
        }
95
    }
96

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

Jacob Walser's avatar
Jacob Walser committed
100 101 102 103 104
    if (!_name2JoystickMap.count()) {
        setActiveJoystick(NULL);
        return;
    }
	
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    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
126 127

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

    if (_activeJoystick == joystick) {
        return;
    }

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

        settings.beginGroup(_settingsGroup);
        settings.setValue(_settingsKeyActiveJoystick, _activeJoystick->name());
        emit activeJoystickChanged(_activeJoystick);
        emit activeJoystickNameChanged(_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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

void JoystickManager::_updateAvailableJoysticks(void)
{
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
    	switch(event.type) {
        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;
		default:
            break;
        }
    }
}