blob: 7b27a73462327b01332320fd0a8fe6167dccaa7b [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),
Pierre Ossmanc5e25602009-03-20 12:59:05 +000032 supportsLocalCursor(false), supportsLocalXCursor(false),
33 supportsDesktopResize(false), supportsExtendedDesktopSize(false),
34 supportsDesktopRename(false), supportsLastRect(false),
Pierre Ossmancbd1b2c2009-03-20 16:05:04 +000035 supportsSetDesktopSize(false),
Pierre Ossmanc5e25602009-03-20 12:59:05 +000036 customCompressLevel(false), compressLevel(6),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037 noJpeg(false), qualityLevel(-1),
38 name_(0), nEncodings_(0), encodings_(0),
39 currentEncoding_(encodingRaw), verStrPos(0)
40{
41 setName("");
42}
43
44ConnParams::~ConnParams()
45{
46 delete [] name_;
47 delete [] encodings_;
48}
49
50bool ConnParams::readVersion(rdr::InStream* is, bool* done)
51{
52 if (verStrPos >= 12) return false;
53 while (is->checkNoWait(1) && verStrPos < 12) {
54 verStr[verStrPos++] = is->readU8();
55 }
56
57 if (verStrPos < 12) {
58 *done = false;
59 return true;
60 }
61 *done = true;
62 verStr[12] = 0;
63 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
64}
65
66void ConnParams::writeVersion(rdr::OutStream* os)
67{
68 char str[13];
69 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
70 os->writeBytes(str, 12);
71 os->flush();
72}
73
74void ConnParams::setPF(const PixelFormat& pf)
75{
76 pf_ = pf;
77
78 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
79 throw Exception("setPF: not 8, 16 or 32 bpp?");
80}
81
82void ConnParams::setName(const char* name)
83{
84 delete [] name_;
85 name_ = strDup(name);
86}
87
88void ConnParams::setEncodings(int nEncodings, const rdr::U32* encodings)
89{
90 if (nEncodings > nEncodings_) {
91 delete [] encodings_;
92 encodings_ = new rdr::U32[nEncodings];
93 }
94 nEncodings_ = nEncodings;
95 useCopyRect = false;
96 supportsLocalCursor = false;
97 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +000098 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099 supportsLocalXCursor = false;
100 supportsLastRect = false;
101 customCompressLevel = false;
102 compressLevel = -1;
103 noJpeg = true;
104 qualityLevel = -1;
105 currentEncoding_ = encodingRaw;
106
107 for (int i = nEncodings-1; i >= 0; i--) {
108 encodings_[i] = encodings[i];
109
110 if (encodings[i] == encodingCopyRect)
111 useCopyRect = true;
112 else if (encodings[i] == pseudoEncodingCursor)
113 supportsLocalCursor = true;
114 else if (encodings[i] == pseudoEncodingXCursor)
115 supportsLocalXCursor = true;
116 else if (encodings[i] == pseudoEncodingDesktopSize)
117 supportsDesktopResize = true;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000118 else if (encodings[i] == pseudoEncodingExtendedDesktopSize)
119 supportsExtendedDesktopSize = true;
Peter Åstrandc39e0782009-01-15 12:21:42 +0000120 else if (encodings[i] == pseudoEncodingDesktopName)
121 supportsDesktopRename = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122 else if (encodings[i] == pseudoEncodingLastRect)
123 supportsLastRect = true;
124 else if (encodings[i] >= pseudoEncodingCompressLevel0 &&
125 encodings[i] <= pseudoEncodingCompressLevel9) {
126 customCompressLevel = true;
127 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
128 } else if (encodings[i] >= pseudoEncodingQualityLevel0 &&
129 encodings[i] <= pseudoEncodingQualityLevel9) {
130 noJpeg = false;
131 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
132 } else if (encodings[i] <= encodingMax && Encoder::supported(encodings[i]))
133 currentEncoding_ = encodings[i];
134 }
135}