aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/gamepads/sdl2/qsdlgamepadbackend.cpp
blob: 1ffba303364d4cef911c0d34e4faa82e05b43134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Gamepad module
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qsdlgamepadbackend_p.h"

#include <QtCore/QDebug>

#include <SDL.h>
// Reset bool redefinition from SDL header
#undef bool

QT_BEGIN_NAMESPACE

QSdlGamepadBackend::QSdlGamepadBackend(QObject *parent)
    : QGamepadBackend(parent)
{
    connect(&m_eventLoopTimer, SIGNAL(timeout()), this, SLOT(pumpSdlEventLoop()));
}

QSdlGamepadBackend::~QSdlGamepadBackend()
{
}

void QSdlGamepadBackend::pumpSdlEventLoop()
{
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_CONTROLLERAXISMOTION) {
            SDL_ControllerAxisEvent axisEvent = event.caxis;
            //qDebug() << axisEvent.timestamp << "Axis Event: " << axisEvent.which << axisEvent.axis << axisEvent.value;
            double value;
            if (axisEvent.value >= 0)
                value = axisEvent.value / 32767.0;
            else
                value = axisEvent.value / 32768.0;
            switch (axisEvent.axis) {
                case SDL_CONTROLLER_AXIS_LEFTX:
                    emit gamepadAxisMoved(m_instanceIdForIndex[axisEvent.which], QGamepadManager::AxisLeftX, value);
                    break;
                case SDL_CONTROLLER_AXIS_LEFTY:
                    emit gamepadAxisMoved(m_instanceIdForIndex[axisEvent.which], QGamepadManager::AxisLeftY, value);
                    break;
                case SDL_CONTROLLER_AXIS_RIGHTX:
                    emit gamepadAxisMoved(m_instanceIdForIndex[axisEvent.which], QGamepadManager::AxisRightX, value);
                    break;
                case SDL_CONTROLLER_AXIS_RIGHTY:
                    emit gamepadAxisMoved(m_instanceIdForIndex[axisEvent.which], QGamepadManager::AxisRightY, value);
                    break;
                case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
                    if (value == 0)
                        emit gamepadButtonReleased(m_instanceIdForIndex[axisEvent.which], QGamepadManager::ButtonL2);
                    else
                        emit gamepadButtonPressed(m_instanceIdForIndex[axisEvent.which], QGamepadManager::ButtonL2, value);
                    break;
                case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
                    if (value == 0)
                        emit gamepadButtonReleased(m_instanceIdForIndex[axisEvent.which], QGamepadManager::ButtonR2);
                    else
                        emit gamepadButtonPressed(m_instanceIdForIndex[axisEvent.which], QGamepadManager::ButtonR2, value);
                    break;
                default:
                    break;
            }

        } else if (event.type == SDL_CONTROLLERBUTTONDOWN) {
            SDL_ControllerButtonEvent buttonEvent = event.cbutton;
            //qDebug() << buttonEvent.timestamp << "Button Press: " << buttonEvent.which << buttonEvent.button << buttonEvent.state;
            emit gamepadButtonPressed(m_instanceIdForIndex[buttonEvent.which], translateButton(buttonEvent.button), 1.0);
        } else if (event.type == SDL_CONTROLLERBUTTONUP) {
            SDL_ControllerButtonEvent buttonEvent = event.cbutton;
            //qDebug() << buttonEvent.timestamp << "Button Release: " << buttonEvent.which << buttonEvent.button << buttonEvent.state;
            emit gamepadButtonReleased(m_instanceIdForIndex[buttonEvent.which], translateButton(buttonEvent.button));
        } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
            SDL_ControllerDeviceEvent deviceEvent = event.cdevice;
            //qDebug() << deviceEvent.timestamp << "Controller Added: " << deviceEvent.which;
            addController(deviceEvent.which);
        } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
            SDL_ControllerDeviceEvent deviceEvent = event.cdevice;

            int index = m_instanceIdForIndex[deviceEvent.which];
            SDL_GameControllerClose(m_indexForController[index]);
            emit gamepadRemoved(index);
            m_indexForController.remove(index);
            m_instanceIdForIndex.remove(deviceEvent.which);

        } else if (event.type == SDL_CONTROLLERDEVICEREMAPPED) {
            //SDL_ControllerDeviceEvent deviceEvent = event.cdevice;
            //qDebug() << deviceEvent.timestamp << "Controller Remapped: " << deviceEvent.which;
        }
    }
}

bool QSdlGamepadBackend::start()
{
    //Initialize SDL with necessary subsystems for gamepad support
    if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK)) {
        qDebug() << SDL_GetError();
        return false;
    }

    m_eventLoopTimer.start(16);
    for (int i = 0; i < SDL_NumJoysticks() ; i++)
        addController(i);

    return true;
}

void QSdlGamepadBackend::stop()
{
    m_eventLoopTimer.stop();
    SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK);
}

QGamepadManager::GamepadButton QSdlGamepadBackend::translateButton(int button)
{
    switch (button) {
    case SDL_CONTROLLER_BUTTON_A:
        return QGamepadManager::ButtonA;
    case SDL_CONTROLLER_BUTTON_B:
        return QGamepadManager::ButtonB;
    case SDL_CONTROLLER_BUTTON_X:
        return QGamepadManager::ButtonX;
    case SDL_CONTROLLER_BUTTON_Y:
        return QGamepadManager::ButtonY;
    case SDL_CONTROLLER_BUTTON_BACK:
        return QGamepadManager::ButtonSelect;
    case SDL_CONTROLLER_BUTTON_GUIDE:
        return QGamepadManager::ButtonGuide;
    case SDL_CONTROLLER_BUTTON_START:
        return QGamepadManager::ButtonStart;
    case SDL_CONTROLLER_BUTTON_LEFTSTICK:
        return QGamepadManager::ButtonL3;
    case SDL_CONTROLLER_BUTTON_RIGHTSTICK:
        return QGamepadManager::ButtonR3;
    case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
        return QGamepadManager::ButtonL1;
    case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
        return QGamepadManager::ButtonR1;
    case SDL_CONTROLLER_BUTTON_DPAD_UP:
        return QGamepadManager::ButtonUp;
    case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
        return QGamepadManager::ButtonDown;
    case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
        return QGamepadManager::ButtonLeft;
    case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
        return QGamepadManager::ButtonRight;
    default:
        return QGamepadManager::ButtonInvalid;
    }
}

void QSdlGamepadBackend::addController(int index)
{
    char GUID[100];
    SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(index), GUID, 100);
    if (!SDL_IsGameController(index))
        return;

    SDL_GameController *controller = SDL_GameControllerOpen(index);
    if (controller) {
        m_indexForController.insert(index, controller);
        int instanceID = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
        m_instanceIdForIndex.insert(instanceID, index);
        //qDebug() << "Controller " << index << " added with instanceId: " << instanceID;
        emit gamepadAdded(index);
    }
}

QT_END_NAMESPACE