forked from linuxdeepin/deepin-fcitx5configtool-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessmonitor.cpp
More file actions
113 lines (99 loc) · 4.27 KB
/
processmonitor.cpp
File metadata and controls
113 lines (99 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-FileCopyrightText: 2024 - 2027 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "processmonitor.h"
#include <QCoreApplication>
#include <QProcess>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(procMon, "fcitx5.helper.processmonitor")
const static QString PROCESS_NAME = "/usr/bin/fcitx5-start";
static QString getCurrentUserName() {
// qCDebug(procMon) << "Entering getCurrentUserName";
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
return env.value("USER");
}
bool ProcessMonitor::exeCommand(const QString &cmd, const QStringList &args, QString &output, QString &err) {
qCDebug(procMon) << "Entering exeCommand with command:" << cmd << "and args:" << args;
QProcess process;
process.setProgram(cmd);
process.setArguments(args);
process.start();
process.waitForFinished(-1);
output += process.readAllStandardOutput();
err += process.readAllStandardError();
bool success = (process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0);
if (!success) {
qCWarning(procMon) << "Command failed:" << cmd << args.join(" ") << "Error:" << err;
}
qCDebug(procMon) << "Exiting exeCommand with success:" << success;
return success;
}
void ProcessMonitor::checkFcitx5Process() {
// qCDebug(procMon) << "Entering checkFcitx5Process";
// Check if the fcitx5 process is already running
if (!m_previousProcessID.isEmpty() && QFile::exists("/proc/" + m_previousProcessID)) {
QString commandLine;
QFile file("/proc/" + m_previousProcessID + "/cmdline");
if (file.open(QIODevice::ReadOnly)) {
commandLine = file.readAll().trimmed();
file.close();
}
QStringList arguments = commandLine.split(QRegularExpression("[\\0\\s]+"));
if (!arguments.isEmpty()) {
QString executableName = QFileInfo(arguments.first()).fileName();
if (executableName == "fcitx5" || arguments.first() == "fcitx5") {
// qCDebug(procMon) << "fcitx5 is already running with PID:" << m_previousProcessID;
return;
}
}
qCDebug(procMon) << "Clearing invalid previous process ID";
m_previousProcessID.clear();
}
QString output, error;
exeCommand("pgrep", QStringList() << "-u" << getCurrentUserName() << "-x" << "fcitx5", output, error);
if (output.isEmpty()) {
qCDebug(procMon) << "fcitx5 not running, starting new process";
m_previousProcessID.clear();
QProcess process;
bool started = process.startDetached(PROCESS_NAME, QStringList() << "-d");
if (started) {
qCDebug(procMon) << "Successfully started fcitx5 process";
} else {
qCWarning(procMon) << "Failed to start fcitx5 process";
}
} else {
m_previousProcessID = output.trimmed();
qCDebug(procMon) << "Found running fcitx5 process with PID:" << m_previousProcessID;
}
qCDebug(procMon) << "Exiting checkFcitx5Process";
}
ProcessMonitor::ProcessMonitor(QObject *parent) : QObject(parent)
{
// qCDebug(procMon) << "Entering ProcessMonitor constructor";
connect(&m_timer, &QTimer::timeout, this, &ProcessMonitor::checkFcitx5Process);
QString output, error;
exeCommand("pgrep", QStringList() << "-u" << getCurrentUserName() << "-x" << "fcitx5", output, error);
if (!output.isEmpty()) {
QStringList pids = output.trimmed().split('\n', Qt::SkipEmptyParts);
for (const QString &pid : pids) {
qCDebug(procMon) << "Killing existing fcitx5 process with PID:" << pid;
QString killOutput, killError;
exeCommand("kill", QStringList() << pid.trimmed(), killOutput, killError);
}
}
QProcess process;
bool started = process.startDetached(PROCESS_NAME, QStringList() << "-d");
if (started) {
qCDebug(procMon) << "Successfully started fcitx5 process in constructor";
} else {
qCWarning(procMon) << "Failed to start fcitx5 process in constructor";
}
}
void ProcessMonitor::startMonitoring() {
// qCDebug(procMon) << "Entering startMonitoring";
m_timer.start(4000);
// qCDebug(procMon) << "Exiting startMonitoring";
}