all repos — mgba @ 6a188289c209a3c905dc5209640904a91165c879

mGBA Game Boy Advance Emulator

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
 17#include <mgba/core/interface.h>
 18#include <mgba-util/configuration.h>
 19
 20using namespace QGBA;
 21
 22#ifdef BUILD_SDL
 23int InputController::s_sdlInited = 0;
 24mSDLEvents InputController::s_sdlEvents;
 25#endif
 26
 27InputController::InputController(int playerId, QWidget* topLevel, 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	, m_topLevel(topLevel)
 37	, m_focusParent(topLevel)
 38{
 39	mInputMapInit(&m_inputMap, &GBAInputInfo);
 40
 41#ifdef BUILD_SDL
 42	if (s_sdlInited == 0) {
 43		mSDLInitEvents(&s_sdlEvents);
 44	}
 45	++s_sdlInited;
 46	m_sdlPlayer.bindings = &m_inputMap;
 47	mSDLInitBindingsGBA(&m_inputMap);
 48	updateJoysticks();
 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	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_X, GBA_KEY_A);
 61	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Z, GBA_KEY_B);
 62	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_A, GBA_KEY_L);
 63	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_S, GBA_KEY_R);
 64	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Return, GBA_KEY_START);
 65	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Backspace, GBA_KEY_SELECT);
 66	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Up, GBA_KEY_UP);
 67	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Down, GBA_KEY_DOWN);
 68	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Left, GBA_KEY_LEFT);
 69	mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Right, GBA_KEY_RIGHT);
 70}
 71
 72InputController::~InputController() {
 73	mInputMapDeinit(&m_inputMap);
 74
 75#ifdef BUILD_SDL
 76	if (m_playerAttached) {
 77		mSDLDetachPlayer(&s_sdlEvents, &m_sdlPlayer);
 78	}
 79
 80	--s_sdlInited;
 81	if (s_sdlInited == 0) {
 82		mSDLDeinitEvents(&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	mSDLEventsLoadConfig(&s_sdlEvents, config->input());
 93	if (!m_playerAttached) {
 94		m_playerAttached = mSDLAttachPlayer(&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	mInputMapLoad(&m_inputMap, type, m_config->input());
103#ifdef BUILD_SDL
104	if (m_playerAttached) {
105		mSDLPlayerLoadConfig(&m_sdlPlayer, m_config->input());
106	}
107#endif
108}
109
110void InputController::loadProfile(uint32_t type, const QString& profile) {
111	bool loaded = mInputProfileLoad(&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		mSDLPlayerSaveConfig(&m_sdlPlayer, m_config->input());
129	}
130	m_config->write();
131#endif
132}
133
134void InputController::saveConfiguration(uint32_t type) {
135	mInputMapSave(&m_inputMap, type, m_config->input());
136	m_config->write();
137}
138
139void InputController::saveProfile(uint32_t type, const QString& profile) {
140	mInputProfileSave(&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->joystick);
150#else
151		return SDL_JoystickName(SDL_JoystickIndex(m_sdlPlayer.joystick->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 < SDL_JoystickListSize(&s_sdlEvents.joysticks); ++i) {
165			const char* name;
166#if SDL_VERSION_ATLEAST(2, 0, 0)
167			name = SDL_JoystickName(SDL_JoystickListGetPointer(&s_sdlEvents.joysticks, i)->joystick);
168#else
169			name = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&s_sdlEvents.joysticks, i)->joystick));
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.joystick ? m_sdlPlayer.joystick->index : 0;
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		mSDLPlayerChangeJoystick(&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	mInputSetPreferredDevice(m_config->input(), "gba", type, m_playerId, device.toUtf8().constData());
206}
207
208mRumble* 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
219mRotationSource* 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 static_cast<GBAKey>(mInputMapKey(&m_inputMap, KEYBOARD, key));
279}
280
281void InputController::bindKey(uint32_t type, int key, GBAKey gbaKey) {
282	return mInputBindKey(&m_inputMap, type, key, gbaKey);
283}
284
285void InputController::updateJoysticks() {
286#ifdef BUILD_SDL
287	mSDLUpdateJoysticks(&s_sdlEvents);
288#endif
289}
290
291int InputController::pollEvents() {
292	int activeButtons = 0;
293#ifdef BUILD_SDL
294	if (m_playerAttached && m_sdlPlayer.joystick) {
295		SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
296		SDL_JoystickUpdate();
297		int numButtons = SDL_JoystickNumButtons(joystick);
298		int i;
299		for (i = 0; i < numButtons; ++i) {
300			GBAKey key = static_cast<GBAKey>(mInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i));
301			if (key == GBA_KEY_NONE) {
302				continue;
303			}
304			if (hasPendingEvent(key)) {
305				continue;
306			}
307			if (SDL_JoystickGetButton(joystick, i)) {
308				activeButtons |= 1 << key;
309			}
310		}
311		int numHats = SDL_JoystickNumHats(joystick);
312		for (i = 0; i < numHats; ++i) {
313			int hat = SDL_JoystickGetHat(joystick, i);
314			if (hat & SDL_HAT_UP) {
315				activeButtons |= 1 << GBA_KEY_UP;
316			}
317			if (hat & SDL_HAT_LEFT) {
318				activeButtons |= 1 << GBA_KEY_LEFT;
319			}
320			if (hat & SDL_HAT_DOWN) {
321				activeButtons |= 1 << GBA_KEY_DOWN;
322			}
323			if (hat & SDL_HAT_RIGHT) {
324				activeButtons |= 1 << GBA_KEY_RIGHT;
325			}
326		}
327
328		int numAxes = SDL_JoystickNumAxes(joystick);
329		for (i = 0; i < numAxes; ++i) {
330			int value = SDL_JoystickGetAxis(joystick, i);
331
332			enum GBAKey key = static_cast<GBAKey>(mInputMapAxis(&m_inputMap, SDL_BINDING_BUTTON, i, value));
333			if (key != GBA_KEY_NONE) {
334				activeButtons |= 1 << key;
335			}
336		}
337	}
338#endif
339	return activeButtons;
340}
341
342QSet<int> InputController::activeGamepadButtons(int type) {
343	QSet<int> activeButtons;
344#ifdef BUILD_SDL
345	if (m_playerAttached && type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
346		SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
347		SDL_JoystickUpdate();
348		int numButtons = SDL_JoystickNumButtons(joystick);
349		int i;
350		for (i = 0; i < numButtons; ++i) {
351			if (SDL_JoystickGetButton(joystick, i)) {
352				activeButtons.insert(i);
353			}
354		}
355	}
356#endif
357	return activeButtons;
358}
359
360void InputController::recalibrateAxes() {
361#ifdef BUILD_SDL
362	if (m_playerAttached && m_sdlPlayer.joystick) {
363		SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
364		SDL_JoystickUpdate();
365		int numAxes = SDL_JoystickNumAxes(joystick);
366		if (numAxes < 1) {
367			return;
368		}
369		m_deadzones.resize(numAxes);
370		int i;
371		for (i = 0; i < numAxes; ++i) {
372			m_deadzones[i] = SDL_JoystickGetAxis(joystick, i);
373		}
374	}
375#endif
376}
377
378QSet<QPair<int, GamepadAxisEvent::Direction>> InputController::activeGamepadAxes(int type) {
379	QSet<QPair<int, GamepadAxisEvent::Direction>> activeAxes;
380#ifdef BUILD_SDL
381	if (m_playerAttached && type == SDL_BINDING_BUTTON && m_sdlPlayer.joystick) {
382		SDL_Joystick* joystick = m_sdlPlayer.joystick->joystick;
383		SDL_JoystickUpdate();
384		int numAxes = SDL_JoystickNumAxes(joystick);
385		if (numAxes < 1) {
386			return activeAxes;
387		}
388		m_deadzones.resize(numAxes);
389		int i;
390		for (i = 0; i < numAxes; ++i) {
391			int32_t axis = SDL_JoystickGetAxis(joystick, i);
392			axis -= m_deadzones[i];
393			if (axis >= AXIS_THRESHOLD || axis <= -AXIS_THRESHOLD) {
394				activeAxes.insert(qMakePair(i, axis > 0 ? GamepadAxisEvent::POSITIVE : GamepadAxisEvent::NEGATIVE));
395			}
396		}
397	}
398#endif
399	return activeAxes;
400}
401
402void InputController::bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction direction, GBAKey key) {
403	const mInputAxis* old = mInputQueryAxis(&m_inputMap, type, axis);
404	mInputAxis description = { GBA_KEY_NONE, GBA_KEY_NONE, -AXIS_THRESHOLD, AXIS_THRESHOLD };
405	if (old) {
406		description = *old;
407	}
408	int deadzone = 0;
409	if (axis > 0 && m_deadzones.size() > axis) {
410		deadzone = m_deadzones[axis];
411	}
412	switch (direction) {
413	case GamepadAxisEvent::NEGATIVE:
414		description.lowDirection = key;
415
416		description.deadLow = deadzone - AXIS_THRESHOLD;
417		break;
418	case GamepadAxisEvent::POSITIVE:
419		description.highDirection = key;
420		description.deadHigh = deadzone + AXIS_THRESHOLD;
421		break;
422	default:
423		return;
424	}
425	mInputBindAxis(&m_inputMap, type, axis, &description);
426}
427
428void InputController::unbindAllAxes(uint32_t type) {
429	mInputUnbindAllAxes(&m_inputMap, type);
430}
431
432void InputController::testGamepad(int type) {
433	auto activeAxes = activeGamepadAxes(type);
434	auto oldAxes = m_activeAxes;
435	m_activeAxes = activeAxes;
436
437	auto activeButtons = activeGamepadButtons(type);
438	auto oldButtons = m_activeButtons;
439	m_activeButtons = activeButtons;
440
441	if (!QApplication::focusWidget()) {
442		return;
443	}
444
445	activeAxes.subtract(oldAxes);
446	oldAxes.subtract(m_activeAxes);
447
448	for (auto& axis : m_activeAxes) {
449		bool newlyAboveThreshold = activeAxes.contains(axis);
450		if (newlyAboveThreshold) {
451			GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
452			postPendingEvent(event->gbaKey());
453			sendGamepadEvent(event);
454			if (!event->isAccepted()) {
455				clearPendingEvent(event->gbaKey());
456			}
457		}
458	}
459	for (auto axis : oldAxes) {
460		GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
461		clearPendingEvent(event->gbaKey());
462		sendGamepadEvent(event);
463	}
464
465	if (!QApplication::focusWidget()) {
466		return;
467	}
468
469	activeButtons.subtract(oldButtons);
470	oldButtons.subtract(m_activeButtons);
471
472	for (int button : activeButtons) {
473		GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
474		postPendingEvent(event->gbaKey());
475		sendGamepadEvent(event);
476		if (!event->isAccepted()) {
477			clearPendingEvent(event->gbaKey());
478		}
479	}
480	for (int button : oldButtons) {
481		GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
482		clearPendingEvent(event->gbaKey());
483		sendGamepadEvent(event);
484	}
485}
486
487void InputController::sendGamepadEvent(QEvent* event) {
488	QWidget* focusWidget = nullptr;
489	if (m_focusParent) {
490		focusWidget = m_focusParent->focusWidget();
491		if (!focusWidget) {
492			focusWidget = m_focusParent;
493		}
494	} else {
495		focusWidget = QApplication::focusWidget();
496	}
497	QApplication::sendEvent(focusWidget, event);
498}
499
500void InputController::postPendingEvent(GBAKey key) {
501	m_pendingEvents.insert(key);
502}
503
504void InputController::clearPendingEvent(GBAKey key) {
505	m_pendingEvents.remove(key);
506}
507
508bool InputController::hasPendingEvent(GBAKey key) const {
509	return m_pendingEvents.contains(key);
510}
511
512void InputController::suspendScreensaver() {
513#ifdef BUILD_SDL
514#if SDL_VERSION_ATLEAST(2, 0, 0)
515	mSDLSuspendScreensaver(&s_sdlEvents);
516#endif
517#endif
518}
519
520void InputController::resumeScreensaver() {
521#ifdef BUILD_SDL
522#if SDL_VERSION_ATLEAST(2, 0, 0)
523	mSDLResumeScreensaver(&s_sdlEvents);
524#endif
525#endif
526}
527
528void InputController::setScreensaverSuspendable(bool suspendable) {
529#ifdef BUILD_SDL
530#if SDL_VERSION_ATLEAST(2, 0, 0)
531	mSDLSetScreensaverSuspendable(&s_sdlEvents, suspendable);
532#endif
533#endif
534}
535
536void InputController::stealFocus(QWidget* focus) {
537	m_focusParent = focus;
538}
539
540void InputController::releaseFocus(QWidget* focus) {
541	if (focus == m_focusParent) {
542		m_focusParent = m_topLevel;
543	}
544}