blob: 7233fbd76c9cd0f714ca0e740a8e48382ccb006e [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{
205 rdr::U8 pr, pg, pb;
206 rdr::U8 sr, sg, sb;
207 int data_len = ((width+7)/8) * height;
208 int mask_len = ((width+7)/8) * height;
209 rdr::U8Array data(data_len);
210 rdr::U8Array mask(mask_len);
211
212 int x, y;
213 rdr::U8 buf[width*height*4];
214 rdr::U8* out;
215
216 if (width * height) {
217 pr = is->readU8();
218 pg = is->readU8();
219 pb = is->readU8();
220
221 sr = is->readU8();
222 sg = is->readU8();
223 sb = is->readU8();
224
225 is->readBytes(data.buf, data_len);
226 is->readBytes(mask.buf, mask_len);
227 }
228
229 int maskBytesPerRow = (width+7)/8;
230 out = buf;
231 for (y = 0;y < height;y++) {
232 for (x = 0;x < width;x++) {
233 int byte = y * maskBytesPerRow + x / 8;
234 int bit = 7 - x % 8;
235
236 if (data.buf[byte] & (1 << bit)) {
237 out[0] = pr;
238 out[1] = pg;
239 out[2] = pb;
240 } else {
241 out[0] = sr;
242 out[1] = sg;
243 out[2] = sb;
244 }
245
246 if (mask.buf[byte] & (1 << bit))
247 out[3] = 255;
248 else
249 out[3] = 0;
250
251 out += 4;
252 }
253 }
254
255 handler->setCursor(width, height, hotspot, buf);
256}
257
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000258void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
259{
260 int data_len = width * height * (handler->cp.pf().bpp/8);
261 int mask_len = ((width+7)/8) * height;
262 rdr::U8Array data(data_len);
263 rdr::U8Array mask(mask_len);
264
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100265 int x, y;
266 rdr::U8 buf[width*height*4];
267 rdr::U8* in;
268 rdr::U8* out;
269
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000270 is->readBytes(data.buf, data_len);
271 is->readBytes(mask.buf, mask_len);
272
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100273 int maskBytesPerRow = (width+7)/8;
274 in = data.buf;
275 out = buf;
276 for (y = 0;y < height;y++) {
277 for (x = 0;x < width;x++) {
278 int byte = y * maskBytesPerRow + x / 8;
279 int bit = 7 - x % 8;
280
281 handler->cp.pf().rgbFromBuffer(out, in, 1);
282
283 if (mask.buf[byte] & (1 << bit))
284 out[3] = 255;
285 else
286 out[3] = 0;
287
288 in += handler->cp.pf().bpp/8;
289 out += 4;
290 }
291 }
292
293 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000294}
295
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100296void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
297{
298 int encoding;
299
300 const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
301 ManagedPixelBuffer pb(rgbaPF, width, height);
302 PixelFormat origPF;
303
304 rdr::U8* buf;
305 int stride;
306
307 encoding = is->readS32();
308
309 origPF = handler->cp.pf();
310 handler->cp.setPF(rgbaPF);
311 handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
312 handler->cp.setPF(origPF);
313
314 // On-wire data has pre-multiplied alpha, but we store it
315 // non-pre-multiplied
316 buf = pb.getBufferRW(pb.getRect(), &stride);
317 assert(stride == width);
318
319 for (int i = 0;i < pb.area();i++) {
320 rdr::U8 alpha;
321
322 alpha = buf[3];
323 if (alpha == 0)
324 alpha = 1; // Avoid division by zero
325
326 buf[0] = (unsigned)buf[0] * 255/alpha;
327 buf[1] = (unsigned)buf[1] * 255/alpha;
328 buf[2] = (unsigned)buf[2] * 255/alpha;
329 buf[3] = alpha;
330
331 buf += 4;
332 }
333
334 pb.commitBufferRW(pb.getRect());
335
336 handler->setCursor(width, height, hotspot,
337 pb.getBuffer(pb.getRect(), &stride));
338}
339
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100340void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
341{
342 char* name = is->readString();
343
344 if (x || y || w || h) {
345 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
346 } else {
347 handler->setName(name);
348 }
349
350 delete [] name;
351}
352
353void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
354{
355 unsigned int screens, i;
356 rdr::U32 id, flags;
357 int sx, sy, sw, sh;
358 ScreenSet layout;
359
360 screens = is->readU8();
361 is->skip(3);
362
363 for (i = 0;i < screens;i++) {
364 id = is->readU32();
365 sx = is->readU16();
366 sy = is->readU16();
367 sw = is->readU16();
368 sh = is->readU16();
369 flags = is->readU32();
370
371 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
372 }
373
374 handler->setExtendedDesktopSize(x, y, w, h, layout);
375}