2002-05-27 20:51:07 +04:00
|
|
|
#include "CXWindowsUtil.h"
|
|
|
|
#include "CThread.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CLog.h"
|
2002-05-27 20:51:07 +04:00
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
2002-05-27 22:30:13 +04:00
|
|
|
//
|
|
|
|
// CXWindowsUtil
|
|
|
|
//
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsUtil::getWindowProperty(Display* display, Window window,
|
|
|
|
Atom property, CString* data, Atom* type,
|
|
|
|
int* format, bool deleteProperty)
|
2002-05-27 20:51:07 +04:00
|
|
|
{
|
|
|
|
assert(display != NULL);
|
|
|
|
|
|
|
|
Atom actualType;
|
|
|
|
int actualDatumSize;
|
|
|
|
|
2002-05-27 22:30:13 +04:00
|
|
|
// ignore errors. XGetWindowProperty() will report failure.
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock lock(display);
|
2002-05-27 22:30:13 +04:00
|
|
|
|
2002-05-27 20:51:07 +04:00
|
|
|
// read the property
|
|
|
|
const long length = XMaxRequestSize(display);
|
|
|
|
long offset = 0;
|
|
|
|
unsigned long bytesLeft = 1;
|
|
|
|
while (bytesLeft != 0) {
|
|
|
|
// get more data
|
|
|
|
unsigned long numItems;
|
|
|
|
unsigned char* rawData;
|
|
|
|
const int result = XGetWindowProperty(display, window, property,
|
|
|
|
offset, length, False, AnyPropertyType,
|
|
|
|
&actualType, &actualDatumSize,
|
|
|
|
&numItems, &bytesLeft, &rawData);
|
|
|
|
if (result != Success || actualType == None || actualDatumSize == 0) {
|
|
|
|
// failed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 19:43:40 +04: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 20:51:07 +04: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);
|
|
|
|
}
|
|
|
|
|
2002-06-23 19:43:40 +04:00
|
|
|
log((CLOG_DEBUG1 "read property %d on window 0x%08x: bytes=%d", property, window, (data == NULL) ? 0 : data->size()));
|
2002-05-27 20:51:07 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsUtil::setWindowProperty(Display* display, Window window,
|
|
|
|
Atom property, const void* vdata, UInt32 size,
|
|
|
|
Atom type, SInt32 format)
|
2002-05-27 20:51:07 +04: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 22:30:13 +04:00
|
|
|
// save errors
|
|
|
|
bool error = false;
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock lock(display, &error);
|
2002-05-27 22:30:13 +04:00
|
|
|
|
2002-05-27 20:51:07 +04:00
|
|
|
// how much data to send in first chunk?
|
|
|
|
UInt32 chunkSize = size;
|
2002-06-11 02:06:45 +04:00
|
|
|
if (chunkSize > length) {
|
2002-05-27 20:51:07 +04:00
|
|
|
chunkSize = length;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2002-05-27 20:51:07 +04:00
|
|
|
|
|
|
|
// send first chunk
|
|
|
|
XChangeProperty(display, window, property,
|
|
|
|
type, format, PropModeReplace,
|
|
|
|
data, chunkSize / datumSize);
|
|
|
|
|
|
|
|
// append remaining chunks
|
|
|
|
data += chunkSize;
|
|
|
|
size -= chunkSize;
|
2002-05-27 22:30:13 +04:00
|
|
|
while (!error && size > 0) {
|
2002-05-27 20:51:07 +04:00
|
|
|
chunkSize = size;
|
2002-06-11 02:06:45 +04:00
|
|
|
if (chunkSize > length) {
|
2002-05-27 20:51:07 +04:00
|
|
|
chunkSize = length;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2002-05-27 20:51:07 +04:00
|
|
|
XChangeProperty(display, window, property,
|
|
|
|
type, format, PropModeAppend,
|
|
|
|
data, chunkSize / datumSize);
|
|
|
|
data += chunkSize;
|
|
|
|
size -= chunkSize;
|
|
|
|
}
|
|
|
|
|
2002-05-27 22:30:13 +04:00
|
|
|
return !error;
|
2002-05-27 20:51:07 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
Time
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsUtil::getCurrentTime(Display* display, Window window)
|
2002-05-27 20:51:07 +04: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-03 01:03:38 +04:00
|
|
|
XIfEvent(display, &xevent, &CXWindowsUtil::propertyNotifyPredicate,
|
|
|
|
(XPointer)&filter);
|
2002-05-27 20:51:07 +04: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-11 02:06:45 +04:00
|
|
|
Bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsUtil::propertyNotifyPredicate(Display*, XEvent* xevent, XPointer arg)
|
2002-05-27 20:51:07 +04: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 22:30:13 +04:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CXWindowsUtil::CErrorLock
|
|
|
|
//
|
|
|
|
|
|
|
|
CXWindowsUtil::CErrorLock* CXWindowsUtil::CErrorLock::s_top = NULL;
|
|
|
|
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock::CErrorLock(Display* display) :
|
|
|
|
m_display(display)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
|
|
|
install(&CXWindowsUtil::CErrorLock::ignoreHandler, NULL);
|
|
|
|
}
|
|
|
|
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock::CErrorLock(Display* display, bool* flag) :
|
|
|
|
m_display(display)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
|
|
|
install(&CXWindowsUtil::CErrorLock::saveHandler, flag);
|
|
|
|
}
|
|
|
|
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock::CErrorLock(Display* display,
|
|
|
|
ErrorHandler handler, void* data) :
|
|
|
|
m_display(display)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
|
|
|
install(handler, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsUtil::CErrorLock::~CErrorLock()
|
|
|
|
{
|
2002-06-22 23:47:27 +04:00
|
|
|
// make sure everything finishes before uninstalling handler
|
|
|
|
XSync(m_display, False);
|
|
|
|
|
|
|
|
// restore old handler
|
2002-05-27 22:30:13 +04:00
|
|
|
XSetErrorHandler(m_oldXHandler);
|
|
|
|
s_top = m_next;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsUtil::CErrorLock::install(ErrorHandler handler, void* data)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
2002-06-22 23:47:27 +04:00
|
|
|
// make sure everything finishes before installing handler
|
|
|
|
XSync(m_display, False);
|
|
|
|
|
|
|
|
// install handler
|
2002-05-27 22:30:13 +04:00
|
|
|
m_handler = handler;
|
|
|
|
m_userData = data;
|
|
|
|
m_oldXHandler = XSetErrorHandler(
|
|
|
|
&CXWindowsUtil::CErrorLock::internalHandler);
|
|
|
|
m_next = s_top;
|
|
|
|
s_top = this;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
int
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsUtil::CErrorLock::internalHandler(Display* display, XErrorEvent* event)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
|
|
|
if (s_top != NULL && s_top->m_handler != NULL) {
|
|
|
|
s_top->m_handler(display, event, s_top->m_userData);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock::ignoreHandler(Display*, XErrorEvent* e, void*)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
2002-06-22 23:47:27 +04:00
|
|
|
log((CLOG_DEBUG "ignoring X error: %d", e->error_code));
|
2002-05-27 22:30:13 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-22 23:47:27 +04:00
|
|
|
CXWindowsUtil::CErrorLock::saveHandler(Display*, XErrorEvent* e, void* flag)
|
2002-05-27 22:30:13 +04:00
|
|
|
{
|
2002-06-22 23:47:27 +04:00
|
|
|
log((CLOG_DEBUG "flagging X error: %d", e->error_code));
|
2002-05-27 22:30:13 +04:00
|
|
|
*reinterpret_cast<bool*>(flag) = true;
|
|
|
|
}
|