blob: 3f968dea69e83edfc390b2675c90fa4e799dc82f [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/OutStream.h>
20#include <rfb/msgTypes.h>
21#include <rfb/PixelFormat.h>
22#include <rfb/Rect.h>
23#include <rfb/ConnParams.h>
24#include <rfb/Decoder.h>
25#include <rfb/CMsgWriter.h>
26
27using namespace rfb;
28
29CMsgWriter::CMsgWriter(ConnParams* cp_, rdr::OutStream* os_)
30 : cp(cp_), os(os_)
31{
32}
33
34CMsgWriter::~CMsgWriter()
35{
36}
37
38void CMsgWriter::writeSetPixelFormat(const PixelFormat& pf)
39{
40 startMsg(msgTypeSetPixelFormat);
41 os->pad(3);
42 pf.write(os);
43 endMsg();
44}
45
46void CMsgWriter::writeSetEncodings(int nEncodings, rdr::U32* encodings)
47{
48 startMsg(msgTypeSetEncodings);
49 os->skip(1);
50 os->writeU16(nEncodings);
51 for (int i = 0; i < nEncodings; i++)
52 os->writeU32(encodings[i]);
53 endMsg();
54}
55
56// Ask for encodings based on which decoders are supported. Assumes higher
57// encoding numbers are more desirable.
58
59void CMsgWriter::writeSetEncodings(int preferredEncoding, bool useCopyRect)
60{
61 int nEncodings = 0;
62 rdr::U32 encodings[encodingMax+3];
63 if (cp->supportsLocalCursor)
64 encodings[nEncodings++] = pseudoEncodingCursor;
65 if (cp->supportsDesktopResize)
66 encodings[nEncodings++] = pseudoEncodingDesktopSize;
Pierre Ossman49f88222009-03-20 13:02:50 +000067 if (cp->supportsExtendedDesktopSize)
68 encodings[nEncodings++] = pseudoEncodingExtendedDesktopSize;
Peter Åstrandc39e0782009-01-15 12:21:42 +000069 if (cp->supportsDesktopRename)
70 encodings[nEncodings++] = pseudoEncodingDesktopName;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071 if (Decoder::supported(preferredEncoding)) {
72 encodings[nEncodings++] = preferredEncoding;
73 }
74 if (useCopyRect) {
75 encodings[nEncodings++] = encodingCopyRect;
76 }
Pierre Ossman090e7d62009-03-12 10:16:07 +000077
78 /*
79 * Prefer encodings in this order:
80 *
81 * Tight, ZRLE, Hextile, *
82 */
83
84 if ((preferredEncoding != encodingTight) &&
85 Decoder::supported(encodingTight))
86 encodings[nEncodings++] = encodingTight;
87
88 if ((preferredEncoding != encodingZRLE) &&
89 Decoder::supported(encodingZRLE))
90 encodings[nEncodings++] = encodingZRLE;
91
92 if ((preferredEncoding != encodingHextile) &&
93 Decoder::supported(encodingHextile))
94 encodings[nEncodings++] = encodingHextile;
95
96 // Remaining encodings
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097 for (int i = encodingMax; i >= 0; i--) {
Pierre Ossman090e7d62009-03-12 10:16:07 +000098 switch (i) {
99 case encodingTight:
100 case encodingZRLE:
101 case encodingHextile:
102 break;
103 default:
104 if ((i != preferredEncoding) && Decoder::supported(i))
105 encodings[nEncodings++] = i;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 }
107 }
Pierre Ossman090e7d62009-03-12 10:16:07 +0000108
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 encodings[nEncodings++] = pseudoEncodingLastRect;
110 if (cp->customCompressLevel && cp->compressLevel >= 0 && cp->compressLevel <= 9)
111 encodings[nEncodings++] = pseudoEncodingCompressLevel0 + cp->compressLevel;
Pierre Ossman3d2ccc42009-03-11 14:33:49 +0000112 if (!cp->noJpeg && cp->qualityLevel >= 0 && cp->qualityLevel <= 9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel;
114
115 writeSetEncodings(nEncodings, encodings);
116}
117
118void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental)
119{
120 startMsg(msgTypeFramebufferUpdateRequest);
121 os->writeU8(incremental);
122 os->writeU16(r.tl.x);
123 os->writeU16(r.tl.y);
124 os->writeU16(r.width());
125 os->writeU16(r.height());
126 endMsg();
127}
128
129
130void CMsgWriter::keyEvent(rdr::U32 key, bool down)
131{
132 startMsg(msgTypeKeyEvent);
133 os->writeU8(down);
134 os->pad(2);
135 os->writeU32(key);
136 endMsg();
137}
138
139
140void CMsgWriter::pointerEvent(const Point& pos, int buttonMask)
141{
142 Point p(pos);
143 if (p.x < 0) p.x = 0;
144 if (p.y < 0) p.y = 0;
145 if (p.x >= cp->width) p.x = cp->width - 1;
146 if (p.y >= cp->height) p.y = cp->height - 1;
147
148 startMsg(msgTypePointerEvent);
149 os->writeU8(buttonMask);
150 os->writeU16(p.x);
151 os->writeU16(p.y);
152 endMsg();
153}
154
155
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000156void CMsgWriter::clientCutText(const char* str, rdr::U32 len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157{
158 startMsg(msgTypeClientCutText);
159 os->pad(3);
160 os->writeU32(len);
161 os->writeBytes(str, len);
162 endMsg();
163}