blob: 26e0d50ca1d7eb5780862d208bf9a2cc319bf3ef [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;
Peter Åstrandc39e0782009-01-15 12:21:42 +000067 if (cp->supportsDesktopRename)
68 encodings[nEncodings++] = pseudoEncodingDesktopName;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069 if (Decoder::supported(preferredEncoding)) {
70 encodings[nEncodings++] = preferredEncoding;
71 }
72 if (useCopyRect) {
73 encodings[nEncodings++] = encodingCopyRect;
74 }
75 for (int i = encodingMax; i >= 0; i--) {
76 if (i != preferredEncoding && Decoder::supported(i)) {
77 encodings[nEncodings++] = i;
78 }
79 }
80 encodings[nEncodings++] = pseudoEncodingLastRect;
81 if (cp->customCompressLevel && cp->compressLevel >= 0 && cp->compressLevel <= 9)
82 encodings[nEncodings++] = pseudoEncodingCompressLevel0 + cp->compressLevel;
83 if (!cp->noJpeg && cp->qualityLevel >= 1 && cp->qualityLevel <= 9)
84 encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel;
85
86 writeSetEncodings(nEncodings, encodings);
87}
88
89void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental)
90{
91 startMsg(msgTypeFramebufferUpdateRequest);
92 os->writeU8(incremental);
93 os->writeU16(r.tl.x);
94 os->writeU16(r.tl.y);
95 os->writeU16(r.width());
96 os->writeU16(r.height());
97 endMsg();
98}
99
100
101void CMsgWriter::keyEvent(rdr::U32 key, bool down)
102{
103 startMsg(msgTypeKeyEvent);
104 os->writeU8(down);
105 os->pad(2);
106 os->writeU32(key);
107 endMsg();
108}
109
110
111void CMsgWriter::pointerEvent(const Point& pos, int buttonMask)
112{
113 Point p(pos);
114 if (p.x < 0) p.x = 0;
115 if (p.y < 0) p.y = 0;
116 if (p.x >= cp->width) p.x = cp->width - 1;
117 if (p.y >= cp->height) p.y = cp->height - 1;
118
119 startMsg(msgTypePointerEvent);
120 os->writeU8(buttonMask);
121 os->writeU16(p.x);
122 os->writeU16(p.y);
123 endMsg();
124}
125
126
127void CMsgWriter::clientCutText(const char* str, int len)
128{
129 startMsg(msgTypeClientCutText);
130 os->pad(3);
131 os->writeU32(len);
132 os->writeBytes(str, len);
133 endMsg();
134}