blob: 7dcfdccf98d5575e2f04bcba7d3592c76bd08d8c [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 <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),
Peter Åstrand365427a2004-12-29 10:59:03 +000030 supportsLocalCursor(false), supportsDesktopResize(false), supportsLastRect(false),
31 customCompressLevel(false), compressLevel(6), noJpeg(false), qualityLevel(-1),
32 name_(0), nEncodings_(0), encodings_(0),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000033 currentEncoding_(encodingRaw), verStrPos(0)
34{
35 setName("");
36}
37
38ConnParams::~ConnParams()
39{
40 delete [] name_;
41 delete [] encodings_;
42}
43
44bool ConnParams::readVersion(rdr::InStream* is, bool* done)
45{
46 if (verStrPos >= 12) return false;
47 while (is->checkNoWait(1) && verStrPos < 12) {
48 verStr[verStrPos++] = is->readU8();
49 }
50
51 if (verStrPos < 12) {
52 *done = false;
53 return true;
54 }
55 *done = true;
56 verStr[12] = 0;
57 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
58}
59
60void ConnParams::writeVersion(rdr::OutStream* os)
61{
62 char str[13];
63 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
64 os->writeBytes(str, 12);
65 os->flush();
66}
67
68void ConnParams::setPF(const PixelFormat& pf)
69{
70 pf_ = pf;
71
72 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
73 throw Exception("setPF: not 8, 16 or 32 bpp?");
74}
75
76void ConnParams::setName(const char* name)
77{
78 delete [] name_;
79 name_ = strDup(name);
80}
81
82void ConnParams::setEncodings(int nEncodings, const rdr::U32* encodings)
83{
84 if (nEncodings > nEncodings_) {
85 delete [] encodings_;
86 encodings_ = new rdr::U32[nEncodings];
87 }
88 nEncodings_ = nEncodings;
89 useCopyRect = false;
90 supportsLocalCursor = false;
Peter Åstrand462753d2004-11-16 15:23:25 +000091 supportsLastRect = false;
Peter Åstrand365427a2004-12-29 10:59:03 +000092 customCompressLevel = false;
93 compressLevel = -1;
94 noJpeg = true;
Peter Åstrand462753d2004-11-16 15:23:25 +000095 qualityLevel = -1;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000096 currentEncoding_ = encodingRaw;
97
98 for (int i = nEncodings-1; i >= 0; i--) {
99 encodings_[i] = encodings[i];
100
101 if (encodings[i] == encodingCopyRect)
102 useCopyRect = true;
103 else if (encodings[i] == pseudoEncodingCursor)
104 supportsLocalCursor = true;
105 else if (encodings[i] == pseudoEncodingDesktopSize)
106 supportsDesktopResize = true;
Peter Åstrand462753d2004-11-16 15:23:25 +0000107 else if (encodings[i] == pseudoEncodingLastRect)
108 supportsLastRect = true;
Peter Åstrand365427a2004-12-29 10:59:03 +0000109 else if (encodings[i] >= pseudoEncodingCompressLevel0 &&
110 encodings[i] <= pseudoEncodingCompressLevel9) {
111 customCompressLevel = true;
112 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
113 } else if (encodings[i] >= pseudoEncodingQualityLevel0 &&
114 encodings[i] <= pseudoEncodingQualityLevel9) {
115 noJpeg = false;
116 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
117 } else if (encodings[i] <= encodingMax && Encoder::supported(encodings[i]))
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000118 currentEncoding_ = encodings[i];
119 }
120}