all repos — mgba @ e17e4fd19003209ff9db1f0ac1394e463c7b4a47

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		"Controller", // The Xbox 360 controller drivers on OS X are vague...
 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		"PLAYSTATION\\(R\\)3 Controller", // DualShock 3 (OS X)
 92		(int[GBA_KEY_MAX]) {
 93		/*keyA      */ 13,
 94		/*keyB      */ 14,
 95		/*keySelect */  0,
 96		/*keyStart  */  3,
 97		/*keyRight  */  5,
 98		/*keyLeft   */  7,
 99		/*keyUp     */  4,
100		/*keyDown   */  6,
101		/*keyR      */ 11,
102		/*keyL      */ 10
103		},
104		(ShortcutButton[]) {
105			{"loadState", 15},
106			{"saveState", 12},
107			{"holdFastForward", 9},
108			{"holdRewind", 8},
109			{}
110		}
111	},
112	{
113		"Wiimote \\(..-..-..-..-..-..\\)", // WJoy (OS X)
114		(int[GBA_KEY_MAX]) {
115		/*keyA      */ 15,
116		/*keyB      */ 16,
117		/*keySelect */  7,
118		/*keyStart  */  6,
119		/*keyRight  */ 14,
120		/*keyLeft   */ 13,
121		/*keyUp     */ 11,
122		/*keyDown   */ 12,
123		/*keyR      */ 20,
124		/*keyL      */ 19
125		},
126		(ShortcutButton[]) {
127			{"loadState", 18},
128			{"saveState", 17},
129			{"holdFastForward", 22},
130			{"holdRewind", 21},
131			{}
132		}
133	},
134};
135
136constexpr InputProfile::InputProfile(const char* name,
137                                     int keys[GBA_KEY_MAX],
138                                     const ShortcutButton* shortcutButtons,
139                                     const ShortcutAxis* shortcutAxes,
140                                     AxisValue axes[GBA_KEY_MAX],
141                                     const struct Coord& tiltAxis,
142                                     const struct Coord& gyroAxis,
143                                     float gyroSensitivity)
144	: m_profileName(name)
145	, m_keys {
146		keys[GBA_KEY_A],
147		keys[GBA_KEY_B],
148		keys[GBA_KEY_SELECT],
149		keys[GBA_KEY_START],
150		keys[GBA_KEY_RIGHT],
151		keys[GBA_KEY_LEFT],
152		keys[GBA_KEY_UP],
153		keys[GBA_KEY_DOWN],
154		keys[GBA_KEY_R],
155		keys[GBA_KEY_L]
156	}
157	, m_shortcutButtons(shortcutButtons)
158	, m_shortcutAxes(shortcutAxes)
159	, m_axes {
160		axes[GBA_KEY_A],
161		axes[GBA_KEY_B],
162		axes[GBA_KEY_SELECT],
163		axes[GBA_KEY_START],
164		axes[GBA_KEY_RIGHT],
165		axes[GBA_KEY_LEFT],
166		axes[GBA_KEY_UP],
167		axes[GBA_KEY_DOWN],
168		axes[GBA_KEY_R],
169		axes[GBA_KEY_L]
170	}
171	, m_tiltAxis(tiltAxis)
172	, m_gyroAxis(gyroAxis)
173	, m_gyroSensitivity(gyroSensitivity)
174{
175}
176
177const InputProfile* InputProfile::findProfile(const QString& name) {
178	for (size_t i = 0; i < sizeof(s_defaultMaps) / sizeof(*s_defaultMaps); ++i) {
179		QRegExp re(s_defaultMaps[i].m_profileName);
180		if (re.exactMatch(name)) {
181			return &s_defaultMaps[i];
182		}
183	}
184	return nullptr;
185}
186
187void InputProfile::apply(InputController* controller) const {
188	for (size_t i = 0; i < GBA_KEY_MAX; ++i) {
189#ifdef BUILD_SDL
190		controller->bindKey(SDL_BINDING_BUTTON, m_keys[i], static_cast<GBAKey>(i));
191		controller->bindAxis(SDL_BINDING_BUTTON, m_axes[i].axis, m_axes[i].direction, static_cast<GBAKey>(i));
192#endif
193	}
194	controller->registerTiltAxisX(m_tiltAxis.x);
195	controller->registerTiltAxisY(m_tiltAxis.y);
196	controller->registerGyroAxisX(m_gyroAxis.x);
197	controller->registerGyroAxisY(m_gyroAxis.y);
198	controller->setGyroSensitivity(m_gyroSensitivity);
199}
200
201bool InputProfile::lookupShortcutButton(const QString& shortcutName, int* button) const {
202	for (size_t i = 0; m_shortcutButtons[i].shortcut; ++i) {
203		const ShortcutButton& shortcut = m_shortcutButtons[i];
204		if (QLatin1String(shortcut.shortcut) == shortcutName) {
205			*button = shortcut.button;
206			return true;
207		}
208	}
209	return false;
210}
211
212bool InputProfile::lookupShortcutAxis(const QString& shortcutName, int* axis, GamepadAxisEvent::Direction* direction) const {
213	for (size_t i = 0; m_shortcutAxes[i].shortcut; ++i) {
214		const ShortcutAxis& shortcut = m_shortcutAxes[i];
215		if (QLatin1String(shortcut.shortcut) == shortcutName) {
216			*axis = shortcut.axis;
217			*direction = shortcut.direction;
218			return true;
219		}
220	}
221	return false;
222}