blob: 12dcf047ca98ef94c586ef2644fdf6bc39768ee9 [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>
Pierre Ossman2c764942011-11-14 15:54:30 +000025#include <rfb/fenceTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/ServerCore.h>
27#include <rfb/ComparingUpdateTracker.h>
28#include <rfb/KeyRemapper.h>
29#define XK_MISCELLANY
30#define XK_XKB_KEYS
31#include <rfb/keysymdef.h>
32
33using namespace rfb;
34
35static LogWriter vlog("VNCSConnST");
36
37VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
38 bool reverse)
Pierre Ossman2c764942011-11-14 15:54:30 +000039 : SConnection(reverse), sock(s), inProcessMessages(false),
40 syncFence(false), fenceFlags(0), fenceDataLen(0), fenceData(NULL),
41 server(server_),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 updates(false), image_getter(server->useEconomicTranslate),
43 drawRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000044 updateTimer(this), pointerEventTime(0),
45 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046{
47 setStreams(&sock->inStream(), &sock->outStream());
48 peerEndpoint.buf = sock->getPeerEndpoint();
49 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
50
51 // Configure the socket
52 setSocketTimeouts();
53 lastEventTime = time(0);
54
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055 server->clients.push_front(this);
56}
57
58
59VNCSConnectionST::~VNCSConnectionST()
60{
61 // If we reach here then VNCServerST is deleting us!
62 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
63 peerEndpoint.buf,
64 (closeReason.buf) ? closeReason.buf : "");
65
66 // Release any keys the client still had pressed
67 std::set<rdr::U32>::iterator i;
68 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
69 server->desktop->keyEvent(*i, false);
70 if (server->pointerClient == this)
71 server->pointerClient = 0;
72
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073 // Remove this client from the server
74 server->clients.remove(this);
75
Pierre Ossman2c764942011-11-14 15:54:30 +000076 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077}
78
79
80// Methods called from VNCServerST
81
82bool VNCSConnectionST::init()
83{
84 try {
85 initialiseProtocol();
86 } catch (rdr::Exception& e) {
87 close(e.str());
88 return false;
89 }
90 return true;
91}
92
93void VNCSConnectionST::close(const char* reason)
94{
95 // Log the reason for the close
96 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +000097 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 else
99 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
100
101 if (authenticated()) {
102 server->lastDisconnectTime = time(0);
103 }
104
105 // Just shutdown the socket and mark our state as closing. Eventually the
106 // calling code will call VNCServerST's removeSocket() method causing us to
107 // be deleted.
108 sock->shutdown();
109 setState(RFBSTATE_CLOSING);
110}
111
112
113void VNCSConnectionST::processMessages()
114{
115 if (state() == RFBSTATE_CLOSING) return;
116 try {
117 // - Now set appropriate socket timeouts and process data
118 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000119
120 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121
Pierre Ossmana830bec2011-11-08 12:12:02 +0000122 // Get the underlying TCP layer to build large packets if we send
123 // multiple small responses.
124 network::TcpSocket::cork(sock->getFd(), true);
125
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126 while (getInStream()->checkNoWait(1)) {
127 processMsg();
Pierre Ossman2c764942011-11-14 15:54:30 +0000128 if (syncFence) {
129 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
130 syncFence = false;
131 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132 }
133
Pierre Ossmana830bec2011-11-08 12:12:02 +0000134 // Flush out everything in case we go idle after this.
135 network::TcpSocket::cork(sock->getFd(), false);
136
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000137 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000138
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000139 // If there were anything requiring an update, try to send it here.
140 // We wait until now with this to aggregate responses and to give
141 // higher priority to user actions such as keyboard and pointer events.
142 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 } catch (rdr::EndOfStream&) {
144 close("Clean disconnection");
145 } catch (rdr::Exception &e) {
146 close(e.str());
147 }
148}
149
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150void VNCSConnectionST::pixelBufferChange()
151{
152 try {
153 if (!authenticated()) return;
154 if (cp.width && cp.height && (server->pb->width() != cp.width ||
155 server->pb->height() != cp.height))
156 {
157 // We need to clip the next update to the new size, but also add any
158 // extra bits if it's bigger. If we wanted to do this exactly, something
159 // like the code below would do it, but at the moment we just update the
160 // entire new size. However, we do need to clip the renderedCursorRect
161 // because that might be added to updates in writeFramebufferUpdate().
162
163 //updates.intersect(server->pb->getRect());
164 //
165 //if (server->pb->width() > cp.width)
166 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
167 // server->pb->height()));
168 //if (server->pb->height() > cp.height)
169 // updates.add_changed(Rect(0, cp.height, cp.width,
170 // server->pb->height()));
171
172 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
173
174 cp.width = server->pb->width();
175 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000176 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000178 // We should only send EDS to client asking for both
179 if (!writer()->writeExtendedDesktopSize()) {
180 if (!writer()->writeSetDesktopSize()) {
181 close("Client does not support desktop resize");
182 return;
183 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 }
185 }
186 }
187 // Just update the whole screen at the moment because we're too lazy to
188 // work out what's actually changed.
189 updates.clear();
190 updates.add_changed(server->pb->getRect());
191 vlog.debug("pixel buffer changed - re-initialising image getter");
192 image_getter.init(server->pb, cp.pf(), writer());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000193 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 } catch(rdr::Exception &e) {
195 close(e.str());
196 }
197}
198
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000199void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000200{
201 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000202 writeFramebufferUpdate();
203 } catch(rdr::Exception &e) {
204 close(e.str());
205 }
206}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000207
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000208void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
209{
210 try {
211 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000212 } catch(rdr::Exception &e) {
213 close(e.str());
214 }
215}
216
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217void VNCSConnectionST::setColourMapEntriesOrClose(int firstColour,int nColours)
218{
219 try {
220 setColourMapEntries(firstColour, nColours);
221 } catch(rdr::Exception& e) {
222 close(e.str());
223 }
224}
225
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000226void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000227{
228 try {
229 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
230 } catch(rdr::Exception& e) {
231 close(e.str());
232 }
233}
234
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000235void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236{
237 try {
238 if (!(accessRights & AccessCutText)) return;
239 if (!rfb::Server::sendCutText) return;
240 if (state() == RFBSTATE_NORMAL)
241 writer()->writeServerCutText(str, len);
242 } catch(rdr::Exception& e) {
243 close(e.str());
244 }
245}
246
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000247
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000248void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000249{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000250 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000251 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000252 } catch(rdr::Exception& e) {
253 close(e.str());
254 }
255}
256
257
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000258void VNCSConnectionST::setCursorOrClose()
259{
260 try {
261 setCursor();
262 } catch(rdr::Exception& e) {
263 close(e.str());
264 }
265}
266
267
268int VNCSConnectionST::checkIdleTimeout()
269{
270 int idleTimeout = rfb::Server::idleTimeout;
271 if (idleTimeout == 0) return 0;
272 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
273 idleTimeout = 15; // minimum of 15 seconds while authenticating
274 time_t now = time(0);
275 if (now < lastEventTime) {
276 // Someone must have set the time backwards. Set lastEventTime so that the
277 // idleTimeout will count from now.
278 vlog.info("Time has gone backwards - resetting idle timeout");
279 lastEventTime = now;
280 }
281 int timeLeft = lastEventTime + idleTimeout - now;
282 if (timeLeft < -60) {
283 // Our callback is over a minute late - someone must have set the time
284 // forwards. Set lastEventTime so that the idleTimeout will count from
285 // now.
286 vlog.info("Time has gone forwards - resetting idle timeout");
287 lastEventTime = now;
288 return secsToMillis(idleTimeout);
289 }
290 if (timeLeft <= 0) {
291 close("Idle timeout");
292 return 0;
293 }
294 return secsToMillis(timeLeft);
295}
296
297// renderedCursorChange() is called whenever the server-side rendered cursor
298// changes shape or position. It ensures that the next update will clean up
299// the old rendered cursor and if necessary draw the new rendered cursor.
300
301void VNCSConnectionST::renderedCursorChange()
302{
303 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000304 if (!renderedCursorRect.is_empty())
305 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000306 if (needRenderedCursor())
307 drawRenderedCursor = true;
308}
309
310// needRenderedCursor() returns true if this client needs the server-side
311// rendered cursor. This may be because it does not support local cursor or
312// because the current cursor position has not been set by this client.
313// Unfortunately we can't know for sure when the current cursor position has
314// been set by this client. We guess that this is the case when the current
315// cursor position is the same as the last pointer event from this client, or
316// if it is a very short time since this client's last pointer event (up to a
317// second). [ Ideally we should do finer-grained timing here and make the time
318// configurable, but I don't think it's that important. ]
319
320bool VNCSConnectionST::needRenderedCursor()
321{
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000322 bool pointerpos = (!server->cursorPos.equals(pointerEventPos) && (time(0) - pointerEventTime) > 0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000323 return (state() == RFBSTATE_NORMAL
Peter Ã…strandffeeb262010-02-10 09:29:00 +0000324 && ((!cp.supportsLocalCursor && !cp.supportsLocalXCursor) || pointerpos));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000325}
326
327
328void VNCSConnectionST::approveConnectionOrClose(bool accept,
329 const char* reason)
330{
331 try {
332 approveConnection(accept, reason);
333 } catch (rdr::Exception& e) {
334 close(e.str());
335 }
336}
337
338
339
340// -=- Callbacks from SConnection
341
342void VNCSConnectionST::authSuccess()
343{
344 lastEventTime = time(0);
345
346 server->startDesktop();
347
348 // - Set the connection parameters appropriately
349 cp.width = server->pb->width();
350 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000351 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000352 cp.setName(server->getName());
353
354 // - Set the default pixel format
355 cp.setPF(server->pb->getPF());
356 char buffer[256];
357 cp.pf().print(buffer, 256);
358 vlog.info("Server default pixel format %s", buffer);
359 image_getter.init(server->pb, cp.pf(), 0);
360
361 // - Mark the entire display as "dirty"
362 updates.add_changed(server->pb->getRect());
363 startTime = time(0);
364}
365
366void VNCSConnectionST::queryConnection(const char* userName)
367{
368 // - Authentication succeeded - clear from blacklist
369 CharArray name; name.buf = sock->getPeerAddress();
370 server->blHosts->clearBlackmark(name.buf);
371
372 // - Special case to provide a more useful error message
373 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
374 server->authClientCount() > 0) {
375 approveConnection(false, "The server is already in use");
376 return;
377 }
378
379 // - Does the client have the right to bypass the query?
380 if (reverseConnection ||
381 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
382 (accessRights & AccessNoQuery))
383 {
384 approveConnection(true);
385 return;
386 }
387
388 // - Get the server to display an Accept/Reject dialog, if required
389 // If a dialog is displayed, the result will be PENDING, and the
390 // server will call approveConnection at a later time
391 CharArray reason;
392 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
393 &reason.buf);
394 if (qr == VNCServerST::PENDING)
395 return;
396
397 // - If server returns ACCEPT/REJECT then pass result to SConnection
398 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
399}
400
401void VNCSConnectionST::clientInit(bool shared)
402{
403 lastEventTime = time(0);
404 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
405 if (rfb::Server::neverShared) shared = false;
406 if (!shared) {
407 if (rfb::Server::disconnectClients) {
408 // - Close all the other connected clients
409 vlog.debug("non-shared connection - closing clients");
410 server->closeClients("Non-shared connection requested", getSock());
411 } else {
412 // - Refuse this connection if there are existing clients, in addition to
413 // this one
414 if (server->authClientCount() > 1) {
415 close("Server is already in use");
416 return;
417 }
418 }
419 }
420 SConnection::clientInit(shared);
421}
422
423void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
424{
425 SConnection::setPixelFormat(pf);
426 char buffer[256];
427 pf.print(buffer, 256);
428 vlog.info("Client pixel format %s", buffer);
429 image_getter.init(server->pb, pf, writer());
430 setCursor();
431}
432
433void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
434{
435 pointerEventTime = lastEventTime = time(0);
436 server->lastUserInputTime = lastEventTime;
437 if (!(accessRights & AccessPtrEvents)) return;
438 if (!rfb::Server::acceptPointerEvents) return;
439 if (!server->pointerClient || server->pointerClient == this) {
440 pointerEventPos = pos;
441 if (buttonMask)
442 server->pointerClient = this;
443 else
444 server->pointerClient = 0;
445 server->desktop->pointerEvent(pointerEventPos, buttonMask);
446 }
447}
448
449
450class VNCSConnectionSTShiftPresser {
451public:
452 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
453 : desktop(desktop_), pressed(false) {}
454 ~VNCSConnectionSTShiftPresser() {
455 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
456 }
457 void press() {
458 desktop->keyEvent(XK_Shift_L, true);
459 pressed = true;
460 }
461 SDesktop* desktop;
462 bool pressed;
463};
464
465// keyEvent() - record in the pressedKeys which keys were pressed. Allow
466// multiple down events (for autorepeat), but only allow a single up event.
467void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
468 lastEventTime = time(0);
469 server->lastUserInputTime = lastEventTime;
470 if (!(accessRights & AccessKeyEvents)) return;
471 if (!rfb::Server::acceptKeyEvents) return;
472
473 // Remap the key if required
474 if (server->keyRemapper)
475 key = server->keyRemapper->remapKey(key);
476
477 // Turn ISO_Left_Tab into shifted Tab.
478 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
479 if (key == XK_ISO_Left_Tab) {
480 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
481 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
482 shiftPresser.press();
483 key = XK_Tab;
484 }
485
486 if (down) {
487 pressedKeys.insert(key);
488 } else {
489 if (!pressedKeys.erase(key)) return;
490 }
491 server->desktop->keyEvent(key, down);
492}
493
494void VNCSConnectionST::clientCutText(const char* str, int len)
495{
496 if (!(accessRights & AccessCutText)) return;
497 if (!rfb::Server::acceptCutText) return;
498 server->desktop->clientCutText(str, len);
499}
500
501void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
502{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000503 Rect safeRect;
504
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000505 if (!(accessRights & AccessView)) return;
506
507 SConnection::framebufferUpdateRequest(r, incremental);
508
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000509 // Check that the client isn't sending crappy requests
510 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
511 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
512 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000513 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
514 } else {
515 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000516 }
517
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000518 // Just update the requested region.
519 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000520 Region reqRgn(r);
521 requested.assign_union(reqRgn);
522
523 if (!incremental) {
524 // Non-incremental update - treat as if area requested has changed
525 updates.add_changed(reqRgn);
526 server->comparer->add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000527
528 // And send the screen layout to the client (which, unlike the
529 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000530 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000531
532 // We do not send a DesktopSize since it only contains the
533 // framebuffer size (which the client already should know) and
534 // because some clients don't handle extra DesktopSize events
535 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000536 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000537}
538
Pierre Ossman34bb0612009-03-21 21:16:14 +0000539void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
540 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000541{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000542 unsigned int result;
543
544 // Don't bother the desktop with an invalid configuration
545 if (!layout.validate(fb_width, fb_height)) {
546 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
547 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000548 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000549 return;
550 }
551
552 // FIXME: the desktop will call back to VNCServerST and an extra set
553 // of ExtendedDesktopSize messages will be sent. This is okay
554 // protocol-wise, but unnecessary.
555 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
556
Pierre Ossman04e62db2009-03-23 16:57:07 +0000557 writer()->writeExtendedDesktopSize(reasonClient, result,
558 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000559
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000560 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000561 if (result == resultSuccess) {
562 if (server->screenLayout != layout)
563 throw Exception("Desktop configured a different screen layout than requested");
564 server->notifyScreenLayoutChange(this);
565 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000566
567 // but always send back a reply to the requesting client
568 // (do this last as it might throw an exception on socket errors)
569 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000570}
571
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000572void VNCSConnectionST::setInitialColourMap()
573{
574 setColourMapEntries(0, 0);
575}
576
Pierre Ossman2c764942011-11-14 15:54:30 +0000577void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
578{
579 if (flags & fenceFlagRequest) {
580 if (flags & fenceFlagSyncNext) {
581 if (syncFence)
582 vlog.error("Fence trying to synchronise another fence");
583
584 syncFence = true;
585
586 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
587 fenceDataLen = len;
588 delete [] fenceData;
589 if (len > 0) {
590 fenceData = new char[len];
591 memcpy(fenceData, data, len);
592 }
593
594 return;
595 }
596
597 // We handle everything synchronously so we trivially honor these modes
598 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
599
600 writer()->writeFence(flags, len, data);
601 return;
602 }
603
604 switch (len) {
605 case 0:
606 // Initial dummy fence;
607 break;
608 default:
609 vlog.error("Fence response of unexpected size received");
610 }
611}
612
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000613// supportsLocalCursor() is called whenever the status of
614// cp.supportsLocalCursor has changed. If the client does now support local
615// cursor, we make sure that the old server-side rendered cursor is cleaned up
616// and the cursor is sent to the client.
617
618void VNCSConnectionST::supportsLocalCursor()
619{
620 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000621 if (!renderedCursorRect.is_empty())
622 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000623 drawRenderedCursor = false;
624 setCursor();
625 }
626}
627
Pierre Ossman2c764942011-11-14 15:54:30 +0000628void VNCSConnectionST::supportsFence()
629{
630 writer()->writeFence(fenceFlagRequest, 0, NULL);
631}
632
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000633void VNCSConnectionST::writeSetCursorCallback()
634{
635 if (cp.supportsLocalXCursor) {
636 Pixel pix0, pix1;
637 rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1));
638 if (bitmap.buf) {
639 // The client supports XCursor and the cursor only has two
640 // colors. Use the XCursor encoding.
641 writer()->writeSetXCursor(server->cursor.width(),
642 server->cursor.height(),
643 server->cursor.hotspot.x,
644 server->cursor.hotspot.y,
645 bitmap.buf, server->cursor.mask.buf);
646 return;
647 } else {
648 // More than two colors
649 if (!cp.supportsLocalCursor) {
650 // FIXME: We could reduce to two colors.
651 vlog.info("Unable to send multicolor cursor: RichCursor not supported by client");
652 return;
653 }
654 }
655 }
656
657 // Use RichCursor
658 rdr::U8* transData = writer()->getImageBuf(server->cursor.area());
659 image_getter.translatePixels(server->cursor.data, transData,
660 server->cursor.area());
661 writer()->writeSetCursor(server->cursor.width(),
662 server->cursor.height(),
663 server->cursor.hotspot,
664 transData, server->cursor.mask.buf);
665}
666
667
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000668bool VNCSConnectionST::handleTimeout(Timer* t)
669{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000670 try {
671 if (t == &updateTimer)
672 writeFramebufferUpdate();
673 } catch (rdr::Exception& e) {
674 close(e.str());
675 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000676
677 return false;
678}
679
680
681bool VNCSConnectionST::isCongested()
682{
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000683 if (sock->outStream().bufferUsage() > 0)
684 return true;
685
686 return false;
687}
688
689
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000690void VNCSConnectionST::writeFramebufferUpdate()
691{
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000692 updateTimer.stop();
693
Pierre Ossman2c764942011-11-14 15:54:30 +0000694 // We're in the middle of processing a command that's supposed to be
695 // synchronised. Allowing an update to slip out right now might violate
696 // that synchronisation.
697 if (syncFence)
698 return;
699
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000700 // We try to aggregate responses, so don't send out anything whilst we
701 // still have incoming messages. processMessages() will give us another
702 // chance to run once things are idle.
703 if (inProcessMessages)
704 return;
705
Pierre Ossmane9962f72009-04-23 12:31:42 +0000706 if (state() != RFBSTATE_NORMAL || requested.is_empty())
707 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000708
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000709 // Check that we actually have some space on the link and retry in a
710 // bit if things are congested.
711 if (isCongested()) {
712 updateTimer.start(50);
713 return;
714 }
715
Pierre Ossmane9962f72009-04-23 12:31:42 +0000716 // First take care of any updates that cannot contain framebuffer data
717 // changes.
718 if (writer()->needNoDataUpdate()) {
719 writer()->writeNoDataUpdate();
720 requested.clear();
721 return;
722 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000723
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000724 updates.enable_copyrect(cp.useCopyRect);
725
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000726 // Fetch updates from server object, and see if we are allowed to send
727 // anything right now (the framebuffer might have changed in ways we
728 // haven't yet been informed of).
729 if (!server->checkUpdate())
730 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000731
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000732 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000733 // getUpdateInfo() will normalize the `updates' object such way that its
Pierre Ossman02e43d72009-03-05 11:57:11 +0000734 // `changed' and `copied' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000735
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000736 UpdateInfo ui;
737 updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000738 bool needNewUpdateInfo = false;
739
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000740 // If the previous position of the rendered cursor overlaps the source of the
741 // copy, then when the copy happens the corresponding rectangle in the
742 // destination will be wrong, so add it to the changed region.
743
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000744 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
745 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000746 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000747 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000748 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000749 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000750 }
751 }
752
753 // If we need to remove the old rendered cursor, just add the rectangle to
754 // the changed region.
755
756 if (removeRenderedCursor) {
757 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000758 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000759 renderedCursorRect.clear();
760 removeRenderedCursor = false;
761 }
762
763 // Return if there is nothing to send the client.
764
765 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
766 return;
767
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000768 // The `updates' object could change, make sure we have valid update info.
769
770 if (needNewUpdateInfo)
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000771 updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000772
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000773 // If the client needs a server-side rendered cursor, work out the cursor
774 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
775 // with the update region, we need to draw the rendered cursor regardless of
776 // whether it has changed.
777
778 if (needRenderedCursor()) {
779 renderedCursorRect
780 = (server->renderedCursor.getRect(server->renderedCursorTL)
781 .intersect(requested.get_bounding_rect()));
782
783 if (renderedCursorRect.is_empty()) {
784 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000785 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000786 .intersect(renderedCursorRect).is_empty()) {
787 drawRenderedCursor = true;
788 }
789
790 // We could remove the new cursor rect from updates here. It's not clear
791 // whether this is worth it. If we do remove it, then we won't draw over
792 // the same bit of screen twice, but we have the overhead of a more complex
793 // region.
794
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000795 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000796 // updates.subtract(renderedCursorRect);
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000797 // updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000798 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000799 }
800
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000801 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000802 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000803 // complicated as compared to the original VNC4.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000804 writer()->setupCurrentEncoder();
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000805 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000806 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000807
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000808 std::vector<Rect> rects;
809 std::vector<Rect>::const_iterator i;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000810 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000811 for (i = rects.begin(); i != rects.end(); i++) {
DRCcd2c5d42011-08-11 11:18:34 +0000812 if (i->width() && i->height()) {
813 int nUpdateRects = writer()->getNumRects(*i);
814 if (nUpdateRects == 0 && cp.currentEncoding() == encodingTight) {
815 nRects = 0xFFFF; break;
816 }
817 else
818 nRects += nUpdateRects;
819 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000820 }
821
822 writer()->writeFramebufferUpdateStart(nRects);
823 Region updatedRegion;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000824 writer()->writeRects(ui, &image_getter, &updatedRegion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000825 updates.subtract(updatedRegion);
826 if (drawRenderedCursor)
827 writeRenderedCursorRect();
828 writer()->writeFramebufferUpdateEnd();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000829 requested.clear();
830 }
831}
832
833
834// writeRenderedCursorRect() writes a single rectangle drawing the rendered
835// cursor on the client.
836
837void VNCSConnectionST::writeRenderedCursorRect()
838{
839 image_getter.setPixelBuffer(&server->renderedCursor);
840 image_getter.setOffset(server->renderedCursorTL);
841
842 Rect actual;
843 writer()->writeRect(renderedCursorRect, &image_getter, &actual);
844
845 image_getter.setPixelBuffer(server->pb);
846 image_getter.setOffset(Point(0,0));
847
848 drawRenderedCursor = false;
849}
850
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000851void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
852{
853 if (!authenticated())
854 return;
855
856 cp.screenLayout = server->screenLayout;
857
858 if (state() != RFBSTATE_NORMAL)
859 return;
860
861 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
862 cp.screenLayout);
863 writeFramebufferUpdate();
864}
865
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000866void VNCSConnectionST::setColourMapEntries(int firstColour, int nColours)
867{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000868 if (!readyForSetColourMapEntries)
869 return;
870 if (server->pb->getPF().trueColour)
871 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000872
Pierre Ossmana2739342011-03-08 16:53:07 +0000873 image_getter.setColourMapEntries(firstColour, nColours);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000874
875 if (cp.pf().trueColour) {
876 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000877 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000878 }
879}
880
881
882// setCursor() is called whenever the cursor has changed shape or pixel format.
883// If the client supports local cursor then it will arrange for the cursor to
884// be sent to the client.
885
886void VNCSConnectionST::setCursor()
887{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000888 if (state() != RFBSTATE_NORMAL)
889 return;
890 if (!cp.supportsLocalCursor)
891 return;
892
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000893 writer()->cursorChange(this);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000894 writeFramebufferUpdate();
895}
896
897void VNCSConnectionST::setDesktopName(const char *name)
898{
899 cp.setName(name);
900
901 if (state() != RFBSTATE_NORMAL)
902 return;
903
904 if (!writer()->writeSetDesktopName()) {
905 fprintf(stderr, "Client does not support desktop rename\n");
906 return;
907 }
908
909 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000910}
911
912void VNCSConnectionST::setSocketTimeouts()
913{
914 int timeoutms = rfb::Server::clientWaitTimeMillis;
915 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
916 if (timeoutms == 0)
917 timeoutms = -1;
918 sock->inStream().setTimeout(timeoutms);
919 sock->outStream().setTimeout(timeoutms);
920}
921
922char* VNCSConnectionST::getStartTime()
923{
924 char* result = ctime(&startTime);
925 result[24] = '\0';
926 return result;
927}
928
929void VNCSConnectionST::setStatus(int status)
930{
931 switch (status) {
932 case 0:
933 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
934 break;
935 case 1:
Adam Tkac8e985062011-02-07 11:33:57 +0000936 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000937 break;
938 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +0000939 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000940 break;
941 }
942 framebufferUpdateRequest(server->pb->getRect(), false);
943}
944int VNCSConnectionST::getStatus()
945{
946 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
947 return 0;
948 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
949 return 1;
950 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
951 return 2;
952 return 4;
953}
954