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> |
| 25 | #include <rfb/Encoder.h> |
| 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 | 701ad68 | 2011-11-20 15:39:17 +0000 | [diff] [blame] | 39 | customCompressLevel(false), compressLevel(2), |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 40 | noJpeg(false), qualityLevel(-1), fineQualityLevel(-1), |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 41 | subsampling(subsampleUndefined), name_(0), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | currentEncoding_(encodingRaw), verStrPos(0) |
| 43 | { |
| 44 | setName(""); |
| 45 | } |
| 46 | |
| 47 | ConnParams::~ConnParams() |
| 48 | { |
| 49 | delete [] name_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool ConnParams::readVersion(rdr::InStream* is, bool* done) |
| 53 | { |
| 54 | if (verStrPos >= 12) return false; |
| 55 | while (is->checkNoWait(1) && verStrPos < 12) { |
| 56 | verStr[verStrPos++] = is->readU8(); |
| 57 | } |
| 58 | |
| 59 | if (verStrPos < 12) { |
| 60 | *done = false; |
| 61 | return true; |
| 62 | } |
| 63 | *done = true; |
| 64 | verStr[12] = 0; |
| 65 | return (sscanf(verStr, "RFB %03d.%03d\n", &majorVersion,&minorVersion) == 2); |
| 66 | } |
| 67 | |
| 68 | void ConnParams::writeVersion(rdr::OutStream* os) |
| 69 | { |
| 70 | char str[13]; |
| 71 | sprintf(str, "RFB %03d.%03d\n", majorVersion, minorVersion); |
| 72 | os->writeBytes(str, 12); |
| 73 | os->flush(); |
| 74 | } |
| 75 | |
| 76 | void ConnParams::setPF(const PixelFormat& pf) |
| 77 | { |
| 78 | pf_ = pf; |
| 79 | |
| 80 | if (pf.bpp != 8 && pf.bpp != 16 && pf.bpp != 32) |
| 81 | throw Exception("setPF: not 8, 16 or 32 bpp?"); |
| 82 | } |
| 83 | |
| 84 | void ConnParams::setName(const char* name) |
| 85 | { |
| 86 | delete [] name_; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 87 | name_ = strDup(name); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 90 | void ConnParams::setEncodings(int nEncodings, const rdr::S32* encodings) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 91 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 92 | useCopyRect = false; |
| 93 | supportsLocalCursor = false; |
| 94 | supportsDesktopResize = false; |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 95 | supportsExtendedDesktopSize = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | supportsLocalXCursor = false; |
| 97 | supportsLastRect = false; |
| 98 | customCompressLevel = false; |
| 99 | compressLevel = -1; |
| 100 | noJpeg = true; |
| 101 | qualityLevel = -1; |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 102 | fineQualityLevel = -1; |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 103 | subsampling = subsampleUndefined; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | currentEncoding_ = encodingRaw; |
| 105 | |
| 106 | for (int i = nEncodings-1; i >= 0; i--) { |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 107 | switch (encodings[i]) { |
| 108 | case encodingCopyRect: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 109 | useCopyRect = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 110 | break; |
| 111 | case pseudoEncodingCursor: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | supportsLocalCursor = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 113 | break; |
| 114 | case pseudoEncodingXCursor: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | supportsLocalXCursor = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 116 | break; |
| 117 | case pseudoEncodingDesktopSize: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 118 | supportsDesktopResize = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 119 | break; |
| 120 | case pseudoEncodingExtendedDesktopSize: |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 121 | supportsExtendedDesktopSize = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 122 | break; |
| 123 | case pseudoEncodingDesktopName: |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 124 | supportsDesktopRename = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 125 | break; |
| 126 | case pseudoEncodingLastRect: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 127 | supportsLastRect = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 128 | break; |
| 129 | case pseudoEncodingFence: |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 130 | supportsFence = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 131 | break; |
| 132 | case pseudoEncodingContinuousUpdates: |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 133 | supportsContinuousUpdates = true; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 134 | break; |
| 135 | case pseudoEncodingSubsamp1X: |
| 136 | subsampling = subsampleNone; |
| 137 | break; |
| 138 | case pseudoEncodingSubsampGray: |
| 139 | subsampling = subsampleGray; |
| 140 | break; |
| 141 | case pseudoEncodingSubsamp2X: |
| 142 | subsampling = subsample2X; |
| 143 | break; |
| 144 | case pseudoEncodingSubsamp4X: |
| 145 | subsampling = subsample4X; |
| 146 | break; |
| 147 | case pseudoEncodingSubsamp8X: |
| 148 | subsampling = subsample8X; |
| 149 | break; |
| 150 | case pseudoEncodingSubsamp16X: |
| 151 | subsampling = subsample16X; |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | if (encodings[i] >= pseudoEncodingCompressLevel0 && |
| 156 | encodings[i] <= pseudoEncodingCompressLevel9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 157 | compressLevel = encodings[i] - pseudoEncodingCompressLevel0; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 158 | |
| 159 | if (encodings[i] >= pseudoEncodingQualityLevel0 && |
| 160 | encodings[i] <= pseudoEncodingQualityLevel9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 161 | qualityLevel = encodings[i] - pseudoEncodingQualityLevel0; |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 162 | |
| 163 | if (encodings[i] >= pseudoEncodingFineQualityLevel0 && |
| 164 | encodings[i] <= pseudoEncodingFineQualityLevel100) |
| 165 | fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0; |
| 166 | |
| 167 | if (Encoder::supported(encodings[i])) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 168 | currentEncoding_ = encodings[i]; |
| 169 | } |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 170 | |
Pierre Ossman | 6bcf137 | 2014-01-15 13:44:04 +0100 | [diff] [blame^] | 171 | if (compressLevel != -1) |
| 172 | customCompressLevel = true; |
| 173 | if ((qualityLevel != -1) || (fineQualityLevel != -1) || |
| 174 | (subsampling != subsampleUndefined)) |
| 175 | noJpeg = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | } |