blob: db142fe4f3a57a450fb2a725f584acac3b57b6d2 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Single-Threaded VNC Server implementation
20
21
22// Note about how sockets get closed:
23//
24// Closing sockets to clients is non-trivial because the code which calls
25// VNCServerST must explicitly know about all the sockets (so that it can block
26// on them appropriately). However, VNCServerST may want to close clients for
27// a number of reasons, and from a variety of entry points. The simplest is
28// when processSocketEvent() is called for a client, and the remote end has
29// closed its socket. A more complex reason is when processSocketEvent() is
30// called for a client which has just sent a ClientInit with the shared flag
31// set to false - in this case we want to close all other clients. Yet another
32// reason for disconnecting clients is when the desktop size has changed as a
33// result of a call to setPixelBuffer().
34//
35// The responsibility for creating and deleting sockets is entirely with the
36// calling code. When VNCServerST wants to close a connection to a client it
37// calls the VNCSConnectionST's close() method which calls shutdown() on the
38// socket. Eventually the calling code will notice that the socket has been
39// shut down and call removeSocket() so that we can delete the
40// VNCSConnectionST. Note that the socket must not be deleted by the calling
41// code until after removeSocket() has been called.
42//
43// One minor complication is that we don't allocate a VNCSConnectionST object
44// for a blacklisted host (since we want to minimise the resources used for
45// dealing with such a connection). In order to properly implement the
46// getSockets function, we must maintain a separate closingSockets list,
47// otherwise blacklisted connections might be "forgotten".
48
49
Pierre Ossman559a2e82012-01-23 15:54:11 +000050#include <assert.h>
Pierre Ossmanf99c5712009-03-13 14:41:27 +000051#include <stdlib.h>
52
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053#include <rfb/ServerCore.h>
54#include <rfb/VNCServerST.h>
55#include <rfb/VNCSConnectionST.h>
56#include <rfb/ComparingUpdateTracker.h>
Adam Tkaca6578bf2010-04-23 14:07:41 +000057#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058#include <rfb/KeyRemapper.h>
59#include <rfb/util.h>
60
61#include <rdr/types.h>
62
63using namespace rfb;
64
65static LogWriter slog("VNCServerST");
66LogWriter VNCServerST::connectionsLog("Connections");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000068rfb::IntParameter deferUpdateTime("DeferUpdate",
DRC4548f302011-12-22 15:57:59 +000069 "Time in milliseconds to defer updates",1);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000070
71rfb::BoolParameter alwaysSetDeferUpdateTimer("AlwaysSetDeferUpdateTimer",
72 "Always reset the defer update timer on every change",false);
73
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074//
75// -=- VNCServerST Implementation
76//
77
78// -=- Constructors/Destructor
79
Adam Tkaca6578bf2010-04-23 14:07:41 +000080VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000081 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
82 blockCounter(0), pb(0),
Adam Tkacd36b6262009-09-04 10:57:20 +000083 name(strDup(name_)), pointerClient(0), comparer(0),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
86 useEconomicTranslate(false),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000087 lastConnectionTime(0), disableclients(false),
88 deferTimer(this), deferPending(false)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089{
90 lastUserInputTime = lastDisconnectTime = time(0);
91 slog.debug("creating single-threaded server %s", name.buf);
92}
93
94VNCServerST::~VNCServerST()
95{
96 slog.debug("shutting down server %s", name.buf);
97
98 // Close any active clients, with appropriate logging & cleanup
99 closeClients("Server shutdown");
100
101 // Delete all the clients, and their sockets, and any closing sockets
102 // NB: Deleting a client implicitly removes it from the clients list
103 while (!clients.empty()) {
104 delete clients.front();
105 }
106
107 // Stop the desktop object if active, *only* after deleting all clients!
108 if (desktopStarted) {
109 desktopStarted = false;
110 desktop->stop();
111 }
112
113 delete comparer;
114}
115
116
117// SocketServer methods
118
119void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
120{
121 // - Check the connection isn't black-marked
122 // *** do this in getSecurity instead?
123 CharArray address(sock->getPeerAddress());
124 if (blHosts->isBlackmarked(address.buf)) {
125 connectionsLog.error("blacklisted: %s", address.buf);
126 try {
127 SConnection::writeConnFailedFromScratch("Too many security failures",
128 &sock->outStream());
129 } catch (rdr::Exception&) {
130 }
131 sock->shutdown();
132 closingSockets.push_back(sock);
133 return;
134 }
135
136 if (clients.empty()) {
137 lastConnectionTime = time(0);
138 }
139
140 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
141 client->init();
142}
143
144void VNCServerST::removeSocket(network::Socket* sock) {
145 // - If the socket has resources allocated to it, delete them
146 std::list<VNCSConnectionST*>::iterator ci;
147 for (ci = clients.begin(); ci != clients.end(); ci++) {
148 if ((*ci)->getSock() == sock) {
149 // - Delete the per-Socket resources
150 delete *ci;
151
152 // - Check that the desktop object is still required
153 if (authClientCount() == 0 && desktopStarted) {
154 slog.debug("no authenticated clients - stopping desktop");
155 desktopStarted = false;
156 desktop->stop();
157 }
158 return;
159 }
160 }
161
162 // - If the Socket has no resources, it may have been a closingSocket
163 closingSockets.remove(sock);
164}
165
166void VNCServerST::processSocketEvent(network::Socket* sock)
167{
168 // - Find the appropriate VNCSConnectionST and process the event
169 std::list<VNCSConnectionST*>::iterator ci;
170 for (ci = clients.begin(); ci != clients.end(); ci++) {
171 if ((*ci)->getSock() == sock) {
172 (*ci)->processMessages();
173 return;
174 }
175 }
176 throw rdr::Exception("invalid Socket in VNCServerST");
177}
178
179int VNCServerST::checkTimeouts()
180{
181 int timeout = 0;
182 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000183
184 soonestTimeout(&timeout, Timer::checkTimeouts());
185
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
187 ci_next = ci; ci_next++;
188 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
189 }
190
191 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000192 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 // Check MaxDisconnectionTime
195 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
196 if (now < lastDisconnectTime) {
197 // Someone must have set the time backwards.
198 slog.info("Time has gone backwards - resetting lastDisconnectTime");
199 lastDisconnectTime = now;
200 }
201 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
202 if (timeLeft < -60) {
203 // Someone must have set the time forwards.
204 slog.info("Time has gone forwards - resetting lastDisconnectTime");
205 lastDisconnectTime = now;
206 timeLeft = rfb::Server::maxDisconnectionTime;
207 }
208 if (timeLeft <= 0) {
209 slog.info("MaxDisconnectionTime reached, exiting");
210 exit(0);
211 }
212 soonestTimeout(&timeout, timeLeft * 1000);
213 }
214
215 // Check MaxConnectionTime
216 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
217 if (now < lastConnectionTime) {
218 // Someone must have set the time backwards.
219 slog.info("Time has gone backwards - resetting lastConnectionTime");
220 lastConnectionTime = now;
221 }
222 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
223 if (timeLeft < -60) {
224 // Someone must have set the time forwards.
225 slog.info("Time has gone forwards - resetting lastConnectionTime");
226 lastConnectionTime = now;
227 timeLeft = rfb::Server::maxConnectionTime;
228 }
229 if (timeLeft <= 0) {
230 slog.info("MaxConnectionTime reached, exiting");
231 exit(0);
232 }
233 soonestTimeout(&timeout, timeLeft * 1000);
234 }
235
236
237 // Check MaxIdleTime
238 if (rfb::Server::maxIdleTime) {
239 if (now < lastUserInputTime) {
240 // Someone must have set the time backwards.
241 slog.info("Time has gone backwards - resetting lastUserInputTime");
242 lastUserInputTime = now;
243 }
244 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
245 if (timeLeft < -60) {
246 // Someone must have set the time forwards.
247 slog.info("Time has gone forwards - resetting lastUserInputTime");
248 lastUserInputTime = now;
249 timeLeft = rfb::Server::maxIdleTime;
250 }
251 if (timeLeft <= 0) {
252 slog.info("MaxIdleTime reached, exiting");
253 exit(0);
254 }
255 soonestTimeout(&timeout, timeLeft * 1000);
256 }
257
258 return timeout;
259}
260
261
262// VNCServer methods
263
Pierre Ossman559a2e82012-01-23 15:54:11 +0000264void VNCServerST::blockUpdates()
265{
266 blockCounter++;
267}
268
269void VNCServerST::unblockUpdates()
270{
271 assert(blockCounter > 0);
272
273 blockCounter--;
274
275 // Flush out any updates we might have blocked
276 if (blockCounter == 0)
277 tryUpdate();
278}
279
Pierre Ossman04e62db2009-03-23 16:57:07 +0000280void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000281{
282 pb = pb_;
283 delete comparer;
284 comparer = 0;
285
Pierre Ossman04e62db2009-03-23 16:57:07 +0000286 screenLayout = layout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000287
Pierre Ossman04e62db2009-03-23 16:57:07 +0000288 if (!pb) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000289 if (desktopStarted)
290 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Pierre Ossman04e62db2009-03-23 16:57:07 +0000291 return;
292 }
293
294 comparer = new ComparingUpdateTracker(pb);
295 cursor.setPF(pb->getPF());
296 renderedCursor.setPF(pb->getPF());
297
298 // Make sure that we have at least one screen
299 if (screenLayout.num_screens() == 0)
300 screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
301
302 std::list<VNCSConnectionST*>::iterator ci, ci_next;
303 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
304 ci_next = ci; ci_next++;
305 (*ci)->pixelBufferChange();
306 // Since the new pixel buffer means an ExtendedDesktopSize needs to
307 // be sent anyway, we don't need to call screenLayoutChange.
308 }
309}
310
311void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
312{
313 ScreenSet layout;
314
315 if (!pb_) {
316 if (desktopStarted)
317 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
318 return;
319 }
320
321 layout = screenLayout;
322
323 // Check that the screen layout is still valid
324 if (!layout.validate(pb_->width(), pb_->height())) {
325 Rect fbRect;
326 ScreenSet::iterator iter, iter_next;
327
328 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
329
330 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
331 iter_next = iter; ++iter_next;
332 if (iter->dimensions.enclosed_by(fbRect))
333 continue;
334 iter->dimensions = iter->dimensions.intersect(fbRect);
335 if (iter->dimensions.is_empty()) {
336 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
337 (int)iter->id, (unsigned)iter->id);
338 layout.remove_screen(iter->id);
339 }
340 }
341 }
342
343 setPixelBuffer(pb_, layout);
344}
345
346void VNCServerST::setScreenLayout(const ScreenSet& layout)
347{
348 if (!pb)
349 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
350 if (!layout.validate(pb->width(), pb->height()))
351 throw Exception("setScreenLayout: invalid screen layout");
352
Pierre Ossmandf453202009-04-02 14:26:45 +0000353 screenLayout = layout;
354
Pierre Ossman04e62db2009-03-23 16:57:07 +0000355 std::list<VNCSConnectionST*>::iterator ci, ci_next;
356 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
357 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000358 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000359 }
360}
361
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000362void VNCServerST::bell()
363{
364 std::list<VNCSConnectionST*>::iterator ci, ci_next;
365 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
366 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000367 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000368 }
369}
370
371void VNCServerST::serverCutText(const char* str, int len)
372{
373 std::list<VNCSConnectionST*>::iterator ci, ci_next;
374 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
375 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000376 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000377 }
378}
379
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000380void VNCServerST::setName(const char* name_)
381{
Adam Tkacd36b6262009-09-04 10:57:20 +0000382 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000383 std::list<VNCSConnectionST*>::iterator ci, ci_next;
384 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
385 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000386 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000387 }
388}
389
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000390void VNCServerST::add_changed(const Region& region)
391{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000392 if (comparer == NULL)
393 return;
394
395 comparer->add_changed(region);
396 startDefer();
397 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000398}
399
400void VNCServerST::add_copied(const Region& dest, const Point& delta)
401{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000402 if (comparer == NULL)
403 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000404
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000405 comparer->add_copied(dest, delta);
406 startDefer();
407 tryUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000408}
409
410void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
411 void* data, void* mask)
412{
413 cursor.hotspot = newHotspot;
414 cursor.setSize(width, height);
415 memcpy(cursor.data, data, cursor.dataLen());
416 memcpy(cursor.mask.buf, mask, cursor.maskLen());
417
418 cursor.crop();
419
420 renderedCursorInvalid = true;
421
422 std::list<VNCSConnectionST*>::iterator ci, ci_next;
423 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
424 ci_next = ci; ci_next++;
425 (*ci)->renderedCursorChange();
426 (*ci)->setCursorOrClose();
427 }
428}
429
430void VNCServerST::setCursorPos(const Point& pos)
431{
432 if (!cursorPos.equals(pos)) {
433 cursorPos = pos;
434 renderedCursorInvalid = true;
435 std::list<VNCSConnectionST*>::iterator ci;
436 for (ci = clients.begin(); ci != clients.end(); ci++)
437 (*ci)->renderedCursorChange();
438 }
439}
440
441// Other public methods
442
443void VNCServerST::approveConnection(network::Socket* sock, bool accept,
444 const char* reason)
445{
446 std::list<VNCSConnectionST*>::iterator ci;
447 for (ci = clients.begin(); ci != clients.end(); ci++) {
448 if ((*ci)->getSock() == sock) {
449 (*ci)->approveConnectionOrClose(accept, reason);
450 return;
451 }
452 }
453}
454
455void VNCServerST::closeClients(const char* reason, network::Socket* except)
456{
457 std::list<VNCSConnectionST*>::iterator i, next_i;
458 for (i=clients.begin(); i!=clients.end(); i=next_i) {
459 next_i = i; next_i++;
460 if ((*i)->getSock() != except)
461 (*i)->close(reason);
462 }
463}
464
465void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
466{
467 sockets->clear();
468 std::list<VNCSConnectionST*>::iterator ci;
469 for (ci = clients.begin(); ci != clients.end(); ci++) {
470 sockets->push_back((*ci)->getSock());
471 }
472 std::list<network::Socket*>::iterator si;
473 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
474 sockets->push_back(*si);
475 }
476}
477
478SConnection* VNCServerST::getSConnection(network::Socket* sock) {
479 std::list<VNCSConnectionST*>::iterator ci;
480 for (ci = clients.begin(); ci != clients.end(); ci++) {
481 if ((*ci)->getSock() == sock)
482 return *ci;
483 }
484 return 0;
485}
486
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000487bool VNCServerST::handleTimeout(Timer* t)
488{
489 if (t != &deferTimer)
490 return false;
491
492 tryUpdate();
493
494 return false;
495}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000496
497// -=- Internal methods
498
499void VNCServerST::startDesktop()
500{
501 if (!desktopStarted) {
502 slog.debug("starting desktop");
503 desktop->start(this);
504 desktopStarted = true;
505 if (!pb)
506 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
507 }
508}
509
510int VNCServerST::authClientCount() {
511 int count = 0;
512 std::list<VNCSConnectionST*>::iterator ci;
513 for (ci = clients.begin(); ci != clients.end(); ci++) {
514 if ((*ci)->authenticated())
515 count++;
516 }
517 return count;
518}
519
520inline bool VNCServerST::needRenderedCursor()
521{
522 std::list<VNCSConnectionST*>::iterator ci;
523 for (ci = clients.begin(); ci != clients.end(); ci++)
524 if ((*ci)->needRenderedCursor()) return true;
525 return false;
526}
527
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000528inline void VNCServerST::startDefer()
529{
530 if (deferUpdateTime == 0)
531 return;
532
533 if (deferPending && !alwaysSetDeferUpdateTimer)
534 return;
535
536 gettimeofday(&deferStart, NULL);
537 deferTimer.start(deferUpdateTime);
538
539 deferPending = true;
540}
541
542inline bool VNCServerST::checkDefer()
543{
544 if (!deferPending)
545 return true;
546
547 if (msSince(&deferStart) >= deferUpdateTime)
548 return true;
549
550 return false;
551}
552
553void VNCServerST::tryUpdate()
554{
555 std::list<VNCSConnectionST*>::iterator ci, ci_next;
556
Pierre Ossman559a2e82012-01-23 15:54:11 +0000557 if (blockCounter > 0)
558 return;
559
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000560 if (!checkDefer())
561 return;
562
563 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
564 ci_next = ci; ci_next++;
565 (*ci)->writeFramebufferUpdateOrClose();
566 }
567}
568
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000569// checkUpdate() is called just before sending an update. It checks to see
570// what updates are pending and propagates them to the update tracker for each
571// client. It uses the ComparingUpdateTracker's compare() method to filter out
572// areas of the screen which haven't actually changed. It also checks the
573// state of the (server-side) rendered cursor, if necessary rendering it again
574// with the correct background.
575
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000576bool VNCServerST::checkUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000577{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000578 UpdateInfo ui;
579 comparer->getUpdateInfo(&ui, pb->getRect());
580
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000581 bool renderCursor = needRenderedCursor();
582
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000583 if (ui.is_empty() && !(renderCursor && renderedCursorInvalid))
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000584 return true;
585
Pierre Ossman559a2e82012-01-23 15:54:11 +0000586 // Block clients as the frame buffer cannot be safely accessed
587 if (blockCounter > 0)
588 return false;
589
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000590 // Block client from updating if we are currently deferring updates
591 if (!checkDefer())
592 return false;
593
594 deferPending = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000595
Pierre Ossman02e43d72009-03-05 11:57:11 +0000596 Region toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000597
598 if (renderCursor) {
599 Rect clippedCursorRect
600 = cursor.getRect(cursorTL()).intersect(pb->getRect());
601
602 if (!renderedCursorInvalid && (toCheck.intersect(clippedCursorRect)
603 .is_empty())) {
604 renderCursor = false;
605 } else {
606 renderedCursorTL = clippedCursorRect.tl;
607 renderedCursor.setSize(clippedCursorRect.width(),
608 clippedCursorRect.height());
609 toCheck.assign_union(clippedCursorRect);
610 }
611 }
612
613 pb->grabRegion(toCheck);
614
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000615 if (getComparerState())
616 comparer->enable();
617 else
618 comparer->disable();
619
620 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000621 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000622
623 if (renderCursor) {
624 pb->getImage(renderedCursor.data,
625 renderedCursor.getRect(renderedCursorTL));
626 renderedCursor.maskRect(cursor.getRect(cursorTL()
627 .subtract(renderedCursorTL)),
628 cursor.data, cursor.mask.buf);
629 renderedCursorInvalid = false;
630 }
631
632 std::list<VNCSConnectionST*>::iterator ci, ci_next;
633 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
634 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000635 (*ci)->add_copied(ui.copied, ui.copy_delta);
636 (*ci)->add_changed(ui.changed);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000637 }
638
639 comparer->clear();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000640
641 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000642}
643
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000644void VNCServerST::getConnInfo(ListConnInfo * listConn)
645{
646 listConn->Clear();
647 listConn->setDisable(getDisable());
648 if (clients.empty())
649 return;
650 std::list<VNCSConnectionST*>::iterator i;
651 for (i = clients.begin(); i != clients.end(); i++)
652 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
653 (*i)->getStartTime(), (*i)->getStatus());
654}
655
656void VNCServerST::setConnStatus(ListConnInfo* listConn)
657{
658 setDisable(listConn->getDisable());
659 if (listConn->Empty() || clients.empty()) return;
660 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
661 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
662 std::list<VNCSConnectionST*>::iterator i;
663 for (i = clients.begin(); i != clients.end(); i++) {
664 if ((*i) == conn) {
665 int status = listConn->iGetStatus();
666 if (status == 3) {
667 (*i)->close(0);
668 } else {
669 (*i)->setStatus(status);
670 }
671 break;
672 }
673 }
674 }
675}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000676
Pierre Ossman04e62db2009-03-23 16:57:07 +0000677void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
678{
679 std::list<VNCSConnectionST*>::iterator ci, ci_next;
680 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
681 ci_next = ci; ci_next++;
682 if ((*ci) == requester)
683 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000684 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000685 }
686}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000687
688bool VNCServerST::getComparerState()
689{
690 if (rfb::Server::compareFB == 0)
691 return false;
692 if (rfb::Server::compareFB != 2)
693 return true;
694
695 std::list<VNCSConnectionST*>::iterator ci, ci_next;
696 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
697 ci_next = ci; ci_next++;
698 if ((*ci)->getComparerState())
699 return true;
700 }
701 return false;
702}