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