Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 3 | * Copyright 2014 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 4 | * |
| 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 Tkac | 20e0d71 | 2008-11-14 14:48:21 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 21 | #include <rdr/InStream.h> |
| 22 | #include <rdr/OutStream.h> |
| 23 | #include <rfb/Exception.h> |
| 24 | #include <rfb/encodings.h> |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame^] | 25 | #include <rfb/ledStates.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/ConnParams.h> |
| 27 | #include <rfb/util.h> |
| 28 | |
| 29 | using namespace rfb; |
| 30 | |
| 31 | ConnParams::ConnParams() |
Adam Tkac | 3d0af20 | 2010-11-18 15:19:47 +0000 | [diff] [blame] | 32 | : majorVersion(0), minorVersion(0), |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame] | 33 | width(0), height(0), useCopyRect(false), |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 34 | supportsLocalCursor(false), supportsLocalXCursor(false), |
Pierre Ossman | 8053c8e | 2017-02-21 12:59:04 +0100 | [diff] [blame] | 35 | supportsLocalCursorWithAlpha(false), |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 36 | supportsDesktopResize(false), supportsExtendedDesktopSize(false), |
| 37 | supportsDesktopRename(false), supportsLastRect(false), |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame^] | 38 | supportsLEDState(false), supportsSetDesktopSize(false), |
| 39 | supportsFence(false), supportsContinuousUpdates(false), |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 40 | compressLevel(2), qualityLevel(-1), fineQualityLevel(-1), |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame^] | 41 | subsampling(subsampleUndefined), name_(0), verStrPos(0), |
| 42 | ledState_(ledUnknown) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 43 | { |
| 44 | setName(""); |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 45 | cursor_ = new Cursor(0, 0, Point(), NULL); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ConnParams::~ConnParams() |
| 49 | { |
| 50 | delete [] name_; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 51 | delete cursor_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | bool ConnParams::readVersion(rdr::InStream* is, bool* done) |
| 55 | { |
| 56 | if (verStrPos >= 12) return false; |
| 57 | while (is->checkNoWait(1) && verStrPos < 12) { |
| 58 | verStr[verStrPos++] = is->readU8(); |
| 59 | } |
| 60 | |
| 61 | if (verStrPos < 12) { |
| 62 | *done = false; |
| 63 | return true; |
| 64 | } |
| 65 | *done = true; |
| 66 | verStr[12] = 0; |
| 67 | return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2); |
| 68 | } |
| 69 | |
| 70 | void ConnParams::writeVersion(rdr::OutStream* os) |
| 71 | { |
| 72 | char str[13]; |
| 73 | sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion); |
| 74 | os->writeBytes(str, 12); |
| 75 | os->flush(); |
| 76 | } |
| 77 | |
| 78 | void ConnParams::setPF(const PixelFormat& pf) |
| 79 | { |
| 80 | pf_ = pf; |
| 81 | |
| 82 | if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32) |
| 83 | throw Exception("setPF: not 8, 16 or 32 bpp?"); |
| 84 | } |
| 85 | |
| 86 | void ConnParams::setName(const char* name) |
| 87 | { |
| 88 | delete [] name_; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 89 | name_ = strDup(name); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 92 | void ConnParams::setCursor(const Cursor& other) |
| 93 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 94 | delete cursor_; |
| 95 | cursor_ = new Cursor(other); |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Pierre Ossman | f22d350 | 2015-11-10 12:58:49 +0100 | [diff] [blame] | 98 | bool ConnParams::supportsEncoding(rdr::S32 encoding) const |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 99 | { |
| 100 | return encodings_.count(encoding) != 0; |
| 101 | } |
| 102 | |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 103 | void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 105 | useCopyRect = false; |
| 106 | supportsLocalCursor = false; |
Pierre Ossman | 8053c8e | 2017-02-21 12:59:04 +0100 | [diff] [blame] | 107 | supportsLocalCursorWithAlpha = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 108 | supportsDesktopResize = false; |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 109 | supportsExtendedDesktopSize = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | supportsLocalXCursor = false; |
| 111 | supportsLastRect = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | compressLevel = -1; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | qualityLevel = -1; |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 114 | fineQualityLevel = -1; |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 115 | subsampling = subsampleUndefined; |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 116 | |
| 117 | encodings_.clear(); |
| 118 | encodings_.insert(encodingRaw); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | |
| 120 | for (int i = nEncodings-1; i >= 0; i--) { |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 121 | switch (encodings[i]) { |
| 122 | case encodingCopyRect: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 123 | useCopyRect = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 124 | break; |
| 125 | case pseudoEncodingCursor: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 126 | supportsLocalCursor = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 127 | break; |
| 128 | case pseudoEncodingXCursor: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 129 | supportsLocalXCursor = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 130 | break; |
Pierre Ossman | 8053c8e | 2017-02-21 12:59:04 +0100 | [diff] [blame] | 131 | case pseudoEncodingCursorWithAlpha: |
| 132 | supportsLocalCursorWithAlpha = true; |
| 133 | break; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 134 | case pseudoEncodingDesktopSize: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 135 | supportsDesktopResize = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 136 | break; |
| 137 | case pseudoEncodingExtendedDesktopSize: |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 138 | supportsExtendedDesktopSize = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 139 | break; |
| 140 | case pseudoEncodingDesktopName: |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 141 | supportsDesktopRename = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 142 | break; |
| 143 | case pseudoEncodingLastRect: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 144 | supportsLastRect = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 145 | break; |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame^] | 146 | case pseudoEncodingLEDState: |
| 147 | supportsLEDState = true; |
| 148 | break; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 149 | case pseudoEncodingFence: |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 150 | supportsFence = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 151 | break; |
| 152 | case pseudoEncodingContinuousUpdates: |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 153 | supportsContinuousUpdates = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 154 | break; |
| 155 | case pseudoEncodingSubsamp1X: |
| 156 | subsampling = subsampleNone; |
| 157 | break; |
| 158 | case pseudoEncodingSubsampGray: |
| 159 | subsampling = subsampleGray; |
| 160 | break; |
| 161 | case pseudoEncodingSubsamp2X: |
| 162 | subsampling = subsample2X; |
| 163 | break; |
| 164 | case pseudoEncodingSubsamp4X: |
| 165 | subsampling = subsample4X; |
| 166 | break; |
| 167 | case pseudoEncodingSubsamp8X: |
| 168 | subsampling = subsample8X; |
| 169 | break; |
| 170 | case pseudoEncodingSubsamp16X: |
| 171 | subsampling = subsample16X; |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | if (encodings[i] >= pseudoEncodingCompressLevel0 && |
| 176 | encodings[i] <= pseudoEncodingCompressLevel9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 177 | compressLevel = encodings[i] - pseudoEncodingCompressLevel0; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 178 | |
| 179 | if (encodings[i] >= pseudoEncodingQualityLevel0 && |
| 180 | encodings[i] <= pseudoEncodingQualityLevel9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 181 | qualityLevel = encodings[i] - pseudoEncodingQualityLevel0; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 182 | |
| 183 | if (encodings[i] >= pseudoEncodingFineQualityLevel0 && |
| 184 | encodings[i] <= pseudoEncodingFineQualityLevel100) |
| 185 | fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0; |
| 186 | |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 187 | if (encodings[i] > 0) |
| 188 | encodings_.insert(encodings[i]); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | } |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame^] | 191 | |
| 192 | void ConnParams::setLEDState(unsigned int state) |
| 193 | { |
| 194 | ledState_ = state; |
| 195 | } |