blob: a85f85ea463e6fd5471739d50901fc425a06f730 [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 <rdr/OutStream.h>
19#include <rdr/MemOutStream.h>
20#include <rfb/msgTypes.h>
21#include <rfb/Exception.h>
22#include <rfb/ConnParams.h>
23#include <rfb/SMsgWriterV3.h>
24
25using namespace rfb;
26
27SMsgWriterV3::SMsgWriterV3(ConnParams* cp, rdr::OutStream* os)
28 : SMsgWriter(cp, os), updateOS(0), realOS(os), nRectsInUpdate(0),
29 nRectsInHeader(0), wsccb(0),
30 needSetDesktopSize(false)
31{
32}
33
34SMsgWriterV3::~SMsgWriterV3()
35{
36 delete updateOS;
37}
38
39void SMsgWriterV3::writeServerInit()
40{
41 os->writeU16(cp->width);
42 os->writeU16(cp->height);
43 cp->pf().write(os);
44 os->writeString(cp->name());
45 endMsg();
46}
47
48void SMsgWriterV3::startMsg(int type)
49{
50 if (os != realOS)
51 throw Exception("startMsg called while writing an update?");
52
53 os->writeU8(type);
54}
55
56void SMsgWriterV3::endMsg()
57{
58 os->flush();
59}
60
61bool SMsgWriterV3::writeSetDesktopSize() {
62 if (!cp->supportsDesktopResize) return false;
63 needSetDesktopSize = true;
64 return true;
65}
66
67void SMsgWriterV3::cursorChange(WriteSetCursorCallback* cb)
68{
69 wsccb = cb;
70}
71
72void SMsgWriterV3::writeSetCursor(int width, int height, const Point& hotspot,
73 void* data, void* mask)
74{
75 if (!wsccb) return;
76 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
77 throw Exception("SMsgWriterV3::writeSetCursor: nRects out of sync");
78 os->writeS16(hotspot.x);
79 os->writeS16(hotspot.y);
80 os->writeU16(width);
81 os->writeU16(height);
82 os->writeU32(pseudoEncodingCursor);
83 os->writeBytes(data, width * height * (cp->pf().bpp/8));
84 os->writeBytes(mask, (width+7)/8 * height);
85}
86
87void SMsgWriterV3::writeSetXCursor(int width, int height, int hotspotX,
88 int hotspotY, void* data, void* mask)
89{
90 if (!wsccb) return;
91 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
92 throw Exception("SMsgWriterV3::writeSetXCursor: nRects out of sync");
93 os->writeS16(hotspotX);
94 os->writeS16(hotspotY);
95 os->writeU16(width);
96 os->writeU16(height);
97 os->writeU32(pseudoEncodingXCursor);
98 // FIXME: We only support black and white cursors, currently. We
99 // could pass the correct color by using the pix0/pix1 values
100 // returned from getBitmap, in writeSetCursorCallback. However, we
101 // would then need to undo the conversion from rgb to Pixel that is
102 // done by FakeAllocColor.
103 if (width * height) {
104 os->writeU8(0);
105 os->writeU8(0);
106 os->writeU8(0);
107 os->writeU8(255);
108 os->writeU8(255);
109 os->writeU8(255);
110 os->writeBytes(data, (width+7)/8 * height);
111 os->writeBytes(mask, (width+7)/8 * height);
112 }
113}
114
115void SMsgWriterV3::writeFramebufferUpdateStart(int nRects)
116{
117 startMsg(msgTypeFramebufferUpdate);
118 os->pad(1);
119 if (wsccb) nRects++;
120 if (needSetDesktopSize) nRects++;
121 os->writeU16(nRects);
122 nRectsInUpdate = 0;
123 nRectsInHeader = nRects;
124 if (wsccb) {
125 wsccb->writeSetCursorCallback();
126 wsccb = 0;
127 }
128}
129
130void SMsgWriterV3::writeFramebufferUpdateStart()
131{
132 nRectsInUpdate = nRectsInHeader = 0;
133 if (!updateOS)
134 updateOS = new rdr::MemOutStream;
135 os = updateOS;
136}
137
138void SMsgWriterV3::writeFramebufferUpdateEnd()
139{
140 if (needSetDesktopSize) {
141 if (!cp->supportsDesktopResize)
142 throw Exception("Client does not support desktop resize");
143 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
144 throw Exception("SMsgWriterV3 setDesktopSize: nRects out of sync");
145 os->writeS16(0);
146 os->writeS16(0);
147 os->writeU16(cp->width);
148 os->writeU16(cp->height);
149 os->writeU32(pseudoEncodingDesktopSize);
150 needSetDesktopSize = false;
151 }
152
153 if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
154 throw Exception("SMsgWriterV3::writeFramebufferUpdateEnd: "
155 "nRects out of sync");
156 if (os == updateOS) {
157 os = realOS;
158 startMsg(msgTypeFramebufferUpdate);
159 os->pad(1);
160 os->writeU16(nRectsInUpdate);
161 os->writeBytes(updateOS->data(), updateOS->length());
162 updateOS->clear();
163 }
164
165 updatesSent++;
166 endMsg();
167}
168
169bool SMsgWriterV3::needFakeUpdate()
170{
171 return wsccb || needSetDesktopSize;
172}
173
174void SMsgWriterV3::startRect(const Rect& r, unsigned int encoding)
175{
176 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
177 throw Exception("SMsgWriterV3::startRect: nRects out of sync");
178
179 currentEncoding = encoding;
180 lenBeforeRect = os->length();
181 if (encoding != encodingCopyRect)
182 rawBytesEquivalent += 12 + r.width() * r.height() * (bpp()/8);
183
184 os->writeS16(r.tl.x);
185 os->writeS16(r.tl.y);
186 os->writeU16(r.width());
187 os->writeU16(r.height());
188 os->writeU32(encoding);
189}
190
191void SMsgWriterV3::endRect()
192{
193 if (currentEncoding <= encodingMax) {
194 bytesSent[currentEncoding] += os->length() - lenBeforeRect;
195 rectsSent[currentEncoding]++;
196 }
197}