all repos — mgba @ b97be4b461101be20a4522ee939fa277b53fd0bf

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