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> |
| 21 | #include <rfb/PixelFormat.h> |
| 22 | #include <rfb/Rect.h> |
| 23 | #include <rfb/ConnParams.h> |
| 24 | #include <rfb/Decoder.h> |
| 25 | #include <rfb/CMsgWriter.h> |
| 26 | |
| 27 | using namespace rfb; |
| 28 | |
| 29 | CMsgWriter::CMsgWriter(ConnParams* cp_, rdr::OutStream* os_) |
| 30 | : cp(cp_), os(os_) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | CMsgWriter::~CMsgWriter() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void CMsgWriter::writeSetPixelFormat(const PixelFormat& pf) |
| 39 | { |
| 40 | startMsg(msgTypeSetPixelFormat); |
| 41 | os->pad(3); |
| 42 | pf.write(os); |
| 43 | endMsg(); |
| 44 | } |
| 45 | |
| 46 | void CMsgWriter::writeSetEncodings(int nEncodings, rdr::U32* encodings) |
| 47 | { |
| 48 | startMsg(msgTypeSetEncodings); |
| 49 | os->skip(1); |
| 50 | os->writeU16(nEncodings); |
| 51 | for (int i = 0; i < nEncodings; i++) |
| 52 | os->writeU32(encodings[i]); |
| 53 | endMsg(); |
| 54 | } |
| 55 | |
| 56 | // Ask for encodings based on which decoders are supported. Assumes higher |
| 57 | // encoding numbers are more desirable. |
| 58 | |
| 59 | void CMsgWriter::writeSetEncodings(int preferredEncoding, bool useCopyRect) |
| 60 | { |
| 61 | int nEncodings = 0; |
| 62 | rdr::U32 encodings[encodingMax+3]; |
| 63 | if (cp->supportsLocalCursor) |
| 64 | encodings[nEncodings++] = pseudoEncodingCursor; |
| 65 | if (cp->supportsDesktopResize) |
| 66 | encodings[nEncodings++] = pseudoEncodingDesktopSize; |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 67 | if (cp->supportsDesktopRename) |
| 68 | encodings[nEncodings++] = pseudoEncodingDesktopName; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | if (Decoder::supported(preferredEncoding)) { |
| 70 | encodings[nEncodings++] = preferredEncoding; |
| 71 | } |
| 72 | if (useCopyRect) { |
| 73 | encodings[nEncodings++] = encodingCopyRect; |
| 74 | } |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame^] | 75 | |
| 76 | /* |
| 77 | * Prefer encodings in this order: |
| 78 | * |
| 79 | * Tight, ZRLE, Hextile, * |
| 80 | */ |
| 81 | |
| 82 | if ((preferredEncoding != encodingTight) && |
| 83 | Decoder::supported(encodingTight)) |
| 84 | encodings[nEncodings++] = encodingTight; |
| 85 | |
| 86 | if ((preferredEncoding != encodingZRLE) && |
| 87 | Decoder::supported(encodingZRLE)) |
| 88 | encodings[nEncodings++] = encodingZRLE; |
| 89 | |
| 90 | if ((preferredEncoding != encodingHextile) && |
| 91 | Decoder::supported(encodingHextile)) |
| 92 | encodings[nEncodings++] = encodingHextile; |
| 93 | |
| 94 | // Remaining encodings |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 95 | for (int i = encodingMax; i >= 0; i--) { |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame^] | 96 | switch (i) { |
| 97 | case encodingTight: |
| 98 | case encodingZRLE: |
| 99 | case encodingHextile: |
| 100 | break; |
| 101 | default: |
| 102 | if ((i != preferredEncoding) && Decoder::supported(i)) |
| 103 | encodings[nEncodings++] = i; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame^] | 106 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | encodings[nEncodings++] = pseudoEncodingLastRect; |
| 108 | if (cp->customCompressLevel && cp->compressLevel >= 0 && cp->compressLevel <= 9) |
| 109 | encodings[nEncodings++] = pseudoEncodingCompressLevel0 + cp->compressLevel; |
Pierre Ossman | 3d2ccc4 | 2009-03-11 14:33:49 +0000 | [diff] [blame] | 110 | if (!cp->noJpeg && cp->qualityLevel >= 0 && cp->qualityLevel <= 9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 111 | encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel; |
| 112 | |
| 113 | writeSetEncodings(nEncodings, encodings); |
| 114 | } |
| 115 | |
| 116 | void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental) |
| 117 | { |
| 118 | startMsg(msgTypeFramebufferUpdateRequest); |
| 119 | os->writeU8(incremental); |
| 120 | os->writeU16(r.tl.x); |
| 121 | os->writeU16(r.tl.y); |
| 122 | os->writeU16(r.width()); |
| 123 | os->writeU16(r.height()); |
| 124 | endMsg(); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void CMsgWriter::keyEvent(rdr::U32 key, bool down) |
| 129 | { |
| 130 | startMsg(msgTypeKeyEvent); |
| 131 | os->writeU8(down); |
| 132 | os->pad(2); |
| 133 | os->writeU32(key); |
| 134 | endMsg(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void CMsgWriter::pointerEvent(const Point& pos, int buttonMask) |
| 139 | { |
| 140 | Point p(pos); |
| 141 | if (p.x < 0) p.x = 0; |
| 142 | if (p.y < 0) p.y = 0; |
| 143 | if (p.x >= cp->width) p.x = cp->width - 1; |
| 144 | if (p.y >= cp->height) p.y = cp->height - 1; |
| 145 | |
| 146 | startMsg(msgTypePointerEvent); |
| 147 | os->writeU8(buttonMask); |
| 148 | os->writeU16(p.x); |
| 149 | os->writeU16(p.y); |
| 150 | endMsg(); |
| 151 | } |
| 152 | |
| 153 | |
Adam Tkac | acf6c6b | 2009-02-13 12:42:05 +0000 | [diff] [blame] | 154 | void CMsgWriter::clientCutText(const char* str, rdr::U32 len) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 155 | { |
| 156 | startMsg(msgTypeClientCutText); |
| 157 | os->pad(3); |
| 158 | os->writeU32(len); |
| 159 | os->writeBytes(str, len); |
| 160 | endMsg(); |
| 161 | } |