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