2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
#ifndef CWIN32PLATFORM_H
|
|
|
|
#define CWIN32PLATFORM_H
|
|
|
|
|
|
|
|
#include "IPlatform.h"
|
2002-06-08 21:48:00 +00:00
|
|
|
#include "CCondVar.h"
|
|
|
|
#include "CMutex.h"
|
2002-06-10 16:49:46 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2002-06-08 21:48:00 +00:00
|
|
|
#include <windows.h>
|
2002-06-04 12:26:23 +00:00
|
|
|
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Microsoft windows platform dependent functions
|
2002-06-04 12:26:23 +00:00
|
|
|
class CWin32Platform : public IPlatform {
|
|
|
|
public:
|
2002-06-08 21:48:00 +00:00
|
|
|
typedef int (*RunFunc)(CMutex*);
|
|
|
|
typedef void (*StopFunc)(void);
|
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
CWin32Platform();
|
|
|
|
virtual ~CWin32Platform();
|
|
|
|
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Test if windows 95, et al.
|
|
|
|
/*!
|
|
|
|
Returns true iff the platform is win95/98/me.
|
|
|
|
*/
|
2002-06-08 21:48:00 +00:00
|
|
|
static bool isWindows95Family();
|
|
|
|
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Utility for calling SetServiceStatus()
|
2002-06-08 21:48:00 +00:00
|
|
|
static void setStatus(SERVICE_STATUS_HANDLE, DWORD state);
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Utility for calling SetServiceStatus()
|
2002-06-08 21:48:00 +00:00
|
|
|
static void setStatus(SERVICE_STATUS_HANDLE,
|
2002-06-10 22:06:45 +00:00
|
|
|
DWORD state, DWORD step, DWORD waitHint);
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Utility for calling SetServiceStatus()
|
2002-06-08 21:48:00 +00:00
|
|
|
static void setStatusError(SERVICE_STATUS_HANDLE, DWORD error);
|
|
|
|
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Run service
|
|
|
|
/*!
|
|
|
|
Run a service. The RunFunc should unlock the passed in mutex
|
|
|
|
(which will be locked on entry) when not initializing or
|
|
|
|
shutting down (i.e. when running its loop). StopFunc should
|
|
|
|
cause the RunFunc() to return. Returns what RunFunc returns.
|
|
|
|
RunFunc should throw CDaemonFailed if the service fails.
|
|
|
|
*/
|
2002-06-08 21:48:00 +00:00
|
|
|
int runDaemon(RunFunc, StopFunc);
|
|
|
|
|
2002-07-29 17:03:55 +00:00
|
|
|
//! Daemon failed exception
|
|
|
|
/*!
|
|
|
|
Thrown by RunFunc on service failure. Result is the error
|
|
|
|
code reported by the service.
|
|
|
|
*/
|
2002-06-08 21:48:00 +00:00
|
|
|
class CDaemonFailed {
|
|
|
|
public:
|
|
|
|
CDaemonFailed(int result) : m_result(result) { }
|
|
|
|
|
|
|
|
public:
|
|
|
|
int m_result;
|
|
|
|
};
|
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
// IPlatform overrides
|
2002-06-08 21:48:00 +00:00
|
|
|
virtual bool installDaemon(const char* name,
|
2002-06-10 22:06:45 +00:00
|
|
|
const char* description,
|
|
|
|
const char* pathname,
|
|
|
|
const char* commandLine);
|
2002-06-14 18:08:20 +00:00
|
|
|
virtual EResult uninstallDaemon(const char* name);
|
2002-06-08 21:48:00 +00:00
|
|
|
virtual int daemonize(const char* name, DaemonFunc);
|
2002-06-14 18:08:20 +00:00
|
|
|
virtual void installDaemonLogger(const char* name);
|
2002-06-04 12:26:23 +00:00
|
|
|
virtual const char* getBasename(const char* pathname) const;
|
|
|
|
virtual CString getUserDirectory() const;
|
|
|
|
virtual CString getSystemDirectory() const;
|
|
|
|
virtual CString addPathComponent(
|
2002-06-10 22:06:45 +00:00
|
|
|
const CString& prefix,
|
|
|
|
const CString& suffix) const;
|
2002-06-04 12:26:23 +00:00
|
|
|
|
|
|
|
private:
|
2002-06-08 21:48:00 +00:00
|
|
|
static HKEY openKey(HKEY parent, const char*);
|
|
|
|
static HKEY openKey(HKEY parent, const char**);
|
|
|
|
static void closeKey(HKEY);
|
|
|
|
static void deleteKey(HKEY, const char* name);
|
|
|
|
static void deleteValue(HKEY, const char* name);
|
|
|
|
static void setValue(HKEY, const char* name,
|
2002-06-10 22:06:45 +00:00
|
|
|
const CString& value);
|
2002-06-08 21:48:00 +00:00
|
|
|
static CString readValueString(HKEY, const char* name);
|
|
|
|
static HKEY openNTServicesKey();
|
|
|
|
static HKEY open95ServicesKey();
|
|
|
|
|
|
|
|
void serviceMain(DWORD, LPTSTR*);
|
|
|
|
static void WINAPI serviceMainEntry(DWORD, LPTSTR*);
|
|
|
|
|
2002-07-24 19:26:18 +00:00
|
|
|
void runDaemonThread(void*);
|
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
void serviceHandler(DWORD ctrl);
|
|
|
|
static void WINAPI serviceHandlerEntry(DWORD ctrl);
|
|
|
|
|
|
|
|
static bool serviceLogger(int, const char*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DaemonFunc m_daemonFunc;
|
|
|
|
int m_daemonResult;
|
|
|
|
|
|
|
|
SERVICE_STATUS_HANDLE m_statusHandle;
|
|
|
|
|
|
|
|
CMutex* m_serviceMutex;
|
|
|
|
CCondVar<DWORD>* m_serviceState;
|
|
|
|
bool m_serviceHandlerWaiting;
|
|
|
|
bool m_serviceRunning;
|
|
|
|
StopFunc m_stop;
|
|
|
|
|
|
|
|
static HANDLE s_eventLog;
|
|
|
|
|
|
|
|
static CWin32Platform* s_daemonPlatform;
|
2002-06-04 12:26:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|