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