2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2002-05-27 16:51:07 +00:00
|
|
|
#include "CXWindowsUtil.h"
|
|
|
|
#include "CThread.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.h"
|
2002-05-27 16:51:07 +00:00
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
2002-05-27 18:30:13 +00:00
|
|
|
//
|
|
|
|
// CXWindowsUtil
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsUtil::getWindowProperty(Display* display, Window window,
|
|
|
|
Atom property, CString* data, Atom* type,
|
2002-07-26 18:27:31 +00:00
|
|
|
SInt32* format, bool deleteProperty)
|
2002-05-27 16:51:07 +00:00
|
|
|
{
|
|
|
|
assert(display != NULL);
|
|
|
|
|
|
|
|
Atom actualType;
|
|
|
|
int actualDatumSize;
|
|
|
|
|
2002-05-27 18:30:13 +00:00
|
|
|
// ignore errors. XGetWindowProperty() will report failure.
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock lock(display);
|
2002-05-27 18:30:13 +00:00
|
|
|
|
2002-05-27 16:51:07 +00:00
|
|
|
// read the property
|
2003-03-21 19:13:15 +00:00
|
|
|
bool okay = true;
|
2002-05-27 16:51:07 +00:00
|
|
|
const long length = XMaxRequestSize(display);
|
|
|
|
long offset = 0;
|
|
|
|
unsigned long bytesLeft = 1;
|
|
|
|
while (bytesLeft != 0) {
|
|
|
|
// get more data
|
|
|
|
unsigned long numItems;
|
|
|
|
unsigned char* rawData;
|
2003-03-21 19:13:15 +00:00
|
|
|
if (XGetWindowProperty(display, window, property,
|
2002-05-27 16:51:07 +00:00
|
|
|
offset, length, False, AnyPropertyType,
|
|
|
|
&actualType, &actualDatumSize,
|
2003-03-21 19:13:15 +00:00
|
|
|
&numItems, &bytesLeft, &rawData) != Success ||
|
|
|
|
actualType == None || actualDatumSize == 0) {
|
2002-05-27 16:51:07 +00:00
|
|
|
// failed
|
2003-03-21 19:13:15 +00:00
|
|
|
okay = false;
|
2002-07-26 15:22:25 +00:00
|
|
|
break;
|
2002-05-27 16:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2002-06-23 15:43:40 +00:00
|
|
|
if (data != NULL) {
|
|
|
|
data->append((char*)rawData, numBytes);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// data is not required so don't try to get any more
|
|
|
|
bytesLeft = 0;
|
|
|
|
}
|
2002-05-27 16:51:07 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2003-03-21 19:13:15 +00:00
|
|
|
if (okay) {
|
2003-01-08 21:36:13 +00:00
|
|
|
LOG((CLOG_DEBUG2 "read property %d on window 0x%08x: bytes=%d", property, window, (data == NULL) ? 0 : data->size()));
|
2002-07-26 15:22:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2003-03-21 19:13:15 +00:00
|
|
|
LOG((CLOG_DEBUG2 "can't read property %d on window 0x%08x", property, window));
|
2002-07-26 15:22:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-05-27 16:51:07 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsUtil::setWindowProperty(Display* display, Window window,
|
|
|
|
Atom property, const void* vdata, UInt32 size,
|
|
|
|
Atom type, SInt32 format)
|
2002-05-27 16:51:07 +00:00
|
|
|
{
|
|
|
|
const UInt32 length = 4 * XMaxRequestSize(display);
|
|
|
|
const unsigned char* data = reinterpret_cast<const unsigned char*>(vdata);
|
|
|
|
const UInt32 datumSize = static_cast<UInt32>(format / 8);
|
|
|
|
|
2002-05-27 18:30:13 +00:00
|
|
|
// save errors
|
|
|
|
bool error = false;
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock lock(display, &error);
|
2002-05-27 18:30:13 +00:00
|
|
|
|
2002-05-27 16:51:07 +00:00
|
|
|
// how much data to send in first chunk?
|
|
|
|
UInt32 chunkSize = size;
|
2002-06-10 22:06:45 +00:00
|
|
|
if (chunkSize > length) {
|
2002-05-27 16:51:07 +00:00
|
|
|
chunkSize = length;
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2002-05-27 16:51:07 +00:00
|
|
|
|
|
|
|
// send first chunk
|
|
|
|
XChangeProperty(display, window, property,
|
|
|
|
type, format, PropModeReplace,
|
|
|
|
data, chunkSize / datumSize);
|
|
|
|
|
|
|
|
// append remaining chunks
|
|
|
|
data += chunkSize;
|
|
|
|
size -= chunkSize;
|
2002-05-27 18:30:13 +00:00
|
|
|
while (!error && size > 0) {
|
2002-05-27 16:51:07 +00:00
|
|
|
chunkSize = size;
|
2002-06-10 22:06:45 +00:00
|
|
|
if (chunkSize > length) {
|
2002-05-27 16:51:07 +00:00
|
|
|
chunkSize = length;
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2002-05-27 16:51:07 +00:00
|
|
|
XChangeProperty(display, window, property,
|
|
|
|
type, format, PropModeAppend,
|
|
|
|
data, chunkSize / datumSize);
|
|
|
|
data += chunkSize;
|
|
|
|
size -= chunkSize;
|
|
|
|
}
|
|
|
|
|
2002-05-27 18:30:13 +00:00
|
|
|
return !error;
|
2002-05-27 16:51:07 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
Time
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsUtil::getCurrentTime(Display* display, Window window)
|
2002-05-27 16:51:07 +00:00
|
|
|
{
|
|
|
|
// 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;
|
2002-06-02 21:03:38 +00:00
|
|
|
XIfEvent(display, &xevent, &CXWindowsUtil::propertyNotifyPredicate,
|
|
|
|
(XPointer)&filter);
|
2002-05-27 16:51:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
Bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsUtil::propertyNotifyPredicate(Display*, XEvent* xevent, XPointer arg)
|
2002-05-27 16:51:07 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2002-05-27 18:30:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CXWindowsUtil::CErrorLock
|
|
|
|
//
|
|
|
|
|
|
|
|
CXWindowsUtil::CErrorLock* CXWindowsUtil::CErrorLock::s_top = NULL;
|
|
|
|
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock::CErrorLock(Display* display) :
|
|
|
|
m_display(display)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
|
|
|
install(&CXWindowsUtil::CErrorLock::ignoreHandler, NULL);
|
|
|
|
}
|
|
|
|
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock::CErrorLock(Display* display, bool* flag) :
|
|
|
|
m_display(display)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
|
|
|
install(&CXWindowsUtil::CErrorLock::saveHandler, flag);
|
|
|
|
}
|
|
|
|
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock::CErrorLock(Display* display,
|
|
|
|
ErrorHandler handler, void* data) :
|
|
|
|
m_display(display)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
|
|
|
install(handler, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsUtil::CErrorLock::~CErrorLock()
|
|
|
|
{
|
2002-06-22 19:47:27 +00:00
|
|
|
// make sure everything finishes before uninstalling handler
|
2002-07-24 13:01:18 +00:00
|
|
|
if (m_display != NULL) {
|
|
|
|
XSync(m_display, False);
|
|
|
|
}
|
2002-06-22 19:47:27 +00:00
|
|
|
|
|
|
|
// restore old handler
|
2002-05-27 18:30:13 +00:00
|
|
|
XSetErrorHandler(m_oldXHandler);
|
|
|
|
s_top = m_next;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsUtil::CErrorLock::install(ErrorHandler handler, void* data)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
2002-06-22 19:47:27 +00:00
|
|
|
// make sure everything finishes before installing handler
|
2002-07-24 13:01:18 +00:00
|
|
|
if (m_display != NULL) {
|
|
|
|
XSync(m_display, False);
|
|
|
|
}
|
2002-06-22 19:47:27 +00:00
|
|
|
|
|
|
|
// install handler
|
2002-05-27 18:30:13 +00:00
|
|
|
m_handler = handler;
|
|
|
|
m_userData = data;
|
|
|
|
m_oldXHandler = XSetErrorHandler(
|
|
|
|
&CXWindowsUtil::CErrorLock::internalHandler);
|
|
|
|
m_next = s_top;
|
|
|
|
s_top = this;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsUtil::CErrorLock::internalHandler(Display* display, XErrorEvent* event)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
|
|
|
if (s_top != NULL && s_top->m_handler != NULL) {
|
|
|
|
s_top->m_handler(display, event, s_top->m_userData);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock::ignoreHandler(Display*, XErrorEvent* e, void*)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_DEBUG1 "ignoring X error: %d", e->error_code));
|
2002-05-27 18:30:13 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-22 19:47:27 +00:00
|
|
|
CXWindowsUtil::CErrorLock::saveHandler(Display*, XErrorEvent* e, void* flag)
|
2002-05-27 18:30:13 +00:00
|
|
|
{
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_DEBUG1 "flagging X error: %d", e->error_code));
|
2002-05-27 18:30:13 +00:00
|
|
|
*reinterpret_cast<bool*>(flag) = true;
|
|
|
|
}
|