blob: c21f8bc54b0f80ac8a48816d6589d0abd1b79966 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRCb4a83232011-08-19 04:57:18 +00002 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossman7638e9c2014-01-16 13:12:40 +01003 * Copyright 2009-2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20#include <stdio.h>
21#include <assert.h>
22#include <rdr/OutStream.h>
23#include <rfb/msgTypes.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010024#include <rfb/fenceTypes.h>
25#include <rfb/Exception.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/ColourMap.h>
27#include <rfb/ConnParams.h>
28#include <rfb/UpdateTracker.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010029#include <rfb/Encoder.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <rfb/SMsgWriter.h>
31#include <rfb/LogWriter.h>
32
33using namespace rfb;
34
35static LogWriter vlog("SMsgWriter");
36
37SMsgWriter::SMsgWriter(ConnParams* cp_, rdr::OutStream* os_)
Pierre Ossman7638e9c2014-01-16 13:12:40 +010038 : imageBufIdealSize(0), cp(cp_), os(os_), currentEncoding(0),
39 nRectsInUpdate(0), nRectsInHeader(0),
40 wsccb(0), needSetDesktopSize(false),
41 needExtendedDesktopSize(false), needSetDesktopName(false),
42 lenBeforeRect(0), updatesSent(0), rawBytesEquivalent(0),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043 imageBuf(0), imageBufSize(0)
44{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000045 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046 encoders[i] = 0;
47 bytesSent[i] = 0;
48 rectsSent[i] = 0;
49 }
50}
51
52SMsgWriter::~SMsgWriter()
53{
54 vlog.info("framebuffer updates %d",updatesSent);
55 int bytes = 0;
Peter Åstrand98fe98c2010-02-10 07:43:02 +000056 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057 delete encoders[i];
58 if (i != encodingCopyRect)
59 bytes += bytesSent[i];
60 if (rectsSent[i])
61 vlog.info(" %s rects %d, bytes %d",
62 encodingName(i), rectsSent[i], bytesSent[i]);
63 }
DRC887c5fd2011-08-19 03:13:47 +000064 vlog.info(" raw bytes equivalent %llu, compression ratio %f",
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065 rawBytesEquivalent, (double)rawBytesEquivalent / bytes);
66 delete [] imageBuf;
67}
68
Pierre Ossman7638e9c2014-01-16 13:12:40 +010069void SMsgWriter::writeServerInit()
70{
71 os->writeU16(cp->width);
72 os->writeU16(cp->height);
73 cp->pf().write(os);
74 os->writeString(cp->name());
75 endMsg();
76}
77
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
79 ColourMap* cm)
80{
81 startMsg(msgTypeSetColourMapEntries);
82 os->pad(1);
83 os->writeU16(firstColour);
84 os->writeU16(nColours);
85 for (int i = firstColour; i < firstColour+nColours; i++) {
86 int r, g, b;
87 cm->lookup(i, &r, &g, &b);
88 os->writeU16(r);
89 os->writeU16(g);
90 os->writeU16(b);
91 }
92 endMsg();
93}
94
95void SMsgWriter::writeBell()
96{
97 startMsg(msgTypeBell);
98 endMsg();
99}
100
101void SMsgWriter::writeServerCutText(const char* str, int len)
102{
103 startMsg(msgTypeServerCutText);
104 os->pad(3);
105 os->writeU32(len);
106 os->writeBytes(str, len);
107 endMsg();
108}
109
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100110void SMsgWriter::writeFence(rdr::U32 flags, unsigned len, const char data[])
111{
112 if (!cp->supportsFence)
113 throw Exception("Client does not support fences");
114 if (len > 64)
115 throw Exception("Too large fence payload");
116 if ((flags & ~fenceFlagsSupported) != 0)
117 throw Exception("Unknown fence flags");
118
119 startMsg(msgTypeServerFence);
120 os->pad(3);
121
122 os->writeU32(flags);
123
124 os->writeU8(len);
125 os->writeBytes(data, len);
126
127 endMsg();
128}
129
130void SMsgWriter::writeEndOfContinuousUpdates()
131{
132 if (!cp->supportsContinuousUpdates)
133 throw Exception("Client does not support continuous updates");
134
135 startMsg(msgTypeEndOfContinuousUpdates);
136 endMsg();
137}
138
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139void SMsgWriter::setupCurrentEncoder()
140{
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000141 int encoding = cp->currentEncoding();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142
143 // FIXME: Code duplication, see writeRect().
144 if (!encoders[encoding]) {
145 encoders[encoding] = Encoder::createEncoder(encoding, this);
146 assert(encoders[encoding]);
147 }
148
149 encoders[encoding]->setCompressLevel(cp->compressLevel);
150 encoders[encoding]->setQualityLevel(cp->qualityLevel);
DRCb4a83232011-08-19 04:57:18 +0000151 encoders[encoding]->setFineQualityLevel(cp->fineQualityLevel,
152 cp->subsampling);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153}
154
155int SMsgWriter::getNumRects(const Rect &r)
156{
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000157 int encoding = cp->currentEncoding();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158
159 if (!encoders[encoding])
160 setupCurrentEncoder();
161
162 return encoders[encoding]->getNumRects(r);
163}
164
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100165bool SMsgWriter::writeSetDesktopSize() {
166 if (!cp->supportsDesktopResize)
167 return false;
168
169 needSetDesktopSize = true;
170
171 return true;
172}
173
174bool SMsgWriter::writeExtendedDesktopSize() {
175 if (!cp->supportsExtendedDesktopSize)
176 return false;
177
178 needExtendedDesktopSize = true;
179
180 return true;
181}
182
183bool SMsgWriter::writeExtendedDesktopSize(rdr::U16 reason, rdr::U16 result,
184 int fb_width, int fb_height,
185 const ScreenSet& layout) {
186 ExtendedDesktopSizeMsg msg;
187
188 if (!cp->supportsExtendedDesktopSize)
189 return false;
190
191 msg.reason = reason;
192 msg.result = result;
193 msg.fb_width = fb_width;
194 msg.fb_height = fb_height;
195 msg.layout = layout;
196
197 extendedDesktopSizeMsgs.push_back(msg);
198
199 return true;
200}
201
202bool SMsgWriter::writeSetDesktopName() {
203 if (!cp->supportsDesktopRename)
204 return false;
205
206 needSetDesktopName = true;
207
208 return true;
209}
210
211void SMsgWriter::cursorChange(WriteSetCursorCallback* cb)
212{
213 wsccb = cb;
214}
215
216void SMsgWriter::writeSetCursor(int width, int height, const Point& hotspot,
217 void* data, void* mask)
218{
219 if (!wsccb)
220 return;
221
222 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
223 throw Exception("SMsgWriter::writeSetCursor: nRects out of sync");
224
225 os->writeS16(hotspot.x);
226 os->writeS16(hotspot.y);
227 os->writeU16(width);
228 os->writeU16(height);
229 os->writeU32(pseudoEncodingCursor);
230 os->writeBytes(data, width * height * (cp->pf().bpp/8));
231 os->writeBytes(mask, (width+7)/8 * height);
232}
233
234void SMsgWriter::writeSetXCursor(int width, int height, int hotspotX,
235 int hotspotY, void* data, void* mask)
236{
237 if (!wsccb)
238 return;
239
240 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
241 throw Exception("SMsgWriter::writeSetXCursor: nRects out of sync");
242
243 os->writeS16(hotspotX);
244 os->writeS16(hotspotY);
245 os->writeU16(width);
246 os->writeU16(height);
247 os->writeU32(pseudoEncodingXCursor);
248 // FIXME: We only support black and white cursors, currently. We
249 // could pass the correct color by using the pix0/pix1 values
250 // returned from getBitmap, in writeSetCursorCallback. However, we
251 // would then need to undo the conversion from rgb to Pixel that is
252 // done by FakeAllocColor.
253 if (width * height) {
254 os->writeU8(0);
255 os->writeU8(0);
256 os->writeU8(0);
257 os->writeU8(255);
258 os->writeU8(255);
259 os->writeU8(255);
260 os->writeBytes(data, (width+7)/8 * height);
261 os->writeBytes(mask, (width+7)/8 * height);
262 }
263}
264
Pierre Ossmane9962f72009-04-23 12:31:42 +0000265bool SMsgWriter::needFakeUpdate()
266{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100267 return wsccb || needSetDesktopName || needNoDataUpdate();
Pierre Ossmane9962f72009-04-23 12:31:42 +0000268}
269
Pierre Ossmane9962f72009-04-23 12:31:42 +0000270bool SMsgWriter::needNoDataUpdate()
271{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100272 return needSetDesktopSize || needExtendedDesktopSize ||
273 !extendedDesktopSizeMsgs.empty();
Pierre Ossmane9962f72009-04-23 12:31:42 +0000274}
275
276void SMsgWriter::writeNoDataUpdate()
277{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100278 int nRects;
279
280 nRects = 0;
281
282 if (needSetDesktopSize)
283 nRects++;
284 if (needExtendedDesktopSize)
285 nRects++;
286 if (!extendedDesktopSizeMsgs.empty())
287 nRects += extendedDesktopSizeMsgs.size();
288
289 writeFramebufferUpdateStart(nRects);
290 writeNoDataRects();
291 writeFramebufferUpdateEnd();
Pierre Ossmane9962f72009-04-23 12:31:42 +0000292}
293
DRCffe09d62011-08-17 02:27:59 +0000294void SMsgWriter::writeRects(const UpdateInfo& ui, TransImageGetter* ig,
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000295 Region* updatedRegion)
296{
297 std::vector<Rect> rects;
298 std::vector<Rect>::const_iterator i;
299 updatedRegion->copyFrom(ui.changed);
300 updatedRegion->assign_union(ui.copied);
301
302 ui.copied.get_rects(&rects, ui.copy_delta.x <= 0, ui.copy_delta.y <= 0);
303 for (i = rects.begin(); i != rects.end(); i++)
304 writeCopyRect(*i, i->tl.x - ui.copy_delta.x, i->tl.y - ui.copy_delta.y);
305
306 ui.changed.get_rects(&rects);
307 for (i = rects.begin(); i != rects.end(); i++) {
308 Rect actual;
309 if (!writeRect(*i, ig, &actual)) {
310 updatedRegion->assign_subtract(*i);
311 updatedRegion->assign_union(actual);
312 }
313 }
314}
315
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100316void SMsgWriter::writeFramebufferUpdateStart(int nRects)
317{
318 startMsg(msgTypeFramebufferUpdate);
319 os->pad(1);
320
321 if (nRects != 0xFFFF) {
322 if (wsccb)
323 nRects++;
324 if (needSetDesktopName)
325 nRects++;
326 }
327
328 os->writeU16(nRects);
329
330 nRectsInUpdate = 0;
331 if (nRects == 0xFFFF)
332 nRectsInHeader = 0;
333 else
334 nRectsInHeader = nRects;
335
336 writePseudoRects();
337}
338
339void SMsgWriter::writeFramebufferUpdateEnd()
340{
341 if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
342 throw Exception("SMsgWriter::writeFramebufferUpdateEnd: "
343 "nRects out of sync");
344
345 if (nRectsInHeader == 0) {
346 // Send last rect. marker
347 os->writeS16(0);
348 os->writeS16(0);
349 os->writeU16(0);
350 os->writeU16(0);
351 os->writeU32(pseudoEncodingLastRect);
352 }
353
354 updatesSent++;
355 endMsg();
356}
357
DRCffe09d62011-08-17 02:27:59 +0000358bool SMsgWriter::writeRect(const Rect& r, TransImageGetter* ig, Rect* actual)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000359{
360 return writeRect(r, cp->currentEncoding(), ig, actual);
361}
362
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000363bool SMsgWriter::writeRect(const Rect& r, int encoding,
DRCffe09d62011-08-17 02:27:59 +0000364 TransImageGetter* ig, Rect* actual)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000365{
366 if (!encoders[encoding]) {
367 encoders[encoding] = Encoder::createEncoder(encoding, this);
368 assert(encoders[encoding]);
369 }
370 return encoders[encoding]->writeRect(r, ig, actual);
371}
372
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000373void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
374{
375 startRect(r,encodingCopyRect);
376 os->writeU16(srcX);
377 os->writeU16(srcY);
378 endRect();
379}
380
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100381void SMsgWriter::startRect(const Rect& r, int encoding)
382{
383 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
384 throw Exception("SMsgWriter::startRect: nRects out of sync");
385
386 currentEncoding = encoding;
387 lenBeforeRect = os->length();
388 if (encoding != encodingCopyRect)
389 rawBytesEquivalent += 12 + r.width() * r.height() * (bpp()/8);
390
391 os->writeS16(r.tl.x);
392 os->writeS16(r.tl.y);
393 os->writeU16(r.width());
394 os->writeU16(r.height());
395 os->writeU32(encoding);
396}
397
398void SMsgWriter::endRect()
399{
400 if (currentEncoding <= encodingMax) {
401 bytesSent[currentEncoding] += os->length() - lenBeforeRect;
402 rectsSent[currentEncoding]++;
403 }
404}
405
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000406rdr::U8* SMsgWriter::getImageBuf(int required, int requested, int* nPixels)
407{
408 int requiredBytes = required * (cp->pf().bpp / 8);
409 int requestedBytes = requested * (cp->pf().bpp / 8);
410 int size = requestedBytes;
411 if (size > imageBufIdealSize) size = imageBufIdealSize;
412
413 if (size < requiredBytes)
414 size = requiredBytes;
415
416 if (imageBufSize < size) {
417 imageBufSize = size;
418 delete [] imageBuf;
419 imageBuf = new rdr::U8[imageBufSize];
420 }
421 if (nPixels)
422 *nPixels = imageBufSize / (cp->pf().bpp / 8);
423 return imageBuf;
424}
425
426int SMsgWriter::bpp()
427{
428 return cp->pf().bpp;
429}
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100430
431void SMsgWriter::startMsg(int type)
432{
433 os->writeU8(type);
434}
435
436void SMsgWriter::endMsg()
437{
438 os->flush();
439}
440
441void SMsgWriter::writePseudoRects()
442{
443 if (wsccb) {
444 wsccb->writeSetCursorCallback();
445 wsccb = 0;
446 }
447
448 if (needSetDesktopName) {
449 if (!cp->supportsDesktopRename)
450 throw Exception("Client does not support desktop rename");
451 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
452 throw Exception("SMsgWriter::setDesktopName: nRects out of sync");
453
454 os->writeS16(0);
455 os->writeS16(0);
456 os->writeU16(0);
457 os->writeU16(0);
458 os->writeU32(pseudoEncodingDesktopName);
459 os->writeString(cp->name());
460
461 needSetDesktopName = false;
462 }
463}
464
465void SMsgWriter::writeNoDataRects()
466{
467 // Start with specific ExtendedDesktopSize messages
468 if (!extendedDesktopSizeMsgs.empty()) {
469 std::list<ExtendedDesktopSizeMsg>::const_iterator ri;
470 ScreenSet::const_iterator si;
471
472 if (!cp->supportsExtendedDesktopSize)
473 throw Exception("Client does not support extended desktop resize");
474 if ((nRectsInUpdate += extendedDesktopSizeMsgs.size()) > nRectsInHeader && nRectsInHeader)
475 throw Exception("SMsgWriter::SetDesktopSize reply: nRects out of sync");
476
477 for (ri = extendedDesktopSizeMsgs.begin();ri != extendedDesktopSizeMsgs.end();++ri) {
478 os->writeU16(ri->reason);
479 os->writeU16(ri->result);
480 os->writeU16(ri->fb_width);
481 os->writeU16(ri->fb_height);
482 os->writeU32(pseudoEncodingExtendedDesktopSize);
483
484 os->writeU8(ri->layout.num_screens());
485 os->pad(3);
486
487 for (si = ri->layout.begin();si != ri->layout.end();++si) {
488 os->writeU32(si->id);
489 os->writeU16(si->dimensions.tl.x);
490 os->writeU16(si->dimensions.tl.y);
491 os->writeU16(si->dimensions.width());
492 os->writeU16(si->dimensions.height());
493 os->writeU32(si->flags);
494 }
495 }
496
497 extendedDesktopSizeMsgs.clear();
498 }
499
500 // Send this before SetDesktopSize to make life easier on the clients
501 if (needExtendedDesktopSize) {
502 if (!cp->supportsExtendedDesktopSize)
503 throw Exception("Client does not support extended desktop resize");
504 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
505 throw Exception("SMsgWriter::setExtendedDesktopSize: nRects out of sync");
506
507 os->writeU16(0);
508 os->writeU16(0);
509 os->writeU16(cp->width);
510 os->writeU16(cp->height);
511 os->writeU32(pseudoEncodingExtendedDesktopSize);
512
513 os->writeU8(cp->screenLayout.num_screens());
514 os->pad(3);
515
516 ScreenSet::const_iterator iter;
517 for (iter = cp->screenLayout.begin();iter != cp->screenLayout.end();++iter) {
518 os->writeU32(iter->id);
519 os->writeU16(iter->dimensions.tl.x);
520 os->writeU16(iter->dimensions.tl.y);
521 os->writeU16(iter->dimensions.width());
522 os->writeU16(iter->dimensions.height());
523 os->writeU32(iter->flags);
524 }
525
526 needExtendedDesktopSize = false;
527 }
528
529 // Some clients assume this is the last rectangle so don't send anything
530 // more after this
531 if (needSetDesktopSize) {
532 if (!cp->supportsDesktopResize)
533 throw Exception("Client does not support desktop resize");
534 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
535 throw Exception("SMsgWriter::setDesktopSize: nRects out of sync");
536
537 os->writeS16(0);
538 os->writeS16(0);
539 os->writeU16(cp->width);
540 os->writeU16(cp->height);
541 os->writeU32(pseudoEncodingDesktopSize);
542
543 needSetDesktopSize = false;
544 }
545}