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
11#include <QObject>
12#include <QSet>
13#include <QVector>
14
15class QTimer;
16
17extern "C" {
18#include "gba/input.h"
19
20#ifdef BUILD_SDL
21#include "platform/sdl/sdl-events.h"
22#endif
23}
24
25struct mRotationSource;
26struct mRumble;
27
28namespace QGBA {
29
30class ConfigController;
31
32class InputController : public QObject {
33Q_OBJECT
34
35public:
36 static const uint32_t KEYBOARD = 0x51545F4B;
37
38 InputController(int playerId = 0, QWidget* topLevel = nullptr, QObject* parent = nullptr);
39 ~InputController();
40
41 void setConfiguration(ConfigController* config);
42 void saveConfiguration();
43 void loadConfiguration(uint32_t type);
44 void loadProfile(uint32_t type, const QString& profile);
45 void saveConfiguration(uint32_t type);
46 void saveProfile(uint32_t type, const QString& profile);
47 const char* profileForType(uint32_t type);
48
49 bool allowOpposing() const { return m_allowOpposing; }
50 void setAllowOpposing(bool allowOpposing) { m_allowOpposing = allowOpposing; }
51
52 GBAKey mapKeyboard(int key) const;
53
54 void bindKey(uint32_t type, int key, GBAKey);
55
56 const mInputMap* map() const { return &m_inputMap; }
57
58 void updateJoysticks();
59 int pollEvents();
60
61 static const int32_t AXIS_THRESHOLD = 0x3000;
62 QSet<int> activeGamepadButtons(int type);
63 QSet<QPair<int, GamepadAxisEvent::Direction>> activeGamepadAxes(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 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
94 // TODO: Move these to somewhere that makes sense
95 void suspendScreensaver();
96 void resumeScreensaver();
97 void setScreensaverSuspendable(bool);
98
99private:
100 void postPendingEvent(GBAKey);
101 void clearPendingEvent(GBAKey);
102 bool hasPendingEvent(GBAKey) const;
103 void sendGamepadEvent(QEvent*);
104
105 mInputMap m_inputMap;
106 ConfigController* m_config;
107 int m_playerId;
108 bool m_allowOpposing;
109 QWidget* m_topLevel;
110 QWidget* m_focusParent;
111
112#ifdef BUILD_SDL
113 static int s_sdlInited;
114 static mSDLEvents s_sdlEvents;
115 mSDLPlayer m_sdlPlayer;
116 bool m_playerAttached;
117#endif
118
119 QVector<int> m_deadzones;
120
121 QSet<int> m_activeButtons;
122 QSet<QPair<int, GamepadAxisEvent::Direction>> m_activeAxes;
123 QTimer* m_gamepadTimer;
124
125 QSet<GBAKey> m_pendingEvents;
126};
127
128}
129
130#endif