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#include "InputProfile.h"
12
13#include <QApplication>
14#include <QTimer>
15#include <QWidget>
16
17extern "C" {
18#include "util/configuration.h"
19}
20
21using namespace QGBA;
22
23#ifdef BUILD_SDL
24int InputController::s_sdlInited = 0;
25GBASDLEvents InputController::s_sdlEvents;
26#endif
27
28InputController::InputController(int playerId, QWidget* topLevel, QObject* parent)
29 : QObject(parent)
30 , m_playerId(playerId)
31 , m_config(nullptr)
32 , m_gamepadTimer(nullptr)
33#ifdef BUILD_SDL
34 , m_playerAttached(false)
35#endif
36 , m_allowOpposing(false)
37 , m_topLevel(topLevel)
38 , m_focusParent(topLevel)
39{
40 GBAInputMapInit(&m_inputMap);
41
42#ifdef BUILD_SDL
43 if (s_sdlInited == 0) {
44 GBASDLInitEvents(&s_sdlEvents);
45 }
46 ++s_sdlInited;
47 m_sdlPlayer.bindings = &m_inputMap;
48 GBASDLInitBindings(&m_inputMap);
49#endif
50
51 m_gamepadTimer = new QTimer(this);
52#ifdef BUILD_SDL
53 connect(m_gamepadTimer, &QTimer::timeout, [this]() {
54 testGamepad(SDL_BINDING_BUTTON);
55 });
56#endif
57 m_gamepadTimer->setInterval(50);
58 m_gamepadTimer->start();
59
60 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_X, GBA_KEY_A);
61 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Z, GBA_KEY_B);
62 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_A, GBA_KEY_L);
63 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_S, GBA_KEY_R);
64 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Return, GBA_KEY_START);
65 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Backspace, GBA_KEY_SELECT);
66 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Up, GBA_KEY_UP);
67 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Down, GBA_KEY_DOWN);
68 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Left, GBA_KEY_LEFT);
69 GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Right, GBA_KEY_RIGHT);
70}
71
72InputController::~InputController() {
73 GBAInputMapDeinit(&m_inputMap);
74
75#ifdef BUILD_SDL
76 if (m_playerAttached) {
77 GBASDLDetachPlayer(&s_sdlEvents, &m_sdlPlayer);
78 }
79
80 --s_sdlInited;
81 if (s_sdlInited == 0) {
82 GBASDLDeinitEvents(&s_sdlEvents);
83 }
84#endif
85}
86
87void InputController::setConfiguration(ConfigController* config) {
88 m_config = config;
89 setAllowOpposing(config->getOption("allowOpposingDirections").toInt());
90 loadConfiguration(KEYBOARD);
91#ifdef BUILD_SDL
92 GBASDLEventsLoadConfig(&s_sdlEvents, config->input());
93 if (!m_playerAttached) {
94 m_playerAttached = GBASDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer);
95 }
96 loadConfiguration(SDL_BINDING_BUTTON);
97 loadProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));
98#endif
99}
100
101void InputController::loadConfiguration(uint32_t type) {
102 GBAInputMapLoad(&m_inputMap, type, m_config->input());
103#ifdef BUILD_SDL
104 if (m_playerAttached) {
105 GBASDLPlayerLoadConfig(&m_sdlPlayer, m_config->input());
106 }
107#endif
108}
109
110void InputController::loadProfile(uint32_t type, const QString& profile) {
111 bool loaded = GBAInputProfileLoad(&m_inputMap, type, m_config->input(), profile.toUtf8().constData());
112 recalibrateAxes();
113 if (!loaded) {
114 const InputProfile* ip = InputProfile::findProfile(profile);
115 if (ip) {
116 ip->apply(this);
117 }
118 }
119 emit profileLoaded(profile);
120}
121
122void InputController::saveConfiguration() {
123 saveConfiguration(KEYBOARD);
124#ifdef BUILD_SDL
125 saveConfiguration(SDL_BINDING_BUTTON);
126 saveProfile(SDL_BINDING_BUTTON, profileForType(SDL_BINDING_BUTTON));
127 if (m_playerAttached) {
128 GBASDLPlayerSaveConfig(&m_sdlPlayer, m_config->input());
129 }
130 m_config->write();
131#endif
132}
133
134void InputController::saveConfiguration(uint32_t type) {
135 GBAInputMapSave(&m_inputMap, type, m_config->input());
136 m_config->write();
137}
138
139void InputController::saveProfile(uint32_t type, const QString& profile) {
140 GBAInputProfileSave(&m_inputMap, type, m_config->input(), profile.toUtf8().constData());
141 m_config->write();
142}
143
144const char* InputController::profileForType(uint32_t type) {
145 UNUSED(type);
146#ifdef BUILD_SDL
147 if (type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
148#if SDL_VERSION_ATLEAST(2, 0, 0)
149 return SDL_JoystickName(m_sdlPlayer.joystick);
150#else
151 return SDL_JoystickName(SDL_JoystickIndex(m_sdlPlayer.joystick));
152#endif
153 }
154#endif
155 return 0;
156}
157
158QStringList InputController::connectedGamepads(uint32_t type) const {
159 UNUSED(type);
160
161#ifdef BUILD_SDL
162 if (type == SDL_BINDING_BUTTON) {
163 QStringList pads;
164 for (size_t i = 0; i < s_sdlEvents.nJoysticks; ++i) {
165 const char* name;
166#if SDL_VERSION_ATLEAST(2, 0, 0)
167 name = SDL_JoystickName(s_sdlEvents.joysticks[i]);
168#else
169 name = SDL_JoystickName(SDL_JoystickIndex(s_sdlEvents.joysticks[i]));
170#endif
171 if (name) {
172 pads.append(QString(name));
173 } else {
174 pads.append(QString());
175 }
176 }
177 return pads;
178 }
179#endif
180
181 return QStringList();
182}
183
184int InputController::gamepad(uint32_t type) const {
185#ifdef BUILD_SDL
186 if (type == SDL_BINDING_BUTTON) {
187 return m_sdlPlayer.joystickIndex;
188 }
189#endif
190 return 0;
191}
192
193void InputController::setGamepad(uint32_t type, int index) {
194#ifdef BUILD_SDL
195 if (type == SDL_BINDING_BUTTON) {
196 GBASDLPlayerChangeJoystick(&s_sdlEvents, &m_sdlPlayer, index);
197 }
198#endif
199}
200
201void InputController::setPreferredGamepad(uint32_t type, const QString& device) {
202 if (!m_config) {
203 return;
204 }
205 GBAInputSetPreferredDevice(m_config->input(), type, m_playerId, device.toUtf8().constData());
206}
207
208GBARumble* InputController::rumble() {
209#ifdef BUILD_SDL
210#if SDL_VERSION_ATLEAST(2, 0, 0)
211 if (m_playerAttached) {
212 return &m_sdlPlayer.rumble.d;
213 }
214#endif
215#endif
216 return nullptr;
217}
218
219GBARotationSource* InputController::rotationSource() {
220#ifdef BUILD_SDL
221 if (m_playerAttached) {
222 return &m_sdlPlayer.rotation.d;
223 }
224#endif
225 return nullptr;
226}
227
228void InputController::registerTiltAxisX(int axis) {
229#ifdef BUILD_SDL
230 if (m_playerAttached) {
231 m_sdlPlayer.rotation.axisX = axis;
232 }
233#endif
234}
235
236void InputController::registerTiltAxisY(int axis) {
237#ifdef BUILD_SDL
238 if (m_playerAttached) {
239 m_sdlPlayer.rotation.axisY = axis;
240 }
241#endif
242}
243
244void InputController::registerGyroAxisX(int axis) {
245#ifdef BUILD_SDL
246 if (m_playerAttached) {
247 m_sdlPlayer.rotation.gyroX = axis;
248 }
249#endif
250}
251
252void InputController::registerGyroAxisY(int axis) {
253#ifdef BUILD_SDL
254 if (m_playerAttached) {
255 m_sdlPlayer.rotation.gyroY = axis;
256 }
257#endif
258}
259
260float InputController::gyroSensitivity() const {
261#ifdef BUILD_SDL
262 if (m_playerAttached) {
263 return m_sdlPlayer.rotation.gyroSensitivity;
264 }
265#endif
266 return 0;
267}
268
269void InputController::setGyroSensitivity(float sensitivity) {
270#ifdef BUILD_SDL
271 if (m_playerAttached) {
272 m_sdlPlayer.rotation.gyroSensitivity = sensitivity;
273 }
274#endif
275}
276
277GBAKey InputController::mapKeyboard(int key) const {
278 return GBAInputMapKey(&m_inputMap, KEYBOARD, key);
279}
280
281void InputController::bindKey(uint32_t type, int key, GBAKey gbaKey) {
282 return GBAInputBindKey(&m_inputMap, type, key, gbaKey);
283}
284
285int InputController::pollEvents() {
286 int activeButtons = 0;
287#ifdef BUILD_SDL
288 if (m_playerAttached) {
289 SDL_Joystick* joystick = m_sdlPlayer.joystick;
290 SDL_JoystickUpdate();
291 int numButtons = SDL_JoystickNumButtons(joystick);
292 int i;
293 for (i = 0; i < numButtons; ++i) {
294 GBAKey key = GBAInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
295 if (key == GBA_KEY_NONE) {
296 continue;
297 }
298 if (hasPendingEvent(key)) {
299 continue;
300 }
301 if (SDL_JoystickGetButton(joystick, i)) {
302 activeButtons |= 1 << key;
303 }
304 }
305 int numHats = SDL_JoystickNumHats(joystick);
306 for (i = 0; i < numHats; ++i) {
307 int hat = SDL_JoystickGetHat(joystick, i);
308 if (hat & SDL_HAT_UP) {
309 activeButtons |= 1 << GBA_KEY_UP;
310 }
311 if (hat & SDL_HAT_LEFT) {
312 activeButtons |= 1 << GBA_KEY_LEFT;
313 }
314 if (hat & SDL_HAT_DOWN) {
315 activeButtons |= 1 << GBA_KEY_DOWN;
316 }
317 if (hat & SDL_HAT_RIGHT) {
318 activeButtons |= 1 << GBA_KEY_RIGHT;
319 }
320 }
321
322 int numAxes = SDL_JoystickNumAxes(joystick);
323 for (i = 0; i < numAxes; ++i) {
324 int value = SDL_JoystickGetAxis(joystick, i);
325
326 enum GBAKey key = GBAInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value);
327 if (key != GBA_KEY_NONE) {
328 activeButtons |= 1 << key;
329 }
330 }
331 }
332#endif
333 return activeButtons;
334}
335
336QSet<int> InputController::activeGamepadButtons(int type) {
337 QSet<int> activeButtons;
338#ifdef BUILD_SDL
339 if (m_playerAttached && type == SDL_BINDING_BUTTON) {
340 SDL_Joystick* joystick = m_sdlPlayer.joystick;
341 SDL_JoystickUpdate();
342 int numButtons = SDL_JoystickNumButtons(joystick);
343 int i;
344 for (i = 0; i < numButtons; ++i) {
345 if (SDL_JoystickGetButton(joystick, i)) {
346 activeButtons.insert(i);
347 }
348 }
349 }
350#endif
351 return activeButtons;
352}
353
354void InputController::recalibrateAxes() {
355#ifdef BUILD_SDL
356 if (m_playerAttached) {
357 SDL_Joystick* joystick = m_sdlPlayer.joystick;
358 SDL_JoystickUpdate();
359 int numAxes = SDL_JoystickNumAxes(joystick);
360 if (numAxes < 1) {
361 return;
362 }
363 m_deadzones.resize(numAxes);
364 int i;
365 for (i = 0; i < numAxes; ++i) {
366 m_deadzones[i] = SDL_JoystickGetAxis(joystick, i);
367 }
368 }
369#endif
370}
371
372QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes(int type) {
373 QSet<QPair<int, GamepadAxisEvent::Direction>> activeAxes;
374#ifdef BUILD_SDL
375 if (m_playerAttached && type == SDL_BINDING_BUTTON) {
376 SDL_Joystick* joystick = m_sdlPlayer.joystick;
377 SDL_JoystickUpdate();
378 int numAxes = SDL_JoystickNumAxes(joystick);
379 if (numAxes < 1) {
380 return activeAxes;
381 }
382 m_deadzones.resize(numAxes);
383 int i;
384 for (i = 0; i < numAxes; ++i) {
385 int32_t axis = SDL_JoystickGetAxis(joystick, i);
386 axis -= m_deadzones[i];
387 if (axis >= AXIS_THRESHOLD || axis <= -AXIS_THRESHOLD) {
388 activeAxes.insert(qMakePair(i, axis > 0 ? GamepadAxisEvent::POSITIVE : GamepadAxisEvent::NEGATIVE));
389 }
390 }
391 }
392#endif
393 return activeAxes;
394}
395
396void InputController::bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction direction, GBAKey key) {
397 const GBAAxis* old = GBAInputQueryAxis(&m_inputMap, type, axis);
398 GBAAxis description = { GBA_KEY_NONE, GBA_KEY_NONE, -AXIS_THRESHOLD, AXIS_THRESHOLD };
399 if (old) {
400 description = *old;
401 }
402 int deadzone = 0;
403 if (m_deadzones.size() > axis) {
404 deadzone = m_deadzones[axis];
405 }
406 switch (direction) {
407 case GamepadAxisEvent::NEGATIVE:
408 description.lowDirection = key;
409
410 description.deadLow = deadzone - AXIS_THRESHOLD;
411 break;
412 case GamepadAxisEvent::POSITIVE:
413 description.highDirection = key;
414 description.deadHigh = deadzone + AXIS_THRESHOLD;
415 break;
416 default:
417 return;
418 }
419 GBAInputBindAxis(&m_inputMap, type, axis, &description);
420}
421
422void InputController::unbindAllAxes(uint32_t type) {
423 GBAInputUnbindAllAxes(&m_inputMap, type);
424}
425
426void InputController::testGamepad(int type) {
427 auto activeAxes = activeGamepadAxes(type);
428 auto oldAxes = m_activeAxes;
429 m_activeAxes = activeAxes;
430
431 auto activeButtons = activeGamepadButtons(type);
432 auto oldButtons = m_activeButtons;
433 m_activeButtons = activeButtons;
434
435 if (!QApplication::focusWidget()) {
436 return;
437 }
438
439 activeAxes.subtract(oldAxes);
440 oldAxes.subtract(m_activeAxes);
441
442 for (auto& axis : m_activeAxes) {
443 bool newlyAboveThreshold = activeAxes.contains(axis);
444 if (newlyAboveThreshold) {
445 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
446 postPendingEvent(event->gbaKey());
447 sendGamepadEvent(event);
448 if (!event->isAccepted()) {
449 clearPendingEvent(event->gbaKey());
450 }
451 }
452 }
453 for (auto axis : oldAxes) {
454 GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
455 clearPendingEvent(event->gbaKey());
456 sendGamepadEvent(event);
457 }
458
459 if (!QApplication::focusWidget()) {
460 return;
461 }
462
463 activeButtons.subtract(oldButtons);
464 oldButtons.subtract(m_activeButtons);
465
466 for (int button : activeButtons) {
467 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
468 postPendingEvent(event->gbaKey());
469 sendGamepadEvent(event);
470 if (!event->isAccepted()) {
471 clearPendingEvent(event->gbaKey());
472 }
473 }
474 for (int button : oldButtons) {
475 GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
476 clearPendingEvent(event->gbaKey());
477 sendGamepadEvent(event);
478 }
479}
480
481void InputController::sendGamepadEvent(QEvent* event) {
482 QWidget* focusWidget = nullptr;
483 if (m_focusParent) {
484 focusWidget = m_focusParent->focusWidget();
485 if (!focusWidget) {
486 focusWidget = m_focusParent;
487 }
488 } else {
489 focusWidget = QApplication::focusWidget();
490 }
491 QApplication::sendEvent(focusWidget, event);
492}
493
494void InputController::postPendingEvent(GBAKey key) {
495 m_pendingEvents.insert(key);
496}
497
498void InputController::clearPendingEvent(GBAKey key) {
499 m_pendingEvents.remove(key);
500}
501
502bool InputController::hasPendingEvent(GBAKey key) const {
503 return m_pendingEvents.contains(key);
504}
505
506void InputController::suspendScreensaver() {
507#ifdef BUILD_SDL
508#if SDL_VERSION_ATLEAST(2, 0, 0)
509 GBASDLSuspendScreensaver(&s_sdlEvents);
510#endif
511#endif
512}
513
514void InputController::resumeScreensaver() {
515#ifdef BUILD_SDL
516#if SDL_VERSION_ATLEAST(2, 0, 0)
517 GBASDLResumeScreensaver(&s_sdlEvents);
518#endif
519#endif
520}
521
522void InputController::setScreensaverSuspendable(bool suspendable) {
523#ifdef BUILD_SDL
524#if SDL_VERSION_ATLEAST(2, 0, 0)
525 GBASDLSetScreensaverSuspendable(&s_sdlEvents, suspendable);
526#endif
527#endif
528}
529
530void InputController::stealFocus(QWidget* focus) {
531 m_focusParent = focus;
532}
533
534void InputController::releaseFocus(QWidget* focus) {
535 if (focus == m_focusParent) {
536 m_focusParent = m_topLevel;
537 }
538}