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> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 20 | #include <rfb/msgTypes.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 21 | #include <rdr/InStream.h> |
| 22 | #include <rfb/Exception.h> |
| 23 | #include <rfb/util.h> |
| 24 | #include <rfb/CMsgHandler.h> |
| 25 | #include <rfb/CMsgReader.h> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 26 | #include <rfb/Decoder.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace rfb; |
| 29 | |
| 30 | CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_) |
| 31 | : imageBufIdealSize(0), handler(handler_), is(is_), |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 32 | imageBuf(0), imageBufSize(0), nUpdateRectsLeft(0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 33 | { |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 34 | for (int i = 0; i <= encodingMax; i++) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | decoders[i] = 0; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | CMsgReader::~CMsgReader() |
| 40 | { |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 41 | for (int i = 0; i <= encodingMax; i++) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | delete decoders[i]; |
| 43 | } |
| 44 | delete [] imageBuf; |
| 45 | } |
| 46 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 47 | void CMsgReader::readServerInit() |
| 48 | { |
| 49 | int width = is->readU16(); |
| 50 | int height = is->readU16(); |
| 51 | handler->setDesktopSize(width, height); |
| 52 | PixelFormat pf; |
| 53 | pf.read(is); |
| 54 | handler->setPixelFormat(pf); |
| 55 | CharArray name(is->readString()); |
| 56 | handler->setName(name.buf); |
| 57 | handler->serverInit(); |
| 58 | } |
| 59 | |
| 60 | void CMsgReader::readMsg() |
| 61 | { |
| 62 | if (nUpdateRectsLeft == 0) { |
| 63 | int type = is->readU8(); |
| 64 | |
| 65 | switch (type) { |
| 66 | case msgTypeSetColourMapEntries: |
| 67 | readSetColourMapEntries(); |
| 68 | break; |
| 69 | case msgTypeBell: |
| 70 | readBell(); |
| 71 | break; |
| 72 | case msgTypeServerCutText: |
| 73 | readServerCutText(); |
| 74 | break; |
| 75 | case msgTypeFramebufferUpdate: |
| 76 | readFramebufferUpdate(); |
| 77 | break; |
| 78 | case msgTypeServerFence: |
| 79 | readFence(); |
| 80 | break; |
| 81 | case msgTypeEndOfContinuousUpdates: |
| 82 | readEndOfContinuousUpdates(); |
| 83 | break; |
| 84 | default: |
| 85 | fprintf(stderr, "unknown message type %d\n", type); |
| 86 | throw Exception("unknown message type"); |
| 87 | } |
| 88 | } else { |
| 89 | int x = is->readU16(); |
| 90 | int y = is->readU16(); |
| 91 | int w = is->readU16(); |
| 92 | int h = is->readU16(); |
| 93 | int encoding = is->readS32(); |
| 94 | |
| 95 | switch (encoding) { |
| 96 | case pseudoEncodingLastRect: |
| 97 | nUpdateRectsLeft = 1; // this rectangle is the last one |
| 98 | break; |
| 99 | case pseudoEncodingCursor: |
| 100 | readSetCursor(w, h, Point(x,y)); |
| 101 | break; |
| 102 | case pseudoEncodingDesktopName: |
| 103 | readSetDesktopName(x, y, w, h); |
| 104 | break; |
| 105 | case pseudoEncodingDesktopSize: |
| 106 | handler->setDesktopSize(w, h); |
| 107 | break; |
| 108 | case pseudoEncodingExtendedDesktopSize: |
| 109 | readExtendedDesktopSize(x, y, w, h); |
| 110 | break; |
| 111 | default: |
| 112 | readRect(Rect(x, y, x+w, y+h), encoding); |
| 113 | break; |
| 114 | }; |
| 115 | |
| 116 | nUpdateRectsLeft--; |
| 117 | if (nUpdateRectsLeft == 0) |
| 118 | handler->framebufferUpdateEnd(); |
| 119 | } |
| 120 | } |
| 121 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 122 | void CMsgReader::readSetColourMapEntries() |
| 123 | { |
| 124 | is->skip(1); |
| 125 | int firstColour = is->readU16(); |
| 126 | int nColours = is->readU16(); |
| 127 | rdr::U16Array rgbs(nColours * 3); |
| 128 | for (int i = 0; i < nColours * 3; i++) |
| 129 | rgbs.buf[i] = is->readU16(); |
| 130 | handler->setColourMapEntries(firstColour, nColours, rgbs.buf); |
| 131 | } |
| 132 | |
| 133 | void CMsgReader::readBell() |
| 134 | { |
| 135 | handler->bell(); |
| 136 | } |
| 137 | |
| 138 | void CMsgReader::readServerCutText() |
| 139 | { |
| 140 | is->skip(3); |
Adam Tkac | acf6c6b | 2009-02-13 12:42:05 +0000 | [diff] [blame] | 141 | rdr::U32 len = is->readU32(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 142 | if (len > 256*1024) { |
| 143 | is->skip(len); |
| 144 | fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len); |
| 145 | return; |
| 146 | } |
| 147 | CharArray ca(len+1); |
| 148 | ca.buf[len] = 0; |
| 149 | is->readBytes(ca.buf, len); |
| 150 | handler->serverCutText(ca.buf, len); |
| 151 | } |
| 152 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 153 | void CMsgReader::readFence() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 154 | { |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 155 | rdr::U32 flags; |
| 156 | rdr::U8 len; |
| 157 | char data[64]; |
| 158 | |
| 159 | is->skip(3); |
| 160 | |
| 161 | flags = is->readU32(); |
| 162 | |
| 163 | len = is->readU8(); |
| 164 | if (len > sizeof(data)) { |
| 165 | fprintf(stderr, "Ignoring fence with too large payload\n"); |
| 166 | is->skip(len); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | is->readBytes(data, len); |
| 171 | |
| 172 | handler->fence(flags, len, data); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 175 | void CMsgReader::readEndOfContinuousUpdates() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | { |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 177 | handler->endOfContinuousUpdates(); |
| 178 | } |
| 179 | |
| 180 | void CMsgReader::readFramebufferUpdate() |
| 181 | { |
| 182 | is->skip(1); |
| 183 | nUpdateRectsLeft = is->readU16(); |
| 184 | handler->framebufferUpdateStart(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Peter Åstrand | 98fe98c | 2010-02-10 07:43:02 +0000 | [diff] [blame] | 187 | void CMsgReader::readRect(const Rect& r, int encoding) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 188 | { |
| 189 | if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) { |
| 190 | fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n", |
| 191 | r.width(), r.height(), r.tl.x, r.tl.y, |
| 192 | handler->cp.width, handler->cp.height); |
| 193 | throw Exception("Rect too big"); |
| 194 | } |
| 195 | |
| 196 | if (r.is_empty()) |
| 197 | fprintf(stderr, "Warning: zero size rect\n"); |
| 198 | |
| 199 | handler->beginRect(r, encoding); |
| 200 | |
| 201 | if (encoding == encodingCopyRect) { |
| 202 | readCopyRect(r); |
| 203 | } else { |
Adam Tkac | 5eeaa3b | 2008-11-18 16:00:14 +0000 | [diff] [blame] | 204 | |
Pierre Ossman | 2292296 | 2012-07-19 11:06:27 +0000 | [diff] [blame] | 205 | if (!Decoder::supported(encoding)) { |
Adam Tkac | 5eeaa3b | 2008-11-18 16:00:14 +0000 | [diff] [blame] | 206 | fprintf(stderr, "Unknown rect encoding %d\n", encoding); |
| 207 | throw Exception("Unknown rect encoding"); |
| 208 | } |
| 209 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 210 | if (!decoders[encoding]) { |
| 211 | decoders[encoding] = Decoder::createDecoder(encoding, this); |
| 212 | if (!decoders[encoding]) { |
| 213 | fprintf(stderr, "Unknown rect encoding %d\n", encoding); |
| 214 | throw Exception("Unknown rect encoding"); |
| 215 | } |
| 216 | } |
| 217 | decoders[encoding]->readRect(r, handler); |
| 218 | } |
| 219 | |
| 220 | handler->endRect(r, encoding); |
| 221 | } |
| 222 | |
| 223 | void CMsgReader::readCopyRect(const Rect& r) |
| 224 | { |
| 225 | int srcX = is->readU16(); |
| 226 | int srcY = is->readU16(); |
| 227 | handler->copyRect(r, srcX, srcY); |
| 228 | } |
| 229 | |
| 230 | void CMsgReader::readSetCursor(int width, int height, const Point& hotspot) |
| 231 | { |
| 232 | int data_len = width * height * (handler->cp.pf().bpp/8); |
| 233 | int mask_len = ((width+7)/8) * height; |
| 234 | rdr::U8Array data(data_len); |
| 235 | rdr::U8Array mask(mask_len); |
| 236 | |
| 237 | is->readBytes(data.buf, data_len); |
| 238 | is->readBytes(mask.buf, mask_len); |
| 239 | |
| 240 | handler->setCursor(width, height, hotspot, data.buf, mask.buf); |
| 241 | } |
| 242 | |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame^] | 243 | void CMsgReader::readSetDesktopName(int x, int y, int w, int h) |
| 244 | { |
| 245 | char* name = is->readString(); |
| 246 | |
| 247 | if (x || y || w || h) { |
| 248 | fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n"); |
| 249 | } else { |
| 250 | handler->setName(name); |
| 251 | } |
| 252 | |
| 253 | delete [] name; |
| 254 | } |
| 255 | |
| 256 | void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h) |
| 257 | { |
| 258 | unsigned int screens, i; |
| 259 | rdr::U32 id, flags; |
| 260 | int sx, sy, sw, sh; |
| 261 | ScreenSet layout; |
| 262 | |
| 263 | screens = is->readU8(); |
| 264 | is->skip(3); |
| 265 | |
| 266 | for (i = 0;i < screens;i++) { |
| 267 | id = is->readU32(); |
| 268 | sx = is->readU16(); |
| 269 | sy = is->readU16(); |
| 270 | sw = is->readU16(); |
| 271 | sh = is->readU16(); |
| 272 | flags = is->readU32(); |
| 273 | |
| 274 | layout.add_screen(Screen(id, sx, sy, sw, sh, flags)); |
| 275 | } |
| 276 | |
| 277 | handler->setExtendedDesktopSize(x, y, w, h, layout); |
| 278 | } |
| 279 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 280 | rdr::U8* CMsgReader::getImageBuf(int required, int requested, int* nPixels) |
| 281 | { |
| 282 | int requiredBytes = required * (handler->cp.pf().bpp / 8); |
| 283 | int requestedBytes = requested * (handler->cp.pf().bpp / 8); |
| 284 | int size = requestedBytes; |
| 285 | if (size > imageBufIdealSize) size = imageBufIdealSize; |
| 286 | |
| 287 | if (size < requiredBytes) |
| 288 | size = requiredBytes; |
| 289 | |
| 290 | if (imageBufSize < size) { |
| 291 | imageBufSize = size; |
| 292 | delete [] imageBuf; |
| 293 | imageBuf = new rdr::U8[imageBufSize]; |
| 294 | } |
| 295 | if (nPixels) |
| 296 | *nPixels = imageBufSize / (handler->cp.pf().bpp / 8); |
| 297 | return imageBuf; |
| 298 | } |
| 299 | |
| 300 | int CMsgReader::bpp() |
| 301 | { |
| 302 | return handler->cp.pf().bpp; |
| 303 | } |