2002-09-02 17:30:04 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CLog.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "ILogOutputter.h"
|
|
|
|
#include "CArch.h"
|
|
|
|
#include "CStringUtil.h"
|
|
|
|
#include "XArch.h"
|
2002-09-02 17:30:04 +00:00
|
|
|
#include "CAutoStart.h"
|
|
|
|
#include "LaunchUtil.h"
|
|
|
|
#include "resource.h"
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
static const char* CLIENT_DAEMON_NAME = "Synergy Client";
|
|
|
|
static const char* SERVER_DAEMON_NAME = "Synergy Server";
|
|
|
|
static const char* CLIENT_DAEMON_INFO = "Uses a shared mouse and keyboard.";
|
|
|
|
static const char* SERVER_DAEMON_INFO = "Shares this system's mouse and keyboard with others.";
|
2002-09-02 17:30:04 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
//
|
|
|
|
// CAutoStartOutputter
|
|
|
|
//
|
|
|
|
// This class detects a message above a certain level and saves it
|
|
|
|
//
|
|
|
|
|
|
|
|
class CAutoStartOutputter : public ILogOutputter {
|
|
|
|
public:
|
|
|
|
CAutoStartOutputter(CString* msg) : m_msg(msg) { }
|
|
|
|
virtual ~CAutoStartOutputter() { }
|
|
|
|
|
|
|
|
// ILogOutputter overrides
|
|
|
|
virtual void open(const char*) { }
|
|
|
|
virtual void close() { }
|
2007-06-17 11:19:18 +00:00
|
|
|
virtual void show(bool) { }
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual bool write(ELevel level, const char* message);
|
|
|
|
virtual const char* getNewline() const { return ""; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CString* m_msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
CAutoStartOutputter::write(ELevel level, const char* message)
|
|
|
|
{
|
|
|
|
if (level <= CLog::kERROR) {
|
|
|
|
*m_msg = message;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
//
|
|
|
|
// CAutoStart
|
|
|
|
//
|
|
|
|
|
|
|
|
CAutoStart* CAutoStart::s_singleton = NULL;
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
CAutoStart::CAutoStart(HWND parent, bool isServer, const CString& cmdLine) :
|
2002-09-02 17:30:04 +00:00
|
|
|
m_parent(parent),
|
2007-06-17 11:19:18 +00:00
|
|
|
m_isServer(isServer),
|
2002-09-02 17:30:04 +00:00
|
|
|
m_cmdLine(cmdLine),
|
2007-06-17 11:19:18 +00:00
|
|
|
m_name(isServer ? SERVER_DAEMON_NAME : CLIENT_DAEMON_NAME)
|
2002-09-02 17:30:04 +00:00
|
|
|
{
|
|
|
|
assert(s_singleton == NULL);
|
|
|
|
s_singleton = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAutoStart::~CAutoStart()
|
|
|
|
{
|
|
|
|
s_singleton = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CAutoStart::doModal()
|
|
|
|
{
|
|
|
|
// install our log outputter
|
2003-01-04 22:01:32 +00:00
|
|
|
CLOG->insert(new CAutoStartOutputter(&m_errorMessage));
|
2002-09-02 17:30:04 +00:00
|
|
|
|
|
|
|
// do dialog
|
|
|
|
DialogBoxParam(s_instance, MAKEINTRESOURCE(IDD_AUTOSTART),
|
2007-09-09 20:48:03 +00:00
|
|
|
m_parent, (DLGPROC)dlgProc, (LPARAM)this);
|
2002-09-02 17:30:04 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// remove log outputter
|
|
|
|
CLOG->pop_front();
|
2002-09-02 17:30:04 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
void
|
|
|
|
CAutoStart::reinstallDaemon(bool isClient, const CString& cmdLine)
|
|
|
|
{
|
|
|
|
// get installation state
|
|
|
|
const char* name = (isClient ? CLIENT_DAEMON_NAME : SERVER_DAEMON_NAME);
|
|
|
|
bool installedSystem = ARCH->isDaemonInstalled(name, true);
|
|
|
|
bool installedUser = ARCH->isDaemonInstalled(name, false);
|
|
|
|
|
|
|
|
// reinstall if anything is installed
|
|
|
|
if (installedSystem || installedUser) {
|
|
|
|
ARCH->installDaemon(name,
|
|
|
|
isClient ? CLIENT_DAEMON_INFO : SERVER_DAEMON_INFO,
|
|
|
|
getAppPath(isClient ? CLIENT_APP : SERVER_APP).c_str(),
|
|
|
|
cmdLine.c_str(),
|
|
|
|
NULL,
|
|
|
|
installedSystem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CAutoStart::uninstallDaemons(bool client)
|
|
|
|
{
|
|
|
|
if (client) {
|
|
|
|
try {
|
|
|
|
ARCH->uninstallDaemon(CLIENT_DAEMON_NAME, true);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
ARCH->uninstallDaemon(CLIENT_DAEMON_NAME, false);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
ARCH->uninstallDaemon(SERVER_DAEMON_NAME, true);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
ARCH->uninstallDaemon(SERVER_DAEMON_NAME, false);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
bool
|
2007-06-17 11:19:18 +00:00
|
|
|
CAutoStart::startDaemon()
|
2002-09-02 17:30:04 +00:00
|
|
|
{
|
2007-06-17 11:19:18 +00:00
|
|
|
const char* name = NULL;
|
|
|
|
if (ARCH->isDaemonInstalled(CLIENT_DAEMON_NAME, true)) {
|
|
|
|
name = CLIENT_DAEMON_NAME;
|
|
|
|
}
|
|
|
|
else if (ARCH->isDaemonInstalled(SERVER_DAEMON_NAME, true)) {
|
|
|
|
name = SERVER_DAEMON_NAME;
|
|
|
|
}
|
|
|
|
if (name == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open service manager
|
|
|
|
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_READ);
|
|
|
|
if (mgr == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the service
|
|
|
|
SC_HANDLE service = OpenService(mgr, name, SERVICE_START);
|
|
|
|
if (service == NULL) {
|
|
|
|
CloseServiceHandle(mgr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the service
|
|
|
|
BOOL okay = StartService(service, 0, NULL);
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
CloseServiceHandle(service);
|
|
|
|
CloseServiceHandle(mgr);
|
|
|
|
|
|
|
|
return (okay != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CAutoStart::isDaemonInstalled()
|
|
|
|
{
|
|
|
|
return (ARCH->isDaemonInstalled(CLIENT_DAEMON_NAME, false) ||
|
|
|
|
ARCH->isDaemonInstalled(CLIENT_DAEMON_NAME, true) ||
|
|
|
|
ARCH->isDaemonInstalled(SERVER_DAEMON_NAME, false) ||
|
|
|
|
ARCH->isDaemonInstalled(SERVER_DAEMON_NAME, true));
|
2002-09-02 17:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CAutoStart::update()
|
|
|
|
{
|
|
|
|
// get installation state
|
2003-01-04 22:01:32 +00:00
|
|
|
const bool installedSystem = ARCH->isDaemonInstalled(
|
2002-09-02 17:30:04 +00:00
|
|
|
m_name.c_str(), true);
|
2003-01-04 22:01:32 +00:00
|
|
|
const bool installedUser = ARCH->isDaemonInstalled(
|
2002-09-02 17:30:04 +00:00
|
|
|
m_name.c_str(), false);
|
|
|
|
|
|
|
|
// get user's permissions
|
2003-01-04 22:01:32 +00:00
|
|
|
const bool canInstallSystem = ARCH->canInstallDaemon(
|
2002-09-02 17:30:04 +00:00
|
|
|
m_name.c_str(), true);
|
2003-01-04 22:01:32 +00:00
|
|
|
const bool canInstallUser = ARCH->canInstallDaemon(
|
2002-09-02 17:30:04 +00:00
|
|
|
m_name.c_str(), false);
|
|
|
|
|
|
|
|
// update messages
|
|
|
|
CString msg, label;
|
|
|
|
if (canInstallSystem) {
|
2004-03-28 14:05:31 +00:00
|
|
|
if (canInstallUser) {
|
|
|
|
msg = getString(IDS_AUTOSTART_PERMISSION_ALL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg = getString(IDS_AUTOSTART_PERMISSION_SYSTEM);
|
|
|
|
}
|
2002-09-02 17:30:04 +00:00
|
|
|
}
|
|
|
|
else if (canInstallUser) {
|
|
|
|
msg = getString(IDS_AUTOSTART_PERMISSION_USER);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg = getString(IDS_AUTOSTART_PERMISSION_NONE);
|
|
|
|
}
|
|
|
|
setWindowText(getItem(m_hwnd, IDC_AUTOSTART_PERMISSION_MSG), msg);
|
|
|
|
if (installedSystem) {
|
|
|
|
msg = getString(IDS_AUTOSTART_INSTALLED_SYSTEM);
|
|
|
|
label = getString(IDS_UNINSTALL_LABEL);
|
|
|
|
}
|
|
|
|
else if (installedUser) {
|
|
|
|
msg = getString(IDS_AUTOSTART_INSTALLED_USER);
|
|
|
|
label = getString(IDS_UNINSTALL_LABEL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg = getString(IDS_AUTOSTART_INSTALLED_NONE);
|
|
|
|
label = getString(IDS_INSTALL_LABEL);
|
|
|
|
}
|
|
|
|
setWindowText(getItem(m_hwnd, IDC_AUTOSTART_INSTALLED_MSG), msg);
|
|
|
|
|
|
|
|
// update buttons
|
|
|
|
setWindowText(getItem(m_hwnd, IDC_AUTOSTART_INSTALL_SYSTEM), label);
|
|
|
|
setWindowText(getItem(m_hwnd, IDC_AUTOSTART_INSTALL_USER), label);
|
|
|
|
if (installedSystem) {
|
|
|
|
enableItem(m_hwnd, IDC_AUTOSTART_INSTALL_SYSTEM, canInstallSystem);
|
|
|
|
enableItem(m_hwnd, IDC_AUTOSTART_INSTALL_USER, false);
|
|
|
|
m_install = false;
|
|
|
|
}
|
|
|
|
else if (installedUser) {
|
|
|
|
enableItem(m_hwnd, IDC_AUTOSTART_INSTALL_SYSTEM, false);
|
|
|
|
enableItem(m_hwnd, IDC_AUTOSTART_INSTALL_USER, canInstallUser);
|
|
|
|
m_install = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
enableItem(m_hwnd, IDC_AUTOSTART_INSTALL_SYSTEM, canInstallSystem);
|
|
|
|
enableItem(m_hwnd, IDC_AUTOSTART_INSTALL_USER, canInstallUser);
|
|
|
|
m_install = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CAutoStart::onInstall(bool allUsers)
|
|
|
|
{
|
|
|
|
if (!m_install) {
|
|
|
|
return onUninstall(allUsers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the app path
|
|
|
|
CString appPath = getAppPath(m_isServer ? SERVER_APP : CLIENT_APP);
|
|
|
|
|
|
|
|
// clear error message
|
|
|
|
m_errorMessage = "";
|
|
|
|
|
2004-06-10 19:39:07 +00:00
|
|
|
// install
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
ARCH->installDaemon(m_name.c_str(),
|
2002-09-02 17:30:04 +00:00
|
|
|
m_isServer ? SERVER_DAEMON_INFO : CLIENT_DAEMON_INFO,
|
2004-05-02 16:01:59 +00:00
|
|
|
appPath.c_str(), m_cmdLine.c_str(),
|
2004-06-10 19:39:07 +00:00
|
|
|
NULL, allUsers);
|
2003-01-04 22:01:32 +00:00
|
|
|
askOkay(m_hwnd, getString(IDS_INSTALL_TITLE),
|
|
|
|
getString(allUsers ?
|
|
|
|
IDS_INSTALLED_SYSTEM :
|
|
|
|
IDS_INSTALLED_USER));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (XArchDaemon& e) {
|
2002-09-02 17:30:04 +00:00
|
|
|
if (m_errorMessage.empty()) {
|
2003-01-04 22:01:32 +00:00
|
|
|
m_errorMessage = CStringUtil::format(
|
|
|
|
getString(IDS_INSTALL_GENERIC_ERROR).c_str(),
|
|
|
|
e.what().c_str());
|
2002-09-02 17:30:04 +00:00
|
|
|
}
|
|
|
|
showError(m_hwnd, m_errorMessage);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CAutoStart::onUninstall(bool allUsers)
|
|
|
|
{
|
|
|
|
// clear error message
|
|
|
|
m_errorMessage = "";
|
|
|
|
|
|
|
|
// uninstall
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
ARCH->uninstallDaemon(m_name.c_str(), allUsers);
|
|
|
|
askOkay(m_hwnd, getString(IDS_UNINSTALL_TITLE),
|
|
|
|
getString(allUsers ?
|
|
|
|
IDS_UNINSTALLED_SYSTEM :
|
|
|
|
IDS_UNINSTALLED_USER));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (XArchDaemon& e) {
|
2002-09-02 17:30:04 +00:00
|
|
|
if (m_errorMessage.empty()) {
|
2003-01-04 22:01:32 +00:00
|
|
|
m_errorMessage = CStringUtil::format(
|
|
|
|
getString(IDS_UNINSTALL_GENERIC_ERROR).c_str(),
|
|
|
|
e.what().c_str());
|
2002-09-02 17:30:04 +00:00
|
|
|
}
|
|
|
|
showError(m_hwnd, m_errorMessage);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
CAutoStart::doDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM)
|
|
|
|
{
|
|
|
|
switch (message) {
|
|
|
|
case WM_INITDIALOG:
|
|
|
|
// save our hwnd
|
|
|
|
m_hwnd = hwnd;
|
|
|
|
|
|
|
|
// update the controls
|
|
|
|
update();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case WM_COMMAND:
|
|
|
|
switch (LOWORD(wParam)) {
|
|
|
|
case IDC_AUTOSTART_INSTALL_SYSTEM:
|
|
|
|
onInstall(true);
|
|
|
|
update();
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case IDC_AUTOSTART_INSTALL_USER:
|
|
|
|
onInstall(false);
|
|
|
|
update();
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case IDCANCEL:
|
|
|
|
EndDialog(hwnd, 0);
|
|
|
|
m_hwnd = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CALLBACK
|
|
|
|
CAutoStart::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
return s_singleton->doDlgProc(hwnd, message, wParam, lParam);
|
|
|
|
}
|