all repos — mgba @ 78acb25afce980b04a627ba4d3bb9aa9a930fa0c

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