Skip to content

Commit

Permalink
[fix] Fixed autostart function of lingmo-session (#6)
Browse files Browse the repository at this point in the history
* Added some comments

* [fix] Fixed autostart function of lingmo-session
  • Loading branch information
elysia-best authored Jul 8, 2024
1 parent 4994c66 commit a046f16
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 46 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ build/*
debian/.debhelper
debian/files
debian/lingmo-core*
obj-x86_64*
obj-x86_64*

.cache/
.idea/
4 changes: 4 additions & 0 deletions session/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
project(lingmo-session)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED 17)

set(TARGET lingmo-session)

set(SOURCES
Expand Down
2 changes: 2 additions & 0 deletions session/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Application::Application(int &argc, char **argv)
m_networkProxyManager->update();

QTimer::singleShot(50, this, &Application::updateUserDirs);

// Launch Lingmo and user defined processes !
QTimer::singleShot(100, m_processManager, &ProcessManager::start);
}

Expand Down
7 changes: 5 additions & 2 deletions session/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define APPLICATION_H

#include <QApplication>
#include <chrono>
#include <thread>

#include "processmanager.h"
#include "networkproxymanager.h"
Expand Down Expand Up @@ -54,11 +56,12 @@ public slots:
m_power.suspend();
}

void startDesktopProcess() {
[[maybe_unused]] void startDesktopProcess() {
// Start Lingmo Desktop Environment
m_processManager->startDesktopProcess();
}

void updateNetworkProxy() {
[[maybe_unused]] void updateNetworkProxy() {
m_networkProxyManager->update();
}

Expand Down
49 changes: 25 additions & 24 deletions session/daemon-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,45 @@
#include <QStringList>

namespace LINGMO_SESSION {
Daemon::Daemon(const QList<QPair<QString, QStringList>>& processList, QObject* parent)
: QObject(parent), m_processList(processList) {
for (const auto& processInfo : m_processList) {
startProcess(processInfo);
Daemon::Daemon(const QList<QPair<QString, QStringList>> &processList, bool _enableAutoStart, QObject *parent)
: QObject(parent), m_processList(processList), m_enableAutoRestart(_enableAutoStart) {
for (const auto &processInfo : m_processList) {
startProcess(processInfo);
}
}
}

void Daemon::onProcessError(QProcess::ProcessError error) {
auto process = qobject_cast<QProcess *>(sender());

void Daemon::onProcessError(QProcess::ProcessError error) {
QProcess* process = qobject_cast<QProcess*>(sender());
if (!process)
return;
return;

QString program = process->program();
qDebug() << "Process error:" << program << "Error:" << error;

for (const auto& processInfo : m_processList) {
if (processInfo.first == program) {
qDebug() << "Restarting process due to error:" << program;
QTimer::singleShot(1, this, [this, processInfo]() {
for (const auto &processInfo : m_processList) {
if (processInfo.first == program) {
qDebug() << "Restarting process due to error:" << program;
QTimer::singleShot(1, this, [this, processInfo]() {
startProcess(processInfo);
}); // Restart after 1 second
return;
}
}); // Restart after 1 second
return;
}
}
}
}

void Daemon::startProcess(const QPair<QString, QStringList>& processInfo) {
QProcess* process = new QProcess(this);
void Daemon::startProcess(const QPair<QString, QStringList> &processInfo) {
auto process = new QProcess(this);

connect(process, &QProcess::errorOccurred,
if (this->m_enableAutoRestart)
connect(process, &QProcess::errorOccurred,
this, &Daemon::onProcessError);

process->start(processInfo.first, processInfo.second);
if (process->waitForStarted()) {
qDebug() << "Process started:" << processInfo.first << "PID:" << process->processId();
}
else {
qDebug() << "Failed to start process:" << processInfo.first << process->errorString();
qDebug() << "Process started:" << processInfo.first << "PID:" << process->processId();
} else {
qDebug() << "Failed to start process:" << processInfo.first << process->errorString();
}
}
}
} // namespace LINGMO_SESSION
7 changes: 6 additions & 1 deletion session/daemon-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace LINGMO_SESSION {
* @param processList Process list to start
* @param parent
*/
Daemon(const QList<QPair<QString, QStringList>>& processList, QObject* parent = nullptr);
explicit Daemon(const QList<QPair<QString, QStringList>>& processList, bool _enableAutoStart = true, QObject* parent = nullptr);

public slots:

Expand All @@ -39,6 +39,11 @@ namespace LINGMO_SESSION {
void startProcess(const QPair<QString, QStringList>& processInfo);

QList<QPair<QString, QStringList>> m_processList;

/**
* @brief Whether to enable auto reload when process exited.
*/
bool m_enableAutoRestart;
};
}
#endif
44 changes: 26 additions & 18 deletions session/processmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void ProcessManager::logout()

