blob: e3b73bf57b84ba12a25b36831aa3672ff67786fa [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18#include <stdio.h>
19#include <rdr/InStream.h>
20#include <rfb/Exception.h>
21#include <rfb/util.h>
22#include <rfb/CMsgHandler.h>
23#include <rfb/CMsgReader.h>
24
25using namespace rfb;
26
27CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
28 : imageBufIdealSize(0), handler(handler_), is(is_),
29 imageBuf(0), imageBufSize(0)
30{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000031 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032 decoders[i] = 0;
33 }
34}
35
36CMsgReader::~CMsgReader()
37{
Peter Åstrand98fe98c2010-02-10 07:43:02 +000038 for (int i = 0; i <= encodingMax; i++) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039 delete decoders[i];
40 }
41 delete [] imageBuf;
42}
43
44void CMsgReader::readSetColourMapEntries()
45{
46 is->skip(1);
47 int firstColour = is->readU16();
48 int nColours = is->readU16();
49 rdr::U16Array rgbs(nColours * 3);
50 for (int i = 0; i < nColours * 3; i++)
51 rgbs.buf[i] = is->readU16();
52 handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
53}
54
55void CMsgReader::readBell()
56{
57 handler->bell();
58}
59
60void CMsgReader::readServerCutText()
61{
62 is->skip(3);
Adam Tkacacf6c6b2009-02-13 12:42:05 +000063 rdr::U32 len = is->readU32();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000064 if (len > 256*1024) {
65 is->skip(len);
66 fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
67 return;
68 }
69 CharArray ca(len+1);
70 ca.buf[len] = 0;
71 is->readBytes(ca.buf, len);
72 handler->serverCutText(ca.buf, len);
73}
74
75void CMsgReader::readFramebufferUpdateStart()
76{
77 handler->framebufferUpdateStart();
78}
79
80void CMsgReader::readFramebufferUpdateEnd()
81{
82 handler->framebufferUpdateEnd();
83}
84
Peter Åstrand98fe98c2010-02-10 07:43:02 +000085void CMsgReader::readRect(const Rect& r, int encoding)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086{
87 if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
88 fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
89 r.width(), r.height(), r.tl.x, r.tl.y,
90 handler->cp.width, handler->cp.height);
91 throw Exception("Rect too big");
92 }
93
94 if (r.is_empty())
95 fprintf(stderr, "Warning: zero size rect\n");
96
97 handler->beginRect(r, encoding);
98
99 if (encoding == encodingCopyRect) {
100 readCopyRect(r);
101 } else {
Adam Tkac5eeaa3b2008-11-18 16:00:14 +0000102
103 if (encoding > encodingMax) {
104 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
105 throw Exception("Unknown rect encoding");
106 }
107
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108 if (!decoders[encoding]) {
109 decoders[encoding] = Decoder::createDecoder(encoding, this);
110 if (!decoders[encoding]) {
111 fprintf(stderr, "Unknown rect encoding %d\n", encoding);
112 throw Exception("Unknown rect encoding");
113 }
114 }
115 decoders[encoding]->readRect(r, handler);
116 }
117
118 handler->endRect(r, encoding);
119}
120
121void CMsgReader::readCopyRect(const Rect& r)
122{
123 int srcX = is->readU16();
124 int srcY = is->readU16();
125 handler->copyRect(r, srcX, srcY);
126}
127
128void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
129{
130 int data_len = width * height * (handler->cp.pf().bpp/8);
131 int mask_len = ((width+7)/8) * height;
132 rdr::U8Array data(data_len);
133 rdr::U8Array mask(mask_len);
134
135 is->readBytes(data.buf, data_len);
136 is->readBytes(mask.buf, mask_len);
137
138 handler->setCursor(width, height, hotspot, data.buf, mask.buf);
139}
140
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000141rdr::U8* CMsgReader::getImageBuf(int required, int requested, int* nPixels)
142{
143 int requiredBytes = required * (handler->cp.pf().bpp / 8);
144 int requestedBytes = requested * (handler->cp.pf().bpp / 8);
145 int size = requestedBytes;
146 if (size > imageBufIdealSize) size = imageBufIdealSize;
147
148 if (size < requiredBytes)
149 size = requiredBytes;
150
151 if (imageBufSize < size) {
152 imageBufSize = size;
153 delete [] imageBuf;
154 imageBuf = new rdr::U8[imageBufSize];
155 }
156 if (nPixels)
157 *nPixels = imageBufSize / (handler->cp.pf().bpp / 8);
158 return imageBuf;
159}
160
161int CMsgReader::bpp()
162{
163 return handler->cp.pf().bpp;
164}