all repos — mgba @ 8d7cbbe66936181f0851a5ad7ded9c648d7a98b6

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