blob: 80b4a5ac4dfbada7e4abdbfb0ee07c1d4890a769 [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>
Pierre Ossman2fa63f82016-12-05 15:26:21 +010025#include <rfb/ledStates.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#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),
Pierre Ossman8053c8e2017-02-21 12:59:04 +010035 supportsLocalCursorWithAlpha(false),
Pierre Ossmanc5e25602009-03-20 12:59:05 +000036 supportsDesktopResize(false), supportsExtendedDesktopSize(false),
37 supportsDesktopRename(false), supportsLastRect(false),
Pierre Ossman5ae28212017-05-16 14:30:38 +020038 supportsLEDState(false), supportsQEMUKeyEvent(false),
39 supportsSetDesktopSize(false), supportsFence(false),
40 supportsContinuousUpdates(false),
Pierre Ossmana22459d2014-03-17 14:42:10 +010041 compressLevel(2), qualityLevel(-1), fineQualityLevel(-1),
Pierre Ossman2fa63f82016-12-05 15:26:21 +010042 subsampling(subsampleUndefined), name_(0), verStrPos(0),
43 ledState_(ledUnknown)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044{
45 setName("");
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010046 cursor_ = new Cursor(0, 0, Point(), NULL);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047}
48
49ConnParams::~ConnParams()
50{
51 delete [] name_;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010052 delete cursor_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053}
54
55bool ConnParams::readVersion(rdr::InStream* is, bool* done)
56{
57 if (verStrPos >= 12) return false;
58 while (is->checkNoWait(1) && verStrPos < 12) {
59 verStr[verStrPos++] = is->readU8();
60 }
61
62 if (verStrPos < 12) {
63 *done = false;
64 return true;
65 }
66 *done = true;
67 verStr[12] = 0;
68 return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2);
69}
70
71void ConnParams::writeVersion(rdr::OutStream* os)
72{
73 char str[13];
74 sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion);
75 os->writeBytes(str, 12);
76 os->flush();
77}
78
79void ConnParams::setPF(const PixelFormat& pf)
80{
81 pf_ = pf;
82
83 if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32)
84 throw Exception("setPF: not 8, 16 or 32 bpp?");
85}
86
87void ConnParams::setName(const char* name)
88{
89 delete [] name_;
Adam Tkacd36b6262009-09-04 10:57:20 +000090 name_ = strDup(name);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091}
92
Pierre Ossman126e5642014-02-13 14:40:25 +010093void ConnParams::setCursor(const Cursor& other)
94{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010095 delete cursor_;
96 cursor_ = new Cursor(other);
Pierre Ossman126e5642014-02-13 14:40:25 +010097}
98
Pierre Ossmanf22d3502015-11-10 12:58:49 +010099bool ConnParams::supportsEncoding(rdr::S32 encoding) const
Pierre Ossmanc0397262014-03-14 15:59:46 +0100100{
101 return encodings_.count(encoding) != 0;
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;
Pierre Ossman8053c8e2017-02-21 12:59:04 +0100108 supportsLocalCursorWithAlpha = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 supportsDesktopResize = false;
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000110 supportsExtendedDesktopSize = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111 supportsLocalXCursor = false;
112 supportsLastRect = false;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200113 supportsQEMUKeyEvent = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114 compressLevel = -1;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 qualityLevel = -1;
DRCb4a83232011-08-19 04:57:18 +0000116 fineQualityLevel = -1;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100117 subsampling = subsampleUndefined;
Pierre Ossmanc0397262014-03-14 15:59:46 +0100118
119 encodings_.clear();
120 encodings_.insert(encodingRaw);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121
122 for (int i = nEncodings-1; i >= 0; i--) {
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100123 switch (encodings[i]) {
124 case encodingCopyRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125 useCopyRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100126 break;
127 case pseudoEncodingCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128 supportsLocalCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100129 break;
130 case pseudoEncodingXCursor:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131 supportsLocalXCursor = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100132 break;
Pierre Ossman8053c8e2017-02-21 12:59:04 +0100133 case pseudoEncodingCursorWithAlpha:
134 supportsLocalCursorWithAlpha = true;
135 break;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100136 case pseudoEncodingDesktopSize:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 supportsDesktopResize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100138 break;
139 case pseudoEncodingExtendedDesktopSize:
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000140 supportsExtendedDesktopSize = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100141 break;
142 case pseudoEncodingDesktopName:
Peter Åstrandc39e0782009-01-15 12:21:42 +0000143 supportsDesktopRename = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100144 break;
145 case pseudoEncodingLastRect:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 supportsLastRect = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100147 break;
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100148 case pseudoEncodingLEDState:
149 supportsLEDState = true;
Pierre Ossman58a4c132018-03-28 12:33:22 +0200150 break;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200151 case pseudoEncodingQEMUKeyEvent:
152 supportsQEMUKeyEvent = true;
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100153 break;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100154 case pseudoEncodingFence:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000155 supportsFence = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100156 break;
157 case pseudoEncodingContinuousUpdates:
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000158 supportsContinuousUpdates = true;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100159 break;
160 case pseudoEncodingSubsamp1X:
161 subsampling = subsampleNone;
162 break;
163 case pseudoEncodingSubsampGray:
164 subsampling = subsampleGray;
165 break;
166 case pseudoEncodingSubsamp2X:
167 subsampling = subsample2X;
168 break;
169 case pseudoEncodingSubsamp4X:
170 subsampling = subsample4X;
171 break;
172 case pseudoEncodingSubsamp8X:
173 subsampling = subsample8X;
174 break;
175 case pseudoEncodingSubsamp16X:
176 subsampling = subsample16X;
177 break;
178 }
179
180 if (encodings[i] >= pseudoEncodingCompressLevel0 &&
181 encodings[i] <= pseudoEncodingCompressLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182 compressLevel = encodings[i] - pseudoEncodingCompressLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100183
184 if (encodings[i] >= pseudoEncodingQualityLevel0 &&
185 encodings[i] <= pseudoEncodingQualityLevel9)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 qualityLevel = encodings[i] - pseudoEncodingQualityLevel0;
Pierre Ossman6bcf1372014-01-15 13:44:04 +0100187
188 if (encodings[i] >= pseudoEncodingFineQualityLevel0 &&
189 encodings[i] <= pseudoEncodingFineQualityLevel100)
190 fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0;
191
Pierre Ossmanc0397262014-03-14 15:59:46 +0100192 if (encodings[i] > 0)
193 encodings_.insert(encodings[i]);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195}
Pierre Ossman2fa63f82016-12-05 15:26:21 +0100196
197void ConnParams::setLEDState(unsigned int state)
198{
199 ledState_ = state;
200}