blob: 20a72801d3d8678bba69fbe66a9b200cff3c4d46 [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
87void SMsgWriterV3::writeFramebufferUpdateStart(int nRects)
88{
89 startMsg(msgTypeFramebufferUpdate);
90 os->pad(1);
91 if (wsccb) nRects++;
92 if (needSetDesktopSize) nRects++;
93 os->writeU16(nRects);
94 nRectsInUpdate = 0;
95 nRectsInHeader = nRects;
96 if (wsccb) {
97 wsccb->writeSetCursorCallback();
98 wsccb = 0;
99 }
100}
101
102void SMsgWriterV3::writeFramebufferUpdateStart()
103{
104 nRectsInUpdate = nRectsInHeader = 0;
105 if (!updateOS)
106 updateOS = new rdr::MemOutStream;
107 os = updateOS;
108}
109
110void SMsgWriterV3::writeFramebufferUpdateEnd()
111{
112 if (needSetDesktopSize) {
113 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
114 throw Exception("SMsgWriterV3 setDesktopSize: nRects out of sync");
115 os->writeS16(0);
116 os->writeS16(0);
117 os->writeU16(cp->width);
118 os->writeU16(cp->height);
119 os->writeU32(pseudoEncodingDesktopSize);
120 needSetDesktopSize = false;
121 }
122
123 if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
124 throw Exception("SMsgWriterV3::writeFramebufferUpdateEnd: "
125 "nRects out of sync");
126 if (os == updateOS) {
127 os = realOS;
128 startMsg(msgTypeFramebufferUpdate);
129 os->pad(1);
130 os->writeU16(nRectsInUpdate);
131 os->writeBytes(updateOS->data(), updateOS->length());
132 updateOS->clear();
133 }
134
135 updatesSent++;
136 endMsg();
137}
138
139bool SMsgWriterV3::needFakeUpdate()
140{
141 return wsccb || needSetDesktopSize;
142}
143
144void SMsgWriterV3::startRect(const Rect& r, unsigned int encoding)
145{
146 if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
147 throw Exception("SMsgWriterV3::startRect: nRects out of sync");
148
149 currentEncoding = encoding;
150 lenBeforeRect = os->length();
151 if (encoding != encodingCopyRect)
152 rawBytesEquivalent += 12 + r.width() * r.height() * (bpp()/8);
153
154 os->writeS16(r.tl.x);
155 os->writeS16(r.tl.y);
156 os->writeU16(r.width());
157 os->writeU16(r.height());
158 os->writeU32(encoding);
159}
160
161void SMsgWriterV3::endRect()
162{
163 if (currentEncoding <= encodingMax) {
164 bytesSent[currentEncoding] += os->length() - lenBeforeRect;
165 rectsSent[currentEncoding]++;
166 }
167}
168
169void SMsgWriterV3::setOutStream(rdr::OutStream* os_)
170{
171 SMsgWriter::setOutStream(os_);
172 realOS = os;
173}