src/platform/qt/InputProfile.cpp (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#include "InputProfile.h"
7
8#include "InputController.h"
9
10#include <QRegExp>
11
12using namespace QGBA;
13
14const InputProfile InputProfile::s_defaultMaps[] = {
15 {
16 "XInput Controller #\\d+", // XInput (Windows)
17 (int[GBA_KEY_MAX]) {
18 /*keyA */ 11,
19 /*keyB */ 10,
20 /*keySelect */ 5,
21 /*keyStart */ 4,
22 /*keyRight */ 3,
23 /*keyLeft */ 2,
24 /*keyUp */ 0,
25 /*keyDown */ 1,
26 /*keyR */ 9,
27 /*keyL */ 8
28 },
29 (ShortcutButton[]) {
30 {"loadState", 12},
31 {"saveState", 13},
32 {}
33 },
34 (ShortcutAxis[]) {
35 {"holdFastForward", GamepadAxisEvent::Direction::POSITIVE, 5},
36 {"holdRewind", GamepadAxisEvent::Direction::POSITIVE, 4},
37 {}
38 }
39 },
40 {
41 "Controller", // The Xbox 360 controller drivers on OS X are vague...
42 (int[GBA_KEY_MAX]) {
43 /*keyA */ 1,
44 /*keyB */ 0,
45 /*keySelect */ 9,
46 /*keyStart */ 8,
47 /*keyRight */ 14,
48 /*keyLeft */ 13,
49 /*keyUp */ 11,
50 /*keyDown */ 12,
51 /*keyR */ 5,
52 /*keyL */ 4
53 },
54 (ShortcutButton[]) {
55 {"loadState", 2},
56 {"saveState", 3},
57 {}
58 },
59 (ShortcutAxis[]) {
60 {"holdFastForward", GamepadAxisEvent::Direction::POSITIVE, 5},
61 {"holdRewind", GamepadAxisEvent::Direction::POSITIVE, 2},
62 {}
63 }
64 },
65 {
66 "PLAYSTATION\\(R\\)3 Controller", // DualShock 3 (OS X)
67 (int[GBA_KEY_MAX]) {
68 /*keyA */ 13,
69 /*keyB */ 14,
70 /*keySelect */ 0,
71 /*keyStart */ 3,
72 /*keyRight */ 5,
73 /*keyLeft */ 7,
74 /*keyUp */ 4,
75 /*keyDown */ 6,
76 /*keyR */ 11,
77 /*keyL */ 10
78 },
79 (ShortcutButton[]) {
80 {"loadState", 15},
81 {"saveState", 12},
82 {"holdFastForward", 9},
83 {"holdRewind", 8},
84 {}
85 }
86 },
87 {
88 "Wiimote \\(..-..-..-..-..-..\\)", // WJoy (OS X)
89 (int[GBA_KEY_MAX]) {
90 /*keyA */ 15,
91 /*keyB */ 16,
92 /*keySelect */ 7,
93 /*keyStart */ 6,
94 /*keyRight */ 14,
95 /*keyLeft */ 13,
96 /*keyUp */ 11,
97 /*keyDown */ 12,
98 /*keyR */ 20,
99 /*keyL */ 19
100 },
101 (ShortcutButton[]) {
102 {"loadState", 18},
103 {"saveState", 17},
104 {"holdFastForward", 22},
105 {"holdRewind", 21},
106 {}
107 }
108 },
109};
110
111constexpr InputProfile::InputProfile(const char* name,
112 int keys[GBA_KEY_MAX],
113 const ShortcutButton* shortcutButtons,
114 const ShortcutAxis* shortcutAxes,
115 AxisValue axes[GBA_KEY_MAX],
116 const struct Coord& tiltAxis,
117 const struct Coord& gyroAxis,
118 float gyroSensitivity)
119 : m_profileName(name)
120 , m_keys {
121 keys[GBA_KEY_A],
122 keys[GBA_KEY_B],
123 keys[GBA_KEY_SELECT],
124 keys[GBA_KEY_START],
125 keys[GBA_KEY_RIGHT],
126 keys[GBA_KEY_LEFT],
127 keys[GBA_KEY_UP],
128 keys[GBA_KEY_DOWN],
129 keys[GBA_KEY_R],
130 keys[GBA_KEY_L]
131 }
132 , m_shortcutButtons(shortcutButtons)
133 , m_shortcutAxes(shortcutAxes)
134 , m_axes {
135 axes[GBA_KEY_A],
136 axes[GBA_KEY_B],
137 axes[GBA_KEY_SELECT],
138 axes[GBA_KEY_START],
139 axes[GBA_KEY_RIGHT],
140 axes[GBA_KEY_LEFT],
141 axes[GBA_KEY_UP],
142 axes[GBA_KEY_DOWN],
143 axes[GBA_KEY_R],
144 axes[GBA_KEY_L]
145 }
146 , m_tiltAxis(tiltAxis)
147 , m_gyroAxis(gyroAxis)
148 , m_gyroSensitivity(gyroSensitivity)
149{
150}
151
152const InputProfile* InputProfile::findProfile(const QString& name) {
153 for (size_t i = 0; i < sizeof(s_defaultMaps) / sizeof(*s_defaultMaps); ++i) {
154 QRegExp re(s_defaultMaps[i].m_profileName);
155 if (re.exactMatch(name)) {
156 return &s_defaultMaps[i];
157 }
158 }
159 return nullptr;
160}
161
162void InputProfile::apply(InputController* controller) const {
163 for (size_t i = 0; i < GBA_KEY_MAX; ++i) {
164#ifdef BUILD_SDL
165 controller->bindKey(SDL_BINDING_BUTTON, m_keys[i], static_cast<GBAKey>(i));
166 controller->bindAxis(SDL_BINDING_BUTTON, m_axes[i].axis, m_axes[i].direction, static_cast<GBAKey>(i));
167#endif
168 }
169 controller->registerTiltAxisX(m_tiltAxis.x);
170 controller->registerTiltAxisY(m_tiltAxis.y);
171 controller->registerGyroAxisX(m_gyroAxis.x);
172 controller->registerGyroAxisY(m_gyroAxis.y);
173 controller->setGyroSensitivity(m_gyroSensitivity);
174}
175
176bool InputProfile::lookupShortcutButton(const QString& shortcutName, int* button) const {
177 for (size_t i = 0; m_shortcutButtons[i].shortcut; ++i) {
178 const ShortcutButton& shortcut = m_shortcutButtons[i];
179 if (QLatin1String(shortcut.shortcut) == shortcutName) {
180 *button = shortcut.button;
181 return true;
182 }
183 }
184 return false;
185}
186
187bool InputProfile::lookupShortcutAxis(const QString& shortcutName, int* axis, GamepadAxisEvent::Direction* direction) const {
188 for (size_t i = 0; m_shortcutAxes[i].shortcut; ++i) {
189 const ShortcutAxis& shortcut = m_shortcutAxes[i];
190 if (QLatin1String(shortcut.shortcut) == shortcutName) {
191 *axis = shortcut.axis;
192 *direction = shortcut.direction;
193 return true;
194 }
195 }
196 return false;
197}