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 | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame^] | 25 | #include <rfb/EncodeManager.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), |
| 35 | supportsDesktopResize(false), supportsExtendedDesktopSize(false), |
| 36 | supportsDesktopRename(false), supportsLastRect(false), |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 37 | supportsSetDesktopSize(false), supportsFence(false), |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 38 | supportsContinuousUpdates(false), |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 39 | compressLevel(2), qualityLevel(-1), fineQualityLevel(-1), |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 40 | subsampling(subsampleUndefined), name_(0), |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame^] | 41 | preferredEncoding_(encodingRaw), verStrPos(0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | { |
| 43 | setName(""); |
| 44 | } |
| 45 | |
| 46 | ConnParams::~ConnParams() |
| 47 | { |
| 48 | delete [] name_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool 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 | |
| 67 | void 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 | |
| 75 | void 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 | |
| 83 | void ConnParams::setName(const char* name) |
| 84 | { |
| 85 | delete [] name_; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 86 | name_ = strDup(name); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 89 | void 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 | |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame^] | 104 | bool ConnParams::supportsEncoding(rdr::S32 encoding) |
| 105 | { |
| 106 | return encodings_.count(encoding) != 0; |
| 107 | } |
| 108 | |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 109 | void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 111 | useCopyRect = false; |
| 112 | supportsLocalCursor = false; |
| 113 | supportsDesktopResize = false; |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 114 | supportsExtendedDesktopSize = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | supportsLocalXCursor = false; |
| 116 | supportsLastRect = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | compressLevel = -1; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 118 | qualityLevel = -1; |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 119 | fineQualityLevel = -1; |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 120 | subsampling = subsampleUndefined; |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame^] | 121 | preferredEncoding_ = encodingRaw; |
| 122 | |
| 123 | encodings_.clear(); |
| 124 | encodings_.insert(encodingRaw); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 125 | |
| 126 | for (int i = nEncodings-1; i >= 0; i--) { |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 127 | switch (encodings[i]) { |
| 128 | case encodingCopyRect: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 129 | useCopyRect = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 130 | break; |
| 131 | case pseudoEncodingCursor: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 132 | supportsLocalCursor = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 133 | break; |
| 134 | case pseudoEncodingXCursor: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 135 | supportsLocalXCursor = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 136 | break; |
| 137 | case pseudoEncodingDesktopSize: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 138 | supportsDesktopResize = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 139 | break; |
| 140 | case pseudoEncodingExtendedDesktopSize: |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 141 | supportsExtendedDesktopSize = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 142 | break; |
| 143 | case pseudoEncodingDesktopName: |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 144 | supportsDesktopRename = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 145 | break; |
| 146 | case pseudoEncodingLastRect: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 147 | supportsLastRect = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame] | 148 | break; |
| 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 (EncodeManager::supported(encodings[i])) |
| 188 | preferredEncoding_ = encodings[i]; |
| 189 | |
| 190 | if (encodings[i] > 0) |
| 191 | encodings_.insert(encodings[i]); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 192 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 193 | } |