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 <QReadWriteLock>
14#include <QObject>
15#include <QSet>
16#include <QTimer>
17#include <QVector>
18
19#include <memory>
20
21#include <mgba/gba/interface.h>
22#include <mgba/internal/gba/input.h>
23
24#ifdef BUILD_SDL
25#include "platform/sdl/sdl-events.h"
26#endif
27
28
29#ifdef BUILD_QT_MULTIMEDIA
30#include "VideoDumper.h"
31#include <QCamera>
32#endif
33
34struct mRotationSource;
35struct mRumble;
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, int index);
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 QList<QPair<QByteArray, QString>> listCameras() const;
101
102 mRumble* rumble();
103 mRotationSource* rotationSource();
104 mImageSource* imageSource() { return &m_image; }
105 GBALuminanceSource* luminance() { return &m_lux; }
106
107signals:
108 void profileLoaded(const QString& profile);
109 void luminanceValueChanged(int value);
110
111public slots:
112 void testGamepad(int type);
113 void updateJoysticks();
114
115 // TODO: Move these to somewhere that makes sense
116 void suspendScreensaver();
117 void resumeScreensaver();
118 void setScreensaverSuspendable(bool);
119
120 void increaseLuminanceLevel();
121 void decreaseLuminanceLevel();
122 void setLuminanceLevel(int level);
123 void setLuminanceValue(uint8_t value);
124
125 void loadCamImage(const QString& path);
126 void setCamImage(const QImage& image);
127
128 void setCamera(const QByteArray& id);
129
130private slots:
131#ifdef BUILD_QT_MULTIMEDIA
132 void prepareCamSettings(QCamera::Status);
133#endif
134 void setupCam();
135 void teardownCam();
136
137private:
138 void postPendingEvent(GBAKey);
139 void clearPendingEvent(GBAKey);
140 bool hasPendingEvent(GBAKey) const;
141 void sendGamepadEvent(QEvent*);
142
143 struct InputControllerLux : GBALuminanceSource {
144 InputController* p;
145 uint8_t value;
146 } m_lux;
147 uint8_t m_luxValue;
148 int m_luxLevel;
149
150 struct InputControllerImage : mImageSource {
151 InputController* p;
152 QImage image;
153 QImage resizedImage;
154 bool outOfDate;
155 QMutex mutex;
156 int w, h;
157 } m_image;
158
159#ifdef BUILD_QT_MULTIMEDIA
160 std::unique_ptr<QCamera> m_camera;
161 VideoDumper m_videoDumper;
162#endif
163
164 mInputMap m_inputMap;
165 ConfigController* m_config = nullptr;
166 int m_playerId;
167 QWidget* m_topLevel;
168 QWidget* m_focusParent;
169
170#ifdef BUILD_SDL
171 static int s_sdlInited;
172 static mSDLEvents s_sdlEvents;
173 mSDLPlayer m_sdlPlayer{};
174 bool m_playerAttached = false;
175#endif
176
177 QVector<int> m_deadzones;
178
179 QSet<int> m_activeButtons;
180 QSet<QPair<int, GamepadAxisEvent::Direction>> m_activeAxes;
181 QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats;
182 QTimer m_gamepadTimer{nullptr};
183
184 QSet<GBAKey> m_pendingEvents;
185 QReadWriteLock m_eventsLock;
186};
187
188}