Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 2 | * Copyright 2009-2014 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | #include <stdio.h> |
| 20 | #include <rdr/OutStream.h> |
| 21 | #include <rfb/msgTypes.h> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 22 | #include <rfb/fenceTypes.h> |
Pierre Ossman | 316a324 | 2014-01-15 12:40:20 +0100 | [diff] [blame] | 23 | #include <rfb/encodings.h> |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 24 | #include <rfb/qemuTypes.h> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 25 | #include <rfb/Exception.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/PixelFormat.h> |
| 27 | #include <rfb/Rect.h> |
| 28 | #include <rfb/ConnParams.h> |
| 29 | #include <rfb/Decoder.h> |
| 30 | #include <rfb/CMsgWriter.h> |
| 31 | |
| 32 | using namespace rfb; |
| 33 | |
| 34 | CMsgWriter::CMsgWriter(ConnParams* cp_, rdr::OutStream* os_) |
| 35 | : cp(cp_), os(os_) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | CMsgWriter::~CMsgWriter() |
| 40 | { |
| 41 | } |
| 42 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 43 | void CMsgWriter::writeClientInit(bool shared) |
| 44 | { |
| 45 | os->writeU8(shared); |
| 46 | endMsg(); |
| 47 | } |
| 48 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | void CMsgWriter::writeSetPixelFormat(const PixelFormat& pf) |
| 50 | { |
| 51 | startMsg(msgTypeSetPixelFormat); |
| 52 | os->pad(3); |
| 53 | pf.write(os); |
| 54 | endMsg(); |
| 55 | } |
| 56 | |
| 57 | void CMsgWriter::writeSetEncodings(int nEncodings, rdr::U32* encodings) |
| 58 | { |
| 59 | startMsg(msgTypeSetEncodings); |
| 60 | os->skip(1); |
| 61 | os->writeU16(nEncodings); |
| 62 | for (int i = 0; i < nEncodings; i++) |
| 63 | os->writeU32(encodings[i]); |
| 64 | endMsg(); |
| 65 | } |
| 66 | |
| 67 | // Ask for encodings based on which decoders are supported. Assumes higher |
| 68 | // encoding numbers are more desirable. |
| 69 | |
| 70 | void CMsgWriter::writeSetEncodings(int preferredEncoding, bool useCopyRect) |
| 71 | { |
| 72 | int nEncodings = 0; |
| 73 | rdr::U32 encodings[encodingMax+3]; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 74 | |
Pierre Ossman | 6b68f97 | 2017-02-19 15:51:19 +0100 | [diff] [blame] | 75 | if (cp->supportsLocalCursor) { |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 76 | encodings[nEncodings++] = pseudoEncodingCursorWithAlpha; |
Pierre Ossman | a8ecb79 | 2017-08-16 16:25:24 +0200 | [diff] [blame] | 77 | encodings[nEncodings++] = pseudoEncodingCursor; |
| 78 | encodings[nEncodings++] = pseudoEncodingXCursor; |
Pierre Ossman | 6b68f97 | 2017-02-19 15:51:19 +0100 | [diff] [blame] | 79 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | if (cp->supportsDesktopResize) |
| 81 | encodings[nEncodings++] = pseudoEncodingDesktopSize; |
Pierre Ossman | 49f8822 | 2009-03-20 13:02:50 +0000 | [diff] [blame] | 82 | if (cp->supportsExtendedDesktopSize) |
| 83 | encodings[nEncodings++] = pseudoEncodingExtendedDesktopSize; |
Peter Åstrand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 84 | if (cp->supportsDesktopRename) |
| 85 | encodings[nEncodings++] = pseudoEncodingDesktopName; |
Pierre Ossman | 2fa63f8 | 2016-12-05 15:26:21 +0100 | [diff] [blame] | 86 | if (cp->supportsLEDState) |
| 87 | encodings[nEncodings++] = pseudoEncodingLEDState; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 88 | |
| 89 | encodings[nEncodings++] = pseudoEncodingLastRect; |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 90 | encodings[nEncodings++] = pseudoEncodingContinuousUpdates; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 91 | encodings[nEncodings++] = pseudoEncodingFence; |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 92 | encodings[nEncodings++] = pseudoEncodingQEMUKeyEvent; |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 93 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | if (Decoder::supported(preferredEncoding)) { |
| 95 | encodings[nEncodings++] = preferredEncoding; |
| 96 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 97 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | if (useCopyRect) { |
| 99 | encodings[nEncodings++] = encodingCopyRect; |
| 100 | } |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * Prefer encodings in this order: |
| 104 | * |
| 105 | * Tight, ZRLE, Hextile, * |
| 106 | */ |
| 107 | |
| 108 | if ((preferredEncoding != encodingTight) && |
| 109 | Decoder::supported(encodingTight)) |
| 110 | encodings[nEncodings++] = encodingTight; |
| 111 | |
| 112 | if ((preferredEncoding != encodingZRLE) && |
| 113 | Decoder::supported(encodingZRLE)) |
| 114 | encodings[nEncodings++] = encodingZRLE; |
| 115 | |
| 116 | if ((preferredEncoding != encodingHextile) && |
| 117 | Decoder::supported(encodingHextile)) |
| 118 | encodings[nEncodings++] = encodingHextile; |
| 119 | |
| 120 | // Remaining encodings |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 121 | for (int i = encodingMax; i >= 0; i--) { |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 122 | switch (i) { |
Pierre Ossman | d704e4a | 2014-01-31 11:21:51 +0100 | [diff] [blame] | 123 | case encodingCopyRect: |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 124 | case encodingTight: |
| 125 | case encodingZRLE: |
| 126 | case encodingHextile: |
Pierre Ossman | d704e4a | 2014-01-31 11:21:51 +0100 | [diff] [blame] | 127 | /* These have already been sent earlier */ |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 128 | break; |
| 129 | default: |
| 130 | if ((i != preferredEncoding) && Decoder::supported(i)) |
| 131 | encodings[nEncodings++] = i; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
Pierre Ossman | 090e7d6 | 2009-03-12 10:16:07 +0000 | [diff] [blame] | 134 | |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 135 | if (cp->compressLevel >= 0 && cp->compressLevel <= 9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 136 | encodings[nEncodings++] = pseudoEncodingCompressLevel0 + cp->compressLevel; |
Pierre Ossman | a22459d | 2014-03-17 14:42:10 +0100 | [diff] [blame] | 137 | if (cp->qualityLevel >= 0 && cp->qualityLevel <= 9) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 138 | encodings[nEncodings++] = pseudoEncodingQualityLevel0 + cp->qualityLevel; |
| 139 | |
| 140 | writeSetEncodings(nEncodings, encodings); |
| 141 | } |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 142 | |
| 143 | void CMsgWriter::writeSetDesktopSize(int width, int height, |
| 144 | const ScreenSet& layout) |
| 145 | { |
| 146 | if (!cp->supportsSetDesktopSize) |
| 147 | throw Exception("Server does not support SetDesktopSize"); |
| 148 | |
| 149 | startMsg(msgTypeSetDesktopSize); |
| 150 | os->pad(1); |
| 151 | |
| 152 | os->writeU16(width); |
| 153 | os->writeU16(height); |
| 154 | |
| 155 | os->writeU8(layout.num_screens()); |
| 156 | os->pad(1); |
| 157 | |
| 158 | ScreenSet::const_iterator iter; |
| 159 | for (iter = layout.begin();iter != layout.end();++iter) { |
| 160 | os->writeU32(iter->id); |
| 161 | os->writeU16(iter->dimensions.tl.x); |
| 162 | os->writeU16(iter->dimensions.tl.y); |
| 163 | os->writeU16(iter->dimensions.width()); |
| 164 | os->writeU16(iter->dimensions.height()); |
| 165 | os->writeU32(iter->flags); |
| 166 | } |
| 167 | |
| 168 | endMsg(); |
| 169 | } |
| 170 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 171 | void CMsgWriter::writeFramebufferUpdateRequest(const Rect& r, bool incremental) |
| 172 | { |
| 173 | startMsg(msgTypeFramebufferUpdateRequest); |
| 174 | os->writeU8(incremental); |
| 175 | os->writeU16(r.tl.x); |
| 176 | os->writeU16(r.tl.y); |
| 177 | os->writeU16(r.width()); |
| 178 | os->writeU16(r.height()); |
| 179 | endMsg(); |
| 180 | } |
| 181 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 182 | void CMsgWriter::writeEnableContinuousUpdates(bool enable, |
| 183 | int x, int y, int w, int h) |
| 184 | { |
| 185 | if (!cp->supportsContinuousUpdates) |
| 186 | throw Exception("Server does not support continuous updates"); |
| 187 | |
| 188 | startMsg(msgTypeEnableContinuousUpdates); |
| 189 | |
| 190 | os->writeU8(!!enable); |
| 191 | |
| 192 | os->writeU16(x); |
| 193 | os->writeU16(y); |
| 194 | os->writeU16(w); |
| 195 | os->writeU16(h); |
| 196 | |
| 197 | endMsg(); |
| 198 | } |
| 199 | |
| 200 | void CMsgWriter::writeFence(rdr::U32 flags, unsigned len, const char data[]) |
| 201 | { |
| 202 | if (!cp->supportsFence) |
| 203 | throw Exception("Server does not support fences"); |
| 204 | if (len > 64) |
| 205 | throw Exception("Too large fence payload"); |
| 206 | if ((flags & ~fenceFlagsSupported) != 0) |
| 207 | throw Exception("Unknown fence flags"); |
| 208 | |
| 209 | startMsg(msgTypeClientFence); |
| 210 | os->pad(3); |
| 211 | |
| 212 | os->writeU32(flags); |
| 213 | |
| 214 | os->writeU8(len); |
| 215 | os->writeBytes(data, len); |
| 216 | |
| 217 | endMsg(); |
| 218 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 219 | |
Pierre Ossman | 59da99f | 2016-02-05 10:43:12 +0100 | [diff] [blame^] | 220 | void CMsgWriter::writeKeyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 221 | { |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 222 | if (!cp->supportsQEMUKeyEvent || !keycode) { |
| 223 | /* This event isn't meaningful without a valid keysym */ |
| 224 | if (!keysym) |
| 225 | return; |
| 226 | |
| 227 | startMsg(msgTypeKeyEvent); |
| 228 | os->writeU8(down); |
| 229 | os->pad(2); |
| 230 | os->writeU32(keysym); |
| 231 | endMsg(); |
| 232 | } else { |
| 233 | startMsg(msgTypeQEMUClientMessage); |
| 234 | os->writeU8(qemuExtendedKeyEvent); |
| 235 | os->writeU16(down); |
| 236 | os->writeU32(keysym); |
| 237 | os->writeU32(keycode); |
| 238 | endMsg(); |
| 239 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
Pierre Ossman | 59da99f | 2016-02-05 10:43:12 +0100 | [diff] [blame^] | 243 | void CMsgWriter::writePointerEvent(const Point& pos, int buttonMask) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 244 | { |
| 245 | Point p(pos); |
| 246 | if (p.x < 0) p.x = 0; |
| 247 | if (p.y < 0) p.y = 0; |
| 248 | if (p.x >= cp->width) p.x = cp->width - 1; |
| 249 | if (p.y >= cp->height) p.y = cp->height - 1; |
| 250 | |
| 251 | startMsg(msgTypePointerEvent); |
| 252 | os->writeU8(buttonMask); |
| 253 | os->writeU16(p.x); |
| 254 | os->writeU16(p.y); |
| 255 | endMsg(); |
| 256 | } |
| 257 | |
| 258 | |
Pierre Ossman | 59da99f | 2016-02-05 10:43:12 +0100 | [diff] [blame^] | 259 | void CMsgWriter::writeClientCutText(const char* str, rdr::U32 len) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 260 | { |
| 261 | startMsg(msgTypeClientCutText); |
| 262 | os->pad(3); |
| 263 | os->writeU32(len); |
| 264 | os->writeBytes(str, len); |
| 265 | endMsg(); |
| 266 | } |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 267 | |
| 268 | void CMsgWriter::startMsg(int type) |
| 269 | { |
| 270 | os->writeU8(type); |
| 271 | } |
| 272 | |
| 273 | void CMsgWriter::endMsg() |
| 274 | { |
| 275 | os->flush(); |
| 276 | } |