mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-27 06:12:17 +08:00
Reorganized source tree. Moved client.cpp into cmd/synergy as
synergy.cpp and server.cpp into cmd/synergyd as synergyd.cpp. Moved and renamed related files. Moved remaining source files into lib/.... Modified and added makefiles as appropriate. Result is that library files are under lib with each library in its own directory and program files are under cmd with each command in its own directory.
This commit is contained in:
147
lib/platform/CMSWindowsClipboard.cpp
Normal file
147
lib/platform/CMSWindowsClipboard.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#include "CMSWindowsClipboard.h"
|
||||
#include "CMSWindowsClipboardTextConverter.h"
|
||||
#include "CMSWindowsClipboardUTF16Converter.h"
|
||||
#include "CLog.h"
|
||||
|
||||
//
|
||||
// CMSWindowsClipboard
|
||||
//
|
||||
|
||||
CMSWindowsClipboard::CMSWindowsClipboard(HWND window) :
|
||||
m_window(window),
|
||||
m_time(0)
|
||||
{
|
||||
// add converters, most desired first
|
||||
m_converters.push_back(new CMSWindowsClipboardUTF16Converter);
|
||||
m_converters.push_back(new CMSWindowsClipboardTextConverter);
|
||||
}
|
||||
|
||||
CMSWindowsClipboard::~CMSWindowsClipboard()
|
||||
{
|
||||
clearConverters();
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsClipboard::empty()
|
||||
{
|
||||
log((CLOG_DEBUG "empty clipboard"));
|
||||
|
||||
if (!EmptyClipboard()) {
|
||||
log((CLOG_DEBUG "failed to grab clipboard"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsClipboard::add(EFormat format, const CString& data)
|
||||
{
|
||||
log((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format));
|
||||
|
||||
// convert data to win32 form
|
||||
for (ConverterList::const_iterator index = m_converters.begin();
|
||||
index != m_converters.end(); ++index) {
|
||||
IMSWindowsClipboardConverter* converter = *index;
|
||||
|
||||
// skip converters for other formats
|
||||
if (converter->getFormat() == format) {
|
||||
HANDLE win32Data = converter->fromIClipboard(data);
|
||||
if (win32Data != NULL) {
|
||||
UINT win32Format = converter->getWin32Format();
|
||||
if (SetClipboardData(win32Format, win32Data) == NULL) {
|
||||
// free converted data if we couldn't put it on
|
||||
// the clipboard
|
||||
GlobalFree(win32Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsClipboard::open(Time time) const
|
||||
{
|
||||
log((CLOG_DEBUG "open clipboard"));
|
||||
|
||||
if (!OpenClipboard(m_window)) {
|
||||
log((CLOG_WARN "failed to open clipboard"));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_time = time;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsClipboard::close() const
|
||||
{
|
||||
log((CLOG_DEBUG "close clipboard"));
|
||||
CloseClipboard();
|
||||
}
|
||||
|
||||
IClipboard::Time
|
||||
CMSWindowsClipboard::getTime() const
|
||||
{
|
||||
return m_time;
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsClipboard::has(EFormat format) const
|
||||
{
|
||||
for (ConverterList::const_iterator index = m_converters.begin();
|
||||
index != m_converters.end(); ++index) {
|
||||
IMSWindowsClipboardConverter* converter = *index;
|
||||
if (converter->getFormat() == format) {
|
||||
if (IsClipboardFormatAvailable(converter->getWin32Format())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboard::get(EFormat format) const
|
||||
{
|
||||
// find the converter for the first clipboard format we can handle
|
||||
IMSWindowsClipboardConverter* converter = NULL;
|
||||
UINT win32Format = EnumClipboardFormats(0);
|
||||
while (converter == NULL && win32Format != 0) {
|
||||
for (ConverterList::const_iterator index = m_converters.begin();
|
||||
index != m_converters.end(); ++index) {
|
||||
converter = *index;
|
||||
if (converter->getWin32Format() == win32Format &&
|
||||
converter->getFormat() == format) {
|
||||
break;
|
||||
}
|
||||
converter = NULL;
|
||||
}
|
||||
win32Format = EnumClipboardFormats(win32Format);
|
||||
}
|
||||
|
||||
// if no converter then we don't recognize any formats
|
||||
if (converter == NULL) {
|
||||
return CString();
|
||||
}
|
||||
|
||||
// get a handle to the clipboard data
|
||||
HANDLE win32Data = GetClipboardData(converter->getWin32Format());
|
||||
if (win32Data == NULL) {
|
||||
return CString();
|
||||
}
|
||||
|
||||
// convert
|
||||
return converter->toIClipboard(win32Data);
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsClipboard::clearConverters()
|
||||
{
|
||||
for (ConverterList::iterator index = m_converters.begin();
|
||||
index != m_converters.end(); ++index) {
|
||||
delete *index;
|
||||
}
|
||||
m_converters.clear();
|
||||
}
|
||||
69
lib/platform/CMSWindowsClipboard.h
Normal file
69
lib/platform/CMSWindowsClipboard.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#ifndef CMSWINDOWSCLIPBOARD_H
|
||||
#define CMSWINDOWSCLIPBOARD_H
|
||||
|
||||
#include "IClipboard.h"
|
||||
#include "stdvector.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
class IMSWindowsClipboardConverter;
|
||||
|
||||
//! Microsoft windows clipboard implementation
|
||||
class CMSWindowsClipboard : public IClipboard {
|
||||
public:
|
||||
CMSWindowsClipboard(HWND window);
|
||||
virtual ~CMSWindowsClipboard();
|
||||
|
||||
// IClipboard overrides
|
||||
virtual bool empty();
|
||||
virtual void add(EFormat, const CString& data);
|
||||
virtual bool open(Time) const;
|
||||
virtual void close() const;
|
||||
virtual Time getTime() const;
|
||||
virtual bool has(EFormat) const;
|
||||
virtual CString get(EFormat) const;
|
||||
|
||||
private:
|
||||
void clearConverters();
|
||||
|
||||
UINT convertFormatToWin32(EFormat) const;
|
||||
HANDLE convertTextToWin32(const CString& data) const;
|
||||
CString convertTextFromWin32(HANDLE) const;
|
||||
|
||||
private:
|
||||
typedef std::vector<IMSWindowsClipboardConverter*> ConverterList;
|
||||
|
||||
HWND m_window;
|
||||
mutable Time m_time;
|
||||
ConverterList m_converters;
|
||||
};
|
||||
|
||||
//! Clipboard format converter interface
|
||||
/*!
|
||||
This interface defines the methods common to all win32 clipboard format
|
||||
converters.
|
||||
*/
|
||||
class IMSWindowsClipboardConverter : public IInterface {
|
||||
public:
|
||||
// accessors
|
||||
|
||||
// return the clipboard format this object converts from/to
|
||||
virtual IClipboard::EFormat
|
||||
getFormat() const = 0;
|
||||
|
||||
// return the atom representing the win32 clipboard format that
|
||||
// this object converts from/to
|
||||
virtual UINT getWin32Format() const = 0;
|
||||
|
||||
// convert from the IClipboard format to the win32 clipboard format.
|
||||
// the input data must be in the IClipboard format returned by
|
||||
// getFormat(). the return data will be in the win32 clipboard
|
||||
// format returned by getWin32Format(), allocated by GlobalAlloc().
|
||||
virtual HANDLE fromIClipboard(const CString&) const = 0;
|
||||
|
||||
// convert from the win32 clipboard format to the IClipboard format
|
||||
// (i.e., the reverse of fromIClipboard()).
|
||||
virtual CString toIClipboard(HANDLE data) const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
131
lib/platform/CMSWindowsClipboardAnyTextConverter.cpp
Executable file
131
lib/platform/CMSWindowsClipboardAnyTextConverter.cpp
Executable file
@@ -0,0 +1,131 @@
|
||||
#include "CMSWindowsClipboardAnyTextConverter.h"
|
||||
|
||||
//
|
||||
// CMSWindowsClipboardAnyTextConverter
|
||||
//
|
||||
|
||||
CMSWindowsClipboardAnyTextConverter::CMSWindowsClipboardAnyTextConverter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CMSWindowsClipboardAnyTextConverter::~CMSWindowsClipboardAnyTextConverter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
IClipboard::EFormat
|
||||
CMSWindowsClipboardAnyTextConverter::getFormat() const
|
||||
{
|
||||
return IClipboard::kText;
|
||||
}
|
||||
|
||||
HANDLE
|
||||
CMSWindowsClipboardAnyTextConverter::fromIClipboard(const CString& data) const
|
||||
{
|
||||
// convert linefeeds and then convert to desired encoding
|
||||
CString text = doFromIClipboard(convertLinefeedToWin32(data));
|
||||
UInt32 size = text.size();
|
||||
|
||||
// copy to memory handle
|
||||
HGLOBAL gData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
|
||||
if (gData != NULL) {
|
||||
// get a pointer to the allocated memory
|
||||
char* dst = (char*)GlobalLock(gData);
|
||||
if (dst != NULL) {
|
||||
memcpy(dst, text.data(), size);
|
||||
GlobalUnlock(gData);
|
||||
}
|
||||
else {
|
||||
GlobalFree(gData);
|
||||
gData = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return gData;
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardAnyTextConverter::toIClipboard(HANDLE data) const
|
||||
{
|
||||
// get datator
|
||||
const char* src = (const char*)GlobalLock(data);
|
||||
UInt32 srcSize = (UInt32)GlobalSize(data);
|
||||
if (src == NULL || srcSize <= 1) {
|
||||
return CString();
|
||||
}
|
||||
|
||||
// convert text
|
||||
CString text = doToIClipboard(CString(src, srcSize));
|
||||
|
||||
// release handle
|
||||
GlobalUnlock(data);
|
||||
|
||||
// convert newlines
|
||||
return convertLinefeedToUnix(text);
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardAnyTextConverter::convertLinefeedToWin32(
|
||||
const CString& src) const
|
||||
{
|
||||
// note -- we assume src is a valid UTF-8 string
|
||||
|
||||
// count newlines in string
|
||||
UInt32 numNewlines = 0;
|
||||
UInt32 n = src.size();
|
||||
for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
||||
if (*scan == '\n') {
|
||||
++numNewlines;
|
||||
}
|
||||
}
|
||||
if (numNewlines == 0) {
|
||||
return src;
|
||||
}
|
||||
|
||||
// allocate new string
|
||||
CString dst;
|
||||
dst.reserve(src.size() + numNewlines);
|
||||
|
||||
// copy string, converting newlines
|
||||
n = src.size();
|
||||
for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
||||
if (scan[0] == '\n') {
|
||||
dst += '\r';
|
||||
}
|
||||
dst += scan[0];
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardAnyTextConverter::convertLinefeedToUnix(
|
||||
const CString& src) const
|
||||
{
|
||||
// count newlines in string
|
||||
UInt32 numNewlines = 0;
|
||||
UInt32 n = src.size();
|
||||
for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
||||
if (scan[0] == '\r' && scan[1] == '\n') {
|
||||
++numNewlines;
|
||||
}
|
||||
}
|
||||
if (numNewlines == 0) {
|
||||
return src;
|
||||
}
|
||||
|
||||
// allocate new string
|
||||
CString dst;
|
||||
dst.reserve(src.size());
|
||||
|
||||
// copy string, converting newlines
|
||||
n = src.size();
|
||||
for (const char* scan = src.c_str(); n > 0; ++scan, --n) {
|
||||
if (scan[0] != '\r' || scan[1] != '\n') {
|
||||
dst += scan[0];
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
42
lib/platform/CMSWindowsClipboardAnyTextConverter.h
Executable file
42
lib/platform/CMSWindowsClipboardAnyTextConverter.h
Executable file
@@ -0,0 +1,42 @@
|
||||
#ifndef CMSWINDOWSCLIPBOARDANYTEXTCONVERTER_H
|
||||
#define CMSWINDOWSCLIPBOARDANYTEXTCONVERTER_H
|
||||
|
||||
#include "CMSWindowsClipboard.h"
|
||||
|
||||
//! Convert to/from some text encoding
|
||||
class CMSWindowsClipboardAnyTextConverter :
|
||||
public IMSWindowsClipboardConverter {
|
||||
public:
|
||||
CMSWindowsClipboardAnyTextConverter();
|
||||
virtual ~CMSWindowsClipboardAnyTextConverter();
|
||||
|
||||
// IMSWindowsClipboardConverter overrides
|
||||
virtual IClipboard::EFormat
|
||||
getFormat() const;
|
||||
virtual UINT getWin32Format() const = 0;
|
||||
virtual HANDLE fromIClipboard(const CString&) const;
|
||||
virtual CString toIClipboard(HANDLE) const;
|
||||
|
||||
protected:
|
||||
//! Convert from IClipboard format
|
||||
/*!
|
||||
Do UTF-8 conversion only. Memory handle allocation and
|
||||
linefeed conversion is done by this class. doFromIClipboard()
|
||||
must include the nul terminator in the returned string (not
|
||||
including the CString's nul terminator).
|
||||
*/
|
||||
virtual CString doFromIClipboard(const CString&) const = 0;
|
||||
|
||||
//! Convert to IClipboard format
|
||||
/*!
|
||||
Do UTF-8 conversion only. Memory handle allocation and
|
||||
linefeed conversion is done by this class.
|
||||
*/
|
||||
virtual CString doToIClipboard(const CString&) const = 0;
|
||||
|
||||
private:
|
||||
CString convertLinefeedToWin32(const CString&) const;
|
||||
CString convertLinefeedToUnix(const CString&) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
40
lib/platform/CMSWindowsClipboardTextConverter.cpp
Executable file
40
lib/platform/CMSWindowsClipboardTextConverter.cpp
Executable file
@@ -0,0 +1,40 @@
|
||||
#include "CMSWindowsClipboardTextConverter.h"
|
||||
#include "CUnicode.h"
|
||||
|
||||
//
|
||||
// CMSWindowsClipboardTextConverter
|
||||
//
|
||||
|
||||
CMSWindowsClipboardTextConverter::CMSWindowsClipboardTextConverter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CMSWindowsClipboardTextConverter::~CMSWindowsClipboardTextConverter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
UINT
|
||||
CMSWindowsClipboardTextConverter::getWin32Format() const
|
||||
{
|
||||
return CF_TEXT;
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardTextConverter::doFromIClipboard(const CString& data) const
|
||||
{
|
||||
// convert and add nul terminator
|
||||
return CUnicode::UTF8ToText(data) += '\0';
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardTextConverter::doToIClipboard(const CString& data) const
|
||||
{
|
||||
// convert and strip nul terminator
|
||||
CString dst = CUnicode::textToUTF8(data);
|
||||
if (dst.size() > 0 && dst[dst.size() - 1] == '\0') {
|
||||
dst.erase(dst.size() - 1);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
22
lib/platform/CMSWindowsClipboardTextConverter.h
Executable file
22
lib/platform/CMSWindowsClipboardTextConverter.h
Executable file
@@ -0,0 +1,22 @@
|
||||
#ifndef CMSWINDOWSCLIPBOARDTEXTCONVERTER_H
|
||||
#define CMSWINDOWSCLIPBOARDTEXTCONVERTER_H
|
||||
|
||||
#include "CMSWindowsClipboardAnyTextConverter.h"
|
||||
|
||||
//! Convert to/from locale text encoding
|
||||
class CMSWindowsClipboardTextConverter :
|
||||
public CMSWindowsClipboardAnyTextConverter {
|
||||
public:
|
||||
CMSWindowsClipboardTextConverter();
|
||||
virtual ~CMSWindowsClipboardTextConverter();
|
||||
|
||||
// IMSWindowsClipboardConverter overrides
|
||||
virtual UINT getWin32Format() const;
|
||||
|
||||
protected:
|
||||
// CMSWindowsClipboardAnyTextConverter overrides
|
||||
virtual CString doFromIClipboard(const CString&) const;
|
||||
virtual CString doToIClipboard(const CString&) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
40
lib/platform/CMSWindowsClipboardUTF16Converter.cpp
Executable file
40
lib/platform/CMSWindowsClipboardUTF16Converter.cpp
Executable file
@@ -0,0 +1,40 @@
|
||||
#include "CMSWindowsClipboardUTF16Converter.h"
|
||||
#include "CUnicode.h"
|
||||
|
||||
//
|
||||
// CMSWindowsClipboardUTF16Converter
|
||||
//
|
||||
|
||||
CMSWindowsClipboardUTF16Converter::CMSWindowsClipboardUTF16Converter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CMSWindowsClipboardUTF16Converter::~CMSWindowsClipboardUTF16Converter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
UINT
|
||||
CMSWindowsClipboardUTF16Converter::getWin32Format() const
|
||||
{
|
||||
return CF_UNICODETEXT;
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardUTF16Converter::doFromIClipboard(const CString& data) const
|
||||
{
|
||||
// convert and add nul terminator
|
||||
return CUnicode::UTF8ToUTF16(data).append(sizeof(wchar_t), 0);
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsClipboardUTF16Converter::doToIClipboard(const CString& data) const
|
||||
{
|
||||
// convert and strip nul terminator
|
||||
CString dst = CUnicode::UTF16ToUTF8(data);
|
||||
if (dst.size() > 0 && dst[dst.size() - 1] == '\0') {
|
||||
dst.erase(dst.size() - 1);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
22
lib/platform/CMSWindowsClipboardUTF16Converter.h
Executable file
22
lib/platform/CMSWindowsClipboardUTF16Converter.h
Executable file
@@ -0,0 +1,22 @@
|
||||
#ifndef CMSWINDOWSCLIPBOARDUTF16CONVERTER_H
|
||||
#define CMSWINDOWSCLIPBOARDUTF16CONVERTER_H
|
||||
|
||||
#include "CMSWindowsClipboardAnyTextConverter.h"
|
||||
|
||||
//! Convert to/from UTF-16 encoding
|
||||
class CMSWindowsClipboardUTF16Converter :
|
||||
public CMSWindowsClipboardAnyTextConverter {
|
||||
public:
|
||||
CMSWindowsClipboardUTF16Converter();
|
||||
virtual ~CMSWindowsClipboardUTF16Converter();
|
||||
|
||||
// IMSWindowsClipboardConverter overrides
|
||||
virtual UINT getWin32Format() const;
|
||||
|
||||
protected:
|
||||
// CMSWindowsClipboardAnyTextConverter overrides
|
||||
virtual CString doFromIClipboard(const CString&) const;
|
||||
virtual CString doToIClipboard(const CString&) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
731
lib/platform/CMSWindowsScreen.cpp
Normal file
731
lib/platform/CMSWindowsScreen.cpp
Normal file
@@ -0,0 +1,731 @@
|
||||
#include "CMSWindowsScreen.h"
|
||||
#include "CMSWindowsClipboard.h"
|
||||
#include "CMSWindowsScreenSaver.h"
|
||||
#include "CPlatform.h"
|
||||
#include "CClipboard.h"
|
||||
#include "IMSWindowsScreenEventHandler.h"
|
||||
#include "IScreenReceiver.h"
|
||||
#include "CThread.h"
|
||||
#include "CLock.h"
|
||||
#include "TMethodJob.h"
|
||||
#include "CLog.h"
|
||||
#include "CString.h"
|
||||
#include <cstring>
|
||||
|
||||
//
|
||||
// add backwards compatible multihead support (and suppress bogus warning)
|
||||
//
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4706) // assignment within conditional
|
||||
#define COMPILE_MULTIMON_STUBS
|
||||
#include <multimon.h>
|
||||
#pragma warning(pop)
|
||||
|
||||
//
|
||||
// CMSWindowsScreen
|
||||
//
|
||||
|
||||
HINSTANCE CMSWindowsScreen::s_instance = NULL;
|
||||
CMSWindowsScreen* CMSWindowsScreen::s_screen = NULL;
|
||||
|
||||
CMSWindowsScreen::CMSWindowsScreen(IScreenReceiver* receiver,
|
||||
IMSWindowsScreenEventHandler* eventHandler) :
|
||||
m_receiver(receiver),
|
||||
m_eventHandler(eventHandler),
|
||||
m_class(NULL),
|
||||
m_icon(NULL),
|
||||
m_cursor(NULL),
|
||||
m_is95Family(CPlatform::isWindows95Family()),
|
||||
m_window(NULL),
|
||||
m_x(0), m_y(0),
|
||||
m_w(0), m_h(0),
|
||||
m_multimon(false),
|
||||
m_threadID(0),
|
||||
m_lastThreadID(0),
|
||||
m_nextClipboardWindow(NULL),
|
||||
m_clipboardOwner(NULL),
|
||||
m_timer(0),
|
||||
m_desk(NULL),
|
||||
m_deskName(),
|
||||
m_hookLibrary(NULL),
|
||||
m_installScreensaver(NULL),
|
||||
m_uninstallScreensaver(NULL),
|
||||
m_screensaver(NULL),
|
||||
m_screensaverNotify(false)
|
||||
{
|
||||
assert(s_screen == NULL);
|
||||
assert(m_receiver != NULL);
|
||||
assert(m_eventHandler != NULL);
|
||||
|
||||
s_screen = this;
|
||||
|
||||
// make sure this thread has a message queue
|
||||
MSG dummy;
|
||||
PeekMessage(&dummy, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
||||
}
|
||||
|
||||
CMSWindowsScreen::~CMSWindowsScreen()
|
||||
{
|
||||
assert(s_screen != NULL);
|
||||
assert(m_class == 0);
|
||||
|
||||
s_screen = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::init(HINSTANCE instance)
|
||||
{
|
||||
s_instance = instance;
|
||||
}
|
||||
|
||||
HWND
|
||||
CMSWindowsScreen::openDesktop()
|
||||
{
|
||||
// save thread id
|
||||
m_threadID = GetCurrentThreadId();
|
||||
|
||||
// get the input desktop and switch to it
|
||||
if (!switchDesktop(openInputDesktop())) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// poll input desktop to see if it changes (onPreDispatch()
|
||||
// handles WM_TIMER)
|
||||
m_timer = 0;
|
||||
if (!m_is95Family) {
|
||||
m_timer = SetTimer(NULL, 0, 200, NULL);
|
||||
}
|
||||
|
||||
return m_window;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::closeDesktop()
|
||||
{
|
||||
// remove timer
|
||||
if (m_timer != 0) {
|
||||
KillTimer(NULL, m_timer);
|
||||
}
|
||||
|
||||
// disconnect from desktop
|
||||
switchDesktop(NULL);
|
||||
|
||||
// clear thread id
|
||||
m_threadID = 0;
|
||||
|
||||
assert(m_window == NULL);
|
||||
assert(m_desk == NULL);
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::isMultimon() const
|
||||
{
|
||||
return m_multimon;
|
||||
}
|
||||
|
||||
HINSTANCE
|
||||
CMSWindowsScreen::getInstance()
|
||||
{
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::open()
|
||||
{
|
||||
assert(s_instance != NULL);
|
||||
assert(m_class == 0);
|
||||
|
||||
log((CLOG_DEBUG "opening display"));
|
||||
|
||||
// create the transparent cursor
|
||||
createBlankCursor();
|
||||
|
||||
// register a window class
|
||||
WNDCLASSEX classInfo;
|
||||
classInfo.cbSize = sizeof(classInfo);
|
||||
classInfo.style = CS_DBLCLKS | CS_NOCLOSE;
|
||||
classInfo.lpfnWndProc = &CMSWindowsScreen::wndProc;
|
||||
classInfo.cbClsExtra = 0;
|
||||
classInfo.cbWndExtra = 0;
|
||||
classInfo.hInstance = s_instance;
|
||||
classInfo.hIcon = NULL;
|
||||
classInfo.hCursor = m_cursor;
|
||||
classInfo.hbrBackground = NULL;
|
||||
classInfo.lpszMenuName = NULL;
|
||||
classInfo.lpszClassName = "Synergy";
|
||||
classInfo.hIconSm = NULL;
|
||||
m_class = RegisterClassEx(&classInfo);
|
||||
|
||||
// get screen shape
|
||||
updateScreenShape();
|
||||
|
||||
// initialize the screen saver
|
||||
m_screensaver = new CMSWindowsScreenSaver();
|
||||
|
||||
// load the hook library and get the screen saver functions
|
||||
m_hookLibrary = LoadLibrary("synrgyhk");
|
||||
if (m_hookLibrary != NULL) {
|
||||
m_installScreensaver = (InstallScreenSaverFunc)GetProcAddress(
|
||||
m_hookLibrary, "installScreenSaver");
|
||||
m_uninstallScreensaver = (UninstallScreenSaverFunc)GetProcAddress(
|
||||
m_hookLibrary, "uninstallScreenSaver");
|
||||
if (m_installScreensaver == NULL || m_uninstallScreensaver == NULL) {
|
||||
// disable if either install or uninstall is unavailable
|
||||
m_installScreensaver = NULL;
|
||||
m_uninstallScreensaver = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::mainLoop()
|
||||
{
|
||||
// must call mainLoop() from same thread as openDesktop()
|
||||
assert(m_threadID == GetCurrentThreadId());
|
||||
|
||||
// event loop
|
||||
CEvent event;
|
||||
event.m_result = 0;
|
||||
for (;;) {
|
||||
// wait for an event in a cancellable way
|
||||
CThread::waitForEvent();
|
||||
GetMessage(&event.m_msg, NULL, 0, 0);
|
||||
|
||||
// handle quit message
|
||||
if (event.m_msg.message == WM_QUIT) {
|
||||
CThread::getCurrentThread().cancel();
|
||||
}
|
||||
|
||||
// dispatch message
|
||||
if (!onPreDispatch(&event)) {
|
||||
TranslateMessage(&event.m_msg);
|
||||
DispatchMessage(&event.m_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::exitMainLoop()
|
||||
{
|
||||
PostThreadMessage(m_threadID, WM_QUIT, 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::close()
|
||||
{
|
||||
assert(s_instance != NULL);
|
||||
|
||||
// done with hook library
|
||||
if (m_hookLibrary != NULL) {
|
||||
FreeLibrary(m_hookLibrary);
|
||||
m_installScreensaver = NULL;
|
||||
m_uninstallScreensaver = NULL;
|
||||
m_hookLibrary = NULL;
|
||||
}
|
||||
|
||||
// done with screen saver
|
||||
delete m_screensaver;
|
||||
m_screensaver = NULL;
|
||||
|
||||
// unregister the window class
|
||||
if (m_class != 0) {
|
||||
UnregisterClass((LPCTSTR)m_class, s_instance);
|
||||
m_class = 0;
|
||||
}
|
||||
|
||||
// delete resources
|
||||
if (m_cursor != NULL) {
|
||||
DestroyCursor(m_cursor);
|
||||
m_cursor = NULL;
|
||||
}
|
||||
|
||||
log((CLOG_DEBUG "closed display"));
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::setClipboard(ClipboardID, const IClipboard* src)
|
||||
{
|
||||
CMSWindowsClipboard dst(m_window);
|
||||
if (src != NULL) {
|
||||
// save clipboard data
|
||||
return CClipboard::copy(&dst, src);
|
||||
}
|
||||
else {
|
||||
// assert clipboard ownership
|
||||
if (!dst.open(0)) {
|
||||
return false;
|
||||
}
|
||||
dst.empty();
|
||||
dst.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::checkClipboards()
|
||||
{
|
||||
// if we think we own the clipboard but we don't then somebody
|
||||
// grabbed the clipboard on this screen without us knowing.
|
||||
// tell the server that this screen grabbed the clipboard.
|
||||
//
|
||||
// this works around bugs in the clipboard viewer chain.
|
||||
// sometimes NT will simply never send WM_DRAWCLIPBOARD
|
||||
// messages for no apparent reason and rebooting fixes the
|
||||
// problem. since we don't want a broken clipboard until the
|
||||
// next reboot we do this double check. clipboard ownership
|
||||
// won't be reflected on other screens until we leave but at
|
||||
// least the clipboard itself will work.
|
||||
HWND clipboardOwner = GetClipboardOwner();
|
||||
if (m_clipboardOwner != clipboardOwner) {
|
||||
m_clipboardOwner = clipboardOwner;
|
||||
if (m_clipboardOwner != m_window && m_clipboardOwner != NULL) {
|
||||
m_receiver->onGrabClipboard(kClipboardClipboard);
|
||||
m_receiver->onGrabClipboard(kClipboardSelection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::openScreensaver(bool notify)
|
||||
{
|
||||
assert(m_screensaver != NULL);
|
||||
|
||||
m_screensaverNotify = notify;
|
||||
if (m_screensaverNotify) {
|
||||
if (m_installScreensaver != NULL) {
|
||||
m_installScreensaver();
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_screensaver->disable();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::closeScreensaver()
|
||||
{
|
||||
if (m_screensaver != NULL) {
|
||||
if (m_screensaverNotify) {
|
||||
if (m_uninstallScreensaver != NULL) {
|
||||
m_uninstallScreensaver();
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_screensaver->enable();
|
||||
}
|
||||
}
|
||||
m_screensaverNotify = false;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::screensaver(bool activate)
|
||||
{
|
||||
assert(m_screensaver != NULL);
|
||||
if (activate) {
|
||||
m_screensaver->activate();
|
||||
}
|
||||
else {
|
||||
m_screensaver->deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::syncDesktop()
|
||||
{
|
||||
// change calling thread's desktop
|
||||
if (!m_is95Family) {
|
||||
if (SetThreadDesktop(m_desk) == 0) {
|
||||
log((CLOG_WARN "failed to set desktop: %d", GetLastError()));
|
||||
}
|
||||
}
|
||||
|
||||
// attach input queues if not already attached. this has a habit
|
||||
// of sucking up more and more CPU each time it's called (even if
|
||||
// the threads are already attached). since we only expect one
|
||||
// thread to call this more than once we can save just the last
|
||||
// the attached thread.
|
||||
DWORD threadID = GetCurrentThreadId();
|
||||
if (threadID != m_lastThreadID && threadID != m_threadID) {
|
||||
m_lastThreadID = threadID;
|
||||
AttachThreadInput(threadID, m_threadID, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::getClipboard(ClipboardID, IClipboard* dst) const
|
||||
{
|
||||
CMSWindowsClipboard src(m_window);
|
||||
CClipboard::copy(dst, &src);
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::getShape(SInt32& x, SInt32& y,
|
||||
SInt32& w, SInt32& h) const
|
||||
{
|
||||
assert(m_class != 0);
|
||||
|
||||
x = m_x;
|
||||
y = m_y;
|
||||
w = m_w;
|
||||
h = m_h;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::getCursorPos(SInt32& x, SInt32& y) const
|
||||
{
|
||||
POINT pos;
|
||||
if (GetCursorPos(&pos)) {
|
||||
x = pos.x;
|
||||
y = pos.y;
|
||||
}
|
||||
else {
|
||||
getCursorCenter(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::getCursorCenter(SInt32& x, SInt32& y) const
|
||||
{
|
||||
x = GetSystemMetrics(SM_CXSCREEN) >> 1;
|
||||
y = GetSystemMetrics(SM_CYSCREEN) >> 1;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::updateScreenShape()
|
||||
{
|
||||
// get shape
|
||||
m_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
|
||||
m_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
|
||||
m_w = GetSystemMetrics(SM_CXVIRTUALSCREEN);
|
||||
m_h = GetSystemMetrics(SM_CYVIRTUALSCREEN);
|
||||
log((CLOG_INFO "screen shape: %d,%d %dx%d", m_x, m_y, m_w, m_h));
|
||||
|
||||
// check for multiple monitors
|
||||
m_multimon = (m_w != GetSystemMetrics(SM_CXSCREEN) ||
|
||||
m_h != GetSystemMetrics(SM_CYSCREEN));
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::onPreDispatch(const CEvent* event)
|
||||
{
|
||||
// handle event
|
||||
const MSG* msg = &event->m_msg;
|
||||
switch (msg->message) {
|
||||
case SYNERGY_MSG_SCREEN_SAVER:
|
||||
{
|
||||
// activating or deactivating?
|
||||
bool activate = (msg->wParam != 0);
|
||||
|
||||
// ignore this message if there are any other screen saver
|
||||
// messages already in the queue. this is important because
|
||||
// our checkStarted() function has a deliberate delay, so it
|
||||
// can't respond to events at full CPU speed and will fall
|
||||
// behind if a lot of screen saver events are generated.
|
||||
// that can easily happen because windows will continually
|
||||
// send SC_SCREENSAVE until the screen saver starts, even if
|
||||
// the screen saver is disabled!
|
||||
MSG msg;
|
||||
if (!PeekMessage(&msg, NULL, SYNERGY_MSG_SCREEN_SAVER,
|
||||
SYNERGY_MSG_SCREEN_SAVER, PM_NOREMOVE)) {
|
||||
if (activate) {
|
||||
if (m_screensaver->checkStarted(
|
||||
SYNERGY_MSG_SCREEN_SAVER, FALSE, 0)) {
|
||||
m_eventHandler->onScreensaver(true);
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_eventHandler->onScreensaver(false);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
case WM_TIMER:
|
||||
// if current desktop is not the input desktop then switch to it.
|
||||
// windows 95 doesn't support multiple desktops so don't bother
|
||||
// to check under it.
|
||||
if (!m_is95Family) {
|
||||
HDESK desk = openInputDesktop();
|
||||
if (desk != NULL) {
|
||||
if (isCurrentDesktop(desk)) {
|
||||
CloseDesktop(desk);
|
||||
}
|
||||
else {
|
||||
switchDesktop(desk);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return m_eventHandler->onPreDispatch(event);
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::onEvent(CEvent* event)
|
||||
{
|
||||
assert(event != NULL);
|
||||
|
||||
const MSG& msg = event->m_msg;
|
||||
switch (msg.message) {
|
||||
case WM_QUERYENDSESSION:
|
||||
if (m_is95Family) {
|
||||
event->m_result = TRUE;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_ENDSESSION:
|
||||
if (m_is95Family) {
|
||||
if (msg.wParam == TRUE && msg.lParam == 0) {
|
||||
exitMainLoop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_PAINT:
|
||||
ValidateRect(msg.hwnd, NULL);
|
||||
return true;
|
||||
|
||||
case WM_DRAWCLIPBOARD:
|
||||
log((CLOG_DEBUG "clipboard was taken"));
|
||||
|
||||
// first pass it on
|
||||
if (m_nextClipboardWindow != NULL) {
|
||||
SendMessage(m_nextClipboardWindow,
|
||||
msg.message, msg.wParam, msg.lParam);
|
||||
}
|
||||
|
||||
// now notify client that somebody changed the clipboard (unless
|
||||
// we're now the owner, in which case it's because we took
|
||||
// ownership, or now it's owned by nobody, which will happen if
|
||||
// we owned it and switched desktops because we destroy our
|
||||
// window to do that).
|
||||
m_clipboardOwner = GetClipboardOwner();
|
||||
if (m_clipboardOwner != m_window && m_clipboardOwner != NULL) {
|
||||
m_receiver->onGrabClipboard(kClipboardClipboard);
|
||||
m_receiver->onGrabClipboard(kClipboardSelection);
|
||||
}
|
||||
return true;
|
||||
|
||||
case WM_CHANGECBCHAIN:
|
||||
if (m_nextClipboardWindow == (HWND)msg.wParam) {
|
||||
m_nextClipboardWindow = (HWND)msg.lParam;
|
||||
}
|
||||
else if (m_nextClipboardWindow != NULL) {
|
||||
SendMessage(m_nextClipboardWindow,
|
||||
msg.message, msg.wParam, msg.lParam);
|
||||
}
|
||||
return true;
|
||||
|
||||
case WM_DISPLAYCHANGE:
|
||||
{
|
||||
// screen resolution may have changed. get old shape.
|
||||
SInt32 xOld, yOld, wOld, hOld;
|
||||
getShape(xOld, yOld, wOld, hOld);
|
||||
|
||||
// update shape
|
||||
updateScreenShape();
|
||||
|
||||
// collect new screen info
|
||||
CClientInfo info;
|
||||
getShape(info.m_x, info.m_y, info.m_w, info.m_h);
|
||||
getCursorPos(info.m_mx, info.m_my);
|
||||
info.m_zoneSize = m_eventHandler->getJumpZoneSize();
|
||||
|
||||
// do nothing if resolution hasn't changed
|
||||
if (info.m_x != xOld || info.m_y != yOld ||
|
||||
info.m_w != wOld || info.m_h != hOld) {
|
||||
// forward event
|
||||
m_eventHandler->onEvent(event);
|
||||
|
||||
// send new screen info
|
||||
m_receiver->onInfoChanged(info);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return m_eventHandler->onEvent(event);
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreen::createBlankCursor()
|
||||
{
|
||||
// create a transparent cursor
|
||||
int cw = GetSystemMetrics(SM_CXCURSOR);
|
||||
int ch = GetSystemMetrics(SM_CYCURSOR);
|
||||
UInt8* cursorAND = new UInt8[ch * ((cw + 31) >> 2)];
|
||||
UInt8* cursorXOR = new UInt8[ch * ((cw + 31) >> 2)];
|
||||
memset(cursorAND, 0xff, ch * ((cw + 31) >> 2));
|
||||
memset(cursorXOR, 0x00, ch * ((cw + 31) >> 2));
|
||||
m_cursor = CreateCursor(s_instance, 0, 0, cw, ch, cursorAND, cursorXOR);
|
||||
delete[] cursorXOR;
|
||||
delete[] cursorAND;
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::switchDesktop(HDESK desk)
|
||||
{
|
||||
// did we own the clipboard?
|
||||
bool ownClipboard = (m_clipboardOwner == m_window && m_window != NULL);
|
||||
|
||||
// destroy old window
|
||||
if (m_window != NULL) {
|
||||
// first remove clipboard snooper
|
||||
ChangeClipboardChain(m_window, m_nextClipboardWindow);
|
||||
m_nextClipboardWindow = NULL;
|
||||
|
||||
// we no longer own the clipboard
|
||||
if (ownClipboard) {
|
||||
m_clipboardOwner = NULL;
|
||||
}
|
||||
|
||||
// let client clean up before we destroy the window
|
||||
m_eventHandler->preDestroyWindow(m_window);
|
||||
|
||||
// now destroy window
|
||||
DestroyWindow(m_window);
|
||||
m_window = NULL;
|
||||
|
||||
// done with desk
|
||||
if (!m_is95Family) {
|
||||
CloseDesktop(m_desk);
|
||||
}
|
||||
m_desk = NULL;
|
||||
m_deskName = "";
|
||||
}
|
||||
|
||||
// if no new desktop then we're done
|
||||
if (desk == NULL) {
|
||||
log((CLOG_INFO "disconnecting desktop"));
|
||||
return true;
|
||||
}
|
||||
|
||||
// uninstall screen saver hooks
|
||||
if (m_screensaverNotify) {
|
||||
if (m_uninstallScreensaver != NULL) {
|
||||
m_uninstallScreensaver();
|
||||
}
|
||||
}
|
||||
|
||||
// set the desktop. can only do this when there are no windows
|
||||
// and hooks on the current desktop owned by this thread.
|
||||
if (SetThreadDesktop(desk) == 0) {
|
||||
log((CLOG_ERR "failed to set desktop: %d", GetLastError()));
|
||||
if (!m_is95Family) {
|
||||
CloseDesktop(desk);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the window
|
||||
m_window = CreateWindowEx(WS_EX_TOPMOST |
|
||||
WS_EX_TRANSPARENT |
|
||||
WS_EX_TOOLWINDOW,
|
||||
(LPCTSTR)m_class,
|
||||
"Synergy",
|
||||
WS_POPUP,
|
||||
0, 0, 1, 1,
|
||||
NULL, NULL,
|
||||
getInstance(),
|
||||
NULL);
|
||||
if (m_window == NULL) {
|
||||
log((CLOG_ERR "failed to create window: %d", GetLastError()));
|
||||
if (!m_is95Family) {
|
||||
CloseDesktop(desk);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// reinstall screen saver hooks
|
||||
if (m_screensaverNotify) {
|
||||
if (m_installScreensaver != NULL) {
|
||||
m_installScreensaver();
|
||||
}
|
||||
}
|
||||
|
||||
// install our clipboard snooper
|
||||
m_nextClipboardWindow = SetClipboardViewer(m_window);
|
||||
|
||||
// reassert clipboard ownership
|
||||
if (ownClipboard) {
|
||||
// FIXME -- take clipboard ownership, but we should also set
|
||||
// the clipboard data.
|
||||
}
|
||||
m_clipboardOwner = GetClipboardOwner();
|
||||
|
||||
// save new desktop
|
||||
m_desk = desk;
|
||||
m_deskName = getDesktopName(m_desk);
|
||||
log((CLOG_INFO "switched to desktop \"%s\"", m_deskName.c_str()));
|
||||
|
||||
// let client prepare the window
|
||||
m_eventHandler->postCreateWindow(m_window);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
HDESK
|
||||
CMSWindowsScreen::openInputDesktop() const
|
||||
{
|
||||
if (m_is95Family) {
|
||||
// there's only one desktop on windows 95 et al.
|
||||
return GetThreadDesktop(GetCurrentThreadId());
|
||||
}
|
||||
else {
|
||||
return OpenInputDesktop(DF_ALLOWOTHERACCOUNTHOOK, TRUE,
|
||||
DESKTOP_CREATEWINDOW |
|
||||
DESKTOP_HOOKCONTROL |
|
||||
GENERIC_WRITE);
|
||||
}
|
||||
}
|
||||
|
||||
CString
|
||||
CMSWindowsScreen::getDesktopName(HDESK desk) const
|
||||
{
|
||||
if (desk == NULL) {
|
||||
return CString();
|
||||
}
|
||||
else if (m_is95Family) {
|
||||
return "desktop";
|
||||
}
|
||||
else {
|
||||
DWORD size;
|
||||
GetUserObjectInformation(desk, UOI_NAME, NULL, 0, &size);
|
||||
TCHAR* name = new TCHAR[size / sizeof(TCHAR) + 1];
|
||||
GetUserObjectInformation(desk, UOI_NAME, name, size, &size);
|
||||
CString result(name);
|
||||
delete[] name;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreen::isCurrentDesktop(HDESK desk) const
|
||||
{
|
||||
return CStringUtil::CaselessCmp::equal(getDesktopName(desk), m_deskName);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK
|
||||
CMSWindowsScreen::wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
assert(s_screen != NULL);
|
||||
|
||||
CEvent event;
|
||||
event.m_msg.hwnd = hwnd;
|
||||
event.m_msg.message = msg;
|
||||
event.m_msg.wParam = wParam;
|
||||
event.m_msg.lParam = lParam;
|
||||
event.m_result = 0;
|
||||
|
||||
if (s_screen->onEvent(&event)) {
|
||||
return event.m_result;
|
||||
}
|
||||
else {
|
||||
return DefWindowProc(hwnd, msg, wParam, lParam);
|
||||
}
|
||||
}
|
||||
172
lib/platform/CMSWindowsScreen.h
Normal file
172
lib/platform/CMSWindowsScreen.h
Normal file
@@ -0,0 +1,172 @@
|
||||
#ifndef CMSWINDOWSSCREEN_H
|
||||
#define CMSWINDOWSSCREEN_H
|
||||
|
||||
#include "IScreen.h"
|
||||
#include "CSynergyHook.h"
|
||||
#include "CMutex.h"
|
||||
#include "CString.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
class CMSWindowsScreenSaver;
|
||||
class CThread;
|
||||
|
||||
// Microsoft windows event
|
||||
class CEvent {
|
||||
public:
|
||||
MSG m_msg;
|
||||
LRESULT m_result;
|
||||
};
|
||||
|
||||
class IScreenReceiver;
|
||||
class IMSWindowsScreenEventHandler;
|
||||
|
||||
// Microsoft windows screen implementation
|
||||
class CMSWindowsScreen : public IScreen {
|
||||
public:
|
||||
CMSWindowsScreen(IScreenReceiver*, IMSWindowsScreenEventHandler*);
|
||||
virtual ~CMSWindowsScreen();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Initialize
|
||||
/*!
|
||||
Saves the application's HINSTANCE. This \b must be called by
|
||||
WinMain with the HINSTANCE it was passed.
|
||||
*/
|
||||
static void init(HINSTANCE);
|
||||
|
||||
//! Open desktop
|
||||
/*!
|
||||
Open the desktop and create and return the window. Returns NULL
|
||||
on failure.
|
||||
*/
|
||||
HWND openDesktop();
|
||||
|
||||
//! Close desktop
|
||||
/*!
|
||||
Close the window and desktop.
|
||||
*/
|
||||
void closeDesktop();
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Test for multiple monitors
|
||||
/*!
|
||||
Returns true iff the system appears to have multiple monitors.
|
||||
*/
|
||||
bool isMultimon() const;
|
||||
|
||||
//! Get instance
|
||||
/*!
|
||||
Returns the application instance handle passed to init().
|
||||
*/
|
||||
static HINSTANCE getInstance();
|
||||
|
||||
//@}
|
||||
|
||||
// IScreen overrides
|
||||
// note -- this class expects the hook DLL to have been loaded
|
||||
// and initialized before open() is called.
|
||||
void open();
|
||||
void mainLoop();
|
||||
void exitMainLoop();
|
||||
void close();
|
||||
bool setClipboard(ClipboardID, const IClipboard*);
|
||||
void checkClipboards();
|
||||
void openScreensaver(bool notify);
|
||||
void closeScreensaver();
|
||||
void screensaver(bool activate);
|
||||
void syncDesktop();
|
||||
bool getClipboard(ClipboardID, IClipboard*) const;
|
||||
void getShape(SInt32&, SInt32&, SInt32&, SInt32&) const;
|
||||
void getCursorPos(SInt32&, SInt32&) const;
|
||||
void getCursorCenter(SInt32&, SInt32&) const;
|
||||
|
||||
private:
|
||||
// update screen size cache
|
||||
void updateScreenShape();
|
||||
|
||||
// internal pre-dispatch event processing
|
||||
bool onPreDispatch(const CEvent* event);
|
||||
|
||||
// internal (post-dispatch) event processing
|
||||
bool onEvent(CEvent* event);
|
||||
|
||||
// create the transparent cursor
|
||||
void createBlankCursor();
|
||||
|
||||
// switch to the given desktop. this destroys the window and unhooks
|
||||
// all hooks, switches the desktop, then creates the window and rehooks
|
||||
// all hooks (because you can't switch the thread's desktop if it has
|
||||
// any windows or hooks).
|
||||
bool switchDesktop(HDESK desk);
|
||||
|
||||
// get the input desktop. caller must CloseDesktop() the result.
|
||||
// do not call under windows 95/98/me.
|
||||
HDESK openInputDesktop() const;
|
||||
|
||||
// get the desktop's name. do not call under windows 95/98/me.
|
||||
CString getDesktopName(HDESK) const;
|
||||
|
||||
// returns true iff desk is the current desk. do not call under
|
||||
// windows 95/98/me.
|
||||
bool isCurrentDesktop(HDESK desk) const;
|
||||
|
||||
// our window proc
|
||||
static LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
private:
|
||||
static HINSTANCE s_instance;
|
||||
|
||||
IScreenReceiver* m_receiver;
|
||||
IMSWindowsScreenEventHandler* m_eventHandler;
|
||||
|
||||
ATOM m_class;
|
||||
HICON m_icon;
|
||||
HCURSOR m_cursor;
|
||||
|
||||
// true if windows 95/98/me
|
||||
bool m_is95Family;
|
||||
|
||||
// our window
|
||||
HWND m_window;
|
||||
|
||||
// screen shape
|
||||
SInt32 m_x, m_y;
|
||||
SInt32 m_w, m_h;
|
||||
|
||||
// true if system appears to have multiple monitors
|
||||
bool m_multimon;
|
||||
|
||||
// the main loop's thread id
|
||||
DWORD m_threadID;
|
||||
|
||||
// the thread id of the last attached thread
|
||||
DWORD m_lastThreadID;
|
||||
|
||||
// clipboard stuff
|
||||
HWND m_nextClipboardWindow;
|
||||
HWND m_clipboardOwner;
|
||||
|
||||
// the timer used to check for desktop switching
|
||||
UINT m_timer;
|
||||
|
||||
// the current desk and it's name
|
||||
HDESK m_desk;
|
||||
CString m_deskName;
|
||||
|
||||
// screen saver stuff
|
||||
HINSTANCE m_hookLibrary;
|
||||
InstallScreenSaverFunc m_installScreensaver;
|
||||
UninstallScreenSaverFunc m_uninstallScreensaver;
|
||||
CMSWindowsScreenSaver* m_screensaver;
|
||||
bool m_screensaverNotify;
|
||||
|
||||
static CMSWindowsScreen* s_screen;
|
||||
};
|
||||
|
||||
#endif
|
||||
298
lib/platform/CMSWindowsScreenSaver.cpp
Executable file
298
lib/platform/CMSWindowsScreenSaver.cpp
Executable file
@@ -0,0 +1,298 @@
|
||||
#include "CMSWindowsScreenSaver.h"
|
||||
#include "CThread.h"
|
||||
#include "CLog.h"
|
||||
#include "TMethodJob.h"
|
||||
|
||||
#if !defined(SPI_GETSCREENSAVERRUNNING)
|
||||
#define SPI_GETSCREENSAVERRUNNING 114
|
||||
#endif
|
||||
|
||||
//
|
||||
// CMSWindowsScreenSaver
|
||||
//
|
||||
|
||||
CMSWindowsScreenSaver::CMSWindowsScreenSaver() :
|
||||
m_process(NULL),
|
||||
m_threadID(0),
|
||||
m_watch(NULL)
|
||||
{
|
||||
// detect OS
|
||||
m_is95Family = false;
|
||||
m_is95 = false;
|
||||
m_isNT = false;
|
||||
OSVERSIONINFO info;
|
||||
info.dwOSVersionInfoSize = sizeof(info);
|
||||
if (GetVersionEx(&info)) {
|
||||
m_is95Family = (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
|
||||
if (info.dwPlatformId == VER_PLATFORM_WIN32_NT &&
|
||||
info.dwMajorVersion <= 4) {
|
||||
m_isNT = true;
|
||||
}
|
||||
else if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
|
||||
info.dwMajorVersion == 4 &&
|
||||
info.dwMinorVersion == 0) {
|
||||
m_is95 = true;
|
||||
}
|
||||
}
|
||||
|
||||
// check if screen saver is enabled
|
||||
SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &m_wasEnabled, 0);
|
||||
}
|
||||
|
||||
CMSWindowsScreenSaver::~CMSWindowsScreenSaver()
|
||||
{
|
||||
unwatchProcess();
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreenSaver::checkStarted(UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
// screen saver may have started. look for it and get
|
||||
// the process. if we can't find it then assume it
|
||||
// didn't really start. we wait a moment before
|
||||
// looking to give the screen saver a chance to start.
|
||||
// this shouldn't be a problem since we only get here
|
||||
// if the screen saver wants to kick in, meaning that
|
||||
// the system is idle or the user deliberately started
|
||||
// the screen saver.
|
||||
Sleep(250);
|
||||
HWND hwnd = findScreenSaver();
|
||||
if (hwnd == NULL) {
|
||||
// didn't start
|
||||
log((CLOG_DEBUG "can't find screen saver window"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// get the process
|
||||
DWORD processID;
|
||||
GetWindowThreadProcessId(hwnd, &processID);
|
||||
HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, processID);
|
||||
if (process == NULL) {
|
||||
// didn't start
|
||||
log((CLOG_DEBUG "can't open screen saver process"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// watch for the process to exit
|
||||
m_threadID = GetCurrentThreadId();
|
||||
m_msg = msg;
|
||||
m_wParam = wParam;
|
||||
m_lParam = lParam;
|
||||
watchProcess(process);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::enable()
|
||||
{
|
||||
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, m_wasEnabled, 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::disable()
|
||||
{
|
||||
SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &m_wasEnabled, 0);
|
||||
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::activate()
|
||||
{
|
||||
// don't activate if already active
|
||||
if (!isActive()) {
|
||||
HWND hwnd = GetForegroundWindow();
|
||||
if (hwnd != NULL) {
|
||||
PostMessage(hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0);
|
||||
}
|
||||
else {
|
||||
// no foreground window. pretend we got the event instead.
|
||||
DefWindowProc(NULL, WM_SYSCOMMAND, SC_SCREENSAVE, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::deactivate()
|
||||
{
|
||||
bool killed = false;
|
||||
if (!m_is95Family) {
|
||||
// NT runs screen saver in another desktop
|
||||
HDESK desktop = OpenDesktop("screen-saver", 0, FALSE,
|
||||
DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
|
||||
if (desktop != NULL) {
|
||||
EnumDesktopWindows(desktop,
|
||||
&CMSWindowsScreenSaver::killScreenSaverFunc,
|
||||
reinterpret_cast<LPARAM>(&killed));
|
||||
CloseDesktop(desktop);
|
||||
}
|
||||
}
|
||||
|
||||
// if above failed or wasn't tried, try the windows 95 way
|
||||
if (!killed) {
|
||||
// find screen saver window and close it
|
||||
HWND hwnd = FindWindow("WindowsScreenSaverClass", NULL);
|
||||
if (hwnd != NULL) {
|
||||
PostMessage(hwnd, WM_CLOSE, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// force timer to restart
|
||||
SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &m_wasEnabled, 0);
|
||||
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
|
||||
!m_wasEnabled, 0, SPIF_SENDWININICHANGE);
|
||||
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
|
||||
m_wasEnabled, 0, SPIF_SENDWININICHANGE);
|
||||
}
|
||||
|
||||
bool
|
||||
CMSWindowsScreenSaver::isActive() const
|
||||
{
|
||||
if (m_is95) {
|
||||
return (FindWindow("WindowsScreenSaverClass", NULL) != NULL);
|
||||
}
|
||||
else if (m_isNT) {
|
||||
// screen saver runs on a separate desktop
|
||||
HDESK desktop = OpenDesktop("screen-saver", 0, FALSE, MAXIMUM_ALLOWED);
|
||||
if (desktop == NULL && GetLastError() != ERROR_ACCESS_DENIED) {
|
||||
// desktop doesn't exist so screen saver is not running
|
||||
return false;
|
||||
}
|
||||
|
||||
// desktop exists. this should indicate that the screen saver
|
||||
// is running but an OS bug can cause a valid handle to be
|
||||
// returned even if the screen saver isn't running (Q230117).
|
||||
// we'll try to enumerate the windows on the desktop and, if
|
||||
// there are any, we assume the screen saver is running. (note
|
||||
// that if we don't have permission to enumerate then we'll
|
||||
// assume that the screen saver is not running.) that'd be
|
||||
// easy enough except there's another OS bug (Q198590) that can
|
||||
// cause EnumDesktopWindows() to enumerate the windows of
|
||||
// another desktop if the requested desktop has no windows. to
|
||||
// work around that we have to verify that the enumerated
|
||||
// windows are, in fact, on the expected desktop.
|
||||
CFindScreenSaverInfo info;
|
||||
info.m_desktop = desktop;
|
||||
info.m_window = NULL;
|
||||
EnumDesktopWindows(desktop,
|
||||
&CMSWindowsScreenSaver::findScreenSaverFunc,
|
||||
reinterpret_cast<LPARAM>(&info));
|
||||
|
||||
// done with desktop
|
||||
CloseDesktop(desktop);
|
||||
|
||||
// screen saver is running if a window was found
|
||||
return (info.m_window != NULL);
|
||||
}
|
||||
else {
|
||||
BOOL running;
|
||||
SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &running, 0);
|
||||
return (running != FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CALLBACK
|
||||
CMSWindowsScreenSaver::findScreenSaverFunc(HWND hwnd, LPARAM arg)
|
||||
{
|
||||
CFindScreenSaverInfo* info = reinterpret_cast<CFindScreenSaverInfo*>(arg);
|
||||
|
||||
if (info->m_desktop != NULL) {
|
||||
DWORD threadID = GetWindowThreadProcessId(hwnd, NULL);
|
||||
HDESK desktop = GetThreadDesktop(threadID);
|
||||
if (desktop != NULL && desktop != info->m_desktop) {
|
||||
// stop enumerating -- wrong desktop
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// found a window
|
||||
info->m_window = hwnd;
|
||||
|
||||
// don't need to enumerate further
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
BOOL CALLBACK
|
||||
CMSWindowsScreenSaver::killScreenSaverFunc(HWND hwnd, LPARAM arg)
|
||||
{
|
||||
if (IsWindowVisible(hwnd)) {
|
||||
PostMessage(hwnd, WM_CLOSE, 0, 0);
|
||||
*reinterpret_cast<bool*>(arg) = true;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
HWND
|
||||
CMSWindowsScreenSaver::findScreenSaver()
|
||||
{
|
||||
if (!m_is95Family) {
|
||||
// screen saver runs on a separate desktop
|
||||
HDESK desktop = OpenDesktop("screen-saver", 0, FALSE, MAXIMUM_ALLOWED);
|
||||
if (desktop != NULL) {
|
||||
// search
|
||||
CFindScreenSaverInfo info;
|
||||
info.m_desktop = desktop;
|
||||
info.m_window = NULL;
|
||||
EnumDesktopWindows(desktop,
|
||||
&CMSWindowsScreenSaver::findScreenSaverFunc,
|
||||
reinterpret_cast<LPARAM>(&info));
|
||||
|
||||
// done with desktop
|
||||
CloseDesktop(desktop);
|
||||
|
||||
// return window if found
|
||||
if (info.m_window != NULL) {
|
||||
return info.m_window;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try windows 95 way
|
||||
return FindWindow("WindowsScreenSaverClass", NULL);
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::watchProcess(HANDLE process)
|
||||
{
|
||||
// stop watching previous process
|
||||
unwatchProcess();
|
||||
|
||||
// watch new process in another thread
|
||||
if (process != NULL) {
|
||||
log((CLOG_DEBUG "watching screen saver process"));
|
||||
m_process = process;
|
||||
m_watch = new CThread(new TMethodJob<CMSWindowsScreenSaver>(this,
|
||||
&CMSWindowsScreenSaver::watchProcessThread));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::unwatchProcess()
|
||||
{
|
||||
if (m_watch != NULL) {
|
||||
log((CLOG_DEBUG "stopped watching screen saver process"));
|
||||
m_watch->cancel();
|
||||
m_watch->wait();
|
||||
delete m_watch;
|
||||
m_watch = NULL;
|
||||
}
|
||||
if (m_process != NULL) {
|
||||
CloseHandle(m_process);
|
||||
m_process = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CMSWindowsScreenSaver::watchProcessThread(void*)
|
||||
{
|
||||
for (;;) {
|
||||
CThread::testCancel();
|
||||
if (WaitForSingleObject(m_process, 50) == WAIT_OBJECT_0) {
|
||||
// process terminated. send screen saver deactivation
|
||||
// message.
|
||||
log((CLOG_DEBUG "screen saver died"));
|
||||
PostThreadMessage(m_threadID, m_msg, m_wParam, m_lParam);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
66
lib/platform/CMSWindowsScreenSaver.h
Executable file
66
lib/platform/CMSWindowsScreenSaver.h
Executable file
@@ -0,0 +1,66 @@
|
||||
#ifndef CMSWINDOWSSCREENSAVER_H
|
||||
#define CMSWINDOWSSCREENSAVER_H
|
||||
|
||||
#include "IScreenSaver.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
class CThread;
|
||||
|
||||
//! Microsoft windows screen saver implementation
|
||||
class CMSWindowsScreenSaver : public IScreenSaver {
|
||||
public:
|
||||
CMSWindowsScreenSaver();
|
||||
virtual ~CMSWindowsScreenSaver();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Check if screen saver started
|
||||
/*!
|
||||
Check if the screen saver really started. Returns false if it
|
||||
hasn't, true otherwise. When the screen saver stops, \c msg will
|
||||
be posted to the current thread's message queue with the given
|
||||
parameters.
|
||||
*/
|
||||
bool checkStarted(UINT msg, WPARAM, LPARAM);
|
||||
|
||||
//@}
|
||||
|
||||
// IScreenSaver overrides
|
||||
virtual void enable();
|
||||
virtual void disable();
|
||||
virtual void activate();
|
||||
virtual void deactivate();
|
||||
virtual bool isActive() const;
|
||||
|
||||
private:
|
||||
class CFindScreenSaverInfo {
|
||||
public:
|
||||
HDESK m_desktop;
|
||||
HWND m_window;
|
||||
};
|
||||
|
||||
static BOOL CALLBACK findScreenSaverFunc(HWND hwnd, LPARAM lParam);
|
||||
static BOOL CALLBACK killScreenSaverFunc(HWND hwnd, LPARAM lParam);
|
||||
|
||||
HWND findScreenSaver();
|
||||
void watchProcess(HANDLE process);
|
||||
void unwatchProcess();
|
||||
void watchProcessThread(void*);
|
||||
|
||||
private:
|
||||
bool m_is95Family;
|
||||
bool m_is95;
|
||||
bool m_isNT;
|
||||
BOOL m_wasEnabled;
|
||||
|
||||
HANDLE m_process;
|
||||
CThread* m_watch;
|
||||
DWORD m_threadID;
|
||||
UINT m_msg;
|
||||
WPARAM m_wParam;
|
||||
LPARAM m_lParam;
|
||||
};
|
||||
|
||||
#endif
|
||||
15
lib/platform/CPlatform.cpp
Normal file
15
lib/platform/CPlatform.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "common.h"
|
||||
|
||||
#if WINDOWS_LIKE
|
||||
|
||||
#include "CWin32Platform.cpp"
|
||||
|
||||
#elif UNIX_LIKE
|
||||
|
||||
#include "CUnixPlatform.cpp"
|
||||
|
||||
#else
|
||||
|
||||
#error Unsupported platform
|
||||
|
||||
#endif
|
||||
22
lib/platform/CPlatform.h
Normal file
22
lib/platform/CPlatform.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef CPLATFORM_H
|
||||
#define CPLATFORM_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if WINDOWS_LIKE
|
||||
|
||||
#include "CWin32Platform.h"
|
||||
typedef CWin32Platform CPlatform;
|
||||
|
||||
#elif UNIX_LIKE
|
||||
|
||||
#include "CUnixPlatform.h"
|
||||
typedef CUnixPlatform CPlatform;
|
||||
|
||||
#else
|
||||
|
||||
#error Unsupported platform
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
726
lib/platform/CSynergyHook.cpp
Normal file
726
lib/platform/CSynergyHook.cpp
Normal file
@@ -0,0 +1,726 @@
|
||||
#include "CSynergyHook.h"
|
||||
#include "ProtocolTypes.h"
|
||||
#include <zmouse.h>
|
||||
|
||||
//
|
||||
// extra mouse wheel stuff
|
||||
//
|
||||
|
||||
enum EWheelSupport {
|
||||
kWheelNone,
|
||||
kWheelOld,
|
||||
kWheelWin2000,
|
||||
kWheelModern
|
||||
};
|
||||
|
||||
// declare extended mouse hook struct. useable on win2k
|
||||
typedef struct tagMOUSEHOOKSTRUCTWin2000 {
|
||||
MOUSEHOOKSTRUCT mhs;
|
||||
DWORD mouseData;
|
||||
} MOUSEHOOKSTRUCTWin2000;
|
||||
|
||||
#if !defined(SM_MOUSEWHEELPRESENT)
|
||||
#define SM_MOUSEWHEELPRESENT 75
|
||||
#endif
|
||||
|
||||
|
||||
//
|
||||
// globals
|
||||
//
|
||||
|
||||
#pragma comment(linker, "-section:shared,rws")
|
||||
#pragma data_seg("shared")
|
||||
// all data in this shared section *must* be initialized
|
||||
|
||||
static HINSTANCE g_hinstance = NULL;
|
||||
static DWORD g_processID = 0;
|
||||
static EWheelSupport g_wheelSupport = kWheelNone;
|
||||
static UINT g_wmMouseWheel = 0;
|
||||
static DWORD g_threadID = 0;
|
||||
static HHOOK g_keyboard = NULL;
|
||||
static HHOOK g_mouse = NULL;
|
||||
static HHOOK g_cbt = NULL;
|
||||
static HHOOK g_getMessage = NULL;
|
||||
static HANDLE g_keyHookThread = NULL;
|
||||
static DWORD g_keyHookThreadID = 0;
|
||||
static HANDLE g_keyHookEvent = NULL;
|
||||
static HHOOK g_keyboardLL = NULL;
|
||||
static bool g_screenSaver = false;
|
||||
static bool g_relay = false;
|
||||
static UInt32 g_zoneSides = 0;
|
||||
static SInt32 g_zoneSize = 0;
|
||||
static SInt32 g_xScreen = 0;
|
||||
static SInt32 g_yScreen = 0;
|
||||
static SInt32 g_wScreen = 0;
|
||||
static SInt32 g_hScreen = 0;
|
||||
static HCURSOR g_cursor = NULL;
|
||||
static DWORD g_cursorThread = 0;
|
||||
|
||||
#pragma data_seg()
|
||||
|
||||
//
|
||||
// internal functions
|
||||
//
|
||||
|
||||
static
|
||||
void
|
||||
hideCursor(DWORD thread)
|
||||
{
|
||||
// we should be running the context of the window who's cursor
|
||||
// we want to hide so we shouldn't have to attach thread input.
|
||||
g_cursor = GetCursor();
|
||||
g_cursorThread = thread;
|
||||
SetCursor(NULL);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
restoreCursor()
|
||||
{
|
||||
// restore the show cursor in the window we hid it last
|
||||
if (g_cursor != NULL && g_cursorThread != 0) {
|
||||
DWORD myThread = GetCurrentThreadId();
|
||||
if (myThread != g_cursorThread)
|
||||
AttachThreadInput(myThread, g_cursorThread, TRUE);
|
||||
SetCursor(g_cursor);
|
||||
if (myThread != g_cursorThread)
|
||||
AttachThreadInput(myThread, g_cursorThread, FALSE);
|
||||
}
|
||||
g_cursor = NULL;
|
||||
g_cursorThread = 0;
|
||||
}
|
||||
|
||||
static
|
||||
LRESULT CALLBACK
|
||||
keyboardHook(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0) {
|
||||
if (g_relay) {
|
||||
// forward message to our window
|
||||
PostThreadMessage(g_threadID, SYNERGY_MSG_KEY, wParam, lParam);
|
||||
|
||||
// let certain keys pass through
|
||||
switch (wParam) {
|
||||
case VK_CAPITAL:
|
||||
case VK_NUMLOCK:
|
||||
case VK_SCROLL:
|
||||
// pass event on. we want to let these through to
|
||||
// the window proc because otherwise the keyboard
|
||||
// lights may not stay synchronized.
|
||||
break;
|
||||
|
||||
default:
|
||||
// discard event
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(g_keyboard, code, wParam, lParam);
|
||||
}
|
||||
|
||||
static
|
||||
LRESULT CALLBACK
|
||||
mouseHook(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0) {
|
||||
if (g_relay) {
|
||||
switch (wParam) {
|
||||
case WM_LBUTTONDOWN:
|
||||
case WM_MBUTTONDOWN:
|
||||
case WM_RBUTTONDOWN:
|
||||
case WM_LBUTTONUP:
|
||||
case WM_MBUTTONUP:
|
||||
case WM_RBUTTONUP:
|
||||
PostThreadMessage(g_threadID,
|
||||
SYNERGY_MSG_MOUSE_BUTTON, wParam, 0);
|
||||
return 1;
|
||||
|
||||
case WM_MOUSEWHEEL:
|
||||
{
|
||||
// win2k and other systems supporting WM_MOUSEWHEEL in
|
||||
// the mouse hook are gratuitously different (and poorly
|
||||
// documented).
|
||||
switch (g_wheelSupport) {
|
||||
case kWheelModern: {
|
||||
const MOUSEHOOKSTRUCT* info =
|
||||
(const MOUSEHOOKSTRUCT*)lParam;
|
||||
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_WHEEL,
|
||||
static_cast<short>(
|
||||
LOWORD(info->dwExtraInfo)), 0);
|
||||
break;
|
||||
}
|
||||
|
||||
case kWheelWin2000: {
|
||||
const MOUSEHOOKSTRUCTWin2000* info =
|
||||
(const MOUSEHOOKSTRUCTWin2000*)lParam;
|
||||
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_WHEEL,
|
||||
static_cast<short>(
|
||||
HIWORD(info->mouseData)), 0);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
|
||||
case WM_MOUSEMOVE:
|
||||
{
|
||||
const MOUSEHOOKSTRUCT* info =
|
||||
(const MOUSEHOOKSTRUCT*)lParam;
|
||||
|
||||
// we want the cursor to be hidden at all times so we
|
||||
// hide the cursor on whatever window has it. but then
|
||||
// we have to show the cursor whenever we leave that
|
||||
// window (or at some later time before we stop relaying).
|
||||
// so check the window with the cursor. if it's not the
|
||||
// same window that had it before then show the cursor
|
||||
// in the last window and hide it in this window.
|
||||
DWORD thread = GetWindowThreadProcessId(info->hwnd, NULL);
|
||||
if (thread != g_cursorThread) {
|
||||
restoreCursor();
|
||||
hideCursor(thread);
|
||||
}
|
||||
|
||||
// relay the motion
|
||||
SInt32 x = (SInt32)info->pt.x;
|
||||
SInt32 y = (SInt32)info->pt.y;
|
||||
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// check for mouse inside jump zone
|
||||
bool inside = false;
|
||||
const MOUSEHOOKSTRUCT* info = (const MOUSEHOOKSTRUCT*)lParam;
|
||||
SInt32 x = (SInt32)info->pt.x;
|
||||
SInt32 y = (SInt32)info->pt.y;
|
||||
if (!inside && (g_zoneSides & kLeftMask) != 0) {
|
||||
inside = (x < g_xScreen + g_zoneSize);
|
||||
}
|
||||
if (!inside && (g_zoneSides & kRightMask) != 0) {
|
||||
inside = (x >= g_xScreen + g_wScreen - g_zoneSize);
|
||||
}
|
||||
if (!inside && (g_zoneSides & kTopMask) != 0) {
|
||||
inside = (y < g_yScreen + g_zoneSize);
|
||||
}
|
||||
if (!inside && (g_zoneSides & kBottomMask) != 0) {
|
||||
inside = (y >= g_yScreen + g_hScreen - g_zoneSize);
|
||||
}
|
||||
|
||||
// if inside then eat event and notify our window
|
||||
if (inside) {
|
||||
restoreCursor();
|
||||
PostThreadMessage(g_threadID, SYNERGY_MSG_MOUSE_MOVE, x, y);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(g_mouse, code, wParam, lParam);
|
||||
}
|
||||
|
||||
static
|
||||
LRESULT CALLBACK
|
||||
cbtHook(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0) {
|
||||
if (g_relay) {
|
||||
// do nothing for now. may add something later.
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(g_cbt, code, wParam, lParam);
|
||||
}
|
||||
|
||||
static
|
||||
LRESULT CALLBACK
|
||||
getMessageHook(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0) {
|
||||
if (g_screenSaver) {
|
||||
MSG* msg = reinterpret_cast<MSG*>(lParam);
|
||||
if (msg->message == WM_SYSCOMMAND && msg->wParam == SC_SCREENSAVE) {
|
||||
// broadcast screen saver started message
|
||||
PostThreadMessage(g_threadID,
|
||||
SYNERGY_MSG_SCREEN_SAVER, TRUE, 0);
|
||||
}
|
||||
}
|
||||
if (g_relay) {
|
||||
MSG* msg = reinterpret_cast<MSG*>(lParam);
|
||||
if (msg->message == g_wmMouseWheel) {
|
||||
// post message to our window
|
||||
PostThreadMessage(g_threadID,
|
||||
SYNERGY_MSG_MOUSE_WHEEL, msg->wParam, 0);
|
||||
|
||||
// zero out the delta in the message so it's (hopefully)
|
||||
// ignored
|
||||
msg->wParam = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(g_getMessage, code, wParam, lParam);
|
||||
}
|
||||
|
||||
#if (_WIN32_WINNT >= 0x0400)
|
||||
|
||||
//
|
||||
// low-level keyboard hook -- this allows us to capture and handle
|
||||
// alt+tab, alt+esc, ctrl+esc, and windows key hot keys. on the down
|
||||
// side, key repeats are not compressed for us.
|
||||
//
|
||||
|
||||
static
|
||||
LRESULT CALLBACK
|
||||
keyboardLLHook(int code, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (code >= 0) {
|
||||
if (g_relay) {
|
||||
KBDLLHOOKSTRUCT* info = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
|
||||
|
||||
// let certain keys pass through
|
||||
switch (info->vkCode) {
|
||||
case VK_CAPITAL:
|
||||
case VK_NUMLOCK:
|
||||
case VK_SCROLL:
|
||||
// pass event on. we want to let these through to
|
||||
// the window proc because otherwise the keyboard
|
||||
// lights may not stay synchronized.
|
||||
break;
|
||||
|
||||
default:
|
||||
// construct lParam for WM_KEYDOWN, etc.
|
||||
DWORD lParam = 1; // repeat code
|
||||
lParam |= (info->scanCode << 16); // scan code
|
||||
if (info->flags & LLKHF_EXTENDED) {
|
||||
lParam |= (1lu << 24); // extended key
|
||||
}
|
||||
if (info->flags & LLKHF_ALTDOWN) {
|
||||
lParam |= (1lu << 29); // context code
|
||||
}
|
||||
if (info->flags & LLKHF_UP) {
|
||||
lParam |= (1lu << 31); // transition
|
||||
}
|
||||
// FIXME -- bit 30 should be set if key was already down
|
||||
|
||||
// forward message to our window
|
||||
PostThreadMessage(g_threadID,
|
||||
SYNERGY_MSG_KEY, info->vkCode, lParam);
|
||||
|
||||
// discard event
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CallNextHookEx(g_keyboardLL, code, wParam, lParam);
|
||||
}
|
||||
|
||||
static
|
||||
DWORD WINAPI
|
||||
getKeyboardLLProc(void*)
|
||||
{
|
||||
// thread proc for low-level keyboard hook. this does nothing but
|
||||
// install the hook, process events, and uninstall the hook.
|
||||
|
||||
// force this thread to have a message queue
|
||||
MSG msg;
|
||||
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
||||
|
||||
// install low-level keyboard hook
|
||||
g_keyboardLL = SetWindowsHookEx(WH_KEYBOARD_LL,
|
||||
&keyboardLLHook,
|
||||
g_hinstance,
|
||||
0);
|
||||
if (g_keyboardLL == NULL) {
|
||||
// indicate failure and exit
|
||||
g_keyHookThreadID = 0;
|
||||
SetEvent(g_keyHookEvent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ready
|
||||
SetEvent(g_keyHookEvent);
|
||||
|
||||
// message loop
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
switch (GetMessage(&msg, NULL, 0, 0)) {
|
||||
case -1:
|
||||
break;
|
||||
|
||||
case 0:
|
||||
done = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// uninstall hook
|
||||
UnhookWindowsHookEx(g_keyboardLL);
|
||||
g_keyboardLL = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else // (_WIN32_WINNT < 0x0400)
|
||||
|
||||
#error foo
|
||||
|
||||
static
|
||||
DWORD WINAPI
|
||||
getKeyboardLLProc(void*)
|
||||
{
|
||||
g_keyHookThreadID = 0;
|
||||
SetEvent(g_keyHookEvent);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static
|
||||
EWheelSupport
|
||||
getWheelSupport()
|
||||
{
|
||||
// get operating system
|
||||
OSVERSIONINFO info;
|
||||
info.dwOSVersionInfoSize = sizeof(info);
|
||||
if (!GetVersionEx(&info)) {
|
||||
return kWheelNone;
|
||||
}
|
||||
|
||||
// see if modern wheel is present
|
||||
if (GetSystemMetrics(SM_MOUSEWHEELPRESENT)) {
|
||||
// note if running on win2k
|
||||
if (info.dwPlatformId == VER_PLATFORM_WIN32_NT &&
|
||||
info.dwMajorVersion == 5 &&
|
||||
info.dwMinorVersion == 0) {
|
||||
return kWheelWin2000;
|
||||
}
|
||||
return kWheelModern;
|
||||
}
|
||||
|
||||
// not modern. see if we've got old-style support.
|
||||
UINT wheelSupportMsg = RegisterWindowMessage(MSH_WHEELSUPPORT);
|
||||
HWND wheelSupportWindow = FindWindow(MSH_WHEELMODULE_CLASS,
|
||||
MSH_WHEELMODULE_TITLE);
|
||||
if (wheelSupportWindow != NULL && wheelSupportMsg != 0) {
|
||||
if (SendMessage(wheelSupportWindow, wheelSupportMsg, 0, 0) != 0) {
|
||||
g_wmMouseWheel = RegisterWindowMessage(MSH_MOUSEWHEEL);
|
||||
if (g_wmMouseWheel != 0) {
|
||||
return kWheelOld;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// assume modern. we don't do anything special in this case
|
||||
// except respond to WM_MOUSEWHEEL messages. GetSystemMetrics()
|
||||
// can apparently return FALSE even if a mouse wheel is present
|
||||
// though i'm not sure exactly when it does that (WinME returns
|
||||
// FALSE for my logitech USB trackball).
|
||||
return kWheelModern;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// external functions
|
||||
//
|
||||
|
||||
BOOL WINAPI
|
||||
DllMain(HINSTANCE instance, DWORD reason, LPVOID)
|
||||
{
|
||||
if (reason == DLL_PROCESS_ATTACH) {
|
||||
DisableThreadLibraryCalls(instance);
|
||||
if (g_processID == 0) {
|
||||
g_hinstance = instance;
|
||||
g_processID = GetCurrentProcessId();
|
||||
}
|
||||
}
|
||||
else if (reason == DLL_PROCESS_DETACH) {
|
||||
if (g_processID == GetCurrentProcessId()) {
|
||||
if (g_keyboard != NULL || g_mouse != NULL || g_cbt != NULL) {
|
||||
uninstall();
|
||||
}
|
||||
g_processID = 0;
|
||||
g_hinstance = NULL;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int
|
||||
init(DWORD threadID)
|
||||
{
|
||||
assert(g_hinstance != NULL);
|
||||
|
||||
// try to open process that last called init() to see if it's
|
||||
// still running or if it died without cleaning up.
|
||||
if (g_processID != 0 && g_processID != GetCurrentProcessId()) {
|
||||
HANDLE process = OpenProcess(STANDARD_RIGHTS_REQUIRED,
|
||||
FALSE, g_processID);
|
||||
if (process != NULL) {
|
||||
// old process (probably) still exists so refuse to
|
||||
// reinitialize this DLL (and thus steal it from the
|
||||
// old process).
|
||||
CloseHandle(process);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// clean up after old process. the system should've already
|
||||
// removed the hooks so we just need to reset our state.
|
||||
g_hinstance = GetModuleHandle("synrgyhk");
|
||||
g_processID = GetCurrentProcessId();
|
||||
g_wheelSupport = kWheelNone;
|
||||
g_threadID = 0;
|
||||
g_keyboard = NULL;
|
||||
g_mouse = NULL;
|
||||
g_cbt = NULL;
|
||||
g_getMessage = NULL;
|
||||
g_keyHookThread = NULL;
|
||||
g_keyHookThreadID = 0;
|
||||
g_keyHookEvent = NULL;
|
||||
g_keyboardLL = NULL;
|
||||
g_screenSaver = false;
|
||||
}
|
||||
|
||||
// save thread id. we'll post messages to this thread's
|
||||
// message queue.
|
||||
g_threadID = threadID;
|
||||
|
||||
// set defaults
|
||||
g_relay = false;
|
||||
g_zoneSides = 0;
|
||||
g_zoneSize = 0;
|
||||
g_xScreen = 0;
|
||||
g_yScreen = 0;
|
||||
g_wScreen = 0;
|
||||
g_hScreen = 0;
|
||||
g_cursor = NULL;
|
||||
g_cursorThread = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
cleanup(void)
|
||||
{
|
||||
assert(g_hinstance != NULL);
|
||||
|
||||
if (g_processID == GetCurrentProcessId()) {
|
||||
g_threadID = 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
install()
|
||||
{
|
||||
assert(g_hinstance != NULL);
|
||||
assert(g_keyboard == NULL);
|
||||
assert(g_mouse == NULL);
|
||||
assert(g_cbt == NULL);
|
||||
assert(g_getMessage == NULL || g_screenSaver);
|
||||
|
||||
// must be initialized
|
||||
if (g_threadID == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check for mouse wheel support
|
||||
g_wheelSupport = getWheelSupport();
|
||||
|
||||
// install keyboard hook
|
||||
g_keyboard = SetWindowsHookEx(WH_KEYBOARD,
|
||||
&keyboardHook,
|
||||
g_hinstance,
|
||||
0);
|
||||
if (g_keyboard == NULL) {
|
||||
g_threadID = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// install mouse hook
|
||||
g_mouse = SetWindowsHookEx(WH_MOUSE,
|
||||
&mouseHook,
|
||||
g_hinstance,
|
||||
0);
|
||||
if (g_mouse == NULL) {
|
||||
// uninstall keyboard hook before failing
|
||||
UnhookWindowsHookEx(g_keyboard);
|
||||
g_keyboard = NULL;
|
||||
g_threadID = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// install CBT hook
|
||||
g_cbt = SetWindowsHookEx(WH_CBT,
|
||||
&cbtHook,
|
||||
g_hinstance,
|
||||
0);
|
||||
if (g_cbt == NULL) {
|
||||
// uninstall keyboard and mouse hooks before failing
|
||||
UnhookWindowsHookEx(g_keyboard);
|
||||
UnhookWindowsHookEx(g_mouse);
|
||||
g_keyboard = NULL;
|
||||
g_mouse = NULL;
|
||||
g_threadID = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// install GetMessage hook (unless already installed)
|
||||
if (g_wheelSupport == kWheelOld && g_getMessage == NULL) {
|
||||
g_getMessage = SetWindowsHookEx(WH_GETMESSAGE,
|
||||
&getMessageHook,
|
||||
g_hinstance,
|
||||
0);
|
||||
// ignore failure; we just won't get mouse wheel messages
|
||||
}
|
||||
|
||||
// install low-level keyboard hook, if possible. since this hook
|
||||
// is called in the context of the installing thread and that
|
||||
// thread must have a message loop but we don't want the caller's
|
||||
// message loop to do the work, we'll fire up a separate thread
|
||||
// just for the hook. note that low-level keyboard hooks are only
|
||||
// available on windows NT SP3 and above.
|
||||
g_keyHookEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
if (g_keyHookEvent != NULL) {
|
||||
g_keyHookThread = CreateThread(NULL, 0, &getKeyboardLLProc, 0,
|
||||
CREATE_SUSPENDED, &g_keyHookThreadID);
|
||||
if (g_keyHookThread != NULL) {
|
||||
// start the thread and wait for it to initialize
|
||||
ResumeThread(g_keyHookThread);
|
||||
WaitForSingleObject(g_keyHookEvent, INFINITE);
|
||||
ResetEvent(g_keyHookEvent);
|
||||
|
||||
// the thread clears g_keyHookThreadID if it failed
|
||||
if (g_keyHookThreadID == 0) {
|
||||
CloseHandle(g_keyHookThread);
|
||||
g_keyHookThread = NULL;
|
||||
}
|
||||
}
|
||||
if (g_keyHookThread == NULL) {
|
||||
CloseHandle(g_keyHookEvent);
|
||||
g_keyHookEvent = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
uninstall(void)
|
||||
{
|
||||
assert(g_hinstance != NULL);
|
||||
|
||||
// uninstall hooks
|
||||
if (g_keyHookThread != NULL) {
|
||||
PostThreadMessage(g_keyHookThreadID, WM_QUIT, 0, 0);
|
||||
WaitForSingleObject(g_keyHookThread, INFINITE);
|
||||
CloseHandle(g_keyHookEvent);
|
||||
CloseHandle(g_keyHookThread);
|
||||
g_keyHookEvent = NULL;
|
||||
g_keyHookThread = NULL;
|
||||
g_keyHookThreadID = 0;
|
||||
}
|
||||
if (g_keyboard != NULL) {
|
||||
UnhookWindowsHookEx(g_keyboard);
|
||||
}
|
||||
if (g_mouse != NULL) {
|
||||
UnhookWindowsHookEx(g_mouse);
|
||||
}
|
||||
if (g_cbt != NULL) {
|
||||
UnhookWindowsHookEx(g_cbt);
|
||||
}
|
||||
if (g_getMessage != NULL && !g_screenSaver) {
|
||||
UnhookWindowsHookEx(g_getMessage);
|
||||
g_getMessage = NULL;
|
||||
}
|
||||
g_keyboard = NULL;
|
||||
g_mouse = NULL;
|
||||
g_cbt = NULL;
|
||||
g_wheelSupport = kWheelNone;
|
||||
|
||||
// show the cursor
|
||||
restoreCursor();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
installScreenSaver(void)
|
||||
{
|
||||
assert(g_hinstance != NULL);
|
||||
|
||||
// must be initialized
|
||||
if (g_threadID == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// generate screen saver messages
|
||||
g_screenSaver = true;
|
||||
|
||||
// install hook unless it's already installed
|
||||
if (g_getMessage == NULL) {
|
||||
g_getMessage = SetWindowsHookEx(WH_GETMESSAGE,
|
||||
&getMessageHook,
|
||||
g_hinstance,
|
||||
0);
|
||||
}
|
||||
|
||||
return (g_getMessage != NULL) ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
uninstallScreenSaver(void)
|
||||
{
|
||||
assert(g_hinstance != NULL);
|
||||
|
||||
// uninstall hook unless the mouse wheel hook is installed
|
||||
if (g_getMessage != NULL && g_wheelSupport == kWheelNone) {
|
||||
UnhookWindowsHookEx(g_getMessage);
|
||||
g_getMessage = NULL;
|
||||
}
|
||||
|
||||
// screen saver hook is no longer installed
|
||||
g_screenSaver = false;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
setSides(UInt32 sides)
|
||||
{
|
||||
g_zoneSides = sides;
|
||||
}
|
||||
|
||||
void
|
||||
setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h, SInt32 jumpZoneSize)
|
||||
{
|
||||
g_zoneSize = jumpZoneSize;
|
||||
g_xScreen = x;
|
||||
g_yScreen = y;
|
||||
g_wScreen = w;
|
||||
g_hScreen = h;
|
||||
}
|
||||
|
||||
void
|
||||
setRelay(int enable)
|
||||
{
|
||||
g_relay = (enable != 0);
|
||||
if (!g_relay) {
|
||||
restoreCursor();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
55
lib/platform/CSynergyHook.h
Normal file
55
lib/platform/CSynergyHook.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef CSYNERGYHOOK_H
|
||||
#define CSYNERGYHOOK_H
|
||||
|
||||
#include "BasicTypes.h"
|
||||
|
||||
#if WINDOWS_LIKE
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#else
|
||||
#error CSynergyHook is a win32 specific file
|
||||
#endif
|
||||
|
||||
#if defined(SYNRGYHK_EXPORTS)
|
||||
#define CSYNERGYHOOK_API __declspec(dllexport)
|
||||
#else
|
||||
#define CSYNERGYHOOK_API __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#define SYNERGY_MSG_MARK WM_APP + 0x0011 // mark id; <unused>
|
||||
#define SYNERGY_MSG_KEY WM_APP + 0x0012 // vk code; key data
|
||||
#define SYNERGY_MSG_MOUSE_BUTTON WM_APP + 0x0013 // button msg; <unused>
|
||||
#define SYNERGY_MSG_MOUSE_WHEEL WM_APP + 0x0014 // delta; <unused>
|
||||
#define SYNERGY_MSG_MOUSE_MOVE WM_APP + 0x0015 // x; y
|
||||
#define SYNERGY_MSG_POST_WARP WM_APP + 0x0016 // <unused>; <unused>
|
||||
#define SYNERGY_MSG_PRE_WARP WM_APP + 0x0017 // x; y
|
||||
#define SYNERGY_MSG_SCREEN_SAVER WM_APP + 0x0018 // activated; <unused>
|
||||
#define SYNERGY_MSG_INPUT_FIRST SYNERGY_MSG_KEY
|
||||
#define SYNERGY_MSG_INPUT_LAST SYNERGY_MSG_PRE_WARP
|
||||
|
||||
extern "C" {
|
||||
|
||||
typedef int (*InitFunc)(DWORD targetQueueThreadID);
|
||||
typedef int (*CleanupFunc)(void);
|
||||
typedef int (*InstallFunc)(void);
|
||||
typedef int (*UninstallFunc)(void);
|
||||
typedef int (*InstallScreenSaverFunc)(void);
|
||||
typedef int (*UninstallScreenSaverFunc)(void);
|
||||
typedef void (*SetSidesFunc)(UInt32);
|
||||
typedef void (*SetZoneFunc)(SInt32, SInt32, SInt32, SInt32, SInt32);
|
||||
typedef void (*SetRelayFunc)(int);
|
||||
|
||||
CSYNERGYHOOK_API int init(DWORD);
|
||||
CSYNERGYHOOK_API int cleanup(void);
|
||||
CSYNERGYHOOK_API int install(void);
|
||||
CSYNERGYHOOK_API int uninstall(void);
|
||||
CSYNERGYHOOK_API int installScreenSaver(void);
|
||||
CSYNERGYHOOK_API int uninstallScreenSaver(void);
|
||||
CSYNERGYHOOK_API void setSides(UInt32 sides);
|
||||
CSYNERGYHOOK_API void setZone(SInt32 x, SInt32 y, SInt32 w, SInt32 h,
|
||||
SInt32 jumpZoneSize);
|
||||
CSYNERGYHOOK_API void setRelay(int enable); // relay iff enable != 0
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
180
lib/platform/CUnixPlatform.cpp
Normal file
180
lib/platform/CUnixPlatform.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
#include "CUnixPlatform.h"
|
||||
#include "CLog.h"
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <syslog.h>
|
||||
|
||||
|
||||
//
|
||||
// CUnixPlatform
|
||||
//
|
||||
|
||||
CUnixPlatform::CUnixPlatform()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CUnixPlatform::~CUnixPlatform()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
bool
|
||||
CUnixPlatform::installDaemon(const char*, const char*, const char*, const char*)
|
||||
{
|
||||
// daemons don't require special installation
|
||||
return true;
|
||||
}
|
||||
|
||||
CUnixPlatform::EResult
|
||||
CUnixPlatform::uninstallDaemon(const char*)
|
||||
{
|
||||
// daemons don't require special installation
|
||||
return kSuccess;
|
||||
}
|
||||
|
||||
int
|
||||
CUnixPlatform::daemonize(const char* name, DaemonFunc func)
|
||||
{
|
||||
// fork so shell thinks we're done and so we're not a process
|
||||
// group leader
|
||||
switch (fork()) {
|
||||
case -1:
|
||||
// failed
|
||||
return -1;
|
||||
|
||||
case 0:
|
||||
// child
|
||||
break;
|
||||
|
||||
default:
|
||||
// parent exits
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// become leader of a new session
|
||||
setsid();
|
||||
|
||||
// chdir to root so we don't keep mounted filesystems points busy
|
||||
chdir("/");
|
||||
|
||||
// mask off permissions for any but owner
|
||||
umask(077);
|
||||
|
||||
// close open files. we only expect stdin, stdout, stderr to be open.
|
||||
close(0);
|
||||
close(1);
|
||||
close(2);
|
||||
|
||||
// attach file descriptors 0, 1, 2 to /dev/null so inadvertent use
|
||||
// of standard I/O safely goes in the bit bucket.
|
||||
open("/dev/null", O_RDONLY);
|
||||
open("/dev/null", O_RDWR);
|
||||
dup(1);
|
||||
|
||||
// hook up logger
|
||||
installDaemonLogger(name);
|
||||
|
||||
// invoke function
|
||||
return func(this, 1, &name);
|
||||
}
|
||||
|
||||
void
|
||||
CUnixPlatform::installDaemonLogger(const char* name)
|
||||
{
|
||||
openlog(name, 0, LOG_DAEMON);
|
||||
CLog::setOutputter(&CUnixPlatform::deamonLogger);
|
||||
}
|
||||
|
||||
const char*
|
||||
CUnixPlatform::getBasename(const char* pathname) const
|
||||
{
|
||||
if (pathname == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* basename = strrchr(pathname, '/');
|
||||
if (basename != NULL) {
|
||||
return basename + 1;
|
||||
}
|
||||
else {
|
||||
return pathname;
|
||||
}
|
||||
}
|
||||
|
||||
CString
|
||||
CUnixPlatform::getUserDirectory() const
|
||||
{
|
||||
#if HAVE_GETPWUID_R
|
||||
struct passwd pwent;
|
||||
struct passwd* pwentp;
|
||||
long size = sysconf(_SC_GETPW_R_SIZE_MAX);
|
||||
char* buffer = new char[size];
|
||||
getpwuid_r(getuid(), &pwent, buffer, size, &pwentp);
|
||||
delete[] buffer;
|
||||
#else
|
||||
struct passwd* pwentp = getpwuid(getuid());
|
||||
#endif
|
||||
if (pwentp != NULL && pwentp->pw_dir != NULL) {
|
||||
return pwentp->pw_dir;
|
||||
}
|
||||
else {
|
||||
return CString();
|
||||
}
|
||||
}
|
||||
|
||||
CString
|
||||
CUnixPlatform::getSystemDirectory() const
|
||||
{
|
||||
return "/etc";
|
||||
}
|
||||
|
||||
CString
|
||||
CUnixPlatform::addPathComponent(const CString& prefix,
|
||||
const CString& suffix) const
|
||||
{
|
||||
CString path;
|
||||
path.reserve(prefix.size() + 1 + suffix.size());
|
||||
path += prefix;
|
||||
if (path.size() == 0 || path[path.size() - 1] != '/') {
|
||||
path += '/';
|
||||
}
|
||||
path += suffix;
|
||||
return path;
|
||||
}
|
||||
|
||||
bool
|
||||
CUnixPlatform::deamonLogger(int priority, const char* msg)
|
||||
{
|
||||
// convert priority
|
||||
switch (priority) {
|
||||
case CLog::kFATAL:
|
||||
case CLog::kERROR:
|
||||
priority = LOG_ERR;
|
||||
break;
|
||||
|
||||
case CLog::kWARNING:
|
||||
priority = LOG_WARNING;
|
||||
break;
|
||||
|
||||
case CLog::kNOTE:
|
||||
priority = LOG_NOTICE;
|
||||
break;
|
||||
|
||||
case CLog::kINFO:
|
||||
priority = LOG_INFO;
|
||||
break;
|
||||
|
||||
default:
|
||||
priority = LOG_DEBUG;
|
||||
break;
|
||||
}
|
||||
|
||||
// log it
|
||||
syslog(priority, "%s", msg);
|
||||
return true;
|
||||
}
|
||||
31
lib/platform/CUnixPlatform.h
Normal file
31
lib/platform/CUnixPlatform.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef CUNIXPLATFORM_H
|
||||
#define CUNIXPLATFORM_H
|
||||
|
||||
#include "IPlatform.h"
|
||||
|
||||
//! Unix platform dependent functions
|
||||
class CUnixPlatform : public IPlatform {
|
||||
public:
|
||||
CUnixPlatform();
|
||||
virtual ~CUnixPlatform();
|
||||
|
||||
// IPlatform overrides
|
||||
virtual bool installDaemon(const char* name,
|
||||
const char* description,
|
||||
const char* pathname,
|
||||
const char* commandLine);
|
||||
virtual EResult uninstallDaemon(const char* name);
|
||||
virtual int daemonize(const char* name, DaemonFunc);
|
||||
virtual void installDaemonLogger(const char* name);
|
||||
virtual const char* getBasename(const char* pathname) const;
|
||||
virtual CString getUserDirectory() const;
|
||||
virtual CString getSystemDirectory() const;
|
||||
virtual CString addPathComponent(
|
||||
const CString& prefix,
|
||||
const CString& suffix) const;
|
||||
|
||||
private:
|
||||
static bool deamonLogger(int, const char*);
|
||||
};
|
||||
|
||||
#endif
|
||||
876
lib/platform/CWin32Platform.cpp
Normal file
876
lib/platform/CWin32Platform.cpp
Normal file
@@ -0,0 +1,876 @@
|
||||
#include "CWin32Platform.h"
|
||||
#include "CLock.h"
|
||||
#include "CThread.h"
|
||||
#include "CLog.h"
|
||||
#include "TMethodJob.h"
|
||||
#include "stdvector.h"
|
||||
#include <cstring>
|
||||
#include <shlobj.h>
|
||||
#include <tchar.h>
|
||||
|
||||
//
|
||||
// CWin32Platform
|
||||
//
|
||||
|
||||
HANDLE CWin32Platform::s_eventLog = NULL;
|
||||
CWin32Platform* CWin32Platform::s_daemonPlatform = NULL;
|
||||
|
||||
CWin32Platform::CWin32Platform()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CWin32Platform::~CWin32Platform()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
bool
|
||||
CWin32Platform::isWindows95Family()
|
||||
{
|
||||
OSVERSIONINFO version;
|
||||
version.dwOSVersionInfoSize = sizeof(version);
|
||||
if (GetVersionEx(&version) == 0) {
|
||||
log((CLOG_WARN "cannot determine OS: %d", GetLastError()));
|
||||
return true;
|
||||
}
|
||||
return (version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::setStatus(SERVICE_STATUS_HANDLE handle, DWORD state)
|
||||
{
|
||||
setStatus(handle, state, 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::setStatus(SERVICE_STATUS_HANDLE handle,
|
||||
DWORD state, DWORD step, DWORD waitHint)
|
||||
{
|
||||
SERVICE_STATUS status;
|
||||
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS |
|
||||
SERVICE_INTERACTIVE_PROCESS;
|
||||
status.dwCurrentState = state;
|
||||
status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
|
||||
SERVICE_ACCEPT_PAUSE_CONTINUE |
|
||||
SERVICE_ACCEPT_SHUTDOWN;
|
||||
status.dwWin32ExitCode = NO_ERROR;
|
||||
status.dwServiceSpecificExitCode = 0;
|
||||
status.dwCheckPoint = step;
|
||||
status.dwWaitHint = waitHint;
|
||||
SetServiceStatus(handle, &status);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::setStatusError(SERVICE_STATUS_HANDLE handle, DWORD error)
|
||||
{
|
||||
SERVICE_STATUS status;
|
||||
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS |
|
||||
SERVICE_INTERACTIVE_PROCESS;
|
||||
status.dwCurrentState = SERVICE_STOPPED;
|
||||
status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
|
||||
SERVICE_ACCEPT_PAUSE_CONTINUE |
|
||||
SERVICE_ACCEPT_SHUTDOWN;
|
||||
status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
|
||||
status.dwServiceSpecificExitCode = error;
|
||||
status.dwCheckPoint = 0;
|
||||
status.dwWaitHint = 0;
|
||||
SetServiceStatus(handle, &status);
|
||||
}
|
||||
|
||||
bool
|
||||
CWin32Platform::installDaemon(const char* name, const char* description,
|
||||
const char* pathname, const char* commandLine)
|
||||
{
|
||||
// windows 95 family services
|
||||
if (isWindows95Family()) {
|
||||
// open registry
|
||||
HKEY key = open95ServicesKey();
|
||||
if (key == NULL) {
|
||||
log((CLOG_ERR "cannot open RunServices registry key", GetLastError()));
|
||||
return false;
|
||||
}
|
||||
|
||||
// construct entry
|
||||
CString value;
|
||||
value += "\"";
|
||||
value += pathname;
|
||||
value += "\" ";
|
||||
value += commandLine;
|
||||
|
||||
// install entry
|
||||
setValue(key, name, value);
|
||||
|
||||
// clean up
|
||||
closeKey(key);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// windows NT family services
|
||||
else {
|
||||
// open service manager
|
||||
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
||||
if (mgr == NULL) {
|
||||
log((CLOG_ERR "OpenSCManager failed with %d", GetLastError()));
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the servie
|
||||
SC_HANDLE service = CreateService(mgr,
|
||||
name,
|
||||
name,
|
||||
0,
|
||||
SERVICE_WIN32_OWN_PROCESS |
|
||||
SERVICE_INTERACTIVE_PROCESS,
|
||||
SERVICE_AUTO_START,
|
||||
SERVICE_ERROR_NORMAL,
|
||||
pathname,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
// done with service and manager
|
||||
if (service != NULL) {
|
||||
CloseServiceHandle(service);
|
||||
CloseServiceHandle(mgr);
|
||||
}
|
||||
else {
|
||||
log((CLOG_ERR "CreateService failed with %d", GetLastError()));
|
||||
CloseServiceHandle(mgr);
|
||||
return false;
|
||||
}
|
||||
|
||||
// open the registry key for this service
|
||||
HKEY key = openNTServicesKey();
|
||||
key = openKey(key, name);
|
||||
if (key == NULL) {
|
||||
// can't open key
|
||||
uninstallDaemon(name);
|
||||
return false;
|
||||
}
|
||||
|
||||
// set the description
|
||||
setValue(key, "Description", description);
|
||||
|
||||
// set command line
|
||||
key = openKey(key, "Parameters");
|
||||
if (key == NULL) {
|
||||
// can't open key
|
||||
uninstallDaemon(name);
|
||||
return false;
|
||||
}
|
||||
setValue(key, "CommandLine", commandLine);
|
||||
|
||||
// done with registry
|
||||
closeKey(key);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
IPlatform::EResult
|
||||
CWin32Platform::uninstallDaemon(const char* name)
|
||||
{
|
||||
// windows 95 family services
|
||||
if (isWindows95Family()) {
|
||||
// open registry
|
||||
HKEY key = open95ServicesKey();
|
||||
if (key == NULL) {
|
||||
log((CLOG_ERR "cannot open RunServices registry key", GetLastError()));
|
||||
return kAlready;
|
||||
}
|
||||
|
||||
// remove entry
|
||||
deleteValue(key, name);
|
||||
|
||||
// clean up
|
||||
closeKey(key);
|
||||
|
||||
return kSuccess;
|
||||
}
|
||||
|
||||
// windows NT family services
|
||||
else {
|
||||
// remove parameters for this service
|
||||
HKEY key = openNTServicesKey();
|
||||
key = openKey(key, name);
|
||||
if (key != NULL) {
|
||||
deleteKey(key, "Parameters");
|
||||
closeKey(key);
|
||||
}
|
||||
|
||||
// open service manager
|
||||
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
|
||||
if (mgr == NULL) {
|
||||
log((CLOG_ERR "OpenSCManager failed with %d", GetLastError()));
|
||||
return kFailed;
|
||||
}
|
||||
|
||||
// open the service. oddly, you must open a service to delete it.
|
||||
EResult result;
|
||||
SC_HANDLE service = OpenService(mgr, name, DELETE);
|
||||
if (service == NULL) {
|
||||
const DWORD e = GetLastError();
|
||||
log((CLOG_ERR "OpenService failed with %d", e));
|
||||
result = (e == ERROR_SERVICE_DOES_NOT_EXIST) ? kAlready : kFailed;
|
||||
}
|
||||
|
||||
else {
|
||||
if (DeleteService(service) != 0) {
|
||||
result = kSuccess;
|
||||
}
|
||||
else {
|
||||
const DWORD e = GetLastError();
|
||||
switch (e) {
|
||||
case ERROR_SERVICE_MARKED_FOR_DELETE:
|
||||
result = kAlready;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = kFailed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CloseServiceHandle(service);
|
||||
}
|
||||
|
||||
// close the manager
|
||||
CloseServiceHandle(mgr);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
CWin32Platform::daemonize(const char* name, DaemonFunc func)
|
||||
{
|
||||
assert(name != NULL);
|
||||
assert(func != NULL);
|
||||
|
||||
// windows 95 family services
|
||||
if (isWindows95Family()) {
|
||||
typedef DWORD (WINAPI *RegisterServiceProcessT)(DWORD, DWORD);
|
||||
|
||||
// mark this process as a service so it's not killed when the
|
||||
// user logs off.
|
||||
HINSTANCE kernel = LoadLibrary("kernel32.dll");
|
||||
if (kernel == NULL) {
|
||||
log((CLOG_ERR "LoadLibrary failed with %d", GetLastError()));
|
||||
return -1;
|
||||
}
|
||||
RegisterServiceProcessT RegisterServiceProcess =
|
||||
reinterpret_cast<RegisterServiceProcessT>(
|
||||
GetProcAddress(kernel,
|
||||
_T("RegisterServiceProcess")));
|
||||
if (RegisterServiceProcess == NULL) {
|
||||
log((CLOG_ERR "can't lookup RegisterServiceProcess: %d", GetLastError()));
|
||||
FreeLibrary(kernel);
|
||||
return -1;
|
||||
}
|
||||
if (RegisterServiceProcess(NULL, 1) == 0) {
|
||||
log((CLOG_ERR "RegisterServiceProcess failed with %d", GetLastError()));
|
||||
FreeLibrary(kernel);
|
||||
return -1;
|
||||
}
|
||||
FreeLibrary(kernel);
|
||||
|
||||
// now simply call the daemon function
|
||||
return func(this, 1, &name);
|
||||
}
|
||||
|
||||
// windows NT family services
|
||||
else {
|
||||
// save daemon function
|
||||
m_daemonFunc = func;
|
||||
|
||||
// construct the service entry
|
||||
SERVICE_TABLE_ENTRY entry[2];
|
||||
entry[0].lpServiceName = const_cast<char*>(name);
|
||||
entry[0].lpServiceProc = &CWin32Platform::serviceMainEntry;
|
||||
entry[1].lpServiceName = NULL;
|
||||
entry[1].lpServiceProc = NULL;
|
||||
|
||||
// hook us up to the service control manager. this won't return
|
||||
// (if successful) until the processes have terminated.
|
||||
s_daemonPlatform = this;
|
||||
if (StartServiceCtrlDispatcher(entry)) {
|
||||
s_daemonPlatform = NULL;
|
||||
return m_daemonResult;
|
||||
}
|
||||
log((CLOG_ERR "StartServiceCtrlDispatcher failed with %d", GetLastError()));
|
||||
s_daemonPlatform = NULL;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::installDaemonLogger(const char* name)
|
||||
{
|
||||
if (!CWin32Platform::isWindows95Family()) {
|
||||
// open event log and direct log messages to it
|
||||
if (s_eventLog == NULL) {
|
||||
s_eventLog = RegisterEventSource(NULL, name);
|
||||
if (s_eventLog != NULL) {
|
||||
CLog::setOutputter(&CWin32Platform::serviceLogger);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char*
|
||||
CWin32Platform::getBasename(const char* pathname) const
|
||||
{
|
||||
if (pathname == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check for last /
|
||||
const char* basename = strrchr(pathname, '/');
|
||||
if (basename != NULL) {
|
||||
++basename;
|
||||
}
|
||||
else {
|
||||
basename = pathname;
|
||||
}
|
||||
|
||||
// check for last backslash
|
||||
const char* basename2 = strrchr(pathname, '\\');
|
||||
if (basename2 != NULL && basename2 > basename) {
|
||||
basename = basename2 + 1;
|
||||
}
|
||||
|
||||
return basename;
|
||||
}
|
||||
|
||||
CString
|
||||
CWin32Platform::getUserDirectory() const
|
||||
{
|
||||
// try %HOMEPATH%
|
||||
TCHAR dir[MAX_PATH];
|
||||
DWORD size = sizeof(dir) / sizeof(TCHAR);
|
||||
DWORD result = GetEnvironmentVariable(_T("HOMEPATH"), dir, size);
|
||||
if (result != 0 && result <= size) {
|
||||
// sanity check -- if dir doesn't appear to start with a
|
||||
// drive letter and isn't a UNC name then don't use it
|
||||
// FIXME -- allow UNC names
|
||||
if (dir[0] != '\0' && (dir[1] == ':' ||
|
||||
((dir[0] == '\\' || dir[0] == '/') &&
|
||||
(dir[1] == '\\' || dir[1] == '/')))) {
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
|
||||
// get the location of the personal files. that's as close to
|
||||
// a home directory as we're likely to find.
|
||||
ITEMIDLIST* idl;
|
||||
if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &idl))) {
|
||||
TCHAR* path = NULL;
|
||||
if (SHGetPathFromIDList(idl, dir)) {
|
||||
DWORD attr = GetFileAttributes(dir);
|
||||
if (attr != 0xffffffff && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
||||
path = dir;
|
||||
}
|
||||
|
||||
IMalloc* shalloc;
|
||||
if (SUCCEEDED(SHGetMalloc(&shalloc))) {
|
||||
shalloc->Free(idl);
|
||||
shalloc->Release();
|
||||
}
|
||||
|
||||
if (path != NULL) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// use root of C drive as a default
|
||||
return "C:";
|
||||
}
|
||||
|
||||
CString
|
||||
CWin32Platform::getSystemDirectory() const
|
||||
{
|
||||
// get windows directory
|
||||
char dir[MAX_PATH];
|
||||
if (GetWindowsDirectory(dir, sizeof(dir)) != 0) {
|
||||
return dir;
|
||||
}
|
||||
else {
|
||||
// can't get it. use C:\ as a default.
|
||||
return "C:";
|
||||
}
|
||||
}
|
||||
|
||||
CString
|
||||
CWin32Platform::addPathComponent(const CString& prefix,
|
||||
const CString& suffix) const
|
||||
{
|
||||
CString path;
|
||||
path.reserve(prefix.size() + 1 + suffix.size());
|
||||
path += prefix;
|
||||
if (path.size() == 0 ||
|
||||
(path[path.size() - 1] != '\\' &&
|
||||
path[path.size() - 1] != '/')) {
|
||||
path += '\\';
|
||||
}
|
||||
path += suffix;
|
||||
return path;
|
||||
}
|
||||
|
||||
HKEY
|
||||
CWin32Platform::openKey(HKEY key, const char* keyName)
|
||||
{
|
||||
// open next key
|
||||
HKEY newKey;
|
||||
LONG result = RegOpenKeyEx(key, keyName, 0,
|
||||
KEY_WRITE | KEY_QUERY_VALUE, &newKey);
|
||||
if (result != ERROR_SUCCESS) {
|
||||
DWORD disp;
|
||||
result = RegCreateKeyEx(key, keyName, 0, _T(""),
|
||||
0, KEY_WRITE | KEY_QUERY_VALUE,
|
||||
NULL, &newKey, &disp);
|
||||
}
|
||||
if (result != ERROR_SUCCESS) {
|
||||
RegCloseKey(key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// switch to new key
|
||||
RegCloseKey(key);
|
||||
return newKey;
|
||||
}
|
||||
|
||||
HKEY
|
||||
CWin32Platform::openKey(HKEY key, const char** keyNames)
|
||||
{
|
||||
for (UInt32 i = 0; key != NULL && keyNames[i] != NULL; ++i) {
|
||||
// open next key
|
||||
key = openKey(key, keyNames[i]);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::closeKey(HKEY key)
|
||||
{
|
||||
assert(key != NULL);
|
||||
RegCloseKey(key);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::deleteKey(HKEY key, const char* name)
|
||||
{
|
||||
assert(key != NULL);
|
||||
assert(name != NULL);
|
||||
RegDeleteKey(key, name);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::deleteValue(HKEY key, const char* name)
|
||||
{
|
||||
assert(key != NULL);
|
||||
assert(name != NULL);
|
||||
RegDeleteValue(key, name);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::setValue(HKEY key, const char* name, const CString& value)
|
||||
{
|
||||
assert(key != NULL);
|
||||
assert(name != NULL);
|
||||
RegSetValueEx(key, name, 0, REG_SZ,
|
||||
reinterpret_cast<const BYTE*>(value.c_str()),
|
||||
value.size() + 1);
|
||||
}
|
||||
|
||||
CString
|
||||
CWin32Platform::readValueString(HKEY key, const char* name)
|
||||
{
|
||||
// get the size of the string
|
||||
DWORD type;
|
||||
DWORD size = 0;
|
||||
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, &size);
|
||||
if (result != ERROR_SUCCESS || type != REG_SZ) {
|
||||
return CString();
|
||||
}
|
||||
|
||||
// allocate space
|
||||
char* buffer = new char[size];
|
||||
|
||||
// read it
|
||||
result = RegQueryValueEx(key, name, 0, &type,
|
||||
reinterpret_cast<BYTE*>(buffer), &size);
|
||||
if (result != ERROR_SUCCESS || type != REG_SZ) {
|
||||
delete[] buffer;
|
||||
return CString();
|
||||
}
|
||||
|
||||
// clean up and return value
|
||||
CString value(buffer);
|
||||
delete[] buffer;
|
||||
return value;
|
||||
}
|
||||
|
||||
HKEY
|
||||
CWin32Platform::openNTServicesKey()
|
||||
{
|
||||
static const char* s_keyNames[] = {
|
||||
_T("SYSTEM"),
|
||||
_T("CurrentControlSet"),
|
||||
_T("Services"),
|
||||
NULL
|
||||
};
|
||||
|
||||
return openKey(HKEY_LOCAL_MACHINE, s_keyNames);
|
||||
}
|
||||
|
||||
HKEY
|
||||
CWin32Platform::open95ServicesKey()
|
||||
{
|
||||
static const char* s_keyNames[] = {
|
||||
_T("Software"),
|
||||
_T("Microsoft"),
|
||||
_T("Windows"),
|
||||
_T("CurrentVersion"),
|
||||
_T("RunServices"),
|
||||
NULL
|
||||
};
|
||||
|
||||
return openKey(HKEY_LOCAL_MACHINE, s_keyNames);
|
||||
}
|
||||
|
||||
int
|
||||
CWin32Platform::runDaemon(RunFunc run, StopFunc stop)
|
||||
{
|
||||
// should only be called from DaemonFunc
|
||||
assert(m_serviceMutex != NULL);
|
||||
|
||||
CLock lock(m_serviceMutex);
|
||||
try {
|
||||
int result;
|
||||
m_stop = stop;
|
||||
m_serviceHandlerWaiting = false;
|
||||
m_serviceRunning = false;
|
||||
for (;;) {
|
||||
// mark server as running
|
||||
setStatus(m_statusHandle, SERVICE_RUNNING);
|
||||
|
||||
// run callback in another thread
|
||||
m_serviceRunning = true;
|
||||
{
|
||||
CThread thread(new TMethodJob<CWin32Platform>(this,
|
||||
&CWin32Platform::runDaemonThread, run));
|
||||
result = reinterpret_cast<int>(thread.getResult());
|
||||
}
|
||||
m_serviceRunning = false;
|
||||
|
||||
// notify handler that the server stopped. if handler
|
||||
// isn't waiting then we stopped unexpectedly and we
|
||||
// quit.
|
||||
if (m_serviceHandlerWaiting) {
|
||||
m_serviceHandlerWaiting = false;
|
||||
m_serviceState->broadcast();
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
|
||||
// wait until we're told what to do next
|
||||
while (*m_serviceState != SERVICE_RUNNING &&
|
||||
*m_serviceState != SERVICE_STOPPED) {
|
||||
m_serviceState->wait();
|
||||
}
|
||||
|
||||
// exit loop if we've been told to stop
|
||||
if (*m_serviceState == SERVICE_STOPPED) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// prevent daemonHandler from changing state
|
||||
*m_serviceState = SERVICE_STOPPED;
|
||||
|
||||
// tell service control that the service is stopped.
|
||||
// FIXME -- hopefully this will ensure that our handler won't
|
||||
// be called again but i can't find documentation that
|
||||
// verifies that. if it does it'll crash on the mutex that
|
||||
// we're about to destroy.
|
||||
setStatus(m_statusHandle, *m_serviceState);
|
||||
|
||||
// clean up
|
||||
m_stop = NULL;
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (...) {
|
||||
// FIXME -- report error
|
||||
|
||||
// prevent serviceHandler from changing state
|
||||
*m_serviceState = SERVICE_STOPPED;
|
||||
|
||||
// set status
|
||||
setStatusError(m_statusHandle, 0);
|
||||
|
||||
// wake up serviceHandler if it's waiting then wait for it
|
||||
if (m_serviceHandlerWaiting) {
|
||||
m_serviceHandlerWaiting = false;
|
||||
m_serviceState->broadcast();
|
||||
m_serviceState->wait();
|
||||
// serviceHandler has exited by now
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::runDaemonThread(void* vrun)
|
||||
{
|
||||
RunFunc run = reinterpret_cast<RunFunc>(vrun);
|
||||
CThread::exit(reinterpret_cast<void*>(run(m_serviceMutex)));
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::serviceMain(DWORD argc, LPTSTR* argvIn)
|
||||
{
|
||||
typedef std::vector<LPCTSTR> ArgList;
|
||||
typedef std::vector<CString> Arguments;
|
||||
const char** argv = const_cast<const char**>(argvIn);
|
||||
|
||||
// open event log and direct log messages to it
|
||||
installDaemonLogger(argv[0]);
|
||||
|
||||
// create synchronization objects
|
||||
CThread::init();
|
||||
m_serviceMutex = new CMutex;
|
||||
m_serviceState = new CCondVar<DWORD>(m_serviceMutex, SERVICE_RUNNING);
|
||||
|
||||
// register our service handler functiom
|
||||
m_statusHandle = RegisterServiceCtrlHandler(argv[0],
|
||||
&CWin32Platform::serviceHandlerEntry);
|
||||
if (m_statusHandle == NULL) {
|
||||
// cannot start as service
|
||||
m_daemonResult = -1;
|
||||
delete m_serviceState;
|
||||
delete m_serviceMutex;
|
||||
return;
|
||||
}
|
||||
|
||||
// tell service control manager that we're starting
|
||||
setStatus(m_statusHandle, SERVICE_START_PENDING, 0, 1000);
|
||||
|
||||
// if no arguments supplied then try getting them from the registry.
|
||||
// the first argument doesn't count because it's the service name.
|
||||
Arguments args;
|
||||
ArgList myArgv;
|
||||
if (argc <= 1) {
|
||||
// read command line
|
||||
CString commandLine;
|
||||
HKEY key = openNTServicesKey();
|
||||
key = openKey(key, argv[0]);
|
||||
key = openKey(key, "Parameters");
|
||||
if (key != NULL) {
|
||||
commandLine = readValueString(key, "CommandLine");
|
||||
}
|
||||
|
||||
// if the command line isn't empty then parse and use it
|
||||
if (!commandLine.empty()) {
|
||||
// parse, honoring double quoted substrings
|
||||
CString::size_type i = commandLine.find_first_not_of(" \t");
|
||||
while (i != CString::npos && i != commandLine.size()) {
|
||||
// find end of string
|
||||
CString::size_type e;
|
||||
if (commandLine[i] == '\"') {
|
||||
// quoted. find closing quote.
|
||||
++i;
|
||||
e = commandLine.find("\"", i);
|
||||
|
||||
// whitespace must follow closing quote
|
||||
if (e == CString::npos ||
|
||||
(e + 1 != commandLine.size() &&
|
||||
commandLine[e + 1] != ' ' &&
|
||||
commandLine[e + 1] != '\t')) {
|
||||
args.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
// extract
|
||||
args.push_back(commandLine.substr(i, e - i));
|
||||
i = e + 1;
|
||||
}
|
||||
else {
|
||||
// unquoted. find next whitespace.
|
||||
e = commandLine.find_first_of(" \t", i);
|
||||
if (e == CString::npos) {
|
||||
e = commandLine.size();
|
||||
}
|
||||
|
||||
// extract
|
||||
args.push_back(commandLine.substr(i, e - i));
|
||||
i = e + 1;
|
||||
}
|
||||
|
||||
// next argument
|
||||
i = commandLine.find_first_not_of(" \t", i);
|
||||
}
|
||||
|
||||
// service name goes first
|
||||
myArgv.push_back(argv[0]);
|
||||
|
||||
// get pointers
|
||||
for (UInt32 i = 0; i < args.size(); ++i) {
|
||||
myArgv.push_back(args[i].c_str());
|
||||
}
|
||||
|
||||
// adjust argc/argv
|
||||
argc = myArgv.size();
|
||||
argv = &myArgv[0];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// invoke daemon function
|
||||
m_daemonResult = m_daemonFunc(this, static_cast<int>(argc), argv);
|
||||
}
|
||||
catch (CDaemonFailed& e) {
|
||||
setStatusError(m_statusHandle, e.m_result);
|
||||
m_daemonResult = -1;
|
||||
}
|
||||
catch (...) {
|
||||
setStatusError(m_statusHandle, 1);
|
||||
m_daemonResult = -1;
|
||||
}
|
||||
|
||||
// clean up
|
||||
delete m_serviceState;
|
||||
delete m_serviceMutex;
|
||||
|
||||
// FIXME -- close event log?
|
||||
}
|
||||
|
||||
void WINAPI
|
||||
CWin32Platform::serviceMainEntry(DWORD argc, LPTSTR* argv)
|
||||
{
|
||||
s_daemonPlatform->serviceMain(argc, argv);
|
||||
}
|
||||
|
||||
void
|
||||
CWin32Platform::serviceHandler(DWORD ctrl)
|
||||
{
|
||||
assert(m_serviceMutex != NULL);
|
||||
assert(m_serviceState != NULL);
|
||||
|
||||
CLock lock(m_serviceMutex);
|
||||
|
||||
// ignore request if service is already stopped
|
||||
if (*m_serviceState == SERVICE_STOPPED) {
|
||||
setStatus(m_statusHandle, *m_serviceState);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (ctrl) {
|
||||
case SERVICE_CONTROL_PAUSE:
|
||||
// update state
|
||||
*m_serviceState = SERVICE_PAUSE_PENDING;
|
||||
setStatus(m_statusHandle, *m_serviceState, 0, 1000);
|
||||
|
||||
// stop run callback if running and wait for it to finish
|
||||
if (m_serviceRunning) {
|
||||
m_serviceHandlerWaiting = true;
|
||||
m_stop();
|
||||
m_serviceState->wait();
|
||||
}
|
||||
|
||||
// update state if service hasn't stopped while we were waiting
|
||||
if (*m_serviceState != SERVICE_STOPPED) {
|
||||
*m_serviceState = SERVICE_PAUSED;
|
||||
}
|
||||
m_serviceState->broadcast();
|
||||
break;
|
||||
|
||||
case SERVICE_CONTROL_CONTINUE:
|
||||
// required status update
|
||||
setStatus(m_statusHandle, *m_serviceState);
|
||||
|
||||
// update state but let main loop send RUNNING notification
|
||||
*m_serviceState = SERVICE_RUNNING;
|
||||
m_serviceState->broadcast();
|
||||
return;
|
||||
|
||||
case SERVICE_CONTROL_STOP:
|
||||
case SERVICE_CONTROL_SHUTDOWN:
|
||||
// update state
|
||||
*m_serviceState = SERVICE_STOP_PENDING;
|
||||
setStatus(m_statusHandle, *m_serviceState, 0, 1000);
|
||||
|
||||
// stop run callback if running and wait for it to finish
|
||||
if (m_serviceRunning) {
|
||||
m_serviceHandlerWaiting = true;
|
||||
m_stop();
|
||||
m_serviceState->wait();
|
||||
}
|
||||
|
||||
// update state
|
||||
*m_serviceState = SERVICE_STOPPED;
|
||||
m_serviceState->broadcast();
|
||||
break;
|
||||
|
||||
default:
|
||||
log((CLOG_WARN "unknown service command: %d", ctrl));
|
||||
// fall through
|
||||
|
||||
case SERVICE_CONTROL_INTERROGATE:
|
||||
break;
|
||||
}
|
||||
|
||||
// send update
|
||||
setStatus(m_statusHandle, *m_serviceState);
|
||||
}
|
||||
|
||||
void WINAPI
|
||||
CWin32Platform::serviceHandlerEntry(DWORD ctrl)
|
||||
{
|
||||
s_daemonPlatform->serviceHandler(ctrl);
|
||||
}
|
||||
|
||||
bool
|
||||
CWin32Platform::serviceLogger(int priority, const char* msg)
|
||||
{
|
||||
if (s_eventLog == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// convert priority
|
||||
WORD type;
|
||||
switch (priority) {
|
||||
case CLog::kFATAL:
|
||||
case CLog::kERROR:
|
||||
type = EVENTLOG_ERROR_TYPE;
|
||||
break;
|
||||
|
||||
case CLog::kWARNING:
|
||||
type = EVENTLOG_WARNING_TYPE;
|
||||
break;
|
||||
|
||||
default:
|
||||
type = EVENTLOG_INFORMATION_TYPE;
|
||||
break;
|
||||
}
|
||||
|
||||
// log it
|
||||
// FIXME -- win32 wants to use a message table to look up event
|
||||
// strings. log messages aren't organized that way so we'll
|
||||
// just dump our string into the raw data section of the event
|
||||
// so users can at least see the message. note that we use our
|
||||
// priority as the event category.
|
||||
ReportEvent(s_eventLog, type, static_cast<WORD>(priority),
|
||||
0, // event ID
|
||||
NULL,
|
||||
0,
|
||||
strlen(msg + 1), // raw data size
|
||||
NULL,
|
||||
const_cast<char*>(msg));// raw data
|
||||
return true;
|
||||
}
|
||||
110
lib/platform/CWin32Platform.h
Normal file
110
lib/platform/CWin32Platform.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#ifndef CWIN32PLATFORM_H
|
||||
#define CWIN32PLATFORM_H
|
||||
|
||||
#include "IPlatform.h"
|
||||
#include "CCondVar.h"
|
||||
#include "CMutex.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
//! Microsoft windows platform dependent functions
|
||||
class CWin32Platform : public IPlatform {
|
||||
public:
|
||||
typedef int (*RunFunc)(CMutex*);
|
||||
typedef void (*StopFunc)(void);
|
||||
|
||||
CWin32Platform();
|
||||
virtual ~CWin32Platform();
|
||||
|
||||
//! Test if windows 95, et al.
|
||||
/*!
|
||||
Returns true iff the platform is win95/98/me.
|
||||
*/
|
||||
static bool isWindows95Family();
|
||||
|
||||
//! Utility for calling SetServiceStatus()
|
||||
static void setStatus(SERVICE_STATUS_HANDLE, DWORD state);
|
||||
//! Utility for calling SetServiceStatus()
|
||||
static void setStatus(SERVICE_STATUS_HANDLE,
|
||||
DWORD state, DWORD step, DWORD waitHint);
|
||||
//! Utility for calling SetServiceStatus()
|
||||
static void setStatusError(SERVICE_STATUS_HANDLE, DWORD error);
|
||||
|
||||
//! 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.
|
||||
*/
|
||||
int runDaemon(RunFunc, StopFunc);
|
||||
|
||||
//! Daemon failed exception
|
||||
/*!
|
||||
Thrown by RunFunc on service failure. Result is the error
|
||||
code reported by the service.
|
||||
*/
|
||||
class CDaemonFailed {
|
||||
public:
|
||||
CDaemonFailed(int result) : m_result(result) { }
|
||||
|
||||
public:
|
||||
int m_result;
|
||||
};
|
||||
|
||||
// IPlatform overrides
|
||||
virtual bool installDaemon(const char* name,
|
||||
const char* description,
|
||||
const char* pathname,
|
||||
const char* commandLine);
|
||||
virtual EResult uninstallDaemon(const char* name);
|
||||
virtual int daemonize(const char* name, DaemonFunc);
|
||||
virtual void installDaemonLogger(const char* name);
|
||||
virtual const char* getBasename(const char* pathname) const;
|
||||
virtual CString getUserDirectory() const;
|
||||
virtual CString getSystemDirectory() const;
|
||||
virtual CString addPathComponent(
|
||||
const CString& prefix,
|
||||
const CString& suffix) const;
|
||||
|
||||
private:
|
||||
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,
|
||||
const CString& value);
|
||||
static CString readValueString(HKEY, const char* name);
|
||||
static HKEY openNTServicesKey();
|
||||
static HKEY open95ServicesKey();
|
||||
|
||||
void serviceMain(DWORD, LPTSTR*);
|
||||
static void WINAPI serviceMainEntry(DWORD, LPTSTR*);
|
||||
|
||||
void runDaemonThread(void*);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
#endif
|
||||
1442
lib/platform/CXWindowsClipboard.cpp
Normal file
1442
lib/platform/CXWindowsClipboard.cpp
Normal file
File diff suppressed because it is too large
Load Diff
358
lib/platform/CXWindowsClipboard.h
Normal file
358
lib/platform/CXWindowsClipboard.h
Normal file
@@ -0,0 +1,358 @@
|
||||
#ifndef CXWINDOWSCLIPBOARD_H
|
||||
#define CXWINDOWSCLIPBOARD_H
|
||||
|
||||
#include "IClipboard.h"
|
||||
#include "ClipboardTypes.h"
|
||||
#include "stdmap.h"
|
||||
#include "stdlist.h"
|
||||
#include "stdvector.h"
|
||||
#if defined(X_DISPLAY_MISSING)
|
||||
# error X11 is required to build synergy
|
||||
#else
|
||||
# include <X11/Xlib.h>
|
||||
#endif
|
||||
|
||||
class IXWindowsClipboardConverter;
|
||||
|
||||
//! X11 clipboard implementation
|
||||
class CXWindowsClipboard : public IClipboard {
|
||||
public:
|
||||
/*!
|
||||
Use \c window as the window that owns or interacts with the
|
||||
clipboard identified by \c id.
|
||||
*/
|
||||
CXWindowsClipboard(Display*, Window window, ClipboardID id);
|
||||
virtual ~CXWindowsClipboard();
|
||||
|
||||
//! Notify clipboard was lost
|
||||
/*!
|
||||
Tells clipboard it lost ownership at the given time.
|
||||
*/
|
||||
void lost(Time);
|
||||
|
||||
//! Add clipboard request
|
||||
/*!
|
||||
Adds a selection request to the request list. If the given
|
||||
owner window isn't this clipboard's window then this simply
|
||||
sends a failure event to the requestor.
|
||||
*/
|
||||
void addRequest(Window owner,
|
||||
Window requestor, Atom target,
|
||||
::Time time, Atom property);
|
||||
|
||||
//! Process clipboard request
|
||||
/*!
|
||||
Continues processing a selection request. Returns true if the
|
||||
request was handled, false if the request was unknown.
|
||||
*/
|
||||
bool processRequest(Window requestor,
|
||||
::Time time, Atom property);
|
||||
|
||||
//! Cancel clipboard request
|
||||
/*!
|
||||
Terminate a selection request. Returns true iff the request
|
||||
was known and handled.
|
||||
*/
|
||||
bool destroyRequest(Window requestor);
|
||||
|
||||
//! Get window
|
||||
/*!
|
||||
Returns the clipboard's window (passed the c'tor).
|
||||
*/
|
||||
Window getWindow() const;
|
||||
|
||||
//! Get selection atom
|
||||
/*!
|
||||
Returns the selection atom that identifies the clipboard to X11
|
||||
(e.g. XA_PRIMARY).
|
||||
*/
|
||||
Atom getSelection() const;
|
||||
|
||||
// IClipboard overrides
|
||||
virtual bool empty();
|
||||
virtual void add(EFormat, const CString& data);
|
||||
virtual bool open(Time) const;
|
||||
virtual void close() const;
|
||||
virtual Time getTime() const;
|
||||
virtual bool has(EFormat) const;
|
||||
virtual CString get(EFormat) const;
|
||||
|
||||
private:
|
||||
// remove all converters from our list
|
||||
void clearConverters();
|
||||
|
||||
// get the converter for a clipboard format. returns NULL if no
|
||||
// suitable converter. iff onlyIfNotAdded is true then also
|
||||
// return NULL if a suitable converter was found but we already
|
||||
// have data of the converter's clipboard format.
|
||||
IXWindowsClipboardConverter*
|
||||
getConverter(Atom target,
|
||||
bool onlyIfNotAdded = false) const;
|
||||
|
||||
// convert target atom to clipboard format
|
||||
EFormat getFormat(Atom target) const;
|
||||
|
||||
// add a non-MULTIPLE request. does not verify that the selection
|
||||
// was owned at the given time. returns true if the conversion
|
||||
// could be performed, false otherwise. in either case, the
|
||||
// reply is inserted.
|
||||
bool addSimpleRequest(
|
||||
Window requestor, Atom target,
|
||||
::Time time, Atom property);
|
||||
|
||||
// if not already checked then see if the cache is stale and, if so,
|
||||
// clear it. this has the side effect of updating m_timeOwned.
|
||||
void checkCache() const;
|
||||
|
||||
// clear the cache, resetting the cached flag and the added flag for
|
||||
// each format.
|
||||
void clearCache() const;
|
||||
void doClearCache();
|
||||
|
||||
// cache all formats of the selection
|
||||
void fillCache() const;
|
||||
void doFillCache();
|
||||
|
||||
//
|
||||
// helper classes
|
||||
//
|
||||
|
||||
// read an ICCCM conforming selection
|
||||
class CICCCMGetClipboard {
|
||||
public:
|
||||
CICCCMGetClipboard(Window requestor, Time time, Atom property);
|
||||
~CICCCMGetClipboard();
|
||||
|
||||
// convert the given selection to the given type. returns
|
||||
// true iff the conversion was successful or the conversion
|
||||
// cannot be performed (in which case *actualTarget == None).
|
||||
bool readClipboard(Display* display,
|
||||
Atom selection, Atom target,
|
||||
Atom* actualTarget, CString* data);
|
||||
|
||||
private:
|
||||
bool processEvent(Display* display, XEvent* event);
|
||||
|
||||
private:
|
||||
Window m_requestor;
|
||||
Time m_time;
|
||||
Atom m_property;
|
||||
bool m_incr;
|
||||
bool m_failed;
|
||||
bool m_done;
|
||||
|
||||
// true iff we've received the selection notify
|
||||
bool m_reading;
|
||||
|
||||
// the converted selection data
|
||||
CString* m_data;
|
||||
|
||||
// the actual type of the data. if this is None then the
|
||||
// selection owner cannot convert to the requested type.
|
||||
Atom* m_actualTarget;
|
||||
|
||||
public:
|
||||
// true iff the selection owner didn't follow ICCCM conventions
|
||||
bool m_error;
|
||||
};
|
||||
|
||||
// Motif structure IDs
|
||||
enum { kMotifClipFormat = 1, kMotifClipItem, kMotifClipHeader };
|
||||
|
||||
// _MOTIF_CLIP_HEADER structure
|
||||
class CMotifClipHeader {
|
||||
public:
|
||||
SInt32 m_id; // kMotifClipHeader
|
||||
SInt32 m_pad1[3];
|
||||
SInt32 m_item;
|
||||
SInt32 m_pad2[4];
|
||||
SInt32 m_numItems;
|
||||
SInt32 m_pad3[3];
|
||||
Window m_selectionOwner;
|
||||
SInt32 m_pad4[2];
|
||||
};
|
||||
|
||||
// Motif clip item structure
|
||||
class CMotifClipItem {
|
||||
public:
|
||||
SInt32 m_id; // kMotifClipItem
|
||||
SInt32 m_pad1[5];
|
||||
SInt32 m_size;
|
||||
SInt32 m_numFormats;
|
||||
SInt32 m_numDeletedFormats;
|
||||
SInt32 m_pad2[6];
|
||||
};
|
||||
|
||||
// Motif clip format structure
|
||||
class CMotifClipFormat {
|
||||
public:
|
||||
SInt32 m_id; // kMotifClipFormat
|
||||
SInt32 m_pad1[6];
|
||||
SInt32 m_length;
|
||||
SInt32 m_data;
|
||||
Atom m_type;
|
||||
SInt32 m_pad2[1];
|
||||
int m_deleted;
|
||||
SInt32 m_pad3[4];
|
||||
};
|
||||
|
||||
// stores data needed to respond to a selection request
|
||||
class CReply {
|
||||
public:
|
||||
CReply(Window, Atom target, ::Time);
|
||||
CReply(Window, Atom target, ::Time, Atom property,
|
||||
const CString& data, Atom type, int format);
|
||||
|
||||
public:
|
||||
// information about the request
|
||||
Window m_requestor;
|
||||
Atom m_target;
|
||||
::Time m_time;
|
||||
Atom m_property;
|
||||
|
||||
// true iff we've sent the notification for this reply
|
||||
bool m_replied;
|
||||
|
||||
// true iff the reply has sent its last message
|
||||
bool m_done;
|
||||
|
||||
// the data to send and its type and format
|
||||
CString m_data;
|
||||
Atom m_type;
|
||||
int m_format;
|
||||
|
||||
// index of next byte in m_data to send
|
||||
UInt32 m_ptr;
|
||||
};
|
||||
typedef std::list<CReply*> CReplyList;
|
||||
typedef std::map<Window, CReplyList> CReplyMap;
|
||||
typedef std::map<Window, long> CReplyEventMask;
|
||||
|
||||
// ICCCM interoperability methods
|
||||
void icccmFillCache();
|
||||
bool icccmGetSelection(Atom target,
|
||||
Atom* actualTarget, CString* data) const;
|
||||
Time icccmGetTime() const;
|
||||
|
||||
// motif interoperability methods
|
||||
bool motifLockClipboard() const;
|
||||
void motifUnlockClipboard() const;
|
||||
bool motifOwnsClipboard() const;
|
||||
void motifFillCache();
|
||||
bool motifGetSelection(const CMotifClipFormat*,
|
||||
Atom* actualTarget, CString* data) const;
|
||||
Time motifGetTime() const;
|
||||
|
||||
// reply methods
|
||||
bool insertMultipleReply(Window, ::Time, Atom);
|
||||
void insertReply(CReply*);
|
||||
void pushReplies();
|
||||
void pushReplies(CReplyMap::iterator,
|
||||
CReplyList&, CReplyList::iterator);
|
||||
bool sendReply(CReply*);
|
||||
void clearReplies();
|
||||
void clearReplies(CReplyList&);
|
||||
void sendNotify(Window requestor, Atom selection,
|
||||
Atom target, Atom property, Time time);
|
||||
bool wasOwnedAtTime(::Time) const;
|
||||
|
||||
// data conversion methods
|
||||
Atom getTargetsData(CString&, int* format) const;
|
||||
Atom getTimestampData(CString&, int* format) const;
|
||||
|
||||
private:
|
||||
typedef std::vector<IXWindowsClipboardConverter*> ConverterList;
|
||||
|
||||
Display* m_display;
|
||||
Window m_window;
|
||||
ClipboardID m_id;
|
||||
Atom m_selection;
|
||||
mutable bool m_open;
|
||||
mutable Time m_time;
|
||||
bool m_owner;
|
||||
mutable Time m_timeOwned;
|
||||
Time m_timeLost;
|
||||
|
||||
// true iff open and clipboard owned by a motif app
|
||||
mutable bool m_motif;
|
||||
|
||||
// the added/cached clipboard data
|
||||
mutable bool m_checkCache;
|
||||
bool m_cached;
|
||||
Time m_cacheTime;
|
||||
bool m_added[kNumFormats];
|
||||
CString m_data[kNumFormats];
|
||||
|
||||
// conversion request replies
|
||||
CReplyMap m_replies;
|
||||
CReplyEventMask m_eventMasks;
|
||||
|
||||
// clipboard format converters
|
||||
ConverterList m_converters;
|
||||
|
||||
// atoms we'll need
|
||||
Atom m_atomTargets;
|
||||
Atom m_atomMultiple;
|
||||
Atom m_atomTimestamp;
|
||||
Atom m_atomInteger;
|
||||
Atom m_atomAtom;
|
||||
Atom m_atomAtomPair;
|
||||
Atom m_atomData;
|
||||
Atom m_atomINCR;
|
||||
Atom m_atomMotifClipLock;
|
||||
Atom m_atomMotifClipHeader;
|
||||
Atom m_atomMotifClipAccess;
|
||||
Atom m_atomGDKSelection;
|
||||
};
|
||||
|
||||
//! Clipboard format converter interface
|
||||
/*!
|
||||
This interface defines the methods common to all X11 clipboard format
|
||||
converters.
|
||||
*/
|
||||
class IXWindowsClipboardConverter : public IInterface {
|
||||
public:
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Get clipboard format
|
||||
/*!
|
||||
Return the clipboard format this object converts from/to.
|
||||
*/
|
||||
virtual IClipboard::EFormat
|
||||
getFormat() const = 0;
|
||||
|
||||
//! Get X11 format atom
|
||||
/*!
|
||||
Return the atom representing the X selection format that
|
||||
this object converts from/to.
|
||||
*/
|
||||
virtual Atom getAtom() const = 0;
|
||||
|
||||
//! Get X11 property datum size
|
||||
/*!
|
||||
Return the size (in bits) of data elements returned by
|
||||
toIClipboard().
|
||||
*/
|
||||
virtual int getDataSize() const = 0;
|
||||
|
||||
//! Convert from IClipboard format
|
||||
/*!
|
||||
Convert from the IClipboard format to the X selection format.
|
||||
The input data must be in the IClipboard format returned by
|
||||
getFormat(). The return data will be in the X selection
|
||||
format returned by getAtom().
|
||||
*/
|
||||
virtual CString fromIClipboard(const CString&) const = 0;
|
||||
|
||||
//! Convert to IClipboard format
|
||||
/*!
|
||||
Convert from the X selection format to the IClipboard format
|
||||
(i.e., the reverse of fromIClipboard()).
|
||||
*/
|
||||
virtual CString toIClipboard(const CString&) const = 0;
|
||||
|
||||
//@}
|
||||
};
|
||||
|
||||
#endif
|
||||
60
lib/platform/CXWindowsClipboardTextConverter.cpp
Normal file
60
lib/platform/CXWindowsClipboardTextConverter.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "CXWindowsClipboardTextConverter.h"
|
||||
#include "CUnicode.h"
|
||||
|
||||
//
|
||||
// CXWindowsClipboardTextConverter
|
||||
//
|
||||
|
||||
CXWindowsClipboardTextConverter::CXWindowsClipboardTextConverter(
|
||||
Display* display, const char* name) :
|
||||
m_atom(XInternAtom(display, name, False))
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CXWindowsClipboardTextConverter::~CXWindowsClipboardTextConverter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
IClipboard::EFormat
|
||||
CXWindowsClipboardTextConverter::getFormat() const
|
||||
{
|
||||
return IClipboard::kText;
|
||||
}
|
||||
|
||||
Atom
|
||||
CXWindowsClipboardTextConverter::getAtom() const
|
||||
{
|
||||
return m_atom;
|
||||
}
|
||||
|
||||
int
|
||||
CXWindowsClipboardTextConverter::getDataSize() const
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
CString
|
||||
CXWindowsClipboardTextConverter::fromIClipboard(const CString& data) const
|
||||
{
|
||||
return CUnicode::UTF8ToText(data);
|
||||
}
|
||||
|
||||
CString
|
||||
CXWindowsClipboardTextConverter::toIClipboard(const CString& data) const
|
||||
{
|
||||
// convert to UTF-8
|
||||
bool errors;
|
||||
CString utf8 = CUnicode::textToUTF8(data, &errors);
|
||||
|
||||
// if there were decoding errors then, to support old applications
|
||||
// that don't understand UTF-8 but can report the exact binary
|
||||
// UTF-8 representation, see if the data appears to be UTF-8. if
|
||||
// so then use it as is.
|
||||
if (errors && CUnicode::isUTF8(data)) {
|
||||
return data;
|
||||
}
|
||||
|
||||
return utf8;
|
||||
}
|
||||
27
lib/platform/CXWindowsClipboardTextConverter.h
Normal file
27
lib/platform/CXWindowsClipboardTextConverter.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CXWINDOWSCLIPBOARDTEXTCONVERTER_H
|
||||
#define CXWINDOWSCLIPBOARDTEXTCONVERTER_H
|
||||
|
||||
#include "CXWindowsClipboard.h"
|
||||
|
||||
//! Convert to/from locale text encoding
|
||||
class CXWindowsClipboardTextConverter : public IXWindowsClipboardConverter {
|
||||
public:
|
||||
/*!
|
||||
\c name is converted to an atom and that is reported by getAtom().
|
||||
*/
|
||||
CXWindowsClipboardTextConverter(Display* display, const char* name);
|
||||
virtual ~CXWindowsClipboardTextConverter();
|
||||
|
||||
// IXWindowsClipboardConverter overrides
|
||||
virtual IClipboard::EFormat
|
||||
getFormat() const;
|
||||
virtual Atom getAtom() const;
|
||||
virtual int getDataSize() const;
|
||||
virtual CString fromIClipboard(const CString&) const;
|
||||
virtual CString toIClipboard(const CString&) const;
|
||||
|
||||
private:
|
||||
Atom m_atom;
|
||||
};
|
||||
|
||||
#endif
|
||||
48
lib/platform/CXWindowsClipboardUCS2Converter.cpp
Normal file
48
lib/platform/CXWindowsClipboardUCS2Converter.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "CXWindowsClipboardUCS2Converter.h"
|
||||
#include "CUnicode.h"
|
||||
|
||||
//
|
||||
// CXWindowsClipboardUCS2Converter
|
||||
//
|
||||
|
||||
CXWindowsClipboardUCS2Converter::CXWindowsClipboardUCS2Converter(
|
||||
Display* display, const char* name) :
|
||||
m_atom(XInternAtom(display, name, False))
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CXWindowsClipboardUCS2Converter::~CXWindowsClipboardUCS2Converter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
IClipboard::EFormat
|
||||
CXWindowsClipboardUCS2Converter::getFormat() const
|
||||
{
|
||||
return IClipboard::kText;
|
||||
}
|
||||
|
||||
Atom
|
||||
CXWindowsClipboardUCS2Converter::getAtom() const
|
||||
{
|
||||
return m_atom;
|
||||
}
|
||||
|
||||
int
|
||||
CXWindowsClipboardUCS2Converter::getDataSize() const
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
CString
|
||||
CXWindowsClipboardUCS2Converter::fromIClipboard(const CString& data) const
|
||||
{
|
||||
return CUnicode::UTF8ToUCS2(data);
|
||||
}
|
||||
|
||||
CString
|
||||
CXWindowsClipboardUCS2Converter::toIClipboard(const CString& data) const
|
||||
{
|
||||
return CUnicode::UCS2ToUTF8(data);
|
||||
}
|
||||
27
lib/platform/CXWindowsClipboardUCS2Converter.h
Normal file
27
lib/platform/CXWindowsClipboardUCS2Converter.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CXWINDOWSCLIPBOARDUCS2CONVERTER_H
|
||||
#define CXWINDOWSCLIPBOARDUCS2CONVERTER_H
|
||||
|
||||
#include "CXWindowsClipboard.h"
|
||||
|
||||
//! Convert to/from UCS-2 encoding
|
||||
class CXWindowsClipboardUCS2Converter : public IXWindowsClipboardConverter {
|
||||
public:
|
||||
/*!
|
||||
\c name is converted to an atom and that is reported by getAtom().
|
||||
*/
|
||||
CXWindowsClipboardUCS2Converter(Display* display, const char* name);
|
||||
virtual ~CXWindowsClipboardUCS2Converter();
|
||||
|
||||
// IXWindowsClipboardConverter overrides
|
||||
virtual IClipboard::EFormat
|
||||
getFormat() const;
|
||||
virtual Atom getAtom() const;
|
||||
virtual int getDataSize() const;
|
||||
virtual CString fromIClipboard(const CString&) const;
|
||||
virtual CString toIClipboard(const CString&) const;
|
||||
|
||||
private:
|
||||
Atom m_atom;
|
||||
};
|
||||
|
||||
#endif
|
||||
47
lib/platform/CXWindowsClipboardUTF8Converter.cpp
Normal file
47
lib/platform/CXWindowsClipboardUTF8Converter.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "CXWindowsClipboardUTF8Converter.h"
|
||||
|
||||
//
|
||||
// CXWindowsClipboardUTF8Converter
|
||||
//
|
||||
|
||||
CXWindowsClipboardUTF8Converter::CXWindowsClipboardUTF8Converter(
|
||||
Display* display, const char* name) :
|
||||
m_atom(XInternAtom(display, name, False))
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CXWindowsClipboardUTF8Converter::~CXWindowsClipboardUTF8Converter()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
IClipboard::EFormat
|
||||
CXWindowsClipboardUTF8Converter::getFormat() const
|
||||
{
|
||||
return IClipboard::kText;
|
||||
}
|
||||
|
||||
Atom
|
||||
CXWindowsClipboardUTF8Converter::getAtom() const
|
||||
{
|
||||
return m_atom;
|
||||
}
|
||||
|
||||
int
|
||||
CXWindowsClipboardUTF8Converter::getDataSize() const
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
CString
|
||||
CXWindowsClipboardUTF8Converter::fromIClipboard(const CString& data) const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
CString
|
||||
CXWindowsClipboardUTF8Converter::toIClipboard(const CString& data) const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
27
lib/platform/CXWindowsClipboardUTF8Converter.h
Normal file
27
lib/platform/CXWindowsClipboardUTF8Converter.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CXWINDOWSCLIPBOARDUTF8CONVERTER_H
|
||||
#define CXWINDOWSCLIPBOARDUTF8CONVERTER_H
|
||||
|
||||
#include "CXWindowsClipboard.h"
|
||||
|
||||
//! Convert to/from UTF-8 encoding
|
||||
class CXWindowsClipboardUTF8Converter : public IXWindowsClipboardConverter {
|
||||
public:
|
||||
/*!
|
||||
\c name is converted to an atom and that is reported by getAtom().
|
||||
*/
|
||||
CXWindowsClipboardUTF8Converter(Display* display, const char* name);
|
||||
virtual ~CXWindowsClipboardUTF8Converter();
|
||||
|
||||
// IXWindowsClipboardConverter overrides
|
||||
virtual IClipboard::EFormat
|
||||
getFormat() const;
|
||||
virtual Atom getAtom() const;
|
||||
virtual int getDataSize() const;
|
||||
virtual CString fromIClipboard(const CString&) const;
|
||||
virtual CString toIClipboard(const CString&) const;
|
||||
|
||||
private:
|
||||
Atom m_atom;
|
||||
};
|
||||
|
||||
#endif
|
||||
745
lib/platform/CXWindowsScreen.cpp
Normal file
745
lib/platform/CXWindowsScreen.cpp
Normal file
@@ -0,0 +1,745 @@
|
||||
#include "CXWindowsScreen.h"
|
||||
#include "CXWindowsClipboard.h"
|
||||
#include "CXWindowsScreenSaver.h"
|
||||
#include "CXWindowsUtil.h"
|
||||
#include "CClipboard.h"
|
||||
#include "IScreenEventHandler.h"
|
||||
#include "IScreenReceiver.h"
|
||||
#include "XScreen.h"
|
||||
#include "CLock.h"
|
||||
#include "CThread.h"
|
||||
#include "CLog.h"
|
||||
#include "IJob.h"
|
||||
#include <cstring>
|
||||
#if defined(X_DISPLAY_MISSING)
|
||||
# error X11 is required to build synergy
|
||||
#else
|
||||
# include <X11/X.h>
|
||||
# include <X11/Xutil.h>
|
||||
#endif
|
||||
#if UNIX_LIKE
|
||||
#include <sys/poll.h>
|
||||
#endif
|
||||
|
||||
//
|
||||
// CXWindowsScreen::CTimer
|
||||
//
|
||||
|
||||
CXWindowsScreen::CTimer::CTimer(IJob* job, double timeout) :
|
||||
m_job(job),
|
||||
m_timeout(timeout)
|
||||
{
|
||||
assert(m_job != NULL);
|
||||
assert(m_timeout > 0.0);
|
||||
|
||||
reset();
|
||||
}
|
||||
|
||||
CXWindowsScreen::CTimer::~CTimer()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::CTimer::run()
|
||||
{
|
||||
m_job->run();
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::CTimer::reset()
|
||||
{
|
||||
m_time = m_timeout;
|
||||
}
|
||||
|
||||
CXWindowsScreen::CTimer::CTimer&
|
||||
CXWindowsScreen::CTimer::operator-=(double dt)
|
||||
{
|
||||
m_time -= dt;
|
||||
return *this;
|
||||
}
|
||||
|
||||
CXWindowsScreen::CTimer::operator double() const
|
||||
{
|
||||
return m_time;
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreen::CTimer::operator<(const CTimer& t) const
|
||||
{
|
||||
return m_time < t.m_time;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// CXWindowsScreen
|
||||
//
|
||||
|
||||
CXWindowsScreen* CXWindowsScreen::s_screen = NULL;
|
||||
|
||||
CXWindowsScreen::CXWindowsScreen(IScreenReceiver* receiver,
|
||||
IScreenEventHandler* eventHandler) :
|
||||
m_display(NULL),
|
||||
m_root(None),
|
||||
m_stop(false),
|
||||
m_receiver(receiver),
|
||||
m_eventHandler(eventHandler),
|
||||
m_window(None),
|
||||
m_x(0), m_y(0),
|
||||
m_w(0), m_h(0),
|
||||
m_screensaver(NULL),
|
||||
m_screensaverNotify(false),
|
||||
m_atomScreensaver(None)
|
||||
{
|
||||
assert(s_screen == NULL);
|
||||
assert(m_receiver != NULL);
|
||||
assert(m_eventHandler != NULL);
|
||||
|
||||
s_screen = this;
|
||||
|
||||
// no clipboards to start with
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
m_clipboard[id] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
CXWindowsScreen::~CXWindowsScreen()
|
||||
{
|
||||
assert(s_screen != NULL);
|
||||
assert(m_display == NULL);
|
||||
|
||||
s_screen = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::addTimer(IJob* job, double timeout)
|
||||
{
|
||||
CLock lock(&m_timersMutex);
|
||||
removeTimerNoLock(job);
|
||||
m_timers.push(CTimer(job, timeout));
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::removeTimer(IJob* job)
|
||||
{
|
||||
CLock lock(&m_timersMutex);
|
||||
removeTimerNoLock(job);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::removeTimerNoLock(IJob* job)
|
||||
{
|
||||
// do it the hard way. first collect all jobs that are not
|
||||
// the removed job.
|
||||
CTimerPriorityQueue::container_type tmp;
|
||||
for (CTimerPriorityQueue::iterator index = m_timers.begin();
|
||||
index != m_timers.end(); ++index) {
|
||||
if (index->getJob() != job) {
|
||||
tmp.push_back(*index);
|
||||
}
|
||||
}
|
||||
|
||||
// now swap in the new list
|
||||
m_timers.swap(tmp);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::setWindow(Window window)
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
assert(m_display != NULL);
|
||||
|
||||
// destroy the clipboards
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
delete m_clipboard[id];
|
||||
m_clipboard[id] = NULL;
|
||||
}
|
||||
|
||||
// save the new window
|
||||
m_window = window;
|
||||
|
||||
// initialize the clipboards
|
||||
if (m_window != None) {
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
m_clipboard[id] = new CXWindowsClipboard(m_display, m_window, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Window
|
||||
CXWindowsScreen::getRoot() const
|
||||
{
|
||||
assert(m_display != NULL);
|
||||
return m_root;
|
||||
}
|
||||
|
||||
Cursor
|
||||
CXWindowsScreen::getBlankCursor() const
|
||||
{
|
||||
return m_cursor;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::open()
|
||||
{
|
||||
assert(m_display == NULL);
|
||||
|
||||
// set the X I/O error handler so we catch the display disconnecting
|
||||
XSetIOErrorHandler(&CXWindowsScreen::ioErrorHandler);
|
||||
|
||||
// get the DISPLAY
|
||||
const char* display = getenv("DISPLAY");
|
||||
if (display == NULL) {
|
||||
display = ":0.0";
|
||||
}
|
||||
|
||||
// open the display
|
||||
log((CLOG_DEBUG "XOpenDisplay(\"%s\")", display));
|
||||
m_display = XOpenDisplay(display);
|
||||
if (m_display == NULL) {
|
||||
throw XScreenOpenFailure();
|
||||
}
|
||||
|
||||
// get root window
|
||||
m_root = DefaultRootWindow(m_display);
|
||||
|
||||
// create the transparent cursor
|
||||
createBlankCursor();
|
||||
|
||||
// get screen shape
|
||||
updateScreenShape();
|
||||
|
||||
// initialize the screen saver
|
||||
m_atomScreensaver = XInternAtom(m_display, "SCREENSAVER", False);
|
||||
m_screensaver = new CXWindowsScreenSaver(this, m_display);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::mainLoop()
|
||||
{
|
||||
// wait for an event in a cancellable way and don't lock the
|
||||
// display while we're waiting.
|
||||
CEvent event;
|
||||
m_mutex.lock();
|
||||
|
||||
#if UNIX_LIKE
|
||||
|
||||
// use poll() to wait for a message from the X server or for timeout.
|
||||
// this is a good deal more efficient than polling and sleeping.
|
||||
struct pollfd pfds[1];
|
||||
pfds[0].fd = ConnectionNumber(m_display);
|
||||
pfds[0].events = POLLIN;
|
||||
while (!m_stop) {
|
||||
// compute timeout to next timer
|
||||
int timeout = (m_timers.empty() ? -1 :
|
||||
static_cast<int>(1000.0 * m_timers.top()));
|
||||
|
||||
// wait for message from X server or for timeout. also check
|
||||
// if the thread has been cancelled. poll() should return -1
|
||||
// with EINTR when the thread is cancelled.
|
||||
m_mutex.unlock();
|
||||
poll(pfds, 1, timeout);
|
||||
CThread::testCancel();
|
||||
m_mutex.lock();
|
||||
|
||||
// process timers
|
||||
processTimers();
|
||||
|
||||
// handle pending events
|
||||
while (!m_stop && XPending(m_display) > 0) {
|
||||
// get the event
|
||||
XNextEvent(m_display, &event.m_event);
|
||||
|
||||
// process the event. if unhandled then let the subclass
|
||||
// have a go at it.
|
||||
m_mutex.unlock();
|
||||
if (!onPreDispatch(&event)) {
|
||||
m_eventHandler->onEvent(&event);
|
||||
}
|
||||
m_mutex.lock();
|
||||
}
|
||||
}
|
||||
|
||||
#else // !UNIX_LIKE
|
||||
|
||||
// poll and sleep
|
||||
while (!m_stop) {
|
||||
// poll for pending events and process timers
|
||||
while (!m_stop && XPending(m_display) == 0) {
|
||||
// check timers
|
||||
if (processTimers()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// wait
|
||||
m_mutex.unlock();
|
||||
CThread::sleep(0.01);
|
||||
m_mutex.lock();
|
||||
}
|
||||
|
||||
// process events
|
||||
while (!m_stop && XPending(m_display) > 0) {
|
||||
// get the event
|
||||
XNextEvent(m_display, &event.m_event);
|
||||
|
||||
// process the event. if unhandled then let the subclass
|
||||
// have a go at it.
|
||||
m_mutex.unlock();
|
||||
if (!onPreDispatch(&event)) {
|
||||
m_eventHandler->onEvent(&event);
|
||||
}
|
||||
m_mutex.lock();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !UNIX_LIKE
|
||||
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::exitMainLoop()
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
m_stop = true;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::close()
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
|
||||
// done with screen saver
|
||||
delete m_screensaver;
|
||||
m_screensaver = NULL;
|
||||
|
||||
// destroy clipboards
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
delete m_clipboard[id];
|
||||
m_clipboard[id] = NULL;
|
||||
}
|
||||
|
||||
// close the display
|
||||
if (m_display != NULL) {
|
||||
XCloseDisplay(m_display);
|
||||
m_display = NULL;
|
||||
log((CLOG_DEBUG "closed display"));
|
||||
}
|
||||
XSetIOErrorHandler(NULL);
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreen::setClipboard(ClipboardID id, const IClipboard* clipboard)
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
|
||||
// fail if we don't have the requested clipboard
|
||||
if (m_clipboard[id] == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// get the actual time. ICCCM does not allow CurrentTime.
|
||||
Time timestamp = CXWindowsUtil::getCurrentTime(
|
||||
m_display, m_clipboard[id]->getWindow());
|
||||
|
||||
if (clipboard != NULL) {
|
||||
// save clipboard data
|
||||
return CClipboard::copy(m_clipboard[id], clipboard, timestamp);
|
||||
}
|
||||
else {
|
||||
// assert clipboard ownership
|
||||
if (!m_clipboard[id]->open(timestamp)) {
|
||||
return false;
|
||||
}
|
||||
m_clipboard[id]->empty();
|
||||
m_clipboard[id]->close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::checkClipboards()
|
||||
{
|
||||
// do nothing, we're always up to date
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::openScreensaver(bool notify)
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
assert(m_screensaver != NULL);
|
||||
|
||||
m_screensaverNotify = notify;
|
||||
if (m_screensaverNotify) {
|
||||
m_screensaver->setNotify(m_window);
|
||||
}
|
||||
else {
|
||||
m_screensaver->disable();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::closeScreensaver()
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
if (m_screensaver != NULL) {
|
||||
if (m_screensaverNotify) {
|
||||
m_screensaver->setNotify(None);
|
||||
}
|
||||
else {
|
||||
m_screensaver->enable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::screensaver(bool activate)
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
assert(m_screensaver != NULL);
|
||||
|
||||
if (activate) {
|
||||
m_screensaver->activate();
|
||||
}
|
||||
else {
|
||||
m_screensaver->deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::syncDesktop()
|
||||
{
|
||||
// do nothing; X doesn't suffer from this bogosity
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreen::getClipboard(ClipboardID id, IClipboard* clipboard) const
|
||||
{
|
||||
assert(clipboard != NULL);
|
||||
|
||||
// block others from using the display while we get the clipboard
|
||||
CLock lock(&m_mutex);
|
||||
|
||||
// fail if we don't have the requested clipboard
|
||||
if (m_clipboard[id] == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// get the actual time. ICCCM does not allow CurrentTime.
|
||||
Time timestamp = CXWindowsUtil::getCurrentTime(
|
||||
m_display, m_clipboard[id]->getWindow());
|
||||
|
||||
// copy the clipboard
|
||||
return CClipboard::copy(clipboard, m_clipboard[id], timestamp);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
assert(m_display != NULL);
|
||||
|
||||
x = m_x;
|
||||
y = m_y;
|
||||
w = m_w;
|
||||
h = m_h;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::getCursorPos(SInt32& x, SInt32& y) const
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
assert(m_display != NULL);
|
||||
|
||||
Window root, window;
|
||||
int mx, my, xWindow, yWindow;
|
||||
unsigned int mask;
|
||||
if (XQueryPointer(m_display, getRoot(), &root, &window,
|
||||
&mx, &my, &xWindow, &yWindow, &mask)) {
|
||||
x = mx;
|
||||
y = my;
|
||||
}
|
||||
else {
|
||||
getCursorCenter(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::getCursorCenter(SInt32& x, SInt32& y) const
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
assert(m_display != NULL);
|
||||
|
||||
x = m_x + (m_w >> 1);
|
||||
y = m_y + (m_h >> 1);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::updateScreenShape()
|
||||
{
|
||||
m_x = 0;
|
||||
m_y = 0;
|
||||
m_w = WidthOfScreen(DefaultScreenOfDisplay(m_display));
|
||||
m_h = HeightOfScreen(DefaultScreenOfDisplay(m_display));
|
||||
log((CLOG_INFO "screen shape: %d,%d %dx%d", m_x, m_y, m_w, m_h));
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreen::onPreDispatch(CEvent* event)
|
||||
{
|
||||
assert(event != NULL);
|
||||
XEvent* xevent = &event->m_event;
|
||||
|
||||
switch (xevent->type) {
|
||||
case MappingNotify:
|
||||
// keyboard mapping changed
|
||||
XRefreshKeyboardMapping(&xevent->xmapping);
|
||||
|
||||
// pass event on
|
||||
break;
|
||||
|
||||
case SelectionClear:
|
||||
{
|
||||
// we just lost the selection. that means someone else
|
||||
// grabbed the selection so this screen is now the
|
||||
// selection owner. report that to the receiver.
|
||||
ClipboardID id = getClipboardID(xevent->xselectionclear.selection);
|
||||
if (id != kClipboardEnd) {
|
||||
log((CLOG_DEBUG "lost clipboard %d ownership at time %d", id, xevent->xselectionclear.time));
|
||||
m_clipboard[id]->lost(xevent->xselectionclear.time);
|
||||
m_receiver->onGrabClipboard(id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SelectionNotify:
|
||||
// notification of selection transferred. we shouldn't
|
||||
// get this here because we handle them in the selection
|
||||
// retrieval methods. we'll just delete the property
|
||||
// with the data (satisfying the usual ICCCM protocol).
|
||||
if (xevent->xselection.property != None) {
|
||||
CLock lock(&m_mutex);
|
||||
XDeleteProperty(m_display,
|
||||
xevent->xselection.requestor,
|
||||
xevent->xselection.property);
|
||||
}
|
||||
return true;
|
||||
|
||||
case SelectionRequest:
|
||||
{
|
||||
// somebody is asking for clipboard data
|
||||
ClipboardID id = getClipboardID(
|
||||
xevent->xselectionrequest.selection);
|
||||
if (id != kClipboardEnd) {
|
||||
CLock lock(&m_mutex);
|
||||
m_clipboard[id]->addRequest(
|
||||
xevent->xselectionrequest.owner,
|
||||
xevent->xselectionrequest.requestor,
|
||||
xevent->xselectionrequest.target,
|
||||
xevent->xselectionrequest.time,
|
||||
xevent->xselectionrequest.property);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case PropertyNotify:
|
||||
// property delete may be part of a selection conversion
|
||||
if (xevent->xproperty.state == PropertyDelete) {
|
||||
processClipboardRequest(xevent->xproperty.window,
|
||||
xevent->xproperty.time,
|
||||
xevent->xproperty.atom);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case ClientMessage:
|
||||
if (xevent->xclient.message_type == m_atomScreensaver ||
|
||||
xevent->xclient.format == 32) {
|
||||
// screen saver activation/deactivation event
|
||||
m_eventHandler->onScreensaver(xevent->xclient.data.l[0] != 0);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case DestroyNotify:
|
||||
// looks like one of the windows that requested a clipboard
|
||||
// transfer has gone bye-bye.
|
||||
destroyClipboardRequest(xevent->xdestroywindow.window);
|
||||
|
||||
// we don't know if the event was handled or not so continue
|
||||
break;
|
||||
}
|
||||
|
||||
// let screen saver have a go
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
m_screensaver->onPreDispatch(xevent);
|
||||
}
|
||||
|
||||
return m_eventHandler->onPreDispatch(event);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::createBlankCursor()
|
||||
{
|
||||
// this seems just a bit more complicated than really necessary
|
||||
|
||||
// get the closet cursor size to 1x1
|
||||
unsigned int w, h;
|
||||
XQueryBestCursor(m_display, m_root, 1, 1, &w, &h);
|
||||
|
||||
// make bitmap data for cursor of closet size. since the cursor
|
||||
// is blank we can use the same bitmap for shape and mask: all
|
||||
// zeros.
|
||||
const int size = ((w + 7) >> 3) * h;
|
||||
char* data = new char[size];
|
||||
memset(data, 0, size);
|
||||
|
||||
// make bitmap
|
||||
Pixmap bitmap = XCreateBitmapFromData(m_display, m_root, data, w, h);
|
||||
|
||||
// need an arbitrary color for the cursor
|
||||
XColor color;
|
||||
color.pixel = 0;
|
||||
color.red = color.green = color.blue = 0;
|
||||
color.flags = DoRed | DoGreen | DoBlue;
|
||||
|
||||
// make cursor from bitmap
|
||||
m_cursor = XCreatePixmapCursor(m_display, bitmap, bitmap,
|
||||
&color, &color, 0, 0);
|
||||
|
||||
// don't need bitmap or the data anymore
|
||||
delete[] data;
|
||||
XFreePixmap(m_display, bitmap);
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreen::processTimers()
|
||||
{
|
||||
std::vector<IJob*> jobs;
|
||||
{
|
||||
CLock lock(&m_timersMutex);
|
||||
|
||||
// get current time
|
||||
const double time = m_time.getTime();
|
||||
|
||||
// done if no timers have expired
|
||||
if (m_timers.empty() || m_timers.top() > time) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// subtract current time from all timers. note that this won't
|
||||
// change the order of elements in the priority queue (except
|
||||
// for floating point round off which we'll ignore).
|
||||
for (CTimerPriorityQueue::iterator index = m_timers.begin();
|
||||
index != m_timers.end(); ++index) {
|
||||
(*index) -= time;
|
||||
}
|
||||
|
||||
// process all timers at or below zero, saving the jobs
|
||||
while (m_timers.top() <= 0.0) {
|
||||
CTimer timer = m_timers.top();
|
||||
jobs.push_back(timer.getJob());
|
||||
timer.reset();
|
||||
m_timers.pop();
|
||||
m_timers.push(timer);
|
||||
}
|
||||
|
||||
// reset the clock
|
||||
m_time.reset();
|
||||
}
|
||||
|
||||
// now run the jobs. note that if one of these jobs removes
|
||||
// a timer later in the jobs list and deletes that job pointer
|
||||
// then this will crash when it tries to run that job.
|
||||
for (std::vector<IJob*>::iterator index = jobs.begin();
|
||||
index != jobs.end(); ++index) {
|
||||
(*index)->run();
|
||||
}
|
||||
}
|
||||
|
||||
ClipboardID
|
||||
CXWindowsScreen::getClipboardID(Atom selection) const
|
||||
{
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
if (m_clipboard[id] != NULL &&
|
||||
m_clipboard[id]->getSelection() == selection) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
return kClipboardEnd;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::processClipboardRequest(Window requestor,
|
||||
Time time, Atom property)
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
|
||||
// check every clipboard until one returns success
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
if (m_clipboard[id] != NULL &&
|
||||
m_clipboard[id]->processRequest(requestor, time, property)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreen::destroyClipboardRequest(Window requestor)
|
||||
{
|
||||
CLock lock(&m_mutex);
|
||||
|
||||
// check every clipboard until one returns success
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
if (m_clipboard[id] != NULL &&
|
||||
m_clipboard[id]->destroyRequest(requestor)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
CXWindowsScreen::ioErrorHandler(Display*)
|
||||
{
|
||||
// the display has disconnected, probably because X is shutting
|
||||
// down. X forces us to exit at this point. that's arguably
|
||||
// a flaw in X but, realistically, it's difficult to gracefully
|
||||
// handle not having a Display* anymore. we'll simply log the
|
||||
// error, notify the subclass (which must not use the display
|
||||
// so we set it to NULL), and exit.
|
||||
log((CLOG_WARN "X display has unexpectedly disconnected"));
|
||||
s_screen->m_display = NULL;
|
||||
s_screen->m_receiver->onError();
|
||||
log((CLOG_CRIT "quiting due to X display disconnection"));
|
||||
exit(17);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// CDisplayLock
|
||||
//
|
||||
|
||||
CDisplayLock::CDisplayLock(const CXWindowsScreen* screen) :
|
||||
m_mutex(&screen->m_mutex),
|
||||
m_display(screen->m_display)
|
||||
{
|
||||
// note -- it's permitted for m_display to be NULL. that might
|
||||
// happen if we couldn't connect to the display or if the
|
||||
// display unexpectedly disconnected. the caller is expected
|
||||
// to check for NULL as necessary.
|
||||
|
||||
m_mutex->lock();
|
||||
}
|
||||
|
||||
CDisplayLock::~CDisplayLock()
|
||||
{
|
||||
m_mutex->unlock();
|
||||
}
|
||||
|
||||
CDisplayLock::operator Display*() const
|
||||
{
|
||||
return m_display;
|
||||
}
|
||||
280
lib/platform/CXWindowsScreen.h
Normal file
280
lib/platform/CXWindowsScreen.h
Normal file
@@ -0,0 +1,280 @@
|
||||
#ifndef CXWINDOWSSCREEN_H
|
||||
#define CXWINDOWSSCREEN_H
|
||||
|
||||
#include "IScreen.h"
|
||||
#include "CMutex.h"
|
||||
#include "CStopwatch.h"
|
||||
#include "stdvector.h"
|
||||
#if defined(X_DISPLAY_MISSING)
|
||||
# error X11 is required to build synergy
|
||||
#else
|
||||
# include <X11/Xlib.h>
|
||||
#endif
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
class IJob;
|
||||
class IScreenEventHandler;
|
||||
class IScreenReceiver;
|
||||
class CXWindowsClipboard;
|
||||
class CXWindowsScreenSaver;
|
||||
|
||||
// X11 event
|
||||
class CEvent {
|
||||
public:
|
||||
XEvent m_event;
|
||||
SInt32 m_result;
|
||||
};
|
||||
|
||||
// X11 screen implementation
|
||||
class CXWindowsScreen : public IScreen {
|
||||
public:
|
||||
CXWindowsScreen(IScreenReceiver*, IScreenEventHandler*);
|
||||
virtual ~CXWindowsScreen();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Add timer
|
||||
/*!
|
||||
Add a job to invoke every timeout seconds. The job is
|
||||
called with the display locked. If a job timeout expires twice
|
||||
or more before the job can be called then the job is called
|
||||
just once. The caller retains ownership of the job.
|
||||
*/
|
||||
void addTimer(IJob*, double timeout);
|
||||
|
||||
//! Remove timer
|
||||
/*!
|
||||
Remove a job. The caller retains ownership of the job.
|
||||
*/
|
||||
void removeTimer(IJob*);
|
||||
|
||||
//! Set window
|
||||
/*!
|
||||
Set the window (created by the subclass). This performs some
|
||||
initialization and saves the window in case it's needed later.
|
||||
*/
|
||||
void setWindow(Window);
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Get window
|
||||
/*!
|
||||
Returns the root window of the screen.
|
||||
*/
|
||||
Window getRoot() const;
|
||||
|
||||
//! Get transparent cursor
|
||||
/*!
|
||||
Returns a cursor that is transparent everywhere.
|
||||
*/
|
||||
Cursor getBlankCursor() const;
|
||||
|
||||
//@}
|
||||
|
||||
// IScreen overrides
|
||||
void open();
|
||||
void mainLoop();
|
||||
void exitMainLoop();
|
||||
void close();
|
||||
bool setClipboard(ClipboardID, const IClipboard*);
|
||||
void checkClipboards();
|
||||
void openScreensaver(bool notify);
|
||||
void closeScreensaver();
|
||||
void screensaver(bool activate);
|
||||
void syncDesktop();
|
||||
bool getClipboard(ClipboardID, IClipboard*) const;
|
||||
void getShape(SInt32&, SInt32&, SInt32&, SInt32&) const;
|
||||
void getCursorPos(SInt32&, SInt32&) const;
|
||||
void getCursorCenter(SInt32&, SInt32&) const;
|
||||
|
||||
private:
|
||||
// update screen size cache
|
||||
void updateScreenShape();
|
||||
|
||||
// process events before dispatching to receiver
|
||||
bool onPreDispatch(CEvent* event);
|
||||
|
||||
// create the transparent cursor
|
||||
void createBlankCursor();
|
||||
|
||||
// remove a timer without locking
|
||||
void removeTimerNoLock(IJob*);
|
||||
|
||||
// process timers
|
||||
bool processTimers();
|
||||
|
||||
// determine the clipboard from the X selection. returns
|
||||
// kClipboardEnd if no such clipboard.
|
||||
ClipboardID getClipboardID(Atom selection) const;
|
||||
|
||||
// continue processing a selection request
|
||||
void processClipboardRequest(Window window,
|
||||
Time time, Atom property);
|
||||
|
||||
// terminate a selection request
|
||||
void destroyClipboardRequest(Window window);
|
||||
|
||||
// X I/O error handler
|
||||
static int ioErrorHandler(Display*);
|
||||
|
||||
private:
|
||||
// a priority queue will direct access to the elements
|
||||
template <class T, class Container = std::vector<T>,
|
||||
class Compare = std::greater<typename Container::value_type> >
|
||||
class CPriorityQueue {
|
||||
public:
|
||||
typedef typename Container::value_type value_type;
|
||||
typedef typename Container::size_type size_type;
|
||||
typedef typename Container::iterator iterator;
|
||||
typedef Container container_type;
|
||||
|
||||
CPriorityQueue() { }
|
||||
CPriorityQueue(Container& swappedIn);
|
||||
~CPriorityQueue() { }
|
||||
|
||||
// manipulators
|
||||
|
||||
void push(const value_type& v)
|
||||
{
|
||||
c.push_back(v);
|
||||
std::push_heap(c.begin(), c.end(), comp);
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
std::pop_heap(c.begin(), c.end(), comp);
|
||||
c.pop_back();
|
||||
}
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return c.begin();
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return c.end();
|
||||
}
|
||||
|
||||
void swap(CPriorityQueue<T, Container, Compare>& q)
|
||||
{
|
||||
c.swap(q.c);
|
||||
}
|
||||
|
||||
void swap(Container& c2)
|
||||
{
|
||||
c.swap(c2);
|
||||
std::make_heap(c.begin(), c.end(), comp);
|
||||
}
|
||||
|
||||
// accessors
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return c.empty();
|
||||
}
|
||||
|
||||
size_type size() const
|
||||
{
|
||||
return c.size();
|
||||
}
|
||||
|
||||
const value_type&
|
||||
top() const
|
||||
{
|
||||
return c.front();
|
||||
}
|
||||
|
||||
private:
|
||||
Container c;
|
||||
Compare comp;
|
||||
};
|
||||
|
||||
// a timer priority queue element
|
||||
class CTimer {
|
||||
public:
|
||||
CTimer(IJob* job, double timeout);
|
||||
~CTimer();
|
||||
|
||||
// manipulators
|
||||
|
||||
void run();
|
||||
|
||||
void reset();
|
||||
|
||||
CTimer& operator-=(double);
|
||||
|
||||
// accessors
|
||||
|
||||
IJob* getJob() const
|
||||
{
|
||||
return m_job;
|
||||
}
|
||||
|
||||
operator double() const;
|
||||
|
||||
bool operator<(const CTimer&) const;
|
||||
|
||||
private:
|
||||
IJob* m_job;
|
||||
double m_timeout;
|
||||
double m_time;
|
||||
};
|
||||
|
||||
private:
|
||||
friend class CDisplayLock;
|
||||
|
||||
typedef CPriorityQueue<CTimer> CTimerPriorityQueue;
|
||||
|
||||
// X is not thread safe
|
||||
CMutex m_mutex;
|
||||
|
||||
Display* m_display;
|
||||
Window m_root;
|
||||
bool m_stop;
|
||||
|
||||
IScreenReceiver* m_receiver;
|
||||
IScreenEventHandler* m_eventHandler;
|
||||
Window m_window;
|
||||
|
||||
SInt32 m_x, m_y;
|
||||
SInt32 m_w, m_h;
|
||||
|
||||
// clipboards
|
||||
CXWindowsClipboard* m_clipboard[kClipboardEnd];
|
||||
|
||||
// the transparent cursor
|
||||
Cursor m_cursor;
|
||||
|
||||
// screen saver stuff
|
||||
CXWindowsScreenSaver* m_screensaver;
|
||||
bool m_screensaverNotify;
|
||||
Atom m_atomScreensaver;
|
||||
|
||||
// timers, the stopwatch used to time, and a mutex for the timers
|
||||
CTimerPriorityQueue m_timers;
|
||||
CStopwatch m_time;
|
||||
CMutex m_timersMutex;
|
||||
|
||||
// pointer to (singleton) screen. this is only needed by
|
||||
// ioErrorHandler().
|
||||
static CXWindowsScreen* s_screen;
|
||||
};
|
||||
|
||||
class CDisplayLock {
|
||||
public:
|
||||
CDisplayLock(const CXWindowsScreen*);
|
||||
~CDisplayLock();
|
||||
|
||||
operator Display*() const;
|
||||
|
||||
private:
|
||||
const CMutex* m_mutex;
|
||||
Display* m_display;
|
||||
};
|
||||
|
||||
#endif
|
||||
446
lib/platform/CXWindowsScreenSaver.cpp
Normal file
446
lib/platform/CXWindowsScreenSaver.cpp
Normal file
@@ -0,0 +1,446 @@
|
||||
#include "CXWindowsScreenSaver.h"
|
||||
#include "CXWindowsScreen.h"
|
||||
#include "CXWindowsUtil.h"
|
||||
#include "CLog.h"
|
||||
#include "TMethodJob.h"
|
||||
#include <X11/Xatom.h>
|
||||
#if defined(HAVE_X11_EXTENSIONS_XTEST_H)
|
||||
# include <X11/extensions/XTest.h>
|
||||
#else
|
||||
# error The XTest extension is required to build synergy
|
||||
#endif
|
||||
|
||||
//
|
||||
// CXWindowsScreenSaver
|
||||
//
|
||||
|
||||
CXWindowsScreenSaver::CXWindowsScreenSaver(
|
||||
CXWindowsScreen* screen, Display* display) :
|
||||
m_screen(screen),
|
||||
m_display(display),
|
||||
m_notify(None),
|
||||
m_xscreensaver(None),
|
||||
m_xscreensaverActive(false),
|
||||
m_disabled(false)
|
||||
{
|
||||
// screen saver disable callback
|
||||
m_disableJob = new TMethodJob<CXWindowsScreenSaver>(this,
|
||||
&CXWindowsScreenSaver::disableCallback);
|
||||
|
||||
// get atoms
|
||||
m_atomScreenSaver = XInternAtom(m_display,
|
||||
"SCREENSAVER", False);
|
||||
m_atomScreenSaverVersion = XInternAtom(m_display,
|
||||
"_SCREENSAVER_VERSION", False);
|
||||
m_atomScreenSaverActivate = XInternAtom(m_display,
|
||||
"ACTIVATE", False);
|
||||
m_atomScreenSaverDeactivate = XInternAtom(m_display,
|
||||
"DEACTIVATE", False);
|
||||
|
||||
// create dummy window to receive xscreensaver responses. this
|
||||
// shouldn't be necessary (we should be able to send responses
|
||||
// to None) but it doesn't hurt.
|
||||
XSetWindowAttributes attr;
|
||||
attr.event_mask = 0;//PropertyChangeMask;
|
||||
attr.do_not_propagate_mask = 0;
|
||||
attr.override_redirect = True;
|
||||
m_xscreensaverSink = XCreateWindow(m_display,
|
||||
DefaultRootWindow(m_display),
|
||||
0, 0, 1, 1, 0, 0,
|
||||
InputOnly, CopyFromParent,
|
||||
CWDontPropagate | CWEventMask |
|
||||
CWOverrideRedirect,
|
||||
&attr);
|
||||
log((CLOG_DEBUG "xscreensaver sink window is 0x%08x", m_xscreensaverSink));
|
||||
|
||||
// watch top-level windows for changes
|
||||
{
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(m_display, &error);
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(m_display, root, &attr);
|
||||
m_rootEventMask = attr.your_event_mask;
|
||||
XSelectInput(m_display, root, m_rootEventMask | SubstructureNotifyMask);
|
||||
if (error) {
|
||||
log((CLOG_DEBUG "didn't set root event mask"));
|
||||
m_rootEventMask = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// get the xscreensaver window, if any
|
||||
if (!findXScreenSaver()) {
|
||||
setXScreenSaver(None);
|
||||
}
|
||||
|
||||
// get the built-in settings
|
||||
XGetScreenSaver(m_display, &m_timeout, &m_interval,
|
||||
&m_preferBlanking, &m_allowExposures);
|
||||
}
|
||||
|
||||
CXWindowsScreenSaver::~CXWindowsScreenSaver()
|
||||
{
|
||||
// clear watch list
|
||||
clearWatchForXScreenSaver();
|
||||
|
||||
// stop watching root for events
|
||||
CXWindowsUtil::CErrorLock lock(m_display);
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
XSelectInput(m_display, root, m_rootEventMask);
|
||||
|
||||
// destroy dummy sink window
|
||||
XDestroyWindow(m_display, m_xscreensaverSink);
|
||||
|
||||
// done with disable job
|
||||
m_screen->removeTimer(m_disableJob);
|
||||
delete m_disableJob;
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreenSaver::onPreDispatch(const XEvent* xevent)
|
||||
{
|
||||
switch (xevent->type) {
|
||||
case CreateNotify:
|
||||
if (m_xscreensaver == None) {
|
||||
if (isXScreenSaver(xevent->xcreatewindow.window)) {
|
||||
// found the xscreensaver
|
||||
setXScreenSaver(xevent->xcreatewindow.window);
|
||||
}
|
||||
else {
|
||||
// another window to watch. to detect the xscreensaver
|
||||
// window we look for a property but that property may
|
||||
// not yet exist by the time we get this event so we
|
||||
// have to watch the window for property changes.
|
||||
// this would be so much easier if xscreensaver did the
|
||||
// smart thing and stored its window in a property on
|
||||
// the root window.
|
||||
addWatchXScreenSaver(xevent->xcreatewindow.window);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case DestroyNotify:
|
||||
if (xevent->xdestroywindow.window == m_xscreensaver) {
|
||||
// xscreensaver is gone
|
||||
log((CLOG_DEBUG "xscreensaver died"));
|
||||
setXScreenSaver(None);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case PropertyNotify:
|
||||
if (xevent->xproperty.state == PropertyNewValue) {
|
||||
if (isXScreenSaver(xevent->xproperty.window)) {
|
||||
// found the xscreensaver
|
||||
setXScreenSaver(xevent->xcreatewindow.window);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MapNotify:
|
||||
if (xevent->xmap.window == m_xscreensaver) {
|
||||
// xscreensaver has activated
|
||||
setXScreenSaverActive(true);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case UnmapNotify:
|
||||
if (xevent->xunmap.window == m_xscreensaver) {
|
||||
// xscreensaver has deactivated
|
||||
setXScreenSaverActive(false);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::setNotify(Window notify)
|
||||
{
|
||||
m_notify = notify;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::enable()
|
||||
{
|
||||
// for xscreensaver
|
||||
m_disabled = false;
|
||||
m_screen->removeTimer(m_disableJob);
|
||||
|
||||
// for built-in X screen saver
|
||||
XSetScreenSaver(m_display, m_timeout, m_interval,
|
||||
m_preferBlanking, m_allowExposures);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::disable()
|
||||
{
|
||||
// for xscreensaver. 5 seconds should be plenty often to
|
||||
// suppress the screen saver.
|
||||
m_disabled = true;
|
||||
m_screen->addTimer(m_disableJob, 5.0);
|
||||
|
||||
// use built-in X screen saver
|
||||
XGetScreenSaver(m_display, &m_timeout, &m_interval,
|
||||
&m_preferBlanking, &m_allowExposures);
|
||||
XSetScreenSaver(m_display, 0, m_interval,
|
||||
m_preferBlanking, m_allowExposures);
|
||||
// FIXME -- now deactivate?
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::activate()
|
||||
{
|
||||
// remove disable job timer
|
||||
if (m_disabled) {
|
||||
m_screen->removeTimer(m_disableJob);
|
||||
}
|
||||
|
||||
// try xscreensaver
|
||||
findXScreenSaver();
|
||||
if (m_xscreensaver != None) {
|
||||
sendXScreenSaverCommand(m_atomScreenSaverActivate);
|
||||
return;
|
||||
}
|
||||
|
||||
// use built-in X screen saver
|
||||
XForceScreenSaver(m_display, ScreenSaverActive);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::deactivate()
|
||||
{
|
||||
// reinstall disable job timer
|
||||
if (m_disabled) {
|
||||
m_screen->addTimer(m_disableJob, 5.0);
|
||||
}
|
||||
|
||||
// try xscreensaver
|
||||
findXScreenSaver();
|
||||
if (m_xscreensaver != None) {
|
||||
sendXScreenSaverCommand(m_atomScreenSaverDeactivate);
|
||||
return;
|
||||
}
|
||||
|
||||
// use built-in X screen saver
|
||||
XForceScreenSaver(m_display, ScreenSaverReset);
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreenSaver::isActive() const
|
||||
{
|
||||
// check xscreensaver
|
||||
if (m_xscreensaver != None) {
|
||||
return m_xscreensaverActive;
|
||||
}
|
||||
|
||||
// can't check built-in X screen saver activity
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::sendNotify(bool activated)
|
||||
{
|
||||
if (m_notify != None) {
|
||||
XEvent event;
|
||||
event.xclient.type = ClientMessage;
|
||||
event.xclient.display = m_display;
|
||||
event.xclient.window = m_notify;
|
||||
event.xclient.message_type = m_atomScreenSaver;
|
||||
event.xclient.format = 32;
|
||||
event.xclient.data.l[0] = activated ? 1 : 0;
|
||||
event.xclient.data.l[1] = 0;
|
||||
event.xclient.data.l[2] = 0;
|
||||
event.xclient.data.l[3] = 0;
|
||||
event.xclient.data.l[4] = 0;
|
||||
|
||||
CXWindowsUtil::CErrorLock lock(m_display);
|
||||
XSendEvent(m_display, m_notify, False, 0, &event);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreenSaver::findXScreenSaver()
|
||||
{
|
||||
// do nothing if we've already got the xscreensaver window
|
||||
if (m_xscreensaver == None) {
|
||||
// find top-level window xscreensaver window
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
Window rw, pw, *cw;
|
||||
unsigned int nc;
|
||||
if (XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
|
||||
for (unsigned int i = 0; i < nc; ++i) {
|
||||
if (isXScreenSaver(cw[i])) {
|
||||
setXScreenSaver(cw[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
XFree(cw);
|
||||
}
|
||||
}
|
||||
|
||||
return (m_xscreensaver != None);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::setXScreenSaver(Window window)
|
||||
{
|
||||
log((CLOG_DEBUG "xscreensaver window: 0x%08x", window));
|
||||
|
||||
// save window
|
||||
m_xscreensaver = window;
|
||||
|
||||
if (m_xscreensaver != None) {
|
||||
// clear old watch list
|
||||
clearWatchForXScreenSaver();
|
||||
|
||||
// see if xscreensaver is active
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(m_display, &error);
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(m_display, m_xscreensaver, &attr);
|
||||
setXScreenSaverActive(!error && attr.map_state != IsUnmapped);
|
||||
}
|
||||
else {
|
||||
// screen saver can't be active if it doesn't exist
|
||||
setXScreenSaverActive(false);
|
||||
|
||||
// start watching for xscreensaver
|
||||
watchForXScreenSaver();
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreenSaver::isXScreenSaver(Window w) const
|
||||
{
|
||||
// check for m_atomScreenSaverVersion string property
|
||||
Atom type;
|
||||
return (CXWindowsUtil::getWindowProperty(m_display, w,
|
||||
m_atomScreenSaverVersion,
|
||||
NULL, &type, NULL, False) &&
|
||||
type == XA_STRING);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::setXScreenSaverActive(bool activated)
|
||||
{
|
||||
if (m_xscreensaverActive != activated) {
|
||||
log((CLOG_DEBUG "xscreensaver %s", activated ? "activated" : "deactivated"));
|
||||
m_xscreensaverActive = activated;
|
||||
sendNotify(activated);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::sendXScreenSaverCommand(Atom cmd, long arg1, long arg2)
|
||||
{
|
||||
XEvent event;
|
||||
event.xclient.type = ClientMessage;
|
||||
event.xclient.display = m_display;
|
||||
event.xclient.window = m_xscreensaverSink;
|
||||
event.xclient.message_type = m_atomScreenSaver;
|
||||
event.xclient.format = 32;
|
||||
event.xclient.data.l[0] = static_cast<long>(cmd);
|
||||
event.xclient.data.l[1] = arg1;
|
||||
event.xclient.data.l[2] = arg2;
|
||||
event.xclient.data.l[3] = 0;
|
||||
event.xclient.data.l[4] = 0;
|
||||
|
||||
log((CLOG_DEBUG "send xscreensaver command: %d %d %d", (long)cmd, arg1, arg2));
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(m_display, &error);
|
||||
XSendEvent(m_display, m_xscreensaver, False, 0, &event);
|
||||
if (error) {
|
||||
findXScreenSaver();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::watchForXScreenSaver()
|
||||
{
|
||||
// clear old watch list
|
||||
clearWatchForXScreenSaver();
|
||||
|
||||
// add every child of the root to the list of windows to watch
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
Window rw, pw, *cw;
|
||||
unsigned int nc;
|
||||
if (XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
|
||||
for (unsigned int i = 0; i < nc; ++i) {
|
||||
addWatchXScreenSaver(cw[i]);
|
||||
}
|
||||
XFree(cw);
|
||||
}
|
||||
|
||||
// now check for xscreensaver window in case it set the property
|
||||
// before we could request property change events.
|
||||
if (findXScreenSaver()) {
|
||||
// found it so clear out our watch list
|
||||
clearWatchForXScreenSaver();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::clearWatchForXScreenSaver()
|
||||
{
|
||||
// stop watching all windows
|
||||
CXWindowsUtil::CErrorLock lock(m_display);
|
||||
for (CWatchList::iterator index = m_watchWindows.begin();
|
||||
index != m_watchWindows.end(); ++index) {
|
||||
XSelectInput(m_display, index->first, index->second);
|
||||
}
|
||||
m_watchWindows.clear();
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::addWatchXScreenSaver(Window window)
|
||||
{
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(m_display, &error);
|
||||
|
||||
// get window attributes
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(m_display, window, &attr);
|
||||
|
||||
// if successful and window uses override_redirect (like xscreensaver
|
||||
// does) then watch it for property changes.
|
||||
if (!error && attr.override_redirect == True) {
|
||||
XSelectInput(m_display, window,
|
||||
attr.your_event_mask | PropertyChangeMask);
|
||||
if (!error) {
|
||||
// if successful then add the window to our list
|
||||
m_watchWindows.insert(std::make_pair(window, attr.your_event_mask));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::disableCallback(void*)
|
||||
{
|
||||
// send fake mouse motion directly to xscreensaver
|
||||
if (m_xscreensaver != None) {
|
||||
XEvent event;
|
||||
event.xmotion.type = MotionNotify;
|
||||
event.xmotion.display = m_display;
|
||||
event.xmotion.window = m_xscreensaver;
|
||||
event.xmotion.root = DefaultRootWindow(m_display);
|
||||
event.xmotion.subwindow = None;
|
||||
event.xmotion.time = CurrentTime;
|
||||
event.xmotion.x = 0;
|
||||
event.xmotion.y = 0;
|
||||
event.xmotion.x_root = 0;
|
||||
event.xmotion.y_root = 0;
|
||||
event.xmotion.state = 0;
|
||||
event.xmotion.is_hint = NotifyNormal;
|
||||
event.xmotion.same_screen = True;
|
||||
|
||||
CXWindowsUtil::CErrorLock lock(m_display);
|
||||
XSendEvent(m_display, m_xscreensaver, False, 0, &event);
|
||||
}
|
||||
|
||||
// force screen saver off and reset the timer
|
||||
XForceScreenSaver(m_display, ScreenSaverReset);
|
||||
}
|
||||
135
lib/platform/CXWindowsScreenSaver.h
Normal file
135
lib/platform/CXWindowsScreenSaver.h
Normal file
@@ -0,0 +1,135 @@
|
||||
#ifndef CXWINDOWSSCREENSAVER_H
|
||||
#define CXWINDOWSSCREENSAVER_H
|
||||
|
||||
#include "IScreenSaver.h"
|
||||
#include "stdmap.h"
|
||||
#if defined(X_DISPLAY_MISSING)
|
||||
# error X11 is required to build synergy
|
||||
#else
|
||||
# include <X11/Xlib.h>
|
||||
#endif
|
||||
|
||||
class IJob;
|
||||
class CXWindowsScreen;
|
||||
|
||||
//! X11 screen saver implementation
|
||||
class CXWindowsScreenSaver : public IScreenSaver {
|
||||
public:
|
||||
// note -- the caller must ensure that Display* passed to c'tor isn't
|
||||
// being used in another call to Xlib when calling any method on this
|
||||
// object (including during the c'tor and d'tor).
|
||||
CXWindowsScreenSaver(CXWindowsScreen*, Display*);
|
||||
virtual ~CXWindowsScreenSaver();
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Event filtering
|
||||
/*!
|
||||
Called for each event before event translation and dispatch. Return
|
||||
true to skip translation and dispatch. Subclasses should call the
|
||||
superclass's version first and return true if it returns true.
|
||||
*/
|
||||
bool onPreDispatch(const XEvent*);
|
||||
|
||||
//! Set notify target
|
||||
/*!
|
||||
Tells this object to send a ClientMessage to the given window
|
||||
when the screen saver activates or deactivates. Only one window
|
||||
can be notified at a time. The message type is the "SCREENSAVER"
|
||||
atom, the format is 32, and the data.l[0] member is non-zero
|
||||
if activated, zero if deactivated. Pass None to disable
|
||||
notification.
|
||||
*/
|
||||
void setNotify(Window);
|
||||
|
||||
//@}
|
||||
|
||||
// IScreenSaver overrides
|
||||
virtual void enable();
|
||||
virtual void disable();
|
||||
virtual void activate();
|
||||
virtual void deactivate();
|
||||
virtual bool isActive() const;
|
||||
|
||||
private:
|
||||
// send a notification
|
||||
void sendNotify(bool activated);
|
||||
|
||||
// find and set the running xscreensaver's window. returns true iff
|
||||
// found.
|
||||
bool findXScreenSaver();
|
||||
|
||||
// set the xscreensaver's window, updating the activation state flag
|
||||
void setXScreenSaver(Window);
|
||||
|
||||
// returns true if the window appears to be the xscreensaver window
|
||||
bool isXScreenSaver(Window) const;
|
||||
|
||||
// set xscreensaver's activation state flag. sends notification
|
||||
// if the state has changed.
|
||||
void setXScreenSaverActive(bool activated);
|
||||
|
||||
// send a command to xscreensaver
|
||||
void sendXScreenSaverCommand(Atom, long = 0, long = 0);
|
||||
|
||||
// watch all windows that could potentially be the xscreensaver for
|
||||
// the events that will confirm it.
|
||||
void watchForXScreenSaver();
|
||||
|
||||
// stop watching all watched windows
|
||||
void clearWatchForXScreenSaver();
|
||||
|
||||
// add window to the watch list
|
||||
void addWatchXScreenSaver(Window window);
|
||||
|
||||
// called periodically to prevent the screen saver from starting
|
||||
void disableCallback(void*);
|
||||
|
||||
private:
|
||||
typedef std::map<Window, long> CWatchList;
|
||||
|
||||
// the event loop object
|
||||
CXWindowsScreen* m_screen;
|
||||
|
||||
// the X display
|
||||
Display* m_display;
|
||||
|
||||
// old event mask on root window
|
||||
long m_rootEventMask;
|
||||
|
||||
// window to notify on screen saver activation/deactivation
|
||||
Window m_notify;
|
||||
|
||||
// xscreensaver's window
|
||||
Window m_xscreensaver;
|
||||
|
||||
// xscreensaver activation state
|
||||
bool m_xscreensaverActive;
|
||||
|
||||
// dummy window to receive xscreensaver repsonses
|
||||
Window m_xscreensaverSink;
|
||||
|
||||
// potential xscreensaver windows being watched
|
||||
CWatchList m_watchWindows;
|
||||
|
||||
// atoms used to communicate with xscreensaver's window
|
||||
Atom m_atomScreenSaver;
|
||||
Atom m_atomScreenSaverVersion;
|
||||
Atom m_atomScreenSaverActivate;
|
||||
Atom m_atomScreenSaverDeactivate;
|
||||
|
||||
// built-in screen saver settings
|
||||
int m_timeout;
|
||||
int m_interval;
|
||||
int m_preferBlanking;
|
||||
int m_allowExposures;
|
||||
|
||||
// true iff the disabled job timer is installed
|
||||
bool m_disabled;
|
||||
|
||||
// the job used to invoke disableCallback
|
||||
IJob* m_disableJob;
|
||||
};
|
||||
|
||||
#endif
|
||||
262
lib/platform/CXWindowsUtil.cpp
Normal file
262
lib/platform/CXWindowsUtil.cpp
Normal file
@@ -0,0 +1,262 @@
|
||||
#include "CXWindowsUtil.h"
|
||||
#include "CThread.h"
|
||||
#include "CLog.h"
|
||||
#include <X11/Xatom.h>
|
||||
|
||||
//
|
||||
// CXWindowsUtil
|
||||
//
|
||||
|
||||
bool
|
||||
CXWindowsUtil::getWindowProperty(Display* display, Window window,
|
||||
Atom property, CString* data, Atom* type,
|
||||
SInt32* format, bool deleteProperty)
|
||||
{
|
||||
assert(display != NULL);
|
||||
|
||||
Atom actualType;
|
||||
int actualDatumSize;
|
||||
|
||||
// ignore errors. XGetWindowProperty() will report failure.
|
||||
CXWindowsUtil::CErrorLock lock(display);
|
||||
|
||||
// read the property
|
||||
int result = Success;
|
||||
const long length = XMaxRequestSize(display);
|
||||
long offset = 0;
|
||||
unsigned long bytesLeft = 1;
|
||||
while (bytesLeft != 0) {
|
||||
// get more data
|
||||
unsigned long numItems;
|
||||
unsigned char* rawData;
|
||||
result = XGetWindowProperty(display, window, property,
|
||||
offset, length, False, AnyPropertyType,
|
||||
&actualType, &actualDatumSize,
|
||||
&numItems, &bytesLeft, &rawData);
|
||||
if (result != Success || actualType == None || actualDatumSize == 0) {
|
||||
// failed
|
||||
break;
|
||||
}
|
||||
|
||||
// compute bytes read and advance offset
|
||||
unsigned long numBytes;
|
||||
switch (actualDatumSize) {
|
||||
case 8:
|
||||
default:
|
||||
numBytes = numItems;
|
||||
offset += numItems / 4;
|
||||
break;
|
||||
|
||||
case 16:
|
||||
numBytes = 2 * numItems;
|
||||
offset += numItems / 2;
|
||||
break;
|
||||
|
||||
case 32:
|
||||
numBytes = 4 * numItems;
|
||||
offset += numItems;
|
||||
break;
|
||||
}
|
||||
|
||||
// append data
|
||||
if (data != NULL) {
|
||||
data->append((char*)rawData, numBytes);
|
||||
}
|
||||
else {
|
||||
// data is not required so don't try to get any more
|
||||
bytesLeft = 0;
|
||||
}
|
||||
|
||||
// done with returned data
|
||||
XFree(rawData);
|
||||
}
|
||||
|
||||
// delete the property if requested
|
||||
if (deleteProperty) {
|
||||
XDeleteProperty(display, window, property);
|
||||
}
|
||||
|
||||
// save property info
|
||||
if (type != NULL) {
|
||||
*type = actualType;
|
||||
}
|
||||
if (format != NULL) {
|
||||
*format = static_cast<SInt32>(actualDatumSize);
|
||||
}
|
||||
|
||||
if (result == Success) {
|
||||
log((CLOG_DEBUG1 "read property %d on window 0x%08x: bytes=%d", property, window, (data == NULL) ? 0 : data->size()));
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
log((CLOG_DEBUG1 "can't read property %d on window 0x%08x", property, window));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsUtil::setWindowProperty(Display* display, Window window,
|
||||
Atom property, const void* vdata, UInt32 size,
|
||||
Atom type, SInt32 format)
|
||||
{
|
||||
const UInt32 length = 4 * XMaxRequestSize(display);
|
||||
const unsigned char* data = reinterpret_cast<const unsigned char*>(vdata);
|
||||
const UInt32 datumSize = static_cast<UInt32>(format / 8);
|
||||
|
||||
// save errors
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(display, &error);
|
||||
|
||||
// how much data to send in first chunk?
|
||||
UInt32 chunkSize = size;
|
||||
if (chunkSize > length) {
|
||||
chunkSize = length;
|
||||
}
|
||||
|
||||
// send first chunk
|
||||
XChangeProperty(display, window, property,
|
||||
type, format, PropModeReplace,
|
||||
data, chunkSize / datumSize);
|
||||
|
||||
// append remaining chunks
|
||||
data += chunkSize;
|
||||
size -= chunkSize;
|
||||
while (!error && size > 0) {
|
||||
chunkSize = size;
|
||||
if (chunkSize > length) {
|
||||
chunkSize = length;
|
||||
}
|
||||
XChangeProperty(display, window, property,
|
||||
type, format, PropModeAppend,
|
||||
data, chunkSize / datumSize);
|
||||
data += chunkSize;
|
||||
size -= chunkSize;
|
||||
}
|
||||
|
||||
return !error;
|
||||
}
|
||||
|
||||
Time
|
||||
CXWindowsUtil::getCurrentTime(Display* display, Window window)
|
||||
{
|
||||
// select property events on window
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(display, window, &attr);
|
||||
XSelectInput(display, window, attr.your_event_mask | PropertyChangeMask);
|
||||
|
||||
// make a property name to receive dummy change
|
||||
Atom atom = XInternAtom(display, "TIMESTAMP", False);
|
||||
|
||||
// do a zero-length append to get the current time
|
||||
unsigned char dummy;
|
||||
XChangeProperty(display, window, atom,
|
||||
XA_INTEGER, 8,
|
||||
PropModeAppend,
|
||||
&dummy, 0);
|
||||
|
||||
// look for property notify events with the following
|
||||
CPropertyNotifyPredicateInfo filter;
|
||||
filter.m_window = window;
|
||||
filter.m_property = atom;
|
||||
|
||||
// wait for reply
|
||||
XEvent xevent;
|
||||
XIfEvent(display, &xevent, &CXWindowsUtil::propertyNotifyPredicate,
|
||||
(XPointer)&filter);
|
||||
assert(xevent.type == PropertyNotify);
|
||||
assert(xevent.xproperty.window == window);
|
||||
assert(xevent.xproperty.atom == atom);
|
||||
|
||||
// restore event mask
|
||||
XSelectInput(display, window, attr.your_event_mask);
|
||||
|
||||
return xevent.xproperty.time;
|
||||
}
|
||||
|
||||
Bool
|
||||
CXWindowsUtil::propertyNotifyPredicate(Display*, XEvent* xevent, XPointer arg)
|
||||
{
|
||||
CPropertyNotifyPredicateInfo* filter =
|
||||
reinterpret_cast<CPropertyNotifyPredicateInfo*>(arg);
|
||||
return (xevent->type == PropertyNotify &&
|
||||
xevent->xproperty.window == filter->m_window &&
|
||||
xevent->xproperty.atom == filter->m_property &&
|
||||
xevent->xproperty.state == PropertyNewValue) ? True : False;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// CXWindowsUtil::CErrorLock
|
||||
//
|
||||
|
||||
CXWindowsUtil::CErrorLock* CXWindowsUtil::CErrorLock::s_top = NULL;
|
||||
|
||||
CXWindowsUtil::CErrorLock::CErrorLock(Display* display) :
|
||||
m_display(display)
|
||||
{
|
||||
install(&CXWindowsUtil::CErrorLock::ignoreHandler, NULL);
|
||||
}
|
||||
|
||||
CXWindowsUtil::CErrorLock::CErrorLock(Display* display, bool* flag) :
|
||||
m_display(display)
|
||||
{
|
||||
install(&CXWindowsUtil::CErrorLock::saveHandler, flag);
|
||||
}
|
||||
|
||||
CXWindowsUtil::CErrorLock::CErrorLock(Display* display,
|
||||
ErrorHandler handler, void* data) :
|
||||
m_display(display)
|
||||
{
|
||||
install(handler, data);
|
||||
}
|
||||
|
||||
CXWindowsUtil::CErrorLock::~CErrorLock()
|
||||
{
|
||||
// make sure everything finishes before uninstalling handler
|
||||
if (m_display != NULL) {
|
||||
XSync(m_display, False);
|
||||
}
|
||||
|
||||
// restore old handler
|
||||
XSetErrorHandler(m_oldXHandler);
|
||||
s_top = m_next;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsUtil::CErrorLock::install(ErrorHandler handler, void* data)
|
||||
{
|
||||
// make sure everything finishes before installing handler
|
||||
if (m_display != NULL) {
|
||||
XSync(m_display, False);
|
||||
}
|
||||
|
||||
// install handler
|
||||
m_handler = handler;
|
||||
m_userData = data;
|
||||
m_oldXHandler = XSetErrorHandler(
|
||||
&CXWindowsUtil::CErrorLock::internalHandler);
|
||||
m_next = s_top;
|
||||
s_top = this;
|
||||
}
|
||||
|
||||
int
|
||||
CXWindowsUtil::CErrorLock::internalHandler(Display* display, XErrorEvent* event)
|
||||
{
|
||||
if (s_top != NULL && s_top->m_handler != NULL) {
|
||||
s_top->m_handler(display, event, s_top->m_userData);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsUtil::CErrorLock::ignoreHandler(Display*, XErrorEvent* e, void*)
|
||||
{
|
||||
log((CLOG_DEBUG "ignoring X error: %d", e->error_code));
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsUtil::CErrorLock::saveHandler(Display*, XErrorEvent* e, void* flag)
|
||||
{
|
||||
log((CLOG_DEBUG "flagging X error: %d", e->error_code));
|
||||
*reinterpret_cast<bool*>(flag) = true;
|
||||
}
|
||||
103
lib/platform/CXWindowsUtil.h
Normal file
103
lib/platform/CXWindowsUtil.h
Normal file
@@ -0,0 +1,103 @@
|
||||
#ifndef CXWINDOWSUTIL_H
|
||||
#define CXWINDOWSUTIL_H
|
||||
|
||||
#include "CString.h"
|
||||
#include "BasicTypes.h"
|
||||
#if defined(X_DISPLAY_MISSING)
|
||||
# error X11 is required to build synergy
|
||||
#else
|
||||
# include <X11/Xlib.h>
|
||||
#endif
|
||||
|
||||
//! X11 utility functions
|
||||
class CXWindowsUtil {
|
||||
public:
|
||||
//! Get property
|
||||
/*!
|
||||
Gets property \c property on \c window. \b Appends the data to
|
||||
\c *data if \c data is not NULL, saves the property type in \c *type
|
||||
if \c type is not NULL, and saves the property format in \c *format
|
||||
if \c format is not NULL. If \c deleteProperty is true then the
|
||||
property is deleted after being read.
|
||||
*/
|
||||
static bool getWindowProperty(Display*,
|
||||
Window window, Atom property,
|
||||
CString* data, Atom* type,
|
||||
SInt32* format, bool deleteProperty);
|
||||
|
||||
//! Set property
|
||||
/*!
|
||||
Sets property \c property on \c window to \c size bytes of data from
|
||||
\c data.
|
||||
*/
|
||||
static bool setWindowProperty(Display*,
|
||||
Window window, Atom property,
|
||||
const void* data, UInt32 size,
|
||||
Atom type, SInt32 format);
|
||||
|
||||
//! Get X server time
|
||||
/*!
|
||||
Returns the current X server time.
|
||||
*/
|
||||
static Time getCurrentTime(Display*, Window);
|
||||
|
||||
//! X11 error handler
|
||||
/*!
|
||||
This class sets an X error handler in the c'tor and restores the
|
||||
previous error handler in the d'tor. A lock should only be
|
||||
installed while the display is locked by the thread.
|
||||
|
||||
CErrorLock() ignores errors
|
||||
CErrorLock(bool* flag) sets *flag to true if any error occurs
|
||||
*/
|
||||
class CErrorLock {
|
||||
public:
|
||||
//! Error handler type
|
||||
typedef void (*ErrorHandler)(Display*, XErrorEvent*, void* userData);
|
||||
|
||||
/*!
|
||||
Ignore X11 errors.
|
||||
*/
|
||||
CErrorLock(Display*);
|
||||
|
||||
/*!
|
||||
Set \c *errorFlag if any error occurs.
|
||||
*/
|
||||
CErrorLock(Display*, bool* errorFlag);
|
||||
|
||||
/*!
|
||||
Call \c handler on each error.
|
||||
*/
|
||||
CErrorLock(Display*, ErrorHandler handler, void* userData);
|
||||
|
||||
~CErrorLock();
|
||||
|
||||
private:
|
||||
void install(ErrorHandler, void*);
|
||||
static int internalHandler(Display*, XErrorEvent*);
|
||||
static void ignoreHandler(Display*, XErrorEvent*, void*);
|
||||
static void saveHandler(Display*, XErrorEvent*, void*);
|
||||
|
||||
private:
|
||||
typedef int (*XErrorHandler)(Display*, XErrorEvent*);
|
||||
|
||||
Display* m_display;
|
||||
ErrorHandler m_handler;
|
||||
void* m_userData;
|
||||
XErrorHandler m_oldXHandler;
|
||||
CErrorLock* m_next;
|
||||
static CErrorLock* s_top;
|
||||
};
|
||||
|
||||
private:
|
||||
class CPropertyNotifyPredicateInfo {
|
||||
public:
|
||||
Window m_window;
|
||||
Atom m_property;
|
||||
};
|
||||
|
||||
static Bool propertyNotifyPredicate(Display*,
|
||||
XEvent* xevent, XPointer arg);
|
||||
};
|
||||
|
||||
#endif
|
||||
35
lib/platform/IMSWindowsScreenEventHandler.h
Executable file
35
lib/platform/IMSWindowsScreenEventHandler.h
Executable file
@@ -0,0 +1,35 @@
|
||||
#ifndef IMSWINDOWSSCREENEVENTHANDLER_H
|
||||
#define IMSWINDOWSSCREENEVENTHANDLER_H
|
||||
|
||||
#include "IScreenEventHandler.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
//! MS Windows screen event handler interface
|
||||
class IMSWindowsScreenEventHandler : public IScreenEventHandler {
|
||||
public:
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Notify of window creation
|
||||
/*!
|
||||
This is called after the window is created.
|
||||
*/
|
||||
virtual void postCreateWindow(HWND) = 0;
|
||||
|
||||
//! Notify of window destruction
|
||||
/*!
|
||||
This is called before the window is destroyed.
|
||||
*/
|
||||
virtual void preDestroyWindow(HWND) = 0;
|
||||
|
||||
//@}
|
||||
|
||||
// IScreenEventHandler overrides
|
||||
virtual void onScreensaver(bool activated) = 0;
|
||||
virtual bool onPreDispatch(const CEvent* event) = 0;
|
||||
virtual bool onEvent(CEvent* event) = 0;
|
||||
virtual SInt32 getJumpZoneSize() const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
113
lib/platform/IPlatform.h
Normal file
113
lib/platform/IPlatform.h
Normal file
@@ -0,0 +1,113 @@
|
||||
#ifndef IPLATFORM_H
|
||||
#define IPLATFORM_H
|
||||
|
||||
#include "IInterface.h"
|
||||
#include "CString.h"
|
||||
|
||||
//! Platform dependent functions interface
|
||||
/*!
|
||||
This interface provides methods to query platform specific data and
|
||||
perform operations that are inherently non-portable.
|
||||
*/
|
||||
class IPlatform : public IInterface {
|
||||
public:
|
||||
typedef int (*DaemonFunc)(IPlatform*, int argc, const char** argv);
|
||||
|
||||
//! Uninstall result codes
|
||||
enum EResult {
|
||||
kSuccess, //!< Uninstall succeeded
|
||||
kFailed, //!< Uninstall failed
|
||||
kAlready //!< Not installed
|
||||
};
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Install daemon
|
||||
/*!
|
||||
Install a daemon. \c name is the name of the daemon passed to the
|
||||
system and \c description is a short human readable description of
|
||||
the daemon. \c pathname is the path to the daemon executable.
|
||||
\c commandLine should \b not include the name of program as the
|
||||
first argument.
|
||||
*/
|
||||
// FIXME -- throw on error? will get better error messages that way.
|
||||
virtual bool installDaemon(const char* name,
|
||||
const char* description,
|
||||
const char* pathname,
|
||||
const char* commandLine) = 0;
|
||||
//! Uninstall daemon
|
||||
/*!
|
||||
Uninstall a daemon.
|
||||
*/
|
||||
virtual EResult uninstallDaemon(const char* name) = 0;
|
||||
|
||||
//! Daemonize the process
|
||||
/*!
|
||||
Daemonize. This should call installDaemonLogger(). Returns
|
||||
true iff successful. \c name is the name of the daemon.
|
||||
Once daemonized, \c func is invoked and daemonize returns when
|
||||
and what it does. daemonize() returns -1 on error.
|
||||
|
||||
Exactly what happens when daemonizing depends on the platform.
|
||||
<ul>
|
||||
<li>unix:
|
||||
Detaches from terminal. \c func gets passed one argument, the
|
||||
name passed to daemonize().
|
||||
<li>win32:
|
||||
Becomes a service. Argument 0 is the name of the service
|
||||
and the rest are the arguments passed to StartService().
|
||||
\c func is only called when the service is actually started.
|
||||
\c func must behave like a proper ServiceMain() function; in
|
||||
particular, it must call RegisterServiceCtrlHandler() and
|
||||
SetServiceStatus().
|
||||
</ul>
|
||||
*/
|
||||
virtual int daemonize(const char* name, DaemonFunc func) = 0;
|
||||
|
||||
//! Install daemon logger
|
||||
/*!
|
||||
Directs CLog to send messages to the daemon log. Used when
|
||||
messages should no longer go to the console. \c name is used
|
||||
in the log to identify this process.
|
||||
*/
|
||||
virtual void installDaemonLogger(const char* name) = 0;
|
||||
|
||||
//@}
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Extract base name
|
||||
/*!
|
||||
Find the base name in the given \c pathname.
|
||||
*/
|
||||
virtual const char* getBasename(const char* pathname) const = 0;
|
||||
|
||||
//! Get user's home directory
|
||||
/*!
|
||||
Returns the user's home directory. Returns the empty string if
|
||||
this cannot be determined.
|
||||
*/
|
||||
virtual CString getUserDirectory() const = 0;
|
||||
|
||||
//! Get system directory
|
||||
/*!
|
||||
Returns the ussystem configuration file directory.
|
||||
*/
|
||||
virtual CString getSystemDirectory() const = 0;
|
||||
|
||||
//! Concatenate path components
|
||||
/*!
|
||||
Concatenate pathname components with a directory separator
|
||||
between them. This should not check if the resulting path
|
||||
is longer than allowed by the system; we'll rely on the
|
||||
system calls to tell us that.
|
||||
*/
|
||||
virtual CString addPathComponent(
|
||||
const CString& prefix,
|
||||
const CString& suffix) const = 0;
|
||||
|
||||
//@}
|
||||
};
|
||||
|
||||
#endif
|
||||
31
lib/platform/Makefile.am
Normal file
31
lib/platform/Makefile.am
Normal file
@@ -0,0 +1,31 @@
|
||||
## Process this file with automake to produce Makefile.in
|
||||
NULL =
|
||||
DEPTH = ../..
|
||||
|
||||
# FIXME -- add CUnixPlatform.cpp as an unbuilt source
|
||||
noinst_LIBRARIES = libplatform.a
|
||||
libplatform_a_SOURCES = \
|
||||
CPlatform.cpp \
|
||||
CXWindowsClipboard.cpp \
|
||||
CXWindowsClipboardTextConverter.cpp \
|
||||
CXWindowsClipboardUCS2Converter.cpp \
|
||||
CXWindowsClipboardUTF8Converter.cpp \
|
||||
CXWindowsScreen.cpp \
|
||||
CXWindowsScreenSaver.cpp \
|
||||
CXWindowsUtil.cpp \
|
||||
CPlatform.h \
|
||||
CUnixPlatform.h \
|
||||
CXWindowsClipboard.h \
|
||||
CXWindowsClipboardTextConverter.h \
|
||||
CXWindowsClipboardUCS2Converter.h \
|
||||
CXWindowsClipboardUTF8Converter.h \
|
||||
CXWindowsScreen.h \
|
||||
CXWindowsScreenSaver.h \
|
||||
CXWindowsUtil.h \
|
||||
IPlatform.h \
|
||||
$(NULL)
|
||||
INCLUDES = \
|
||||
-I$(DEPTH)/lib/base \
|
||||
-I$(DEPTH)/lib/mt \
|
||||
-I$(DEPTH)/lib/synergy \
|
||||
$(NULL)
|
||||
63
lib/platform/makehook.dsp
Normal file
63
lib/platform/makehook.dsp
Normal file
@@ -0,0 +1,63 @@
|
||||
# Microsoft Developer Studio Project File - Name="makehook" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Generic Project" 0x010a
|
||||
|
||||
CFG=makehook - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "makehook.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "makehook.mak" CFG="makehook - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "makehook - Win32 Release" (based on "Win32 (x86) Generic Project")
|
||||
!MESSAGE "makehook - Win32 Debug" (based on "Win32 (x86) Generic Project")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
MTL=midl.exe
|
||||
|
||||
!IF "$(CFG)" == "makehook - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "makehook___Win32_Release"
|
||||
# PROP BASE Intermediate_Dir "makehook___Win32_Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Target_Dir ""
|
||||
|
||||
!ELSEIF "$(CFG)" == "makehook - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "makehook___Win32_Debug"
|
||||
# PROP BASE Intermediate_Dir "makehook___Win32_Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Target_Dir ""
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "makehook - Win32 Release"
|
||||
# Name "makehook - Win32 Debug"
|
||||
# End Target
|
||||
# End Project
|
||||
175
lib/platform/platform.dsp
Normal file
175
lib/platform/platform.dsp
Normal file
@@ -0,0 +1,175 @@
|
||||
# Microsoft Developer Studio Project File - Name="platform" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Static Library" 0x0104
|
||||
|
||||
CFG=platform - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "platform.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "platform.mak" CFG="platform - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "platform - Win32 Release" (based on "Win32 (x86) Static Library")
|
||||
!MESSAGE "platform - Win32 Debug" (based on "Win32 (x86) Static Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "platform - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W4 /GX /O2 /I "..\base" /I "..\mt" /I "..\synergy" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LIB32=link.exe -lib
|
||||
# ADD BASE LIB32 /nologo
|
||||
# ADD LIB32 /nologo
|
||||
|
||||
!ELSEIF "$(CFG)" == "platform - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "..\base" /I "..\mt" /I "..\synergy" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FD /GZ /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LIB32=link.exe -lib
|
||||
# ADD BASE LIB32 /nologo
|
||||
# ADD LIB32 /nologo
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "platform - Win32 Release"
|
||||
# Name "platform - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboard.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboardAnyTextConverter.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboardTextConverter.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboardUTF16Converter.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsScreen.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsScreenSaver.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CPlatform.cpp
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboard.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboardAnyTextConverter.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboardTextConverter.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsClipboardUTF16Converter.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsScreen.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CMSWindowsScreenSaver.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CPlatform.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CWin32Platform.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\IMSWindowsScreenEventHandler.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\IPlatform.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# Begin Group "Included Files"
|
||||
|
||||
# PROP Default_Filter "inc"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CWin32Platform.cpp
|
||||
# PROP Exclude_From_Build 1
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
112
lib/platform/synrgyhk.dsp
Normal file
112
lib/platform/synrgyhk.dsp
Normal file
@@ -0,0 +1,112 @@
|
||||
# Microsoft Developer Studio Project File - Name="synrgyhk" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=synrgyhk - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "synrgyhk.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "synrgyhk.mak" CFG="synrgyhk - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "synrgyhk - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "synrgyhk - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "synrgyhk - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "synrgyhk___Win32_Release"
|
||||
# PROP BASE Intermediate_Dir "synrgyhk___Win32_Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "../../Release"
|
||||
# PROP Intermediate_Dir "ReleaseHook"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SYNRGYHK_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W4 /GX /O2 /I "..\base" /I "..\net" /I "..\synergy" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SYNRGYHK_EXPORTS" /FD /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
|
||||
!ELSEIF "$(CFG)" == "synrgyhk - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "synrgyhk___Win32_Debug"
|
||||
# PROP BASE Intermediate_Dir "synrgyhk___Win32_Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "../../Debug"
|
||||
# PROP Intermediate_Dir "DebugHook"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SYNRGYHK_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "..\synergy" /I "..\base" /I "..\net" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SYNRGYHK_EXPORTS" /FD /GZ /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "synrgyhk - Win32 Release"
|
||||
# Name "synrgyhk - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CSynergyHook.cpp
|
||||
# ADD CPP /D _WIN32_WINNT=0x0400
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CSynergyHook.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
Reference in New Issue
Block a user