blob: d4ae58945e47456da1becbd7997f434dbb81087a [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 <rdr/InStream.h>
19#include <rdr/OutStream.h>
20#include <rfb/Exception.h>
21#include <rfb/encodings.h>
22#include <rfb/Encoder.h>
23#include <rfb/ConnParams.h>
24#include <rfb/util.h>
25
26using namespace rfb;
27
28ConnParams::ConnParams()
29 : majorVersion(0), minorVersion(0), width(0), height(0), useCopyRect(false),
30 supportsLocalCursor(false), supportsLocalXCursor(false), supportsDesktopResize(true),
31 supportsLastRect(false), customCompressLevel(false), compressLevel(6),
32 noJpeg(false), qualityLevel(-1),
33 name_(0), nEncodings_(0), encodings_(0),
34 currentEncoding_(encodingRaw), verStrPos(0)
35{
36 setName("");
37}
38
39ConnParams::~ConnParams()
40{
41 delete [] name_;
42 delete [] encodings_;
43}
44
45bool ConnParams::readVersion(rdr::InStream* is, bool* done)
46{
47 if (verStrPos >= 12) return false;
48 while (is->checkNoWait(1) && verStrPos < 12) {
49 verStr[verStrPos++] = is->readU8();
50 }
51
52 if (verStrPos < 12) {
53 *done = false;
54 return true;
55 }
56 *done = true;
57 verStr[12] = 0;
58 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
59}
60
61void ConnParams::writeVersion(rdr::OutStream* os)
62{
63 char str[13];
64 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
65 os->writeBytes(str, 12);
66 os->flush();
67}
68
69void ConnParams::setPF(const PixelFormat& pf)
70{
71 pf_ = pf;
72
73 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
74 throw Exception("setPF: not 8, 16 or 32 bpp?");
75}
76
77void ConnParams::setName(const char* name)
78{
79 delete [] name_;
80 name_ = strDup(name);
81}
82
83void ConnParams::setEncodings(int nEncodings, const rdr::U32* encodings)
84{
85 if (nEncodings > nEncodings_) {
86 delete [] encodings_;
87 encodings_ = new rdr::U32[nEncodings];
88 }
89 nEncodings_ = nEncodings;
90 useCopyRect = false;
91 supportsLocalCursor = false;
92 supportsDesktopResize = false;
93 supportsLocalXCursor = false;
94 supportsLastRect = false;
95 customCompressLevel = false;
96 compressLevel = -1;
97 noJpeg = true;
98 qualityLevel = -1;
99 currentEncoding_ = encodingRaw;
100
101 for (int i = nEncodings-1; i >= 0; i--) {
102 encodings_[i] = encodings[i];
103
104 if (encodings[i] == encodingCopyRect)
105 useCopyRect = true;
106 else if (encodings[i] == pseudoEncodingCursor)
107 supportsLocalCursor = true;
108 else if (encodings[i] == pseudoEncodingXCursor)
109 supportsLocalXCursor = true;
110 else if (encodings[i] == pseudoEncodingDesktopSize)
111 supportsDesktopResize = true;
112 else if (encodings[i] == pseudoEncodingLastRect)
113 supportsLastRect = true;
114 else if (encodings[i] >= pseudoEncodingCompressLevel0 &&
115 encodings[i] <= pseudoEncodingCompressLevel9) {
116 customCompressLevel = true;
117 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
118 } else if (encodings[i] >= pseudoEncodingQualityLevel0 &&
119 encodings[i] <= pseudoEncodingQualityLevel9) {
120 noJpeg = false;
121 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
122 } else if (encodings[i] <= encodingMax && Encoder::supported(encodings[i]))
123 currentEncoding_ = encodings[i];
124 }
125}