blob: 152c2c85701e214cb14ffd7357dfa08d2ae6c08e [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 */
19#include <stdio.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010020#include <rfb/msgTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021#include <rdr/InStream.h>
22#include <rfb/Exception.h>
23#include <rfb/util.h>
24#include <rfb/CMsgHandler.h>
25#include <rfb/CMsgReader.h>
26
27using namespace rfb;
28
29CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
30 : imageBufIdealSize(0), handler(handler_), is(is_),
Pierre Ossman7bfb73b2015-11-10 13:03:26 +010031 nUpdateRectsLeft(0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033}
34
35CMsgReader::~CMsgReader()
36{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037}
38
Pierre Ossman7638e9c2014-01-16 13:12:40 +010039void CMsgReader::readServerInit()
40{
41 int width = is->readU16();
42 int height = is->readU16();
43 handler->setDesktopSize(width, height);
44 PixelFormat pf;
45 pf.read(is);
46 handler->setPixelFormat(pf);
47 CharArray name(is->readString());
48 handler->setName(name.buf);
49 handler->serverInit();
50}
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;
97 case pseudoEncodingDesktopName:
98 readSetDesktopName(x, y, w, h);
99 break;
100 case pseudoEncodingDesktopSize:
101 handler->setDesktopSize(w, h);
102 break;
103 case pseudoEncodingExtendedDesktopSize:
104 readExtendedDesktopSize(x, y, w, h);
105 break;
106 default:
107 readRect(Rect(x, y, x+w, y+h), encoding);
108 break;
109 };
110
111 nUpdateRectsLeft--;
112 if (nUpdateRectsLeft == 0)
113 handler->framebufferUpdateEnd();
114 }
115}
116
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117void CMsgReader::readSetColourMapEntries()
118{
119 is->skip(1);
120 int firstColour = is->readU16();
121 int nColours = is->readU16();
122 rdr::U16Array rgbs(nColours * 3);
123 for (int i = 0; i < nColours * 3; i++)
124 rgbs.buf[i] = is->readU16();
125 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
126}
127
128void CMsgReader::readBell()
129{
130 handler->bell();
131}
132
133void CMsgReader::readServerCutText()
134{
135 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000136 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 if (len > 256*1024) {
138 is->skip(len);
139 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
140 return;
141 }
142 CharArray ca(len+1);
143 ca.buf[len] = 0;
144 is->readBytes(ca.buf, len);
145 handler->serverCutText(ca.buf, len);
146}
147
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100148void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100150 rdr::U32 flags;
151 rdr::U8 len;
152 char data[64];
153
154 is->skip(3);
155
156 flags = is->readU32();
157
158 len = is->readU8();
159 if (len > sizeof(data)) {
160 fprintf(stderr, "Ignoring fence with too large payload\n");
161 is->skip(len);
162 return;
163 }
164
165 is->readBytes(data, len);
166
167 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168}
169
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100170void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100172 handler->endOfContinuousUpdates();
173}
174
175void CMsgReader::readFramebufferUpdate()
176{
177 is->skip(1);
178 nUpdateRectsLeft = is->readU16();
179 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180}
181
Peter Ã…strand98fe98c2010-02-10 07:43:02 +0000182void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183{
184 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
185 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
186 r.width(), r.height(), r.tl.x, r.tl.y,
187 handler->cp.width, handler->cp.height);
188 throw Exception("Rect too big");
189 }
190
191 if (r.is_empty())
192 fprintf(stderr, "Warning: zero size rect\n");
193
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100194 handler->dataRect(r, encoding);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195}
196
Pierre Ossman6b68f972017-02-19 15:51:19 +0100197void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
198{
199 rdr::U8 pr, pg, pb;
200 rdr::U8 sr, sg, sb;
201 int data_len = ((width+7)/8) * height;
202 int mask_len = ((width+7)/8) * height;
203 rdr::U8Array data(data_len);
204 rdr::U8Array mask(mask_len);
205
206 int x, y;
207 rdr::U8 buf[width*height*4];
208 rdr::U8* out;
209
210 if (width * height) {
211 pr = is->readU8();
212 pg = is->readU8();
213 pb = is->readU8();
214
215 sr = is->readU8();
216 sg = is->readU8();
217 sb = is->readU8();
218
219 is->readBytes(data.buf, data_len);
220 is->readBytes(mask.buf, mask_len);
221 }
222
223 int maskBytesPerRow = (width+7)/8;
224 out = buf;
225 for (y = 0;y < height;y++) {
226 for (x = 0;x < width;x++) {
227 int byte = y * maskBytesPerRow + x / 8;
228 int bit = 7 - x % 8;
229
230 if (data.buf[byte] & (1 << bit)) {
231 out[0] = pr;
232 out[1] = pg;
233 out[2] = pb;
234 } else {
235 out[0] = sr;
236 out[1] = sg;
237 out[2] = sb;
238 }
239
240 if (mask.buf[byte] & (1 << bit))
241 out[3] = 255;
242 else
243 out[3] = 0;
244
245 out += 4;
246 }
247 }
248
249 handler->setCursor(width, height, hotspot, buf);
250}
251
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000252void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
253{
254 int data_len = width * height * (handler->cp.pf().bpp/8);
255 int mask_len = ((width+7)/8) * height;
256 rdr::U8Array data(data_len);
257 rdr::U8Array mask(mask_len);
258
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100259 int x, y;
260 rdr::U8 buf[width*height*4];
261 rdr::U8* in;
262 rdr::U8* out;
263
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000264 is->readBytes(data.buf, data_len);
265 is->readBytes(mask.buf, mask_len);
266
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100267 int maskBytesPerRow = (width+7)/8;
268 in = data.buf;
269 out = buf;
270 for (y = 0;y < height;y++) {
271 for (x = 0;x < width;x++) {
272 int byte = y * maskBytesPerRow + x / 8;
273 int bit = 7 - x % 8;
274
275 handler->cp.pf().rgbFromBuffer(out, in, 1);
276
277 if (mask.buf[byte] & (1 << bit))
278 out[3] = 255;
279 else
280 out[3] = 0;
281
282 in += handler->cp.pf().bpp/8;
283 out += 4;
284 }
285 }
286
287 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000288}
289
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100290void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
291{
292 char* name = is->readString();
293
294 if (x || y || w || h) {
295 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
296 } else {
297 handler->setName(name);
298 }
299
300 delete [] name;
301}
302
303void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
304{
305 unsigned int screens, i;
306 rdr::U32 id, flags;
307 int sx, sy, sw, sh;
308 ScreenSet layout;
309
310 screens = is->readU8();
311 is->skip(3);
312
313 for (i = 0;i < screens;i++) {
314 id = is->readU32();
315 sx = is->readU16();
316 sy = is->readU16();
317 sw = is->readU16();
318 sh = is->readU16();
319 flags = is->readU32();
320
321 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
322 }
323
324 handler->setExtendedDesktopSize(x, y, w, h, layout);
325}