void ProcessManager::startWindowManager()
{
QProcess *wmProcess = new QProcess;
auto *wmProcess = new QProcess;

wmProcess->start(m_app->wayland() ? "kwin_wayland" : "kwin_x11", QStringList());

Expand Down Expand Up @@ -127,7 +127,8 @@ void ProcessManager::startDaemonProcess()

void ProcessManager::loadAutoStartProcess()
{
QStringList execList;
QList<QPair<QString, QStringList>> list;

const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation,
QStringLiteral("autostart"),
QStandardPaths::LocateDirectory);
Expand All @@ -139,33 +140,40 @@ void ProcessManager::loadAutoStartProcess()
desktop.setIniCodec("UTF-8");
desktop.beginGroup("Desktop Entry");

if (desktop.contains("OnlyShowIn"))
continue;
// Ignore files the require a specific desktop environment
if (desktop.contains("NotShowIn")) {
const QStringList notShowIn = desktop.value("NotShowIn").toStringList();
if (notShowIn.contains("Lingmo"))
continue;
}
if (desktop.contains("OnlyShowIn")) {
const QStringList onlyShowIn = desktop.value("OnlyShowIn").toStringList();
if (!onlyShowIn.contains("Lingmo"))
continue;
}

const QString execValue = desktop.value("Exec").toString();

// 避免冲突
if (execValue.contains("gmenudbusmenuproxy"))
continue;

if (!execValue.isEmpty()) {
execList << execValue;
}
}
}
// 使用 QProcess::splitCommand 来解析命令和参数
QStringList args = QProcess::splitCommand(execValue);

for (const QString &exec : execList) {
QProcess *process = new QProcess;
process->setProgram(exec);
process->start();
process->waitForStarted();
// 检查是否至少有一个元素(即程序路径)
if (!args.isEmpty()) {
auto program = args.first();
args.removeFirst(); // 移除程序路径,剩下的都是参数

if (process->exitCode() == 0) {
m_autoStartProcess.insert(exec, process);
} else {
process->deleteLater();
list << qMakePair(program, args);
} else {
qWarning() << "Invalid 'Exec' found in file!";
}
}
}

m_userAutoStartD = std::make_shared<LINGMO_SESSION::Daemon>(list, false);
}

bool ProcessManager::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
Expand Down
5 changes: 5 additions & 0 deletions session/processmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef PROCESSMANAGER_H
#define PROCESSMANAGER_H

#include <QApplication>
#include <QAbstractNativeEventFilter>
#include <QObject>
#include <QProcess>
Expand Down Expand Up @@ -47,6 +48,7 @@ class ProcessManager : public QObject, public QAbstractNativeEventFilter

/**
* @brief Start the user defined autostart process.
* Typically, they are in <home>/.config/autostart/xxx.desktop
*/
void loadAutoStartProcess();

Expand All @@ -63,6 +65,9 @@ class ProcessManager : public QObject, public QAbstractNativeEventFilter
// Daemon helper for other daemon components
std::shared_ptr<LINGMO_SESSION::Daemon> m_daemonAutoStartD;

// Daemon helper for User Auto Start Process
std::shared_ptr<LINGMO_SESSION::Daemon> m_userAutoStartD;

bool m_wmStarted;
QEventLoop *m_waitLoop;
};
Expand Down

0 comments on commit a046f16

Please sign in to comment.