blob: 615f49ae1f1b9c0c3b34fa1a4f04faf59f897134 [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>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025#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 Ossmana22459d2014-03-17 14:42:10 +010038 compressLevel(2), qualityLevel(-1), fineQualityLevel(-1),
Pierre Ossman48700812014-09-17 17:11:56 +020039 subsampling(subsampleUndefined), name_(0), verStrPos(0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040{
41 setName("");
42}
43
44ConnParams::~ConnParams()
45{
46 delete [] name_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047}
48
49bool ConnParams::readVersion(rdr::InStream* is, bool* done)
50{
51 if (verStrPos >= 12) return false;
52 while (is->checkNoWait(1) && verStrPos < 12) {
53 verStr[verStrPos++] = is->readU8();
54 }
55
56 if (verStrPos < 12) {
57 *done = false;
58 return true;
59 }
60 *done = true;
61 verStr[12] = 0;
62 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
63}
64
65void ConnParams::writeVersion(rdr::OutStream* os)
66{
67 char str[13];
68 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
69 os->writeBytes(str, 12);
70 os->flush();
71}
72
73void ConnParams::setPF(const PixelFormat& pf)
74{
75 pf_ = pf;
76
77 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
78 throw Exception("setPF: not 8, 16 or 32 bpp?");
79}
80
81void ConnParams::setName(const char* name)
82{
83 delete [] name_;
Adam Tkacd36b6262009-09-04 10:57:20 +000084 name_ = strDup(name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085}
86
Pierre Ossman126e5642014-02-13 14:40:25 +010087void ConnParams::setCursor(const Cursor& other)
88{
89 const rdr::U8* data;
90 int stride;
91
92 cursor_.hotspot = other.hotspot;
93 cursor_.setPF(other.getPF());
94 cursor_.setSize(other.width(), other.height());
95
96 data = other.getBuffer(other.getRect(), &stride);
97 cursor_.imageRect(cursor_.getRect(), data, stride);
98
99 memcpy(cursor_.mask.buf, other.mask.buf, cursor_.maskLen());
100}
101
Pierre Ossmanc0397262014-03-14 15:59:46 +0100102bool ConnParams::supportsEncoding(rdr::S32 encoding)
103{
104 return encodings_.count(encoding) != 0;
105}
106
Peter Åstrand98fe98c2010-02-10 07:43:02 +0000107void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 useCopyRect = false;
110 supportsLocalCursor = false;
111 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000112 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 supportsLocalXCursor = false;
114 supportsLastRect = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 compressLevel = -1;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 qualityLevel = -1;
DRCb4a83232011-08-19 04:57:18 +0000117 fineQualityLevel = -1;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100118 subsampling = subsampleUndefined;
Pierre Ossmanc0397262014-03-14 15:59:46 +0100119
120 encodings_.clear();
121 encodings_.insert(encodingRaw);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122
123 for (int i = nEncodings-1; i >= 0; i--) {
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100124 switch (encodings[i]) {
125 case encodingCopyRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126 useCopyRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100127 break;
128 case pseudoEncodingCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129 supportsLocalCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100130 break;
131 case pseudoEncodingXCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132 supportsLocalXCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100133 break;
134 case pseudoEncodingDesktopSize:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 supportsDesktopResize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100136 break;
137 case pseudoEncodingExtendedDesktopSize:
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000138 supportsExtendedDesktopSize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100139 break;
140 case pseudoEncodingDesktopName:
Peter Åstrandc39e0782009-01-15 12:21:42 +0000141 supportsDesktopRename = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100142 break;
143 case pseudoEncodingLastRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000144 supportsLastRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100145 break;
146 case pseudoEncodingFence:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000147 supportsFence = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100148 break;
149 case pseudoEncodingContinuousUpdates:
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000150 supportsContinuousUpdates = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100151 break;
152 case pseudoEncodingSubsamp1X:
153 subsampling = subsampleNone;
154 break;
155 case pseudoEncodingSubsampGray:
156 subsampling = subsampleGray;
157 break;
158 case pseudoEncodingSubsamp2X:
159 subsampling = subsample2X;
160 break;
161 case pseudoEncodingSubsamp4X:
162 subsampling = subsample4X;
163 break;
164 case pseudoEncodingSubsamp8X:
165 subsampling = subsample8X;
166 break;
167 case pseudoEncodingSubsamp16X:
168 subsampling = subsample16X;
169 break;
170 }
171
172 if (encodings[i] >= pseudoEncodingCompressLevel0 &&
173 encodings[i] <= pseudoEncodingCompressLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100175
176 if (encodings[i] >= pseudoEncodingQualityLevel0 &&
177 encodings[i] <= pseudoEncodingQualityLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100179
180 if (encodings[i] >= pseudoEncodingFineQualityLevel0 &&
181 encodings[i] <= pseudoEncodingFineQualityLevel100)
182 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
183
Pierre Ossmanc0397262014-03-14 15:59:46 +0100184 if (encodings[i] > 0)
185 encodings_.insert(encodings[i]);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187}