File: main.cpp

package info (click to toggle)
redshift-qt 0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 476 kB
  • sloc: cpp: 277; makefile: 4
file content (75 lines) | stat: -rw-r--r-- 1,851 bytes parent folder | download
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
#include "systemtray.h"

#include <QtWidgets/QApplication>
#include <QSharedMemory>
#include <QDebug>

#include <csignal>

static 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[])
{
    QStringList argsl = QStringList();
    bool varg = false;

    for (int i = 1; i < argc; ++i){
        if(std::string(argv[i]) == "-h" || std::string(argv[i]) == "--help"){
            qInfo() <<  "Please run `redshift -h` for help output.";
            exit(-1);
        }

        if(std::string(argv[i]) == "-v")
           varg = true;

        argsl.append(argv[i]);
    }
    if(!varg)
       argsl.append("-v");
       
    Q_INIT_RESOURCE(resources);

    QSharedMemory sharedMemoryLock("redshift-qt-lock");
    if (IsInstanceAlreadyRunning(sharedMemoryLock))
        return -1;

    QApplication a(argc, argv);
    a.setApplicationName("Redshift Qt");
    QApplication::setQuitOnLastWindowClosed(false);

    SystemTray tray;
    QApplication::connect(&a, &QApplication::aboutToQuit, &tray, &SystemTray::StopRedshift);

    if (!tray.CreateIcon())
        return 1;

    if (!tray.StartRedshift(argsl))
        return 1;

    globalTray = &tray;
    signal(SIGUSR1, handleSignal);

    return a.exec();
}