Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | #include <stdio.h> |
| 19 | #include <rdr/OutStream.h> |
| 20 | #include <rfb/msgTypes.h> |
Pierre Ossman | 316a324 | 2014-01-15 12:40:20 +0100 | [diff] [blame] | 21 | #include <rfb/encodings.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 22 | #include <rfb/PixelFormat.h> |
| 23 | #include <rfb/Rect.h> |
| 24 | #include <rfb/ConnParams.h> |
| 25 | #include <rfb/Decoder.h> |
| 26 | #include <rfb/CMsgWriter.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | |
| 30 | CMsgWriter::CMsgWriter(ConnParams* cp_, rdr::OutStream* os_) |
| 31 | : cp(cp_), os(os_) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | CMsgWriter::~CMsgWriter() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void CMsgWriter::writeSetPixelFormat(const PixelFormat& pf) |
| 40 | { |
| 41 | startMsg(msgTypeSetPixelFormat); |
| 42 | os->pad(3); |
| 43 | pf.write(os); |
| 44 | endMsg(); |
| 45 | } |
| 46 | |
| 47 | void CMsgWriter::writeSetEncodings(int nEncodings, rdr::U32* encodings) |
| 48 | { |
| 49 | startMsg(msgTypeSetEncodings); |
| 50 | os->skip(1); |
| 51 | os->writeU16(nEncodings); |
| 52 | for (int i = 0; i < nEncodings; i++) |
| 53 | os->writeU32(encodings[i]); |
| 54 | endMsg(); |
| 55 | } |
| 56 | |
| 57 | // Ask for encodings based on which decoders are supported. Assumes higher |
| 58 | // encoding numbers are more desirable. |
| 59 | |
| 60 | void CMsgWriter::writeSetEncodings(int preferredEncoding, bool useCopyRect) |
| 61 | { |
| 62 | int nEncodings = 0; |
| 63 | rdr::U32 encodings[encodingMax+3]; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 64 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | if (cp->supportsLocalCursor) |
| 66 | encodings[nEncodings++] = pseudoEncodingCursor; |
| 67 | if (cp->supportsDesktopResize) |
| 68 | encodings[nEncodings++] = pseudoEncodingDesktopSize; |
Pierre Ossman | 49f8822 | 2009-03-20 13:02:50 +0000 | [diff] [blame] | 69 | if (cp->supportsExtendedDesktopSize) |
| 70 | encodings[nEncodings++] = pseudoEncodingExtendedDesktopSize; |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 71 | if (cp->supportsDesktopRename) |
| 72 | encodings[nEncodings++] = pseudoEncodingDesktopName; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 73 | |
| 74 | encodings[nEncodings++] = pseudoEncodingLastRect; |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 75 | encodings[nEncodings++] = pseudoEncodingContinuousUpdates; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 76 | encodings[nEncodings++] = pseudoEncodingFence; |
| 77 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 78 | if (Decoder::supported(preferredEncoding)) { |
| 79 | encodings[nEncodings++] = preferredEncoding; |
| 80 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 81 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | if (useCopyRect) { |
| 83 | encodings[nEncodings++] = encodingCopyRect; |
| 84 | } |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * Prefer encodings in this order: |
| 88 | * |
| 89 | * Tight, ZRLE, Hextile, * |
| 90 | */ |
| 91 | |
| 92 | if ((preferredEncoding != encodingTight) && |
| 93 | Decoder::supported(encodingTight)) |
| 94 | encodings[nEncodings++] = encodingTight; |
| 95 | |
| 96 | if ((preferredEncoding != encodingZRLE) && |
| 97 | Decoder::supported(encodingZRLE)) |
| 98 | encodings[nEncodings++] = encodingZRLE; |
| 99 | |
| 100 | if ((preferredEncoding != encodingHextile) && |
| 101 | Decoder::supported(encodingHextile)) |
| 102 | encodings[nEncodings++] = encodingHextile; |
| 103 | |
| 104 | // Remaining encodings |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 105 | for (int i = encodingMax; i >= 0; i--) { |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 106 | switch (i) { |
| 107 | case encodingTight: |
| 108 | case encodingZRLE: |
| 109 | case encodingHextile: |
| 110 | break; |
| 111 | default: |
| 112 | if ((i != preferredEncoding) && Decoder::supported(i)) |
| 113 | encodings[nEncodings++] = i; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 116 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | if (cp->customCompressLevel && cp->compressLevel >= 0 && cp->compressLevel <= 9) |
| 118 | encodings[nEncodings++] = pseudoEncodingCompressLevel0 + cp->compressLevel; |
Pierre Ossman | 3d2ccc4 | 2009-03-11 14:33:49 +0000 | [diff] [blame] | 119 | if (!cp->noJpeg && cp->qualityLevel >= 0 && cp->qualityLevel <= 9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 120 | encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel; |
| 121 | |
| 122 | writeSetEncodings(nEncodings, encodings); |
| 123 | } |
| 124 | |
| 125 | void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental) |
| 126 | { |
| 127 | startMsg(msgTypeFramebufferUpdateRequest); |
| 128 | os->writeU8(incremental); |
| 129 | os->writeU16(r.tl.x); |
| 130 | os->writeU16(r.tl.y); |
| 131 | os->writeU16(r.width()); |
| 132 | os->writeU16(r.height()); |
| 133 | endMsg(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void CMsgWriter::keyEvent(rdr::U32 key, bool down) |
| 138 | { |
| 139 | startMsg(msgTypeKeyEvent); |
| 140 | os->writeU8(down); |
| 141 | os->pad(2); |
| 142 | os->writeU32(key); |
| 143 | endMsg(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void CMsgWriter::pointerEvent(const Point& pos, int buttonMask) |
| 148 | { |
| 149 | Point p(pos); |
| 150 | if (p.x < 0) p.x = 0; |
| 151 | if (p.y < 0) p.y = 0; |
| 152 | if (p.x >= cp->width) p.x = cp->width - 1; |
| 153 | if (p.y >= cp->height) p.y = cp->height - 1; |
| 154 | |
| 155 | startMsg(msgTypePointerEvent); |
| 156 | os->writeU8(buttonMask); |
| 157 | os->writeU16(p.x); |
| 158 | os->writeU16(p.y); |
| 159 | endMsg(); |
| 160 | } |
| 161 | |
| 162 | |
Adam Tkac | acf6c6b | 2009-02-13 12:42:05 +0000 | [diff] [blame] | 163 | void CMsgWriter::clientCutText(const char* str, rdr::U32 len) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 164 | { |
| 165 | startMsg(msgTypeClientCutText); |
| 166 | os->pad(3); |
| 167 | os->writeU32(len); |
| 168 | os->writeBytes(str, len); |
| 169 | endMsg(); |
| 170 | } |