2001-11-11 21:15:30 +00:00
|
|
|
#include "CXWindowsScreen.h"
|
2002-05-27 16:22:59 +00:00
|
|
|
#include "CXWindowsClipboard.h"
|
|
|
|
#include "CXWindowsUtil.h"
|
|
|
|
#include "CClipboard.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XScreen.h"
|
2001-11-11 21:15:30 +00:00
|
|
|
#include "CLock.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CThread.h"
|
2001-11-11 21:15:30 +00:00
|
|
|
#include "CLog.h"
|
2001-11-13 23:34:12 +00:00
|
|
|
#include "CString.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2001-11-11 21:15:30 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CXWindowsScreen
|
|
|
|
//
|
|
|
|
|
2002-06-03 13:45:30 +00:00
|
|
|
CXWindowsScreen* CXWindowsScreen::s_screen = NULL;
|
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
CXWindowsScreen::CXWindowsScreen() :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_display(NULL),
|
|
|
|
m_root(None),
|
|
|
|
m_w(0), m_h(0),
|
|
|
|
m_stop(false)
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
2002-06-03 13:45:30 +00:00
|
|
|
assert(s_screen == NULL);
|
|
|
|
s_screen = this;
|
2001-11-11 21:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::~CXWindowsScreen()
|
|
|
|
{
|
2002-06-03 13:45:30 +00:00
|
|
|
assert(s_screen != NULL);
|
2001-11-11 21:15:30 +00:00
|
|
|
assert(m_display == NULL);
|
2002-06-03 13:45:30 +00:00
|
|
|
|
|
|
|
s_screen = NULL;
|
2001-11-11 21:15:30 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CXWindowsScreen::openDisplay()
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
assert(m_display == NULL);
|
|
|
|
|
2002-06-03 13:45:30 +00:00
|
|
|
// set the X I/O error handler so we catch the display disconnecting
|
|
|
|
XSetIOErrorHandler(&CXWindowsScreen::ioErrorHandler);
|
|
|
|
|
2002-06-09 18:03:32 +00:00
|
|
|
// get the DISPLAY
|
|
|
|
const char* display = getenv("DISPLAY");
|
|
|
|
if (display == NULL) {
|
|
|
|
display = ":0.0";
|
|
|
|
}
|
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
// open the display
|
2002-06-09 18:03:32 +00:00
|
|
|
log((CLOG_DEBUG "XOpenDisplay(\"%s\")", display));
|
|
|
|
m_display = XOpenDisplay(display);
|
2002-06-10 22:06:45 +00:00
|
|
|
if (m_display == NULL) {
|
2002-06-03 13:45:30 +00:00
|
|
|
throw XScreenOpenFailure();
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-11-11 21:15:30 +00:00
|
|
|
|
|
|
|
// get default screen
|
2002-06-10 22:06:45 +00:00
|
|
|
m_screen = DefaultScreen(m_display);
|
2001-11-11 21:15:30 +00:00
|
|
|
Screen* screen = ScreenOfDisplay(m_display, m_screen);
|
|
|
|
|
|
|
|
// get screen size
|
|
|
|
m_w = WidthOfScreen(screen);
|
|
|
|
m_h = HeightOfScreen(screen);
|
|
|
|
log((CLOG_INFO "display size: %dx%d", m_w, m_h));
|
|
|
|
|
|
|
|
// get the root window
|
|
|
|
m_root = RootWindow(m_display, m_screen);
|
|
|
|
|
|
|
|
// let subclass prep display
|
2002-06-03 13:45:30 +00:00
|
|
|
onOpenDisplay(m_display);
|
2002-05-27 16:22:59 +00:00
|
|
|
|
|
|
|
// initialize clipboards
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
m_clipboard[id] = createClipboard(id);
|
|
|
|
}
|
2001-11-11 21:15:30 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CXWindowsScreen::closeDisplay()
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
2002-06-03 13:45:30 +00:00
|
|
|
CLock lock(&m_mutex);
|
2001-11-11 21:15:30 +00:00
|
|
|
|
|
|
|
// let subclass close down display
|
2002-06-03 13:45:30 +00:00
|
|
|
onCloseDisplay(m_display);
|
2001-11-11 21:15:30 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// destroy clipboards
|
2002-04-27 14:19:53 +00:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-05-27 16:22:59 +00:00
|
|
|
delete m_clipboard[id];
|
2001-11-26 22:09:53 +00:00
|
|
|
}
|
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
// close the display
|
2002-06-03 13:45:30 +00:00
|
|
|
if (m_display != NULL) {
|
|
|
|
XCloseDisplay(m_display);
|
|
|
|
m_display = NULL;
|
|
|
|
log((CLOG_DEBUG "closed display"));
|
|
|
|
}
|
|
|
|
XSetIOErrorHandler(NULL);
|
2001-11-11 21:15:30 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
|
|
|
CXWindowsScreen::getScreen() const
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
return m_screen;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
Window
|
|
|
|
CXWindowsScreen::getRoot() const
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
return m_root;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::getScreenSize(SInt32* w, SInt32* h) const
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
assert(w != NULL && h != NULL);
|
|
|
|
|
|
|
|
*w = m_w;
|
|
|
|
*h = m_h;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
Cursor
|
|
|
|
CXWindowsScreen::createBlankCursor() const
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
Cursor cursor = XCreatePixmapCursor(m_display, bitmap, bitmap,
|
|
|
|
&color, &color, 0, 0);
|
|
|
|
|
|
|
|
// don't need bitmap or the data anymore
|
|
|
|
delete[] data;
|
|
|
|
XFreePixmap(m_display, bitmap);
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::getEvent(XEvent* xevent) const
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
// wait for an event in a cancellable way and don't lock the
|
|
|
|
// display while we're waiting.
|
|
|
|
m_mutex.lock();
|
2002-05-27 16:22:59 +00:00
|
|
|
for (;;) {
|
|
|
|
while (!m_stop && XPending(m_display) == 0) {
|
|
|
|
m_mutex.unlock();
|
2002-06-02 21:35:20 +00:00
|
|
|
CThread::sleep(0.01);
|
2002-05-27 16:22:59 +00:00
|
|
|
m_mutex.lock();
|
|
|
|
}
|
|
|
|
if (m_stop) {
|
|
|
|
m_mutex.unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// get the event
|
|
|
|
XNextEvent(m_display, xevent);
|
|
|
|
|
|
|
|
// process the event. return the event if unhandled.
|
|
|
|
m_mutex.unlock();
|
|
|
|
if (!const_cast<CXWindowsScreen*>(this)->processEvent(xevent)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
m_mutex.lock();
|
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CXWindowsScreen::doStop()
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
2002-06-03 13:45:30 +00:00
|
|
|
// caller must have locked display
|
2001-11-19 00:33:36 +00:00
|
|
|
m_stop = true;
|
2001-11-11 21:15:30 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
ClipboardID
|
|
|
|
CXWindowsScreen::getClipboardID(Atom selection) const
|
2002-04-27 14:19:53 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
if (m_clipboard[id] != NULL &&
|
|
|
|
m_clipboard[id]->getSelection() == selection) {
|
2002-04-27 14:19:53 +00:00
|
|
|
return id;
|
2002-05-27 16:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-27 14:19:53 +00:00
|
|
|
return kClipboardEnd;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CXWindowsScreen::onUnexpectedClose()
|
2002-06-03 13:45:30 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::processEvent(XEvent* xevent)
|
2001-11-25 22:20:41 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
switch (xevent->type) {
|
2002-06-10 22:06:45 +00:00
|
|
|
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 subclass.
|
|
|
|
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);
|
|
|
|
onLostClipboard(id);
|
|
|
|
return true;
|
|
|
|
}
|
2002-04-27 14:19:53 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
break;
|
2001-11-25 22:20:41 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
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);
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
return true;
|
2001-11-13 23:34:12 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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(
|
2002-05-27 16:22:59 +00:00
|
|
|
xevent->xselectionrequest.owner,
|
|
|
|
xevent->xselectionrequest.requestor,
|
|
|
|
xevent->xselectionrequest.target,
|
|
|
|
xevent->xselectionrequest.time,
|
|
|
|
xevent->xselectionrequest.property);
|
2002-06-10 22:06:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2002-05-01 15:31:47 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
break;
|
2001-11-13 23:34:12 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
case PropertyNotify:
|
|
|
|
// property delete may be part of a selection conversion
|
|
|
|
if (xevent->xproperty.state == PropertyDelete) {
|
|
|
|
processClipboardRequest(xevent->xproperty.window,
|
2002-06-10 22:06:45 +00:00
|
|
|
xevent->xproperty.time,
|
|
|
|
xevent->xproperty.atom);
|
2002-05-27 16:22:59 +00:00
|
|
|
return true;
|
2001-11-13 23:34:12 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
break;
|
2002-05-01 15:31:47 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
case DestroyNotify:
|
|
|
|
// looks like one of the windows that requested a clipboard
|
|
|
|
// transfer has gone bye-bye.
|
|
|
|
destroyClipboardRequest(xevent->xdestroywindow.window);
|
|
|
|
return true;
|
2001-11-13 23:34:12 +00:00
|
|
|
}
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
return false;
|
2001-11-13 23:34:12 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::setDisplayClipboard(ClipboardID id,
|
|
|
|
const IClipboard* clipboard)
|
2001-11-13 23:34:12 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
CLock lock(&m_mutex);
|
2001-11-13 23:34:12 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// fail if we don't have the requested clipboard
|
|
|
|
if (m_clipboard[id] == NULL) {
|
2001-11-13 23:34:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// get the actual time. ICCCM does not allow CurrentTime.
|
|
|
|
Time timestamp = CXWindowsUtil::getCurrentTime(
|
|
|
|
m_display, m_clipboard[id]->getWindow());
|
2001-11-13 23:34:12 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
if (clipboard != NULL) {
|
|
|
|
// save clipboard data
|
|
|
|
return CClipboard::copy(m_clipboard[id], clipboard, timestamp);
|
2001-11-13 23:34:12 +00:00
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
else {
|
|
|
|
// assert clipboard ownership
|
|
|
|
if (!m_clipboard[id]->open(timestamp)) {
|
2001-11-13 23:34:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-05-27 16:22:59 +00:00
|
|
|
m_clipboard[id]->empty();
|
|
|
|
m_clipboard[id]->close();
|
|
|
|
return true;
|
2001-11-13 23:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::getDisplayClipboard(ClipboardID id,
|
|
|
|
IClipboard* clipboard) const
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-05-27 16:22:59 +00:00
|
|
|
assert(clipboard != NULL);
|
2001-11-25 22:20:41 +00:00
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// block others from using the display while we get the clipboard
|
2001-11-26 22:09:53 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// fail if we don't have the requested clipboard
|
|
|
|
if (m_clipboard[id] == NULL) {
|
|
|
|
return false;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// 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);
|
2001-11-25 22:20:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::processClipboardRequest(Window requestor,
|
|
|
|
Time time, Atom property)
|
2001-11-25 22:20:41 +00:00
|
|
|
{
|
2001-11-26 22:09:53 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// check every clipboard until one returns success
|
2002-04-27 14:19:53 +00:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-05-27 16:22:59 +00:00
|
|
|
if (m_clipboard[id] != NULL &&
|
|
|
|
m_clipboard[id]->processRequest(requestor, time, property)) {
|
|
|
|
break;
|
2002-04-25 10:43:53 +00:00
|
|
|
}
|
2001-11-26 22:09:53 +00:00
|
|
|
}
|
2001-11-25 22:20:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::destroyClipboardRequest(Window requestor)
|
2002-04-25 10:43:53 +00:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2002-05-27 16:22:59 +00:00
|
|
|
// check every clipboard until one returns success
|
2002-04-27 14:19:53 +00:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-05-27 16:22:59 +00:00
|
|
|
if (m_clipboard[id] != NULL &&
|
|
|
|
m_clipboard[id]->destroyRequest(requestor)) {
|
|
|
|
break;
|
2002-04-28 00:46:15 +00:00
|
|
|
}
|
2001-11-26 22:09:53 +00:00
|
|
|
}
|
2002-04-28 00:46:15 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::ioErrorHandler(Display*)
|
2002-06-03 13:45:30 +00:00
|
|
|
{
|
|
|
|
// 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->onUnexpectedClose();
|
|
|
|
log((CLOG_CRIT "quiting due to X display disconnection"));
|
2002-06-03 18:53:18 +00:00
|
|
|
exit(17);
|
2002-06-03 13:45:30 +00:00
|
|
|
}
|
|
|
|
|
2001-11-11 21:15:30 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CXWindowsScreen::CDisplayLock
|
|
|
|
//
|
|
|
|
|
2002-06-17 13:31:21 +00:00
|
|
|
CXWindowsScreen::CDisplayLock::CDisplayLock(const CXWindowsScreen* screen) :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_mutex(&screen->m_mutex),
|
|
|
|
m_display(screen->m_display)
|
2001-11-11 21:15:30 +00:00
|
|
|
{
|
|
|
|
assert(m_display != NULL);
|
|
|
|
|
|
|
|
m_mutex->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::CDisplayLock::~CDisplayLock()
|
|
|
|
{
|
|
|
|
m_mutex->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::CDisplayLock::operator Display*() const
|
|
|
|
{
|
|
|
|
return m_display;
|
|
|
|
}
|