-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (56 loc) · 2.17 KB
/
main.cpp
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
#include "widget.h"
#include <QApplication>
#include <QSharedMemory>
#include <QTimer>
#include <QtDBus>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSharedMemory sharedMemory("dereshi-player", &a);
if(!sharedMemory.attach()){
if(sharedMemory.create(1)){
QObject::connect(&a, &QApplication::aboutToQuit, &a, [&sharedMemory]() {
sharedMemory.detach();
// qInfo() << "detach when close ";
});
// char* ch = static_cast<char*>(sharedMemory.data());
// sharedMemory.lock();
// *ch = 1;
// sharedMemory.unlock();
Widget w;
// QTimer timer;
// QObject::connect(&timer, &QTimer::timeout, &a, [&sharedMemory, &w]() {
// char *ch = static_cast<char*>(sharedMemory.data());
// qInfo() << "shared: "<< QString::fromUtf8(ch);
// if (*ch == 0) {
// // qInfo() << "another process ";
// w.showAndActivate();
// sharedMemory.lock();
// *ch = 1;
// sharedMemory.unlock();
// }
// });
// timer.start(500);
QDBusConnection::sessionBus().registerObject("/com/player/dereshi", &w, QDBusConnection::ExportAllSlots);
QDBusConnection::sessionBus().registerService("com.player.dereshi");
w.show();
return a.exec();
}else {
QMessageBox::critical(nullptr, "Error", "cann't create a single instance");
a.quit();
return 1;
}
}else {
// char* ch = static_cast<char*>(sharedMemory.data());
// qInfo() << "instance: " << QString::fromUtf8(ch);
// sharedMemory.lock();
// *ch = 0;
// sharedMemory.unlock();
QDBusInterface interface("com.player.dereshi", "/com/player/dereshi", "", QDBusConnection::sessionBus());
interface.call("showAndActivate");
sharedMemory.detach();
// QMessageBox::critical(nullptr, "warning", "there is one dereshi player is running");
a.quit();
return 0;
}
}