forked from jahnf/Projecteur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
111 lines (101 loc) · 3.99 KB
/
main.cc
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
// This file is part of Projecteur - https://github.com/jahnf/projecteur - See LICENSE.md and README.md
#include "projecteurapp.h"
#include "projecteur-GitVersion.h"
#include "runguard.h"
#include <QCommandLineParser>
#ifndef NDEBUG
#include <QQmlDebuggingEnabler>
#endif
#include <iostream>
namespace {
struct print {
template<typename T>
auto& operator<<(const T& a) const { return std::cout << a; }
~print() { std::cout << std::endl; }
};
struct error {
template<typename T>
auto& operator<<(const T& a) const { return std::cerr << a; }
~error() { std::cerr << std::endl; }
};
}
int main(int argc, char *argv[])
{
QCoreApplication::setApplicationName("Projecteur");
QCoreApplication::setApplicationVersion(projecteur::version_string());
QString ipcCommand;
{
QCommandLineParser parser;
parser.setApplicationDescription("Linux/X11 application for the Logitech Spotlight device.");
const QCommandLineOption versionOption(QStringList{ "v", "version"}, "Print application version.");
const QCommandLineOption fullVersionOption(QStringList{ "f", "fullversion" });
const QCommandLineOption helpOption(QStringList{ "h", "help"}, "Show command line usage.");
const QCommandLineOption commandOption(QStringList{ "c", "command"}, "Send command to a running instance.", "cmd");
parser.addOptions({versionOption, helpOption, commandOption, fullVersionOption});
QStringList args;
for(int i = 0; i < argc; ++i) {
args.push_back(argv[i]);
}
parser.process(args);
if (parser.isSet(helpOption))
{
print() << QCoreApplication::applicationName().toStdString() << " "
<< projecteur::version_string() << std::endl;
print() << "Usage: projecteur [option]" << std::endl;
print() << "<Options>";
print() << " -h, --help " << helpOption.description().toStdString();
print() << " -v, --version " << versionOption.description().toStdString();
print() << " -c COMMAND " << commandOption.description().toStdString() << std::endl;
print() << "<Commands>";
print() << " spot=[on|off] Turn spotlight on/off.";
print() << " settings=[show|hide] Show/hide preferences dialog.";
print() << " quit Quit the running instance.";
return 0;
}
else if (parser.isSet(versionOption) || parser.isSet(fullVersionOption))
{
print() << QCoreApplication::applicationName().toStdString() << " "
<< projecteur::version_string();
if (parser.isSet(fullVersionOption) ||
(std::string(projecteur::version_branch()) != "master" &&
std::string(projecteur::version_branch()) != "not-within-git-repo"))
{ // Not a build from master branch, print out additional information:
print() << " - git-branch: " << projecteur::version_branch();
print() << " - git-hash: " << projecteur::version_fullhash();
}
// Show if we have a build from modified sources
if (projecteur::version_isdirty())
print() << " - dirty-flag: " << projecteur::version_isdirty();
return 0;
}
else if (parser.isSet(commandOption))
{
ipcCommand = parser.value(commandOption);
if (ipcCommand.isEmpty()) {
error() << "Command cannot be an empty string.";
return 44;
}
}
}
RunGuard guard(QCoreApplication::applicationName());
if (!guard.tryToRun())
{
if (ipcCommand.size())
{
return ProjecteurCommandClientApp(ipcCommand, argc, argv).exec();
}
else {
error() << "Another application instance is already running. Exiting.";
return 42;
}
}
else if (ipcCommand.size())
{
// No other application instance running - but command option was used.
error() << "Cannot send command '" << ipcCommand.toStdString() << "' - no running application instance found.";
return 43;
}
//QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
ProjecteurApplication app(argc, argv);
return app.exec();
}