src/platform/qt/ShortcutController.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 "ShortcutController.h"
7
8#include "ConfigController.h"
9#include "GamepadButtonEvent.h"
10#include "InputProfile.h"
11
12#include <QAction>
13#include <QKeyEvent>
14#include <QMenu>
15#include <QRegularExpression>
16
17using namespace QGBA;
18
19ShortcutController::ShortcutController(QObject* parent)
20 : QObject(parent)
21{
22}
23
24void ShortcutController::setConfigController(ConfigController* controller) {
25 m_config = controller;
26}
27
28void ShortcutController::setActionMapper(ActionMapper* actions) {
29 m_actions = actions;
30 connect(actions, &ActionMapper::actionAdded, this, &ShortcutController::generateItem);
31 connect(actions, &ActionMapper::menuCleared, this, &ShortcutController::menuCleared);
32 rebuildItems();
33}
34
35void ShortcutController::updateKey(const QString& name, int keySequence) {
36 auto item = m_items[name];
37 if (!item) {
38 return;
39 }
40 updateKey(item, keySequence);
41 if (m_config) {
42 m_config->setQtOption(item->name(), QKeySequence(keySequence).toString(), KEY_SECTION);
43 }
44}
45
46void ShortcutController::updateKey(std::shared_ptr<Shortcut> item, int keySequence) {
47 int oldShortcut = item->shortcut();
48 if (m_actions->isHeld(item->name())) {
49 if (oldShortcut > 0) {
50 m_heldKeys.take(oldShortcut);
51 }
52 if (keySequence > 0) {
53 m_heldKeys[keySequence] = item;
54 }
55 }
56
57 item->setShortcut(keySequence);
58}
59
60void ShortcutController::updateButton(const QString& name, int button) {
61 auto item = m_items[name];
62 if (!item) {
63 return;
64 }
65 int oldButton = item->button();
66 if (oldButton >= 0) {
67 m_buttons.take(oldButton);
68 }
69 item->setButton(button);
70 if (button >= 0) {
71 clearAxis(name);
72 m_buttons[button] = item;
73 }
74 if (m_config) {
75 m_config->setQtOption(name, button, BUTTON_SECTION);
76 if (!m_profileName.isNull()) {
77 m_config->setQtOption(name, button, BUTTON_PROFILE_SECTION + m_profileName);
78 }
79 }
80}
81
82void ShortcutController::updateAxis(const QString& name, int axis, GamepadAxisEvent::Direction direction) {
83 auto item = m_items[name];
84 if (!item) {
85 return;
86 }
87 int oldAxis = item->axis();
88 GamepadAxisEvent::Direction oldDirection = item->direction();
89 if (oldAxis >= 0) {
90 m_axes.take(std::make_pair(oldAxis, oldDirection));
91 }
92 if (axis >= 0 && direction != GamepadAxisEvent::NEUTRAL) {
93 clearButton(name);
94 m_axes[std::make_pair(axis, direction)] = item;
95 }
96 item->setAxis(axis, direction);
97 if (m_config) {
98 char d = '\0';
99 if (direction == GamepadAxisEvent::POSITIVE) {
100 d = '+';
101 }
102 if (direction == GamepadAxisEvent::NEGATIVE) {
103 d = '-';
104 }
105 m_config->setQtOption(name, QString("%1%2").arg(d).arg(axis), AXIS_SECTION);
106 if (!m_profileName.isNull()) {
107 m_config->setQtOption(name, QString("%1%2").arg(d).arg(axis), AXIS_PROFILE_SECTION + m_profileName);
108 }
109 }
110}
111
112void ShortcutController::clearKey(const QString& name) {
113 updateKey(name, 0);
114}
115
116void ShortcutController::clearButton(const QString& name) {
117 updateButton(name, -1);
118}
119
120void ShortcutController::clearAxis(const QString& name) {
121 updateAxis(name, -1, GamepadAxisEvent::NEUTRAL);
122}
123
124void ShortcutController::rebuildItems() {
125 m_items.clear();
126 m_buttons.clear();
127 m_axes.clear();
128 m_heldKeys.clear();
129 onSubitems({}, std::bind(&ShortcutController::generateItem, this, std::placeholders::_1));
130}
131
132bool ShortcutController::eventFilter(QObject*, QEvent* event) {
133 if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
134 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
135 if (keyEvent->isAutoRepeat()) {
136 return false;
137 }
138 int key = keyEvent->key();
139 if (!isModifierKey(key)) {
140 key |= (keyEvent->modifiers() & ~Qt::KeypadModifier);
141 } else {
142 key = toModifierKey(key | (keyEvent->modifiers() & ~Qt::KeypadModifier));
143 }
144 auto item = m_heldKeys.find(key);
145 if (item != m_heldKeys.end()) {
146 Action::BooleanFunction fn = item.value()->action()->booleanAction();
147 fn(event->type() == QEvent::KeyPress);
148 event->accept();
149 return true;
150 }
151 }
152 if (event->type() == GamepadButtonEvent::Down()) {
153 auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
154 if (item == m_buttons.end()) {
155 return false;
156 }
157 Action* action = item.value()->action();
158 if (action) {
159 action->trigger();
160 }
161 event->accept();
162 return true;
163 }
164 if (event->type() == GamepadButtonEvent::Up()) {
165 auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
166 if (item == m_buttons.end()) {
167 return false;
168 }
169 Action* action = item.value()->action();
170 if (action) {
171 action->trigger(false);
172 }
173 event->accept();
174 return true;
175 }
176 if (event->type() == GamepadAxisEvent::Type()) {
177 GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
178 auto item = m_axes.find(std::make_pair(gae->axis(), gae->direction()));
179 if (item == m_axes.end()) {
180 return false;
181 }
182 Action* action = item.value()->action();
183 if (action) {
184 action->trigger(gae->isNew());
185 }
186 event->accept();
187 return true;
188 }
189 return false;
190}
191
192void ShortcutController::generateItem(const QString& itemName) {
193 if (itemName.isNull() || itemName[0] == '.') {
194 return;
195 }
196 Action* action = m_actions->getAction(itemName);
197 if (action) {
198 std::shared_ptr<Shortcut> item = std::make_shared<Shortcut>(action);
199 m_items[itemName] = item;
200 loadShortcuts(item);
201 }
202 emit shortcutAdded(itemName);
203}
204
205bool ShortcutController::loadShortcuts(std::shared_ptr<Shortcut> item) {
206 if (item->name().isNull()) {
207 return false;
208 }
209 loadGamepadShortcuts(item);
210 QVariant shortcut = m_config->getQtOption(item->name(), KEY_SECTION);
211 if (!shortcut.isNull()) {
212 if (shortcut.toString().endsWith("+")) {
213 updateKey(item, toModifierShortcut(shortcut.toString()));
214 } else {
215 updateKey(item, QKeySequence(shortcut.toString())[0]);
216 }
217 return true;
218 } else {
219 QKeySequence defaultShortcut = m_actions->defaultShortcut(item->name());
220 if (!defaultShortcut.isEmpty()) {
221 updateKey(item, defaultShortcut[0]);
222 return true;
223 }
224 }
225 return false;
226}
227
228void ShortcutController::loadGamepadShortcuts(std::shared_ptr<Shortcut> item) {
229 if (item->name().isNull()) {
230 return;
231 }
232 QVariant button = m_config->getQtOption(item->name(), !m_profileName.isNull() ? BUTTON_PROFILE_SECTION + m_profileName : BUTTON_SECTION);
233 int oldButton = item->button();
234 if (oldButton >= 0) {
235 m_buttons.take(oldButton);
236 item->setButton(-1);
237 }
238 if (!button.isNull()) {
239 item->setButton(button.toInt());
240 m_buttons[button.toInt()] = item;
241 }
242
243 QVariant axis = m_config->getQtOption(item->name(), !m_profileName.isNull() ? AXIS_PROFILE_SECTION + m_profileName : AXIS_SECTION);
244 int oldAxis = item->axis();
245 GamepadAxisEvent::Direction oldDirection = item->direction();
246 if (oldAxis >= 0) {
247 m_axes.take(std::make_pair(oldAxis, oldDirection));
248 item->setAxis(-1, GamepadAxisEvent::NEUTRAL);
249 }
250 if (!axis.isNull()) {
251 QString axisDesc = axis.toString();
252 if (axisDesc.size() >= 2) {
253 GamepadAxisEvent::Direction direction = GamepadAxisEvent::NEUTRAL;
254 if (axisDesc[0] == '-') {
255 direction = GamepadAxisEvent::NEGATIVE;
256 }
257 if (axisDesc[0] == '+') {
258 direction = GamepadAxisEvent::POSITIVE;
259 }
260 bool ok;
261 int axis = axisDesc.mid(1).toInt(&ok);
262 if (ok) {
263 item->setAxis(axis, direction);
264 m_axes[std::make_pair(axis, direction)] = item;
265 }
266 }
267 }
268}
269
270void ShortcutController::loadProfile(const QString& profile) {
271 m_profileName = profile;
272 m_profile = InputProfile::findProfile(profile);
273 onSubitems({}, [this](std::shared_ptr<Shortcut> item) {
274 loadGamepadShortcuts(item);
275 });
276}
277
278void ShortcutController::onSubitems(const QString& menu, std::function<void(std::shared_ptr<Shortcut>)> func) {
279 for (const QString& subitem : m_actions->menuItems(menu)) {
280 auto item = m_items[subitem];
281 if (item) {
282 func(item);
283 }
284 if (subitem.size() && subitem[0] == '.') {
285 onSubitems(subitem.mid(1), func);
286 }
287 }
288}
289
290void ShortcutController::onSubitems(const QString& menu, std::function<void(const QString&)> func) {
291 for (const QString& subitem : m_actions->menuItems(menu)) {
292 func(subitem);
293 if (subitem.size() && subitem[0] == '.') {
294 onSubitems(subitem.mid(1), func);
295 }
296 }
297}
298
299int ShortcutController::toModifierShortcut(const QString& shortcut) {
300 // Qt doesn't seem to work with raw modifier shortcuts!
301 QStringList modifiers = shortcut.split('+');
302 int value = 0;
303 for (const auto& mod : modifiers) {
304 if (mod == QLatin1String("Shift")) {
305 value |= Qt::ShiftModifier;
306 continue;
307 }
308 if (mod == QLatin1String("Ctrl")) {
309 value |= Qt::ControlModifier;
310 continue;
311 }
312 if (mod == QLatin1String("Alt")) {
313 value |= Qt::AltModifier;
314 continue;
315 }
316 if (mod == QLatin1String("Meta")) {
317 value |= Qt::MetaModifier;
318 continue;
319 }
320 }
321 return value;
322}
323
324bool ShortcutController::isModifierKey(int key) {
325 switch (key) {
326 case Qt::Key_Shift:
327 case Qt::Key_Control:
328 case Qt::Key_Alt:
329 case Qt::Key_Meta:
330 return true;
331 default:
332 return false;
333 }
334}
335
336int ShortcutController::toModifierKey(int key) {
337 int modifiers = key & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier);
338 key ^= modifiers;
339 switch (key) {
340 case Qt::Key_Shift:
341 modifiers |= Qt::ShiftModifier;
342 break;
343 case Qt::Key_Control:
344 modifiers |= Qt::ControlModifier;
345 break;
346 case Qt::Key_Alt:
347 modifiers |= Qt::AltModifier;
348 break;
349 case Qt::Key_Meta:
350 modifiers |= Qt::MetaModifier;
351 break;
352 default:
353 break;
354 }
355 return modifiers;
356
357}
358
359const Shortcut* ShortcutController::shortcut(const QString& action) const {
360 return m_items[action].get();
361}
362
363QString ShortcutController::name(int index, const QString& parent) const {
364 QStringList menu = m_actions->menuItems(parent.isNull() || parent[0] != '.' ? parent : parent.mid(1));
365 menu.removeAll({});
366 if (index >= menu.size()) {
367 return {};
368 }
369
370 return menu[index];
371}
372
373QString ShortcutController::parent(const QString& action) const {
374 return QString(".%1").arg(m_actions->menuFor(action));
375}
376
377QString ShortcutController::visibleName(const QString& action) const {
378 if (action.isNull()) {
379 return {};
380 }
381 QString name;
382 if (action[0] == '.') {
383 name = m_actions->menuName(action.mid(1));
384 } else {
385 name = m_actions->getAction(action)->visibleName();
386 }
387 return name.replace(QRegularExpression("&(.)"), "\\1");
388}
389
390int ShortcutController::indexIn(const QString& action) const {
391 QString name = m_actions->menuFor(action);
392 QStringList menu = m_actions->menuItems(name);
393 menu.removeAll({});
394 return menu.indexOf(action);
395}
396
397int ShortcutController::count(const QString& name) const {
398 QStringList menu;
399 if (name.isNull()) {
400 menu = m_actions->menuItems();
401 } else if (name[0] != '.') {
402 return 0;
403 } else {
404 menu = m_actions->menuItems(name.mid(1));
405 }
406 menu.removeAll({});
407 return menu.count();
408}
409
410Shortcut::Shortcut(Action* action)
411 : m_action(action)
412{
413}
414
415void Shortcut::setShortcut(int shortcut) {
416 if (m_shortcut == shortcut) {
417 return;
418 }
419 m_shortcut = shortcut;
420 emit shortcutChanged(shortcut);
421}
422
423void Shortcut::setButton(int button) {
424 if (m_button == button) {
425 return;
426 }
427 m_button = button;
428 emit buttonChanged(button);
429}
430
431void Shortcut::setAxis(int axis, GamepadAxisEvent::Direction direction) {
432 if (m_axis == axis && m_direction == direction) {
433 return;
434 }
435 m_axis = axis;
436 m_direction = direction;
437 emit axisChanged(axis, direction);
438}