src/platform/qt/InputController.h (view raw)
1/* Copyright (c) 2013-2015 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#ifndef QGBA_INPUT_CONTROLLER_H
7#define QGBA_INPUT_CONTROLLER_H
8
9#include "GamepadAxisEvent.h"
10
11#include <QObject>
12#include <QSet>
13
14class QTimer;
15
16extern "C" {
17#include "gba/input.h"
18
19#ifdef BUILD_SDL
20#include "platform/sdl/sdl-events.h"
21#endif
22}
23
24namespace QGBA {
25
26class ConfigController;
27
28class InputController : public QObject {
29Q_OBJECT
30
31public:
32 static const uint32_t KEYBOARD = 0x51545F4B;
33
34 InputController(int playerId = 0, QObject* parent = nullptr);
35 ~InputController();
36
37 void setConfiguration(ConfigController* config);
38 void loadConfiguration(uint32_t type);
39 void loadProfile(uint32_t type, const QString& profile);
40 void saveConfiguration(uint32_t type = KEYBOARD);
41 void saveProfile(uint32_t type, const QString& profile);
42 const char* profileForType(uint32_t type);
43
44 bool allowOpposing() const { return m_allowOpposing; }
45 void setAllowOpposing(bool allowOpposing) { m_allowOpposing = allowOpposing; }
46
47 GBAKey mapKeyboard(int key) const;
48
49 void bindKey(uint32_t type, int key, GBAKey);
50
51 const GBAInputMap* map() const { return &m_inputMap; }
52
53#ifdef BUILD_SDL
54 static const int32_t AXIS_THRESHOLD = 0x3000;
55
56 int testSDLEvents();
57 QSet<int> activeGamepadButtons();
58 QSet<QPair<int, GamepadAxisEvent::Direction>> activeGamepadAxes();
59
60 void bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction, GBAKey);
61
62 QStringList connectedGamepads(uint32_t type) const;
63 int gamepad(uint32_t type) const { return m_sdlPlayer.joystickIndex; }
64 void setGamepad(uint32_t type, int index) { GBASDLPlayerChangeJoystick(&s_sdlEvents, &m_sdlPlayer, index); }
65 void setPreferredGamepad(uint32_t type, const QString& device);
66 GBARumble* rumble();
67#endif
68
69public slots:
70 void testGamepad();
71
72private:
73 void postPendingEvent(GBAKey);
74 void clearPendingEvent(GBAKey);
75 bool hasPendingEvent(GBAKey) const;
76
77 GBAInputMap m_inputMap;
78 ConfigController* m_config;
79 int m_playerId;
80 bool m_allowOpposing;
81
82#ifdef BUILD_SDL
83 static int s_sdlInited;
84 static GBASDLEvents s_sdlEvents;
85 GBASDLPlayer m_sdlPlayer;
86 bool m_playerAttached;
87#endif
88
89 QSet<int> m_activeButtons;
90 QSet<QPair<int, GamepadAxisEvent::Direction>> m_activeAxes;
91 QTimer* m_gamepadTimer;
92
93 QSet<GBAKey> m_pendingEvents;
94};
95
96}
97
98#endif