blob: bcc7f2343d6a66e8343d4299b8a46190b3c96317 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00002 * Copyright 2009-2011 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
Pierre Ossmana830bec2011-11-08 12:12:02 +000020#include <network/TcpSocket.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021#include <rfb/VNCSConnectionST.h>
22#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000023#include <rfb/Security.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000024#include <rfb/screenTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025#include <rfb/ServerCore.h>
26#include <rfb/ComparingUpdateTracker.h>
27#include <rfb/KeyRemapper.h>
28#define XK_MISCELLANY
29#define XK_XKB_KEYS
30#include <rfb/keysymdef.h>
31
32using namespace rfb;
33
34static LogWriter vlog("VNCSConnST");
35
36VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
37 bool reverse)
Pierre Ossmana3ac01e2011-11-07 21:13:54 +000038 : SConnection(reverse), sock(s), inProcessMessages(false), server(server_),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039 updates(false), image_getter(server->useEconomicTranslate),
40 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000041 updateTimer(this), pointerEventTime(0),
42 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043{
44 setStreams(&sock->inStream(), &sock->outStream());
45 peerEndpoint.buf = sock->getPeerEndpoint();
46 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
47
48 // Configure the socket
49 setSocketTimeouts();
50 lastEventTime = time(0);
51
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052 server->clients.push_front(this);
53}
54
55
56VNCSConnectionST::~VNCSConnectionST()
57{
58 // If we reach here then VNCServerST is deleting us!
59 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
60 peerEndpoint.buf,
61 (closeReason.buf) ? closeReason.buf : "");
62
63 // Release any keys the client still had pressed
64 std::set<rdr::U32>::iterator i;
65 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
66 server->desktop->keyEvent(*i, false);
67 if (server->pointerClient == this)
68 server->pointerClient = 0;
69
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070 // Remove this client from the server
71 server->clients.remove(this);
72
73}
74
75
76// Methods called from VNCServerST
77
78bool VNCSConnectionST::init()
79{
80 try {
81 initialiseProtocol();
82 } catch (rdr::Exception& e) {
83 close(e.str());
84 return false;
85 }
86 return true;
87}
88
89void VNCSConnectionST::close(const char* reason)
90{
91 // Log the reason for the close
92 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +000093 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 else
95 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
96
97 if (authenticated()) {
98 server->lastDisconnectTime = time(0);
99 }
100
101 // Just shutdown the socket and mark our state as closing. Eventually the
102 // calling code will call VNCServerST's removeSocket() method causing us to
103 // be deleted.
104 sock->shutdown();
105 setState(RFBSTATE_CLOSING);
106}
107
108
109void VNCSConnectionST::processMessages()
110{
111 if (state() == RFBSTATE_CLOSING) return;
112 try {
113 // - Now set appropriate socket timeouts and process data
114 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000115
116 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117
Pierre Ossmana830bec2011-11-08 12:12:02 +0000118 // Get the underlying TCP layer to build large packets if we send
119 // multiple small responses.
120 network::TcpSocket::cork(sock->getFd(), true);
121
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122 while (getInStream()->checkNoWait(1)) {
123 processMsg();
124 }
125
Pierre Ossmana830bec2011-11-08 12:12:02 +0000126 // Flush out everything in case we go idle after this.
127 network::TcpSocket::cork(sock->getFd(), false);
128
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000129 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000130
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000131 // If there were anything requiring an update, try to send it here.
132 // We wait until now with this to aggregate responses and to give
133 // higher priority to user actions such as keyboard and pointer events.
134 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 } catch (rdr::EndOfStream&) {
136 close("Clean disconnection");
137 } catch (rdr::Exception &e) {
138 close(e.str());
139 }
140}
141
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142void VNCSConnectionST::pixelBufferChange()
143{
144 try {
145 if (!authenticated()) return;
146 if (cp.width && cp.height && (server->pb->width() != cp.width ||
147 server->pb->height() != cp.height))
148 {
149 // We need to clip the next update to the new size, but also add any
150 // extra bits if it's bigger. If we wanted to do this exactly, something
151 // like the code below would do it, but at the moment we just update the
152 // entire new size. However, we do need to clip the renderedCursorRect
153 // because that might be added to updates in writeFramebufferUpdate().
154
155 //updates.intersect(server->pb->getRect());
156 //
157 //if (server->pb->width() > cp.width)
158 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
159 // server->pb->height()));
160 //if (server->pb->height() > cp.height)
161 // updates.add_changed(Rect(0, cp.height, cp.width,
162 // server->pb->height()));
163
164 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
165
166 cp.width = server->pb->width();
167 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000168 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000170 // We should only send EDS to client asking for both
171 if (!writer()->writeExtendedDesktopSize()) {
172 if (!writer()->writeSetDesktopSize()) {
173 close("Client does not support desktop resize");
174 return;
175 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176 }
177 }
178 }
179 // Just update the whole screen at the moment because we're too lazy to
180 // work out what's actually changed.
181 updates.clear();
182 updates.add_changed(server->pb->getRect());
183 vlog.debug("pixel buffer changed - re-initialising image getter");
184 image_getter.init(server->pb, cp.pf(), writer());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000185 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 } catch(rdr::Exception &e) {
187 close(e.str());
188 }
189}
190
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000191void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000192{
193 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000194 writeFramebufferUpdate();
195 } catch(rdr::Exception &e) {
196 close(e.str());
197 }
198}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000199
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000200void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
201{
202 try {
203 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000204 } catch(rdr::Exception &e) {
205 close(e.str());
206 }
207}
208
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209void VNCSConnectionST::setColourMapEntriesOrClose(int firstColour,int nColours)
210{
211 try {
212 setColourMapEntries(firstColour, nColours);
213 } catch(rdr::Exception& e) {
214 close(e.str());
215 }
216}
217
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000218void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000219{
220 try {
221 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
222 } catch(rdr::Exception& e) {
223 close(e.str());
224 }
225}
226
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000227void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000228{
229 try {
230 if (!(accessRights & AccessCutText)) return;
231 if (!rfb::Server::sendCutText) return;
232 if (state() == RFBSTATE_NORMAL)
233 writer()->writeServerCutText(str, len);
234 } catch(rdr::Exception& e) {
235 close(e.str());
236 }
237}
238
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000239
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000240void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000241{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000242 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000243 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000244 } catch(rdr::Exception& e) {
245 close(e.str());
246 }
247}
248
249
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000250void VNCSConnectionST::setCursorOrClose()
251{
252 try {
253 setCursor();
254 } catch(rdr::Exception& e) {
255 close(e.str());
256 }
257}
258
259
260int VNCSConnectionST::checkIdleTimeout()
261{
262 int idleTimeout = rfb::Server::idleTimeout;
263 if (idleTimeout == 0) return 0;
264 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
265 idleTimeout = 15; // minimum of 15 seconds while authenticating
266 time_t now = time(0);
267 if (now < lastEventTime) {
268 // Someone must have set the time backwards. Set lastEventTime so that the
269 // idleTimeout will count from now.
270 vlog.info("Time has gone backwards - resetting idle timeout");
271 lastEventTime = now;
272 }
273 int timeLeft = lastEventTime + idleTimeout - now;
274 if (timeLeft < -60) {
275 // Our callback is over a minute late - someone must have set the time
276 // forwards. Set lastEventTime so that the idleTimeout will count from
277 // now.
278 vlog.info("Time has gone forwards - resetting idle timeout");
279 lastEventTime = now;
280 return secsToMillis(idleTimeout);
281 }
282 if (timeLeft <= 0) {
283 close("Idle timeout");
284 return 0;
285 }
286 return secsToMillis(timeLeft);
287}
288
289// renderedCursorChange() is called whenever the server-side rendered cursor
290// changes shape or position. It ensures that the next update will clean up
291// the old rendered cursor and if necessary draw the new rendered cursor.
292
293void VNCSConnectionST::renderedCursorChange()
294{
295 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000296 if (!renderedCursorRect.is_empty())
297 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000298 if (needRenderedCursor())
299 drawRenderedCursor = true;
300}
301
302// needRenderedCursor() returns true if this client needs the server-side
303// rendered cursor. This may be because it does not support local cursor or
304// because the current cursor position has not been set by this client.
305// Unfortunately we can't know for sure when the current cursor position has
306// been set by this client. We guess that this is the case when the current
307// cursor position is the same as the last pointer event from this client, or
308// if it is a very short time since this client's last pointer event (up to a
309// second). [ Ideally we should do finer-grained timing here and make the time
310// configurable, but I don't think it's that important. ]
311
312bool VNCSConnectionST::needRenderedCursor()
313{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000314 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000315 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000316 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000317}
318
319
320void VNCSConnectionST::approveConnectionOrClose(bool accept,
321 const char* reason)
322{
323 try {
324 approveConnection(accept, reason);
325 } catch (rdr::Exception& e) {
326 close(e.str());
327 }
328}
329
330
331
332// -=- Callbacks from SConnection
333
334void VNCSConnectionST::authSuccess()
335{
336 lastEventTime = time(0);
337
338 server->startDesktop();
339
340 // - Set the connection parameters appropriately
341 cp.width = server->pb->width();
342 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000343 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000344 cp.setName(server->getName());
345
346 // - Set the default pixel format
347 cp.setPF(server->pb->getPF());
348 char buffer[256];
349 cp.pf().print(buffer, 256);
350 vlog.info("Server default pixel format %s", buffer);
351 image_getter.init(server->pb, cp.pf(), 0);
352
353 // - Mark the entire display as "dirty"
354 updates.add_changed(server->pb->getRect());
355 startTime = time(0);
356}
357
358void VNCSConnectionST::queryConnection(const char* userName)
359{
360 // - Authentication succeeded - clear from blacklist
361 CharArray name; name.buf = sock->getPeerAddress();
362 server->blHosts->clearBlackmark(name.buf);
363
364 // - Special case to provide a more useful error message
365 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
366 server->authClientCount() > 0) {
367 approveConnection(false, "The server is already in use");
368 return;
369 }
370
371 // - Does the client have the right to bypass the query?
372 if (reverseConnection ||
373 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
374 (accessRights & AccessNoQuery))
375 {
376 approveConnection(true);
377 return;
378 }
379
380 // - Get the server to display an Accept/Reject dialog, if required
381 // If a dialog is displayed, the result will be PENDING, and the
382 // server will call approveConnection at a later time
383 CharArray reason;
384 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
385 &reason.buf);
386 if (qr == VNCServerST::PENDING)
387 return;
388
389 // - If server returns ACCEPT/REJECT then pass result to SConnection
390 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
391}
392
393void VNCSConnectionST::clientInit(bool shared)
394{
395 lastEventTime = time(0);
396 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
397 if (rfb::Server::neverShared) shared = false;
398 if (!shared) {
399 if (rfb::Server::disconnectClients) {
400 // - Close all the other connected clients
401 vlog.debug("non-shared connection - closing clients");
402 server->closeClients("Non-shared connection requested", getSock());
403 } else {
404 // - Refuse this connection if there are existing clients, in addition to
405 // this one
406 if (server->authClientCount() > 1) {
407 close("Server is already in use");
408 return;
409 }
410 }
411 }
412 SConnection::clientInit(shared);
413}
414
415void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
416{
417 SConnection::setPixelFormat(pf);
418 char buffer[256];
419 pf.print(buffer, 256);
420 vlog.info("Client pixel format %s", buffer);
421 image_getter.init(server->pb, pf, writer());
422 setCursor();
423}
424
425void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
426{
427 pointerEventTime = lastEventTime = time(0);
428 server->lastUserInputTime = lastEventTime;
429 if (!(accessRights & AccessPtrEvents)) return;
430 if (!rfb::Server::acceptPointerEvents) return;
431 if (!server->pointerClient || server->pointerClient == this) {
432 pointerEventPos = pos;
433 if (buttonMask)
434 server->pointerClient = this;
435 else
436 server->pointerClient = 0;
437 server->desktop->pointerEvent(pointerEventPos, buttonMask);
438 }
439}
440
441
442class VNCSConnectionSTShiftPresser {
443public:
444 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
445 : desktop(desktop_), pressed(false) {}
446 ~VNCSConnectionSTShiftPresser() {
447 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
448 }
449 void press() {
450 desktop->keyEvent(XK_Shift_L, true);
451 pressed = true;
452 }
453 SDesktop* desktop;
454 bool pressed;
455};
456
457// keyEvent() - record in the pressedKeys which keys were pressed. Allow
458// multiple down events (for autorepeat), but only allow a single up event.
459void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
460 lastEventTime = time(0);
461 server->lastUserInputTime = lastEventTime;
462 if (!(accessRights & AccessKeyEvents)) return;
463 if (!rfb::Server::acceptKeyEvents) return;
464
465 // Remap the key if required
466 if (server->keyRemapper)
467 key = server->keyRemapper->remapKey(key);
468
469 // Turn ISO_Left_Tab into shifted Tab.
470 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
471 if (key == XK_ISO_Left_Tab) {
472 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
473 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
474 shiftPresser.press();
475 key = XK_Tab;
476 }
477
478 if (down) {
479 pressedKeys.insert(key);
480 } else {
481 if (!pressedKeys.erase(key)) return;
482 }
483 server->desktop->keyEvent(key, down);
484}
485
486void VNCSConnectionST::clientCutText(const char* str, int len)
487{
488 if (!(accessRights & AccessCutText)) return;
489 if (!rfb::Server::acceptCutText) return;
490 server->desktop->clientCutText(str, len);
491}
492
493void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
494{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000495 Rect safeRect;
496
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000497 if (!(accessRights & AccessView)) return;
498
499 SConnection::framebufferUpdateRequest(r, incremental);
500
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000501 // Check that the client isn't sending crappy requests
502 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
503 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
504 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000505 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
506 } else {
507 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000508 }
509
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000510 // Just update the requested region.
511 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000512 Region reqRgn(r);
513 requested.assign_union(reqRgn);
514
515 if (!incremental) {
516 // Non-incremental update - treat as if area requested has changed
517 updates.add_changed(reqRgn);
518 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000519
520 // And send the screen layout to the client (which, unlike the
521 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000522 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000523
524 // We do not send a DesktopSize since it only contains the
525 // framebuffer size (which the client already should know) and
526 // because some clients don't handle extra DesktopSize events
527 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000528 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000529}
530
Pierre Ossman34bb0612009-03-21 21:16:14 +0000531void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
532 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000533{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000534 unsigned int result;
535
536 // Don't bother the desktop with an invalid configuration
537 if (!layout.validate(fb_width, fb_height)) {
538 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
539 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000540 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000541 return;
542 }
543
544 // FIXME: the desktop will call back to VNCServerST and an extra set
545 // of ExtendedDesktopSize messages will be sent. This is okay
546 // protocol-wise, but unnecessary.
547 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
548
Pierre Ossman04e62db2009-03-23 16:57:07 +0000549 writer()->writeExtendedDesktopSize(reasonClient, result,
550 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000551
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000552 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000553 if (result == resultSuccess) {
554 if (server->screenLayout != layout)
555 throw Exception("Desktop configured a different screen layout than requested");
556 server->notifyScreenLayoutChange(this);
557 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000558
559 // but always send back a reply to the requesting client
560 // (do this last as it might throw an exception on socket errors)
561 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000562}
563
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000564void VNCSConnectionST::setInitialColourMap()
565{
566 setColourMapEntries(0, 0);
567}
568
569// supportsLocalCursor() is called whenever the status of
570// cp.supportsLocalCursor has changed. If the client does now support local
571// cursor, we make sure that the old server-side rendered cursor is cleaned up
572// and the cursor is sent to the client.
573
574void VNCSConnectionST::supportsLocalCursor()
575{
576 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000577 if (!renderedCursorRect.is_empty())
578 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000579 drawRenderedCursor = false;
580 setCursor();
581 }
582}
583
584void VNCSConnectionST::writeSetCursorCallback()
585{
586 if (cp.supportsLocalXCursor) {
587 Pixel pix0, pix1;
588 rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1));
589 if (bitmap.buf) {
590 // The client supports XCursor and the cursor only has two
591 // colors. Use the XCursor encoding.
592 writer()->writeSetXCursor(server->cursor.width(),
593 server->cursor.height(),
594 server->cursor.hotspot.x,
595 server->cursor.hotspot.y,
596 bitmap.buf, server->cursor.mask.buf);
597 return;
598 } else {
599 // More than two colors
600 if (!cp.supportsLocalCursor) {
601 // FIXME: We could reduce to two colors.
602 vlog.info("Unable to send multicolor cursor: RichCursor not supported by client");
603 return;
604 }
605 }
606 }
607
608 // Use RichCursor
609 rdr::U8* transData = writer()->getImageBuf(server->cursor.area());
610 image_getter.translatePixels(server->cursor.data, transData,
611 server->cursor.area());
612 writer()->writeSetCursor(server->cursor.width(),
613 server->cursor.height(),
614 server->cursor.hotspot,
615 transData, server->cursor.mask.buf);
616}
617
618
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000619bool VNCSConnectionST::handleTimeout(Timer* t)
620{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000621 try {
622 if (t == &updateTimer)
623 writeFramebufferUpdate();
624 } catch (rdr::Exception& e) {
625 close(e.str());
626 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000627
628 return false;
629}
630
631
632bool VNCSConnectionST::isCongested()
633{
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000634 if (sock->outStream().bufferUsage() > 0)
635 return true;
636
637 return false;
638}
639
640
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000641void VNCSConnectionST::writeFramebufferUpdate()
642{
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000643 updateTimer.stop();
644
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000645 // We try to aggregate responses, so don't send out anything whilst we
646 // still have incoming messages. processMessages() will give us another
647 // chance to run once things are idle.
648 if (inProcessMessages)
649 return;
650
Pierre Ossmane9962f72009-04-23 12:31:42 +0000651 if (state() != RFBSTATE_NORMAL || requested.is_empty())
652 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000653
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000654 // Check that we actually have some space on the link and retry in a
655 // bit if things are congested.
656 if (isCongested()) {
657 updateTimer.start(50);
658 return;
659 }
660
Pierre Ossmane9962f72009-04-23 12:31:42 +0000661 // First take care of any updates that cannot contain framebuffer data
662 // changes.
663 if (writer()->needNoDataUpdate()) {
664 writer()->writeNoDataUpdate();
665 requested.clear();
666 return;
667 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000668
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000669 updates.enable_copyrect(cp.useCopyRect);
670
Pierre Ossman02e43d72009-03-05 11:57:11 +0000671 server->checkUpdate();
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000672
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000673 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000674 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000675 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000676
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000677 UpdateInfo ui;
678 updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000679 bool needNewUpdateInfo = false;
680
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000681 // If the previous position of the rendered cursor overlaps the source of the
682 // copy, then when the copy happens the corresponding rectangle in the
683 // destination will be wrong, so add it to the changed region.
684
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000685 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
686 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000687 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000688 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000689 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000690 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000691 }
692 }
693
694 // If we need to remove the old rendered cursor, just add the rectangle to
695 // the changed region.
696
697 if (removeRenderedCursor) {
698 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000699 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000700 renderedCursorRect.clear();
701 removeRenderedCursor = false;
702 }
703
704 // Return if there is nothing to send the client.
705
706 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
707 return;
708
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000709 // The `updates' object could change, make sure we have valid update info.
710
711 if (needNewUpdateInfo)
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000712 updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000713
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000714 // If the client needs a server-side rendered cursor, work out the cursor
715 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
716 // with the update region, we need to draw the rendered cursor regardless of
717 // whether it has changed.
718
719 if (needRenderedCursor()) {
720 renderedCursorRect
721 = (server->renderedCursor.getRect(server->renderedCursorTL)
722 .intersect(requested.get_bounding_rect()));
723
724 if (renderedCursorRect.is_empty()) {
725 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000726 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000727 .intersect(renderedCursorRect).is_empty()) {
728 drawRenderedCursor = true;
729 }
730
731 // We could remove the new cursor rect from updates here. It's not clear
732 // whether this is worth it. If we do remove it, then we won't draw over
733 // the same bit of screen twice, but we have the overhead of a more complex
734 // region.
735
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000736 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000737 // updates.subtract(renderedCursorRect);
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000738 // updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000739 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000740 }
741
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000742 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000743 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000744 // complicated as compared to the original VNC4.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000745 writer()->setupCurrentEncoder();
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000746 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000747 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000748
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000749 std::vector<Rect> rects;
750 std::vector<Rect>::const_iterator i;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000751 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000752 for (i = rects.begin(); i != rects.end(); i++) {
DRCcd2c5d42011-08-11 11:18:34 +0000753 if (i->width() && i->height()) {
754 int nUpdateRects = writer()->getNumRects(*i);
755 if (nUpdateRects == 0 && cp.currentEncoding() == encodingTight) {
756 nRects = 0xFFFF; break;
757 }
758 else
759 nRects += nUpdateRects;
760 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000761 }
762
763 writer()->writeFramebufferUpdateStart(nRects);
764 Region updatedRegion;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000765 writer()->writeRects(ui, &image_getter, &updatedRegion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000766 updates.subtract(updatedRegion);
767 if (drawRenderedCursor)
768 writeRenderedCursorRect();
769 writer()->writeFramebufferUpdateEnd();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000770 requested.clear();
771 }
772}
773
774
775// writeRenderedCursorRect() writes a single rectangle drawing the rendered
776// cursor on the client.
777
778void VNCSConnectionST::writeRenderedCursorRect()
779{
780 image_getter.setPixelBuffer(&server->renderedCursor);
781 image_getter.setOffset(server->renderedCursorTL);
782
783 Rect actual;
784 writer()->writeRect(renderedCursorRect, &image_getter, &actual);
785
786 image_getter.setPixelBuffer(server->pb);
787 image_getter.setOffset(Point(0,0));
788
789 drawRenderedCursor = false;
790}
791
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000792void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
793{
794 if (!authenticated())
795 return;
796
797 cp.screenLayout = server->screenLayout;
798
799 if (state() != RFBSTATE_NORMAL)
800 return;
801
802 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
803 cp.screenLayout);
804 writeFramebufferUpdate();
805}
806
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000807void VNCSConnectionST::setColourMapEntries(int firstColour, int nColours)
808{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000809 if (!readyForSetColourMapEntries)
810 return;
811 if (server->pb->getPF().trueColour)
812 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000813
Pierre Ossmana2739342011-03-08 16:53:07 +0000814 image_getter.setColourMapEntries(firstColour, nColours);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000815
816 if (cp.pf().trueColour) {
817 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000818 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000819 }
820}
821
822
823// setCursor() is called whenever the cursor has changed shape or pixel format.
824// If the client supports local cursor then it will arrange for the cursor to
825// be sent to the client.
826
827void VNCSConnectionST::setCursor()
828{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000829 if (state() != RFBSTATE_NORMAL)
830 return;
831 if (!cp.supportsLocalCursor)
832 return;
833
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000834 writer()->cursorChange(this);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000835 writeFramebufferUpdate();
836}
837
838void VNCSConnectionST::setDesktopName(const char *name)
839{
840 cp.setName(name);
841
842 if (state() != RFBSTATE_NORMAL)
843 return;
844
845 if (!writer()->writeSetDesktopName()) {
846 fprintf(stderr, "Client does not support desktop rename\n");
847 return;
848 }
849
850 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000851}
852
853void VNCSConnectionST::setSocketTimeouts()
854{
855 int timeoutms = rfb::Server::clientWaitTimeMillis;
856 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
857 if (timeoutms == 0)
858 timeoutms = -1;
859 sock->inStream().setTimeout(timeoutms);
860 sock->outStream().setTimeout(timeoutms);
861}
862
863char* VNCSConnectionST::getStartTime()
864{
865 char* result = ctime(&startTime);
866 result[24] = '\0';
867 return result;
868}
869
870void VNCSConnectionST::setStatus(int status)
871{
872 switch (status) {
873 case 0:
874 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
875 break;
876 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +0000877 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000878 break;
879 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +0000880 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000881 break;
882 }
883 framebufferUpdateRequest(server->pb->getRect(), false);
884}
885int VNCSConnectionST::getStatus()
886{
887 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
888 return 0;
889 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
890 return 1;
891 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
892 return 2;
893 return 4;
894}
895