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--) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | if (encodings[i] == encodingCopyRect) |
| 108 | useCopyRect = true; |
| 109 | else if (encodings[i] == pseudoEncodingCursor) |
| 110 | supportsLocalCursor = true; |
| 111 | else if (encodings[i] == pseudoEncodingXCursor) |
| 112 | supportsLocalXCursor = true; |
| 113 | else if (encodings[i] == pseudoEncodingDesktopSize) |
| 114 | supportsDesktopResize = true; |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 115 | else if (encodings[i] == pseudoEncodingExtendedDesktopSize) |
| 116 | supportsExtendedDesktopSize = true; |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 117 | else if (encodings[i] == pseudoEncodingDesktopName) |
| 118 | supportsDesktopRename = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | else if (encodings[i] == pseudoEncodingLastRect) |
| 120 | supportsLastRect = true; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 121 | else if (encodings[i] == pseudoEncodingFence) |
| 122 | supportsFence = true; |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 123 | else if (encodings[i] == pseudoEncodingContinuousUpdates) |
| 124 | supportsContinuousUpdates = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 125 | else if (encodings[i] >= pseudoEncodingCompressLevel0 && |
| 126 | encodings[i] <= pseudoEncodingCompressLevel9) { |
| 127 | customCompressLevel = true; |
| 128 | compressLevel = encodings[i] - pseudoEncodingCompressLevel0; |
| 129 | } else if (encodings[i] >= pseudoEncodingQualityLevel0 && |
| 130 | encodings[i] <= pseudoEncodingQualityLevel9) { |
| 131 | noJpeg = false; |
| 132 | qualityLevel = encodings[i] - pseudoEncodingQualityLevel0; |
Adam Tkac | ea633a7 | 2010-07-21 14:12:18 +0000 | [diff] [blame] | 133 | } else if (Encoder::supported(encodings[i])) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 134 | currentEncoding_ = encodings[i]; |
| 135 | } |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 136 | |
| 137 | // If the TurboVNC fine quality/subsampling encodings exist, let them |
| 138 | // override the coarse TightVNC quality level |
| 139 | for (int i = nEncodings-1; i >= 0; i--) { |
| 140 | if (encodings[i] >= pseudoEncodingFineQualityLevel0 + 1 && |
| 141 | encodings[i] <= pseudoEncodingFineQualityLevel100) { |
| 142 | noJpeg = false; |
| 143 | fineQualityLevel = encodings[i] - pseudoEncodingFineQualityLevel0; |
| 144 | } else if (encodings[i] >= pseudoEncodingSubsamp1X && |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame^] | 145 | encodings[i] <= pseudoEncodingSubsamp16X) { |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 146 | noJpeg = false; |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame^] | 147 | switch (encodings[i]) { |
| 148 | case pseudoEncodingSubsamp1X: |
| 149 | subsampling = subsampleNone; |
| 150 | break; |
| 151 | case pseudoEncodingSubsampGray: |
| 152 | subsampling = subsampleGray; |
| 153 | break; |
| 154 | case pseudoEncodingSubsamp2X: |
| 155 | subsampling = subsample2X; |
| 156 | break; |
| 157 | case pseudoEncodingSubsamp4X: |
| 158 | subsampling = subsample4X; |
| 159 | break; |
| 160 | case pseudoEncodingSubsamp8X: |
| 161 | subsampling = subsample8X; |
| 162 | break; |
| 163 | case pseudoEncodingSubsamp16X: |
| 164 | subsampling = subsample16X; |
| 165 | break; |
| 166 | } |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | } |