blob: 8466c68c1ece38b766aca26e6071b69511a2b660 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman7638e9c2014-01-16 13:12:40 +01002 * Copyright 2009-2014 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>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010026#include <rfb/Decoder.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027
28using namespace rfb;
29
30CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
31 : imageBufIdealSize(0), handler(handler_), is(is_),
Pierre Ossman7638e9c2014-01-16 13:12:40 +010032 imageBuf(0), imageBufSize(0), nUpdateRectsLeft(0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000034 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035 decoders[i] = 0;
36 }
37}
38
39CMsgReader::~CMsgReader()
40{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000041 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 delete decoders[i];
43 }
44 delete [] imageBuf;
45}
46
Pierre Ossman7638e9c2014-01-16 13:12:40 +010047void CMsgReader::readServerInit()
48{
49 int width = is->readU16();
50 int height = is->readU16();
51 handler->setDesktopSize(width, height);
52 PixelFormat pf;
53 pf.read(is);
54 handler->setPixelFormat(pf);
55 CharArray name(is->readString());
56 handler->setName(name.buf);
57 handler->serverInit();
58}
59
60void CMsgReader::readMsg()
61{
62 if (nUpdateRectsLeft == 0) {
63 int type = is->readU8();
64
65 switch (type) {
66 case msgTypeSetColourMapEntries:
67 readSetColourMapEntries();
68 break;
69 case msgTypeBell:
70 readBell();
71 break;
72 case msgTypeServerCutText:
73 readServerCutText();
74 break;
75 case msgTypeFramebufferUpdate:
76 readFramebufferUpdate();
77 break;
78 case msgTypeServerFence:
79 readFence();
80 break;
81 case msgTypeEndOfContinuousUpdates:
82 readEndOfContinuousUpdates();
83 break;
84 default:
85 fprintf(stderr, "unknown message type %d\n", type);
86 throw Exception("unknown message type");
87 }
88 } else {
89 int x = is->readU16();
90 int y = is->readU16();
91 int w = is->readU16();
92 int h = is->readU16();
93 int encoding = is->readS32();
94
95 switch (encoding) {
96 case pseudoEncodingLastRect:
97 nUpdateRectsLeft = 1; // this rectangle is the last one
98 break;
99 case pseudoEncodingCursor:
100 readSetCursor(w, h, Point(x,y));
101 break;
102 case pseudoEncodingDesktopName:
103 readSetDesktopName(x, y, w, h);
104 break;
105 case pseudoEncodingDesktopSize:
106 handler->setDesktopSize(w, h);
107 break;
108 case pseudoEncodingExtendedDesktopSize:
109 readExtendedDesktopSize(x, y, w, h);
110 break;
111 default:
112 readRect(Rect(x, y, x+w, y+h), encoding);
113 break;
114 };
115
116 nUpdateRectsLeft--;
117 if (nUpdateRectsLeft == 0)
118 handler->framebufferUpdateEnd();
119 }
120}
121
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122void CMsgReader::readSetColourMapEntries()
123{
124 is->skip(1);
125 int firstColour = is->readU16();
126 int nColours = is->readU16();
127 rdr::U16Array rgbs(nColours * 3);
128 for (int i = 0; i < nColours * 3; i++)
129 rgbs.buf[i] = is->readU16();
130 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
131}
132
133void CMsgReader::readBell()
134{
135 handler->bell();
136}
137
138void CMsgReader::readServerCutText()
139{
140 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000141 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 if (len > 256*1024) {
143 is->skip(len);
144 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
145 return;
146 }
147 CharArray ca(len+1);
148 ca.buf[len] = 0;
149 is->readBytes(ca.buf, len);
150 handler->serverCutText(ca.buf, len);
151}
152
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100153void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000154{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100155 rdr::U32 flags;
156 rdr::U8 len;
157 char data[64];
158
159 is->skip(3);
160
161 flags = is->readU32();
162
163 len = is->readU8();
164 if (len > sizeof(data)) {
165 fprintf(stderr, "Ignoring fence with too large payload\n");
166 is->skip(len);
167 return;
168 }
169
170 is->readBytes(data, len);
171
172 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173}
174
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100175void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100177 handler->endOfContinuousUpdates();
178}
179
180void CMsgReader::readFramebufferUpdate()
181{
182 is->skip(1);
183 nUpdateRectsLeft = is->readU16();
184 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000185}
186
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000187void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188{
189 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
190 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
191 r.width(), r.height(), r.tl.x, r.tl.y,
192 handler->cp.width, handler->cp.height);
193 throw Exception("Rect too big");
194 }
195
196 if (r.is_empty())
197 fprintf(stderr, "Warning: zero size rect\n");
198
199 handler->beginRect(r, encoding);
200
201 if (encoding == encodingCopyRect) {
202 readCopyRect(r);
203 } else {
Adam Tkac5eeaa3b2008-11-18 16:00:14 +0000204
Pierre Ossman22922962012-07-19 11:06:27 +0000205 if (!Decoder::supported(encoding)) {
Adam Tkac5eeaa3b2008-11-18 16:00:14 +0000206 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
207 throw Exception("Unknown rect encoding");
208 }
209
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210 if (!decoders[encoding]) {
211 decoders[encoding] = Decoder::createDecoder(encoding, this);
212 if (!decoders[encoding]) {
213 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
214 throw Exception("Unknown rect encoding");
215 }
216 }
217 decoders[encoding]->readRect(r, handler);
218 }
219
220 handler->endRect(r, encoding);
221}
222
223void CMsgReader::readCopyRect(const Rect& r)
224{
225 int srcX = is->readU16();
226 int srcY = is->readU16();
227 handler->copyRect(r, srcX, srcY);
228}
229
230void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
231{
232 int data_len = width * height * (handler->cp.pf().bpp/8);
233 int mask_len = ((width+7)/8) * height;
234 rdr::U8Array data(data_len);
235 rdr::U8Array mask(mask_len);
236
237 is->readBytes(data.buf, data_len);
238 is->readBytes(mask.buf, mask_len);
239
240 handler->setCursor(width, height, hotspot, data.buf, mask.buf);
241}
242
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100243void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
244{
245 char* name = is->readString();
246
247 if (x || y || w || h) {
248 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
249 } else {
250 handler->setName(name);
251 }
252
253 delete [] name;
254}
255
256void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
257{
258 unsigned int screens, i;
259 rdr::U32 id, flags;
260 int sx, sy, sw, sh;
261 ScreenSet layout;
262
263 screens = is->readU8();
264 is->skip(3);
265
266 for (i = 0;i < screens;i++) {
267 id = is->readU32();
268 sx = is->readU16();
269 sy = is->readU16();
270 sw = is->readU16();
271 sh = is->readU16();
272 flags = is->readU32();
273
274 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
275 }
276
277 handler->setExtendedDesktopSize(x, y, w, h, layout);
278}
279
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000280rdr::U8* CMsgReader::getImageBuf(int required, int requested, int* nPixels)
281{
282 int requiredBytes = required * (handler->cp.pf().bpp / 8);
283 int requestedBytes = requested * (handler->cp.pf().bpp / 8);
284 int size = requestedBytes;
285 if (size > imageBufIdealSize) size = imageBufIdealSize;
286
287 if (size < requiredBytes)
288 size = requiredBytes;
289
290 if (imageBufSize < size) {
291 imageBufSize = size;
292 delete [] imageBuf;
293 imageBuf = new rdr::U8[imageBufSize];
294 }
295 if (nPixels)
296 *nPixels = imageBufSize / (handler->cp.pf().bpp / 8);
297 return imageBuf;
298}
299
300int CMsgReader::bpp()
301{
302 return handler->cp.pf().bpp;
303}