blob: a9c59f3ec12895e47d6cc27889036ae212304d26 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <rfb/VNCSConnectionST.h>
20#include <rfb/LogWriter.h>
21#include <rfb/secTypes.h>
22#include <rfb/ServerCore.h>
23#include <rfb/ComparingUpdateTracker.h>
24#include <rfb/KeyRemapper.h>
25#define XK_MISCELLANY
26#define XK_XKB_KEYS
27#include <rfb/keysymdef.h>
28
29using namespace rfb;
30
31static LogWriter vlog("VNCSConnST");
32
33VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
34 bool reverse)
35 : SConnection(server_->securityFactory, reverse), sock(s), server(server_),
36 updates(false), image_getter(server->useEconomicTranslate),
37 drawRenderedCursor(false), removeRenderedCursor(false),
38 pointerEventTime(0), accessRights(AccessDefault),
39 startTime(time(0)), m_pFileTransfer(0)
40{
41 setStreams(&sock->inStream(), &sock->outStream());
42 peerEndpoint.buf = sock->getPeerEndpoint();
43 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
44
45 // Configure the socket
46 setSocketTimeouts();
47 lastEventTime = time(0);
48
49 // Add this client to the VNCServerST
50 if (server->m_pFTManager != NULL) {
51 SFileTransfer *pFT = server->m_pFTManager->createObject(sock);
52 if (pFT != NULL) {
53 m_pFileTransfer = pFT;
54 }
55 }
56
57 server->clients.push_front(this);
58}
59
60
61VNCSConnectionST::~VNCSConnectionST()
62{
63 // If we reach here then VNCServerST is deleting us!
64 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
65 peerEndpoint.buf,
66 (closeReason.buf) ? closeReason.buf : "");
67
68 // Release any keys the client still had pressed
69 std::set<rdr::U32>::iterator i;
70 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++)
71 server->desktop->keyEvent(*i, false);
72 if (server->pointerClient == this)
73 server->pointerClient = 0;
74
75 if (m_pFileTransfer)
76 server->m_pFTManager->destroyObject(m_pFileTransfer);
77
78 // Remove this client from the server
79 server->clients.remove(this);
80
81}
82
83
84// Methods called from VNCServerST
85
86bool VNCSConnectionST::init()
87{
88 try {
Constantin Kaplinsky82328312008-04-24 08:44:24 +000089 setProtocolOptions(server->isVideoSelectionEnabled());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 initialiseProtocol();
91 } catch (rdr::Exception& e) {
92 close(e.str());
93 return false;
94 }
95 return true;
96}
97
98void VNCSConnectionST::close(const char* reason)
99{
100 // Log the reason for the close
101 if (!closeReason.buf)
102 closeReason.buf = strDup(reason);
103 else
104 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
105
106 if (authenticated()) {
107 server->lastDisconnectTime = time(0);
108 }
109
110 // Just shutdown the socket and mark our state as closing. Eventually the
111 // calling code will call VNCServerST's removeSocket() method causing us to
112 // be deleted.
113 sock->shutdown();
114 setState(RFBSTATE_CLOSING);
115}
116
117
118void VNCSConnectionST::processMessages()
119{
120 if (state() == RFBSTATE_CLOSING) return;
121 try {
122 // - Now set appropriate socket timeouts and process data
123 setSocketTimeouts();
124 bool clientsReadyBefore = server->clientsReadyForUpdate();
125
126 while (getInStream()->checkNoWait(1)) {
127 processMsg();
128 }
129
130 if (!clientsReadyBefore && !requested.is_empty())
131 server->desktop->framebufferUpdateRequest();
132 } catch (rdr::EndOfStream&) {
133 close("Clean disconnection");
134 } catch (rdr::Exception &e) {
135 close(e.str());
136 }
137}
138
139void VNCSConnectionST::writeFramebufferUpdateOrClose()
140{
141 try {
142 writeFramebufferUpdate();
143 } catch(rdr::Exception &e) {
144 close(e.str());
145 }
146}
147
148void VNCSConnectionST::pixelBufferChange()
149{
150 try {
151 if (!authenticated()) return;
152 if (cp.width && cp.height && (server->pb->width() != cp.width ||
153 server->pb->height() != cp.height))
154 {
155 // We need to clip the next update to the new size, but also add any
156 // extra bits if it's bigger. If we wanted to do this exactly, something
157 // like the code below would do it, but at the moment we just update the
158 // entire new size. However, we do need to clip the renderedCursorRect
159 // because that might be added to updates in writeFramebufferUpdate().
160
161 //updates.intersect(server->pb->getRect());
162 //
163 //if (server->pb->width() > cp.width)
164 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
165 // server->pb->height()));
166 //if (server->pb->height() > cp.height)
167 // updates.add_changed(Rect(0, cp.height, cp.width,
168 // server->pb->height()));
169
170 renderedCursorRect = renderedCursorRect.intersect(server->pb->getRect());
171
172 cp.width = server->pb->width();
173 cp.height = server->pb->height();
174 if (state() == RFBSTATE_NORMAL) {
175 if (!writer()->writeSetDesktopSize()) {
176 close("Client does not support desktop resize");
177 return;
178 }
179 }
180 }
181 // Just update the whole screen at the moment because we're too lazy to
182 // work out what's actually changed.
183 updates.clear();
184 updates.add_changed(server->pb->getRect());
185 vlog.debug("pixel buffer changed - re-initialising image getter");
186 image_getter.init(server->pb, cp.pf(), writer());
187 if (writer()->needFakeUpdate())
188 writeFramebufferUpdate();
189 } catch(rdr::Exception &e) {
190 close(e.str());
191 }
192}
193
194void VNCSConnectionST::setColourMapEntriesOrClose(int firstColour,int nColours)
195{
196 try {
197 setColourMapEntries(firstColour, nColours);
198 } catch(rdr::Exception& e) {
199 close(e.str());
200 }
201}
202
203void VNCSConnectionST::bell()
204{
205 try {
206 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
207 } catch(rdr::Exception& e) {
208 close(e.str());
209 }
210}
211
212void VNCSConnectionST::serverCutText(const char *str, int len)
213{
214 try {
215 if (!(accessRights & AccessCutText)) return;
216 if (!rfb::Server::sendCutText) return;
217 if (state() == RFBSTATE_NORMAL)
218 writer()->writeServerCutText(str, len);
219 } catch(rdr::Exception& e) {
220 close(e.str());
221 }
222}
223
224void VNCSConnectionST::setCursorOrClose()
225{
226 try {
227 setCursor();
228 } catch(rdr::Exception& e) {
229 close(e.str());
230 }
231}
232
233
234int VNCSConnectionST::checkIdleTimeout()
235{
236 int idleTimeout = rfb::Server::idleTimeout;
237 if (idleTimeout == 0) return 0;
238 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
239 idleTimeout = 15; // minimum of 15 seconds while authenticating
240 time_t now = time(0);
241 if (now < lastEventTime) {
242 // Someone must have set the time backwards. Set lastEventTime so that the
243 // idleTimeout will count from now.
244 vlog.info("Time has gone backwards - resetting idle timeout");
245 lastEventTime = now;
246 }
247 int timeLeft = lastEventTime + idleTimeout - now;
248 if (timeLeft < -60) {
249 // Our callback is over a minute late - someone must have set the time
250 // forwards. Set lastEventTime so that the idleTimeout will count from
251 // now.
252 vlog.info("Time has gone forwards - resetting idle timeout");
253 lastEventTime = now;
254 return secsToMillis(idleTimeout);
255 }
256 if (timeLeft <= 0) {
257 close("Idle timeout");
258 return 0;
259 }
260 return secsToMillis(timeLeft);
261}
262
263// renderedCursorChange() is called whenever the server-side rendered cursor
264// changes shape or position. It ensures that the next update will clean up
265// the old rendered cursor and if necessary draw the new rendered cursor.
266
267void VNCSConnectionST::renderedCursorChange()
268{
269 if (state() != RFBSTATE_NORMAL) return;
270 removeRenderedCursor = true;
271 if (needRenderedCursor())
272 drawRenderedCursor = true;
273}
274
275// needRenderedCursor() returns true if this client needs the server-side
276// rendered cursor. This may be because it does not support local cursor or
277// because the current cursor position has not been set by this client.
278// Unfortunately we can't know for sure when the current cursor position has
279// been set by this client. We guess that this is the case when the current
280// cursor position is the same as the last pointer event from this client, or
281// if it is a very short time since this client's last pointer event (up to a
282// second). [ Ideally we should do finer-grained timing here and make the time
283// configurable, but I don't think it's that important. ]
284
285bool VNCSConnectionST::needRenderedCursor()
286{
287 return (state() == RFBSTATE_NORMAL
288 && (!cp.supportsLocalCursor && !cp.supportsLocalXCursor
289 || (!server->cursorPos.equals(pointerEventPos) &&
290 (time(0) - pointerEventTime) > 0)));
291}
292
293
294void VNCSConnectionST::approveConnectionOrClose(bool accept,
295 const char* reason)
296{
297 try {
298 approveConnection(accept, reason);
299 } catch (rdr::Exception& e) {
300 close(e.str());
301 }
302}
303
304
305
306// -=- Callbacks from SConnection
307
308void VNCSConnectionST::authSuccess()
309{
310 lastEventTime = time(0);
311
312 server->startDesktop();
313
314 // - Set the connection parameters appropriately
315 cp.width = server->pb->width();
316 cp.height = server->pb->height();
317 cp.setName(server->getName());
318
319 // - Set the default pixel format
320 cp.setPF(server->pb->getPF());
321 char buffer[256];
322 cp.pf().print(buffer, 256);
323 vlog.info("Server default pixel format %s", buffer);
324 image_getter.init(server->pb, cp.pf(), 0);
325
326 // - Mark the entire display as "dirty"
327 updates.add_changed(server->pb->getRect());
328 startTime = time(0);
329}
330
331void VNCSConnectionST::queryConnection(const char* userName)
332{
333 // - Authentication succeeded - clear from blacklist
334 CharArray name; name.buf = sock->getPeerAddress();
335 server->blHosts->clearBlackmark(name.buf);
336
337 // - Special case to provide a more useful error message
338 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
339 server->authClientCount() > 0) {
340 approveConnection(false, "The server is already in use");
341 return;
342 }
343
344 // - Does the client have the right to bypass the query?
345 if (reverseConnection ||
346 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
347 (accessRights & AccessNoQuery))
348 {
349 approveConnection(true);
350 return;
351 }
352
353 // - Get the server to display an Accept/Reject dialog, if required
354 // If a dialog is displayed, the result will be PENDING, and the
355 // server will call approveConnection at a later time
356 CharArray reason;
357 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
358 &reason.buf);
359 if (qr == VNCServerST::PENDING)
360 return;
361
362 // - If server returns ACCEPT/REJECT then pass result to SConnection
363 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
364}
365
366void VNCSConnectionST::clientInit(bool shared)
367{
368 lastEventTime = time(0);
369 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
370 if (rfb::Server::neverShared) shared = false;
371 if (!shared) {
372 if (rfb::Server::disconnectClients) {
373 // - Close all the other connected clients
374 vlog.debug("non-shared connection - closing clients");
375 server->closeClients("Non-shared connection requested", getSock());
376 } else {
377 // - Refuse this connection if there are existing clients, in addition to
378 // this one
379 if (server->authClientCount() > 1) {
380 close("Server is already in use");
381 return;
382 }
383 }
384 }
385 SConnection::clientInit(shared);
386}
387
388void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
389{
390 SConnection::setPixelFormat(pf);
391 char buffer[256];
392 pf.print(buffer, 256);
393 vlog.info("Client pixel format %s", buffer);
394 image_getter.init(server->pb, pf, writer());
395 setCursor();
396}
397
398void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
399{
400 pointerEventTime = lastEventTime = time(0);
401 server->lastUserInputTime = lastEventTime;
402 if (!(accessRights & AccessPtrEvents)) return;
403 if (!rfb::Server::acceptPointerEvents) return;
404 if (!server->pointerClient || server->pointerClient == this) {
405 pointerEventPos = pos;
406 if (buttonMask)
407 server->pointerClient = this;
408 else
409 server->pointerClient = 0;
410 server->desktop->pointerEvent(pointerEventPos, buttonMask);
411 }
412}
413
414
415class VNCSConnectionSTShiftPresser {
416public:
417 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
418 : desktop(desktop_), pressed(false) {}
419 ~VNCSConnectionSTShiftPresser() {
420 if (pressed) { desktop->keyEvent(XK_Shift_L, false); }
421 }
422 void press() {
423 desktop->keyEvent(XK_Shift_L, true);
424 pressed = true;
425 }
426 SDesktop* desktop;
427 bool pressed;
428};
429
430// keyEvent() - record in the pressedKeys which keys were pressed. Allow
431// multiple down events (for autorepeat), but only allow a single up event.
432void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
433 lastEventTime = time(0);
434 server->lastUserInputTime = lastEventTime;
435 if (!(accessRights & AccessKeyEvents)) return;
436 if (!rfb::Server::acceptKeyEvents) return;
437
438 // Remap the key if required
439 if (server->keyRemapper)
440 key = server->keyRemapper->remapKey(key);
441
442 // Turn ISO_Left_Tab into shifted Tab.
443 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
444 if (key == XK_ISO_Left_Tab) {
445 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
446 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
447 shiftPresser.press();
448 key = XK_Tab;
449 }
450
451 if (down) {
452 pressedKeys.insert(key);
453 } else {
454 if (!pressedKeys.erase(key)) return;
455 }
456 server->desktop->keyEvent(key, down);
457}
458
459void VNCSConnectionST::clientCutText(const char* str, int len)
460{
461 if (!(accessRights & AccessCutText)) return;
462 if (!rfb::Server::acceptCutText) return;
463 server->desktop->clientCutText(str, len);
464}
465
466void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
467{
468 if (!(accessRights & AccessView)) return;
469
470 SConnection::framebufferUpdateRequest(r, incremental);
471
472 Region reqRgn(r);
473 requested.assign_union(reqRgn);
474
475 if (!incremental) {
476 // Non-incremental update - treat as if area requested has changed
477 updates.add_changed(reqRgn);
478 server->comparer->add_changed(reqRgn);
479 }
480
481 writeFramebufferUpdate();
482}
483
484void VNCSConnectionST::setInitialColourMap()
485{
486 setColourMapEntries(0, 0);
487}
488
489// supportsLocalCursor() is called whenever the status of
490// cp.supportsLocalCursor has changed. If the client does now support local
491// cursor, we make sure that the old server-side rendered cursor is cleaned up
492// and the cursor is sent to the client.
493
494void VNCSConnectionST::supportsLocalCursor()
495{
496 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
497 removeRenderedCursor = true;
498 drawRenderedCursor = false;
499 setCursor();
500 }
501}
502
503void VNCSConnectionST::writeSetCursorCallback()
504{
505 if (cp.supportsLocalXCursor) {
506 Pixel pix0, pix1;
507 rdr::U8Array bitmap(server->cursor.getBitmap(&pix0, &pix1));
508 if (bitmap.buf) {
509 // The client supports XCursor and the cursor only has two
510 // colors. Use the XCursor encoding.
511 writer()->writeSetXCursor(server->cursor.width(),
512 server->cursor.height(),
513 server->cursor.hotspot.x,
514 server->cursor.hotspot.y,
515 bitmap.buf, server->cursor.mask.buf);
516 return;
517 } else {
518 // More than two colors
519 if (!cp.supportsLocalCursor) {
520 // FIXME: We could reduce to two colors.
521 vlog.info("Unable to send multicolor cursor: RichCursor not supported by client");
522 return;
523 }
524 }
525 }
526
527 // Use RichCursor
528 rdr::U8* transData = writer()->getImageBuf(server->cursor.area());
529 image_getter.translatePixels(server->cursor.data, transData,
530 server->cursor.area());
531 writer()->writeSetCursor(server->cursor.width(),
532 server->cursor.height(),
533 server->cursor.hotspot,
534 transData, server->cursor.mask.buf);
535}
536
537
538void VNCSConnectionST::writeFramebufferUpdate()
539{
540 if (state() != RFBSTATE_NORMAL || requested.is_empty()) return;
541
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000542 updates.enable_copyrect(cp.useCopyRect);
543
Constantin Kaplinsky6970b2d2008-06-13 18:07:53 +0000544 static int counter = 1;
545 if (--counter > 0) {
546 server->checkVideoUpdate();
547 } else {
548 counter = rfb::Server::videoPriority;
549 server->checkUpdate();
550 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000551
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000552 // Get the lists of updates. Prior to exporting the data to the `ui' object,
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000553 // getUpdateInfo() will normalize the `updates' object such way that its
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000554 // `changed', `copied' and `video_area' regions would not intersect.
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000555
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000556 UpdateInfo ui;
557 updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000558 bool needNewUpdateInfo = false;
559
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000560 // If the previous position of the rendered cursor overlaps the source of the
561 // copy, then when the copy happens the corresponding rectangle in the
562 // destination will be wrong, so add it to the changed region.
563
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000564 if (!ui.copied.is_empty() && !renderedCursorRect.is_empty()) {
565 Rect bogusCopiedCursor = (renderedCursorRect.translate(ui.copy_delta)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000566 .intersect(server->pb->getRect()));
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000567 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000568 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000569 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000570 }
571 }
572
573 // If we need to remove the old rendered cursor, just add the rectangle to
574 // the changed region.
575
576 if (removeRenderedCursor) {
577 updates.add_changed(renderedCursorRect);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000578 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000579 renderedCursorRect.clear();
580 removeRenderedCursor = false;
581 }
582
583 // Return if there is nothing to send the client.
584
585 if (updates.is_empty() && !writer()->needFakeUpdate() && !drawRenderedCursor)
586 return;
587
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000588 // The `updates' object could change, make sure we have valid update info.
589
590 if (needNewUpdateInfo)
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000591 updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000592
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593 // If the client needs a server-side rendered cursor, work out the cursor
594 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
595 // with the update region, we need to draw the rendered cursor regardless of
596 // whether it has changed.
597
598 if (needRenderedCursor()) {
599 renderedCursorRect
600 = (server->renderedCursor.getRect(server->renderedCursorTL)
601 .intersect(requested.get_bounding_rect()));
602
603 if (renderedCursorRect.is_empty()) {
604 drawRenderedCursor = false;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000605 } else if (!ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000606 .intersect(renderedCursorRect).is_empty()) {
607 drawRenderedCursor = true;
608 }
609
610 // We could remove the new cursor rect from updates here. It's not clear
611 // whether this is worth it. If we do remove it, then we won't draw over
612 // the same bit of screen twice, but we have the overhead of a more complex
613 // region.
614
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000615 //if (drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000616 // updates.subtract(renderedCursorRect);
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000617 // updates.getUpdateInfo(&ui, requested);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000618 //}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000619 }
620
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000621 if (!ui.is_empty() || writer()->needFakeUpdate() || drawRenderedCursor) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000622 // Compute the number of rectangles. Tight encoder makes the things more
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000623 // complicated as compared to the original VNC4.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000624 writer()->setupCurrentEncoder();
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000625 int nRects = (ui.copied.numRects() +
Constantin Kaplinsky1a845d02007-08-31 21:06:53 +0000626 (drawRenderedCursor ? 1 : 0));
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000627 if (!ui.video_area.is_empty()) {
628 if (writer()->canUseJpegEncoder(server->getPixelBuffer())) {
629 nRects++;
630 } else {
631 nRects += writer()->getNumRects(ui.video_area);
632 }
633 }
634
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000635 std::vector<Rect> rects;
636 std::vector<Rect>::const_iterator i;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000637 ui.changed.get_rects(&rects);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000638 for (i = rects.begin(); i != rects.end(); i++) {
639 if (i->width() && i->height())
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000640 nRects += writer()->getNumRects(*i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000641 }
642
643 writer()->writeFramebufferUpdateStart(nRects);
Constantin Kaplinsky651606d2007-10-17 17:40:23 +0000644 if (!ui.video_area.is_empty()) {
645 if (writer()->canUseJpegEncoder(server->getPixelBuffer())) {
646 writer()->writeJpegRect(server->getPixelBuffer(), ui.video_area);
647 } else {
648 Rect actual;
649 writer()->writeRect(ui.video_area, &image_getter, &actual);
650 }
651 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000652 Region updatedRegion;
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000653 writer()->writeRects(ui, &image_getter, &updatedRegion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000654 updates.subtract(updatedRegion);
655 if (drawRenderedCursor)
656 writeRenderedCursorRect();
657 writer()->writeFramebufferUpdateEnd();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000658 requested.clear();
659 }
660}
661
662
663// writeRenderedCursorRect() writes a single rectangle drawing the rendered
664// cursor on the client.
665
666void VNCSConnectionST::writeRenderedCursorRect()
667{
668 image_getter.setPixelBuffer(&server->renderedCursor);
669 image_getter.setOffset(server->renderedCursorTL);
670
671 Rect actual;
672 writer()->writeRect(renderedCursorRect, &image_getter, &actual);
673
674 image_getter.setPixelBuffer(server->pb);
675 image_getter.setOffset(Point(0,0));
676
677 drawRenderedCursor = false;
678}
679
680void VNCSConnectionST::setColourMapEntries(int firstColour, int nColours)
681{
682 if (!readyForSetColourMapEntries) return;
683 if (server->pb->getPF().trueColour) return;
684
685 image_getter.setColourMapEntries(firstColour, nColours, writer());
686
687 if (cp.pf().trueColour) {
688 updates.add_changed(server->pb->getRect());
689 }
690}
691
692
693// setCursor() is called whenever the cursor has changed shape or pixel format.
694// If the client supports local cursor then it will arrange for the cursor to
695// be sent to the client.
696
697void VNCSConnectionST::setCursor()
698{
699 if (state() != RFBSTATE_NORMAL || !cp.supportsLocalCursor) return;
700 writer()->cursorChange(this);
701 if (writer()->needFakeUpdate())
702 writeFramebufferUpdate();
703}
704
705void VNCSConnectionST::setSocketTimeouts()
706{
707 int timeoutms = rfb::Server::clientWaitTimeMillis;
708 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
709 if (timeoutms == 0)
710 timeoutms = -1;
711 sock->inStream().setTimeout(timeoutms);
712 sock->outStream().setTimeout(timeoutms);
713}
714
715char* VNCSConnectionST::getStartTime()
716{
717 char* result = ctime(&startTime);
718 result[24] = '\0';
719 return result;
720}
721
722void VNCSConnectionST::setStatus(int status)
723{
724 switch (status) {
725 case 0:
726 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
727 break;
728 case 1:
729 accessRights = accessRights & !(AccessPtrEvents | AccessKeyEvents) | AccessView;
730 break;
731 case 2:
732 accessRights = accessRights & !(AccessPtrEvents | AccessKeyEvents | AccessView);
733 break;
734 }
735 framebufferUpdateRequest(server->pb->getRect(), false);
736}
737int VNCSConnectionST::getStatus()
738{
739 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
740 return 0;
741 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
742 return 1;
743 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
744 return 2;
745 return 4;
746}
747
748bool VNCSConnectionST::processFTMsg(int type)
749{
750 if (m_pFileTransfer != NULL)
751 return m_pFileTransfer->processMessages(type);
752 else
753 return false;
754}