all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

mGBA Game Boy Advance Emulator

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		{
 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		{
 30		/*loadState       */ 12,
 31		/*saveState       */ 13,
 32		/*holdFastForward */ -1,
 33		/*holdRewind      */ -1,
 34		},
 35		{
 36		/*loadState       */ {GamepadAxisEvent::Direction::NEUTRAL, -1},
 37		/*saveState       */ {GamepadAxisEvent::Direction::NEUTRAL, -1},
 38		/*holdFastForward */ {GamepadAxisEvent::Direction::POSITIVE, 5},
 39		/*holdRewind      */ {GamepadAxisEvent::Direction::POSITIVE, 4},
 40		}
 41	},
 42	{
 43		"(Microsoft X-Box 360 pad|Xbox Gamepad \\(userspace driver\\))", // Linux
 44		{
 45		/*keyA      */  1,
 46		/*keyB      */  0,
 47		/*keySelect */  6,
 48		/*keyStart  */  7,
 49		/*keyRight  */ -1,
 50		/*keyLeft   */ -1,
 51		/*keyUp     */ -1,
 52		/*keyDown   */ -1,
 53		/*keyR      */  5,
 54		/*keyL      */  4
 55		},
 56		{
 57		/*loadState       */ 2,
 58		/*saveState       */ 3,
 59		/*holdFastForward */ -1,
 60		/*holdRewind      */ -1,
 61		},
 62		{
 63		/*loadState       */ {GamepadAxisEvent::Direction::NEUTRAL, -1},
 64		/*saveState       */ {GamepadAxisEvent::Direction::NEUTRAL, -1},
 65		/*holdFastForward */ {GamepadAxisEvent::Direction::POSITIVE, 5},
 66		/*holdRewind      */ {GamepadAxisEvent::Direction::POSITIVE, 2},
 67		}
 68	},
 69	{
 70		"Xbox 360 Wired Controller", // OS X
 71		{
 72		/*keyA      */  1,
 73		/*keyB      */  0,
 74		/*keySelect */  9,
 75		/*keyStart  */  8,
 76		/*keyRight  */ 14,
 77		/*keyLeft   */ 13,
 78		/*keyUp     */ 11,
 79		/*keyDown   */ 12,
 80		/*keyR      */  5,
 81		/*keyL      */  4
 82		},
 83		{
 84		/*loadState       */ 2,
 85		/*saveState       */ 3,
 86		/*holdFastForward */ -1,
 87		/*holdRewind      */ -1,
 88		},
 89		{
 90		/*loadState       */ {GamepadAxisEvent::Direction::NEUTRAL, -1},
 91		/*saveState       */ {GamepadAxisEvent::Direction::NEUTRAL, -1},
 92		/*holdFastForward */ {GamepadAxisEvent::Direction::POSITIVE, 5},
 93		/*holdRewind      */ {GamepadAxisEvent::Direction::POSITIVE, 2},
 94		}
 95	},
 96	{
 97		"(Sony Computer Entertainment )?Wireless Controller", // The DualShock 4 device ID is cut off on Windows
 98		{
 99		/*keyA      */  1,
100		/*keyB      */  2,
101		/*keySelect */  8,
102		/*keyStart  */  9,
103		/*keyRight  */ -1,
104		/*keyLeft   */ -1,
105		/*keyUp     */ -1,
106		/*keyDown   */ -1,
107		/*keyR      */  5,
108		/*keyL      */  4
109		},
110		{
111		/*loadState       */ 0,
112		/*saveState       */ 3,
113		/*holdFastForward */ 7,
114		/*holdRewind      */ 6,
115		},
116	},
117	{
118		"PLAYSTATION\\(R\\)3 Controller", // DualShock 3 (OS X)
119		{
120		/*keyA      */ 13,
121		/*keyB      */ 14,
122		/*keySelect */  0,
123		/*keyStart  */  3,
124		/*keyRight  */  5,
125		/*keyLeft   */  7,
126		/*keyUp     */  4,
127		/*keyDown   */  6,
128		/*keyR      */ 11,
129		/*keyL      */ 10
130		},
131		{
132		/*loadState       */ 15,
133		/*saveState       */ 12,
134		/*holdFastForward */ 9,
135		/*holdRewind      */ 8,
136		},
137	},
138	{
139		"Wiimote \\(..-..-..-..-..-..\\)", // WJoy (OS X)
140		{
141		/*keyA      */ 15,
142		/*keyB      */ 16,
143		/*keySelect */  7,
144		/*keyStart  */  6,
145		/*keyRight  */ 14,
146		/*keyLeft   */ 13,
147		/*keyUp     */ 11,
148		/*keyDown   */ 12,
149		/*keyR      */ 20,
150		/*keyL      */ 19
151		},
152		{
153		/*loadState       */ 18,
154		/*saveState       */ 17,
155		/*holdFastForward */ 22,
156		/*holdRewind      */ 21,
157		},
158	},
159};
160
161constexpr InputProfile::InputProfile(const char* name,
162                                     const KeyList<int> keys,
163                                     const Shortcuts<int> shortcutButtons,
164                                     const Shortcuts<Axis> shortcutAxes,
165                                     const KeyList<AxisValue> axes,
166                                     const struct Coord& tiltAxis,
167                                     const struct Coord& gyroAxis,
168                                     float gyroSensitivity)
169	: m_profileName(name)
170	, m_keys {
171		keys.keyA,
172		keys.keyB,
173		keys.keySelect,
174		keys.keyStart,
175		keys.keyRight,
176		keys.keyLeft,
177		keys.keyUp,
178		keys.keyDown,
179		keys.keyR,
180		keys.keyL,
181	}
182	, m_axes {
183		axes.keyA,
184		axes.keyB,
185		axes.keySelect,
186		axes.keyStart,
187		axes.keyRight,
188		axes.keyLeft,
189		axes.keyUp,
190		axes.keyDown,
191		axes.keyR,
192		axes.keyL,
193	}
194	, m_shortcutButtons(shortcutButtons)
195	, m_shortcutAxes(shortcutAxes)
196	, m_tiltAxis(tiltAxis)
197	, m_gyroAxis(gyroAxis)
198	, m_gyroSensitivity(gyroSensitivity)
199{
200}
201
202const InputProfile* InputProfile::findProfile(const QString& name) {
203	for (size_t i = 0; i < sizeof(s_defaultMaps) / sizeof(*s_defaultMaps); ++i) {
204		QRegExp re(s_defaultMaps[i].m_profileName);
205		if (re.exactMatch(name)) {
206			return &s_defaultMaps[i];
207		}
208	}
209	return nullptr;
210}
211
212void InputProfile::apply(InputController* controller) const {
213	for (size_t i = 0; i < GBA_KEY_MAX; ++i) {
214#ifdef BUILD_SDL
215		controller->bindKey(SDL_BINDING_BUTTON, m_keys[i], static_cast<GBAKey>(i));
216		controller->bindAxis(SDL_BINDING_BUTTON, m_axes[i].axis, m_axes[i].direction, static_cast<GBAKey>(i));
217#endif
218	}
219	controller->registerTiltAxisX(m_tiltAxis.x);
220	controller->registerTiltAxisY(m_tiltAxis.y);
221	controller->registerGyroAxisX(m_gyroAxis.x);
222	controller->registerGyroAxisY(m_gyroAxis.y);
223	controller->setGyroSensitivity(m_gyroSensitivity);
224}
225
226bool InputProfile::lookupShortcutButton(const QString& shortcutName, int* button) const {
227	if (shortcutName == QLatin1String("loadState")) {
228		*button = m_shortcutButtons.loadState;
229		return true;
230	}
231	if (shortcutName == QLatin1String("saveState")) {
232		*button = m_shortcutButtons.saveState;
233		return true;
234	}
235	if (shortcutName == QLatin1String("holdFastForward")) {
236		*button = m_shortcutButtons.holdFastForward;
237		return true;
238	}
239	if (shortcutName == QLatin1String("holdRewind")) {
240		*button = m_shortcutButtons.holdRewind;
241		return true;
242	}
243	return false;
244}
245
246bool InputProfile::lookupShortcutAxis(const QString& shortcutName, int* axis, GamepadAxisEvent::Direction* direction) const {
247	if (shortcutName == QLatin1String("loadState")) {
248		*axis = m_shortcutAxes.loadState.axis;
249		*direction = m_shortcutAxes.loadState.direction;
250		return true;
251	}
252	if (shortcutName == QLatin1String("saveState")) {
253		*axis = m_shortcutAxes.saveState.axis;
254		*direction = m_shortcutAxes.saveState.direction;
255		return true;
256	}
257	if (shortcutName == QLatin1String("holdFastForward")) {
258		*axis = m_shortcutAxes.holdFastForward.axis;
259		*direction = m_shortcutAxes.holdFastForward.direction;
260		return true;
261	}
262	if (shortcutName == QLatin1String("holdRewind")) {
263		*axis = m_shortcutAxes.holdRewind.axis;
264		*direction = m_shortcutAxes.holdRewind.direction;
265		return true;
266	}
267	return false;
268}