blob: bbb9909619b6d9a6724b82db3b5b78e5a6b8f914 [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;
91 case pseudoEncodingCursor:
92 readSetCursor(w, h, Point(x,y));
93 break;
94 case pseudoEncodingDesktopName:
95 readSetDesktopName(x, y, w, h);
96 break;
97 case pseudoEncodingDesktopSize:
98 handler->setDesktopSize(w, h);
99 break;
100 case pseudoEncodingExtendedDesktopSize:
101 readExtendedDesktopSize(x, y, w, h);
102 break;
103 default:
104 readRect(Rect(x, y, x+w, y+h), encoding);
105 break;
106 };
107
108 nUpdateRectsLeft--;
109 if (nUpdateRectsLeft == 0)
110 handler->framebufferUpdateEnd();
111 }
112}
113
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114void CMsgReader::readSetColourMapEntries()
115{
116 is->skip(1);
117 int firstColour = is->readU16();
118 int nColours = is->readU16();
119 rdr::U16Array rgbs(nColours * 3);
120 for (int i = 0; i < nColours * 3; i++)
121 rgbs.buf[i] = is->readU16();
122 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
123}
124
125void CMsgReader::readBell()
126{
127 handler->bell();
128}
129
130void CMsgReader::readServerCutText()
131{
132 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000133 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000134 if (len > 256*1024) {
135 is->skip(len);
136 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
137 return;
138 }
139 CharArray ca(len+1);
140 ca.buf[len] = 0;
141 is->readBytes(ca.buf, len);
142 handler->serverCutText(ca.buf, len);
143}
144
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100145void CMsgReader::readFence()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100147 rdr::U32 flags;
148 rdr::U8 len;
149 char data[64];
150
151 is->skip(3);
152
153 flags = is->readU32();
154
155 len = is->readU8();
156 if (len > sizeof(data)) {
157 fprintf(stderr, "Ignoring fence with too large payload\n");
158 is->skip(len);
159 return;
160 }
161
162 is->readBytes(data, len);
163
164 handler->fence(flags, len, data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165}
166
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100167void CMsgReader::readEndOfContinuousUpdates()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168{
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100169 handler->endOfContinuousUpdates();
170}
171
172void CMsgReader::readFramebufferUpdate()
173{
174 is->skip(1);
175 nUpdateRectsLeft = is->readU16();
176 handler->framebufferUpdateStart();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177}
178
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000179void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180{
181 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
182 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
183 r.width(), r.height(), r.tl.x, r.tl.y,
184 handler->cp.width, handler->cp.height);
185 throw Exception("Rect too big");
186 }
187
188 if (r.is_empty())
189 fprintf(stderr, "Warning: zero size rect\n");
190
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +0100191 handler->dataRect(r, encoding);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192}
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
195{
196 int data_len = width * height * (handler->cp.pf().bpp/8);
197 int mask_len = ((width+7)/8) * height;
198 rdr::U8Array data(data_len);
199 rdr::U8Array mask(mask_len);
200
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100201 int x, y;
202 rdr::U8 buf[width*height*4];
203 rdr::U8* in;
204 rdr::U8* out;
205
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206 is->readBytes(data.buf, data_len);
207 is->readBytes(mask.buf, mask_len);
208
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100209 int maskBytesPerRow = (width+7)/8;
210 in = data.buf;
211 out = buf;
212 for (y = 0;y < height;y++) {
213 for (x = 0;x < width;x++) {
214 int byte = y * maskBytesPerRow + x / 8;
215 int bit = 7 - x % 8;
216
217 handler->cp.pf().rgbFromBuffer(out, in, 1);
218
219 if (mask.buf[byte] & (1 << bit))
220 out[3] = 255;
221 else
222 out[3] = 0;
223
224 in += handler->cp.pf().bpp/8;
225 out += 4;
226 }
227 }
228
229 handler->setCursor(width, height, hotspot, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230}
231
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100232void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
233{
234 char* name = is->readString();
235
236 if (x || y || w || h) {
237 fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
238 } else {
239 handler->setName(name);
240 }
241
242 delete [] name;
243}
244
245void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
246{
247 unsigned int screens, i;
248 rdr::U32 id, flags;
249 int sx, sy, sw, sh;
250 ScreenSet layout;
251
252 screens = is->readU8();
253 is->skip(3);
254
255 for (i = 0;i < screens;i++) {
256 id = is->readU32();
257 sx = is->readU16();
258 sy = is->readU16();
259 sw = is->readU16();
260 sh = is->readU16();
261 flags = is->readU32();
262
263 layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
264 }
265
266 handler->setExtendedDesktopSize(x, y, w, h, layout);
267}