all repos — mgba @ 05e04ba76a396c4e54933951bf72f90097865438

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