blob: c34f216da3f6a2b07b72a6844e017ee4d3008b1d [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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, int hotspotX,
73 int hotspotY, 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(hotspotX);
79 os->writeS16(hotspotY);
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
Peter Åstrand1e9d32f2005-02-16 13:00:38 +000087void 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 os->writeU8(0);
104 os->writeU8(0);
105 os->writeU8(0);
106 os->writeU8(255);
107 os->writeU8(255);
108 os->writeU8(255);
109 os->writeBytes(data, (width+7)/8 * height);
110 os->writeBytes(mask, (width+7)/8 * height);
111}
112
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000113void SMsgWriterV3::writeFramebufferUpdateStart(int nRects)
114{
115 startMsg(msgTypeFramebufferUpdate);
116 os->pad(1);
117 if (wsccb) nRects++;
118 if (needSetDesktopSize) nRects++;
119 os->writeU16(nRects);
120 nRectsInUpdate = 0;
121 nRectsInHeader = nRects;
122 if (wsccb) {
123 wsccb->writeSetCursorCallback();
124 wsccb = 0;
125 }
126}
127
128void SMsgWriterV3::writeFramebufferUpdateStart()
129{
130 nRectsInUpdate = nRectsInHeader = 0;
131 if (!updateOS)
132 updateOS = new rdr::MemOutStream;
133 os = updateOS;
134}
135
136void SMsgWriterV3::writeFramebufferUpdateEnd()
137{
138 if (needSetDesktopSize) {
139 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
140 throw Exception("SMsgWriterV3 setDesktopSize: nRects out of sync");
141 os->writeS16(0);
142 os->writeS16(0);
143 os->writeU16(cp->width);
144 os->writeU16(cp->height);
145 os->writeU32(pseudoEncodingDesktopSize);
146 needSetDesktopSize = false;
147 }
148
149 if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
150 throw Exception("SMsgWriterV3::writeFramebufferUpdateEnd: "
151 "nRects out of sync");
152 if (os == updateOS) {
153 os = realOS;
154 startMsg(msgTypeFramebufferUpdate);
155 os->pad(1);
156 os->writeU16(nRectsInUpdate);
157 os->writeBytes(updateOS->data(), updateOS->length());
158 updateOS->clear();
159 }
160
161 updatesSent++;
162 endMsg();
163}
164
165bool SMsgWriterV3::needFakeUpdate()
166{
167 return wsccb || needSetDesktopSize;
168}
169
170void SMsgWriterV3::startRect(const Rect& r, unsigned int encoding)
171{
172 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
173 throw Exception("SMsgWriterV3::startRect: nRects out of sync");
174
175 currentEncoding = encoding;
176 lenBeforeRect = os->length();
177 if (encoding != encodingCopyRect)
178 rawBytesEquivalent += 12 + r.width() * r.height() * (bpp()/8);
179
180 os->writeS16(r.tl.x);
181 os->writeS16(r.tl.y);
182 os->writeU16(r.width());
183 os->writeU16(r.height());
184 os->writeU32(encoding);
185}
186
187void SMsgWriterV3::endRect()
188{
189 if (currentEncoding <= encodingMax) {
190 bytesSent[currentEncoding] += os->length() - lenBeforeRect;
191 rectsSent[currentEncoding]++;
192 }
193}
194
195void SMsgWriterV3::setOutStream(rdr::OutStream* os_)
196{
197 SMsgWriter::setOutStream(os_);
198 realOS = os;
199}