all repos — mgba @ e7be40e80ccb6dbeee0b79c97f349859b1b4afb8

mGBA Game Boy Advance Emulator

src/platform/qt/input/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 <QSettings>
11
12using namespace QGBA;
13
14QList<InputProfile> InputProfile::s_profiles;
15
16InputProfile::InputProfile(const QString& name)
17	: m_profileName(name)
18{
19}
20
21void InputProfile::loadDefaultProfiles() {
22	loadProfiles(":/input/default-profiles.ini");
23}
24
25void InputProfile::loadProfiles(const QString& path) {
26	QSettings profileIni(path, QSettings::IniFormat);
27
28	for (const auto& group : profileIni.childGroups()) {
29	}
30	profileIni.beginGroup(PROFILE_SECTION);
31	for (const auto& group : profileIni.childGroups()) {
32		loadProfile(profileIni, group);
33	}
34	profileIni.endGroup();
35}
36
37void InputProfile::loadProfile(QSettings& profileIni, const QString& name) {
38	profileIni.beginGroup(name);
39	s_profiles.append(name);
40	InputProfile& profile = s_profiles.last();
41	for (const auto& group : profileIni.childGroups()) {
42		profileIni.beginGroup(group);
43		if (group == MATCH_SECTION) {
44			for (const auto& key : profileIni.childKeys()) {
45				profile.m_match.append(QRegExp(profileIni.value(key).toString()));
46			}
47		}
48		for (const auto& key : profileIni.childKeys()) {
49			InputItem* item = profile.m_inputIndex.itemAt(key);
50			if (!item) {
51				item = profile.m_inputIndex.addItem(QString(), key);
52			}
53			if (group == BUTTON_SECTION) {
54				item->setButton(profileIni.value(key).toInt());
55			}
56			if (group == AXIS_SECTION) {
57				QString axisDescription = profileIni.value(key).toString();
58				GamepadAxisEvent::Direction direction = GamepadAxisEvent::POSITIVE;
59				int axis = profileIni.value(key).toInt();
60				if (axisDescription[0] == '-') {
61					direction = GamepadAxisEvent::NEGATIVE;
62					axis = -axis;
63				}
64
65				item->setAxis(axis, direction);
66			}
67			if (group == KEY_SECTION) {
68				item->setShortcut(profileIni.value(key).toInt());
69			}
70		}
71		profileIni.endGroup();
72	}
73	profile.m_inputIndex.rebuild();
74	profileIni.endGroup();
75}
76
77const InputProfile* InputProfile::findProfile(const QString& name) {
78	if (s_profiles.isEmpty()) {
79		loadDefaultProfiles();
80	}
81	for (const InputProfile& profile : s_profiles) {
82		for (const auto& match : profile.m_match) {
83			if (match.exactMatch(name)) {
84				return &profile;
85			}
86		}
87	}
88	return nullptr;
89}
90
91void InputProfile::apply(InputController* controller) const {
92	controller->rebuildIndex(&m_inputIndex);
93	controller->rebuildKeyIndex(&m_inputIndex);
94	controller->registerTiltAxisX(m_tiltAxis.x);
95	controller->registerTiltAxisY(m_tiltAxis.y);
96	controller->registerGyroAxisX(m_gyroAxis.x);
97	controller->registerGyroAxisY(m_gyroAxis.y);
98	controller->setGyroSensitivity(m_gyroSensitivity);
99}