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