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