all repos — mgba @ 7cdcdbd7b4631485e6c1acd00535e8f3c1151293

mGBA Game Boy Advance Emulator

src/platform/qt/InputController.cpp (view raw)

 1#include "InputController.h"
 2
 3#include "ConfigController.h"
 4
 5extern "C" {
 6#include "util/configuration.h"
 7}
 8
 9using namespace QGBA;
10
11InputController::InputController() {
12	GBAInputMapInit(&m_inputMap);
13
14#ifdef BUILD_SDL
15	m_sdlEvents.bindings = &m_inputMap;
16	GBASDLInitEvents(&m_sdlEvents);
17	SDL_JoystickEventState(SDL_QUERY);
18#endif
19
20	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_X, GBA_KEY_A);
21	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Z, GBA_KEY_B);
22	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_A, GBA_KEY_L);
23	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_S, GBA_KEY_R);
24	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Return, GBA_KEY_START);
25	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Backspace, GBA_KEY_SELECT);
26	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Up, GBA_KEY_UP);
27	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Down, GBA_KEY_DOWN);
28	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Left, GBA_KEY_LEFT);
29	GBAInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Right, GBA_KEY_RIGHT);
30}
31
32InputController::~InputController() {
33	GBAInputMapDeinit(&m_inputMap);
34
35#ifdef BUILD_SDL
36	GBASDLDeinitEvents(&m_sdlEvents);
37#endif
38}
39
40void InputController::setConfiguration(ConfigController* config) {
41	m_config = config;
42	loadConfiguration(KEYBOARD);
43#ifdef BUILD_SDL
44	loadConfiguration(SDL_BINDING_BUTTON);
45#endif
46}
47
48void InputController::loadConfiguration(uint32_t type) {
49	GBAInputMapLoad(&m_inputMap, type, m_config->configuration());
50}
51
52void InputController::saveConfiguration(uint32_t type) {
53	GBAInputMapSave(&m_inputMap, type, m_config->configuration());
54	m_config->write();
55}
56
57GBAKey InputController::mapKeyboard(int key) const {
58	return GBAInputMapKey(&m_inputMap, KEYBOARD, key);
59}
60
61void InputController::bindKey(uint32_t type, int key, GBAKey gbaKey) {
62	return GBAInputBindKey(&m_inputMap, type, key, gbaKey);
63}
64
65#ifdef BUILD_SDL
66int InputController::testSDLEvents() {
67	SDL_Joystick* joystick = m_sdlEvents.joystick;
68	SDL_JoystickUpdate();
69	int numButtons = SDL_JoystickNumButtons(joystick);
70	int activeButtons = 0;
71	int i;
72	for (i = 0; i < numButtons; ++i) {
73		GBAKey key = GBAInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
74		if (key == GBA_KEY_NONE) {
75			continue;
76		}
77		if (SDL_JoystickGetButton(joystick, i)) {
78			activeButtons |= 1 << key;
79		}
80	}
81	int numHats = SDL_JoystickNumHats(joystick);
82	for (i = 0; i < numHats; ++i) {
83		int hat = SDL_JoystickGetHat(joystick, i);
84		if (hat & SDL_HAT_UP) {
85			activeButtons |= 1 << GBA_KEY_UP;
86		}
87		if (hat & SDL_HAT_LEFT) {
88			activeButtons |= 1 << GBA_KEY_LEFT;
89		}
90		if (hat & SDL_HAT_DOWN) {
91			activeButtons |= 1 << GBA_KEY_DOWN;
92		}
93		if (hat & SDL_HAT_RIGHT) {
94			activeButtons |= 1 << GBA_KEY_RIGHT;
95		}
96	}
97	return activeButtons;
98}
99#endif