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#include "GamepadHatEvent.h"
11
12#include <QObject>
13#include <QSet>
14#include <QTimer>
15#include <QVector>
16
17#include <mgba/gba/interface.h>
18#include <mgba/internal/gba/input.h>
19
20#ifdef BUILD_SDL
21#include "platform/sdl/sdl-events.h"
22#endif
23
24struct mRotationSource;
25struct mRumble;
26
27namespace QGBA {
28
29class ConfigController;
30
31class InputController : public QObject {
32Q_OBJECT
33
34public:
35 static const uint32_t KEYBOARD = 0x51545F4B;
36
37 InputController(int playerId = 0, QWidget* topLevel = nullptr, QObject* parent = nullptr);
38 ~InputController();
39
40 void setConfiguration(ConfigController* config);
41 void saveConfiguration();
42 void loadConfiguration(uint32_t type);
43 void loadProfile(uint32_t type, const QString& profile);
44 void saveConfiguration(uint32_t type);
45 void saveProfile(uint32_t type, const QString& profile);
46 const char* profileForType(uint32_t type);
47
48 GBAKey mapKeyboard(int key) const;
49
50 void bindKey(uint32_t type, int key, GBAKey);
51
52 const mInputMap* map() const { return &m_inputMap; }
53
54 int pollEvents();
55
56 static const int32_t AXIS_THRESHOLD = 0x3000;
57 QSet<int> activeGamepadButtons(int type);
58 QSet<QPair<int, GamepadAxisEvent::Direction>> activeGamepadAxes(int type);
59 QSet<QPair<int, GamepadHatEvent::Direction>> activeGamepadHats(int type);
60 void recalibrateAxes();
61
62 void bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction, GBAKey);
63 void unbindAllAxes(uint32_t type);
64
65 void bindHat(uint32_t type, int hat, GamepadHatEvent::Direction, GBAKey);
66
67 QStringList connectedGamepads(uint32_t type) const;
68 int gamepad(uint32_t type) const;
69 void setGamepad(uint32_t type, int index);
70 void setPreferredGamepad(uint32_t type, const QString& device);
71
72 void registerTiltAxisX(int axis);
73 void registerTiltAxisY(int axis);
74 void registerGyroAxisX(int axis);
75 void registerGyroAxisY(int axis);
76
77 float gyroSensitivity() const;
78 void setGyroSensitivity(float sensitivity);
79
80 void stealFocus(QWidget* focus);
81 void releaseFocus(QWidget* focus);
82
83 mRumble* rumble();
84 mRotationSource* rotationSource();
85 GBALuminanceSource* luminance() { return &m_lux; }
86
87signals:
88 void profileLoaded(const QString& profile);
89 void luminanceValueChanged(int value);
90
91public slots:
92 void testGamepad(int type);
93 void updateJoysticks();
94
95 // TODO: Move these to somewhere that makes sense
96 void suspendScreensaver();
97 void resumeScreensaver();
98 void setScreensaverSuspendable(bool);
99
100 void increaseLuminanceLevel();
101 void decreaseLuminanceLevel();
102 void setLuminanceLevel(int level);
103 void setLuminanceValue(uint8_t value);
104
105private:
106 void postPendingEvent(GBAKey);
107 void clearPendingEvent(GBAKey);
108 bool hasPendingEvent(GBAKey) const;
109 void sendGamepadEvent(QEvent*);
110
111 struct InputControllerLux : GBALuminanceSource {
112 InputController* p;
113 uint8_t value;
114 } m_lux;
115 uint8_t m_luxValue;
116 int m_luxLevel;
117
118 mInputMap m_inputMap;
119 ConfigController* m_config = nullptr;
120 int m_playerId;
121 QWidget* m_topLevel;
122 QWidget* m_focusParent;
123
124#ifdef BUILD_SDL
125 static int s_sdlInited;
126 static mSDLEvents s_sdlEvents;
127 mSDLPlayer m_sdlPlayer{};
128 bool m_playerAttached = false;
129#endif
130
131 QVector<int> m_deadzones;
132
133 QSet<int> m_activeButtons;
134 QSet<QPair<int, GamepadAxisEvent::Direction>> m_activeAxes;
135 QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats;
136 QTimer m_gamepadTimer{nullptr};
137
138 QSet<GBAKey> m_pendingEvents;
139};
140
141}
142
143#endif