blob: 36f6daa66059b26f5665db6cb2681f3cfda6cac8 [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 Ossmana22459d2014-03-17 14:42:10 +010039 compressLevel(2), qualityLevel(-1), fineQualityLevel(-1),
Pierre Ossmanb948a912014-01-15 13:23:43 +010040 subsampling(subsampleUndefined), name_(0),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041 currentEncoding_(encodingRaw), verStrPos(0)
42{
43 setName("");
44}
45
46ConnParams::~ConnParams()
47{
48 delete [] name_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049}
50
51bool ConnParams::readVersion(rdr::InStream* is, bool* done)
52{
53 if (verStrPos >= 12) return false;
54 while (is->checkNoWait(1) && verStrPos < 12) {
55 verStr[verStrPos++] = is->readU8();
56 }
57
58 if (verStrPos < 12) {
59 *done = false;
60 return true;
61 }
62 *done = true;
63 verStr[12] = 0;
64 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
65}
66
67void ConnParams::writeVersion(rdr::OutStream* os)
68{
69 char str[13];
70 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
71 os->writeBytes(str, 12);
72 os->flush();
73}
74
75void ConnParams::setPF(const PixelFormat& pf)
76{
77 pf_ = pf;
78
79 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
80 throw Exception("setPF: not 8, 16 or 32 bpp?");
81}
82
83void ConnParams::setName(const char* name)
84{
85 delete [] name_;
Adam Tkacd36b6262009-09-04 10:57:20 +000086 name_ = strDup(name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087}
88
Pierre Ossman126e5642014-02-13 14:40:25 +010089void ConnParams::setCursor(const Cursor& other)
90{
91 const rdr::U8* data;
92 int stride;
93
94 cursor_.hotspot = other.hotspot;
95 cursor_.setPF(other.getPF());
96 cursor_.setSize(other.width(), other.height());
97
98 data = other.getBuffer(other.getRect(), &stride);
99 cursor_.imageRect(cursor_.getRect(), data, stride);
100
101 memcpy(cursor_.mask.buf, other.mask.buf, cursor_.maskLen());
102}
103
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000104void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 useCopyRect = false;
107 supportsLocalCursor = false;
108 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000109 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110 supportsLocalXCursor = false;
111 supportsLastRect = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 compressLevel = -1;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 qualityLevel = -1;
DRCb4a83232011-08-19 04:57:18 +0000114 fineQualityLevel = -1;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100115 subsampling = subsampleUndefined;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 currentEncoding_ = encodingRaw;
117
118 for (int i = nEncodings-1; i >= 0; i--) {
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100119 switch (encodings[i]) {
120 case encodingCopyRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121 useCopyRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100122 break;
123 case pseudoEncodingCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124 supportsLocalCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100125 break;
126 case pseudoEncodingXCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127 supportsLocalXCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100128 break;
129 case pseudoEncodingDesktopSize:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130 supportsDesktopResize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100131 break;
132 case pseudoEncodingExtendedDesktopSize:
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000133 supportsExtendedDesktopSize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100134 break;
135 case pseudoEncodingDesktopName:
Peter Åstrandc39e0782009-01-15 12:21:42 +0000136 supportsDesktopRename = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100137 break;
138 case pseudoEncodingLastRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 supportsLastRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100140 break;
141 case pseudoEncodingFence:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000142 supportsFence = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100143 break;
144 case pseudoEncodingContinuousUpdates:
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000145 supportsContinuousUpdates = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100146 break;
147 case pseudoEncodingSubsamp1X:
148 subsampling = subsampleNone;
149 break;
150 case pseudoEncodingSubsampGray:
151 subsampling = subsampleGray;
152 break;
153 case pseudoEncodingSubsamp2X:
154 subsampling = subsample2X;
155 break;
156 case pseudoEncodingSubsamp4X:
157 subsampling = subsample4X;
158 break;
159 case pseudoEncodingSubsamp8X:
160 subsampling = subsample8X;
161 break;
162 case pseudoEncodingSubsamp16X:
163 subsampling = subsample16X;
164 break;
165 }
166
167 if (encodings[i] >= pseudoEncodingCompressLevel0 &&
168 encodings[i] <= pseudoEncodingCompressLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100170
171 if (encodings[i] >= pseudoEncodingQualityLevel0 &&
172 encodings[i] <= pseudoEncodingQualityLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100174
175 if (encodings[i] >= pseudoEncodingFineQualityLevel0 &&
176 encodings[i] <= pseudoEncodingFineQualityLevel100)
177 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
178
179 if (Encoder::supported(encodings[i]))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180 currentEncoding_ = encodings[i];
181 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182}