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 {
31  class SWIFT_CORE_EXPORT CInputManager : public QObject
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  using namespace std::placeholders;
52  auto function = std::bind(slotPointer, receiver, _1);
53  return bindImpl(action, receiver, function);
54  }
55 
57  template <typename Func>
58  int bind(const QString &action, QObject *receiver, Func functionObject)
59  {
60  return bindImpl(action, receiver, functionObject);
61  }
62 
64  void unbind(int index);
65 
69  void startCapture();
70 
72  void resetAllActions() { m_configuredActions.clear(); }
73 
75  QStringList allAvailableActions() const { return m_availableActions.keys(); }
76 
79 
81  void setForwarding(bool enabled) { m_actionRelayingEnabled = enabled; }
82 
84  void callFunctionsBy(const QString &action, bool isKeyDown, bool shouldEmit = true);
85 
87  void triggerKey(const swift::misc::input::CHotkeyCombination &combination, bool isPressed);
88 
90  void createDevices();
91 
93  void releaseDevices();
94 
96  swift::misc::input::CJoystickButtonList getAllAvailableJoystickButtons() const;
97 
98  signals:
100  void remoteActionFromLocal(const QString &action, bool argument);
101 
104 
107 
109  void hotkeyActionRegistered(const QStringList &actions);
110 
111  private:
113  struct BindInfo
114  {
115  // Using unique int index for identification because std::function does not have a operator==
116  int m_index = 0;
117  QString m_action;
118  QPointer<QObject> m_receiver;
119  std::function<void(bool)> m_function;
120  };
121 
123  void reloadHotkeySettings();
124 
125  void processKeyCombinationChanged(const swift::misc::input::CHotkeyCombination &combination);
126  void processButtonCombinationChanged(const swift::misc::input::CHotkeyCombination &combination);
127 
129  int bindImpl(const QString &action, QObject *receiver, std::function<void(bool)> function);
130 
132  void processCombination(const swift::misc::input::CHotkeyCombination &combination);
133 
134  std::unique_ptr<swift::input::IKeyboard> m_keyboard;
135  std::unique_ptr<swift::input::IJoystick> m_joystick;
136 
139  QSet<QString> m_activeActions;
140  QVector<BindInfo> m_boundActions;
141 
142  bool m_actionRelayingEnabled = false;
143  bool m_captureActive = false;
144  swift::misc::input::CHotkeyCombination m_lastCombination;
145  swift::misc::input::CHotkeyCombination m_capturedCombination;
146  swift::misc::input::CHotkeyCombination m_combinationBeforeCapture;
147 
149  &CInputManager::reloadHotkeySettings };
150  };
151 } // namespace swift::core
152 
153 #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:72
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:78
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:58
void setForwarding(bool enabled)
Enable event forwarding to core.
Definition: inputmanager.h:81
QStringList allAvailableActions() const
Get all available and known actions.
Definition: inputmanager.h:75
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.