blob: f221f22925bb7735dc32247d8e2f8869e9c650db [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];
Pierre Ossmanc754cce2011-11-14 15:44:11 +000063
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000064 if (cp->supportsLocalCursor)
65 encodings[nEncodings++] = pseudoEncodingCursor;
66 if (cp->supportsDesktopResize)
67 encodings[nEncodings++] = pseudoEncodingDesktopSize;
Pierre Ossman49f88222009-03-20 13:02:50 +000068 if (cp->supportsExtendedDesktopSize)
69 encodings[nEncodings++] = pseudoEncodingExtendedDesktopSize;
Peter Åstrandc39e0782009-01-15 12:21:42 +000070 if (cp->supportsDesktopRename)
71 encodings[nEncodings++] = pseudoEncodingDesktopName;
Pierre Ossmanc754cce2011-11-14 15:44:11 +000072
73 encodings[nEncodings++] = pseudoEncodingLastRect;
74 encodings[nEncodings++] = pseudoEncodingFence;
75
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 if (Decoder::supported(preferredEncoding)) {
77 encodings[nEncodings++] = preferredEncoding;
78 }
Pierre Ossmanc754cce2011-11-14 15:44:11 +000079
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 if (useCopyRect) {
81 encodings[nEncodings++] = encodingCopyRect;
82 }
Pierre Ossman090e7d62009-03-12 10:16:07 +000083
84 /*
85 * Prefer encodings in this order:
86 *
87 * Tight, ZRLE, Hextile, *
88 */
89
90 if ((preferredEncoding != encodingTight) &&
91 Decoder::supported(encodingTight))
92 encodings[nEncodings++] = encodingTight;
93
94 if ((preferredEncoding != encodingZRLE) &&
95 Decoder::supported(encodingZRLE))
96 encodings[nEncodings++] = encodingZRLE;
97
98 if ((preferredEncoding != encodingHextile) &&
99 Decoder::supported(encodingHextile))
100 encodings[nEncodings++] = encodingHextile;
101
102 // Remaining encodings
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103 for (int i = encodingMax; i >= 0; i--) {
Pierre Ossman090e7d62009-03-12 10:16:07 +0000104 switch (i) {
105 case encodingTight:
106 case encodingZRLE:
107 case encodingHextile:
108 break;
109 default:
110 if ((i != preferredEncoding) && Decoder::supported(i))
111 encodings[nEncodings++] = i;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 }
113 }
Pierre Ossman090e7d62009-03-12 10:16:07 +0000114
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 if (cp->customCompressLevel && cp->compressLevel >= 0 && cp->compressLevel <= 9)
116 encodings[nEncodings++] = pseudoEncodingCompressLevel0 + cp->compressLevel;
Pierre Ossman3d2ccc42009-03-11 14:33:49 +0000117 if (!cp->noJpeg && cp->qualityLevel >= 0 && cp->qualityLevel <= 9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel;
119
120 writeSetEncodings(nEncodings, encodings);
121}
122
123void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental)
124{
125 startMsg(msgTypeFramebufferUpdateRequest);
126 os->writeU8(incremental);
127 os->writeU16(r.tl.x);
128 os->writeU16(r.tl.y);
129 os->writeU16(r.width());
130 os->writeU16(r.height());
131 endMsg();
132}
133
134
135void CMsgWriter::keyEvent(rdr::U32 key, bool down)
136{
137 startMsg(msgTypeKeyEvent);
138 os->writeU8(down);
139 os->pad(2);
140 os->writeU32(key);
141 endMsg();
142}
143
144
145void CMsgWriter::pointerEvent(const Point& pos, int buttonMask)
146{
147 Point p(pos);
148 if (p.x < 0) p.x = 0;
149 if (p.y < 0) p.y = 0;
150 if (p.x >= cp->width) p.x = cp->width - 1;
151 if (p.y >= cp->height) p.y = cp->height - 1;
152
153 startMsg(msgTypePointerEvent);
154 os->writeU8(buttonMask);
155 os->writeU16(p.x);
156 os->writeU16(p.y);
157 endMsg();
158}
159
160
Adam Tkacacf6c6b2009-02-13 12:42:05 +0000161void CMsgWriter::clientCutText(const char* str, rdr::U32 len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162{
163 startMsg(msgTypeClientCutText);
164 os->pad(3);
165 os->writeU32(len);
166 os->writeBytes(str, len);
167 endMsg();
168}