2 #include <QApplication>
4 #include <QSharedMemory>
5 #include <QCommandLineParser>
20 #define SHMEM_SIZE 128 * 1024
21 #define SHMEM_SIZE_V015 16
43 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions);
47 const QCommandLineOption versionOption = parser.addVersionOption();
49 const QCommandLineOption helpOption = parser.addHelpOption();
51 const QCommandLineOption backgroundOption(QStringList() <<
"b" <<
"background",
52 "Starts in background, without displaying the main window.");
53 parser.addOption(backgroundOption);
55 const QCommandLineOption closeOption(QStringList() <<
"c" <<
"close",
56 "Causes already running instance (if any) to exit.");
57 parser.addOption(closeOption);
60 if (!parser.parse(QCoreApplication::arguments())) {
62 *errorMessage = parser.errorText();
67 if (parser.isSet(versionOption)) {
72 if (parser.isSet(helpOption)) {
77 if (parser.isSet(backgroundOption)) {
82 if (parser.isSet(closeOption)) {
93 foreach(
const QString& line, lines){
94 if(line.startsWith(
"PID ")){
98 if((pid = line.split(
" ")[1].toLong(&ok)) > 0 && ok){
100 return (kill(pid, 0) == 0 || errno != ESRCH);
115 bool running =
false;
125 QStringList lines = QString((
const char*)
appShare.constData()).split(
"\n");
130 lines.append(QString(
"Option ") + command);
131 QByteArray newMem = lines.join(
"\n").left(
SHMEM_SIZE).toLatin1();
133 memcpy(
appShare.data(), newMem.constData(), newMem.length() + 1);
143 snprintf((
char*)
appShare.data(),
appShare.size(),
"PID %ld", (long)getpid());
148 int main(
int argc,
char *argv[]){
150 QApplication a(argc, argv);
153 QCoreApplication::setOrganizationName(
"ckb");
154 QCoreApplication::setApplicationVersion(CKB_VERSION_STR);
155 QCoreApplication::setApplicationName(
"ckb");
158 QCommandLineParser parser;
159 QString errorMessage;
160 parser.setApplicationDescription(
"Open Source Corsair Input Device Driver for Linux and OSX.");
165 printf(
"The ckb GUI should not be run as root.\n");
170 qsrand(QDateTime::currentMSecsSinceEpoch());
174 FILE *fp = fopen(
"/tmp/ckb",
"w");
175 fprintf(fp,
"%d", getpid());
178 #if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
180 qApp->setAttribute(Qt::AA_UseHighDpiPixmaps);
189 fputs(qPrintable(errorMessage), stderr);
190 fputs(
"\n\n", stderr);
191 fputs(qPrintable(parser.helpText()), stderr);
195 printf(
"%s %s\n", qPrintable(QCoreApplication::applicationName()),
196 qPrintable(QCoreApplication::applicationVersion()));
205 printf(
"Asking existing instance to close.\n");
207 printf(
"ckb is not running.\n");
216 if(qApp->isSessionRestored())
219 printf(
"ckb is already running. Exiting.\n");
QSharedMemory appShare("ckb")
static bool isRunning(const char *command)
int main(int argc, char *argv[])
CommandLineParseResults parseCommandLine(QCommandLineParser &parser, QString *errorMessage)
parseCommandLine - Setup options and parse command line arguments.
static bool pidActive(const QStringList &lines)