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 if (m_playerAttached) {
70 GBASDLDetachPlayer(&s_sdlEvents, &m_sdlPlayer);
71 }
72
73 --s_sdlInited;
74 if (s_sdlInited == 0) {
75 GBASDLDeinitEvents(&s_sdlEvents);
76 }
77#endif
78}
79
80void InputController::setConfiguration(ConfigController* config) {
81 m_config = config;
82 setAllowOpposing(config->getOption("allowOpposingDirections").toInt());
83 loadConfiguration(KEYBOARD);
84#ifdef BUILD_SDL
85 GBASDLEventsLoadConfig(&s_sdlEvents, config->input());
86 if (!m_playerAttached) {
87 GBASDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer);
88 m_playerAttached = true;
89 }
90 loadConfiguration(SDL_BINDING_BUTTON);
91 loadProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));
92#endif
93}
94
95void InputController::loadConfiguration(uint32_t type) {
96 GBAInputMapLoad(&m_inputMap, type, m_config->input());
97#ifdef BUILD_SDL
98 if (m_playerAttached) {
99 GBASDLPlayerLoadConfig(&m_sdlPlayer, m_config->input());
100 }
101#endif
102}
103
104void InputController::loadProfile(uint32_t type, const QString& profile) {
105 GBAInputProfileLoad(&m_inputMap, type, m_config->input(), profile.toLocal8Bit().constData());
106#ifdef BUILD_SDL
107 recalibrateAxes();
108#endif
109}
110
111void InputController::saveConfiguration(uint32_t type) {
112 GBAInputMapSave(&m_inputMap, type, m_config->input());
113#ifdef BUILD_SDL
114 if (m_playerAttached) {
115 GBASDLPlayerSaveConfig(&m_sdlPlayer, m_config->input());
116 }
117#endif
118 m_config->write();
119}
120
121void InputController::saveProfile(uint32_t type, const QString& profile) {
122 GBAInputProfileSave(&m_inputMap, type, m_config->input(), profile.toLocal8Bit().constData());
123 m_config->write();
124}
125
126const char* InputController::profileForType(uint32_t type) {
127 UNUSED(type);
128#ifdef BUILD_SDL
129 if (type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
130#if SDL_VERSION_ATLEAST(2, 0, 0)
131 return SDL_JoystickName(m_sdlPlayer.joystick);
132#else
133 return SDL_JoystickName(SDL_JoystickIndex(m_sdlPlayer.joystick));
134#endif
135 }
136#endif
137 return 0;
138}
139
140#ifdef BUILD_SDL
141QStringList InputController::connectedGamepads(uint32_t type) const {
142 UNUSED(type);
143 if (type != SDL_BINDING_BUTTON) {
144 return QStringList();
145 }
146
147 QStringList pads;
148 for (size_t i = 0; i < s_sdlEvents.nJoysticks; ++i) {
149 const char* name;
150#if SDL_VERSION_ATLEAST(2, 0, 0)
151 name = SDL_JoystickName(s_sdlEvents.joysticks[i]);
152#else
153 name = SDL_JoystickName(SDL_JoystickIndex(s_sdlEvents.joysticks[i]));
154#endif
155 if (name) {
156 pads.append(QString(name));
157 } else {
158 pads.append(QString());
159 }
160 }
161 return pads;
162}
163
164void InputController::setPreferredGamepad(uint32_t type, const QString& device) {
165 if (!m_config) {
166 return;
167 }
168 GBAInputSetPreferredDevice(m_config->input(), type, m_sdlPlayer.playerId, device.toLocal8Bit().constData());
169}
170
171GBARumble* InputController::rumble() {
172 return &m_sdlPlayer.rumble.d;
173}
174
175GBARotationSource* InputController::rotationSource() {
176 return &m_sdlPlayer.rotation.d;
177}
178
179void InputController::registerTiltAxisX(int axis) {
180 m_sdlPlayer.rotation.axisX = axis;
181}
182void InputController::registerTiltAxisY(int axis) {
183 m_sdlPlayer.rotation.axisY = axis;
184}
185void InputController::registerGyroAxisX(int axis) {
186 m_sdlPlayer.rotation.gyroX = axis;
187}
188void InputController::registerGyroAxisY(int axis) {
189 m_sdlPlayer.rotation.gyroY = axis;
190}
191
192float InputController::gyroSensitivity() const {
193 return m_sdlPlayer.rotation.gyroSensitivity;
194}
195
196void InputController::setGyroSensitivity(float sensitivity) {
197 m_sdlPlayer.rotation.gyroSensitivity = sensitivity;
198}
199#else
200GBARumble* InputController::rumble() {
201 return nullptr;
202}
203
204GBARotationSource* InputController::rotationSource() {
205 return nullptr;
206}
207#endif
208
209GBAKey InputController::mapKeyboard(int key) const {
210 return GBAInputMapKey(&m_inputMap, KEYBOARD, key);
211}
212
213void InputController::bindKey(uint32_t type, int key, GBAKey gbaKey) {
214 return GBAInputBindKey(&m_inputMap, type, key, gbaKey);
215}
216
217#ifdef BUILD_SDL
218int InputController::testSDLEvents() {
219 SDL_Joystick* joystick = m_sdlPlayer.joystick;
220 SDL_JoystickUpdate();
221 int numButtons = SDL_JoystickNumButtons(joystick);
222 int activeButtons = 0;
223 int i;
224 for (i = 0; i < numButtons; ++i) {
225 GBAKey key = GBAInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
226 if (key == GBA_KEY_NONE) {
227 continue;
228 }
229 if (hasPendingEvent(key)) {
230 continue;
231 }
232 if (SDL_JoystickGetButton(joystick, i)) {
233 activeButtons |= 1 << key;
234 }
235 }
236 int numHats = SDL_JoystickNumHats(joystick);
237 for (i = 0; i < numHats; ++i) {
238 int hat = SDL_JoystickGetHat(joystick, i);
239 if (hat & SDL_HAT_UP) {
240 activeButtons |= 1 << GBA_KEY_UP;
241 }
242 if (hat & SDL_HAT_LEFT) {
243 activeButtons |= 1 << GBA_KEY_LEFT;
244 }
245 if (hat & SDL_HAT_DOWN) {
246 activeButtons |= 1 << GBA_KEY_DOWN;
247 }
248 if (hat & SDL_HAT_RIGHT) {
249 activeButtons |= 1 << GBA_KEY_RIGHT;
250 }
251 }
252
253 int numAxes = SDL_JoystickNumAxes(joystick);
254 for (i = 0; i < numAxes; ++i) {
255 int value = SDL_JoystickGetAxis(joystick, i);
256
257 enum GBAKey key = GBAInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value);
258 if (key != GBA_KEY_NONE) {
259 activeButtons |= 1 << key;
260 }
261 }
262 return activeButtons;
263}
264
265QSet<int> InputController::activeGamepadButtons() {
266 SDL_Joystick* joystick = m_sdlPlayer.joystick;
267 SDL_JoystickUpdate();
268 int numButtons = SDL_JoystickNumButtons(joystick);
269 QSet<int> activeButtons;
270 int i;
271 for (i = 0; i < numButtons; ++i) {
272 if (SDL_JoystickGetButton(joystick, i)) {
273 activeButtons.insert(i);
274 }
275 }
276 return activeButtons;
277}
278
279void InputController::recalibrateAxes() {
280 SDL_Joystick* joystick = m_sdlPlayer.joystick;
281 SDL_JoystickUpdate();
282 int numAxes = SDL_JoystickNumAxes(joystick);
283 if (numAxes < 1) {
284 return;
285 }
286 m_deadzones.resize(numAxes);
287 int i;
288 for (i = 0; i < numAxes; ++i) {
289 m_deadzones[i] = SDL_JoystickGetAxis(joystick, i);
290 }
291}
292
293QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes() {
294 SDL_Joystick* joystick = m_sdlPlayer.joystick;
295 SDL_JoystickUpdate();
296 int numAxes = SDL_JoystickNumAxes(joystick);
297 QSet<QPair<int, GamepadAxisEvent::Direction>> activeAxes;
298 if (numAxes < 1) {
299 return activeAxes;
300 }
301 m_deadzones.resize(numAxes);
302 int i;
303 for (i = 0; i < numAxes; ++i) {
304 int32_t axis = SDL_JoystickGetAxis(joystick, i);
305 axis -= m_deadzones[i];
306 if (axis >= AXIS_THRESHOLD || axis <= -AXIS_THRESHOLD) {
307 activeAxes.insert(qMakePair(i, axis > 0 ? GamepadAxisEvent::POSITIVE : GamepadAxisEvent::NEGATIVE));
308 }
309 }
310 return activeAxes;
311}
312
313void InputController::bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction direction, GBAKey key) {
314 const GBAAxis* old = GBAInputQueryAxis(&m_inputMap, SDL_BINDING_BUTTON, axis);
315 GBAAxis description = { GBA_KEY_NONE, GBA_KEY_NONE, -AXIS_THRESHOLD, AXIS_THRESHOLD };
316 if (old) {
317 description = *old;
318 }
319 switch (direction) {
320 case GamepadAxisEvent::NEGATIVE:
321 description.lowDirection = key;
322 description.deadLow = m_deadzones[axis] - AXIS_THRESHOLD;
323 break;
324 case GamepadAxisEvent::POSITIVE:
325 description.highDirection = key;
326 description.deadHigh = m_deadzones[axis] + AXIS_THRESHOLD;
327 break;
328 default:
329 return;
330 }
331 GBAInputBindAxis(&m_inputMap, SDL_BINDING_BUTTON, axis, &description);
332}
333#endif
334
335void InputController::testGamepad() {
336#ifdef BUILD_SDL
337 auto activeAxes = activeGamepadAxes();
338 auto oldAxes = m_activeAxes;
339 m_activeAxes = activeAxes;
340
341 auto activeButtons = activeGamepadButtons();
342 auto oldButtons = m_activeButtons;
343 m_activeButtons = activeButtons;
344
345 if (!QApplication::focusWidget()) {
346 return;
347 }
348
349 activeAxes.subtract(oldAxes);
350 oldAxes.subtract(m_activeAxes);
351
352 for (auto& axis : m_activeAxes) {
353 bool newlyAboveThreshold = activeAxes.contains(axis);
354 if (newlyAboveThreshold) {
355 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, this);
356 postPendingEvent(event->gbaKey());
357 QApplication::sendEvent(QApplication::focusWidget(), event);
358 if (!event->isAccepted()) {
359 clearPendingEvent(event->gbaKey());
360 }
361 }
362 }
363 for (auto axis : oldAxes) {
364 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, this);
365 clearPendingEvent(event->gbaKey());
366 QApplication::sendEvent(QApplication::focusWidget(), event);
367 }
368
369 if (!QApplication::focusWidget()) {
370 return;
371 }
372
373 activeButtons.subtract(oldButtons);
374 oldButtons.subtract(m_activeButtons);
375
376 for (int button : activeButtons) {
377 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, this);
378 postPendingEvent(event->gbaKey());
379 QApplication::sendEvent(QApplication::focusWidget(), event);
380 if (!event->isAccepted()) {
381 clearPendingEvent(event->gbaKey());
382 }
383 }
384 for (int button : oldButtons) {
385 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, this);
386 clearPendingEvent(event->gbaKey());
387 QApplication::sendEvent(QApplication::focusWidget(), event);
388 }
389#endif
390}
391
392void InputController::postPendingEvent(GBAKey key) {
393 m_pendingEvents.insert(key);
394}
395
396void InputController::clearPendingEvent(GBAKey key) {
397 m_pendingEvents.remove(key);
398}
399
400bool InputController::hasPendingEvent(GBAKey key) const {
401 return m_pendingEvents.contains(key);
402}