2022-11-29 19:00:37 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2022-11-29 19:00:37 +00:00
|
|
|
*/
|
2025-01-24 00:11:17 +00:00
|
|
|
|
2025-08-20 20:37:02 +00:00
|
|
|
#include "VersionInfo.h"
|
2022-11-29 19:00:37 +00:00
|
|
|
#include "arch/Arch.h"
|
|
|
|
|
#include "base/EventQueue.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/Log.h"
|
2025-08-19 13:55:31 +00:00
|
|
|
#include "common/ExitCodes.h"
|
2024-09-17 19:00:25 +00:00
|
|
|
#include "deskflow/ClientApp.h"
|
|
|
|
|
#include "deskflow/ServerApp.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
#if SYSAPI_WIN32
|
|
|
|
|
#include "arch/win32/ArchMiscWindows.h"
|
2025-04-24 01:23:13 +00:00
|
|
|
#include <QCoreApplication>
|
2024-07-02 19:07:06 +00:00
|
|
|
#endif
|
2022-11-29 19:00:37 +00:00
|
|
|
|
2025-08-18 22:02:30 +00:00
|
|
|
#include <QSharedMemory>
|
2025-02-05 17:57:30 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2025-08-20 20:37:02 +00:00
|
|
|
const static auto kHeader = QStringLiteral("%1-core: %2\n").arg(kAppId, kDisplayVersion);
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void showHelp()
|
|
|
|
|
{
|
2025-08-20 20:43:35 +00:00
|
|
|
std::cout << qPrintable(kHeader) << qPrintable(kAppDescription) << "\n\n";
|
2025-02-05 17:57:30 +00:00
|
|
|
std::cout << "Usage: deskflow-core <server | client> [...options]" << std::endl;
|
|
|
|
|
std::cout << "server - start as a server (deskflow-server)" << std::endl;
|
|
|
|
|
std::cout << "client - start as a client (deskflow-client)" << std::endl;
|
2025-08-19 18:13:16 +00:00
|
|
|
|
|
|
|
|
ServerApp sApp(nullptr);
|
|
|
|
|
sApp.help();
|
|
|
|
|
|
|
|
|
|
ClientApp cApp(nullptr);
|
|
|
|
|
cApp.help();
|
2022-11-29 19:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-18 22:02:30 +00:00
|
|
|
bool isHelp(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
|
if (argv[i] == std::string("--help") || argv[i] == std::string("-h"))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 20:37:02 +00:00
|
|
|
void showVersion()
|
|
|
|
|
{
|
|
|
|
|
std::cout << qPrintable(kHeader) << qPrintable(kCopyright) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isVersion(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
|
if (argv[i] == std::string("--version") || argv[i] == std::string("-v"))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool isServer(int argc, char **argv)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return (argc > 1 && argv[1] == std::string("server"));
|
2022-11-29 19:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool isClient(int argc, char **argv)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return (argc > 1 && argv[1] == std::string("client"));
|
2022-11-29 19:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2025-08-18 22:02:30 +00:00
|
|
|
Arch arch;
|
|
|
|
|
arch.init();
|
|
|
|
|
|
|
|
|
|
Log log;
|
|
|
|
|
|
|
|
|
|
if (isHelp(argc, argv)) {
|
|
|
|
|
showHelp();
|
2025-08-19 13:55:31 +00:00
|
|
|
return s_exitSuccess;
|
2025-08-18 22:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 20:37:02 +00:00
|
|
|
if (isVersion(argc, argv)) {
|
|
|
|
|
showVersion();
|
|
|
|
|
return s_exitSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 22:02:30 +00:00
|
|
|
// Create a shared memory segment with a unique key
|
|
|
|
|
// This is to prevent a new instance from running if one is already running
|
|
|
|
|
QSharedMemory sharedMemory("deskflow-core");
|
|
|
|
|
|
|
|
|
|
// Attempt to attach first and detach in order to clean up stale shm chunks
|
|
|
|
|
// This can happen if the previous instance was killed or crashed
|
|
|
|
|
if (sharedMemory.attach())
|
|
|
|
|
sharedMemory.detach();
|
|
|
|
|
|
|
|
|
|
// If we can create 1 byte of SHM we are the only instance
|
|
|
|
|
if (!sharedMemory.create(1)) {
|
|
|
|
|
LOG_WARN("an instance of deskflow core (server or client) is already running");
|
2025-08-19 13:55:31 +00:00
|
|
|
return s_exitDuplicate;
|
2025-08-18 22:02:30 +00:00
|
|
|
}
|
2022-11-29 19:00:37 +00:00
|
|
|
#if SYSAPI_WIN32
|
2025-04-24 01:23:13 +00:00
|
|
|
// HACK to make sure settings gets the correct qApp path
|
|
|
|
|
QCoreApplication m(argc, argv);
|
|
|
|
|
m.deleteLater();
|
|
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
ArchMiscWindows::setInstanceWin32(GetModuleHandle(nullptr));
|
2022-11-29 19:00:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
EventQueue events;
|
2022-11-29 19:00:37 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (isServer(argc, argv)) {
|
2025-01-19 19:37:34 +00:00
|
|
|
ServerApp app(&events);
|
2024-07-02 19:07:06 +00:00
|
|
|
return app.run(argc, argv);
|
|
|
|
|
} else if (isClient(argc, argv)) {
|
2025-01-19 19:37:34 +00:00
|
|
|
ClientApp app(&events);
|
2024-07-02 19:07:06 +00:00
|
|
|
return app.run(argc, argv);
|
|
|
|
|
} else {
|
|
|
|
|
showHelp();
|
|
|
|
|
}
|
2022-11-29 19:00:37 +00:00
|
|
|
|
2025-08-19 13:55:31 +00:00
|
|
|
return s_exitSuccess;
|
2022-11-29 19:00:37 +00:00
|
|
|
}
|