blob: 17152ab71164ba205fa603b114af5c267442ea25 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01002 * Copyright 2009-2017 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 */
Pierre Ossmana4c0aac2017-02-19 15:50:29 +010019
20#include <assert.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021#include <stdio.h>
Pierre Ossmana4c0aac2017-02-19 15:50:29 +010022
Pierre Ossman7638e9c2014-01-16 13:12:40 +010023#include <rfb/msgTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000024#include <rdr/InStream.h>
25#include <rfb/Exception.h>
26#include <rfb/util.h>
27#include <rfb/CMsgHandler.h>
28#include <rfb/CMsgReader.h>
29
30using namespace rfb;
31
32CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
33 : imageBufIdealSize(0), handler(handler_), is(is_),
Pierre Ossman7bfb73b2015-11-10 13:03:26 +010034 nUpdateRectsLeft(0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036}
37
38CMsgReader::~CMsgReader()
39{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040}
41
Pierre Ossman7638e9c2014-01-16 13:12:40 +010042void CMsgReader::readServerInit()
43{
44 int width = is->readU16();
45 int height = is->readU16();
Pierre Ossman7638e9c2014-01-16 13:12:40 +010046 PixelFormat pf;
47 pf.read(is);
Pierre Ossman7638e9c2014-01-16 13:12:40 +010048 CharArray name(is->readString());
Pierre Ossmandd45b442018-10-31 17:08:59 +010049 handler->serverInit(width, height, pf, name.buf);
Pierre Ossman7638e9c2014-01-16 13:12:40 +010050}
51
52void CMsgReader::readMsg()
53{
54 if (nUpdateRectsLeft == 0) {
55 int type = is->readU8();
56
57 switch (type) {
58 case msgTypeSetColourMapEntries:
59 readSetColourMapEntries();
60 break;
61 case msgTypeBell:
62 readBell();
63 break;
64 case msgTypeServerCutText:
65 readServerCutText();
66 break;
67 case msgTypeFramebufferUpdate:
68 readFramebufferUpdate();
69 break;
70 case msgTypeServerFence:
71 readFence();
72 break;
73 case msgTypeEndOfContinuousUpdates:
74 readEndOfContinuousUpdates();
75 break;
76 default:
77 fprintf(stderr, "unknown message type %d\n", type);
78 throw Exception("unknown message type");
79 }
80 } else {
81 int x = is->readU16();
82 int y = is->readU16();
83 int w = is->readU16();
84 int h = is->readU16();
85 int encoding = is->readS32();
86
87 switch (encoding) {
88 case pseudoEncodingLastRect:
89 nUpdateRectsLeft = 1; // this rectangle is the last one
90 break;
Pierre Ossman6b68f972017-02-19 15:51:19 +010091 case pseudoEncodingXCursor:
92 readSetXCursor(w, h, Point(x,y));
93 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +010094 case pseudoEncodingCursor:
95 readSetCursor(w, h, Point(x,y));
96 break;
Pierre Ossmana4c0aac2017-02-19 15:50:29 +010097 case pseudoEncodingCursorWithAlpha:
98 readSetCursorWithAlpha(w, h, Point(x,y));
99 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100100 case pseudoEncodingDesktopName:
101 readSetDesktopName(x, y, w, h);
102 break;
103 case pseudoEncodingDesktopSize:
104 handler->setDesktopSize(w, h);
105 break;
106 case pseudoEncodingExtendedDesktopSize:
107 readExtendedDesktopSize(x, y, w, h);
108 break;
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100109 case pseudoEncodingLEDState:
110 readLEDState();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200111 case pseudoEncodingQEMUKeyEvent:
112 handler->supportsQEMUKeyEvent();
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100113 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100114 default:
115 readRect(Rect(x, y, x+w, y+h), encoding);
116 break;
117 };
118
119 nUpdateRectsLeft--;
120 if (nUpdateRectsLeft == 0)
121 handler->framebufferUpdateEnd();
122 }
123}
124
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125void CMsgReader::readSetColourMapEntries()
126{
127 is->skip(1);
128 int firstColour = is->readU16();
129 int nColours = is->readU16();
130 rdr::U16Array rgbs(nColours * 3);
131 for (int i = 0; i < nColours * 3; i++)
132 rgbs.buf[i] = is->readU16();
133 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
134}
135
136void CMsgReader::readBell()
137{
138 handler->bell();
139}
140
141void CMsgReader::readServerCutText()
142{
143 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000144 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 if (len > 256*1024) {
146 is->skip(len);
147 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
148 return;
149 }
150 CharArray ca(len+1);
151 ca.buf[len] = 0;
152 is->readBytes(ca.buf, len);
153 handler->serverCutText(ca.buf, len);
154}
155
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100156void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100158 rdr::U32 flags;
159 rdr::U8 len;
160 char data[64];
161
162 is->skip(3);
163
164 flags = is->readU32();
165
166 len = is->readU8();
167 if (len > sizeof(data)) {
168 fprintf(stderr, "Ignoring fence with too large payload\n");
169 is->skip(len);
170 return;
171 }
172
173 is->readBytes(data, len);
174
175 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176}
177
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100178void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100180 handler->endOfContinuousUpdates();
181}
182
183void CMsgReader::readFramebufferUpdate()
184{
185 is->skip(1);
186 nUpdateRectsLeft = is->readU16();
187 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188}
189
Peter Ã…strand98fe98c2010-02-10 07:43:02 +0000190void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191{
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200192 if ((r.br.x > handler->server.width()) ||
193 (r.br.y > handler->server.height())) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
195 r.width(), r.height(), r.tl.x, r.tl.y,
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200196 handler->server.width(), handler->server.height());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 throw Exception("Rect too big");
198 }
199
200 if (r.is_empty())
201 fprintf(stderr, "Warning: zero size rect\n");
202
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100203 handler->dataRect(r, encoding);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204}
205
Pierre Ossman6b68f972017-02-19 15:51:19 +0100206void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
207{
Michal Srbc26b4b32017-04-06 23:52:22 +0300208 if (width > maxCursorSize || height > maxCursorSize)
209 throw Exception("Too big cursor");
210
Pierre Ossman6b68f972017-02-19 15:51:19 +0100211 rdr::U8 buf[width*height*4];
Pierre Ossman6b68f972017-02-19 15:51:19 +0100212
Brian P. Hinz33d78322017-11-16 17:46:15 -0500213 if (width * height > 0) {
Pierre Ossman9c88e0d2018-09-13 12:30:30 +0200214 rdr::U8 pr, pg, pb;
215 rdr::U8 sr, sg, sb;
216 int data_len = ((width+7)/8) * height;
217 int mask_len = ((width+7)/8) * height;
218 rdr::U8Array data(data_len);
219 rdr::U8Array mask(mask_len);
220
221 int x, y;
222 rdr::U8* out;
223
Pierre Ossman6b68f972017-02-19 15:51:19 +0100224 pr = is->readU8();
225 pg = is->readU8();
226 pb = is->readU8();
227
228 sr = is->readU8();
229 sg = is->readU8();
230 sb = is->readU8();
231
232 is->readBytes(data.buf, data_len);
233 is->readBytes(mask.buf, mask_len);
Pierre Ossman6b68f972017-02-19 15:51:19 +0100234
Pierre Ossman9c88e0d2018-09-13 12:30:30 +0200235 int maskBytesPerRow = (width+7)/8;
236 out = buf;
237 for (y = 0;y < height;y++) {
238 for (x = 0;x < width;x++) {
239 int byte = y * maskBytesPerRow + x / 8;
240 int bit = 7 - x % 8;
Pierre Ossman6b68f972017-02-19 15:51:19 +0100241
Pierre Ossman9c88e0d2018-09-13 12:30:30 +0200242 if (data.buf[byte] & (1 << bit)) {
243 out[0] = pr;
244 out[1] = pg;
245 out[2] = pb;
246 } else {
247 out[0] = sr;
248 out[1] = sg;
249 out[2] = sb;
250 }
251
252 if (mask.buf[byte] & (1 << bit))
253 out[3] = 255;
254 else
255 out[3] = 0;
256
257 out += 4;
Pierre Ossman6b68f972017-02-19 15:51:19 +0100258 }
Pierre Ossman6b68f972017-02-19 15:51:19 +0100259 }
260 }
261
262 handler->setCursor(width, height, hotspot, buf);
263}
264
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000265void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
266{
Michal Srbc26b4b32017-04-06 23:52:22 +0300267 if (width > maxCursorSize || height > maxCursorSize)
268 throw Exception("Too big cursor");
269
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200270 int data_len = width * height * (handler->server.pf().bpp/8);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000271 int mask_len = ((width+7)/8) * height;
272 rdr::U8Array data(data_len);
273 rdr::U8Array mask(mask_len);
274
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100275 int x, y;
276 rdr::U8 buf[width*height*4];
277 rdr::U8* in;
278 rdr::U8* out;
279
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000280 is->readBytes(data.buf, data_len);
281 is->readBytes(mask.buf, mask_len);
282
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100283 int maskBytesPerRow = (width+7)/8;
284 in = data.buf;
285 out = buf;
286 for (y = 0;y < height;y++) {
287 for (x = 0;x < width;x++) {
288 int byte = y * maskBytesPerRow + x / 8;
289 int bit = 7 - x % 8;
290
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200291 handler->server.pf().rgbFromBuffer(out, in, 1);
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100292
293 if (mask.buf[byte] & (1 << bit))
294 out[3] = 255;
295 else
296 out[3] = 0;
297
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200298 in += handler->server.pf().bpp/8;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100299 out += 4;
300 }
301 }
302
303 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304}
305
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100306void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
307{
Michal Srbc26b4b32017-04-06 23:52:22 +0300308 if (width > maxCursorSize || height > maxCursorSize)
309 throw Exception("Too big cursor");
310
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100311 int encoding;
312
313 const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
314 ManagedPixelBuffer pb(rgbaPF, width, height);
315 PixelFormat origPF;
316
317 rdr::U8* buf;
318 int stride;
319
320 encoding = is->readS32();
321
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200322 origPF = handler->server.pf();
323 handler->server.setPF(rgbaPF);
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100324 handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
Pierre Ossmanb14a6bc2018-06-18 15:44:26 +0200325 handler->server.setPF(origPF);
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100326
327 // On-wire data has pre-multiplied alpha, but we store it
328 // non-pre-multiplied
329 buf = pb.getBufferRW(pb.getRect(), &stride);
330 assert(stride == width);
331
332 for (int i = 0;i < pb.area();i++) {
333 rdr::U8 alpha;
334
335 alpha = buf[3];
336 if (alpha == 0)
337 alpha = 1; // Avoid division by zero
338
339 buf[0] = (unsigned)buf[0] * 255/alpha;
340 buf[1] = (unsigned)buf[1] * 255/alpha;
341 buf[2] = (unsigned)buf[2] * 255/alpha;
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100342
343 buf += 4;
344 }
345
346 pb.commitBufferRW(pb.getRect());
347
348 handler->setCursor(width, height, hotspot,
349 pb.getBuffer(pb.getRect(), &stride));
350}
351
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100352void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
353{
354 char* name = is->readString();
355
356 if (x || y || w || h) {
357 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
358 } else {
359 handler->setName(name);
360 }
361
362 delete [] name;
363}
364
365void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
366{
367 unsigned int screens, i;
368 rdr::U32 id, flags;
369 int sx, sy, sw, sh;
370 ScreenSet layout;
371
372 screens = is->readU8();
373 is->skip(3);
374
375 for (i = 0;i < screens;i++) {
376 id = is->readU32();
377 sx = is->readU16();
378 sy = is->readU16();
379 sw = is->readU16();
380 sh = is->readU16();
381 flags = is->readU32();
382
383 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
384 }
385
386 handler->setExtendedDesktopSize(x, y, w, h, layout);
387}
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100388
389void CMsgReader::readLEDState()
390{
391 rdr::U8 state;
392
393 state = is->readU8();
394
395 handler->setLEDState(state);
396}