From 6644c7370304e11882d25900319ea486fd7f9645 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Fri, 14 Aug 2015 20:24:08 +0200 Subject: [PATCH] refs #453 Simplify hotkey action registration and relaying --- src/blackcore/actionbind.h | 51 +++++++++++++++++++++ src/blackcore/context_application.cpp | 41 +++++++++++++++++ src/blackcore/context_application.h | 22 +++++++-- src/blackcore/context_application_impl.cpp | 31 +++++++++---- src/blackcore/context_application_impl.h | 9 ++-- src/blackcore/context_application_proxy.cpp | 17 +++++-- src/blackcore/context_application_proxy.h | 9 ++-- 7 files changed, 157 insertions(+), 23 deletions(-) create mode 100644 src/blackcore/actionbind.h diff --git a/src/blackcore/actionbind.h b/src/blackcore/actionbind.h new file mode 100644 index 000000000..ec4112189 --- /dev/null +++ b/src/blackcore/actionbind.h @@ -0,0 +1,51 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKCORE_ACTIONBIND_H +#define BLACKCORE_ACTIONBIND_H + +#include "blackcore/input_manager.h" + +namespace BlackCore +{ + /*! + * CActionBind binds a member function to an action + */ + class CActionBind + { + public: + //! Signature of receiving member function + template + using MembFunc = void (U::*)(bool); + + //! Constructor + template + CActionBind(const QString &action, Receiver *receiver, MembFunc slot = nullptr) + { + auto inputManger = CInputManager::instance(); + inputManger->registerAction(action); + m_index = inputManger->bind(action, receiver, slot); + } + + //! Destructor + ~CActionBind() + { + auto inputManger = CInputManager::instance(); + inputManger->unbind(m_index); + } + + private: + int m_index; + }; +} + +#endif + diff --git a/src/blackcore/context_application.cpp b/src/blackcore/context_application.cpp index b4b778d38..7f495bc9d 100644 --- a/src/blackcore/context_application.cpp +++ b/src/blackcore/context_application.cpp @@ -15,9 +15,11 @@ #include "blackcore/settingscache.h" #include "blackmisc/statusmessage.h" #include "blackmisc/loghandler.h" +#include "blackmisc/logmessage.h" #include #include +using namespace BlackCore; using namespace BlackMisc; using namespace BlackMisc::Settings; @@ -58,6 +60,31 @@ namespace BlackCore }); changeSettings(IContextSettings::SettingsHotKeys); + bool s = connect(CInputManager::instance(), &CInputManager::hotkeyActionRegistered, [this](const QStringList &actions) + { + this->registerHotkeyActions(actions, {}); + }); + Q_ASSERT(s); + s = connect(this, &IContextApplication::hotkeyActionsRegistered, [this](const QStringList &actions, const CIdentifier &origin) + { + if(origin.isFromSameProcess()) { return; } + CInputManager::instance()->registerRemoteActions(actions); + }); + Q_ASSERT(s); + + connect(CInputManager::instance(), &CInputManager::remoteActionFromLocal, [this](const QString &action, bool argument) + { + this->callHotkeyAction(action, argument, {}); + }); + connect(this, &IContextApplication::remoteHotkeyAction, [this](const QString &action, bool argument, const CIdentifier &origin) + { + if(origin.isFromLocalMachine()) { return; } + CInputManager::instance()->callFunctionsBy(action, argument); + CLogMessage(this, CLogCategory::contextSlot()).debug() << "Calling function" << action << "from origin" << origin.getMachineName(); + }); + + // Enable event forwarding from GUI process to core + CInputManager::instance()->setForwarding(true); } void IContextApplication::changeSettings(const CVariantMap &settings, const CIdentifier &origin) @@ -67,6 +94,20 @@ namespace BlackCore qFatal("Not implemented"); // avoid losing a change during context interface construction } + void IContextApplication::registerHotkeyActions(const QStringList &actions, const BlackMisc::CIdentifier &origin) + { + Q_UNUSED(actions); + Q_UNUSED(origin); + qFatal("Not implemented"); // avoid losing a change during context interface construction + } + + void IContextApplication::callHotkeyAction(const QString &action, bool argument, const BlackMisc::CIdentifier &origin) + { + Q_UNUSED(action); + Q_UNUSED(argument); + Q_UNUSED(origin); + } + void IContextApplication::changeSettings(uint typeValue) { auto type = static_cast(typeValue); diff --git a/src/blackcore/context_application.h b/src/blackcore/context_application.h index c349c7acd..790beb870 100644 --- a/src/blackcore/context_application.h +++ b/src/blackcore/context_application.h @@ -16,7 +16,6 @@ #include "blackcore/context.h" #include "blackmisc/statusmessagelist.h" #include "blackmisc/audio/voiceroomlist.h" -#include "blackmisc/eveventhotkeyfunction.h" #include "blackmisc/identifierlist.h" #include "blackmisc/variantmap.h" #include @@ -83,6 +82,14 @@ namespace BlackCore //! \note Used for cache relay, do not use directly void settingsChanged(const BlackMisc::CVariantMap &settings, const BlackMisc::CIdentifier &origin); + //! New action was registered + //! \note Used to register hotkey action, do not use directly + void hotkeyActionsRegistered(const QStringList &actions, const BlackMisc::CIdentifier &origin); + + //! Call a hotkey action on a remote process + //! \note Used for hotkey action, do not use directly + void remoteHotkeyAction(const QString &action, bool argument, const BlackMisc::CIdentifier &origin); + //! Work around for audio context, #382 void fakedSetComVoiceRoom(const BlackMisc::Audio::CVoiceRoomList &requestedRooms); @@ -97,6 +104,16 @@ namespace BlackCore //! \note This is the function which relays cache changes via DBus. virtual void changeSettings(const BlackMisc::CVariantMap &settings, const BlackMisc::CIdentifier &origin); + //! Register hotkey action implemented by another process + //! \note Not pure because it can be called from the base class constructor. + //! \note This is the function which relays action registrations via DBus + virtual void registerHotkeyActions(const QStringList &actions, const BlackMisc::CIdentifier &origin); + + //! Call a hotkey action on a remote process + //! \note Not pure because it can be called from the base class constructor. + //! \note This is the function which relays action calls via DBus + virtual void callHotkeyAction(const QString &action, bool argument, const BlackMisc::CIdentifier &origin); + //! Register application, can also be used for ping virtual BlackMisc::CIdentifier registerApplication(const BlackMisc::CIdentifier &application) = 0; @@ -118,9 +135,6 @@ namespace BlackCore //! Remote enabled version of file exists virtual bool existsFile(const QString &fileName) const = 0; - //! Process remote event - virtual void processHotkeyFuncEvent(const BlackMisc::Event::CEventHotkeyFunction &event) = 0; - //! Change settings void changeSettings(uint typeValue); diff --git a/src/blackcore/context_application_impl.cpp b/src/blackcore/context_application_impl.cpp index 77f23c547..00344d1c2 100644 --- a/src/blackcore/context_application_impl.cpp +++ b/src/blackcore/context_application_impl.cpp @@ -47,6 +47,18 @@ namespace BlackCore emit this->settingsChanged(settings, origin); } + void CContextApplication::registerHotkeyActions(const QStringList &actions, const CIdentifier &origin) + { + // Intentionally don't check for round trip here + emit this->hotkeyActionsRegistered(actions, origin); + } + + void CContextApplication::callHotkeyAction(const QString &action, bool argument, const CIdentifier &origin) + { + // Intentionally don't check for round trip here + emit this->remoteHotkeyAction(action, argument, origin); + } + bool CContextApplication::writeToFile(const QString &fileName, const QString &content) { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO << fileName << content.left(25); } @@ -67,8 +79,17 @@ namespace BlackCore CIdentifier CContextApplication::registerApplication(const CIdentifier &application) { if (m_debugEnabled) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO << application; } - this->m_registeredApplications.replaceOrAdd(application, application); - emit registrationChanged(); + if (!m_registeredApplications.contains(application)) + { + m_registeredApplications.push_back(application); + emit registrationChanged(); + emit this->hotkeyActionsRegistered(CInputManager::instance()->allAvailableActions(), {}); + } + else + { + m_registeredApplications.replace(application, application); + } + return application; } @@ -114,10 +135,4 @@ namespace BlackCore return QFile::exists(fileName); } - void CContextApplication::processHotkeyFuncEvent(const BlackMisc::Event::CEventHotkeyFunction &event) - { - CInputManager::getInstance()->callFunctionsBy(event.getFunction(), event.getFunctionArgument()); - CLogMessage(this, CLogCategory::contextSlot()).debug() << "Calling function from origin" << event.getEventOriginator().toQString(); - } - } // namespace diff --git a/src/blackcore/context_application_impl.h b/src/blackcore/context_application_impl.h index 0d49dbf51..b8f051f81 100644 --- a/src/blackcore/context_application_impl.h +++ b/src/blackcore/context_application_impl.h @@ -35,6 +35,12 @@ namespace BlackCore //! \copydoc IContextApplication::changeSettings virtual void changeSettings(const BlackMisc::CVariantMap &settings, const BlackMisc::CIdentifier &origin) override; + //! \copydoc IContextApplication::registerHotkeyActions + virtual void registerHotkeyActions(const QStringList &actions, const BlackMisc::CIdentifier &origin) override; + + //! \copydoc IContextApplication::callHotkeyAction + virtual void callHotkeyAction(const QString &action, bool argument, const BlackMisc::CIdentifier &origin) override; + //! \copydoc IContextApplication::writeToFile virtual bool writeToFile(const QString &fileName, const QString &content) override; @@ -56,9 +62,6 @@ namespace BlackCore //! \copydoc IContextApplication::existsFile virtual bool existsFile(const QString &fileName) const override; - //! \copydoc IContextApplication::processHotkeyFuncEvent - virtual void processHotkeyFuncEvent(const BlackMisc::Event::CEventHotkeyFunction &event) override; - public: //! \todo Remove with old settings using IContextApplication::changeSettings; diff --git a/src/blackcore/context_application_proxy.cpp b/src/blackcore/context_application_proxy.cpp index b03837e8e..85ba3a669 100644 --- a/src/blackcore/context_application_proxy.cpp +++ b/src/blackcore/context_application_proxy.cpp @@ -49,6 +49,8 @@ namespace BlackCore s = connection.connect(serviceName, IContextApplication::ObjectPath(), IContextApplication::InterfaceName(), "fakedSetComVoiceRoom", this, SIGNAL(fakedSetComVoiceRoom(BlackMisc::Audio::CVoiceRoomList))); Q_ASSERT(s); + s = connection.connect(serviceName, IContextApplication::ObjectPath(), IContextApplication::InterfaceName(), + "hotkeyActionsRegistered", this, SIGNAL(hotkeyActionsRegistered(QStringList, BlackMisc::CIdentifier))); Q_UNUSED(s); } @@ -62,6 +64,16 @@ namespace BlackCore this->m_dBusInterface->callDBus(QLatin1Literal("changeSettings"), settings, origin); } + void CContextApplicationProxy::registerHotkeyActions(const QStringList &actions, const CIdentifier &origin) + { + this->m_dBusInterface->callDBus(QLatin1Literal("registerHotkeyActions"), actions, origin); + } + + void CContextApplicationProxy::callHotkeyAction(const QString &action, bool argument, const CIdentifier &origin) + { + this->m_dBusInterface->callDBus(QLatin1Literal("callHotkeyAction"), action, argument, origin); + } + BlackMisc::CIdentifier CContextApplicationProxy::registerApplication(const CIdentifier &application) { return this->m_dBusInterface->callDBusRet(QLatin1Literal("registerApplication"), application); @@ -101,9 +113,4 @@ namespace BlackCore return this->m_dBusInterface->callDBusRet(QLatin1Literal("existsFile"), fileName); } - void CContextApplicationProxy::processHotkeyFuncEvent(const BlackMisc::Event::CEventHotkeyFunction &event) - { - this->m_dBusInterface->callDBus(QLatin1Literal("processHotkeyFuncEvent"), event); - } - } // namespace diff --git a/src/blackcore/context_application_proxy.h b/src/blackcore/context_application_proxy.h index a326214de..22ee50469 100644 --- a/src/blackcore/context_application_proxy.h +++ b/src/blackcore/context_application_proxy.h @@ -31,6 +31,12 @@ namespace BlackCore //! \copydoc IContextApplication::changeSettings virtual void changeSettings(const BlackMisc::CVariantMap &settings, const BlackMisc::CIdentifier &origin) override; + //! \copydoc IContextApplication::registerHotkeyActions + virtual void registerHotkeyActions(const QStringList &actions, const BlackMisc::CIdentifier &origin) override; + + //! \copydoc IContextApplication::callHotkeyAction + virtual void callHotkeyAction(const QString &action, bool argument, const BlackMisc::CIdentifier &origin) override; + //! \copydoc IContextApplication::registerApplication virtual BlackMisc::CIdentifier registerApplication(const BlackMisc::CIdentifier &application) override; @@ -52,9 +58,6 @@ namespace BlackCore //! \copydoc IContextApplication::existsFile virtual bool existsFile(const QString &fileName) const override; - //! \copydoc IContextApplication::processHotkeyFuncEvent - virtual void processHotkeyFuncEvent(const BlackMisc::Event::CEventHotkeyFunction &event) override; - public: //! \todo Remove with old settings using IContextApplication::changeSettings;