swift
Loading...
Searching...
No Matches
inputmanager.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "core/inputmanager.h"
5
6#include <algorithm>
7#include <climits>
8
9#include <QtGlobal>
10
12#include "misc/range.h"
13#include "misc/sequence.h"
14
15// clazy:excludeall=detaching-member
16
17using namespace swift::input;
18using namespace swift::misc;
19using namespace swift::misc::input;
20
21namespace swift::core
22{
23 CInputManager::CInputManager(QObject *parent) : QObject(parent) { reloadHotkeySettings(); }
24
26 {
27 if (!m_availableActions.contains(action))
28 {
29 m_availableActions.insert(action, icon);
30 emit this->hotkeyActionRegistered({ action });
31 }
32 }
33
35 {
36 for (const auto &action : actions)
37 {
38 if (!m_availableActions.contains(action))
39 {
40 m_availableActions.insert(action, {});
41 emit this->hotkeyActionRegistered({ action });
42 }
43 }
44 }
45
46 void CInputManager::unbind(int index)
47 {
48 auto info =
49 std::ranges::find_if(m_boundActions, [index](const BindInfo &info) { return info.m_index == index; });
50 if (info != m_boundActions.end())
51 {
52 m_boundActions.erase(info);
53 }
54 }
55
56 void CInputManager::reloadHotkeySettings()
57 {
58 m_configuredActions.clear();
59 for (const CActionHotkey &actionHotkey : m_actionHotkeys.getThreadLocal())
60 {
61 if (!actionHotkey.getApplicableMachine().isFromLocalMachine())
62 {
63 continue;
64 }
65 CHotkeyCombination combination = actionHotkey.getCombination();
66 if (combination.isEmpty()) continue;
67
68 m_configuredActions.insert(combination, actionHotkey.getAction());
69 }
70 }
71
72 void CInputManager::processKeyCombinationChanged(const CHotkeyCombination &combination)
73 {
74 // Merge in the joystick part
75 // mixed on purpose, as any joystick keyboard combination is possible
76 CHotkeyCombination copy(combination);
77 copy.setJoystickButtons(m_lastCombination.getJoystickButtons());
78 processCombination(copy);
79 }
80
81 void CInputManager::processButtonCombinationChanged(const CHotkeyCombination &combination)
82 {
83 // Merge in the keyboard keys
84 // mixed on purpose, as any joystick keyboard combination is possible
85 CHotkeyCombination copy(combination);
86 copy.setKeyboardKeys(m_lastCombination.getKeyboardKeys());
87 processCombination(copy);
88 }
89
91 {
92 m_captureActive = true;
93 m_capturedCombination = {};
94 m_combinationBeforeCapture = m_lastCombination;
95 }
96
97 void CInputManager::callFunctionsBy(const QString &action, bool isKeyDown, bool shouldEmit)
98 {
99 if (action.isEmpty())
100 {
101 return;
102 }
103 if (m_actionRelayingEnabled && shouldEmit)
104 {
105 emit remoteActionFromLocal(action, isKeyDown);
106 }
107
108 for (const auto &boundAction : std::as_const(m_boundActions))
109 {
110 if (boundAction.m_action == action)
111 {
112 boundAction.m_function(isKeyDown);
113 }
114 }
115 }
116
117 void CInputManager::triggerKey(const CHotkeyCombination &combination, bool isPressed)
118 {
119 Q_UNUSED(isPressed)
120 QString previousAction = m_configuredActions.value(m_lastCombination);
121 QString action = m_configuredActions.value(combination);
122 callFunctionsBy(previousAction, false);
123 callFunctionsBy(action, true);
124 m_lastCombination = combination;
125 }
126
128 {
129 m_keyboard = IKeyboard::create(this);
130 m_joystick = IJoystick::create(this);
131 connect(m_keyboard.get(), &IKeyboard::keyCombinationChanged, this, &CInputManager::processKeyCombinationChanged,
133 connect(m_joystick.get(), &IJoystick::buttonCombinationChanged, this,
134 &CInputManager::processButtonCombinationChanged, Qt::QueuedConnection);
135 }
136
138 {
139 m_keyboard.reset();
140 m_joystick.reset();
141 }
142
144 {
145 return m_joystick->getAllAvailableJoystickButtons();
146 }
147
148 int CInputManager::bindImpl(const QString &action, QObject *receiver, std::function<void(bool)> function)
149 {
150 static int index = 0;
151 Q_ASSERT(index < INT_MAX);
152 BindInfo info;
153 info.m_index = index;
154 ++index;
155 info.m_function = function;
156 info.m_action = action;
157 info.m_receiver = receiver;
158 m_boundActions.push_back(info);
159 return info.m_index;
160 }
161
162 void CInputManager::processCombination(const CHotkeyCombination &currentCombination)
163 {
164 if (m_captureActive)
165 {
166 CHotkeyCombination deltaCombination = currentCombination.getDeltaComparedTo(m_combinationBeforeCapture);
167
168 // Don't continue if there is no relevant combination yet
169 if (m_capturedCombination.isEmpty() && deltaCombination.isEmpty())
170 {
171 return;
172 }
173
174 if (deltaCombination.size() < m_capturedCombination.size())
175 {
176 emit combinationSelectionFinished(m_capturedCombination);
177 m_captureActive = false;
178 }
179 else
180 {
181 emit combinationSelectionChanged(deltaCombination);
182 m_capturedCombination = deltaCombination;
183 }
184 return;
185 }
186
187 QSet<QString> newActiveActions;
188 for (const auto [combination, action] : makePairsRange(std::as_const(m_configuredActions)))
189 {
190 if (combination.isSubsetOf(currentCombination))
191 {
192 newActiveActions.insert(action);
193 }
194 }
195
196 const QSet<QString> pressedActions = newActiveActions - m_activeActions;
197 const QSet<QString> releasedActions = m_activeActions - newActiveActions;
198 m_activeActions = newActiveActions;
199 for (const QString &action : pressedActions)
200 {
201 callFunctionsBy(action, true);
202 }
203 for (const QString &action : releasedActions)
204 {
205 callFunctionsBy(action, false);
206 }
207
208 // combination
209 m_lastCombination = currentCombination;
210 }
211} // namespace swift::core
void registerRemoteActions(const QStringList &actions)
Register remote actions.
void callFunctionsBy(const QString &action, bool isKeyDown, bool shouldEmit=true)
Call functions by hotkeyfunction.
void remoteActionFromLocal(const QString &action, bool argument)
Event hotkeyfunction occured.
void registerAction(const QString &action, swift::misc::CIcons::IconIndex icon=swift::misc::CIcons::StandardIconEmpty16)
Register new action.
void combinationSelectionFinished(const swift::misc::input::CHotkeyCombination &combination)
Combination selection has finished.
void combinationSelectionChanged(const swift::misc::input::CHotkeyCombination &combination)
Selected combination has changed.
void startCapture()
Select a key combination as hotkey. This method returns immediatly. Listen for signals combinationSel...
void unbind(int index)
Unbind a slot.
void hotkeyActionRegistered(const QStringList &actions)
New hotkey action is registered.
void triggerKey(const swift::misc::input::CHotkeyCombination &combination, bool isPressed)
Triggers a key event manually and calls the registered functions.
swift::misc::input::CJoystickButtonList getAllAvailableJoystickButtons() const
Get all available joystick buttons.
CInputManager(QObject *parent=nullptr)
Constructor.
void createDevices()
Creates low level input devices. Once completed, hotkeys start to be processed.
void releaseDevices()
Releases all devices.
void buttonCombinationChanged(const swift::misc::input::CHotkeyCombination &)
Joystick button combination has changed.
static std::unique_ptr< IJoystick > create(QObject *parent=nullptr)
Creates a native joystick handler object.
Definition joystick.cpp:21
void keyCombinationChanged(const swift::misc::input::CHotkeyCombination &)
Key combination changed.
static std::unique_ptr< IKeyboard > create(QObject *parent=nullptr)
Creates a native keyboard handler object.
Definition keyboard.cpp:20
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
Value object encapsulating a action hotkey.
Value object representing hotkey sequence.
int size() const
Get size of sequence.
CHotkeyCombination getDeltaComparedTo(const CHotkeyCombination &other) const
Returns the delta (removing all keys and buttons contained in other).
bool isSubsetOf(const CHotkeyCombination &other) const
Is sequence a subset of other? E.g. CTRL would be a subset of CTRL+D.
bool isEmpty() const
Is sequence empty?
Value object encapsulating a list of joystick buttons.
Backend services of the swift project, like dealing with the network or the simulators.
Definition actionbind.cpp:7
Free functions in swift::misc.
auto makePairsRange(const T &container)
Returns a const CRange for iterating over the keys and values of a Qt associative container.
Definition range.h:386
void clear()
QHash< Key, T >::iterator insert(Key &&key, T &&value)
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QSet< T >::iterator insert(QSet< T >::const_iterator it, const T &value)
bool isEmpty() const const
QueuedConnection