fix(dbus): 修复多display开始菜单唤起错误问题

This commit is contained in:
hewenfei 2023-09-12 10:24:28 +08:00
parent 64035e88dc
commit 0cc72ae13a
6 changed files with 188 additions and 15 deletions

View File

@ -121,6 +121,13 @@ set(SOURCE_FILES
src/settings/user-config.cpp src/settings/user-config.h src/settings/user-config.cpp src/settings/user-config.h
) )
if(COMMAND qt_add_dbus_adaptor)
qt_add_dbus_adaptor(SOURCE_FILES data/org.ukui.menu.xml menu-dbus-service.h UkuiMenu::MenuDbusService)
else()
qt5_add_dbus_adaptor(SOURCE_FILES data/org.ukui.menu.xml menu-dbus-service.h UkuiMenu::MenuDbusService)
endif()
# library sources # library sources
set(LIBRARY_SOURCES set(LIBRARY_SOURCES
src/data-entity.cpp src/data-entity.cpp

16
data/org.ukui.menu.xml Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.ukui.menu">
<method name="WinKeyResponse"/>
<method name="ReloadSecurityConfig"/>
<method name="GetSecurityConfigPath">
<arg type="s" direction="out"/>
</method>
<method name="active">
<arg name="display" type="s" direction="in"/>
</method>
<signal name="activeRequest">
<arg name="display" type="s" direction="out"/>
</signal>
</interface>
</node>

View File

@ -149,6 +149,7 @@ int main(int argc, char *argv[])
QString appid = QString("ukui-menu-%1").arg(display); QString appid = QString("ukui-menu-%1").arg(display);
qDebug() << "ukui-menu launch with:" << display << "appid:" << appid; qDebug() << "ukui-menu launch with:" << display << "appid:" << appid;
QtSingleApplication app(appid, argc, argv); QtSingleApplication app(appid, argc, argv);
QGuiApplication::instance()->setProperty("display", display);
QTranslator translator; QTranslator translator;
QString translationFile{(QString(UKUI_MENU_TRANSLATION_DIR) + "/ukui-menu_" + QLocale::system().name() + ".qm")}; QString translationFile{(QString(UKUI_MENU_TRANSLATION_DIR) + "/ukui-menu_" + QLocale::system().name() + ".qm")};

View File

@ -16,25 +16,69 @@
* *
*/ */
#include <QDir>
#include "menu-dbus-service.h" #include "menu-dbus-service.h"
#include "menuadaptor.h"
#include <KWindowSystem>
#include <QDir>
#include <QDebug>
#include <QDBusConnection>
#include <QDBusServiceWatcher>
#include <QDBusConnectionInterface>
#define MENU_CORE_DBUS_SERVICE "org.ukui.menu" #define MENU_CORE_DBUS_SERVICE "org.ukui.menu"
#define MENU_CORE_DBUS_PATH "/org/ukui/menu" #define MENU_CORE_DBUS_PATH "/org/ukui/menu"
#define MENU_CORE_DBUS_INTERFACE "org.ukui.menu"
using namespace UkuiMenu; using namespace UkuiMenu;
MenuDbusService::MenuDbusService(QObject *parent) : QObject(parent) QString MenuDbusService::displayFromPid(uint pid)
{ {
QDBusConnection::sessionBus().unregisterService(MENU_CORE_DBUS_SERVICE); QFile environFile(QStringLiteral("/proc/%1/environ").arg(QString::number(pid)));
QDBusConnection::sessionBus().registerService(MENU_CORE_DBUS_SERVICE); if (environFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
//注册对象路径,导出所有此对象的插槽 ,registerObject参数路径interface,对象options const QByteArray DISPLAY = KWindowSystem::isPlatformWayland() ? QByteArrayLiteral("WAYLAND_DISPLAY")
QDBusConnection::sessionBus().registerObject(MENU_CORE_DBUS_PATH, this, QDBusConnection::ExportAllSlots); : QByteArrayLiteral("DISPLAY");
const auto lines = environFile.readAll().split('\0');
for (const QByteArray &line : lines) {
const int equalsIdx = line.indexOf('=');
if (equalsIdx <= 0) {
continue;
}
const QByteArray key = line.left(equalsIdx);
if (key == DISPLAY) {
const QByteArray value = line.mid(equalsIdx + 1);
return value;
}
}
}
return {};
}
MenuDbusService::MenuDbusService(const QString &display, QObject *parent) : QObject(parent), m_display(display)
{
bool isServiceRegistered = QDBusConnection::sessionBus().interface()->isServiceRegistered(MENU_CORE_DBUS_SERVICE);
qDebug() << "menu service is registered:" << isServiceRegistered << ", display:" << display;
if (isServiceRegistered) {
initWatcher();
} else {
bool success = registerService();
if (!success) {
initWatcher();
}
qDebug() << "menu service register:" << success;
}
} }
void MenuDbusService::WinKeyResponse() void MenuDbusService::WinKeyResponse()
{ {
Q_EMIT menuActive(); uint callerPid = QDBusConnection::sessionBus().interface()->servicePid(message().service()).value();
QString display = MenuDbusService::displayFromPid(callerPid);
active(display);
} }
QString MenuDbusService::GetSecurityConfigPath() QString MenuDbusService::GetSecurityConfigPath()
@ -47,3 +91,90 @@ void MenuDbusService::ReloadSecurityConfig()
{ {
Q_EMIT reloadConfigSignal(); Q_EMIT reloadConfigSignal();
} }
void MenuDbusService::active(const QString &display)
{
if (display.isEmpty() || (display == m_display)) {
Q_EMIT menuActive();
return;
}
if (m_menuAdaptor) {
Q_EMIT m_menuAdaptor->activeRequest(display);
}
}
void MenuDbusService::activeMenu(QString display)
{
if (display == m_display) {
Q_EMIT menuActive();
}
}
bool MenuDbusService::registerService()
{
m_menuAdaptor = new MenuAdaptor(this);
QDBusReply<QDBusConnectionInterface::RegisterServiceReply> reply =
QDBusConnection::sessionBus().interface()->registerService(MENU_CORE_DBUS_SERVICE,
QDBusConnectionInterface::ReplaceExistingService,
QDBusConnectionInterface::DontAllowReplacement);
if (reply.value() == QDBusConnectionInterface::ServiceNotRegistered) {
return false;
}
bool res = QDBusConnection::sessionBus().registerObject(MENU_CORE_DBUS_PATH, this);
if (!res) {
QDBusConnection::sessionBus().interface()->unregisterService(MENU_CORE_DBUS_SERVICE);
}
return res;
}
void MenuDbusService::onServiceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner)
{
qDebug() << "serviceOwnerChanged:" << service << oldOwner << newOwner;
if (newOwner.isEmpty()) {
bool success = registerService();
if (success) {
disConnectActiveRequest();
delete m_watcher;
m_watcher = nullptr;
}
qDebug() << "try to register service:" << success;
return;
}
uint newOwnerPid = QDBusConnection::sessionBus().interface()->servicePid(newOwner);
qDebug() << "newOwnerPid:" << newOwnerPid << ", myPid:" << QCoreApplication::applicationPid() << ", display:" << m_display;
// if (newOwnerPid == QCoreApplication::applicationPid()) {
// qDebug() << "Becoming a new service";
// }
}
void MenuDbusService::connectToActiveRequest()
{
QDBusConnection::sessionBus().connect(MENU_CORE_DBUS_SERVICE,
MENU_CORE_DBUS_PATH,
MENU_CORE_DBUS_INTERFACE,
"activeRequest",
this,
SLOT(activeMenu(QString)));
}
void MenuDbusService::disConnectActiveRequest()
{
QDBusConnection::sessionBus().disconnect(MENU_CORE_DBUS_SERVICE,
MENU_CORE_DBUS_PATH,
MENU_CORE_DBUS_INTERFACE,
"activeRequest",
this,
SLOT(activeMenu(QString)));
}
void MenuDbusService::initWatcher()
{
m_watcher = new QDBusServiceWatcher(MENU_CORE_DBUS_SERVICE,QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange);
connect(m_watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &MenuDbusService::onServiceOwnerChanged);
connectToActiveRequest();
}

View File

@ -20,27 +20,45 @@
#define MENU_DBUS_SERVICE_H #define MENU_DBUS_SERVICE_H
#include <QObject> #include <QObject>
#include <QDBusInterface> #include <QDBusContext>
#include <QDBusConnection>
class MenuAdaptor;
class QDBusServiceWatcher;
namespace UkuiMenu { namespace UkuiMenu {
class MenuDbusService : public QObject class MenuDbusService : public QObject, public QDBusContext
{ {
Q_OBJECT Q_OBJECT
//申明该类有D-BUS服务接口
Q_CLASSINFO("D-Bus Interface", "org.ukui.menu")
public: public:
explicit MenuDbusService(QObject *parent = nullptr); //传入父指针,调用 explicit MenuDbusService(const QString& display, QObject *parent = nullptr);
public Q_SLOTS: public Q_SLOTS:
void WinKeyResponse(); void WinKeyResponse();
QString GetSecurityConfigPath(); QString GetSecurityConfigPath();
void ReloadSecurityConfig(); void ReloadSecurityConfig();
void active(const QString &display);
Q_SIGNALS: Q_SIGNALS:
void menuActive(); void menuActive();
void reloadConfigSignal(); void reloadConfigSignal();
private Q_SLOTS:
void activeMenu(QString display);
void onServiceOwnerChanged(const QString &service, const QString &oldOwner, const QString &newOwner);
private:
static QString displayFromPid(uint pid);
bool registerService();
void initWatcher();
void connectToActiveRequest();
void disConnectActiveRequest();
private:
QString m_display {};
MenuAdaptor *m_menuAdaptor {nullptr};
QDBusServiceWatcher *m_watcher {nullptr};
}; };
} }

View File

@ -32,7 +32,7 @@
#include "menu-manager.h" #include "menu-manager.h"
#include "data-provider-manager.h" #include "data-provider-manager.h"
#include <QCoreApplication> #include <QGuiApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QQmlContext> #include <QQmlContext>
#include <QQmlEngine> #include <QQmlEngine>
@ -127,7 +127,7 @@ void UkuiMenuApplication::loadMenuUI()
void UkuiMenuApplication::initDbusService() void UkuiMenuApplication::initDbusService()
{ {
m_menuDbusService = new MenuDbusService(this); m_menuDbusService = new MenuDbusService(QGuiApplication::instance()->property("display").toString(), this);
if (m_menuDbusService) { if (m_menuDbusService) {
connect(m_menuDbusService, &MenuDbusService::menuActive, this, [this] { connect(m_menuDbusService, &MenuDbusService::menuActive, this, [this] {
execCommand(Active); execCommand(Active);