diff --git a/src/blackcore/input_manager.cpp b/src/blackcore/input_manager.cpp new file mode 100644 index 000000000..47e5301a6 --- /dev/null +++ b/src/blackcore/input_manager.cpp @@ -0,0 +1,84 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "input_manager.h" + +using namespace BlackInput; +using namespace BlackMisc; +using namespace BlackMisc::Hardware; + +namespace BlackCore +{ + CInputManager *CInputManager::m_instance = nullptr; + + CInputManager::CInputManager(QObject *parent) : + QObject(parent), + m_keyboard(IKeyboard::getInstance()) + { + connect(m_keyboard, &IKeyboard::keyUp, this, &CInputManager::ps_processKeyboardKeyUp); + connect(m_keyboard, &IKeyboard::keyDown, this, &CInputManager::ps_processKeyboardKeyDown); + } + + CInputManager *CInputManager::getInstance() + { + if (!m_instance) + { + m_instance = new CInputManager(); + } + return m_instance; + } + + void CInputManager::changeHotkeySettings(Settings::CSettingKeyboardHotkeyList hotkeys) + { + CKeyboardKeyList keyList; + for (Settings::CSettingKeyboardHotkey settingHotkey : hotkeys) + { + CKeyboardKey key = settingHotkey.getKey(); + if (key.isEmpty()) continue; + + m_hashKeyboardKeyFunctions.insert(key, settingHotkey.getFunction()); + keyList.push_back(key); + } + m_keyboard->setKeysToMonitor(keyList); + } + + void CInputManager::ps_processKeyboardKeyDown(const CKeyboardKey &key) + { + if (!m_hashKeyboardKeyFunctions.contains(key)) return; + + // Get configured hotkey function + CHotkeyFunction hotkeyFunc = m_hashKeyboardKeyFunctions.value(key); + callFunctionsBy(hotkeyFunc, true); + } + + void CInputManager::ps_processKeyboardKeyUp(const CKeyboardKey &key) + { + if (!m_hashKeyboardKeyFunctions.contains(key)) return; + + // Get configured hotkey function + CHotkeyFunction hotkeyFunc = m_hashKeyboardKeyFunctions.value(key); + callFunctionsBy(hotkeyFunc, false); + } + + void CInputManager::callFunctionsBy(const CHotkeyFunction &hotkeyFunction, bool isKeyDown) + { + if (!m_hashRegisteredFunctions.contains(hotkeyFunction)) return; + auto func = m_hashRegisteredFunctions.value(hotkeyFunction); + func(isKeyDown); + } + + CInputManager::RegistrationHandle CInputManager::registerHotkeyFuncImpl(const BlackMisc::CHotkeyFunction &hotkeyFunction, + QObject *receiver, + std::function function) + { + m_hashRegisteredFunctions.insert(hotkeyFunction, function); + + RegistrationHandle handle; + handle.function = function; + handle.hotkeyFunction = hotkeyFunction; + handle.m_receiver = receiver; + return handle; + } +} diff --git a/src/blackcore/input_manager.h b/src/blackcore/input_manager.h new file mode 100644 index 000000000..7253c766a --- /dev/null +++ b/src/blackcore/input_manager.h @@ -0,0 +1,99 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKCORE_INPUTMANAGER_H +#define BLACKCORE_INPUTMANAGER_H + +#include "blackinput/keyboard.h" +#include "blackmisc/hwkeyboardkeylist.h" +#include "blackmisc/hotkeyfunction.h" +#include "blackmisc/setkeyboardhotkeylist.h" +#include +#include +#include +#include + +//! \file + +namespace BlackCore +{ + class CInputManager : public QObject + { + Q_OBJECT + + public: + + //! \brief Handle to a registered hotkey function + struct RegistrationHandle + { + //! \brief Constructor + RegistrationHandle() {} + + BlackMisc::CHotkeyFunction hotkeyFunction; //!< Registered hotkey function + QPointer m_receiver; //!< Registered receiver + std::function function; //!< Registered function + }; + + //! Register a new hotkey function + RegistrationHandle registerHotkeyFunc(const BlackMisc::CHotkeyFunction &hotkeyFunction, QObject *receiver, const QByteArray &slotName) + { + auto function = [=](bool isKeyDown){ QMetaObject::invokeMethod(receiver, slotName, Q_ARG(bool, isKeyDown)); }; + return registerHotkeyFuncImpl(hotkeyFunction, receiver, function); + } + + //! Register a new hotkey function + template + RegistrationHandle registerHotkeyFunc(const BlackMisc::CHotkeyFunction &hotkeyFunction, RecvObj *receiver, void (RecvObj:: *slotPointer)(bool)) + { + using namespace std::placeholders; + auto function = std::bind(slotPointer, receiver, _1); + return registerHotkeyFuncImpl(hotkeyFunction, receiver, function); + } + + //! Register a new hotkey function + template + RegistrationHandle registerHotkeyFunc(const BlackMisc::CHotkeyFunction &hotkeyFunction, QObject *receiver, Func functionObject) + { + return registerHotkeyFuncImpl(hotkeyFunction, receiver, functionObject); + } + + //! Deletes all registered hotkeys. Be careful with this method! + void resetAllHotkeyFuncs() { m_hashRegisteredFunctions.clear(); } + + //! Creates a native keyboard handler object + static CInputManager *getInstance(); + + public slots: + + //! Change hotkey settings + void changeHotkeySettings(BlackMisc::Settings::CSettingKeyboardHotkeyList hotkeys); + + //! Call functions by hotkeyfunction + void callFunctionsBy(const BlackMisc::CHotkeyFunction &hotkeyFunction, bool isKeyDown); + + protected: + //! Constructor + CInputManager(QObject *parent = nullptr); + + private slots: + + void ps_processKeyboardKeyDown(const BlackMisc::Hardware::CKeyboardKey &); + + void ps_processKeyboardKeyUp(const BlackMisc::Hardware::CKeyboardKey &); + + private: + + RegistrationHandle registerHotkeyFuncImpl(const BlackMisc::CHotkeyFunction &hotkeyFunction, QObject *receiver, std::function function); + + static CInputManager *m_instance; + + BlackInput::IKeyboard *m_keyboard = nullptr; + + QHash > m_hashRegisteredFunctions; + QHash m_hashKeyboardKeyFunctions; + }; +} + +#endif //BLACKCORE_INPUTMANAGER_H