all repos — mgba @ 000b49e45b30ec9c856b7b2e8cf8baee21daa56d

mGBA Game Boy Advance Emulator

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			if (m_actions->isHeld(action->name())) {
163				action->trigger(true);
164			} else {
165				action->trigger(!action->isActive());
166			}
167		}
168		event->accept();
169		return true;
170	}
171	if (event->type() == GamepadButtonEvent::Up()) {
172		auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
173		if (item == m_buttons.end()) {
174			return false;
175		}
176		Action* action = item.value()->action();
177		if (action && m_actions->isHeld(action->name())) {
178			action->trigger(false);
179		}
180		event->accept();
181		return true;
182	}
183	if (event->type() == GamepadAxisEvent::Type()) {
184		GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
185		auto item = m_axes.find(std::make_pair(gae->axis(), gae->direction()));
186		if (item == m_axes.end()) {
187			return false;
188		}
189		Action* action = item.value()->action();
190		if (action) {
191			if (gae->isNew()) {
192				if (m_actions->isHeld(action->name())) {
193					action->trigger(true);
194				} else {
195					action->trigger(!action->isActive());
196				}
197			} else if (m_actions->isHeld(action->name())) {
198				action->trigger(false);
199			}
200		}
201		event->accept();
202		return true;
203	}
204	return false;
205}
206
207void ShortcutController::generateItem(const QString& itemName) {
208	if (itemName.isNull() || itemName[0] == '.') {
209		return;
210	}
211	Action* action = m_actions->getAction(itemName);
212	if (action) {
213		std::shared_ptr<Shortcut> item = std::make_shared<Shortcut>(action);
214		m_items[itemName] = item;
215		loadShortcuts(item);
216	}
217	emit shortcutAdded(itemName);
218}
219
220bool ShortcutController::loadShortcuts(std::shared_ptr<Shortcut> item) {
221	if (item->name().isNull()) {
222		return false;
223	}
224	loadGamepadShortcuts(item);
225	QVariant shortcut = m_config->getQtOption(item->name(), KEY_SECTION);
226	if (!shortcut.isNull()) {
227		if (shortcut.toString().endsWith("+")) {
228			updateKey(item, toModifierShortcut(shortcut.toString()));
229		} else {
230			updateKey(item, QKeySequence(shortcut.toString())[0]);
231		}
232		return true;
233	} else {
234		QKeySequence defaultShortcut = m_actions->defaultShortcut(item->name());
235		if (!defaultShortcut.isEmpty()) {
236			updateKey(item, defaultShortcut[0]);
237			return true;
238		}
239	}
240	return false;
241}
242
243void ShortcutController::loadGamepadShortcuts(std::shared_ptr<Shortcut> item) {
244	if (item->name().isNull()) {
245		return;
246	}
247	QVariant button = m_config->getQtOption(item->name(), !m_profileName.isNull() ? BUTTON_PROFILE_SECTION + m_profileName : BUTTON_SECTION);
248	int oldButton = item->button();
249	if (oldButton >= 0) {
250		m_buttons.take(oldButton);
251		item->setButton(-1);
252	}
253	if (!button.isNull()) {
254		item->setButton(button.toInt());
255		m_buttons[button.toInt()] = item;
256	}
257
258	QVariant axis = m_config->getQtOption(item->name(), !m_profileName.isNull() ? AXIS_PROFILE_SECTION + m_profileName : AXIS_SECTION);
259	int oldAxis = item->axis();
260	GamepadAxisEvent::Direction oldDirection = item->direction();
261	if (oldAxis >= 0) {
262		m_axes.take(std::make_pair(oldAxis, oldDirection));
263		item->setAxis(-1, GamepadAxisEvent::NEUTRAL);
264	}
265	if (!axis.isNull()) {
266		QString axisDesc = axis.toString();
267		if (axisDesc.size() >= 2) {
268			GamepadAxisEvent::Direction direction = GamepadAxisEvent::NEUTRAL;
269			if (axisDesc[0] == '-') {
270				direction = GamepadAxisEvent::NEGATIVE;
271			}
272			if (axisDesc[0] == '+') {
273				direction = GamepadAxisEvent::POSITIVE;
274			}
275			bool ok;
276			int axis = axisDesc.mid(1).toInt(&ok);
277			if (ok) {
278				item->setAxis(axis, direction);
279				m_axes[std::make_pair(axis, direction)] = item;
280			}
281		}
282	}
283}
284
285void ShortcutController::loadProfile(const QString& profile) {
286	m_profileName = profile;
287	m_profile = InputProfile::findProfile(profile);
288	onSubitems({}, [this](std::shared_ptr<Shortcut> item) {
289		loadGamepadShortcuts(item);
290	});
291}
292
293void ShortcutController::onSubitems(const QString& menu, std::function<void(std::shared_ptr<Shortcut>)> func) {
294	for (const QString& subitem : m_actions->menuItems(menu)) {
295		auto item = m_items[subitem];
296		if (item) {
297			func(item);
298		}
299		if (subitem.size() && subitem[0] == '.') {
300			onSubitems(subitem.mid(1), func);
301		}
302	}
303}
304
305void ShortcutController::onSubitems(const QString& menu, std::function<void(const QString&)> func) {
306	for (const QString& subitem : m_actions->menuItems(menu)) {
307		func(subitem);
308		if (subitem.size() && subitem[0] == '.') {
309			onSubitems(subitem.mid(1), func);
310		}
311	}
312}
313
314int ShortcutController::toModifierShortcut(const QString& shortcut) {
315	// Qt doesn't seem to work with raw modifier shortcuts!
316	QStringList modifiers = shortcut.split('+');
317	int value = 0;
318	for (const auto& mod : modifiers) {
319		if (mod == QLatin1String("Shift")) {
320			value |= Qt::ShiftModifier;
321			continue;
322		}
323		if (mod == QLatin1String("Ctrl")) {
324			value |= Qt::ControlModifier;
325			continue;
326		}
327		if (mod == QLatin1String("Alt")) {
328			value |= Qt::AltModifier;
329			continue;
330		}
331		if (mod == QLatin1String("Meta")) {
332			value |= Qt::MetaModifier;
333			continue;
334		}
335	}
336	return value;
337}
338
339bool ShortcutController::isModifierKey(int key) {
340	switch (key) {
341	case Qt::Key_Shift:
342	case Qt::Key_Control:
343	case Qt::Key_Alt:
344	case Qt::Key_Meta:
345		return true;
346	default:
347		return false;
348	}
349}
350
351int ShortcutController::toModifierKey(int key) {
352	int modifiers = key & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier);
353	key ^= modifiers;
354	switch (key) {
355	case Qt::Key_Shift:
356		modifiers |= Qt::ShiftModifier;
357		break;
358	case Qt::Key_Control:
359		modifiers |= Qt::ControlModifier;
360		break;
361	case Qt::Key_Alt:
362		modifiers |= Qt::AltModifier;
363		break;
364	case Qt::Key_Meta:
365		modifiers |= Qt::MetaModifier;
366		break;
367	default:
368		break;
369	}
370	return modifiers;
371
372}
373
374const Shortcut* ShortcutController::shortcut(const QString& action) const {
375	return m_items[action].get();
376}
377
378QString ShortcutController::name(int index, const QString& parent) const {
379	QStringList menu = m_actions->menuItems(parent.isNull() || parent[0] != '.' ? parent : parent.mid(1));
380	menu.removeAll({});
381	if (index >= menu.size()) {
382		return {};
383	}
384
385	return menu[index];
386}
387
388QString ShortcutController::parent(const QString& action) const {
389	return QString(".%1").arg(m_actions->menuFor(action));
390}
391
392QString ShortcutController::visibleName(const QString& action) const {
393	if (action.isNull()) {
394		return {};
395	}
396	QString name;
397	if (action[0] == '.') {
398		name = m_actions->menuName(action.mid(1));
399	} else {
400		name = m_actions->getAction(action)->visibleName();
401	}
402	return name.replace(QRegularExpression("&(.)"), "\\1");
403}
404
405int ShortcutController::indexIn(const QString& action) const {
406	QString name = m_actions->menuFor(action);
407	QStringList menu = m_actions->menuItems(name);
408	menu.removeAll({});
409	return menu.indexOf(action);
410}
411
412int ShortcutController::count(const QString& name) const {
413	QStringList menu;
414	if (name.isNull()) {
415		menu = m_actions->menuItems();
416	} else if (name[0] != '.') {
417		return 0;
418	} else {
419		menu = m_actions->menuItems(name.mid(1));
420	}
421	menu.removeAll({});
422	return menu.count();
423}
424
425Shortcut::Shortcut(Action* action)
426	: m_action(action)
427{
428}
429
430void Shortcut::setShortcut(int shortcut) {
431	if (m_shortcut == shortcut) {
432		return;
433	}
434	m_shortcut = shortcut;
435	emit shortcutChanged(shortcut);
436}
437
438void Shortcut::setButton(int button) {
439	if (m_button == button) {
440		return;
441	}
442	m_button = button;
443	emit buttonChanged(button);
444}
445
446void Shortcut::setAxis(int axis, GamepadAxisEvent::Direction direction) {
447	if (m_axis == axis && m_direction == direction) {
448		return;
449	}
450	m_axis = axis;
451	m_direction = direction;
452	emit axisChanged(axis, direction);
453}