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