blob: 1d359d2c74e22b1a682e53e037d48281fd969036 [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();
46 handler->setDesktopSize(width, height);
47 PixelFormat pf;
48 pf.read(is);
49 handler->setPixelFormat(pf);
50 CharArray name(is->readString());
51 handler->setName(name.buf);
52 handler->serverInit();
53}
54
55void CMsgReader::readMsg()
56{
57 if (nUpdateRectsLeft == 0) {
58 int type = is->readU8();
59
60 switch (type) {
61 case msgTypeSetColourMapEntries:
62 readSetColourMapEntries();
63 break;
64 case msgTypeBell:
65 readBell();
66 break;
67 case msgTypeServerCutText:
68 readServerCutText();
69 break;
70 case msgTypeFramebufferUpdate:
71 readFramebufferUpdate();
72 break;
73 case msgTypeServerFence:
74 readFence();
75 break;
76 case msgTypeEndOfContinuousUpdates:
77 readEndOfContinuousUpdates();
78 break;
79 default:
80 fprintf(stderr, "unknown message type %d\n", type);
81 throw Exception("unknown message type");
82 }
83 } else {
84 int x = is->readU16();
85 int y = is->readU16();
86 int w = is->readU16();
87 int h = is->readU16();
88 int encoding = is->readS32();
89
90 switch (encoding) {
91 case pseudoEncodingLastRect:
92 nUpdateRectsLeft = 1; // this rectangle is the last one
93 break;
Pierre Ossman6b68f972017-02-19 15:51:19 +010094 case pseudoEncodingXCursor:
95 readSetXCursor(w, h, Point(x,y));
96 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +010097 case pseudoEncodingCursor:
98 readSetCursor(w, h, Point(x,y));
99 break;
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100100 case pseudoEncodingCursorWithAlpha:
101 readSetCursorWithAlpha(w, h, Point(x,y));
102 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100103 case pseudoEncodingDesktopName:
104 readSetDesktopName(x, y, w, h);
105 break;
106 case pseudoEncodingDesktopSize:
107 handler->setDesktopSize(w, h);
108 break;
109 case pseudoEncodingExtendedDesktopSize:
110 readExtendedDesktopSize(x, y, w, h);
111 break;
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100112 case pseudoEncodingLEDState:
113 readLEDState();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200114 case pseudoEncodingQEMUKeyEvent:
115 handler->supportsQEMUKeyEvent();
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100116 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100117 default:
118 readRect(Rect(x, y, x+w, y+h), encoding);
119 break;
120 };
121
122 nUpdateRectsLeft--;
123 if (nUpdateRectsLeft == 0)
124 handler->framebufferUpdateEnd();
125 }
126}
127
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128void CMsgReader::readSetColourMapEntries()
129{
130 is->skip(1);
131 int firstColour = is->readU16();
132 int nColours = is->readU16();
133 rdr::U16Array rgbs(nColours * 3);
134 for (int i = 0; i < nColours * 3; i++)
135 rgbs.buf[i] = is->readU16();
136 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
137}
138
139void CMsgReader::readBell()
140{
141 handler->bell();
142}
143
144void CMsgReader::readServerCutText()
145{
146 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000147 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000148 if (len > 256*1024) {
149 is->skip(len);
150 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
151 return;
152 }
153 CharArray ca(len+1);
154 ca.buf[len] = 0;
155 is->readBytes(ca.buf, len);
156 handler->serverCutText(ca.buf, len);
157}
158
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100159void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100161 rdr::U32 flags;
162 rdr::U8 len;
163 char data[64];
164
165 is->skip(3);
166
167 flags = is->readU32();
168
169 len = is->readU8();
170 if (len > sizeof(data)) {
171 fprintf(stderr, "Ignoring fence with too large payload\n");
172 is->skip(len);
173 return;
174 }
175
176 is->readBytes(data, len);
177
178 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179}
180
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100181void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100183 handler->endOfContinuousUpdates();
184}
185
186void CMsgReader::readFramebufferUpdate()
187{
188 is->skip(1);
189 nUpdateRectsLeft = is->readU16();
190 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191}
192
Peter Ã…strand98fe98c2010-02-10 07:43:02 +0000193void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194{
195 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
196 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
197 r.width(), r.height(), r.tl.x, r.tl.y,
198 handler->cp.width, handler->cp.height);
199 throw Exception("Rect too big");
200 }
201
202 if (r.is_empty())
203 fprintf(stderr, "Warning: zero size rect\n");
204
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100205 handler->dataRect(r, encoding);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206}
207
Pierre Ossman6b68f972017-02-19 15:51:19 +0100208void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
209{
Michal Srbc26b4b32017-04-06 23:52:22 +0300210 if (width > maxCursorSize || height > maxCursorSize)
211 throw Exception("Too big cursor");
212
Pierre Ossman6b68f972017-02-19 15:51:19 +0100213 rdr::U8 buf[width*height*4];
Pierre Ossman6b68f972017-02-19 15:51:19 +0100214
Brian P. Hinz33d78322017-11-16 17:46:15 -0500215 if (width * height > 0) {
Pierre Ossman9c88e0d2018-09-13 12:30:30 +0200216 rdr::U8 pr, pg, pb;
217 rdr::U8 sr, sg, sb;
218 int data_len = ((width+7)/8) * height;
219 int mask_len = ((width+7)/8) * height;
220 rdr::U8Array data(data_len);
221 rdr::U8Array mask(mask_len);
222
223 int x, y;
224 rdr::U8* out;
225
Pierre Ossman6b68f972017-02-19 15:51:19 +0100226 pr = is->readU8();
227 pg = is->readU8();
228 pb = is->readU8();
229
230 sr = is->readU8();
231 sg = is->readU8();
232 sb = is->readU8();
233
234 is->readBytes(data.buf, data_len);
235 is->readBytes(mask.buf, mask_len);
Pierre Ossman6b68f972017-02-19 15:51:19 +0100236
Pierre Ossman9c88e0d2018-09-13 12:30:30 +0200237 int maskBytesPerRow = (width+7)/8;
238 out = buf;
239 for (y = 0;y < height;y++) {
240 for (x = 0;x < width;x++) {
241 int byte = y * maskBytesPerRow + x / 8;
242 int bit = 7 - x % 8;
Pierre Ossman6b68f972017-02-19 15:51:19 +0100243
Pierre Ossman9c88e0d2018-09-13 12:30:30 +0200244 if (data.buf[byte] & (1 << bit)) {
245 out[0] = pr;
246 out[1] = pg;
247 out[2] = pb;
248 } else {
249 out[0] = sr;
250 out[1] = sg;
251 out[2] = sb;
252 }
253
254 if (mask.buf[byte] & (1 << bit))
255 out[3] = 255;
256 else
257 out[3] = 0;
258
259 out += 4;
Pierre Ossman6b68f972017-02-19 15:51:19 +0100260 }
Pierre Ossman6b68f972017-02-19 15:51:19 +0100261 }
262 }
263
264 handler->setCursor(width, height, hotspot, buf);
265}
266
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000267void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
268{
Michal Srbc26b4b32017-04-06 23:52:22 +0300269 if (width > maxCursorSize || height > maxCursorSize)
270 throw Exception("Too big cursor");
271
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000272 int data_len = width * height * (handler->cp.pf().bpp/8);
273 int mask_len = ((width+7)/8) * height;
274 rdr::U8Array data(data_len);
275 rdr::U8Array mask(mask_len);
276
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100277 int x, y;
278 rdr::U8 buf[width*height*4];
279 rdr::U8* in;
280 rdr::U8* out;
281
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000282 is->readBytes(data.buf, data_len);
283 is->readBytes(mask.buf, mask_len);
284
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100285 int maskBytesPerRow = (width+7)/8;
286 in = data.buf;
287 out = buf;
288 for (y = 0;y < height;y++) {
289 for (x = 0;x < width;x++) {
290 int byte = y * maskBytesPerRow + x / 8;
291 int bit = 7 - x % 8;
292
293 handler->cp.pf().rgbFromBuffer(out, in, 1);
294
295 if (mask.buf[byte] & (1 << bit))
296 out[3] = 255;
297 else
298 out[3] = 0;
299
300 in += handler->cp.pf().bpp/8;
301 out += 4;
302 }
303 }
304
305 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000306}
307
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100308void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
309{
Michal Srbc26b4b32017-04-06 23:52:22 +0300310 if (width > maxCursorSize || height > maxCursorSize)
311 throw Exception("Too big cursor");
312
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100313 int encoding;
314
315 const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
316 ManagedPixelBuffer pb(rgbaPF, width, height);
317 PixelFormat origPF;
318
319 rdr::U8* buf;
320 int stride;
321
322 encoding = is->readS32();
323
324 origPF = handler->cp.pf();
325 handler->cp.setPF(rgbaPF);
326 handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
327 handler->cp.setPF(origPF);
328
329 // On-wire data has pre-multiplied alpha, but we store it
330 // non-pre-multiplied
331 buf = pb.getBufferRW(pb.getRect(), &stride);
332 assert(stride == width);
333
334 for (int i = 0;i < pb.area();i++) {
335 rdr::U8 alpha;
336
337 alpha = buf[3];
338 if (alpha == 0)
339 alpha = 1; // Avoid division by zero
340
341 buf[0] = (unsigned)buf[0] * 255/alpha;
342 buf[1] = (unsigned)buf[1] * 255/alpha;
343 buf[2] = (unsigned)buf[2] * 255/alpha;
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100344
345 buf += 4;
346 }
347
348 pb.commitBufferRW(pb.getRect());
349
350 handler->setCursor(width, height, hotspot,
351 pb.getBuffer(pb.getRect(), &stride));
352}
353
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100354void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
355{
356 char* name = is->readString();
357
358 if (x || y || w || h) {
359 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
360 } else {
361 handler->setName(name);
362 }
363
364 delete [] name;
365}
366
367void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
368{
369 unsigned int screens, i;
370 rdr::U32 id, flags;
371 int sx, sy, sw, sh;
372 ScreenSet layout;
373
374 screens = is->readU8();
375 is->skip(3);
376
377 for (i = 0;i < screens;i++) {
378 id = is->readU32();
379 sx = is->readU16();
380 sy = is->readU16();
381 sw = is->readU16();
382 sh = is->readU16();
383 flags = is->readU32();
384
385 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
386 }
387
388 handler->setExtendedDesktopSize(x, y, w, h, layout);
389}
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100390
391void CMsgReader::readLEDState()
392{
393 rdr::U8 state;
394
395 state = is->readU8();
396
397 handler->setLEDState(state);
398}