blob: 9abe3f244ca471cf190638eafafb7b8f5f10e6f2 [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;
112 default:
113 readRect(Rect(x, y, x+w, y+h), encoding);
114 break;
115 };
116
117 nUpdateRectsLeft--;
118 if (nUpdateRectsLeft == 0)
119 handler->framebufferUpdateEnd();
120 }
121}
122
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123void CMsgReader::readSetColourMapEntries()
124{
125 is->skip(1);
126 int firstColour = is->readU16();
127 int nColours = is->readU16();
128 rdr::U16Array rgbs(nColours * 3);
129 for (int i = 0; i < nColours * 3; i++)
130 rgbs.buf[i] = is->readU16();
131 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
132}
133
134void CMsgReader::readBell()
135{
136 handler->bell();
137}
138
139void CMsgReader::readServerCutText()
140{
141 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000142 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 if (len > 256*1024) {
144 is->skip(len);
145 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
146 return;
147 }
148 CharArray ca(len+1);
149 ca.buf[len] = 0;
150 is->readBytes(ca.buf, len);
151 handler->serverCutText(ca.buf, len);
152}
153
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100154void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100156 rdr::U32 flags;
157 rdr::U8 len;
158 char data[64];
159
160 is->skip(3);
161
162 flags = is->readU32();
163
164 len = is->readU8();
165 if (len > sizeof(data)) {
166 fprintf(stderr, "Ignoring fence with too large payload\n");
167 is->skip(len);
168 return;
169 }
170
171 is->readBytes(data, len);
172
173 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174}
175
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100176void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100178 handler->endOfContinuousUpdates();
179}
180
181void CMsgReader::readFramebufferUpdate()
182{
183 is->skip(1);
184 nUpdateRectsLeft = is->readU16();
185 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186}
187
Peter Ã…strand98fe98c2010-02-10 07:43:02 +0000188void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189{
190 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
191 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
192 r.width(), r.height(), r.tl.x, r.tl.y,
193 handler->cp.width, handler->cp.height);
194 throw Exception("Rect too big");
195 }
196
197 if (r.is_empty())
198 fprintf(stderr, "Warning: zero size rect\n");
199
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100200 handler->dataRect(r, encoding);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201}
202
Pierre Ossman6b68f972017-02-19 15:51:19 +0100203void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
204{
Michal Srbc26b4b32017-04-06 23:52:22 +0300205 if (width > maxCursorSize || height > maxCursorSize)
206 throw Exception("Too big cursor");
207
Pierre Ossman6b68f972017-02-19 15:51:19 +0100208 rdr::U8 pr, pg, pb;
209 rdr::U8 sr, sg, sb;
210 int data_len = ((width+7)/8) * height;
211 int mask_len = ((width+7)/8) * height;
212 rdr::U8Array data(data_len);
213 rdr::U8Array mask(mask_len);
214
215 int x, y;
216 rdr::U8 buf[width*height*4];
217 rdr::U8* out;
218
219 if (width * height) {
220 pr = is->readU8();
221 pg = is->readU8();
222 pb = is->readU8();
223
224 sr = is->readU8();
225 sg = is->readU8();
226 sb = is->readU8();
227
228 is->readBytes(data.buf, data_len);
229 is->readBytes(mask.buf, mask_len);
230 }
231
232 int maskBytesPerRow = (width+7)/8;
233 out = buf;
234 for (y = 0;y < height;y++) {
235 for (x = 0;x < width;x++) {
236 int byte = y * maskBytesPerRow + x / 8;
237 int bit = 7 - x % 8;
238
239 if (data.buf[byte] & (1 << bit)) {
240 out[0] = pr;
241 out[1] = pg;
242 out[2] = pb;
243 } else {
244 out[0] = sr;
245 out[1] = sg;
246 out[2] = sb;
247 }
248
249 if (mask.buf[byte] & (1 << bit))
250 out[3] = 255;
251 else
252 out[3] = 0;
253
254 out += 4;
255 }
256 }
257
258 handler->setCursor(width, height, hotspot, buf);
259}
260
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000261void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
262{
Michal Srbc26b4b32017-04-06 23:52:22 +0300263 if (width > maxCursorSize || height > maxCursorSize)
264 throw Exception("Too big cursor");
265
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000266 int data_len = width * height * (handler->cp.pf().bpp/8);
267 int mask_len = ((width+7)/8) * height;
268 rdr::U8Array data(data_len);
269 rdr::U8Array mask(mask_len);
270
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100271 int x, y;
272 rdr::U8 buf[width*height*4];
273 rdr::U8* in;
274 rdr::U8* out;
275
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000276 is->readBytes(data.buf, data_len);
277 is->readBytes(mask.buf, mask_len);
278
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100279 int maskBytesPerRow = (width+7)/8;
280 in = data.buf;
281 out = buf;
282 for (y = 0;y < height;y++) {
283 for (x = 0;x < width;x++) {
284 int byte = y * maskBytesPerRow + x / 8;
285 int bit = 7 - x % 8;
286
287 handler->cp.pf().rgbFromBuffer(out, in, 1);
288
289 if (mask.buf[byte] & (1 << bit))
290 out[3] = 255;
291 else
292 out[3] = 0;
293
294 in += handler->cp.pf().bpp/8;
295 out += 4;
296 }
297 }
298
299 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000300}
301
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100302void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
303{
Michal Srbc26b4b32017-04-06 23:52:22 +0300304 if (width > maxCursorSize || height > maxCursorSize)
305 throw Exception("Too big cursor");
306
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100307 int encoding;
308
309 const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
310 ManagedPixelBuffer pb(rgbaPF, width, height);
311 PixelFormat origPF;
312
313 rdr::U8* buf;
314 int stride;
315
316 encoding = is->readS32();
317
318 origPF = handler->cp.pf();
319 handler->cp.setPF(rgbaPF);
320 handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
321 handler->cp.setPF(origPF);
322
323 // On-wire data has pre-multiplied alpha, but we store it
324 // non-pre-multiplied
325 buf = pb.getBufferRW(pb.getRect(), &stride);
326 assert(stride == width);
327
328 for (int i = 0;i < pb.area();i++) {
329 rdr::U8 alpha;
330
331 alpha = buf[3];
332 if (alpha == 0)
333 alpha = 1; // Avoid division by zero
334
335 buf[0] = (unsigned)buf[0] * 255/alpha;
336 buf[1] = (unsigned)buf[1] * 255/alpha;
337 buf[2] = (unsigned)buf[2] * 255/alpha;
338 buf[3] = alpha;
339
340 buf += 4;
341 }
342
343 pb.commitBufferRW(pb.getRect());
344
345 handler->setCursor(width, height, hotspot,
346 pb.getBuffer(pb.getRect(), &stride));
347}
348
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100349void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
350{
351 char* name = is->readString();
352
353 if (x || y || w || h) {
354 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
355 } else {
356 handler->setName(name);
357 }
358
359 delete [] name;
360}
361
362void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
363{
364 unsigned int screens, i;
365 rdr::U32 id, flags;
366 int sx, sy, sw, sh;
367 ScreenSet layout;
368
369 screens = is->readU8();
370 is->skip(3);
371
372 for (i = 0;i < screens;i++) {
373 id = is->readU32();
374 sx = is->readU16();
375 sy = is->readU16();
376 sw = is->readU16();
377 sh = is->readU16();
378 flags = is->readU32();
379
380 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
381 }
382
383 handler->setExtendedDesktopSize(x, y, w, h, layout);
384}