blob: be9d48c07d0083975dc9546fd7e2562499cc8830 [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--) {
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100107 switch (encodings[i]) {
108 case encodingCopyRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 useCopyRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100110 break;
111 case pseudoEncodingCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 supportsLocalCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100113 break;
114 case pseudoEncodingXCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 supportsLocalXCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100116 break;
117 case pseudoEncodingDesktopSize:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 supportsDesktopResize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100119 break;
120 case pseudoEncodingExtendedDesktopSize:
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000121 supportsExtendedDesktopSize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100122 break;
123 case pseudoEncodingDesktopName:
Peter Åstrandc39e0782009-01-15 12:21:42 +0000124 supportsDesktopRename = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100125 break;
126 case pseudoEncodingLastRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127 supportsLastRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100128 break;
129 case pseudoEncodingFence:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000130 supportsFence = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100131 break;
132 case pseudoEncodingContinuousUpdates:
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000133 supportsContinuousUpdates = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100134 break;
135 case pseudoEncodingSubsamp1X:
136 subsampling = subsampleNone;
137 break;
138 case pseudoEncodingSubsampGray:
139 subsampling = subsampleGray;
140 break;
141 case pseudoEncodingSubsamp2X:
142 subsampling = subsample2X;
143 break;
144 case pseudoEncodingSubsamp4X:
145 subsampling = subsample4X;
146 break;
147 case pseudoEncodingSubsamp8X:
148 subsampling = subsample8X;
149 break;
150 case pseudoEncodingSubsamp16X:
151 subsampling = subsample16X;
152 break;
153 }
154
155 if (encodings[i] >= pseudoEncodingCompressLevel0 &&
156 encodings[i] <= pseudoEncodingCompressLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100158
159 if (encodings[i] >= pseudoEncodingQualityLevel0 &&
160 encodings[i] <= pseudoEncodingQualityLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100162
163 if (encodings[i] >= pseudoEncodingFineQualityLevel0 &&
164 encodings[i] <= pseudoEncodingFineQualityLevel100)
165 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
166
167 if (Encoder::supported(encodings[i]))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168 currentEncoding_ = encodings[i];
169 }
DRCb4a83232011-08-19 04:57:18 +0000170
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100171 if (compressLevel != -1)
172 customCompressLevel = true;
173 if ((qualityLevel != -1) || (fineQualityLevel != -1) ||
174 (subsampling != subsampleUndefined))
175 noJpeg = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176}