all repos — mgba @ 8aa9c4503a28a89541d891140bfff7218bc2701a

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