2
* synergy -- mouse and keyboard sharing utility
3
* Copyright (C) 2002 Chris Schoeneman
5
* This package is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* found in the file COPYING that should have accompanied this file.
9
* This package is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
15
#ifndef CARCHDAEMONWINDOWS_H
16
#define CARCHDAEMONWINDOWS_H
18
#define WIN32_LEAN_AND_MEAN
20
#include "IArchDaemon.h"
21
#include "IArchMultithread.h"
22
#include "stdstring.h"
26
#define ARCH_DAEMON CArchDaemonWindows
28
//! Win32 implementation of IArchDaemon
29
class CArchDaemonWindows : public IArchDaemon {
31
typedef int (*RunFunc)(void);
34
virtual ~CArchDaemonWindows();
38
When the client calls \c daemonize(), the \c DaemonFunc should call this
39
function after initialization and argument parsing to perform the
40
daemon processing. The \c runFunc should perform the daemon's
41
main loop, calling \c daemonRunning(true) when it enters the main loop
42
(i.e. after initialization) and \c daemonRunning(false) when it leaves
43
the main loop. The \c runFunc is called in a new thread and when the
44
daemon must exit the main loop due to some external control the
45
thread is cancelled on behalf of the client. This function returns
46
what \c runFunc returns. \c runFunc should call \c daemonFailed() if
49
static int runDaemon(RunFunc runFunc);
51
//! Indicate daemon is in main loop
53
The \c runFunc passed to \c runDaemon() should call this function
54
to indicate when it has entered (\c running is \c true) or exited
55
(\c running is \c false) the main loop.
57
static void daemonRunning(bool running);
59
//! Indicate failure of running daemon
61
The \c runFunc passed to \c runDaemon() should call this function
62
to indicate failure. \c result is returned by \c daemonize().
64
static void daemonFailed(int result);
66
// IArchDaemon overrides
67
virtual void installDaemon(const char* name,
68
const char* description,
70
const char* commandLine,
72
virtual void uninstallDaemon(const char* name, bool allUsers);
73
virtual int daemonize(const char* name, DaemonFunc func);
74
virtual bool canInstallDaemon(const char* name, bool allUsers);
75
virtual bool isDaemonInstalled(const char* name, bool allUsers);
78
static HKEY openNTServicesKey();
79
static HKEY open95ServicesKey();
80
static HKEY openUserStartupKey();
82
int doRunDaemon(RunFunc runFunc);
83
void doDaemonRunning(bool running);
85
static void setStatus(DWORD state);
86
static void setStatus(DWORD state, DWORD step, DWORD waitHint);
87
static void setStatusError(DWORD error);
89
void* runDaemonThread(RunFunc);
90
static void* runDaemonThreadEntry(void*);
92
void serviceMain(DWORD, LPTSTR*);
93
static void WINAPI serviceMainEntry(DWORD, LPTSTR*);
95
void serviceHandler(DWORD ctrl);
96
static void WINAPI serviceHandlerEntry(DWORD ctrl);
99
class XArchDaemonRunFailed {
101
XArchDaemonRunFailed(int result) : m_result(result) { }
108
static CArchDaemonWindows* s_daemon;
110
CArchMutex m_serviceMutex;
111
CArchCond m_serviceCondVar;
112
DWORD m_serviceState;
113
bool m_serviceHandlerWaiting;
114
bool m_serviceRunning;
116
CArchThread m_daemonThread;
117
DaemonFunc m_daemonFunc;
120
SERVICE_STATUS_HANDLE m_statusHandle;