2002-08-02 23:57:46 +04: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.
|
|
|
|
*/
|
|
|
|
|
2001-11-12 00:15:30 +03:00
|
|
|
#include "CXWindowsScreen.h"
|
2002-05-27 20:22:59 +04:00
|
|
|
#include "CXWindowsClipboard.h"
|
2002-06-22 23:20:21 +04:00
|
|
|
#include "CXWindowsScreenSaver.h"
|
2002-05-27 20:22:59 +04:00
|
|
|
#include "CXWindowsUtil.h"
|
|
|
|
#include "CClipboard.h"
|
2002-07-14 02:00:38 +04:00
|
|
|
#include "IScreenEventHandler.h"
|
|
|
|
#include "IScreenReceiver.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XScreen.h"
|
2001-11-12 00:15:30 +03:00
|
|
|
#include "CLock.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CThread.h"
|
2001-11-12 00:15:30 +03:00
|
|
|
#include "CLog.h"
|
2002-06-23 19:43:40 +04:00
|
|
|
#include "IJob.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include <cstring>
|
2002-07-14 02:00:38 +04:00
|
|
|
#if defined(X_DISPLAY_MISSING)
|
|
|
|
# error X11 is required to build synergy
|
|
|
|
#else
|
|
|
|
# include <X11/X.h>
|
|
|
|
# include <X11/Xutil.h>
|
|
|
|
#endif
|
2002-07-25 13:55:01 +04:00
|
|
|
#if UNIX_LIKE
|
2002-10-17 02:01:50 +04:00
|
|
|
# if HAVE_POLL
|
|
|
|
# include <sys/poll.h>
|
|
|
|
# else
|
|
|
|
# if HAVE_SYS_SELECT_H
|
|
|
|
# include <sys/select.h>
|
|
|
|
# endif
|
|
|
|
# if HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
# endif
|
|
|
|
# if HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
# endif
|
|
|
|
# if HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
# endif
|
|
|
|
# endif
|
2002-07-25 13:55:01 +04:00
|
|
|
#endif
|
2001-11-12 00:15:30 +03:00
|
|
|
|
2002-06-23 19:43:40 +04:00
|
|
|
//
|
|
|
|
// CXWindowsScreen::CTimer
|
|
|
|
//
|
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
CXWindowsScreen::CTimer::CTimer(IJob* job, double startTime, double resetTime) :
|
2002-06-23 19:43:40 +04:00
|
|
|
m_job(job),
|
2003-02-23 00:53:25 +03:00
|
|
|
m_timeout(resetTime),
|
|
|
|
m_time(resetTime),
|
|
|
|
m_startTime(startTime)
|
2002-06-23 19:43:40 +04:00
|
|
|
{
|
|
|
|
assert(m_timeout > 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::CTimer::~CTimer()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CXWindowsScreen::CTimer::run()
|
|
|
|
{
|
2003-02-23 00:53:25 +03:00
|
|
|
if (m_job != NULL) {
|
|
|
|
m_job->run();
|
|
|
|
}
|
2002-06-23 19:43:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CXWindowsScreen::CTimer::reset()
|
|
|
|
{
|
2003-02-23 00:53:25 +03:00
|
|
|
m_time = m_timeout;
|
|
|
|
m_startTime = 0.0;
|
2002-06-23 19:43:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::CTimer::CTimer&
|
|
|
|
CXWindowsScreen::CTimer::operator-=(double dt)
|
|
|
|
{
|
2003-02-23 00:53:25 +03:00
|
|
|
m_time -= dt - m_startTime;
|
|
|
|
m_startTime = 0.0;
|
2002-06-23 19:43:40 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::CTimer::operator double() const
|
|
|
|
{
|
|
|
|
return m_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CXWindowsScreen::CTimer::operator<(const CTimer& t) const
|
|
|
|
{
|
|
|
|
return m_time < t.m_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-12 00:15:30 +03:00
|
|
|
//
|
|
|
|
// CXWindowsScreen
|
|
|
|
//
|
|
|
|
|
2002-06-03 17:45:30 +04:00
|
|
|
CXWindowsScreen* CXWindowsScreen::s_screen = NULL;
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
CXWindowsScreen::CXWindowsScreen(IScreenReceiver* receiver,
|
|
|
|
IScreenEventHandler* eventHandler) :
|
2002-06-11 02:06:45 +04:00
|
|
|
m_display(NULL),
|
|
|
|
m_root(None),
|
2002-07-14 02:00:38 +04:00
|
|
|
m_stop(false),
|
|
|
|
m_receiver(receiver),
|
|
|
|
m_eventHandler(eventHandler),
|
|
|
|
m_window(None),
|
2002-06-19 21:03:29 +04:00
|
|
|
m_x(0), m_y(0),
|
2002-06-11 02:06:45 +04:00
|
|
|
m_w(0), m_h(0),
|
2002-07-14 02:00:38 +04:00
|
|
|
m_screensaver(NULL),
|
|
|
|
m_screensaverNotify(false),
|
2003-02-23 00:53:25 +03:00
|
|
|
m_atomScreensaver(None),
|
|
|
|
m_oneShotTimer(NULL)
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
assert(s_screen == NULL);
|
|
|
|
assert(m_receiver != NULL);
|
|
|
|
assert(m_eventHandler != NULL);
|
|
|
|
|
2002-06-03 17:45:30 +04:00
|
|
|
s_screen = this;
|
2002-07-24 17:01:18 +04:00
|
|
|
|
|
|
|
// no clipboards to start with
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
m_clipboard[id] = NULL;
|
|
|
|
}
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CXWindowsScreen::~CXWindowsScreen()
|
|
|
|
{
|
2002-06-03 17:45:30 +04:00
|
|
|
assert(s_screen != NULL);
|
2001-11-12 00:15:30 +03:00
|
|
|
assert(m_display == NULL);
|
2002-06-03 17:45:30 +04:00
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
delete m_oneShotTimer;
|
2002-06-03 17:45:30 +04:00
|
|
|
s_screen = NULL;
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
2002-06-23 19:43:40 +04:00
|
|
|
void
|
|
|
|
CXWindowsScreen::addTimer(IJob* job, double timeout)
|
|
|
|
{
|
|
|
|
CLock lock(&m_timersMutex);
|
|
|
|
removeTimerNoLock(job);
|
2003-02-23 00:53:25 +03:00
|
|
|
m_timers.push(CTimer(job, m_time.getTime(), timeout));
|
2002-06-23 19:43:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
UInt32
|
|
|
|
CXWindowsScreen::addOneShotTimer(double timeout)
|
|
|
|
{
|
|
|
|
CLock lock(&m_timersMutex);
|
|
|
|
// FIXME -- support multiple one-shot timers
|
|
|
|
m_oneShotTimer = new CTimer(NULL, m_time.getTime(), timeout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-13 00:41:23 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CXWindowsScreen::setWindow(Window window)
|
2002-07-13 00:41:23 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
assert(m_display != NULL);
|
2002-07-13 00:41:23 +04:00
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
// destroy the clipboards
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
delete m_clipboard[id];
|
|
|
|
m_clipboard[id] = NULL;
|
|
|
|
}
|
2002-07-13 00:41:23 +04:00
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
// 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);
|
2002-07-13 00:41:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
Window
|
|
|
|
CXWindowsScreen::getRoot() const
|
2002-07-13 00:41:23 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
assert(m_display != NULL);
|
|
|
|
return m_root;
|
2002-07-13 00:41:23 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
Cursor
|
|
|
|
CXWindowsScreen::getBlankCursor() const
|
2002-07-13 00:41:23 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
return m_cursor;
|
2002-07-13 00:41:23 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CXWindowsScreen::open()
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
|
|
|
assert(m_display == NULL);
|
|
|
|
|
2002-06-03 17:45:30 +04:00
|
|
|
// set the X I/O error handler so we catch the display disconnecting
|
|
|
|
XSetIOErrorHandler(&CXWindowsScreen::ioErrorHandler);
|
|
|
|
|
2002-06-09 22:03:32 +04:00
|
|
|
// get the DISPLAY
|
|
|
|
const char* display = getenv("DISPLAY");
|
|
|
|
if (display == NULL) {
|
|
|
|
display = ":0.0";
|
|
|
|
}
|
|
|
|
|
2001-11-12 00:15:30 +03:00
|
|
|
// open the display
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "XOpenDisplay(\"%s\")", display));
|
2002-06-09 22:03:32 +04:00
|
|
|
m_display = XOpenDisplay(display);
|
2002-06-11 02:06:45 +04:00
|
|
|
if (m_display == NULL) {
|
2002-07-31 16:39:34 +04:00
|
|
|
throw XScreenUnavailable(60.0);
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2001-11-12 00:15:30 +03:00
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
// get root window
|
|
|
|
m_root = DefaultRootWindow(m_display);
|
2001-11-12 00:15:30 +03:00
|
|
|
|
2002-07-11 22:58:49 +04:00
|
|
|
// create the transparent cursor
|
|
|
|
createBlankCursor();
|
2002-05-27 20:22:59 +04:00
|
|
|
|
2002-07-11 22:58:49 +04:00
|
|
|
// get screen shape
|
|
|
|
updateScreenShape();
|
2002-06-22 23:20:21 +04:00
|
|
|
|
|
|
|
// initialize the screen saver
|
2002-11-05 22:23:05 +03:00
|
|
|
m_atomScreensaver = XInternAtom(m_display, "SYNERGY_SCREENSAVER", False);
|
2002-07-14 02:00:38 +04:00
|
|
|
m_screensaver = new CXWindowsScreenSaver(this, m_display);
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
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();
|
2002-07-25 13:55:01 +04:00
|
|
|
|
|
|
|
#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.
|
2002-10-17 02:01:50 +04:00
|
|
|
#if HAVE_POLL
|
2002-07-25 13:55:01 +04:00
|
|
|
struct pollfd pfds[1];
|
|
|
|
pfds[0].fd = ConnectionNumber(m_display);
|
|
|
|
pfds[0].events = POLLIN;
|
2002-10-17 02:01:50 +04:00
|
|
|
#endif
|
2002-07-14 02:00:38 +04:00
|
|
|
while (!m_stop) {
|
2002-07-25 13:55:01 +04:00
|
|
|
// compute timeout to next timer
|
2003-02-23 00:53:25 +03:00
|
|
|
double dtimeout;
|
|
|
|
{
|
|
|
|
CLock timersLock(&m_timersMutex);
|
|
|
|
dtimeout = (m_timers.empty() ? -1.0 : m_timers.top());
|
|
|
|
if (m_oneShotTimer != NULL &&
|
|
|
|
(dtimeout == -1.0 || *m_oneShotTimer < dtimeout)) {
|
|
|
|
dtimeout = *m_oneShotTimer;
|
|
|
|
}
|
|
|
|
}
|
2002-10-17 02:01:50 +04:00
|
|
|
#if HAVE_POLL
|
2003-02-23 00:53:25 +03:00
|
|
|
int timeout = static_cast<int>(1000.0 * dtimeout);
|
2002-10-17 02:01:50 +04:00
|
|
|
#else
|
|
|
|
struct timeval timeout;
|
|
|
|
struct timeval* timeoutPtr;
|
2003-02-23 00:53:25 +03:00
|
|
|
if (dtimeout < 0.0) {
|
2002-10-17 02:01:50 +04:00
|
|
|
timeoutPtr = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2003-02-23 00:53:25 +03:00
|
|
|
timeout.tv_sec = static_cast<int>(dtimeout);
|
2002-10-17 02:01:50 +04:00
|
|
|
timeout.tv_usec = static_cast<int>(1.0e+6 *
|
2003-02-23 00:53:25 +03:00
|
|
|
(dtimeout - timeout.tv_sec));
|
2002-10-17 02:01:50 +04:00
|
|
|
timeoutPtr = &timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize file descriptor sets
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(ConnectionNumber(m_display), &rfds);
|
|
|
|
#endif
|
2002-07-25 13:55:01 +04:00
|
|
|
|
|
|
|
// 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();
|
2002-10-18 01:37:41 +04:00
|
|
|
CThread::testCancel();
|
2002-10-17 02:01:50 +04:00
|
|
|
#if HAVE_POLL
|
2002-07-25 13:55:01 +04:00
|
|
|
poll(pfds, 1, timeout);
|
2002-10-17 02:01:50 +04:00
|
|
|
#else
|
|
|
|
select(ConnectionNumber(m_display) + 1,
|
|
|
|
SELECT_TYPE_ARG234 &rfds,
|
|
|
|
SELECT_TYPE_ARG234 NULL,
|
|
|
|
SELECT_TYPE_ARG234 NULL,
|
|
|
|
SELECT_TYPE_ARG5 timeoutPtr);
|
|
|
|
#endif
|
2002-07-25 13:55:01 +04:00
|
|
|
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
|
2002-07-14 02:00:38 +04:00
|
|
|
while (!m_stop && XPending(m_display) == 0) {
|
|
|
|
// check timers
|
|
|
|
if (processTimers()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait
|
|
|
|
m_mutex.unlock();
|
|
|
|
CThread::sleep(0.01);
|
|
|
|
m_mutex.lock();
|
|
|
|
}
|
2002-07-25 13:55:01 +04:00
|
|
|
|
|
|
|
// process events
|
|
|
|
while (!m_stop && XPending(m_display) > 0) {
|
2002-07-14 02:00:38 +04:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
2002-07-25 13:55:01 +04:00
|
|
|
|
|
|
|
#endif // !UNIX_LIKE
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CXWindowsScreen::exitMainLoop()
|
|
|
|
{
|
2002-10-18 01:37:41 +04:00
|
|
|
// m_stop should be a condition variable that we signal here
|
|
|
|
// but we can't wait on both the condition variable and the
|
|
|
|
// X connection so there's no point. however, we do need
|
|
|
|
// to wake up the X connection so send ourself some event.
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_stop = true;
|
2002-10-18 01:37:41 +04:00
|
|
|
|
|
|
|
if (m_display != NULL && m_window != None) {
|
|
|
|
XEvent event;
|
|
|
|
event.xclient.type = ClientMessage;
|
|
|
|
event.xclient.display = m_display;
|
|
|
|
event.xclient.window = m_window;
|
|
|
|
event.xclient.message_type = XInternAtom(m_display, "ATOM", False);
|
|
|
|
event.xclient.format = 32;
|
|
|
|
event.xclient.data.l[0] = 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_window, False, 0, &event);
|
|
|
|
}
|
2002-07-14 02:00:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CXWindowsScreen::close()
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
2002-06-03 17:45:30 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-11-12 00:15:30 +03:00
|
|
|
|
2002-06-22 23:20:21 +04:00
|
|
|
// done with screen saver
|
2002-07-14 02:00:38 +04:00
|
|
|
delete m_screensaver;
|
|
|
|
m_screensaver = NULL;
|
2002-06-22 23:20:21 +04:00
|
|
|
|
2002-05-27 20:22:59 +04:00
|
|
|
// destroy clipboards
|
2002-04-27 18:19:53 +04:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-05-27 20:22:59 +04:00
|
|
|
delete m_clipboard[id];
|
2002-06-22 23:20:21 +04:00
|
|
|
m_clipboard[id] = NULL;
|
2001-11-27 01:09:53 +03:00
|
|
|
}
|
|
|
|
|
2001-11-12 00:15:30 +03:00
|
|
|
// close the display
|
2002-06-03 17:45:30 +04:00
|
|
|
if (m_display != NULL) {
|
|
|
|
XCloseDisplay(m_display);
|
|
|
|
m_display = NULL;
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "closed display"));
|
2002-06-03 17:45:30 +04:00
|
|
|
}
|
|
|
|
XSetIOErrorHandler(NULL);
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
bool
|
|
|
|
CXWindowsScreen::setClipboard(ClipboardID id, const IClipboard* clipboard)
|
2002-07-13 00:41:23 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
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;
|
|
|
|
}
|
2002-07-13 00:41:23 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
|
|
|
CXWindowsScreen::checkClipboards()
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
// do nothing, we're always up to date
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
2002-07-15 19:01:36 +04:00
|
|
|
CXWindowsScreen::openScreensaver(bool notify)
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
assert(m_screensaver != NULL);
|
|
|
|
|
|
|
|
m_screensaverNotify = notify;
|
|
|
|
if (m_screensaverNotify) {
|
|
|
|
m_screensaver->setNotify(m_window);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_screensaver->disable();
|
|
|
|
}
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-15 19:01:36 +04:00
|
|
|
CXWindowsScreen::closeScreensaver()
|
2002-07-11 22:58:49 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (m_screensaver != NULL) {
|
|
|
|
if (m_screensaverNotify) {
|
|
|
|
m_screensaver->setNotify(None);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_screensaver->enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-07-11 22:58:49 +04:00
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
|
|
|
CXWindowsScreen::screensaver(bool activate)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
assert(m_screensaver != NULL);
|
|
|
|
|
|
|
|
if (activate) {
|
|
|
|
m_screensaver->activate();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_screensaver->deactivate();
|
2002-07-11 22:58:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CXWindowsScreen::syncDesktop()
|
2002-07-11 22:58:49 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
// 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);
|
2002-07-11 22:58:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CXWindowsScreen::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-11-12 00:15:30 +03:00
|
|
|
assert(m_display != NULL);
|
|
|
|
|
2002-06-19 21:03:29 +04:00
|
|
|
x = m_x;
|
|
|
|
y = m_y;
|
|
|
|
w = m_w;
|
|
|
|
h = m_h;
|
2001-11-12 00:15:30 +03:00
|
|
|
}
|
|
|
|
|
2002-07-11 22:58:49 +04:00
|
|
|
void
|
|
|
|
CXWindowsScreen::getCursorPos(SInt32& x, SInt32& y) const
|
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-11 22:58:49 +04:00
|
|
|
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 {
|
2003-03-13 22:20:55 +03:00
|
|
|
x = m_x + (m_w >> 1);
|
|
|
|
y = m_y + (m_h >> 1);
|
2002-07-11 22:58:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CXWindowsScreen::getCursorCenter(SInt32& x, SInt32& y) const
|
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-11 22:58:49 +04:00
|
|
|
assert(m_display != NULL);
|
|
|
|
|
|
|
|
x = m_x + (m_w >> 1);
|
|
|
|
y = m_y + (m_h >> 1);
|
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
|
|
|
CXWindowsScreen::updateScreenShape()
|
|
|
|
{
|
|
|
|
m_x = 0;
|
|
|
|
m_y = 0;
|
|
|
|
m_w = WidthOfScreen(DefaultScreenOfDisplay(m_display));
|
|
|
|
m_h = HeightOfScreen(DefaultScreenOfDisplay(m_display));
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "screen shape: %d,%d %dx%d", m_x, m_y, m_w, m_h));
|
2002-07-14 02:00:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "lost clipboard %d ownership at time %d", id, xevent->xselectionclear.time));
|
2002-07-14 02:00:38 +04:00
|
|
|
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:
|
2002-11-05 22:23:05 +03:00
|
|
|
if (xevent->xclient.message_type == m_atomScreensaver &&
|
2002-07-14 02:00:38 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-07-11 22:58:49 +04:00
|
|
|
void
|
|
|
|
CXWindowsScreen::createBlankCursor()
|
2001-11-12 00:15:30 +03: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
|
2002-07-11 22:58:49 +04:00
|
|
|
m_cursor = XCreatePixmapCursor(m_display, bitmap, bitmap,
|
2001-11-12 00:15:30 +03:00
|
|
|
&color, &color, 0, 0);
|
|
|
|
|
|
|
|
// don't need bitmap or the data anymore
|
|
|
|
delete[] data;
|
|
|
|
XFreePixmap(m_display, bitmap);
|
2002-07-11 22:58:49 +04:00
|
|
|
}
|
2001-11-12 00:15:30 +03:00
|
|
|
|
2002-06-23 19:43:40 +04:00
|
|
|
bool
|
|
|
|
CXWindowsScreen::processTimers()
|
|
|
|
{
|
2003-02-23 00:53:25 +03:00
|
|
|
bool oneShot = false;
|
2002-06-23 19:43:40 +04:00
|
|
|
std::vector<IJob*> jobs;
|
|
|
|
{
|
|
|
|
CLock lock(&m_timersMutex);
|
|
|
|
|
|
|
|
// get current time
|
|
|
|
const double time = m_time.getTime();
|
|
|
|
|
|
|
|
// done if no timers have expired
|
2003-02-23 00:53:25 +03:00
|
|
|
if ((m_oneShotTimer == NULL || *m_oneShotTimer > time) &&
|
|
|
|
(m_timers.empty() || m_timers.top() > time)) {
|
2002-06-23 19:43:40 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
// handle one shot timers
|
|
|
|
if (m_oneShotTimer != NULL) {
|
|
|
|
*m_oneShotTimer -= time;
|
|
|
|
if (*m_oneShotTimer <= 0.0) {
|
|
|
|
delete m_oneShotTimer;
|
|
|
|
m_oneShotTimer = NULL;
|
|
|
|
oneShot = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-23 19:43:40 +04:00
|
|
|
// 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
|
2003-02-23 00:53:25 +03:00
|
|
|
if (!m_timers.empty()) {
|
|
|
|
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);
|
|
|
|
}
|
2002-06-23 19:43:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset the clock
|
|
|
|
m_time.reset();
|
|
|
|
}
|
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
// now notify of the one shot timers
|
|
|
|
if (oneShot) {
|
|
|
|
m_mutex.unlock();
|
|
|
|
m_eventHandler->onOneShotTimerExpired(0);
|
|
|
|
m_mutex.lock();
|
|
|
|
}
|
|
|
|
|
2002-06-23 19:43:40 +04:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
ClipboardID
|
|
|
|
CXWindowsScreen::getClipboardID(Atom selection) const
|
2001-11-14 02:34:12 +03:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
if (m_clipboard[id] != NULL &&
|
|
|
|
m_clipboard[id]->getSelection() == selection) {
|
|
|
|
return id;
|
2001-11-14 02:34:12 +03:00
|
|
|
}
|
|
|
|
}
|
2002-07-14 02:00:38 +04:00
|
|
|
return kClipboardEnd;
|
2001-11-26 01:20:41 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsScreen::processClipboardRequest(Window requestor,
|
|
|
|
Time time, Atom property)
|
2001-11-26 01:20:41 +03:00
|
|
|
{
|
2001-11-27 01:09:53 +03:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2002-05-27 20:22:59 +04:00
|
|
|
// check every clipboard until one returns success
|
2002-04-27 18:19:53 +04:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-05-27 20:22:59 +04:00
|
|
|
if (m_clipboard[id] != NULL &&
|
|
|
|
m_clipboard[id]->processRequest(requestor, time, property)) {
|
|
|
|
break;
|
2002-04-25 14:43:53 +04:00
|
|
|
}
|
2001-11-27 01:09:53 +03:00
|
|
|
}
|
2001-11-26 01:20:41 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsScreen::destroyClipboardRequest(Window requestor)
|
2002-04-25 14:43:53 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2002-05-27 20:22:59 +04:00
|
|
|
// check every clipboard until one returns success
|
2002-04-27 18:19:53 +04:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-05-27 20:22:59 +04:00
|
|
|
if (m_clipboard[id] != NULL &&
|
|
|
|
m_clipboard[id]->destroyRequest(requestor)) {
|
|
|
|
break;
|
2002-04-28 04:46:15 +04:00
|
|
|
}
|
2001-11-27 01:09:53 +03:00
|
|
|
}
|
2002-04-28 04:46:15 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
int
|
2002-06-17 17:31:21 +04:00
|
|
|
CXWindowsScreen::ioErrorHandler(Display*)
|
2002-06-03 17:45:30 +04: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.
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "X display has unexpectedly disconnected"));
|
2002-06-03 17:45:30 +04:00
|
|
|
s_screen->m_display = NULL;
|
2002-07-16 20:52:26 +04:00
|
|
|
s_screen->m_receiver->onError();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_CRIT "quiting due to X display disconnection"));
|
2002-06-03 22:53:18 +04:00
|
|
|
exit(17);
|
2002-06-03 17:45:30 +04:00
|
|
|
}
|
|
|
|
|
2001-11-12 00:15:30 +03:00
|
|
|
|
|
|
|
//
|
2002-07-14 02:00:38 +04:00
|
|
|
// CDisplayLock
|
2001-11-12 00:15:30 +03:00
|
|
|
//
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
CDisplayLock::CDisplayLock(const CXWindowsScreen* screen) :
|
2002-06-11 02:06:45 +04:00
|
|
|
m_mutex(&screen->m_mutex),
|
|
|
|
m_display(screen->m_display)
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
2002-07-24 17:01:18 +04:00
|
|
|
// 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.
|
2001-11-12 00:15:30 +03:00
|
|
|
|
|
|
|
m_mutex->lock();
|
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
CDisplayLock::~CDisplayLock()
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
|
|
|
m_mutex->unlock();
|
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
CDisplayLock::operator Display*() const
|
2001-11-12 00:15:30 +03:00
|
|
|
{
|
|
|
|
return m_display;
|
|
|
|
}
|