swift
inputmanager.h
Go to the documentation of this file.
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 
5 
6 #ifndef SWIFT_CORE_INPUTMANAGER_H
7 #define SWIFT_CORE_INPUTMANAGER_H
8 
9 #include <functional>
10 #include <memory>
11 
12 #include <QHash>
13 #include <QObject>
14 #include <QPointer>
15 #include <QString>
16 #include <QStringList>
17 #include <QVector>
18 
20 #include "core/swiftcoreexport.h"
21 #include "input/joystick.h"
22 #include "input/keyboard.h"
23 #include "misc/icons.h"
26 #include "misc/settingscache.h"
27 
28 namespace swift::core
29 {
32  {
33  Q_OBJECT
34 
35  public:
37  CInputManager(QObject *parent = nullptr);
38 
40  void registerAction(const QString &action,
41  swift::misc::CIcons::IconIndex icon = swift::misc::CIcons::StandardIconEmpty16);
42 
44  void registerRemoteActions(const QStringList &actions);
45 
48  template <typename RecvObj>
49  int bind(const QString &action, RecvObj *receiver, void (RecvObj::*slotPointer)(bool))
50  {
51  // NOLINTBEGIN(modernize-avoid-bind)
52  using namespace std::placeholders;
53  auto function = std::bind(slotPointer, receiver, _1);
54  return bindImpl(action, receiver, function);
55  // NOLINTEND(modernize-avoid-bind)
56  }
57 
59  template <typename Func>
60  int bind(const QString &action, QObject *receiver, Func functionObject)
61  {
62  return bindImpl(action, receiver, functionObject);
63  }
64 
66  void unbind(int index);
67 
71  void startCapture();
72 
74  void resetAllActions() { m_configuredActions.clear(); }
75 
77  QStringList allAvailableActions() const { return m_availableActions.keys(); }
78 
81 
83  void setForwarding(bool enabled) { m_actionRelayingEnabled = enabled; }
84 
86  void callFunctionsBy(const QString &action, bool isKeyDown, bool shouldEmit = true);
87 
89  void triggerKey(const swift::misc::input::CHotkeyCombination &combination, bool isPressed);
90 
92  void createDevices();
93 
95  void releaseDevices();
96 
98  swift::misc::input::CJoystickButtonList getAllAvailableJoystickButtons() const;
99 
100  signals:
102  void remoteActionFromLocal(const QString &action, bool argument);
103 
106 
109 
111  void hotkeyActionRegistered(const QStringList &actions);
112 
113  private:
115  struct BindInfo
116  {
117  // Using unique int index for identification because std::function does not have a operator==
118  int m_index = 0;
119  QString m_action;
120  QPointer<QObject> m_receiver;
121  std::function<void(bool)> m_function;
122  };
123 
125  void reloadHotkeySettings();
126 
127  void processKeyCombinationChanged(const swift::misc::input::CHotkeyCombination &combination);
128  void processButtonCombinationChanged(const swift::misc::input::CHotkeyCombination &combination);
129 
131  int bindImpl(const QString &action, QObject *receiver, std::function<void(bool)> function);
132 
134  void processCombination(const swift::misc::input::CHotkeyCombination &combination);
135 
136  std::unique_ptr<swift::input::IKeyboard> m_keyboard;
137  std::unique_ptr<swift::input::IJoystick> m_joystick;
138 
141  QSet<QString> m_activeActions;
142  QVector<BindInfo> m_boundActions;
143 
144  bool m_actionRelayingEnabled = false;
145  bool m_captureActive = false;
146  swift::misc::input::CHotkeyCombination m_lastCombination;
147  swift::misc::input::CHotkeyCombination m_capturedCombination;
148  swift::misc::input::CHotkeyCombination m_combinationBeforeCapture;
149 
151  &CInputManager::reloadHotkeySettings };
152  };
153 } // namespace swift::core
154 
155 #endif // SWIFT_CORE_INPUTMANAGER_H
Input manager handling hotkey function calls.
Definition: inputmanager.h:32
void resetAllActions()
Deletes all registered hotkeys. Be careful with this method!
Definition: inputmanager.h:74
void remoteActionFromLocal(const QString &action, bool argument)
Event hotkeyfunction occured.
QMap< QString, swift::misc::CIcons::IconIndex > allAvailableActionsAndIcons() const
All actions and their icons (if any)
Definition: inputmanager.h:80
void combinationSelectionFinished(const swift::misc::input::CHotkeyCombination &combination)
Combination selection has finished.
int bind(const QString &action, RecvObj *receiver, void(RecvObj::*slotPointer)(bool))
Register a new hotkey function.
Definition: inputmanager.h:49
void combinationSelectionChanged(const swift::misc::input::CHotkeyCombination &combination)
Selected combination has changed.
void hotkeyActionRegistered(const QStringList &actions)
New hotkey action is registered.
int bind(const QString &action, QObject *receiver, Func functionObject)
Register a new hotkey function.
Definition: inputmanager.h:60
void setForwarding(bool enabled)
Enable event forwarding to core.
Definition: inputmanager.h:83
QStringList allAvailableActions() const
Get all available and known actions.
Definition: inputmanager.h:77
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition: icons.h:32
Class template for accessing a specific value in the CSettingsCache.
Definition: settingscache.h:68
Value object representing hotkey sequence.
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
#define SWIFT_CORE_EXPORT
Export a class or function from the library.