blob: 43eb8256f81bfbfc04bce73dfccb85776a718642 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01002 * Copyright 2009-2016 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>
Pierre Ossman707fa122015-12-11 20:21:20 +010021
22#include <rfb/ComparingUpdateTracker.h>
23#include <rfb/Encoder.h>
24#include <rfb/KeyRemapper.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000026#include <rfb/Security.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010027#include <rfb/ServerCore.h>
28#include <rfb/SMsgWriter.h>
29#include <rfb/VNCServerST.h>
30#include <rfb/VNCSConnectionST.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000031#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000032#include <rfb/fenceTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033#define XK_MISCELLANY
34#define XK_XKB_KEYS
35#include <rfb/keysymdef.h>
36
37using namespace rfb;
38
39static LogWriter vlog("VNCSConnST");
40
41VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
42 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010043 : sock(s), reverseConnection(reverse),
Pierre Ossmanf8e3b342015-01-26 14:37:04 +010044 queryConnectTimer(this), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000045 pendingSyncFence(false), syncFence(false), fenceFlags(0),
46 fenceDataLen(0), fenceData(NULL),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020047 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020048 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020049 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000050 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051{
52 setStreams(&sock->inStream(), &sock->outStream());
53 peerEndpoint.buf = sock->getPeerEndpoint();
54 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
55
56 // Configure the socket
57 setSocketTimeouts();
58 lastEventTime = time(0);
59
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000060 server->clients.push_front(this);
61}
62
63
64VNCSConnectionST::~VNCSConnectionST()
65{
66 // If we reach here then VNCServerST is deleting us!
67 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
68 peerEndpoint.buf,
69 (closeReason.buf) ? closeReason.buf : "");
70
71 // Release any keys the client still had pressed
72 std::set<rdr::U32>::iterator i;
Pierre Ossman9a153b02015-08-31 10:01:14 +020073 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) {
74 vlog.debug("Releasing key 0x%x on client disconnect", *i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 server->desktop->keyEvent(*i, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020076 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077 if (server->pointerClient == this)
78 server->pointerClient = 0;
79
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 // Remove this client from the server
81 server->clients.remove(this);
82
Pierre Ossman2c764942011-11-14 15:54:30 +000083 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084}
85
86
87// Methods called from VNCServerST
88
89bool VNCSConnectionST::init()
90{
91 try {
92 initialiseProtocol();
93 } catch (rdr::Exception& e) {
94 close(e.str());
95 return false;
96 }
97 return true;
98}
99
100void VNCSConnectionST::close(const char* reason)
101{
102 // Log the reason for the close
103 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000104 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105 else
106 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
107
108 if (authenticated()) {
109 server->lastDisconnectTime = time(0);
110 }
111
112 // Just shutdown the socket and mark our state as closing. Eventually the
113 // calling code will call VNCServerST's removeSocket() method causing us to
114 // be deleted.
115 sock->shutdown();
116 setState(RFBSTATE_CLOSING);
117}
118
119
120void VNCSConnectionST::processMessages()
121{
122 if (state() == RFBSTATE_CLOSING) return;
123 try {
124 // - Now set appropriate socket timeouts and process data
125 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000126
127 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128
Pierre Ossmana830bec2011-11-08 12:12:02 +0000129 // Get the underlying TCP layer to build large packets if we send
130 // multiple small responses.
131 network::TcpSocket::cork(sock->getFd(), true);
132
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000134 if (pendingSyncFence) {
135 syncFence = true;
136 pendingSyncFence = false;
137 }
138
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000140
Pierre Ossman2c764942011-11-14 15:54:30 +0000141 if (syncFence) {
142 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
143 syncFence = false;
144 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 }
146
Pierre Ossmana830bec2011-11-08 12:12:02 +0000147 // Flush out everything in case we go idle after this.
148 network::TcpSocket::cork(sock->getFd(), false);
149
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000150 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000151
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000152 // If there were anything requiring an update, try to send it here.
153 // We wait until now with this to aggregate responses and to give
154 // higher priority to user actions such as keyboard and pointer events.
155 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000156 } catch (rdr::EndOfStream&) {
157 close("Clean disconnection");
158 } catch (rdr::Exception &e) {
159 close(e.str());
160 }
161}
162
Pierre Ossmand408ca52016-04-29 14:26:05 +0200163void VNCSConnectionST::flushSocket()
164{
165 if (state() == RFBSTATE_CLOSING) return;
166 try {
167 setSocketTimeouts();
168 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200169 // Flushing the socket might release an update that was previously
170 // delayed because of congestion.
171 if (sock->outStream().bufferUsage() == 0)
172 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200173 } catch (rdr::Exception &e) {
174 close(e.str());
175 }
176}
177
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178void VNCSConnectionST::pixelBufferChange()
179{
180 try {
181 if (!authenticated()) return;
182 if (cp.width && cp.height && (server->pb->width() != cp.width ||
183 server->pb->height() != cp.height))
184 {
185 // We need to clip the next update to the new size, but also add any
186 // extra bits if it's bigger. If we wanted to do this exactly, something
187 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200188 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189 // because that might be added to updates in writeFramebufferUpdate().
190
191 //updates.intersect(server->pb->getRect());
192 //
193 //if (server->pb->width() > cp.width)
194 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
195 // server->pb->height()));
196 //if (server->pb->height() > cp.height)
197 // updates.add_changed(Rect(0, cp.height, cp.width,
198 // server->pb->height()));
199
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200200 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201
202 cp.width = server->pb->width();
203 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000204 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000206 // We should only send EDS to client asking for both
207 if (!writer()->writeExtendedDesktopSize()) {
208 if (!writer()->writeSetDesktopSize()) {
209 close("Client does not support desktop resize");
210 return;
211 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212 }
213 }
214 }
215 // Just update the whole screen at the moment because we're too lazy to
216 // work out what's actually changed.
217 updates.clear();
218 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000219 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 } catch(rdr::Exception &e) {
221 close(e.str());
222 }
223}
224
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000225void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000226{
227 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000228 writeFramebufferUpdate();
229 } catch(rdr::Exception &e) {
230 close(e.str());
231 }
232}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000233
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000234void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
235{
236 try {
237 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000238 } catch(rdr::Exception &e) {
239 close(e.str());
240 }
241}
242
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000243void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000244{
245 try {
246 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
247 } catch(rdr::Exception& e) {
248 close(e.str());
249 }
250}
251
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000252void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000253{
254 try {
255 if (!(accessRights & AccessCutText)) return;
256 if (!rfb::Server::sendCutText) return;
257 if (state() == RFBSTATE_NORMAL)
258 writer()->writeServerCutText(str, len);
259 } catch(rdr::Exception& e) {
260 close(e.str());
261 }
262}
263
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000264
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000265void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000266{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000267 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000268 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000269 } catch(rdr::Exception& e) {
270 close(e.str());
271 }
272}
273
274
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000275void VNCSConnectionST::setCursorOrClose()
276{
277 try {
278 setCursor();
279 } catch(rdr::Exception& e) {
280 close(e.str());
281 }
282}
283
284
285int VNCSConnectionST::checkIdleTimeout()
286{
287 int idleTimeout = rfb::Server::idleTimeout;
288 if (idleTimeout == 0) return 0;
289 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
290 idleTimeout = 15; // minimum of 15 seconds while authenticating
291 time_t now = time(0);
292 if (now < lastEventTime) {
293 // Someone must have set the time backwards. Set lastEventTime so that the
294 // idleTimeout will count from now.
295 vlog.info("Time has gone backwards - resetting idle timeout");
296 lastEventTime = now;
297 }
298 int timeLeft = lastEventTime + idleTimeout - now;
299 if (timeLeft < -60) {
300 // Our callback is over a minute late - someone must have set the time
301 // forwards. Set lastEventTime so that the idleTimeout will count from
302 // now.
303 vlog.info("Time has gone forwards - resetting idle timeout");
304 lastEventTime = now;
305 return secsToMillis(idleTimeout);
306 }
307 if (timeLeft <= 0) {
308 close("Idle timeout");
309 return 0;
310 }
311 return secsToMillis(timeLeft);
312}
313
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000314
315bool VNCSConnectionST::getComparerState()
316{
317 // We interpret a low compression level as an indication that the client
318 // wants to prioritise CPU usage over bandwidth, and hence disable the
319 // comparing update tracker.
320 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
321}
322
323
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000324// renderedCursorChange() is called whenever the server-side rendered cursor
325// changes shape or position. It ensures that the next update will clean up
326// the old rendered cursor and if necessary draw the new rendered cursor.
327
328void VNCSConnectionST::renderedCursorChange()
329{
330 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200331 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000332 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000333 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200334 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000335 writeFramebufferUpdateOrClose();
336 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000337}
338
339// needRenderedCursor() returns true if this client needs the server-side
340// rendered cursor. This may be because it does not support local cursor or
341// because the current cursor position has not been set by this client.
342// Unfortunately we can't know for sure when the current cursor position has
343// been set by this client. We guess that this is the case when the current
344// cursor position is the same as the last pointer event from this client, or
345// if it is a very short time since this client's last pointer event (up to a
346// second). [ Ideally we should do finer-grained timing here and make the time
347// configurable, but I don't think it's that important. ]
348
349bool VNCSConnectionST::needRenderedCursor()
350{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100351 if (state() != RFBSTATE_NORMAL)
352 return false;
353
354 if (!cp.supportsLocalCursor && !cp.supportsLocalXCursor)
355 return true;
356 if (!server->cursorPos.equals(pointerEventPos) &&
357 (time(0) - pointerEventTime) > 0)
358 return true;
359
360 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000361}
362
363
364void VNCSConnectionST::approveConnectionOrClose(bool accept,
365 const char* reason)
366{
367 try {
368 approveConnection(accept, reason);
369 } catch (rdr::Exception& e) {
370 close(e.str());
371 }
372}
373
374
375
376// -=- Callbacks from SConnection
377
378void VNCSConnectionST::authSuccess()
379{
380 lastEventTime = time(0);
381
382 server->startDesktop();
383
384 // - Set the connection parameters appropriately
385 cp.width = server->pb->width();
386 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000387 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000388 cp.setName(server->getName());
389
390 // - Set the default pixel format
391 cp.setPF(server->pb->getPF());
392 char buffer[256];
393 cp.pf().print(buffer, 256);
394 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000395
396 // - Mark the entire display as "dirty"
397 updates.add_changed(server->pb->getRect());
398 startTime = time(0);
399}
400
401void VNCSConnectionST::queryConnection(const char* userName)
402{
403 // - Authentication succeeded - clear from blacklist
404 CharArray name; name.buf = sock->getPeerAddress();
405 server->blHosts->clearBlackmark(name.buf);
406
407 // - Special case to provide a more useful error message
408 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
409 server->authClientCount() > 0) {
410 approveConnection(false, "The server is already in use");
411 return;
412 }
413
414 // - Does the client have the right to bypass the query?
415 if (reverseConnection ||
416 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
417 (accessRights & AccessNoQuery))
418 {
419 approveConnection(true);
420 return;
421 }
422
423 // - Get the server to display an Accept/Reject dialog, if required
424 // If a dialog is displayed, the result will be PENDING, and the
425 // server will call approveConnection at a later time
426 CharArray reason;
427 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
428 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100429 if (qr == VNCServerST::PENDING) {
430 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000431 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100432 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000433
434 // - If server returns ACCEPT/REJECT then pass result to SConnection
435 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
436}
437
438void VNCSConnectionST::clientInit(bool shared)
439{
440 lastEventTime = time(0);
441 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100442 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000443 if (rfb::Server::neverShared) shared = false;
444 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100445 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000446 // - Close all the other connected clients
447 vlog.debug("non-shared connection - closing clients");
448 server->closeClients("Non-shared connection requested", getSock());
449 } else {
450 // - Refuse this connection if there are existing clients, in addition to
451 // this one
452 if (server->authClientCount() > 1) {
453 close("Server is already in use");
454 return;
455 }
456 }
457 }
458 SConnection::clientInit(shared);
459}
460
461void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
462{
463 SConnection::setPixelFormat(pf);
464 char buffer[256];
465 pf.print(buffer, 256);
466 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000467 setCursor();
468}
469
470void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
471{
472 pointerEventTime = lastEventTime = time(0);
473 server->lastUserInputTime = lastEventTime;
474 if (!(accessRights & AccessPtrEvents)) return;
475 if (!rfb::Server::acceptPointerEvents) return;
476 if (!server->pointerClient || server->pointerClient == this) {
477 pointerEventPos = pos;
478 if (buttonMask)
479 server->pointerClient = this;
480 else
481 server->pointerClient = 0;
482 server->desktop->pointerEvent(pointerEventPos, buttonMask);
483 }
484}
485
486
487class VNCSConnectionSTShiftPresser {
488public:
489 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
490 : desktop(desktop_), pressed(false) {}
491 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200492 if (pressed) {
493 vlog.debug("Releasing fake Shift_L");
494 desktop->keyEvent(XK_Shift_L, false);
495 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000496 }
497 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200498 vlog.debug("Pressing fake Shift_L");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000499 desktop->keyEvent(XK_Shift_L, true);
500 pressed = true;
501 }
502 SDesktop* desktop;
503 bool pressed;
504};
505
506// keyEvent() - record in the pressedKeys which keys were pressed. Allow
507// multiple down events (for autorepeat), but only allow a single up event.
508void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
509 lastEventTime = time(0);
510 server->lastUserInputTime = lastEventTime;
511 if (!(accessRights & AccessKeyEvents)) return;
512 if (!rfb::Server::acceptKeyEvents) return;
513
Pierre Ossman9a153b02015-08-31 10:01:14 +0200514 if (down)
515 vlog.debug("Key pressed: 0x%x", key);
516 else
517 vlog.debug("Key released: 0x%x", key);
518
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000519 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200520 if (server->keyRemapper) {
521 rdr::U32 newkey;
522 newkey = server->keyRemapper->remapKey(key);
523 if (newkey != key) {
524 vlog.debug("Key remapped to 0x%x", newkey);
525 key = newkey;
526 }
527 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000528
529 // Turn ISO_Left_Tab into shifted Tab.
530 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
531 if (key == XK_ISO_Left_Tab) {
532 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
533 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
534 shiftPresser.press();
535 key = XK_Tab;
536 }
537
538 if (down) {
539 pressedKeys.insert(key);
540 } else {
541 if (!pressedKeys.erase(key)) return;
542 }
543 server->desktop->keyEvent(key, down);
544}
545
546void VNCSConnectionST::clientCutText(const char* str, int len)
547{
548 if (!(accessRights & AccessCutText)) return;
549 if (!rfb::Server::acceptCutText) return;
550 server->desktop->clientCutText(str, len);
551}
552
553void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
554{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000555 Rect safeRect;
556
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000557 if (!(accessRights & AccessView)) return;
558
559 SConnection::framebufferUpdateRequest(r, incremental);
560
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000561 // Check that the client isn't sending crappy requests
562 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
563 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
564 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000565 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
566 } else {
567 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000568 }
569
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000570 // Just update the requested region.
571 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000572 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000573 if (!incremental || !continuousUpdates)
574 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575
576 if (!incremental) {
577 // Non-incremental update - treat as if area requested has changed
578 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000579
580 // And send the screen layout to the client (which, unlike the
581 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000582 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000583
584 // We do not send a DesktopSize since it only contains the
585 // framebuffer size (which the client already should know) and
586 // because some clients don't handle extra DesktopSize events
587 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000588 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000589}
590
Pierre Ossman34bb0612009-03-21 21:16:14 +0000591void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
592 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000593{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000594 unsigned int result;
595
Michal Srbb318b8f2014-11-24 13:18:28 +0200596 if (!(accessRights & AccessSetDesktopSize)) return;
597 if (!rfb::Server::acceptSetDesktopSize) return;
598
Pierre Ossman04e62db2009-03-23 16:57:07 +0000599 // Don't bother the desktop with an invalid configuration
600 if (!layout.validate(fb_width, fb_height)) {
601 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
602 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000603 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000604 return;
605 }
606
607 // FIXME: the desktop will call back to VNCServerST and an extra set
608 // of ExtendedDesktopSize messages will be sent. This is okay
609 // protocol-wise, but unnecessary.
610 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
611
Pierre Ossman04e62db2009-03-23 16:57:07 +0000612 writer()->writeExtendedDesktopSize(reasonClient, result,
613 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000614
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000615 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000616 if (result == resultSuccess) {
617 if (server->screenLayout != layout)
618 throw Exception("Desktop configured a different screen layout than requested");
619 server->notifyScreenLayoutChange(this);
620 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000621
622 // but always send back a reply to the requesting client
623 // (do this last as it might throw an exception on socket errors)
624 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000625}
626
Pierre Ossman2c764942011-11-14 15:54:30 +0000627void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
628{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100629 rdr::U8 type;
630
Pierre Ossman2c764942011-11-14 15:54:30 +0000631 if (flags & fenceFlagRequest) {
632 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000633 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000634
635 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
636 fenceDataLen = len;
637 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300638 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000639 if (len > 0) {
640 fenceData = new char[len];
641 memcpy(fenceData, data, len);
642 }
643
644 return;
645 }
646
647 // We handle everything synchronously so we trivially honor these modes
648 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
649
650 writer()->writeFence(flags, len, data);
651 return;
652 }
653
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100654 if (len < 1)
655 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000656
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100657 type = data[0];
658
659 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000660 case 0:
661 // Initial dummy fence;
662 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100663 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100664 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000665 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000666 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100667 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000668 }
669}
670
Pierre Ossman1b478e52011-11-15 12:08:30 +0000671void VNCSConnectionST::enableContinuousUpdates(bool enable,
672 int x, int y, int w, int h)
673{
674 Rect rect;
675
676 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
677 throw Exception("Client tried to enable continuous updates when not allowed");
678
679 continuousUpdates = enable;
680
681 rect.setXYWH(x, y, w, h);
682 cuRegion.reset(rect);
683
684 if (enable) {
685 requested.clear();
686 writeFramebufferUpdate();
687 } else {
688 writer()->writeEndOfContinuousUpdates();
689 }
690}
691
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692// supportsLocalCursor() is called whenever the status of
693// cp.supportsLocalCursor has changed. If the client does now support local
694// cursor, we make sure that the old server-side rendered cursor is cleaned up
695// and the cursor is sent to the client.
696
697void VNCSConnectionST::supportsLocalCursor()
698{
699 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200700 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000701 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000702 setCursor();
703 }
704}
705
Pierre Ossman2c764942011-11-14 15:54:30 +0000706void VNCSConnectionST::supportsFence()
707{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100708 char type = 0;
709 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000710}
711
Pierre Ossman1b478e52011-11-15 12:08:30 +0000712void VNCSConnectionST::supportsContinuousUpdates()
713{
714 // We refuse to use continuous updates if we cannot monitor the buffer
715 // usage using fences.
716 if (!cp.supportsFence)
717 return;
718
719 writer()->writeEndOfContinuousUpdates();
720}
721
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000722
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000723bool VNCSConnectionST::handleTimeout(Timer* t)
724{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000725 try {
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100726 if (t == &queryConnectTimer) {
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100727 if (state() == RFBSTATE_QUERYING)
728 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
729 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000730 } catch (rdr::Exception& e) {
731 close(e.str());
732 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000733
734 return false;
735}
736
737
Pierre Ossman1b478e52011-11-15 12:08:30 +0000738void VNCSConnectionST::writeRTTPing()
739{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100740 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000741
742 if (!cp.supportsFence)
743 return;
744
Pierre Ossman1b478e52011-11-15 12:08:30 +0000745 // We need to make sure any old update are already processed by the
746 // time we get the response back. This allows us to reliably throttle
747 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100748 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000749 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100750 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000751
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100752 congestion.sentPing(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000753}
754
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000755bool VNCSConnectionST::isCongested()
756{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000757 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200758 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000759 if (sock->outStream().bufferUsage() > 0)
760 return true;
761
Pierre Ossman1b478e52011-11-15 12:08:30 +0000762 if (!cp.supportsFence)
763 return false;
764
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100765 return congestion.isCongested(sock->outStream().length(),
766 sock->outStream().getIdleTime());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000767}
768
769
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000770void VNCSConnectionST::writeFramebufferUpdate()
771{
Pierre Ossman2c764942011-11-14 15:54:30 +0000772 // We're in the middle of processing a command that's supposed to be
773 // synchronised. Allowing an update to slip out right now might violate
774 // that synchronisation.
775 if (syncFence)
776 return;
777
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000778 // We try to aggregate responses, so don't send out anything whilst we
779 // still have incoming messages. processMessages() will give us another
780 // chance to run once things are idle.
781 if (inProcessMessages)
782 return;
783
Pierre Ossman1b478e52011-11-15 12:08:30 +0000784 if (state() != RFBSTATE_NORMAL)
785 return;
786 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000787 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000788
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000789 // Check that we actually have some space on the link and retry in a
790 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200791 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000792 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000793
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100794 // Updates often consists of many small writes, and in continuous
795 // mode, we will also have small fence messages around the update. We
796 // need to aggregate these in order to not clog up TCP's congestion
797 // window.
Pierre Ossman36dadf82011-11-15 12:11:32 +0000798 network::TcpSocket::cork(sock->getFd(), true);
799
Pierre Ossmane9962f72009-04-23 12:31:42 +0000800 // First take care of any updates that cannot contain framebuffer data
801 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100802 writeNoDataUpdate();
803
804 // Then real data (if possible)
805 writeDataUpdate();
806
807 network::TcpSocket::cork(sock->getFd(), false);
808}
809
810void VNCSConnectionST::writeNoDataUpdate()
811{
812 if (!writer()->needNoDataUpdate())
813 return;
814
815 writer()->writeNoDataUpdate();
816
817 // Make sure no data update is sent until next request
818 requested.clear();
819}
820
821void VNCSConnectionST::writeDataUpdate()
822{
823 Region req;
824 UpdateInfo ui;
825 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100826 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000827
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000828 updates.enable_copyrect(cp.useCopyRect);
829
Pierre Ossman6e49e952016-10-07 15:59:38 +0200830 // See if we are allowed to send anything right now (the framebuffer
831 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000832 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100833 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +0000834
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100835 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000836 if (continuousUpdates)
837 req = cuRegion.union_(requested);
838 else
839 req = requested;
840
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100841 if (req.is_empty())
842 return;
843
844 // Get the lists of updates. Prior to exporting the data to the `ui' object,
845 // getUpdateInfo() will normalize the `updates' object such way that its
846 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000847 updates.getUpdateInfo(&ui, req);
848 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000849
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000850 // If the previous position of the rendered cursor overlaps the source of the
851 // copy, then when the copy happens the corresponding rectangle in the
852 // destination will be wrong, so add it to the changed region.
853
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200854 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
855 Region bogusCopiedCursor;
856
857 bogusCopiedCursor.copyFrom(damagedCursorRegion);
858 bogusCopiedCursor.translate(ui.copy_delta);
859 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000860 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000861 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000862 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000863 }
864 }
865
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200866 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000867 // the changed region.
868
869 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200870 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000871 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200872 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000873 removeRenderedCursor = false;
874 }
875
876 // Return if there is nothing to send the client.
877
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200878 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100879 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000880
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000881 // The `updates' object could change, make sure we have valid update info.
882
883 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000884 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000885
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000886 // If the client needs a server-side rendered cursor, work out the cursor
887 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
888 // with the update region, we need to draw the rendered cursor regardless of
889 // whether it has changed.
890
Pierre Ossman24684e52016-12-05 16:58:19 +0100891 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000892 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200893 Rect renderedCursorRect;
894
Pierre Ossman24684e52016-12-05 16:58:19 +0100895 cursor = server->getRenderedCursor();
896
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000897 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +0100898 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000899
900 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +0100901 cursor = NULL;
902 } else if (!updateRenderedCursor &&
903 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000904 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +0100905 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000906 }
907
Pierre Ossman5c037202016-12-05 17:00:35 +0100908 if (cursor) {
909 updates.subtract(renderedCursorRect);
910 updates.getUpdateInfo(&ui, req);
911 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200912
913 damagedCursorRegion.assign_union(renderedCursorRect);
914 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000915 }
916
Pierre Ossman24684e52016-12-05 16:58:19 +0100917 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100918 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100919
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100920 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000921
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100922 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000923
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100924 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +0100925
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100926 // The request might be for just part of the screen, so we cannot
927 // just clear the entire update tracker.
928 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +0100929
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100930 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000931}
932
933
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000934void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
935{
936 if (!authenticated())
937 return;
938
939 cp.screenLayout = server->screenLayout;
940
941 if (state() != RFBSTATE_NORMAL)
942 return;
943
944 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
945 cp.screenLayout);
946 writeFramebufferUpdate();
947}
948
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000949
950// setCursor() is called whenever the cursor has changed shape or pixel format.
951// If the client supports local cursor then it will arrange for the cursor to
952// be sent to the client.
953
954void VNCSConnectionST::setCursor()
955{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000956 if (state() != RFBSTATE_NORMAL)
957 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000958
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100959 cp.setCursor(*server->cursor);
Pierre Ossman126e5642014-02-13 14:40:25 +0100960
Pierre Ossman8053c8e2017-02-21 12:59:04 +0100961 if (!writer()->writeSetCursorWithAlpha()) {
962 if (!writer()->writeSetCursor()) {
963 if (!writer()->writeSetXCursor()) {
964 // No client support
965 return;
966 }
Pierre Ossman126e5642014-02-13 14:40:25 +0100967 }
968 }
969
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000970 writeFramebufferUpdate();
971}
972
973void VNCSConnectionST::setDesktopName(const char *name)
974{
975 cp.setName(name);
976
977 if (state() != RFBSTATE_NORMAL)
978 return;
979
980 if (!writer()->writeSetDesktopName()) {
981 fprintf(stderr, "Client does not support desktop rename\n");
982 return;
983 }
984
985 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000986}
987
988void VNCSConnectionST::setSocketTimeouts()
989{
990 int timeoutms = rfb::Server::clientWaitTimeMillis;
991 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
992 if (timeoutms == 0)
993 timeoutms = -1;
994 sock->inStream().setTimeout(timeoutms);
995 sock->outStream().setTimeout(timeoutms);
996}
997
998char* VNCSConnectionST::getStartTime()
999{
1000 char* result = ctime(&startTime);
1001 result[24] = '\0';
1002 return result;
1003}
1004
1005void VNCSConnectionST::setStatus(int status)
1006{
1007 switch (status) {
1008 case 0:
1009 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1010 break;
1011 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001012 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001013 break;
1014 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001015 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001016 break;
1017 }
1018 framebufferUpdateRequest(server->pb->getRect(), false);
1019}
1020int VNCSConnectionST::getStatus()
1021{
1022 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1023 return 0;
1024 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1025 return 1;
1026 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1027 return 2;
1028 return 4;
1029}
1030