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 <QVector>
15
16class QTimer;
17
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 bool allowOpposing() const { return m_allowOpposing; }
49 void setAllowOpposing(bool allowOpposing) { m_allowOpposing = allowOpposing; }
50
51 GBAKey mapKeyboard(int key) const;
52
53 void bindKey(uint32_t type, int key, GBAKey);
54
55 const mInputMap* map() const { return &m_inputMap; }
56
57 void updateJoysticks();
58 int pollEvents();
59
60 static const int32_t AXIS_THRESHOLD = 0x3000;
61 QSet<int> activeGamepadButtons(int type);
62 QSet<QPair<int, GamepadAxisEvent::Direction>> activeGamepadAxes(int type);
63 QSet<QPair<int, GamepadHatEvent::Direction>> activeGamepadHats(int type);
64 void recalibrateAxes();
65
66 void bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction, GBAKey);
67 void unbindAllAxes(uint32_t type);
68
69 void bindHat(uint32_t type, int hat, GamepadHatEvent::Direction, GBAKey);
70
71 QStringList connectedGamepads(uint32_t type) const;
72 int gamepad(uint32_t type) const;
73 void setGamepad(uint32_t type, int index);
74 void setPreferredGamepad(uint32_t type, const QString& device);
75
76 void registerTiltAxisX(int axis);
77 void registerTiltAxisY(int axis);
78 void registerGyroAxisX(int axis);
79 void registerGyroAxisY(int axis);
80
81 float gyroSensitivity() const;
82 void setGyroSensitivity(float sensitivity);
83
84 void stealFocus(QWidget* focus);
85 void releaseFocus(QWidget* focus);
86
87 mRumble* rumble();
88 mRotationSource* rotationSource();
89
90signals:
91 void profileLoaded(const QString& profile);
92
93public slots:
94 void testGamepad(int type);
95
96 // TODO: Move these to somewhere that makes sense
97 void suspendScreensaver();
98 void resumeScreensaver();
99 void setScreensaverSuspendable(bool);
100
101private:
102 void postPendingEvent(GBAKey);
103 void clearPendingEvent(GBAKey);
104 bool hasPendingEvent(GBAKey) const;
105 void sendGamepadEvent(QEvent*);
106
107 mInputMap m_inputMap;
108 ConfigController* m_config;
109 int m_playerId;
110 bool m_allowOpposing;
111 QWidget* m_topLevel;
112 QWidget* m_focusParent;
113
114#ifdef BUILD_SDL
115 static int s_sdlInited;
116 static mSDLEvents s_sdlEvents;
117 mSDLPlayer m_sdlPlayer;
118 bool m_playerAttached;
119#endif
120
121 QVector<int> m_deadzones;
122
123 QSet<int> m_activeButtons;
124 QSet<QPair<int, GamepadAxisEvent::Direction>> m_activeAxes;
125 QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats;
126 QTimer* m_gamepadTimer;
127
128 QSet<GBAKey> m_pendingEvents;
129};
130
131}
132
133#endif