blob: 1693cbfdb31fbd6f002c76f5260462ac121cce4c [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 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;
Peter Åstrand462753d2004-11-16 15:23:25 +000062 rdr::U32 encodings[encodingMax+3];
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000063 if (cp->supportsLocalCursor)
64 encodings[nEncodings++] = pseudoEncodingCursor;
65 if (cp->supportsDesktopResize)
66 encodings[nEncodings++] = pseudoEncodingDesktopSize;
67 if (Decoder::supported(preferredEncoding)) {
68 encodings[nEncodings++] = preferredEncoding;
69 }
70 if (useCopyRect) {
71 encodings[nEncodings++] = encodingCopyRect;
72 }
73 for (int i = encodingMax; i >= 0; i--) {
74 if (i != preferredEncoding && Decoder::supported(i)) {
75 encodings[nEncodings++] = i;
76 }
77 }
Peter Åstrand462753d2004-11-16 15:23:25 +000078 encodings[nEncodings++] = pseudoEncodingLastRect;
Peter Åstranded9d4ae2004-12-07 11:59:14 +000079 if (cp->qualityLevel >= 0 && cp->qualityLevel <= 9)
80 encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel;
81
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000082 writeSetEncodings(nEncodings, encodings);
83}
84
85void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental)
86{
87 startMsg(msgTypeFramebufferUpdateRequest);
88 os->writeU8(incremental);
89 os->writeU16(r.tl.x);
90 os->writeU16(r.tl.y);
91 os->writeU16(r.width());
92 os->writeU16(r.height());
93 endMsg();
94}
95
96
97void CMsgWriter::writeKeyEvent(rdr::U32 key, bool down)
98{
99 startMsg(msgTypeKeyEvent);
100 os->writeU8(down);
101 os->pad(2);
102 os->writeU32(key);
103 endMsg();
104}
105
106
107void CMsgWriter::writePointerEvent(int x, int y, int buttonMask)
108{
109 if (x < 0) x = 0;
110 if (y < 0) y = 0;
111 if (x >= cp->width) x = cp->width - 1;
112 if (y >= cp->height) y = cp->height - 1;
113
114 startMsg(msgTypePointerEvent);
115 os->writeU8(buttonMask);
116 os->writeU16(x);
117 os->writeU16(y);
118 endMsg();
119}
120
121
122void CMsgWriter::writeClientCutText(const char* str, int len)
123{
124 startMsg(msgTypeClientCutText);
125 os->pad(3);
126 os->writeU32(len);
127 os->writeBytes(str, len);
128 endMsg();
129}
130
131void CMsgWriter::setOutStream(rdr::OutStream* os_)
132{
133 os = os_;
134}