mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 13:36:48 +08:00
refs #453 Simplify hotkey action registration and relaying
This commit is contained in:
committed by
Mathew Sutcliffe
parent
199a1e5fcb
commit
6644c73703
51
src/blackcore/actionbind.h
Normal file
51
src/blackcore/actionbind.h
Normal file
@@ -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 <typename U>
|
||||
using MembFunc = void (U::*)(bool);
|
||||
|
||||
//! Constructor
|
||||
template <typename Receiver>
|
||||
CActionBind(const QString &action, Receiver *receiver, MembFunc<Receiver> 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
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
#include "blackcore/settingscache.h"
|
||||
#include "blackmisc/statusmessage.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QThread>
|
||||
|
||||
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<IContextSettings::SettingsType>(typeValue);
|
||||
|
||||
@@ -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 <QObject>
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<BlackMisc::CIdentifier>(QLatin1Literal("registerApplication"), application);
|
||||
@@ -101,9 +113,4 @@ namespace BlackCore
|
||||
return this->m_dBusInterface->callDBusRet<bool>(QLatin1Literal("existsFile"), fileName);
|
||||
}
|
||||
|
||||
void CContextApplicationProxy::processHotkeyFuncEvent(const BlackMisc::Event::CEventHotkeyFunction &event)
|
||||
{
|
||||
this->m_dBusInterface->callDBus(QLatin1Literal("processHotkeyFuncEvent"), event);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user