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