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