blob: ed747523e6301b43735c284bec6ade9fddb12acf [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRCb4a83232011-08-19 04:57:18 +00002 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
Adam Tkac20e0d712008-11-14 14:48:21 +000019#include <stdio.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <rdr/InStream.h>
21#include <rdr/OutStream.h>
22#include <rfb/Exception.h>
23#include <rfb/encodings.h>
24#include <rfb/Encoder.h>
25#include <rfb/ConnParams.h>
26#include <rfb/util.h>
27
28using namespace rfb;
29
30ConnParams::ConnParams()
Adam Tkac3d0af202010-11-18 15:19:47 +000031 : majorVersion(0), minorVersion(0),
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000032 width(0), height(0), useCopyRect(false),
Pierre Ossmanc5e25602009-03-20 12:59:05 +000033 supportsLocalCursor(false), supportsLocalXCursor(false),
34 supportsDesktopResize(false), supportsExtendedDesktopSize(false),
35 supportsDesktopRename(false), supportsLastRect(false),
Pierre Ossmanc754cce2011-11-14 15:44:11 +000036 supportsSetDesktopSize(false), supportsFence(false),
Pierre Ossmanc898d9a2011-11-14 16:22:23 +000037 supportsContinuousUpdates(false),
Pierre Ossman701ad682011-11-20 15:39:17 +000038 customCompressLevel(false), compressLevel(2),
DRCb4a83232011-08-19 04:57:18 +000039 noJpeg(false), qualityLevel(-1), fineQualityLevel(-1),
40 subsampling(SUBSAMP_UNDEFINED),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041 name_(0), nEncodings_(0), encodings_(0),
42 currentEncoding_(encodingRaw), verStrPos(0)
43{
44 setName("");
45}
46
47ConnParams::~ConnParams()
48{
49 delete [] name_;
50 delete [] encodings_;
51}
52
53bool ConnParams::readVersion(rdr::InStream* is, bool* done)
54{
55 if (verStrPos >= 12) return false;
56 while (is->checkNoWait(1) && verStrPos < 12) {
57 verStr[verStrPos++] = is->readU8();
58 }
59
60 if (verStrPos < 12) {
61 *done = false;
62 return true;
63 }
64 *done = true;
65 verStr[12] = 0;
66 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
67}
68
69void ConnParams::writeVersion(rdr::OutStream* os)
70{
71 char str[13];
72 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
73 os->writeBytes(str, 12);
74 os->flush();
75}
76
77void ConnParams::setPF(const PixelFormat& pf)
78{
79 pf_ = pf;
80
81 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
82 throw Exception("setPF: not 8, 16 or 32 bpp?");
83}
84
85void ConnParams::setName(const char* name)
86{
87 delete [] name_;
Adam Tkacd36b6262009-09-04 10:57:20 +000088 name_ = strDup(name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089}
90
Peter Åstrand98fe98c2010-02-10 07:43:02 +000091void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092{
93 if (nEncodings > nEncodings_) {
94 delete [] encodings_;
Peter Åstrand98fe98c2010-02-10 07:43:02 +000095 encodings_ = new rdr::S32[nEncodings];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 }
97 nEncodings_ = nEncodings;
98 useCopyRect = false;
99 supportsLocalCursor = false;
100 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000101 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 supportsLocalXCursor = false;
103 supportsLastRect = false;
104 customCompressLevel = false;
105 compressLevel = -1;
106 noJpeg = true;
107 qualityLevel = -1;
DRCb4a83232011-08-19 04:57:18 +0000108 fineQualityLevel = -1;
109 subsampling = SUBSAMP_UNDEFINED;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110 currentEncoding_ = encodingRaw;
111
112 for (int i = nEncodings-1; i >= 0; i--) {
113 encodings_[i] = encodings[i];
114
115 if (encodings[i] == encodingCopyRect)
116 useCopyRect = true;
117 else if (encodings[i] == pseudoEncodingCursor)
118 supportsLocalCursor = true;
119 else if (encodings[i] == pseudoEncodingXCursor)
120 supportsLocalXCursor = true;
121 else if (encodings[i] == pseudoEncodingDesktopSize)
122 supportsDesktopResize = true;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000123 else if (encodings[i] == pseudoEncodingExtendedDesktopSize)
124 supportsExtendedDesktopSize = true;
Peter Åstrandc39e0782009-01-15 12:21:42 +0000125 else if (encodings[i] == pseudoEncodingDesktopName)
126 supportsDesktopRename = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127 else if (encodings[i] == pseudoEncodingLastRect)
128 supportsLastRect = true;
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000129 else if (encodings[i] == pseudoEncodingFence)
130 supportsFence = true;
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000131 else if (encodings[i] == pseudoEncodingContinuousUpdates)
132 supportsContinuousUpdates = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133 else if (encodings[i] >= pseudoEncodingCompressLevel0 &&
134 encodings[i] <= pseudoEncodingCompressLevel9) {
135 customCompressLevel = true;
136 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
137 } else if (encodings[i] >= pseudoEncodingQualityLevel0 &&
138 encodings[i] <= pseudoEncodingQualityLevel9) {
139 noJpeg = false;
140 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Adam Tkacea633a72010-07-21 14:12:18 +0000141 } else if (Encoder::supported(encodings[i]))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 currentEncoding_ = encodings[i];
143 }
DRCb4a83232011-08-19 04:57:18 +0000144
145 // If the TurboVNC fine quality/subsampling encodings exist, let them
146 // override the coarse TightVNC quality level
147 for (int i = nEncodings-1; i >= 0; i--) {
148 if (encodings[i] >= pseudoEncodingFineQualityLevel0 + 1 &&
149 encodings[i] <= pseudoEncodingFineQualityLevel100) {
150 noJpeg = false;
151 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
152 } else if (encodings[i] >= pseudoEncodingSubsamp1X &&
153 encodings[i] <= pseudoEncodingSubsampGray) {
154 noJpeg = false;
155 subsampling = (JPEG_SUBSAMP)(encodings[i] - pseudoEncodingSubsamp1X);
156 }
157 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158}