all repos — mgba @ fa92b4cd0e461ede2a70077c8acf87078a931d05

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