2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-02-24 13:17:22 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016, 2025 Symless Ltd.
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 Nick Bolton
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-02-06 18:13:54 +00:00
|
|
|
#include "arch/Arch.h"
|
2025-03-05 18:09:25 +00:00
|
|
|
#include "base/EventQueue.h"
|
2025-02-06 18:13:54 +00:00
|
|
|
#include "base/Log.h"
|
2025-02-07 12:50:25 +00:00
|
|
|
#include "common/constants.h"
|
2025-03-05 17:17:07 +00:00
|
|
|
#include "common/version.h"
|
2024-09-17 19:00:25 +00:00
|
|
|
#include "deskflow/DaemonApp.h"
|
2025-02-07 17:03:26 +00:00
|
|
|
#include "deskflow/ipc/DaemonIpcServer.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2025-02-28 12:46:12 +00:00
|
|
|
#if SYSAPI_WIN32
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-02-06 12:03:51 +00:00
|
|
|
#include "arch/win32/ArchMiscWindows.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
2025-02-06 12:03:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
2025-02-06 18:13:54 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
2025-02-10 12:58:23 +00:00
|
|
|
using namespace deskflow::core;
|
|
|
|
|
|
2025-02-24 13:17:22 +00:00
|
|
|
void handleError(const char *message);
|
|
|
|
|
|
2025-02-06 12:03:51 +00:00
|
|
|
int main(int argc, char **argv)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-02-28 12:46:12 +00:00
|
|
|
#if SYSAPI_WIN32
|
2025-03-06 12:36:14 +00:00
|
|
|
ArchMiscWindows::guardRuntimeVersion();
|
|
|
|
|
|
2025-02-06 18:13:54 +00:00
|
|
|
// Save window instance for later use, e.g. `GetModuleFileName` which is used when installing the daemon.
|
2025-02-28 12:46:12 +00:00
|
|
|
ArchMiscWindows::setInstanceWin32(GetModuleHandle(nullptr));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
Arch arch;
|
|
|
|
|
arch.init();
|
|
|
|
|
|
|
|
|
|
Log log;
|
|
|
|
|
EventQueue events;
|
|
|
|
|
|
2025-02-10 17:59:43 +00:00
|
|
|
auto &daemon = DaemonApp::instance();
|
2025-02-24 13:17:22 +00:00
|
|
|
DaemonApp::InitResult initResult;
|
|
|
|
|
try {
|
|
|
|
|
initResult = daemon.init(&events, argc, argv);
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
|
handleError(e.what());
|
|
|
|
|
return kExitFailed;
|
|
|
|
|
} catch (...) {
|
|
|
|
|
handleError("Unrecognized error.");
|
|
|
|
|
return kExitFailed;
|
|
|
|
|
}
|
2025-02-07 17:03:26 +00:00
|
|
|
|
2025-03-04 10:40:31 +00:00
|
|
|
// Important: Log the app name and version number to the log file after the daemon app init
|
|
|
|
|
// because the file log outputter is created there. Logging before would only log to stdout
|
|
|
|
|
// which is not useful for troubleshooting Windows services.
|
|
|
|
|
// It's important to write the version number to the log file so we can be certain the old daemon
|
|
|
|
|
// was uninstalled, since sometimes Windows services can get stuck and fail to be removed.
|
2025-03-05 17:17:07 +00:00
|
|
|
LOG_PRINT("%s Daemon v%s", kAppName, displayVersion().toStdString().c_str());
|
2025-03-04 10:40:31 +00:00
|
|
|
|
2025-02-07 17:03:26 +00:00
|
|
|
switch (initResult) {
|
|
|
|
|
using enum DaemonApp::InitResult;
|
|
|
|
|
|
|
|
|
|
case StartDaemon: {
|
2025-02-24 14:05:02 +00:00
|
|
|
LOG_INFO("starting daemon");
|
2025-02-07 17:03:26 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
|
2025-02-10 17:59:43 +00:00
|
|
|
QThread daemonThread;
|
|
|
|
|
daemon.moveToThread(&daemonThread);
|
|
|
|
|
|
|
|
|
|
QObject::connect(&daemonThread, &QThread::started, [&daemon, &daemonThread]() {
|
2025-02-24 16:37:21 +00:00
|
|
|
LOG_DEBUG("daemon thread started");
|
2025-02-10 17:59:43 +00:00
|
|
|
daemon.run();
|
|
|
|
|
daemonThread.quit();
|
2025-02-24 16:37:21 +00:00
|
|
|
LOG_DEBUG("daemon thread finished");
|
2025-02-10 17:59:43 +00:00
|
|
|
});
|
|
|
|
|
QObject::connect(&daemonThread, &QThread::finished, &app, &QCoreApplication::quit);
|
2025-02-07 17:03:26 +00:00
|
|
|
|
2025-02-10 17:59:43 +00:00
|
|
|
ipc::DaemonIpcServer ipcServer(&app, QString::fromStdString(daemon.logFilename()));
|
2025-02-07 17:03:26 +00:00
|
|
|
|
2025-02-10 17:59:43 +00:00
|
|
|
// Use direct connection as the daemon app is on it's own thread, and so is on a different event loop.
|
2025-02-07 17:03:26 +00:00
|
|
|
QObject::connect(
|
2025-02-10 17:59:43 +00:00
|
|
|
&ipcServer, &ipc::DaemonIpcServer::logLevelChanged, &daemon, &DaemonApp::saveLogLevel, //
|
2025-02-07 17:03:26 +00:00
|
|
|
Qt::DirectConnection
|
|
|
|
|
);
|
|
|
|
|
QObject::connect(
|
2025-02-10 17:59:43 +00:00
|
|
|
&ipcServer, &ipc::DaemonIpcServer::elevateModeChanged, &daemon, &DaemonApp::setElevate, //
|
2025-02-07 17:03:26 +00:00
|
|
|
Qt::DirectConnection
|
|
|
|
|
);
|
|
|
|
|
QObject::connect(
|
2025-02-10 17:59:43 +00:00
|
|
|
&ipcServer, &ipc::DaemonIpcServer::commandChanged, &daemon, &DaemonApp::setCommand, //
|
2025-02-07 17:03:26 +00:00
|
|
|
Qt::DirectConnection
|
|
|
|
|
);
|
|
|
|
|
QObject::connect(
|
2025-02-10 17:59:43 +00:00
|
|
|
&ipcServer, &ipc::DaemonIpcServer::startProcessRequested, &daemon, &DaemonApp::applyWatchdogCommand, //
|
2025-02-07 17:03:26 +00:00
|
|
|
Qt::DirectConnection
|
|
|
|
|
);
|
2025-02-10 11:22:04 +00:00
|
|
|
QObject::connect(
|
2025-02-10 17:59:43 +00:00
|
|
|
&ipcServer, &ipc::DaemonIpcServer::stopProcessRequested, &daemon, &DaemonApp::clearWatchdogCommand, //
|
2025-02-10 11:22:04 +00:00
|
|
|
Qt::DirectConnection
|
|
|
|
|
);
|
2025-03-03 18:01:32 +00:00
|
|
|
QObject::connect(
|
|
|
|
|
&ipcServer, &ipc::DaemonIpcServer::clearSettingsRequested, &daemon, &DaemonApp::clearSettings, //
|
|
|
|
|
Qt::DirectConnection
|
|
|
|
|
);
|
2025-02-10 11:22:04 +00:00
|
|
|
|
2025-02-10 17:59:43 +00:00
|
|
|
daemonThread.start();
|
|
|
|
|
const auto exitCode = QCoreApplication::exec();
|
|
|
|
|
daemonThread.wait();
|
|
|
|
|
|
|
|
|
|
LOG_DEBUG("daemon exited, code: %d", exitCode);
|
|
|
|
|
return exitCode;
|
2025-02-07 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case FatalError:
|
|
|
|
|
return kExitFailed;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return kExitSuccess;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-06 12:03:51 +00:00
|
|
|
#if SYSAPI_WIN32
|
|
|
|
|
|
2025-02-07 17:03:26 +00:00
|
|
|
// Win32 subsystem entry point (simply forwards to main).
|
|
|
|
|
// We need this because using regular main under the Win32 subsystem results in empty args.
|
2025-02-06 12:03:51 +00:00
|
|
|
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
|
|
|
|
{
|
|
|
|
|
return main(__argc, __argv);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2025-02-24 13:17:22 +00:00
|
|
|
|
|
|
|
|
void handleError(const char *message)
|
|
|
|
|
{
|
|
|
|
|
// Always print error to stdout in case run as CLI program.
|
|
|
|
|
LOG_ERR("%s", message);
|
|
|
|
|
|
|
|
|
|
#if SYSAPI_WIN32
|
|
|
|
|
// Show a message box for when run from MSI in Win32 subsystem.
|
|
|
|
|
MessageBoxA(nullptr, message, "Deskflow daemon error", MB_OK | MB_ICONERROR);
|
|
|
|
|
#endif
|
|
|
|
|
}
|