blob: 0aaf71fabb3dff46aac90561973fe10bc38d0492 [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();
114 break;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100115 default:
116 readRect(Rect(x, y, x+w, y+h), encoding);
117 break;
118 };
119
120 nUpdateRectsLeft--;
121 if (nUpdateRectsLeft == 0)
122 handler->framebufferUpdateEnd();
123 }
124}
125
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126void CMsgReader::readSetColourMapEntries()
127{
128 is->skip(1);
129 int firstColour = is->readU16();
130 int nColours = is->readU16();
131 rdr::U16Array rgbs(nColours * 3);
132 for (int i = 0; i < nColours * 3; i++)
133 rgbs.buf[i] = is->readU16();
134 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
135}
136
137void CMsgReader::readBell()
138{
139 handler->bell();
140}
141
142void CMsgReader::readServerCutText()
143{
144 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000145 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 if (len > 256*1024) {
147 is->skip(len);
148 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
149 return;
150 }
151 CharArray ca(len+1);
152 ca.buf[len] = 0;
153 is->readBytes(ca.buf, len);
154 handler->serverCutText(ca.buf, len);
155}
156
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100157void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100159 rdr::U32 flags;
160 rdr::U8 len;
161 char data[64];
162
163 is->skip(3);
164
165 flags = is->readU32();
166
167 len = is->readU8();
168 if (len > sizeof(data)) {
169 fprintf(stderr, "Ignoring fence with too large payload\n");
170 is->skip(len);
171 return;
172 }
173
174 is->readBytes(data, len);
175
176 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177}
178
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100179void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100181 handler->endOfContinuousUpdates();
182}
183
184void CMsgReader::readFramebufferUpdate()
185{
186 is->skip(1);
187 nUpdateRectsLeft = is->readU16();
188 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189}
190
Peter Ã…strand98fe98c2010-02-10 07:43:02 +0000191void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192{
193 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
194 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,
196 handler->cp.width, handler->cp.height);
197 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 pr, pg, pb;
212 rdr::U8 sr, sg, sb;
213 int data_len = ((width+7)/8) * height;
214 int mask_len = ((width+7)/8) * height;
215 rdr::U8Array data(data_len);
216 rdr::U8Array mask(mask_len);
217
218 int x, y;
219 rdr::U8 buf[width*height*4];
220 rdr::U8* out;
221
222 if (width * height) {
223 pr = is->readU8();
224 pg = is->readU8();
225 pb = is->readU8();
226
227 sr = is->readU8();
228 sg = is->readU8();
229 sb = is->readU8();
230
231 is->readBytes(data.buf, data_len);
232 is->readBytes(mask.buf, mask_len);
233 }
234
235 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;
241
242 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;
258 }
259 }
260
261 handler->setCursor(width, height, hotspot, buf);
262}
263
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000264void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
265{
Michal Srbc26b4b32017-04-06 23:52:22 +0300266 if (width > maxCursorSize || height > maxCursorSize)
267 throw Exception("Too big cursor");
268
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000269 int data_len = width * height * (handler->cp.pf().bpp/8);
270 int mask_len = ((width+7)/8) * height;
271 rdr::U8Array data(data_len);
272 rdr::U8Array mask(mask_len);
273
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100274 int x, y;
275 rdr::U8 buf[width*height*4];
276 rdr::U8* in;
277 rdr::U8* out;
278
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000279 is->readBytes(data.buf, data_len);
280 is->readBytes(mask.buf, mask_len);
281
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100282 int maskBytesPerRow = (width+7)/8;
283 in = data.buf;
284 out = buf;
285 for (y = 0;y < height;y++) {
286 for (x = 0;x < width;x++) {
287 int byte = y * maskBytesPerRow + x / 8;
288 int bit = 7 - x % 8;
289
290 handler->cp.pf().rgbFromBuffer(out, in, 1);
291
292 if (mask.buf[byte] & (1 << bit))
293 out[3] = 255;
294 else
295 out[3] = 0;
296
297 in += handler->cp.pf().bpp/8;
298 out += 4;
299 }
300 }
301
302 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000303}
304
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100305void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
306{
Michal Srbc26b4b32017-04-06 23:52:22 +0300307 if (width > maxCursorSize || height > maxCursorSize)
308 throw Exception("Too big cursor");
309
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100310 int encoding;
311
312 const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
313 ManagedPixelBuffer pb(rgbaPF, width, height);
314 PixelFormat origPF;
315
316 rdr::U8* buf;
317 int stride;
318
319 encoding = is->readS32();
320
321 origPF = handler->cp.pf();
322 handler->cp.setPF(rgbaPF);
323 handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
324 handler->cp.setPF(origPF);
325
326 // On-wire data has pre-multiplied alpha, but we store it
327 // non-pre-multiplied
328 buf = pb.getBufferRW(pb.getRect(), &stride);
329 assert(stride == width);
330
331 for (int i = 0;i < pb.area();i++) {
332 rdr::U8 alpha;
333
334 alpha = buf[3];
335 if (alpha == 0)
336 alpha = 1; // Avoid division by zero
337
338 buf[0] = (unsigned)buf[0] * 255/alpha;
339 buf[1] = (unsigned)buf[1] * 255/alpha;
340 buf[2] = (unsigned)buf[2] * 255/alpha;
341 buf[3] = alpha;
342
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}