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-10-06 18:13:28 +04:00
|
|
|
#include "CServer.h"
|
2002-05-30 20:13:16 +04:00
|
|
|
#include "CHTTPServer.h"
|
2002-07-30 22:31:00 +04:00
|
|
|
#include "CPrimaryClient.h"
|
|
|
|
#include "IPrimaryScreenFactory.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CInputPacketStream.h"
|
|
|
|
#include "COutputPacketStream.h"
|
|
|
|
#include "CProtocolUtil.h"
|
2002-07-10 01:22:31 +04:00
|
|
|
#include "CClientProxy1_0.h"
|
2002-12-23 16:55:21 +03:00
|
|
|
#include "OptionTypes.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "ProtocolTypes.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XScreen.h"
|
|
|
|
#include "XSynergy.h"
|
2002-07-30 22:31:00 +04:00
|
|
|
#include "CTCPListenSocket.h"
|
2002-06-17 16:02:26 +04:00
|
|
|
#include "IDataSocket.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "ISocketFactory.h"
|
|
|
|
#include "XSocket.h"
|
2002-07-30 22:31:00 +04:00
|
|
|
#include "IStreamFilterFactory.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CLock.h"
|
|
|
|
#include "CThread.h"
|
|
|
|
#include "CTimerThread.h"
|
2002-10-16 02:01:41 +04:00
|
|
|
#include "XMT.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XThread.h"
|
2002-05-30 20:13:16 +04:00
|
|
|
#include "CFunctionJob.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CLog.h"
|
|
|
|
#include "CStopwatch.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "TMethodJob.h"
|
2003-01-05 01:01:32 +03:00
|
|
|
#include "CArch.h"
|
2001-11-19 03:33:36 +03:00
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
//
|
|
|
|
// CServer
|
|
|
|
//
|
|
|
|
|
2002-06-02 15:49:46 +04:00
|
|
|
const SInt32 CServer::s_httpMaxSimultaneousRequests = 3;
|
|
|
|
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::CServer(const CString& serverName) :
|
2002-06-11 02:06:45 +04:00
|
|
|
m_name(serverName),
|
2002-10-18 01:37:31 +04:00
|
|
|
m_error(false),
|
2002-07-18 21:00:48 +04:00
|
|
|
m_bindTimeout(5.0 * 60.0),
|
2002-07-30 22:31:00 +04:00
|
|
|
m_screenFactory(NULL),
|
2002-07-18 21:00:48 +04:00
|
|
|
m_socketFactory(NULL),
|
2002-07-30 22:31:00 +04:00
|
|
|
m_streamFilterFactory(NULL),
|
2002-07-18 21:00:48 +04:00
|
|
|
m_acceptClientThread(NULL),
|
2002-06-11 02:06:45 +04:00
|
|
|
m_active(NULL),
|
2002-07-10 01:22:31 +04:00
|
|
|
m_primaryClient(NULL),
|
2002-06-11 02:06:45 +04:00
|
|
|
m_seqNum(0),
|
2002-06-26 20:31:48 +04:00
|
|
|
m_activeSaver(NULL),
|
2002-06-11 02:06:45 +04:00
|
|
|
m_httpServer(NULL),
|
2003-02-23 00:53:25 +03:00
|
|
|
m_httpAvailable(&m_mutex, s_httpMaxSimultaneousRequests),
|
2003-02-23 22:29:08 +03:00
|
|
|
m_switchDir(kNoDirection),
|
|
|
|
m_switchScreen(NULL),
|
2003-02-23 00:53:25 +03:00
|
|
|
m_switchWaitDelay(0.0),
|
2003-02-23 22:29:08 +03:00
|
|
|
m_switchWaitEngaged(false),
|
|
|
|
m_switchTwoTapDelay(0.0),
|
|
|
|
m_switchTwoTapEngaged(false),
|
2003-03-13 01:34:07 +03:00
|
|
|
m_switchTwoTapArmed(false),
|
|
|
|
m_status(kNotRunning)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-18 21:00:48 +04:00
|
|
|
// do nothing
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CServer::~CServer()
|
|
|
|
{
|
2002-07-30 22:31:00 +04:00
|
|
|
delete m_screenFactory;
|
|
|
|
delete m_socketFactory;
|
|
|
|
delete m_streamFilterFactory;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-07-31 16:39:34 +04:00
|
|
|
void
|
2002-06-21 21:55:47 +04:00
|
|
|
CServer::open()
|
|
|
|
{
|
|
|
|
// open the screen
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "opening screen"));
|
2002-06-21 21:55:47 +04:00
|
|
|
openPrimaryScreen();
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kNotRunning);
|
2002-06-21 21:55:47 +04:00
|
|
|
}
|
2003-03-13 01:34:07 +03:00
|
|
|
catch (XScreen& e) {
|
2002-07-31 16:39:34 +04:00
|
|
|
// can't open screen
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError, e.what());
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "failed to open screen"));
|
2002-07-31 16:39:34 +04:00
|
|
|
throw;
|
2002-06-21 21:55:47 +04:00
|
|
|
}
|
|
|
|
catch (XUnknownClient& e) {
|
2002-07-31 16:39:34 +04:00
|
|
|
// can't open screen
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kServerNameUnknown);
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_CRIT "unknown screen name `%s'", e.getName().c_str()));
|
2002-07-31 16:39:34 +04:00
|
|
|
throw;
|
2002-06-21 21:55:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-30 19:17:44 +04:00
|
|
|
CServer::mainLoop()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-21 21:55:47 +04:00
|
|
|
// check preconditions
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 01:22:31 +04:00
|
|
|
assert(m_primaryClient != NULL);
|
2002-06-21 21:55:47 +04:00
|
|
|
}
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
try {
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kNotRunning);
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "starting server"));
|
2001-10-14 18:37:41 +04:00
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// start listening for new clients
|
2002-07-18 21:00:48 +04:00
|
|
|
m_acceptClientThread = new CThread(startThread(
|
|
|
|
new TMethodJob<CServer>(this,
|
|
|
|
&CServer::acceptClients)));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-06-09 20:53:25 +04:00
|
|
|
// start listening for HTTP requests
|
|
|
|
if (m_config.getHTTPAddress().isValid()) {
|
|
|
|
m_httpServer = new CHTTPServer(this);
|
2002-06-21 19:18:01 +04:00
|
|
|
startThread(new TMethodJob<CServer>(this,
|
|
|
|
&CServer::acceptHTTPClients));
|
2002-06-09 20:53:25 +04:00
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// handle events
|
2002-07-30 19:17:44 +04:00
|
|
|
m_primaryClient->mainLoop();
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// clean up
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping server"));
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// use a macro to write the stuff that should go into a finally
|
|
|
|
// block so we can repeat it easily. stroustrup's view that
|
|
|
|
// "resource acquistion is initialization" is a better solution
|
|
|
|
// than a finally block is parochial. they both have their
|
|
|
|
// place. adding finally to C++ would've been a drop in a big
|
|
|
|
// bucket.
|
|
|
|
#define FINALLY do { \
|
|
|
|
stopThreads(); \
|
|
|
|
delete m_httpServer; \
|
|
|
|
m_httpServer = NULL; \
|
2003-03-13 01:34:07 +03:00
|
|
|
runStatusJobs(); \
|
2002-07-10 01:22:31 +04:00
|
|
|
} while (false)
|
|
|
|
FINALLY;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-10-16 02:01:41 +04:00
|
|
|
catch (XMT& e) {
|
|
|
|
LOG((CLOG_ERR "server error: %s", e.what()));
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError, e.what());
|
2002-10-16 02:01:41 +04:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
LOG((CLOG_NOTE "stopping server"));
|
|
|
|
FINALLY;
|
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
catch (XBase& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "server error: %s", e.what()));
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError, e.what());
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// clean up
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping server"));
|
2002-07-10 01:22:31 +04:00
|
|
|
FINALLY;
|
2002-06-02 22:49:35 +04:00
|
|
|
}
|
|
|
|
catch (XThread&) {
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kNotRunning);
|
|
|
|
|
2002-06-02 22:49:35 +04:00
|
|
|
// clean up
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping server"));
|
2002-07-10 01:22:31 +04:00
|
|
|
FINALLY;
|
2002-06-02 22:49:35 +04:00
|
|
|
throw;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "unknown server error"));
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError);
|
2001-10-14 18:37:41 +04:00
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// clean up
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping server"));
|
2002-07-10 01:22:31 +04:00
|
|
|
FINALLY;
|
2001-10-06 18:13:28 +04:00
|
|
|
throw;
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
#undef FINALLY
|
2002-10-18 01:37:31 +04:00
|
|
|
|
|
|
|
// throw if there was an error
|
|
|
|
if (m_error) {
|
|
|
|
LOG((CLOG_DEBUG "forwarding child thread exception"));
|
|
|
|
throw XServerRethrow();
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-30 19:17:44 +04:00
|
|
|
CServer::exitMainLoop()
|
2002-04-29 17:31:44 +04:00
|
|
|
{
|
2002-07-30 19:17:44 +04:00
|
|
|
m_primaryClient->exitMainLoop();
|
2002-06-03 17:45:30 +04:00
|
|
|
}
|
|
|
|
|
2002-10-18 01:37:31 +04:00
|
|
|
void
|
|
|
|
CServer::exitMainLoopWithError()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_error = true;
|
|
|
|
}
|
|
|
|
exitMainLoop();
|
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
|
|
|
CServer::close()
|
|
|
|
{
|
|
|
|
if (m_primaryClient != NULL) {
|
|
|
|
closePrimaryScreen();
|
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "closed screen"));
|
2002-07-14 02:00:38 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::setConfig(const CConfig& config)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-10 01:22:31 +04:00
|
|
|
// refuse configuration if it doesn't include the primary screen
|
2002-05-31 22:09:43 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 01:22:31 +04:00
|
|
|
if (m_primaryClient != NULL &&
|
|
|
|
!config.isScreen(m_primaryClient->getName())) {
|
2002-05-31 22:09:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
2002-06-09 03:24:40 +04:00
|
|
|
}
|
2002-06-01 14:52:02 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// close clients that are connected but being dropped from the
|
|
|
|
// configuration.
|
|
|
|
closeClients(config);
|
2002-06-21 19:18:01 +04:00
|
|
|
|
2002-06-09 03:24:40 +04:00
|
|
|
// cut over
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_config = config;
|
|
|
|
|
2003-02-22 19:20:23 +03:00
|
|
|
// process global options
|
|
|
|
const CConfig::CScreenOptions* options = m_config.getOptions("");
|
|
|
|
if (options != NULL && options->size() > 0) {
|
|
|
|
for (CConfig::CScreenOptions::const_iterator index = options->begin();
|
|
|
|
index != options->end(); ++index) {
|
|
|
|
const OptionID id = index->first;
|
|
|
|
const OptionValue value = index->second;
|
2003-02-23 00:53:25 +03:00
|
|
|
if (id == kOptionScreenSwitchDelay) {
|
|
|
|
m_switchWaitDelay = 1.0e-3 * static_cast<double>(value);
|
|
|
|
if (m_switchWaitDelay < 0.0) {
|
|
|
|
m_switchWaitDelay = 0.0;
|
|
|
|
}
|
2003-02-23 22:29:08 +03:00
|
|
|
m_switchWaitEngaged = false;
|
|
|
|
}
|
|
|
|
else if (id == kOptionScreenSwitchTwoTap) {
|
|
|
|
m_switchTwoTapDelay = 1.0e-3 * static_cast<double>(value);
|
|
|
|
if (m_switchTwoTapDelay < 0.0) {
|
|
|
|
m_switchTwoTapDelay = 0.0;
|
|
|
|
}
|
|
|
|
m_switchTwoTapEngaged = false;
|
2003-02-23 00:53:25 +03:00
|
|
|
}
|
2003-02-22 19:20:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-09 03:24:40 +04:00
|
|
|
// tell primary screen about reconfiguration
|
2002-07-10 01:22:31 +04:00
|
|
|
if (m_primaryClient != NULL) {
|
2002-07-11 17:13:37 +04:00
|
|
|
m_primaryClient->reconfigure(getActivePrimarySides());
|
2002-06-09 03:24:40 +04:00
|
|
|
}
|
|
|
|
|
2003-02-22 19:20:23 +03:00
|
|
|
// tell all (connected) clients about current options
|
|
|
|
for (CClientList::const_iterator index = m_clients.begin();
|
|
|
|
index != m_clients.end(); ++index) {
|
|
|
|
IClient* client = index->second;
|
|
|
|
sendOptions(client);
|
|
|
|
}
|
2002-12-23 16:55:21 +03:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// notify of status
|
|
|
|
runStatusJobs();
|
|
|
|
|
2002-05-31 22:09:43 +04:00
|
|
|
return true;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-07-30 22:31:00 +04:00
|
|
|
void
|
|
|
|
CServer::setScreenFactory(IPrimaryScreenFactory* adopted)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
delete m_screenFactory;
|
|
|
|
m_screenFactory = adopted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::setSocketFactory(ISocketFactory* adopted)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
delete m_socketFactory;
|
|
|
|
m_socketFactory = adopted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::setStreamFilterFactory(IStreamFilterFactory* adopted)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
delete m_streamFilterFactory;
|
|
|
|
m_streamFilterFactory = adopted;
|
|
|
|
}
|
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
void
|
|
|
|
CServer::addStatusJob(IJob* job)
|
|
|
|
{
|
|
|
|
m_statusJobs.addJob(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::removeStatusJob(IJob* job)
|
|
|
|
{
|
|
|
|
m_statusJobs.removeJob(job);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
|
|
|
CServer::getPrimaryScreenName() const
|
2002-05-30 20:13:16 +04:00
|
|
|
{
|
2002-06-09 21:59:32 +04:00
|
|
|
return m_name;
|
2002-05-30 20:13:16 +04:00
|
|
|
}
|
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
UInt32
|
|
|
|
CServer::getNumClients() const
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
return m_clients.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::getClients(std::vector<CString>& list) const
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
list.clear();
|
|
|
|
for (CClientList::const_iterator index = m_clients.begin();
|
|
|
|
index != m_clients.end(); ++index) {
|
|
|
|
list.push_back(index->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CServer::EStatus
|
|
|
|
CServer::getStatus(CString* msg) const
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (msg != NULL) {
|
|
|
|
*msg = m_statusMessage;
|
|
|
|
}
|
|
|
|
return m_status;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::getConfig(CConfig* config) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-05-31 18:43:23 +04:00
|
|
|
assert(config != NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
CLock lock(&m_mutex);
|
2002-05-31 18:43:23 +04:00
|
|
|
*config = m_config;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
void
|
|
|
|
CServer::runStatusJobs() const
|
|
|
|
{
|
|
|
|
m_statusJobs.runJobs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::setStatus(EStatus status, const char* msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_status = status;
|
|
|
|
if (m_status == kError) {
|
|
|
|
m_statusMessage = (msg == NULL) ? "Error" : msg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_statusMessage = (msg == NULL) ? "" : msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runStatusJobs();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
UInt32
|
|
|
|
CServer::getActivePrimarySides() const
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2002-07-11 17:13:37 +04:00
|
|
|
// note -- m_mutex must be locked on entry
|
2001-11-19 03:33:36 +03:00
|
|
|
UInt32 sides = 0;
|
2002-07-15 19:01:36 +04:00
|
|
|
if (!m_config.getNeighbor(getPrimaryScreenName(), kLeft).empty()) {
|
|
|
|
sides |= kLeftMask;
|
2002-05-31 22:09:43 +04:00
|
|
|
}
|
2002-07-15 19:01:36 +04:00
|
|
|
if (!m_config.getNeighbor(getPrimaryScreenName(), kRight).empty()) {
|
|
|
|
sides |= kRightMask;
|
2002-05-31 22:09:43 +04:00
|
|
|
}
|
2002-07-15 19:01:36 +04:00
|
|
|
if (!m_config.getNeighbor(getPrimaryScreenName(), kTop).empty()) {
|
|
|
|
sides |= kTopMask;
|
2002-05-31 22:09:43 +04:00
|
|
|
}
|
2002-07-15 19:01:36 +04:00
|
|
|
if (!m_config.getNeighbor(getPrimaryScreenName(), kBottom).empty()) {
|
|
|
|
sides |= kBottomMask;
|
2002-05-31 22:09:43 +04:00
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
return sides;
|
|
|
|
}
|
|
|
|
|
2002-07-16 20:52:26 +04:00
|
|
|
void
|
|
|
|
CServer::onError()
|
|
|
|
{
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError);
|
|
|
|
|
2002-07-16 20:52:26 +04:00
|
|
|
// stop all running threads but don't wait too long since some
|
|
|
|
// threads may be unable to proceed until this thread returns.
|
|
|
|
stopThreads(3.0);
|
|
|
|
|
|
|
|
// done with the HTTP server
|
2002-07-18 21:00:48 +04:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-16 20:52:26 +04:00
|
|
|
delete m_httpServer;
|
|
|
|
m_httpServer = NULL;
|
|
|
|
|
|
|
|
// note -- we do not attempt to close down the primary screen
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-10 18:15:17 +04:00
|
|
|
CServer::onInfoChanged(const CString& name, const CClientInfo& info)
|
2002-06-01 14:52:02 +04:00
|
|
|
{
|
2002-07-10 01:22:31 +04:00
|
|
|
CLock lock(&m_mutex);
|
2002-04-29 18:25:24 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// look up client
|
|
|
|
CClientList::iterator index = m_clients.find(name);
|
|
|
|
if (index == m_clients.end()) {
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XBadClient();
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
IClient* client = index->second;
|
|
|
|
assert(client != NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// update the remote mouse coordinates
|
|
|
|
if (client == m_active) {
|
2002-07-10 18:15:17 +04:00
|
|
|
m_x = info.m_mx;
|
|
|
|
m_y = info.m_my;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "screen \"%s\" shape=%d,%d %dx%d zone=%d pos=%d,%d", name.c_str(), info.m_x, info.m_y, info.m_w, info.m_h, info.m_zoneSize, info.m_mx, info.m_my));
|
2002-05-24 21:54:28 +04:00
|
|
|
|
|
|
|
// handle resolution change to primary screen
|
2002-07-10 01:22:31 +04:00
|
|
|
if (client == m_primaryClient) {
|
|
|
|
if (client == m_active) {
|
|
|
|
onMouseMovePrimaryNoLock(m_x, m_y);
|
2002-05-24 21:54:28 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-06-01 14:52:02 +04:00
|
|
|
onMouseMoveSecondaryNoLock(0, 0);
|
2002-05-24 21:54:28 +04:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
bool
|
2002-07-10 18:15:17 +04:00
|
|
|
CServer::onGrabClipboard(const CString& name, ClipboardID id, UInt32 seqNum)
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// screen must be connected
|
2002-07-10 01:22:31 +04:00
|
|
|
CClientList::iterator grabber = m_clients.find(name);
|
|
|
|
if (grabber == m_clients.end()) {
|
2001-11-25 21:32:41 +03:00
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// ignore grab if sequence number is old. always allow primary
|
|
|
|
// screen to grab.
|
2002-07-10 18:15:17 +04:00
|
|
|
CClipboardInfo& clipboard = m_clipboards[id];
|
2002-07-10 01:22:31 +04:00
|
|
|
if (name != m_primaryClient->getName() &&
|
2002-04-29 17:31:44 +04:00
|
|
|
seqNum < clipboard.m_clipboardSeqNum) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "ignored screen \"%s\" grab of clipboard %d", name.c_str(), id));
|
2002-07-10 01:22:31 +04:00
|
|
|
return false;
|
2002-04-29 17:31:44 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// mark screen as owning clipboard
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "screen \"%s\" grabbed clipboard %d from \"%s\"", name.c_str(), id, clipboard.m_clipboardOwner.c_str()));
|
2002-07-10 01:22:31 +04:00
|
|
|
clipboard.m_clipboardOwner = name;
|
2002-04-29 17:31:44 +04:00
|
|
|
clipboard.m_clipboardSeqNum = seqNum;
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// clear the clipboard data (since it's not known at this point)
|
|
|
|
if (clipboard.m_clipboard.open(0)) {
|
|
|
|
clipboard.m_clipboard.empty();
|
|
|
|
clipboard.m_clipboard.close();
|
2002-04-29 17:31:44 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
clipboard.m_clipboardData = clipboard.m_clipboard.marshall();
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// tell all other screens to take ownership of clipboard. tell the
|
|
|
|
// grabber that it's clipboard isn't dirty.
|
|
|
|
for (CClientList::iterator index = m_clients.begin();
|
|
|
|
index != m_clients.end(); ++index) {
|
|
|
|
IClient* client = index->second;
|
|
|
|
if (index == grabber) {
|
|
|
|
client->setClipboardDirty(id, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
client->grabClipboard(id);
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
return true;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-10 18:15:17 +04:00
|
|
|
CServer::onClipboardChanged(ClipboardID id, UInt32 seqNum, const CString& data)
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 01:22:31 +04:00
|
|
|
onClipboardChangedNoLock(id, seqNum, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::onClipboardChangedNoLock(ClipboardID id,
|
|
|
|
UInt32 seqNum, const CString& data)
|
|
|
|
{
|
2002-04-29 17:31:44 +04:00
|
|
|
CClipboardInfo& clipboard = m_clipboards[id];
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// ignore update if sequence number is old
|
|
|
|
if (seqNum < clipboard.m_clipboardSeqNum) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "ignored screen \"%s\" update of clipboard %d (missequenced)", clipboard.m_clipboardOwner.c_str(), id));
|
2002-07-10 01:22:31 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if data hasn't changed
|
|
|
|
if (data == clipboard.m_clipboardData) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "ignored screen \"%s\" update of clipboard %d (unchanged)", clipboard.m_clipboardOwner.c_str(), id));
|
2002-04-29 17:31:44 +04:00
|
|
|
return;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-04-29 17:31:44 +04:00
|
|
|
|
|
|
|
// unmarshall into our clipboard buffer
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "screen \"%s\" updated clipboard %d", clipboard.m_clipboardOwner.c_str(), id));
|
2002-07-10 01:22:31 +04:00
|
|
|
clipboard.m_clipboardData = data;
|
2002-04-29 17:31:44 +04:00
|
|
|
clipboard.m_clipboard.unmarshall(clipboard.m_clipboardData, 0);
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// tell all clients except the sender that the clipboard is dirty
|
|
|
|
CClientList::const_iterator sender =
|
|
|
|
m_clients.find(clipboard.m_clipboardOwner);
|
|
|
|
for (CClientList::const_iterator index = m_clients.begin();
|
|
|
|
index != m_clients.end(); ++index) {
|
|
|
|
IClient* client = index->second;
|
|
|
|
client->setClipboardDirty(id, index != sender);
|
2002-04-29 17:31:44 +04:00
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// send the new clipboard to the active screen
|
|
|
|
m_active->setClipboard(id, m_clipboards[id].m_clipboardData);
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
|
|
|
CServer::onScreensaver(bool activated)
|
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "onScreenSaver %s", activated ? "activated" : "deactivated"));
|
2002-07-14 02:00:38 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
if (activated) {
|
|
|
|
// save current screen and position
|
|
|
|
m_activeSaver = m_active;
|
|
|
|
m_xSaver = m_x;
|
|
|
|
m_ySaver = m_y;
|
|
|
|
|
|
|
|
// jump to primary screen
|
|
|
|
if (m_active != m_primaryClient) {
|
|
|
|
switchScreen(m_primaryClient, 0, 0, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// jump back to previous screen and position. we must check
|
|
|
|
// that the position is still valid since the screen may have
|
|
|
|
// changed resolutions while the screen saver was running.
|
|
|
|
if (m_activeSaver != NULL && m_activeSaver != m_primaryClient) {
|
|
|
|
// check position
|
|
|
|
IClient* screen = m_activeSaver;
|
|
|
|
SInt32 x, y, w, h;
|
|
|
|
screen->getShape(x, y, w, h);
|
|
|
|
SInt32 zoneSize = screen->getJumpZoneSize();
|
|
|
|
if (m_xSaver < x + zoneSize) {
|
|
|
|
m_xSaver = x + zoneSize;
|
|
|
|
}
|
|
|
|
else if (m_xSaver >= x + w - zoneSize) {
|
|
|
|
m_xSaver = x + w - zoneSize - 1;
|
|
|
|
}
|
|
|
|
if (m_ySaver < y + zoneSize) {
|
|
|
|
m_ySaver = y + zoneSize;
|
|
|
|
}
|
|
|
|
else if (m_ySaver >= y + h - zoneSize) {
|
|
|
|
m_ySaver = y + h - zoneSize - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// jump
|
|
|
|
switchScreen(screen, m_xSaver, m_ySaver, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset state
|
|
|
|
m_activeSaver = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send message to all clients
|
|
|
|
for (CClientList::const_iterator index = m_clients.begin();
|
|
|
|
index != m_clients.end(); ++index) {
|
|
|
|
IClient* client = index->second;
|
|
|
|
client->screensaver(activated);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
void
|
|
|
|
CServer::onOneShotTimerExpired(UInt32 id)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// ignore if it's an old timer or if switch wait isn't engaged anymore
|
|
|
|
if (!m_switchWaitEngaged || id != m_switchWaitTimer) {
|
2003-02-23 00:53:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if mouse is locked to screen
|
|
|
|
if (isLockedToScreenNoLock()) {
|
2003-02-23 22:29:08 +03:00
|
|
|
LOG((CLOG_DEBUG1 "locked to screen"));
|
|
|
|
clearSwitchState();
|
2003-02-23 00:53:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch screen
|
2003-02-23 22:29:08 +03:00
|
|
|
switchScreen(m_switchScreen, m_switchWaitX, m_switchWaitY, false);
|
2003-02-23 00:53:25 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onKeyDown(KeyID id, KeyModifierMask mask)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "onKeyDown id=%d mask=0x%04x", id, mask));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// handle command keys
|
|
|
|
if (onCommandKey(id, mask, true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relay
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->keyDown(id, mask);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onKeyUp(KeyID id, KeyModifierMask mask)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "onKeyUp id=%d mask=0x%04x", id, mask));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// handle command keys
|
|
|
|
if (onCommandKey(id, mask, false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relay
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->keyUp(id, mask);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onKeyRepeat(KeyID id, KeyModifierMask mask, SInt32 count)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "onKeyRepeat id=%d mask=0x%04x count=%d", id, mask, count));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// handle command keys
|
|
|
|
if (onCommandKey(id, mask, false)) {
|
|
|
|
onCommandKey(id, mask, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// relay
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->keyRepeat(id, mask, count);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseDown(ButtonID id)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "onMouseDown id=%d", id));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// relay
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->mouseDown(id);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseUp(ButtonID id)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "onMouseUp id=%d", id));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// relay
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->mouseUp(id);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseMovePrimary(SInt32 x, SInt32 y)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "onMouseMovePrimary %d,%d", x, y));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
return onMouseMovePrimaryNoLock(x, y);
|
|
|
|
}
|
2001-10-14 18:37:41 +04:00
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseMovePrimaryNoLock(SInt32 x, SInt32 y)
|
2002-06-01 14:52:02 +04:00
|
|
|
{
|
2001-10-06 18:13:28 +04:00
|
|
|
// mouse move on primary (server's) screen
|
2002-07-10 01:22:31 +04:00
|
|
|
assert(m_primaryClient != NULL);
|
|
|
|
assert(m_active == m_primaryClient);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// get screen shape
|
|
|
|
SInt32 ax, ay, aw, ah;
|
|
|
|
m_active->getShape(ax, ay, aw, ah);
|
|
|
|
SInt32 zoneSize = m_active->getJumpZoneSize();
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// see if we should change screens
|
2002-07-15 19:01:36 +04:00
|
|
|
EDirection dir;
|
2002-07-10 01:22:31 +04:00
|
|
|
if (x < ax + zoneSize) {
|
|
|
|
x -= zoneSize;
|
2002-07-15 19:01:36 +04:00
|
|
|
dir = kLeft;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
else if (x >= ax + aw - zoneSize) {
|
|
|
|
x += zoneSize;
|
2002-07-15 19:01:36 +04:00
|
|
|
dir = kRight;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
else if (y < ay + zoneSize) {
|
|
|
|
y -= zoneSize;
|
2002-07-15 19:01:36 +04:00
|
|
|
dir = kTop;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
else if (y >= ay + ah - zoneSize) {
|
|
|
|
y += zoneSize;
|
2002-07-15 19:01:36 +04:00
|
|
|
dir = kBottom;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// still on local screen
|
2003-02-23 22:29:08 +03:00
|
|
|
onNoSwitch();
|
2001-11-19 03:33:36 +03:00
|
|
|
return false;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// get jump destination
|
2002-07-10 01:22:31 +04:00
|
|
|
IClient* newScreen = getNeighbor(m_active, dir, x, y);
|
2003-02-23 00:53:25 +03:00
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// should we switch or not?
|
|
|
|
if (isSwitchOkay(newScreen, dir, x, y)) {
|
|
|
|
// switch screen
|
|
|
|
switchScreen(newScreen, x, y, false);
|
|
|
|
return true;
|
2003-02-23 00:53:25 +03:00
|
|
|
}
|
2003-02-23 22:29:08 +03:00
|
|
|
else {
|
2001-11-19 03:33:36 +03:00
|
|
|
return false;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseMoveSecondary(SInt32 dx, SInt32 dy)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "onMouseMoveSecondary %+d,%+d", dx, dy));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
onMouseMoveSecondaryNoLock(dx, dy);
|
|
|
|
}
|
2001-10-14 18:37:41 +04:00
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseMoveSecondaryNoLock(SInt32 dx, SInt32 dy)
|
2002-06-01 14:52:02 +04:00
|
|
|
{
|
2001-10-06 18:13:28 +04:00
|
|
|
// mouse move on secondary (client's) screen
|
|
|
|
assert(m_active != NULL);
|
2002-07-10 01:22:31 +04:00
|
|
|
if (m_active == m_primaryClient) {
|
2002-06-01 14:52:02 +04:00
|
|
|
// we're actually on the primary screen. this can happen
|
|
|
|
// when the primary screen begins processing a mouse move
|
|
|
|
// for a secondary screen, then the active (secondary)
|
|
|
|
// screen disconnects causing us to jump to the primary
|
|
|
|
// screen, and finally the primary screen finishes
|
|
|
|
// processing the mouse move, still thinking it's for
|
|
|
|
// a secondary screen. we just ignore the motion.
|
|
|
|
return;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// save old position
|
|
|
|
const SInt32 xOld = m_x;
|
|
|
|
const SInt32 yOld = m_y;
|
|
|
|
|
|
|
|
// accumulate motion
|
|
|
|
m_x += dx;
|
|
|
|
m_y += dy;
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// get screen shape
|
|
|
|
SInt32 ax, ay, aw, ah;
|
|
|
|
m_active->getShape(ax, ay, aw, ah);
|
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// find direction of neighbor and get the neighbor
|
|
|
|
IClient* newScreen;
|
|
|
|
do {
|
|
|
|
EDirection dir;
|
|
|
|
if (m_x < ax) {
|
|
|
|
dir = kLeft;
|
2002-06-09 01:48:00 +04:00
|
|
|
}
|
2003-02-23 22:29:08 +03:00
|
|
|
else if (m_x > ax + aw - 1) {
|
|
|
|
dir = kRight;
|
2002-06-09 01:48:00 +04:00
|
|
|
}
|
2003-02-23 22:29:08 +03:00
|
|
|
else if (m_y < ay) {
|
|
|
|
dir = kTop;
|
|
|
|
}
|
|
|
|
else if (m_y > ay + ah - 1) {
|
|
|
|
dir = kBottom;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we haven't left the screen
|
|
|
|
newScreen = m_active;
|
|
|
|
|
|
|
|
// if waiting and mouse is not on the border we're waiting
|
|
|
|
// on then stop waiting. also if it's not on the border
|
|
|
|
// then arm the double tap.
|
|
|
|
if (m_switchScreen != NULL) {
|
|
|
|
bool clearWait;
|
|
|
|
SInt32 zoneSize = m_primaryClient->getJumpZoneSize();
|
|
|
|
switch (m_switchDir) {
|
|
|
|
case kLeft:
|
|
|
|
clearWait = (m_x >= ax + zoneSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kRight:
|
|
|
|
clearWait = (m_x <= ax + aw - 1 - zoneSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kTop:
|
|
|
|
clearWait = (m_y >= ay + zoneSize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kBottom:
|
|
|
|
clearWait = (m_y <= ay + ah - 1 + zoneSize);
|
|
|
|
break;
|
2003-03-13 01:34:07 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
clearWait = false;
|
|
|
|
break;
|
2003-02-23 22:29:08 +03:00
|
|
|
}
|
|
|
|
if (clearWait) {
|
|
|
|
onNoSwitch();
|
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// skip rest of block
|
|
|
|
break;
|
|
|
|
}
|
2002-06-09 01:48:00 +04:00
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// try to switch screen. get the neighbor.
|
|
|
|
newScreen = getNeighbor(m_active, dir, m_x, m_y);
|
2003-02-23 00:53:25 +03:00
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// see if we should switch
|
|
|
|
if (!isSwitchOkay(newScreen, dir, m_x, m_y)) {
|
|
|
|
newScreen = m_active;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2003-02-23 22:29:08 +03:00
|
|
|
} while (false);
|
2003-02-23 00:53:25 +03:00
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
if (newScreen != m_active) {
|
|
|
|
// switch screens
|
|
|
|
switchScreen(newScreen, m_x, m_y, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// same screen. clamp mouse to edge.
|
|
|
|
m_x = xOld + dx;
|
|
|
|
m_y = yOld + dy;
|
2002-07-10 01:22:31 +04:00
|
|
|
if (m_x < ax) {
|
|
|
|
m_x = ax;
|
2003-02-23 00:53:25 +03:00
|
|
|
LOG((CLOG_DEBUG2 "clamp to left of \"%s\"", m_active->getName().c_str()));
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
else if (m_x > ax + aw - 1) {
|
|
|
|
m_x = ax + aw - 1;
|
2003-02-23 00:53:25 +03:00
|
|
|
LOG((CLOG_DEBUG2 "clamp to right of \"%s\"", m_active->getName().c_str()));
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
if (m_y < ay) {
|
|
|
|
m_y = ay;
|
2003-02-23 00:53:25 +03:00
|
|
|
LOG((CLOG_DEBUG2 "clamp to top of \"%s\"", m_active->getName().c_str()));
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
else if (m_y > ay + ah - 1) {
|
|
|
|
m_y = ay + ah - 1;
|
2003-02-23 00:53:25 +03:00
|
|
|
LOG((CLOG_DEBUG2 "clamp to bottom of \"%s\"", m_active->getName().c_str()));
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
// warp cursor if it moved.
|
2001-10-06 18:13:28 +04:00
|
|
|
if (m_x != xOld || m_y != yOld) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "move on %s to %d,%d", m_active->getName().c_str(), m_x, m_y));
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->mouseMove(m_x, m_y);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::onMouseWheel(SInt32 delta)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "onMouseWheel %+d", delta));
|
2002-06-01 14:52:02 +04:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
|
|
|
// relay
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->mouseWheel(delta);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
bool
|
|
|
|
CServer::onCommandKey(KeyID /*id*/, KeyModifierMask /*mask*/, bool /*down*/)
|
2002-06-22 23:20:21 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
return false;
|
2002-06-22 23:20:21 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CServer::isLockedToScreenNoLock() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-05-24 18:37:12 +04:00
|
|
|
// locked if primary says we're locked
|
2002-07-10 01:22:31 +04:00
|
|
|
if (m_primaryClient->isLockedToScreen()) {
|
2002-05-24 18:37:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// locked if scroll-lock is toggled on
|
2002-07-10 01:22:31 +04:00
|
|
|
if ((m_primaryClient->getToggleMask() & KeyModifierScrollLock) != 0) {
|
2002-05-24 18:37:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not locked
|
2001-10-06 18:13:28 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CServer::switchScreen(IClient* dst, SInt32 x, SInt32 y, bool forScreensaver)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-30 22:31:00 +04:00
|
|
|
// note -- must be locked on entry
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(dst != NULL);
|
2002-07-10 01:22:31 +04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
{
|
|
|
|
SInt32 dx, dy, dw, dh;
|
|
|
|
dst->getShape(dx, dy, dw, dh);
|
|
|
|
assert(x >= dx && y >= dy && x < dx + dw && y < dy + dh);
|
|
|
|
}
|
|
|
|
#endif
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(m_active != NULL);
|
|
|
|
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "switch from \"%s\" to \"%s\" at %d,%d", m_active->getName().c_str(), dst->getName().c_str(), x, y));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2003-02-23 00:53:25 +03:00
|
|
|
// stop waiting to switch
|
2003-02-23 22:29:08 +03:00
|
|
|
clearSwitchState();
|
2003-02-23 00:53:25 +03:00
|
|
|
|
2002-06-03 22:53:18 +04:00
|
|
|
// record new position
|
|
|
|
m_x = x;
|
|
|
|
m_y = y;
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// wrapping means leaving the active screen and entering it again.
|
|
|
|
// since that's a waste of time we skip that and just warp the
|
|
|
|
// mouse.
|
|
|
|
if (m_active != dst) {
|
2002-06-03 22:53:18 +04:00
|
|
|
// leave active screen
|
2002-07-10 01:22:31 +04:00
|
|
|
if (!m_active->leave()) {
|
|
|
|
// cannot leave screen
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "can't leave screen"));
|
2002-07-10 01:22:31 +04:00
|
|
|
return;
|
|
|
|
}
|
2002-06-03 22:53:18 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// update the primary client's clipboards if we're leaving the
|
|
|
|
// primary screen.
|
|
|
|
if (m_active == m_primaryClient) {
|
2002-07-11 17:13:37 +04:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-07-10 01:22:31 +04:00
|
|
|
CClipboardInfo& clipboard = m_clipboards[id];
|
|
|
|
if (clipboard.m_clipboardOwner == m_primaryClient->getName()) {
|
|
|
|
CString clipboardData;
|
|
|
|
m_primaryClient->getClipboard(id, clipboardData);
|
2002-07-10 18:15:17 +04:00
|
|
|
onClipboardChangedNoLock(id,
|
|
|
|
clipboard.m_clipboardSeqNum, clipboardData);
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
2002-04-29 17:31:44 +04:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// cut over
|
|
|
|
m_active = dst;
|
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// increment enter sequence number
|
|
|
|
++m_seqNum;
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// enter new screen
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->enter(x, y, m_seqNum,
|
2002-07-14 02:00:38 +04:00
|
|
|
m_primaryClient->getToggleMask(),
|
|
|
|
forScreensaver);
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// send the clipboard data to new active screen
|
2002-04-27 18:19:53 +04:00
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->setClipboard(id, m_clipboards[id].m_clipboardData);
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-07-10 01:22:31 +04:00
|
|
|
m_active->mouseMove(x, y);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
IClient*
|
2002-07-15 19:01:36 +04:00
|
|
|
CServer::getNeighbor(IClient* src, EDirection dir) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-12-23 16:55:21 +03:00
|
|
|
// note -- must be locked on entry
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(src != NULL);
|
|
|
|
|
2003-01-17 00:28:15 +03:00
|
|
|
// get source screen name
|
2002-07-10 01:22:31 +04:00
|
|
|
CString srcName = src->getName();
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(!srcName.empty());
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "find neighbor on %s of \"%s\"", CConfig::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2003-01-17 00:28:15 +03:00
|
|
|
// get first neighbor. if it's the source then the source jumps
|
|
|
|
// to itself and we return the source.
|
|
|
|
CString dstName(m_config.getNeighbor(srcName, dir));
|
|
|
|
if (dstName == srcName) {
|
|
|
|
LOG((CLOG_DEBUG2 "\"%s\" is on %s of \"%s\"", dstName.c_str(), CConfig::dirName(dir), srcName.c_str()));
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep checking
|
|
|
|
for (;;) {
|
|
|
|
// if nothing in that direction then return NULL. if the
|
|
|
|
// destination is the source then we can make no more
|
|
|
|
// progress in this direction. since we haven't found a
|
|
|
|
// connected neighbor we return NULL.
|
|
|
|
if (dstName.empty() || dstName == srcName) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "no neighbor on %s of \"%s\"", CConfig::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
return NULL;
|
2001-10-14 18:37:41 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-06-01 14:52:02 +04:00
|
|
|
// look up neighbor cell. if the screen is connected and
|
2003-01-17 00:28:15 +03:00
|
|
|
// ready then we can stop.
|
2002-07-10 01:22:31 +04:00
|
|
|
CClientList::const_iterator index = m_clients.find(dstName);
|
|
|
|
if (index != m_clients.end()) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "\"%s\" is on %s of \"%s\"", dstName.c_str(), CConfig::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
return index->second;
|
|
|
|
}
|
|
|
|
|
2003-01-17 00:28:15 +03:00
|
|
|
// skip over unconnected screen
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "ignored \"%s\" on %s of \"%s\"", dstName.c_str(), CConfig::dirName(dir), srcName.c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
srcName = dstName;
|
2003-01-17 00:28:15 +03:00
|
|
|
|
|
|
|
// look up name of neighbor of skipped screen
|
|
|
|
dstName = m_config.getNeighbor(srcName, dir);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
IClient*
|
|
|
|
CServer::getNeighbor(IClient* src,
|
2002-07-15 19:01:36 +04:00
|
|
|
EDirection srcSide, SInt32& x, SInt32& y) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-12-23 16:55:21 +03:00
|
|
|
// note -- must be locked on entry
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(src != NULL);
|
|
|
|
|
|
|
|
// get the first neighbor
|
2002-07-10 01:22:31 +04:00
|
|
|
IClient* dst = getNeighbor(src, srcSide);
|
2002-06-11 22:31:06 +04:00
|
|
|
if (dst == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// get the source screen's size (needed for kRight and kBottom)
|
2002-07-10 01:22:31 +04:00
|
|
|
SInt32 sx, sy, sw, sh;
|
|
|
|
SInt32 dx, dy, dw, dh;
|
|
|
|
IClient* lastGoodScreen = src;
|
|
|
|
lastGoodScreen->getShape(sx, sy, sw, sh);
|
|
|
|
lastGoodScreen->getShape(dx, dy, dw, dh);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-06-19 21:03:29 +04:00
|
|
|
// find destination screen, adjusting x or y (but not both). the
|
|
|
|
// searches are done in a sort of canonical screen space where
|
|
|
|
// the upper-left corner is 0,0 for each screen. we adjust from
|
|
|
|
// actual to canonical position on entry to and from canonical to
|
|
|
|
// actual on exit from the search.
|
2001-10-06 18:13:28 +04:00
|
|
|
switch (srcSide) {
|
2002-07-15 19:01:36 +04:00
|
|
|
case kLeft:
|
2002-07-10 01:22:31 +04:00
|
|
|
x -= dx;
|
2003-01-17 00:28:15 +03:00
|
|
|
while (dst != NULL) {
|
2001-10-06 18:13:28 +04:00
|
|
|
lastGoodScreen = dst;
|
2002-07-10 01:22:31 +04:00
|
|
|
lastGoodScreen->getShape(dx, dy, dw, dh);
|
|
|
|
x += dw;
|
2001-10-06 18:13:28 +04:00
|
|
|
if (x >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "skipping over screen %s", dst->getName().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
2002-06-19 21:03:29 +04:00
|
|
|
assert(lastGoodScreen != NULL);
|
2002-07-10 01:22:31 +04:00
|
|
|
x += dx;
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kRight:
|
2002-07-10 01:22:31 +04:00
|
|
|
x -= dx;
|
2001-10-06 18:13:28 +04:00
|
|
|
while (dst != NULL) {
|
2002-07-10 01:22:31 +04:00
|
|
|
x -= dw;
|
2001-10-06 18:13:28 +04:00
|
|
|
lastGoodScreen = dst;
|
2002-07-10 01:22:31 +04:00
|
|
|
lastGoodScreen->getShape(dx, dy, dw, dh);
|
|
|
|
if (x < dw) {
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "skipping over screen %s", dst->getName().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
2002-06-19 21:03:29 +04:00
|
|
|
assert(lastGoodScreen != NULL);
|
2002-07-10 01:22:31 +04:00
|
|
|
x += dx;
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kTop:
|
2002-07-10 01:22:31 +04:00
|
|
|
y -= dy;
|
2001-10-06 18:13:28 +04:00
|
|
|
while (dst != NULL) {
|
|
|
|
lastGoodScreen = dst;
|
2002-07-10 01:22:31 +04:00
|
|
|
lastGoodScreen->getShape(dx, dy, dw, dh);
|
|
|
|
y += dh;
|
2001-10-06 18:13:28 +04:00
|
|
|
if (y >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "skipping over screen %s", dst->getName().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
2002-06-19 21:03:29 +04:00
|
|
|
assert(lastGoodScreen != NULL);
|
2002-07-10 01:22:31 +04:00
|
|
|
y += dy;
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kBottom:
|
2002-07-10 01:22:31 +04:00
|
|
|
y -= dy;
|
2001-10-06 18:13:28 +04:00
|
|
|
while (dst != NULL) {
|
2002-07-10 01:22:31 +04:00
|
|
|
y -= dh;
|
2001-10-06 18:13:28 +04:00
|
|
|
lastGoodScreen = dst;
|
2002-07-10 01:22:31 +04:00
|
|
|
lastGoodScreen->getShape(dx, dy, dw, dh);
|
|
|
|
if (y < sh) {
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG2 "skipping over screen %s", dst->getName().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
dst = getNeighbor(lastGoodScreen, srcSide);
|
|
|
|
}
|
2002-06-19 21:03:29 +04:00
|
|
|
assert(lastGoodScreen != NULL);
|
2002-07-10 01:22:31 +04:00
|
|
|
y += dy;
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2001-10-24 01:13:08 +04:00
|
|
|
|
2002-06-19 21:03:29 +04:00
|
|
|
// save destination screen
|
|
|
|
assert(lastGoodScreen != NULL);
|
|
|
|
dst = lastGoodScreen;
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// if entering primary screen then be sure to move in far enough
|
|
|
|
// to avoid the jump zone. if entering a side that doesn't have
|
|
|
|
// a neighbor (i.e. an asymmetrical side) then we don't need to
|
|
|
|
// move inwards because that side can't provoke a jump.
|
2002-07-10 01:22:31 +04:00
|
|
|
if (dst == m_primaryClient) {
|
|
|
|
const CString dstName(dst->getName());
|
2001-10-06 18:13:28 +04:00
|
|
|
switch (srcSide) {
|
2002-07-15 19:01:36 +04:00
|
|
|
case kLeft:
|
|
|
|
if (!m_config.getNeighbor(dstName, kRight).empty() &&
|
2002-07-10 01:22:31 +04:00
|
|
|
x > dx + dw - 1 - dst->getJumpZoneSize())
|
|
|
|
x = dx + dw - 1 - dst->getJumpZoneSize();
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kRight:
|
|
|
|
if (!m_config.getNeighbor(dstName, kLeft).empty() &&
|
2002-07-10 01:22:31 +04:00
|
|
|
x < dx + dst->getJumpZoneSize())
|
|
|
|
x = dx + dst->getJumpZoneSize();
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kTop:
|
|
|
|
if (!m_config.getNeighbor(dstName, kBottom).empty() &&
|
2002-07-10 01:22:31 +04:00
|
|
|
y > dy + dh - 1 - dst->getJumpZoneSize())
|
|
|
|
y = dy + dh - 1 - dst->getJumpZoneSize();
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kBottom:
|
|
|
|
if (!m_config.getNeighbor(dstName, kTop).empty() &&
|
2002-07-10 01:22:31 +04:00
|
|
|
y < dy + dst->getJumpZoneSize())
|
|
|
|
y = dy + dst->getJumpZoneSize();
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-19 21:03:29 +04:00
|
|
|
// adjust the coordinate orthogonal to srcSide to account for
|
|
|
|
// resolution differences. for example, if y is 200 pixels from
|
|
|
|
// the top on a screen 1000 pixels high (20% from the top) when
|
|
|
|
// we cross the left edge onto a screen 600 pixels high then y
|
|
|
|
// should be set 120 pixels from the top (again 20% from the
|
|
|
|
// top).
|
2001-10-06 18:13:28 +04:00
|
|
|
switch (srcSide) {
|
2002-07-15 19:01:36 +04:00
|
|
|
case kLeft:
|
|
|
|
case kRight:
|
2002-07-10 01:22:31 +04:00
|
|
|
y -= sy;
|
2002-06-11 02:06:45 +04:00
|
|
|
if (y < 0) {
|
2001-10-25 03:29:29 +04:00
|
|
|
y = 0;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
else if (y >= sh) {
|
|
|
|
y = dh - 1;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
|
|
|
else {
|
2001-10-25 03:29:29 +04:00
|
|
|
y = static_cast<SInt32>(0.5 + y *
|
2002-07-10 01:22:31 +04:00
|
|
|
static_cast<double>(dh - 1) / (sh - 1));
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
y += dy;
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
|
2002-07-15 19:01:36 +04:00
|
|
|
case kTop:
|
|
|
|
case kBottom:
|
2002-07-10 01:22:31 +04:00
|
|
|
x -= sx;
|
2002-06-11 02:06:45 +04:00
|
|
|
if (x < 0) {
|
2001-10-25 03:29:29 +04:00
|
|
|
x = 0;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
else if (x >= sw) {
|
|
|
|
x = dw - 1;
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
|
|
|
else {
|
2001-10-25 03:29:29 +04:00
|
|
|
x = static_cast<SInt32>(0.5 + x *
|
2002-07-10 01:22:31 +04:00
|
|
|
static_cast<double>(dw - 1) / (sw - 1));
|
2002-06-11 02:06:45 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
x += dx;
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-06-19 21:03:29 +04:00
|
|
|
|
|
|
|
return dst;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-02-23 22:29:08 +03:00
|
|
|
bool
|
|
|
|
CServer::isSwitchOkay(IClient* newScreen, EDirection dir, SInt32 x, SInt32 y)
|
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG1 "try to leave \"%s\" on %s", m_active->getName().c_str(), CConfig::dirName(dir)));
|
|
|
|
|
|
|
|
// is there a neighbor?
|
|
|
|
if (newScreen == NULL) {
|
|
|
|
// there's no neighbor. we don't want to switch and we don't
|
|
|
|
// want to try to switch later.
|
|
|
|
LOG((CLOG_DEBUG1 "no neighbor %s", CConfig::dirName(dir)));
|
|
|
|
clearSwitchState();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// should we switch or not?
|
|
|
|
bool preventSwitch = false;
|
|
|
|
bool allowSwitch = false;
|
|
|
|
|
|
|
|
// note if the switch direction has changed. save the new
|
|
|
|
// direction and screen if so.
|
|
|
|
bool isNewDirection = (dir != m_switchDir);
|
|
|
|
if (isNewDirection || m_switchScreen == NULL) {
|
|
|
|
m_switchDir = dir;
|
|
|
|
m_switchScreen = newScreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this a double tap and do we care?
|
|
|
|
if (!allowSwitch && m_switchTwoTapDelay > 0.0) {
|
|
|
|
if (isNewDirection || !m_switchTwoTapEngaged) {
|
|
|
|
// tapping a different or new edge. prepare for second tap.
|
|
|
|
preventSwitch = true;
|
|
|
|
m_switchTwoTapEngaged = true;
|
|
|
|
m_switchTwoTapArmed = false;
|
|
|
|
m_switchTwoTapTimer.reset();
|
|
|
|
LOG((CLOG_DEBUG1 "waiting for second tap"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// second tap if we were armed. if soon enough then switch.
|
|
|
|
if (m_switchTwoTapArmed &&
|
|
|
|
m_switchTwoTapTimer.getTime() <= m_switchTwoTapDelay) {
|
|
|
|
allowSwitch = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// not fast enough. reset the clock.
|
|
|
|
preventSwitch = true;
|
|
|
|
m_switchTwoTapEngaged = true;
|
|
|
|
m_switchTwoTapArmed = false;
|
|
|
|
m_switchTwoTapTimer.reset();
|
|
|
|
LOG((CLOG_DEBUG1 "waiting for second tap"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if waiting before a switch then prepare to switch later
|
|
|
|
if (!allowSwitch && m_switchWaitDelay > 0.0) {
|
2003-03-13 01:34:07 +03:00
|
|
|
if (isNewDirection || !m_switchWaitEngaged) {
|
2003-02-23 22:29:08 +03:00
|
|
|
m_switchWaitEngaged = true;
|
|
|
|
m_switchWaitX = x;
|
|
|
|
m_switchWaitY = y;
|
|
|
|
m_switchWaitTimer = m_primaryClient->addOneShotTimer(
|
|
|
|
m_switchWaitDelay);
|
|
|
|
LOG((CLOG_DEBUG1 "waiting to switch"));
|
|
|
|
}
|
|
|
|
preventSwitch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if mouse is locked to screen
|
|
|
|
if (!preventSwitch && isLockedToScreenNoLock()) {
|
|
|
|
LOG((CLOG_DEBUG1 "locked to screen"));
|
|
|
|
preventSwitch = true;
|
|
|
|
|
|
|
|
// don't try to switch later. it's possible that we might
|
|
|
|
// not be locked to the screen when the wait delay expires
|
|
|
|
// and could switch then but we'll base the decision on
|
|
|
|
// when the user first attempts the switch. this also
|
|
|
|
// ensures that all switch tests are using the same
|
|
|
|
clearSwitchState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return !preventSwitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::onNoSwitch()
|
|
|
|
{
|
|
|
|
if (m_switchTwoTapEngaged) {
|
|
|
|
if (m_switchTwoTapTimer.getTime() > m_switchTwoTapDelay) {
|
|
|
|
// second tap took too long. disengage.
|
|
|
|
m_switchTwoTapEngaged = false;
|
|
|
|
m_switchTwoTapArmed = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we've moved away from the edge and there's still
|
|
|
|
// time to get back for a double tap.
|
|
|
|
m_switchTwoTapArmed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// once the mouse moves away from the edge we no longer want to
|
|
|
|
// switch after a delay.
|
|
|
|
m_switchWaitEngaged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::clearSwitchState()
|
|
|
|
{
|
|
|
|
if (m_switchScreen != NULL) {
|
|
|
|
m_switchDir = kNoDirection;
|
|
|
|
m_switchScreen = NULL;
|
|
|
|
m_switchWaitEngaged = false;
|
|
|
|
m_switchTwoTapEngaged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
void
|
|
|
|
CServer::closeClients(const CConfig& config)
|
|
|
|
{
|
|
|
|
CThreadList threads;
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// get the set of clients that are connected but are being
|
|
|
|
// dropped from the configuration (or who's canonical name
|
|
|
|
// is changing) and tell them to disconnect. note that
|
|
|
|
// since m_clientThreads doesn't include a thread for the
|
|
|
|
// primary client we will not close it.
|
|
|
|
for (CClientThreadList::iterator
|
|
|
|
index = m_clientThreads.begin();
|
|
|
|
index != m_clientThreads.end(); ) {
|
|
|
|
const CString& name = index->first;
|
|
|
|
if (!config.isCanonicalName(name)) {
|
|
|
|
// lookup IClient with name
|
|
|
|
CClientList::const_iterator index2 = m_clients.find(name);
|
|
|
|
assert(index2 != m_clients.end());
|
|
|
|
|
2002-07-11 17:13:37 +04:00
|
|
|
// save the thread and remove it from m_clientThreads
|
|
|
|
threads.push_back(index->second);
|
|
|
|
m_clientThreads.erase(index++);
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// close that client
|
|
|
|
assert(index2->second != m_primaryClient);
|
|
|
|
index2->second->close();
|
2003-02-23 00:53:25 +03:00
|
|
|
|
|
|
|
// don't switch to it if we planned to
|
2003-02-23 22:29:08 +03:00
|
|
|
if (index2->second == m_switchScreen) {
|
|
|
|
clearSwitchState();
|
2003-02-23 00:53:25 +03:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait a moment to allow each client to close its connection
|
|
|
|
// before we close it (to avoid having our socket enter TIME_WAIT).
|
|
|
|
if (threads.size() > 0) {
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->sleep(1.0);
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cancel the old client threads
|
|
|
|
for (CThreadList::iterator index = threads.begin();
|
|
|
|
index != threads.end(); ++index) {
|
|
|
|
index->cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for old client threads to terminate. we must not hold
|
|
|
|
// the lock while we do this so those threads can finish any
|
|
|
|
// calls to this object.
|
|
|
|
for (CThreadList::iterator index = threads.begin();
|
|
|
|
index != threads.end(); ++index) {
|
|
|
|
index->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up thread list
|
|
|
|
reapThreads();
|
|
|
|
}
|
|
|
|
|
2002-07-18 21:00:48 +04:00
|
|
|
CThread
|
2002-06-21 19:18:01 +04:00
|
|
|
CServer::startThread(IJob* job)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// reap completed threads
|
2002-06-21 19:18:01 +04:00
|
|
|
doReapThreads(m_threads);
|
2002-07-10 01:22:31 +04:00
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
// add new thread to list
|
|
|
|
CThread thread(job);
|
2002-07-18 21:00:48 +04:00
|
|
|
m_threads.push_back(thread);
|
2003-01-05 01:01:32 +03:00
|
|
|
LOG((CLOG_DEBUG1 "started thread 0x%08x", thread.getID()));
|
2002-07-18 21:00:48 +04:00
|
|
|
return thread;
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::stopThreads(double timeout)
|
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "stopping threads"));
|
2002-06-21 19:18:01 +04:00
|
|
|
|
2002-07-18 21:00:48 +04:00
|
|
|
// cancel the accept client thread to prevent more clients from
|
|
|
|
// connecting while we're shutting down.
|
|
|
|
CThread* acceptClientThread;
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
acceptClientThread = m_acceptClientThread;
|
|
|
|
m_acceptClientThread = NULL;
|
|
|
|
}
|
|
|
|
if (acceptClientThread != NULL) {
|
|
|
|
acceptClientThread->cancel();
|
|
|
|
acceptClientThread->wait(timeout);
|
|
|
|
delete acceptClientThread;
|
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// close all clients (except the primary)
|
|
|
|
{
|
|
|
|
CConfig emptyConfig;
|
|
|
|
closeClients(emptyConfig);
|
|
|
|
}
|
|
|
|
|
2002-06-21 19:18:01 +04:00
|
|
|
// swap thread list so nobody can mess with it
|
|
|
|
CThreadList threads;
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
threads.swap(m_threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancel every thread
|
|
|
|
for (CThreadList::iterator index = threads.begin();
|
|
|
|
index != threads.end(); ++index) {
|
2002-07-10 01:22:31 +04:00
|
|
|
index->cancel();
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// now wait for the threads
|
|
|
|
CStopwatch timer(true);
|
|
|
|
while (threads.size() > 0 && (timeout < 0.0 || timer.getTime() < timeout)) {
|
|
|
|
doReapThreads(threads);
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->sleep(0.01);
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete remaining threads
|
|
|
|
for (CThreadList::iterator index = threads.begin();
|
|
|
|
index != threads.end(); ++index) {
|
2003-01-05 01:01:32 +03:00
|
|
|
LOG((CLOG_DEBUG1 "reaped running thread 0x%08x", index->getID()));
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
|
|
|
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "stopped threads"));
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::reapThreads()
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
doReapThreads(m_threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CServer::doReapThreads(CThreadList& threads)
|
|
|
|
{
|
|
|
|
for (CThreadList::iterator index = threads.begin();
|
|
|
|
index != threads.end(); ) {
|
2002-07-10 01:22:31 +04:00
|
|
|
if (index->wait(0.0)) {
|
2002-06-21 19:18:01 +04:00
|
|
|
// thread terminated
|
2003-01-05 01:01:32 +03:00
|
|
|
LOG((CLOG_DEBUG1 "reaped thread 0x%08x", index->getID()));
|
2002-06-21 19:18:01 +04:00
|
|
|
index = threads.erase(index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// thread is running
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CServer::acceptClients(void*)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "starting to wait for clients"));
|
2001-10-14 18:37:41 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
IListenSocket* listen = NULL;
|
2001-10-06 18:13:28 +04:00
|
|
|
try {
|
|
|
|
// create socket listener
|
2002-07-30 22:31:00 +04:00
|
|
|
if (m_socketFactory != NULL) {
|
|
|
|
listen = m_socketFactory->createListen();
|
|
|
|
}
|
|
|
|
assert(listen != NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// bind to the desired port. keep retrying if we can't bind
|
|
|
|
// the address immediately.
|
|
|
|
CStopwatch timer;
|
|
|
|
for (;;) {
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "binding listen socket"));
|
2002-06-09 20:53:25 +04:00
|
|
|
listen->bind(m_config.getSynergyAddress());
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-10-18 01:37:31 +04:00
|
|
|
catch (XSocketAddressInUse& e) {
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError, e.what());
|
2003-01-05 01:01:32 +03:00
|
|
|
LOG((CLOG_WARN "bind failed: %s", e.what()));
|
2002-07-24 17:01:18 +04:00
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// give up if we've waited too long
|
|
|
|
if (timer.getTime() >= m_bindTimeout) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "waited too long to bind, giving up"));
|
2001-10-06 18:13:28 +04:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait a bit before retrying
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->sleep(5.0);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// accept connections and begin processing them
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kRunning);
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "waiting for client connections"));
|
2001-10-06 18:13:28 +04:00
|
|
|
for (;;) {
|
|
|
|
// accept connection
|
|
|
|
CThread::testCancel();
|
2002-06-17 16:02:26 +04:00
|
|
|
IDataSocket* socket = listen->accept();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "accepted client connection"));
|
2001-10-06 18:13:28 +04:00
|
|
|
CThread::testCancel();
|
|
|
|
|
|
|
|
// start handshake thread
|
2002-06-21 19:18:01 +04:00
|
|
|
startThread(new TMethodJob<CServer>(
|
2002-07-10 01:22:31 +04:00
|
|
|
this, &CServer::runClient, socket));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kError, e.what());
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "cannot listen for clients: %s", e.what()));
|
2002-07-11 00:18:32 +04:00
|
|
|
delete listen;
|
2002-10-18 01:37:31 +04:00
|
|
|
exitMainLoopWithError();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
catch (...) {
|
2003-03-13 01:34:07 +03:00
|
|
|
setStatus(kNotRunning);
|
2002-07-11 00:18:32 +04:00
|
|
|
delete listen;
|
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-10 01:22:31 +04:00
|
|
|
CServer::runClient(void* vsocket)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// get the socket pointer from the argument
|
|
|
|
assert(vsocket != NULL);
|
2002-07-11 00:18:32 +04:00
|
|
|
IDataSocket* socket = reinterpret_cast<IDataSocket*>(vsocket);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// create proxy
|
2002-07-11 00:18:32 +04:00
|
|
|
CClientProxy* proxy = NULL;
|
|
|
|
try {
|
|
|
|
proxy = handshakeClient(socket);
|
|
|
|
if (proxy == NULL) {
|
|
|
|
delete socket;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
delete socket;
|
|
|
|
throw;
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// add the connection
|
|
|
|
try {
|
|
|
|
addConnection(proxy);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// save this client's thread
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_clientThreads.insert(std::make_pair(proxy->getName(),
|
|
|
|
CThread::getCurrentThread()));
|
2002-12-23 16:55:21 +03:00
|
|
|
|
|
|
|
// send configuration options
|
|
|
|
sendOptions(proxy);
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
catch (XDuplicateClient& e) {
|
|
|
|
// client has duplicate name
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "a client with name \"%s\" is already connected", e.getName().c_str()));
|
2003-01-12 19:08:45 +03:00
|
|
|
try {
|
|
|
|
CProtocolUtil::writef(proxy->getOutputStream(), kMsgEBusy);
|
|
|
|
}
|
|
|
|
catch (XSocket&) {
|
|
|
|
// ignore
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
delete proxy;
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
2002-07-10 01:22:31 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (XUnknownClient& e) {
|
|
|
|
// client has unknown name
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "a client with name \"%s\" is not in the map", e.getName().c_str()));
|
2003-01-12 19:08:45 +03:00
|
|
|
try {
|
|
|
|
CProtocolUtil::writef(proxy->getOutputStream(), kMsgEUnknown);
|
|
|
|
}
|
|
|
|
catch (XSocket&) {
|
|
|
|
// ignore
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
delete proxy;
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
2002-07-10 01:22:31 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
delete proxy;
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
2002-07-10 01:22:31 +04:00
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// activate screen saver on new client if active on the primary screen
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (m_activeSaver != NULL) {
|
2002-07-14 02:00:38 +04:00
|
|
|
proxy->screensaver(true);
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// handle client messages
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "client \"%s\" has connected", proxy->getName().c_str()));
|
2002-07-30 19:17:44 +04:00
|
|
|
proxy->mainLoop();
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
catch (XBadClient&) {
|
|
|
|
// client not behaving
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "protocol error from client \"%s\"", proxy->getName().c_str()));
|
2003-01-12 19:08:45 +03:00
|
|
|
try {
|
|
|
|
CProtocolUtil::writef(proxy->getOutputStream(), kMsgEBad);
|
|
|
|
}
|
2003-01-12 19:35:11 +03:00
|
|
|
catch (XSocket&) {
|
2003-01-12 19:08:45 +03:00
|
|
|
// ignore. client probably aborted the connection.
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
// misc error
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "error communicating with client \"%s\": %s", proxy->getName().c_str(), e.what()));
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
2002-07-30 19:17:44 +04:00
|
|
|
// mainLoop() was probably cancelled
|
2002-07-10 01:22:31 +04:00
|
|
|
removeConnection(proxy->getName());
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
2002-07-10 01:22:31 +04:00
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// clean up
|
2002-07-10 01:22:31 +04:00
|
|
|
removeConnection(proxy->getName());
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
CClientProxy*
|
|
|
|
CServer::handshakeClient(IDataSocket* socket)
|
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "negotiating with new client"));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// get the input and output streams
|
|
|
|
IInputStream* input = socket->getInputStream();
|
|
|
|
IOutputStream* output = socket->getOutputStream();
|
|
|
|
bool own = false;
|
2002-06-09 21:21:33 +04:00
|
|
|
|
2002-07-30 22:31:00 +04:00
|
|
|
// attach filters
|
|
|
|
if (m_streamFilterFactory != NULL) {
|
|
|
|
input = m_streamFilterFactory->createInput(input, own);
|
|
|
|
output = m_streamFilterFactory->createOutput(output, own);
|
2002-07-11 00:18:32 +04:00
|
|
|
own = true;
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// attach the packetizing filters
|
|
|
|
input = new CInputPacketStream(input, own);
|
|
|
|
output = new COutputPacketStream(output, own);
|
2002-07-11 00:18:32 +04:00
|
|
|
own = true;
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
CClientProxy* proxy = NULL;
|
|
|
|
CString name("<unknown>");
|
|
|
|
try {
|
|
|
|
// give the client a limited time to complete the handshake
|
|
|
|
CTimerThread timer(30.0);
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// say hello
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "saying hello"));
|
2002-07-30 22:31:00 +04:00
|
|
|
CProtocolUtil::writef(output, kMsgHello,
|
2002-07-10 01:22:31 +04:00
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion);
|
|
|
|
output->flush();
|
2002-06-23 19:43:40 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// wait for the reply
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "waiting for hello reply"));
|
2002-07-10 01:22:31 +04:00
|
|
|
UInt32 n = input->getSize();
|
2002-07-30 22:31:00 +04:00
|
|
|
|
|
|
|
// limit the maximum length of the hello
|
|
|
|
if (n > kMaxHelloLength) {
|
2002-07-10 01:22:31 +04:00
|
|
|
throw XBadClient();
|
2002-05-23 18:56:03 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// get and parse the reply to hello
|
|
|
|
SInt16 major, minor;
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "parsing hello reply"));
|
2002-07-30 22:31:00 +04:00
|
|
|
CProtocolUtil::readf(input, kMsgHelloBack,
|
2002-07-10 01:22:31 +04:00
|
|
|
&major, &minor, &name);
|
2002-05-23 18:56:03 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
catch (XIO&) {
|
|
|
|
throw XBadClient();
|
2002-05-31 22:18:29 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// disallow invalid version numbers
|
|
|
|
if (major < 0 || minor < 0) {
|
|
|
|
throw XIncompatibleClient(major, minor);
|
2002-05-23 18:56:03 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// disallow connection from test versions to release versions
|
|
|
|
if (major == 0 && kProtocolMajorVersion != 0) {
|
|
|
|
throw XIncompatibleClient(major, minor);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// hangup (with error) if version isn't supported
|
|
|
|
if (major > kProtocolMajorVersion ||
|
|
|
|
(major == kProtocolMajorVersion && minor > kProtocolMinorVersion)) {
|
|
|
|
throw XIncompatibleClient(major, minor);
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// convert name to canonical form (if any)
|
|
|
|
if (m_config.isScreen(name)) {
|
|
|
|
name = m_config.getCanonicalName(name);
|
2002-06-21 19:18:01 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// create client proxy for highest version supported by the client
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "creating proxy for client \"%s\" version %d.%d", name.c_str(), major, minor));
|
2002-07-10 01:22:31 +04:00
|
|
|
proxy = new CClientProxy1_0(this, name, input, output);
|
|
|
|
|
|
|
|
// negotiate
|
|
|
|
// FIXME
|
|
|
|
|
|
|
|
// ask and wait for the client's info
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "waiting for info for client \"%s\"", name.c_str()));
|
2002-07-10 01:22:31 +04:00
|
|
|
proxy->open();
|
|
|
|
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
catch (XIncompatibleClient& e) {
|
|
|
|
// client is incompatible
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "client \"%s\" has incompatible version %d.%d)", name.c_str(), e.getMajor(), e.getMinor()));
|
2003-01-12 19:08:45 +03:00
|
|
|
try {
|
|
|
|
CProtocolUtil::writef(output, kMsgEIncompatible,
|
2002-07-10 01:22:31 +04:00
|
|
|
kProtocolMajorVersion, kProtocolMinorVersion);
|
2003-01-12 19:08:45 +03:00
|
|
|
}
|
2003-01-12 19:35:11 +03:00
|
|
|
catch (XSocket&) {
|
2003-01-12 19:08:45 +03:00
|
|
|
// ignore
|
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
}
|
|
|
|
catch (XBadClient&) {
|
|
|
|
// client not behaving
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "protocol error from client \"%s\"", name.c_str()));
|
2003-01-12 19:08:45 +03:00
|
|
|
try {
|
|
|
|
CProtocolUtil::writef(output, kMsgEBad);
|
|
|
|
}
|
2003-01-12 19:35:11 +03:00
|
|
|
catch (XSocket&) {
|
2003-01-12 19:08:45 +03:00
|
|
|
// ignore. client probably aborted the connection.
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
// misc error
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "error communicating with client \"%s\": %s", name.c_str(), e.what()));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
catch (...) {
|
|
|
|
// probably timed out
|
|
|
|
if (proxy != NULL) {
|
|
|
|
delete proxy;
|
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
else if (own) {
|
2002-07-10 01:22:31 +04:00
|
|
|
delete input;
|
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failed
|
|
|
|
if (proxy != NULL) {
|
|
|
|
delete proxy;
|
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
else if (own) {
|
2002-07-10 01:22:31 +04:00
|
|
|
delete input;
|
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CServer::acceptHTTPClients(void*)
|
2002-05-30 20:13:16 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "starting to wait for HTTP clients"));
|
2002-05-30 20:13:16 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
IListenSocket* listen = NULL;
|
2002-05-30 20:13:16 +04:00
|
|
|
try {
|
|
|
|
// create socket listener
|
2002-07-30 22:31:00 +04:00
|
|
|
listen = new CTCPListenSocket;
|
2002-05-30 20:13:16 +04:00
|
|
|
|
|
|
|
// bind to the desired port. keep retrying if we can't bind
|
|
|
|
// the address immediately.
|
|
|
|
CStopwatch timer;
|
|
|
|
for (;;) {
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "binding HTTP listen socket"));
|
2002-06-09 20:53:25 +04:00
|
|
|
listen->bind(m_config.getHTTPAddress());
|
2002-05-30 20:13:16 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-07-24 17:01:18 +04:00
|
|
|
catch (XSocketBind& e) {
|
2003-01-05 01:01:32 +03:00
|
|
|
LOG((CLOG_DEBUG1 "bind HTTP failed: %s", e.what()));
|
2002-07-24 17:01:18 +04:00
|
|
|
|
2002-05-30 20:13:16 +04:00
|
|
|
// give up if we've waited too long
|
|
|
|
if (timer.getTime() >= m_bindTimeout) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "waited too long to bind HTTP, giving up"));
|
2002-05-30 20:13:16 +04:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait a bit before retrying
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->sleep(5.0);
|
2002-05-30 20:13:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// accept connections and begin processing them
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "waiting for HTTP connections"));
|
2002-05-30 20:13:16 +04:00
|
|
|
for (;;) {
|
2002-06-02 15:49:46 +04:00
|
|
|
// limit the number of HTTP requests being handled at once
|
|
|
|
{
|
|
|
|
CLock lock(&m_httpAvailable);
|
|
|
|
while (m_httpAvailable == 0) {
|
|
|
|
m_httpAvailable.wait();
|
|
|
|
}
|
|
|
|
assert(m_httpAvailable > 0);
|
|
|
|
m_httpAvailable = m_httpAvailable - 1;
|
|
|
|
}
|
|
|
|
|
2002-05-30 20:13:16 +04:00
|
|
|
// accept connection
|
|
|
|
CThread::testCancel();
|
2002-06-17 16:02:26 +04:00
|
|
|
IDataSocket* socket = listen->accept();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "accepted HTTP connection"));
|
2002-05-30 20:13:16 +04:00
|
|
|
CThread::testCancel();
|
|
|
|
|
|
|
|
// handle HTTP request
|
2002-06-21 19:18:01 +04:00
|
|
|
startThread(new TMethodJob<CServer>(
|
2002-05-30 20:13:16 +04:00
|
|
|
this, &CServer::processHTTPRequest, socket));
|
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete listen;
|
2002-05-30 20:13:16 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "cannot listen for HTTP clients: %s", e.what()));
|
2002-07-11 00:18:32 +04:00
|
|
|
delete listen;
|
2002-10-18 01:37:31 +04:00
|
|
|
exitMainLoopWithError();
|
2002-05-30 20:13:16 +04:00
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
catch (...) {
|
|
|
|
delete listen;
|
|
|
|
throw;
|
|
|
|
}
|
2002-05-30 20:13:16 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::processHTTPRequest(void* vsocket)
|
2002-05-30 20:13:16 +04:00
|
|
|
{
|
2002-06-17 16:02:26 +04:00
|
|
|
IDataSocket* socket = reinterpret_cast<IDataSocket*>(vsocket);
|
2002-05-30 20:13:16 +04:00
|
|
|
try {
|
|
|
|
// process the request and force delivery
|
|
|
|
m_httpServer->processRequest(socket);
|
|
|
|
socket->getOutputStream()->flush();
|
|
|
|
|
|
|
|
// wait a moment to give the client a chance to hangup first
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->sleep(3.0);
|
2002-05-30 20:13:16 +04:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
socket->close();
|
|
|
|
delete socket;
|
2002-06-02 15:49:46 +04:00
|
|
|
|
|
|
|
// increment available HTTP handlers
|
|
|
|
{
|
|
|
|
CLock lock(&m_httpAvailable);
|
|
|
|
m_httpAvailable = m_httpAvailable + 1;
|
|
|
|
m_httpAvailable.signal();
|
|
|
|
}
|
2002-05-30 20:13:16 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
delete socket;
|
2002-06-02 15:49:46 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_httpAvailable);
|
|
|
|
m_httpAvailable = m_httpAvailable + 1;
|
|
|
|
m_httpAvailable.signal();
|
|
|
|
}
|
2002-05-30 20:13:16 +04:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-23 16:55:21 +03:00
|
|
|
void
|
|
|
|
CServer::sendOptions(IClient* client) const
|
|
|
|
{
|
|
|
|
// note -- must be locked on entry
|
|
|
|
|
2003-02-22 19:20:23 +03:00
|
|
|
COptionsList optionsList;
|
|
|
|
|
|
|
|
// look up options for client
|
2002-12-23 16:55:21 +03:00
|
|
|
const CConfig::CScreenOptions* options =
|
|
|
|
m_config.getOptions(client->getName());
|
2003-02-22 19:20:23 +03:00
|
|
|
if (options != NULL && options->size() > 0) {
|
|
|
|
// convert options to a more convenient form for sending
|
|
|
|
optionsList.reserve(2 * options->size());
|
|
|
|
for (CConfig::CScreenOptions::const_iterator index = options->begin();
|
|
|
|
index != options->end(); ++index) {
|
|
|
|
optionsList.push_back(index->first);
|
|
|
|
optionsList.push_back(static_cast<UInt32>(index->second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up global options
|
|
|
|
options = m_config.getOptions("");
|
|
|
|
if (options != NULL && options->size() > 0) {
|
|
|
|
// convert options to a more convenient form for sending
|
|
|
|
optionsList.reserve(optionsList.size() + 2 * options->size());
|
|
|
|
for (CConfig::CScreenOptions::const_iterator index = options->begin();
|
|
|
|
index != options->end(); ++index) {
|
|
|
|
optionsList.push_back(index->first);
|
|
|
|
optionsList.push_back(static_cast<UInt32>(index->second));
|
|
|
|
}
|
2002-12-23 16:55:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// send the options
|
|
|
|
client->setOptions(optionsList);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CServer::openPrimaryScreen()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-10 01:22:31 +04:00
|
|
|
assert(m_primaryClient == NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// reset sequence number
|
|
|
|
m_seqNum = 0;
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// canonicalize the primary screen name
|
2002-07-30 22:31:00 +04:00
|
|
|
CString primaryName = m_config.getCanonicalName(getPrimaryScreenName());
|
|
|
|
if (primaryName.empty()) {
|
2002-07-10 01:22:31 +04:00
|
|
|
throw XUnknownClient(getPrimaryScreenName());
|
2002-06-09 21:59:32 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
|
|
|
|
// clear clipboards
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
CClipboardInfo& clipboard = m_clipboards[id];
|
2002-07-30 22:31:00 +04:00
|
|
|
clipboard.m_clipboardOwner = primaryName;
|
2002-07-10 01:22:31 +04:00
|
|
|
clipboard.m_clipboardSeqNum = m_seqNum;
|
|
|
|
if (clipboard.m_clipboard.open(0)) {
|
|
|
|
clipboard.m_clipboard.empty();
|
|
|
|
clipboard.m_clipboard.close();
|
|
|
|
}
|
|
|
|
clipboard.m_clipboardData = clipboard.m_clipboard.marshall();
|
|
|
|
}
|
|
|
|
|
2002-06-03 17:45:30 +04:00
|
|
|
try {
|
2002-07-30 22:31:00 +04:00
|
|
|
// create the primary client
|
|
|
|
m_primaryClient = new CPrimaryClient(m_screenFactory,
|
|
|
|
this, this, primaryName);
|
2002-07-10 01:22:31 +04:00
|
|
|
|
2002-06-03 17:45:30 +04:00
|
|
|
// add connection
|
2002-07-10 01:22:31 +04:00
|
|
|
addConnection(m_primaryClient);
|
|
|
|
m_active = m_primaryClient;
|
|
|
|
|
|
|
|
// open the screen
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "opening primary screen"));
|
2002-07-10 01:22:31 +04:00
|
|
|
m_primaryClient->open();
|
2002-07-11 17:13:37 +04:00
|
|
|
|
|
|
|
// tell it about the active sides
|
|
|
|
m_primaryClient->reconfigure(getActivePrimarySides());
|
2002-12-23 16:55:21 +03:00
|
|
|
|
|
|
|
// tell primary client about its options
|
|
|
|
sendOptions(m_primaryClient);
|
2002-06-03 17:45:30 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
2002-12-23 16:55:21 +03:00
|
|
|
// if m_active is NULL then we haven't added the connection
|
|
|
|
// for the primary client so we don't try to remove it.
|
2002-07-10 01:22:31 +04:00
|
|
|
if (m_active != NULL) {
|
2002-07-30 22:31:00 +04:00
|
|
|
removeConnection(primaryName);
|
2002-06-03 22:53:18 +04:00
|
|
|
}
|
2002-07-10 01:22:31 +04:00
|
|
|
else {
|
|
|
|
delete m_primaryClient;
|
|
|
|
}
|
|
|
|
m_active = NULL;
|
|
|
|
m_primaryClient = NULL;
|
2002-06-03 17:45:30 +04:00
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CServer::closePrimaryScreen()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-10 01:22:31 +04:00
|
|
|
assert(m_primaryClient != NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// close the primary screen
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "closing primary screen"));
|
2002-07-10 01:22:31 +04:00
|
|
|
m_primaryClient->close();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
// remove connection
|
|
|
|
removeConnection(m_primaryClient->getName());
|
|
|
|
m_primaryClient = NULL;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-07-10 01:22:31 +04:00
|
|
|
void
|
|
|
|
CServer::addConnection(IClient* client)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-10 01:22:31 +04:00
|
|
|
assert(client != NULL);
|
|
|
|
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "adding connection \"%s\"", client->getName().c_str()));
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// name must be in our configuration
|
|
|
|
if (!m_config.isScreen(client->getName())) {
|
|
|
|
throw XUnknownClient(client->getName());
|
|
|
|
}
|
2002-06-09 03:24:40 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// can only have one screen with a given name at any given time
|
|
|
|
if (m_clients.count(client->getName()) != 0) {
|
|
|
|
throw XDuplicateClient(client->getName());
|
|
|
|
}
|
2002-05-31 22:09:43 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// save screen info
|
|
|
|
m_clients.insert(std::make_pair(client->getName(), client));
|
|
|
|
LOG((CLOG_DEBUG "added connection \"%s\"", client->getName().c_str()));
|
|
|
|
}
|
|
|
|
runStatusJobs();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CServer::removeConnection(const CString& name)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "removing connection \"%s\"", name.c_str()));
|
2003-03-13 01:34:07 +03:00
|
|
|
bool updateStatus;
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2001-10-25 03:29:29 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// find client
|
|
|
|
CClientList::iterator index = m_clients.find(name);
|
|
|
|
assert(index != m_clients.end());
|
2001-10-25 03:29:29 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// if this is active screen then we have to jump off of it
|
|
|
|
IClient* active = (m_activeSaver != NULL) ? m_activeSaver : m_active;
|
|
|
|
if (active == index->second && active != m_primaryClient) {
|
|
|
|
// record new position (center of primary screen)
|
|
|
|
m_primaryClient->getCursorCenter(m_x, m_y);
|
2001-10-25 03:29:29 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// stop waiting to switch if we were
|
|
|
|
if (active == m_switchScreen) {
|
|
|
|
clearSwitchState();
|
|
|
|
}
|
2003-02-23 00:53:25 +03:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// don't notify active screen since it probably already
|
|
|
|
// disconnected.
|
|
|
|
LOG((CLOG_INFO "jump from \"%s\" to \"%s\" at %d,%d", active->getName().c_str(), m_primaryClient->getName().c_str(), m_x, m_y));
|
2001-10-25 03:29:29 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// cut over
|
|
|
|
m_active = m_primaryClient;
|
2001-10-25 03:29:29 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// enter new screen (unless we already have because of the
|
|
|
|
// screen saver)
|
|
|
|
if (m_activeSaver == NULL) {
|
|
|
|
m_primaryClient->enter(m_x, m_y, m_seqNum,
|
|
|
|
m_primaryClient->getToggleMask(), false);
|
|
|
|
}
|
2002-06-24 03:24:22 +04:00
|
|
|
}
|
2001-10-25 03:29:29 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// if this screen had the cursor when the screen saver activated
|
|
|
|
// then we can't switch back to it when the screen saver
|
|
|
|
// deactivates.
|
|
|
|
if (m_activeSaver == index->second) {
|
|
|
|
m_activeSaver = NULL;
|
|
|
|
}
|
2002-06-22 23:20:21 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// done with client
|
|
|
|
delete index->second;
|
|
|
|
m_clients.erase(index);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
// remove any thread for this client
|
|
|
|
m_clientThreads.erase(name);
|
|
|
|
|
|
|
|
updateStatus = (m_clients.size() <= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateStatus) {
|
|
|
|
runStatusJobs();
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-04-27 18:19:53 +04:00
|
|
|
|
|
|
|
|
2002-10-18 01:37:31 +04:00
|
|
|
//
|
|
|
|
// CServer::CClipboardInfo
|
|
|
|
//
|
|
|
|
|
|
|
|
CString
|
|
|
|
CServer::XServerRethrow::getWhat() const throw()
|
|
|
|
{
|
|
|
|
return format("XServerRethrow", "child thread failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-27 18:19:53 +04:00
|
|
|
//
|
2002-04-29 17:31:44 +04:00
|
|
|
// CServer::CClipboardInfo
|
2002-04-27 18:19:53 +04:00
|
|
|
//
|
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
CServer::CClipboardInfo::CClipboardInfo() :
|
2002-06-11 02:06:45 +04:00
|
|
|
m_clipboard(),
|
|
|
|
m_clipboardData(),
|
|
|
|
m_clipboardOwner(),
|
2002-07-10 01:22:31 +04:00
|
|
|
m_clipboardSeqNum(0)
|
2002-04-27 18:19:53 +04:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|