src/platform/qt/InputController.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "InputController.h"
7
8#include "ConfigController.h"
9#include "GamepadAxisEvent.h"
10#include "GamepadButtonEvent.h"
11
12#include <QApplication>
13#include <QTimer>
14#include <QWidget>
15
16extern "C" {
17#include "util/configuration.h"
18}
19
20using namespace QGBA;
21
22#ifdef BUILD_SDL
23int InputController::s_sdlInited = 0;
24GBASDLEvents InputController::s_sdlEvents;
25#endif
26
27InputController::InputController(int playerId, QObject* parent)
28 : QObject(parent)
29 , m_playerId(playerId)
30 , m_config(nullptr)
31 , m_gamepadTimer(nullptr)
32#ifdef BUILD_SDL
33 , m_playerAttached(false)
34#endif
35 , m_allowOpposing(false)
36{
37 GBAInputMapInit(&m_inputMap);
38
39#ifdef BUILD_SDL
40 if (s_sdlInited == 0) {
41 GBASDLInitEvents(&s_sdlEvents);
42 }
43 ++s_sdlInited;
44 m_sdlPlayer.bindings = &m_inputMap;
45 GBASDLInitBindings(&m_inputMap);
46
47 m_gamepadTimer = new QTimer(this);
48 connect(m_gamepadTimer, SIGNAL(timeout()), this, SLOT(testGamepad()));
49 m_gamepadTimer->setInterval(50);
50 m_gamepadTimer->start();
51#endif
52
53 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_X, GBA_KEY_A);
54 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Z, GBA_KEY_B);
55 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_A, GBA_KEY_L);
56 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_S, GBA_KEY_R);
57 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Return, GBA_KEY_START);
58 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Backspace, GBA_KEY_SELECT);
59 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Up, GBA_KEY_UP);
60 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Down, GBA_KEY_DOWN);
61 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Left, GBA_KEY_LEFT);
62 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Right, GBA_KEY_RIGHT);
63}
64
65InputController::~InputController() {
66 GBAInputMapDeinit(&m_inputMap);
67
68#ifdef BUILD_SDL
69 --s_sdlInited;
70 if (s_sdlInited == 0) {
71 GBASDLDeinitEvents(&s_sdlEvents);
72 }
73#endif
74}
75
76void InputController::setConfiguration(ConfigController* config) {
77 m_config = config;
78 setAllowOpposing(config->getOption("allowOpposingDirections").toInt());
79 loadConfiguration(KEYBOARD);
80#ifdef BUILD_SDL
81 GBASDLEventsLoadConfig(&s_sdlEvents, config->input());
82 if (!m_playerAttached) {
83 GBASDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer);
84 m_playerAttached = true;
85 }
86 loadConfiguration(SDL_BINDING_BUTTON);
87 loadProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));
88#endif
89}
90
91void InputController::loadConfiguration(uint32_t type) {
92 GBAInputMapLoad(&m_inputMap, type, m_config->input());
93}
94
95void InputController::loadProfile(uint32_t type, const QString& profile) {
96 GBAInputProfileLoad(&m_inputMap, type, m_config->input(), profile.toLocal8Bit().constData());
97}
98
99void InputController::saveConfiguration(uint32_t type) {
100 GBAInputMapSave(&m_inputMap, type, m_config->input());
101 m_config->write();
102}
103
104void InputController::saveProfile(uint32_t type, const QString& profile) {
105 GBAInputProfileSave(&m_inputMap, type, m_config->input(), profile.toLocal8Bit().constData());
106 m_config->write();
107}
108
109const char* InputController::profileForType(uint32_t type) {
110 UNUSED(type);
111#ifdef BUILD_SDL
112 if (type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
113#if SDL_VERSION_ATLEAST(2, 0, 0)
114 return SDL_JoystickName(m_sdlPlayer.joystick);
115#else
116 return SDL_JoystickName(SDL_JoystickIndex(m_sdlPlayer.joystick));
117#endif
118 }
119#endif
120 return 0;
121}
122
123#ifdef BUILD_SDL
124QStringList InputController::connectedGamepads(uint32_t type) const {
125 UNUSED(type);
126 if (type != SDL_BINDING_BUTTON) {
127 return QStringList();
128 }
129
130 QStringList pads;
131 for (size_t i = 0; i < s_sdlEvents.nJoysticks; ++i) {
132 const char* name;
133#if SDL_VERSION_ATLEAST(2, 0, 0)
134 name = SDL_JoystickName(s_sdlEvents.joysticks[i]);
135#else
136 name = SDL_JoystickName(SDL_JoystickIndex(s_sdlEvents.joysticks[i]));
137#endif
138 if (name) {
139 pads.append(QString(name));
140 } else {
141 pads.append(QString());
142 }
143 }
144 return pads;
145}
146
147void InputController::setPreferredGamepad(uint32_t type, const QString& device) {
148 if (!m_config) {
149 return;
150 }
151 GBAInputSetPreferredDevice(m_config->input(), type, m_sdlPlayer.playerId, device.toLocal8Bit().constData());
152}
153
154GBARumble* InputController::rumble() {
155 return &m_sdlPlayer.rumble.d;
156}
157#endif
158
159GBAKey InputController::mapKeyboard(int key) const {
160 return GBAInputMapKey(&m_inputMap, KEYBOARD, key);
161}
162
163void InputController::bindKey(uint32_t type, int key, GBAKey gbaKey) {
164 return GBAInputBindKey(&m_inputMap, type, key, gbaKey);
165}
166
167#ifdef BUILD_SDL
168int InputController::testSDLEvents() {
169 SDL_Joystick* joystick = m_sdlPlayer.joystick;
170 SDL_JoystickUpdate();
171 int numButtons = SDL_JoystickNumButtons(joystick);
172 int activeButtons = 0;
173 int i;
174 for (i = 0; i < numButtons; ++i) {
175 GBAKey key = GBAInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
176 if (key == GBA_KEY_NONE) {
177 continue;
178 }
179 if (hasPendingEvent(key)) {
180 continue;
181 }
182 if (SDL_JoystickGetButton(joystick, i)) {
183 activeButtons |= 1 << key;
184 }
185 }
186 int numHats = SDL_JoystickNumHats(joystick);
187 for (i = 0; i < numHats; ++i) {
188 int hat = SDL_JoystickGetHat(joystick, i);
189 if (hat & SDL_HAT_UP) {
190 activeButtons |= 1 << GBA_KEY_UP;
191 }
192 if (hat & SDL_HAT_LEFT) {
193 activeButtons |= 1 << GBA_KEY_LEFT;
194 }
195 if (hat & SDL_HAT_DOWN) {
196 activeButtons |= 1 << GBA_KEY_DOWN;
197 }
198 if (hat & SDL_HAT_RIGHT) {
199 activeButtons |= 1 << GBA_KEY_RIGHT;
200 }
201 }
202
203 int numAxes = SDL_JoystickNumAxes(joystick);
204 for (i = 0; i < numAxes; ++i) {
205 int value = SDL_JoystickGetAxis(joystick, i);
206
207 enum GBAKey key = GBAInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value);
208 if (key != GBA_KEY_NONE) {
209 activeButtons |= 1 << key;
210 }
211 }
212 return activeButtons;
213}
214
215QSet<int> InputController::activeGamepadButtons() {
216 SDL_Joystick* joystick = m_sdlPlayer.joystick;
217 SDL_JoystickUpdate();
218 int numButtons = SDL_JoystickNumButtons(joystick);
219 QSet<int> activeButtons;
220 int i;
221 for (i = 0; i < numButtons; ++i) {
222 if (SDL_JoystickGetButton(joystick, i)) {
223 activeButtons.insert(i);
224 }
225 }
226 return activeButtons;
227}
228
229QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes() {
230 SDL_Joystick* joystick = m_sdlPlayer.joystick;
231 SDL_JoystickUpdate();
232 int numButtons = SDL_JoystickNumAxes(joystick);
233 QSet<QPair<int, GamepadAxisEvent::Direction>> activeAxes;
234 int i;
235 for (i = 0; i < numButtons; ++i) {
236 int32_t axis = SDL_JoystickGetAxis(joystick, i);
237 if (axis >= AXIS_THRESHOLD || axis <= -AXIS_THRESHOLD) {
238 activeAxes.insert(qMakePair(i, axis > 0 ? GamepadAxisEvent::POSITIVE : GamepadAxisEvent::NEGATIVE));
239 }
240 }
241 return activeAxes;
242}
243
244void InputController::bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction direction, GBAKey key) {
245 const GBAAxis* old = GBAInputQueryAxis(&m_inputMap, SDL_BINDING_BUTTON, axis);
246 GBAAxis description = { GBA_KEY_NONE, GBA_KEY_NONE, -AXIS_THRESHOLD, AXIS_THRESHOLD };
247 if (old) {
248 description = *old;
249 }
250 switch (direction) {
251 case GamepadAxisEvent::NEGATIVE:
252 description.lowDirection = key;
253 description.deadLow = -AXIS_THRESHOLD;
254 break;
255 case GamepadAxisEvent::POSITIVE:
256 description.highDirection = key;
257 description.deadHigh = AXIS_THRESHOLD;
258 break;
259 default:
260 return;
261 }
262 GBAInputBindAxis(&m_inputMap, SDL_BINDING_BUTTON, axis, &description);
263}
264#endif
265
266void InputController::testGamepad() {
267#ifdef BUILD_SDL
268 auto activeAxes = activeGamepadAxes();
269 auto oldAxes = m_activeAxes;
270 m_activeAxes = activeAxes;
271
272 auto activeButtons = activeGamepadButtons();
273 auto oldButtons = m_activeButtons;
274 m_activeButtons = activeButtons;
275
276 if (!QApplication::focusWidget()) {
277 return;
278 }
279
280 activeAxes.subtract(oldAxes);
281 oldAxes.subtract(m_activeAxes);
282
283 for (auto& axis : m_activeAxes) {
284 bool newlyAboveThreshold = activeAxes.contains(axis);
285 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, this);
286 if (newlyAboveThreshold) {
287 postPendingEvent(event->gbaKey());
288 QApplication::sendEvent(QApplication::focusWidget(), event);
289 if (!event->isAccepted()) {
290 clearPendingEvent(event->gbaKey());
291 }
292 } else if (oldAxes.contains(axis)) {
293 clearPendingEvent(event->gbaKey());
294 QApplication::sendEvent(QApplication::focusWidget(), event);
295 }
296 }
297
298 if (!QApplication::focusWidget()) {
299 return;
300 }
301
302 activeButtons.subtract(oldButtons);
303 oldButtons.subtract(m_activeButtons);
304
305 for (int button : activeButtons) {
306 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, this);
307 postPendingEvent(event->gbaKey());
308 QApplication::sendEvent(QApplication::focusWidget(), event);
309 if (!event->isAccepted()) {
310 clearPendingEvent(event->gbaKey());
311 }
312 }
313 for (int button : oldButtons) {
314 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, this);
315 clearPendingEvent(event->gbaKey());
316 QApplication::sendEvent(QApplication::focusWidget(), event);
317 }
318#endif
319}
320
321void InputController::postPendingEvent(GBAKey key) {
322 m_pendingEvents.insert(key);
323}
324
325void InputController::clearPendingEvent(GBAKey key) {
326 m_pendingEvents.remove(key);
327}
328
329bool InputController::hasPendingEvent(GBAKey key) const {
330 return m_pendingEvents.contains(key);
331}