forked from Chemrat/redshift-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
57 lines (42 loc) · 1.33 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
#include "systemtray.h"
#include <QtWidgets/QApplication>
#include <QSharedMemory>
#include <QDebug>
#include <csignal>
SystemTray *globalTray = nullptr;
void handleSignal(int signum)
{
if (signum == SIGUSR1)
globalTray->onSuspend();
}
bool IsInstanceAlreadyRunning(QSharedMemory &memoryLock) {
if (!memoryLock.create(1)) {
qDebug() << "Failed to create shared memory lock, an instance of redshift-qt might be already running\n"
"or it was not terminated correctly. Trying to clean up the lock...";
memoryLock.attach();
memoryLock.detach();
if (!memoryLock.create(1)) {
qDebug() << "An insance of redshift-qt is already running, exiting...";
return true;
}
}
return false;
}
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(resources);
QSharedMemory sharedMemoryLock("redshift-qt-lock");
if (IsInstanceAlreadyRunning(sharedMemoryLock))
return -1;
QApplication a(argc, argv);
QApplication::setQuitOnLastWindowClosed(false);
SystemTray tray;
QApplication::connect(&a, &QApplication::aboutToQuit, &tray, &SystemTray::StopRedshift);
if (!tray.CreateIcon())
return 1;
if (!tray.StartRedshift())
return 1;
globalTray = &tray;
signal(SIGUSR1, handleSignal);
return a.exec();
}