src/platform/qt/InputController.cpp (view raw)
1#include "InputController.h"
2
3#include <Qt>
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::loadDefaultConfiguration(const Configuration* config) {
41 loadConfiguration(KEYBOARD, config);
42#ifdef BUILD_SDL
43 loadConfiguration(SDL_BINDING_BUTTON, config);
44#endif
45}
46
47void InputController::loadConfiguration(uint32_t type, const Configuration* config) {
48 GBAInputMapLoad(&m_inputMap, type, config);
49}
50
51GBAKey InputController::mapKeyboard(int key) const {
52 return GBAInputMapKey(&m_inputMap, KEYBOARD, key);
53}
54
55#ifdef BUILD_SDL
56int InputController::testSDLEvents() {
57 SDL_Joystick* joystick = m_sdlEvents.joystick;
58 SDL_JoystickUpdate();
59 int numButtons = SDL_JoystickNumButtons(joystick);
60 int activeButtons = 0;
61 int i;
62 for (i = 0; i < numButtons; ++i) {
63 GBAKey key = GBAInputMapKey(&m_inputMap, SDL_BINDING_BUTTON, i);
64 if (key == GBA_KEY_NONE) {
65 continue;
66 }
67 if (SDL_JoystickGetButton(joystick, i)) {
68 activeButtons |= 1 << key;
69 }
70 }
71 int numHats = SDL_JoystickNumHats(joystick);
72 for (i = 0; i < numHats; ++i) {
73 int hat = SDL_JoystickGetHat(joystick, i);
74 if (hat & SDL_HAT_UP) {
75 activeButtons |= 1 << GBA_KEY_UP;
76 }
77 if (hat & SDL_HAT_LEFT) {
78 activeButtons |= 1 << GBA_KEY_LEFT;
79 }
80 if (hat & SDL_HAT_DOWN) {
81 activeButtons |= 1 << GBA_KEY_DOWN;
82 }
83 if (hat & SDL_HAT_RIGHT) {
84 activeButtons |= 1 << GBA_KEY_RIGHT;
85 }
86 }
87 return activeButtons;
88}
89#endif