blob: 4d14dac500096db22a8fee3c0ce55cd939a750fa [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.
Pierre Ossmanb948a912014-01-15 13:23:43 +01003 * Copyright 2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
Adam Tkac20e0d712008-11-14 14:48:21 +000020#include <stdio.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021#include <rdr/InStream.h>
22#include <rdr/OutStream.h>
23#include <rfb/Exception.h>
24#include <rfb/encodings.h>
25#include <rfb/Encoder.h>
26#include <rfb/ConnParams.h>
27#include <rfb/util.h>
28
29using namespace rfb;
30
31ConnParams::ConnParams()
Adam Tkac3d0af202010-11-18 15:19:47 +000032 : majorVersion(0), minorVersion(0),
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000033 width(0), height(0), useCopyRect(false),
Pierre Ossmanc5e25602009-03-20 12:59:05 +000034 supportsLocalCursor(false), supportsLocalXCursor(false),
35 supportsDesktopResize(false), supportsExtendedDesktopSize(false),
36 supportsDesktopRename(false), supportsLastRect(false),
Pierre Ossmanc754cce2011-11-14 15:44:11 +000037 supportsSetDesktopSize(false), supportsFence(false),
Pierre Ossmanc898d9a2011-11-14 16:22:23 +000038 supportsContinuousUpdates(false),
Pierre Ossman701ad682011-11-20 15:39:17 +000039 customCompressLevel(false), compressLevel(2),
DRCb4a83232011-08-19 04:57:18 +000040 noJpeg(false), qualityLevel(-1), fineQualityLevel(-1),
Pierre Ossmanb948a912014-01-15 13:23:43 +010041 subsampling(subsampleUndefined), name_(0),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 currentEncoding_(encodingRaw), verStrPos(0)
43{
44 setName("");
45}
46
47ConnParams::~ConnParams()
48{
49 delete [] name_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050}
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{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092 useCopyRect = false;
93 supportsLocalCursor = false;
94 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +000095 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 supportsLocalXCursor = false;
97 supportsLastRect = false;
98 customCompressLevel = false;
99 compressLevel = -1;
100 noJpeg = true;
101 qualityLevel = -1;
DRCb4a83232011-08-19 04:57:18 +0000102 fineQualityLevel = -1;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100103 subsampling = subsampleUndefined;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 currentEncoding_ = encodingRaw;
105
106 for (int i = nEncodings-1; i >= 0; i--) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 if (encodings[i] == encodingCopyRect)
108 useCopyRect = true;
109 else if (encodings[i] == pseudoEncodingCursor)
110 supportsLocalCursor = true;
111 else if (encodings[i] == pseudoEncodingXCursor)
112 supportsLocalXCursor = true;
113 else if (encodings[i] == pseudoEncodingDesktopSize)
114 supportsDesktopResize = true;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000115 else if (encodings[i] == pseudoEncodingExtendedDesktopSize)
116 supportsExtendedDesktopSize = true;
Peter Åstrandc39e0782009-01-15 12:21:42 +0000117 else if (encodings[i] == pseudoEncodingDesktopName)
118 supportsDesktopRename = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119 else if (encodings[i] == pseudoEncodingLastRect)
120 supportsLastRect = true;
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000121 else if (encodings[i] == pseudoEncodingFence)
122 supportsFence = true;
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000123 else if (encodings[i] == pseudoEncodingContinuousUpdates)
124 supportsContinuousUpdates = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125 else if (encodings[i] >= pseudoEncodingCompressLevel0 &&
126 encodings[i] <= pseudoEncodingCompressLevel9) {
127 customCompressLevel = true;
128 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
129 } else if (encodings[i] >= pseudoEncodingQualityLevel0 &&
130 encodings[i] <= pseudoEncodingQualityLevel9) {
131 noJpeg = false;
132 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Adam Tkacea633a72010-07-21 14:12:18 +0000133 } else if (Encoder::supported(encodings[i]))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000134 currentEncoding_ = encodings[i];
135 }
DRCb4a83232011-08-19 04:57:18 +0000136
137 // If the TurboVNC fine quality/subsampling encodings exist, let them
138 // override the coarse TightVNC quality level
139 for (int i = nEncodings-1; i >= 0; i--) {
140 if (encodings[i] >= pseudoEncodingFineQualityLevel0 + 1 &&
141 encodings[i] <= pseudoEncodingFineQualityLevel100) {
142 noJpeg = false;
143 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
144 } else if (encodings[i] >= pseudoEncodingSubsamp1X &&
Pierre Ossmanb948a912014-01-15 13:23:43 +0100145 encodings[i] <= pseudoEncodingSubsamp16X) {
DRCb4a83232011-08-19 04:57:18 +0000146 noJpeg = false;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100147 switch (encodings[i]) {
148 case pseudoEncodingSubsamp1X:
149 subsampling = subsampleNone;
150 break;
151 case pseudoEncodingSubsampGray:
152 subsampling = subsampleGray;
153 break;
154 case pseudoEncodingSubsamp2X:
155 subsampling = subsample2X;
156 break;
157 case pseudoEncodingSubsamp4X:
158 subsampling = subsample4X;
159 break;
160 case pseudoEncodingSubsamp8X:
161 subsampling = subsample8X;
162 break;
163 case pseudoEncodingSubsamp16X:
164 subsampling = subsample16X;
165 break;
166 }
DRCb4a83232011-08-19 04:57:18 +0000167 }
168 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169}