Skip to content

Commit 4484e13

Browse files
committed
Rewrite runningCheck and print config before running
1 parent a7c25bf commit 4484e13

File tree

6 files changed

+73
-41
lines changed

6 files changed

+73
-41
lines changed

configuration.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ const QSize &configuration::getWidgetSize() const {
8383
void configuration::setWidgetSize(const QSize &widgetSize) {
8484
configuration::widgetSize = widgetSize;
8585
}
86+
87+
QString configuration::toString() {
88+
QString result = "";
89+
result += QString("HideOnHover: ") + B2S(this->isHideOnHover()) + '\n';
90+
result += QString("WidgetOnLeft: ") + B2S(this->isWidgetOnLeft()) + '\n';
91+
result += QString("MouseSensibility: ") + N2S(this->getMouseSensibility()) + '\n';
92+
result += QString("WidgetHeight: ") + N2S(this->getWidgetSize().height()) + '\n';
93+
result += QString("WidgetWidth: ") + N2S(this->getWidgetSize().width()) + '\n';
94+
result += QString("ModelName: ") + this->getModelName() + '\n';
95+
result += QString("ResourceDir: ") + this->getResourceDir() + '\n';
96+
return result;
97+
}

configuration.h

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <QApplication>
1010
#include <QSize>
1111

12+
#define B2S(boolean) QString(boolean ? "true" : "false")
13+
#define N2S(number) QString("%1").arg(number)
14+
1215
class configuration {
1316
public:
1417
configuration();
@@ -40,6 +43,8 @@ class configuration {
4043

4144
void setWidgetSize(const QSize &widgetSize);
4245

46+
QString toString();
47+
4348
private:
4449
bool hideOnHover;
4550
QString resourceDir;

main.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@
22
#include "runningCheck.h"
33
#include "configuration.h"
44
#include <QApplication>
5-
#include <string.h>
65

76
int main(int argc, char *argv[]) {
87
QApplication a(argc, argv);
8+
configuration config;
9+
Widget w(config);
10+
QObject::connect(&a, &QApplication::aboutToQuit, &w, []() {
11+
deleteLock();
12+
});
913
if (check()) {
1014
std::perror("already running!");
1115
return -1;
1216
}
13-
configuration config;
14-
Widget w(config);
15-
// cout << "argc: " << argc << endl;
16-
// if (argc == 3) {
17-
// w.setModel(argv[1], argv[2]);
18-
// }
19-
// if (argc == 4) {
20-
// w.setModel(argv[1], argv[2]);
21-
// w.setWidgetPosition(strcmp("left", argv[3]) == 0);
22-
// }
2317
w.show();
2418
return QApplication::exec();
2519
}

runningCheck.cpp

+42-25
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,51 @@ static QByteArray calcHash(QString procDir) {
1717
return hash.result();
1818
}
1919

20-
static bool isDigit(QString s) {
21-
for (auto i : s) {
22-
if (!isdigit(i.toLatin1())) {
20+
bool check() {
21+
QFile lockFile = QFile(LOCK_FILE);
22+
if (! lockFile.exists()) {
23+
if (!lockFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::NewOnly)) {
24+
QMessageBox::critical(nullptr, "Error", "Cannot open lock file!");
25+
QCoreApplication::exit(1);
26+
}
27+
lockFile.write(QByteArray().fromStdString(std::to_string(THIS_PID)));
28+
lockFile.close();
29+
return false;
30+
} else {
31+
// if the lock file exists, there are 2 situations:
32+
// 1. another process is running
33+
// 2. another process didn't exit properly and didn't delete the lock file
34+
// So we need to check if the pid in the lock file is exists and represents a QDesktopPet instance.
35+
if (!lockFile.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
36+
QMessageBox::critical(nullptr, "Error", "Cannot open lock file!");
37+
QCoreApplication::exit(1);
38+
}
39+
int pid = lockFile.readLine().toInt();
40+
QDir processDir = QDir(QString("/proc/$1").arg(pid));
41+
if (! processDir.exists()) {
42+
// situation 2 and the process doesn't exist. we just need to update the pid
43+
lockFile.write(QByteArray().fromStdString(std::to_string(THIS_PID)));
44+
lockFile.close();
2345
return false;
46+
} else {
47+
// check if the process is a QDesktopPet instance
48+
QDir thisProcess = QDir(QString("/proc/$1").arg(THIS_PID));
49+
if (calcHash(thisProcess.absolutePath()) == calcHash(processDir.absolutePath())) {
50+
// situation 1, process exists and process is a QDesktopPet instance
51+
lockFile.close();
52+
return true;
53+
} else {
54+
// situation 2 and the process is not a QDesktopPet instance
55+
// update the pid and continue
56+
lockFile.write(QByteArray().fromStdString(std::to_string(THIS_PID)));
57+
lockFile.close();
58+
return false;
59+
}
2460
}
2561
}
26-
return true;
2762
}
2863

29-
bool check() {
30-
qint64 selfPid = QCoreApplication::applicationPid();
31-
std::cout << selfPid << std::endl;
32-
QString procDir = "/proc/";
33-
QString selfProcDir = procDir + QString::number(selfPid);
34-
QByteArray selfMD5 = calcHash(selfProcDir);
35-
QDirIterator iter("/proc", QDirIterator::Subdirectories);
36-
while (iter.hasNext()) {
37-
QString aProcDir = iter.next();
38-
if (isDigit(aProcDir.right(aProcDir.lastIndexOf('/')))) {
39-
int pid = aProcDir.right(aProcDir.lastIndexOf('/')).toInt();
40-
if (pid > 50 && pid < selfPid) {
41-
QByteArray procHash = calcHash(aProcDir);
42-
if (procHash == selfMD5) {
43-
std::cout<< aProcDir.toStdString() << std::endl;
44-
return true;
45-
}
46-
}
47-
}
48-
}
49-
return false;
64+
void deleteLock() {
65+
QFile lockFile = QFile(LOCK_FILE);
66+
lockFile.remove();
5067
}

runningCheck.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
#include <QByteArray>
1111
#include <QCoreApplication>
1212
#include <QDirIterator>
13+
#include <QMessageBox>
14+
#include <QTextStream>
15+
16+
#define LOCK_FILE QDir::homePath() + "/.pet_running"
17+
#define THIS_PID QCoreApplication::applicationPid()
1318

14-
static QByteArray calcHash(QString);
15-
static bool isDigit(QString s);
1619
bool check();
20+
void deleteLock();
21+
static QByteArray calcHash(QString);
1722

1823
#endif //QDESKTOPPET_2_CMAKE_RUNNINGCHECK_H

widget.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,13 @@ Widget::~Widget() {
9191
}
9292

9393
void Widget::live2dInitialized(QLive2dWidget *wid) {
94-
cout << "Starting with model " << this->currentConfiguration.getModelName().toStdString() << " in " << this->currentConfiguration.getResourceDir().toStdString() << "." << endl;
94+
cout << "Starting Live2D Widget with configuration: " << endl;
95+
cout << QTS(this->currentConfiguration.toString());
9596
wid->setResDir(this->currentConfiguration.getResourceDir().toStdString());
9697
wid->setModel(this->currentConfiguration.getModelName().toStdString());
9798
this->initialized = true;
9899
}
99100
void Widget::mouseEvent(QPoint rel, QPoint raw) {
100-
//cout<<"rel: "<<rel.x()<<", "<<rel.y()<<endl;
101-
//cout<<"abs: "<<raw.x()<<", "<<raw.y()<<endl;
102101
widget->mouseMove(rel);
103102
//widget->mouseMove(rel);
104103
if (this->currentConfiguration.isHideOnHover()) {

0 commit comments

Comments
 (0)