|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | + |
| 5 | +#include "desktopmonitor.h" |
| 6 | +#include "constants.h" |
| 7 | + |
| 8 | +#include <QDBusConnection> |
| 9 | +#include <QDBusInterface> |
| 10 | +#include <QDBusServiceWatcher> |
| 11 | +#include <QDBusConnectionInterface> |
| 12 | +#include <QTimer> |
| 13 | + |
| 14 | +using namespace network::sessionservice; |
| 15 | + |
| 16 | +#define SESSION_MANAG_SERVICE "com.deepin.SessionManager" |
| 17 | + |
| 18 | +DesktopMonitor::DesktopMonitor(QObject *parent) |
| 19 | + : QObject (parent) |
| 20 | + , m_prepare(false) |
| 21 | + , m_checkFinished(false) |
| 22 | + , m_timer(nullptr) |
| 23 | +{ |
| 24 | + init(); |
| 25 | +} |
| 26 | + |
| 27 | +bool DesktopMonitor::prepared() const |
| 28 | +{ |
| 29 | + return m_prepare && (!m_displayEnvironment.isEmpty() || m_checkFinished); |
| 30 | +} |
| 31 | + |
| 32 | +QString DesktopMonitor::displayEnvironment() const |
| 33 | +{ |
| 34 | + return m_displayEnvironment; |
| 35 | +} |
| 36 | + |
| 37 | +void DesktopMonitor::init() |
| 38 | +{ |
| 39 | + m_prepare = QDBusConnection::sessionBus().interface()->isServiceRegistered(SESSION_MANAG_SERVICE); |
| 40 | + qCDebug(DSM) << SESSION_MANAG_SERVICE << " prepare status" << m_prepare; |
| 41 | + if (m_prepare) { |
| 42 | + m_displayEnvironment = getDisplayEnvironment(); |
| 43 | + qCDebug(DSM) << "get display environment" << m_displayEnvironment; |
| 44 | + if (m_displayEnvironment.isEmpty()) { |
| 45 | + m_timer = new QTimer(this); |
| 46 | + m_timer->setInterval(1000); |
| 47 | + m_timer->setProperty("checktime", 0); |
| 48 | + connect(m_timer, &QTimer::timeout, this, &DesktopMonitor::onCheckTimeout); |
| 49 | + m_timer->start(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // 就算注销当前用户后,deepin-service-manager服务也不会退出,所以这里需要监控该服务的退出和启动的状态 |
| 54 | + QDBusServiceWatcher *serviceWatcher = new QDBusServiceWatcher(this); |
| 55 | + serviceWatcher->setConnection(QDBusConnection::sessionBus()); |
| 56 | + serviceWatcher->addWatchedService(SESSION_MANAG_SERVICE); |
| 57 | + connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &DesktopMonitor::onServiceRegistered); |
| 58 | + QDBusConnection::sessionBus().connect("com.deepin.SessionManager", "/com/deepin/SessionManager", "com.deepin.SessionManager", |
| 59 | + "PrepareLogout", this, SLOT(onPrepareLogout())); |
| 60 | +} |
| 61 | + |
| 62 | +QString DesktopMonitor::getDisplayEnvironment() const |
| 63 | +{ |
| 64 | + QString displayenv = qgetenv("DISPLAY"); |
| 65 | + if (!displayenv.isEmpty()) { |
| 66 | + qCDebug(DSM) << "get DISPLAY Environment from local env"; |
| 67 | + return displayenv; |
| 68 | + } |
| 69 | + |
| 70 | + qCDebug(DSM) << "get DISPLAY Environment from systemd1 service"; |
| 71 | + // 有时候获取到的环境变量为空,此时就需要从systemd接口中来获取环境变量了 |
| 72 | + QDBusInterface systemdInterface("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager"); |
| 73 | + QStringList environments = systemdInterface.property("Environment").toStringList(); |
| 74 | + for (const QString &env : environments) { |
| 75 | + QStringList envArray = env.split("="); |
| 76 | + if (envArray.size() < 2) |
| 77 | + continue; |
| 78 | + if (envArray.first() == "DISPLAY") |
| 79 | + return envArray[1]; |
| 80 | + } |
| 81 | + |
| 82 | + return QString(); |
| 83 | +} |
| 84 | + |
| 85 | +void DesktopMonitor::checkFinished() |
| 86 | +{ |
| 87 | + m_checkFinished = true; |
| 88 | + m_timer->stop(); |
| 89 | + m_timer->deleteLater(); |
| 90 | + m_timer = nullptr; |
| 91 | + if (prepared()) { |
| 92 | + qCDebug(DSM) << "display environment check finished"; |
| 93 | + emit desktopChanged(true); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +void DesktopMonitor::onCheckTimeout() |
| 98 | +{ |
| 99 | + m_displayEnvironment = getDisplayEnvironment(); |
| 100 | + if (m_displayEnvironment.isEmpty()) { |
| 101 | + // 连接检测20次,如果20次还是没有这个环境变量,就说明这个环境变量有问题,直接尝试打开 |
| 102 | + int checktime = m_timer->property("checktime").toInt(); |
| 103 | + qCDebug(DSM) << "in " << checktime << "times, the display environment is empty"; |
| 104 | + if (checktime >= 20) { |
| 105 | + checkFinished(); |
| 106 | + } else { |
| 107 | + checktime++; |
| 108 | + m_timer->setProperty("checktime", checktime); |
| 109 | + } |
| 110 | + } else { |
| 111 | + qCDebug(DSM) << "the display environment is not empty" << m_displayEnvironment; |
| 112 | + checkFinished(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void DesktopMonitor::onServiceRegistered(const QString &service) |
| 117 | +{ |
| 118 | + if (service != SESSION_MANAG_SERVICE) |
| 119 | + return; |
| 120 | + |
| 121 | + if (m_prepare && !m_displayEnvironment.isEmpty()) { |
| 122 | + // 如果之前是已经准备好的,无需再次发送信号,这种情况一般发生在kill掉startdde进程或者重启SessionManager服务 |
| 123 | + qCDebug(DSM) << "the prepare is true"; |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + m_prepare = true; |
| 128 | + m_displayEnvironment = getDisplayEnvironment(); |
| 129 | + qCInfo(DSM) << SESSION_MANAG_SERVICE << "start success, display envionment:" << m_displayEnvironment; |
| 130 | + if (prepared()) { |
| 131 | + qCDebug(DSM) << "desktop prepared"; |
| 132 | + emit desktopChanged(true); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void DesktopMonitor::onPrepareLogout() |
| 137 | +{ |
| 138 | + // 注销当前账户 |
| 139 | + m_prepare = false; |
| 140 | + qCDebug(DSM) << "current user is logout"; |
| 141 | + emit desktopChanged(false); |
| 142 | +} |
0 commit comments