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