blob: e44facc9d82c52e0f50619afa6467b97c37f99cb [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 Ossmancbd1b2c2009-03-20 16:05:04 +000036 supportsSetDesktopSize(false),
Pierre Ossmanc5e25602009-03-20 12:59:05 +000037 customCompressLevel(false), compressLevel(6),
DRCb4a83232011-08-19 04:57:18 +000038 noJpeg(false), qualityLevel(-1), fineQualityLevel(-1),
39 subsampling(SUBSAMP_UNDEFINED),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040 name_(0), nEncodings_(0), encodings_(0),
41 currentEncoding_(encodingRaw), verStrPos(0)
42{
43 setName("");
44}
45
46ConnParams::~ConnParams()
47{
48 delete [] name_;
49 delete [] encodings_;
50}
51
52bool ConnParams::readVersion(rdr::InStream* is, bool* done)
53{
54 if (verStrPos >= 12) return false;
55 while (is->checkNoWait(1) && verStrPos < 12) {
56 verStr[verStrPos++] = is->readU8();
57 }
58
59 if (verStrPos < 12) {
60 *done = false;
61 return true;
62 }
63 *done = true;
64 verStr[12] = 0;
65 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
66}
67
68void ConnParams::writeVersion(rdr::OutStream* os)
69{
70 char str[13];
71 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
72 os->writeBytes(str, 12);
73 os->flush();
74}
75
76void ConnParams::setPF(const PixelFormat& pf)
77{
78 pf_ = pf;
79
80 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
81 throw Exception("setPF: not 8, 16 or 32 bpp?");
82}
83
84void ConnParams::setName(const char* name)
85{
86 delete [] name_;
Adam Tkacd36b6262009-09-04 10:57:20 +000087 name_ = strDup(name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088}
89
Peter Åstrand98fe98c2010-02-10 07:43:02 +000090void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091{
92 if (nEncodings > nEncodings_) {
93 delete [] encodings_;
Peter Åstrand98fe98c2010-02-10 07:43:02 +000094 encodings_ = new rdr::S32[nEncodings];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 }
96 nEncodings_ = nEncodings;
97 useCopyRect = false;
98 supportsLocalCursor = false;
99 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000100 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 supportsLocalXCursor = false;
102 supportsLastRect = false;
103 customCompressLevel = false;
104 compressLevel = -1;
105 noJpeg = true;
106 qualityLevel = -1;
DRCb4a83232011-08-19 04:57:18 +0000107 fineQualityLevel = -1;
108 subsampling = SUBSAMP_UNDEFINED;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 currentEncoding_ = encodingRaw;
110
111 for (int i = nEncodings-1; i >= 0; i--) {
112 encodings_[i] = encodings[i];
113
114 if (encodings[i] == encodingCopyRect)
115 useCopyRect = true;
116 else if (encodings[i] == pseudoEncodingCursor)
117 supportsLocalCursor = true;
118 else if (encodings[i] == pseudoEncodingXCursor)
119 supportsLocalXCursor = true;
120 else if (encodings[i] == pseudoEncodingDesktopSize)
121 supportsDesktopResize = true;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000122 else if (encodings[i] == pseudoEncodingExtendedDesktopSize)
123 supportsExtendedDesktopSize = true;
Peter Åstrandc39e0782009-01-15 12:21:42 +0000124 else if (encodings[i] == pseudoEncodingDesktopName)
125 supportsDesktopRename = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126 else if (encodings[i] == pseudoEncodingLastRect)
127 supportsLastRect = true;
128 else if (encodings[i] >= pseudoEncodingCompressLevel0 &&
129 encodings[i] <= pseudoEncodingCompressLevel9) {
130 customCompressLevel = true;
131 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
132 } else if (encodings[i] >= pseudoEncodingQualityLevel0 &&
133 encodings[i] <= pseudoEncodingQualityLevel9) {
134 noJpeg = false;
135 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Adam Tkacea633a72010-07-21 14:12:18 +0000136 } else if (Encoder::supported(encodings[i]))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 currentEncoding_ = encodings[i];
138 }
DRCb4a83232011-08-19 04:57:18 +0000139
140 // If the TurboVNC fine quality/subsampling encodings exist, let them
141 // override the coarse TightVNC quality level
142 for (int i = nEncodings-1; i >= 0; i--) {
143 if (encodings[i] >= pseudoEncodingFineQualityLevel0 + 1 &&
144 encodings[i] <= pseudoEncodingFineQualityLevel100) {
145 noJpeg = false;
146 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
147 } else if (encodings[i] >= pseudoEncodingSubsamp1X &&
148 encodings[i] <= pseudoEncodingSubsampGray) {
149 noJpeg = false;
150 subsampling = (JPEG_SUBSAMP)(encodings[i] - pseudoEncodingSubsamp1X);
151 }
152 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153}