Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2004 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/InStream.h> |
| 20 | #include <rfb/Exception.h> |
| 21 | #include <rfb/util.h> |
| 22 | #include <rfb/CMsgHandler.h> |
| 23 | #include <rfb/CMsgReader.h> |
| 24 | |
| 25 | using namespace rfb; |
| 26 | |
| 27 | CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_) |
| 28 | : imageBufIdealSize(0), handler(handler_), is(is_), |
| 29 | imageBuf(0), imageBufSize(0) |
| 30 | { |
| 31 | for (unsigned int i = 0; i <= encodingMax; i++) { |
| 32 | decoders[i] = 0; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | CMsgReader::~CMsgReader() |
| 37 | { |
| 38 | for (unsigned int i = 0; i <= encodingMax; i++) { |
| 39 | delete decoders[i]; |
| 40 | } |
| 41 | delete [] imageBuf; |
| 42 | } |
| 43 | |
| 44 | void CMsgReader::endMsg() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void CMsgReader::readSetColourMapEntries() |
| 49 | { |
| 50 | is->skip(1); |
| 51 | int firstColour = is->readU16(); |
| 52 | int nColours = is->readU16(); |
| 53 | rdr::U16Array rgbs(nColours * 3); |
| 54 | for (int i = 0; i < nColours * 3; i++) |
| 55 | rgbs.buf[i] = is->readU16(); |
| 56 | endMsg(); |
| 57 | handler->setColourMapEntries(firstColour, nColours, rgbs.buf); |
| 58 | } |
| 59 | |
| 60 | void CMsgReader::readBell() |
| 61 | { |
| 62 | endMsg(); |
| 63 | handler->bell(); |
| 64 | } |
| 65 | |
| 66 | void CMsgReader::readServerCutText() |
| 67 | { |
| 68 | is->skip(3); |
| 69 | int len = is->readU32(); |
| 70 | if (len > 256*1024) { |
| 71 | is->skip(len); |
| 72 | fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len); |
| 73 | return; |
| 74 | } |
| 75 | CharArray ca(len+1); |
| 76 | ca.buf[len] = 0; |
| 77 | is->readBytes(ca.buf, len); |
| 78 | endMsg(); |
| 79 | handler->serverCutText(ca.buf, len); |
| 80 | } |
| 81 | |
| 82 | void CMsgReader::readFramebufferUpdateStart() |
| 83 | { |
| 84 | endMsg(); |
| 85 | handler->framebufferUpdateStart(); |
| 86 | } |
| 87 | |
| 88 | void CMsgReader::readFramebufferUpdateEnd() |
| 89 | { |
| 90 | endMsg(); |
| 91 | handler->framebufferUpdateEnd(); |
| 92 | } |
| 93 | |
| 94 | void CMsgReader::readRect(const Rect& r, unsigned int encoding) |
| 95 | { |
| 96 | if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) { |
| 97 | fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n", |
| 98 | r.width(), r.height(), r.tl.x, r.tl.y, |
| 99 | handler->cp.width, handler->cp.height); |
| 100 | throw Exception("Rect too big"); |
| 101 | } |
| 102 | |
| 103 | if (r.is_empty()) |
| 104 | fprintf(stderr, "Warning: zero size rect\n"); |
| 105 | |
| 106 | handler->beginRect(r, encoding); |
| 107 | |
| 108 | if (encoding == encodingCopyRect) { |
| 109 | readCopyRect(r); |
| 110 | } else { |
| 111 | if (!decoders[encoding]) { |
| 112 | decoders[encoding] = Decoder::createDecoder(encoding, this); |
| 113 | if (!decoders[encoding]) { |
| 114 | fprintf(stderr, "Unknown rect encoding %d\n", encoding); |
| 115 | throw Exception("Unknown rect encoding"); |
| 116 | } |
| 117 | } |
| 118 | decoders[encoding]->readRect(r, handler); |
| 119 | } |
| 120 | |
| 121 | handler->endRect(r, encoding); |
| 122 | } |
| 123 | |
| 124 | void CMsgReader::readCopyRect(const Rect& r) |
| 125 | { |
| 126 | int srcX = is->readU16(); |
| 127 | int srcY = is->readU16(); |
| 128 | handler->copyRect(r, srcX, srcY); |
| 129 | } |
| 130 | |
| 131 | void CMsgReader::readSetCursor(const Point& hotspot, const Point& size) |
| 132 | { |
| 133 | int data_len = size.x * size.y * (handler->cp.pf().bpp/8); |
| 134 | int mask_len = ((size.x+7)/8) * size.y; |
| 135 | rdr::U8Array data(data_len); |
| 136 | rdr::U8Array mask(mask_len); |
| 137 | |
| 138 | is->readBytes(data.buf, data_len); |
| 139 | is->readBytes(mask.buf, mask_len); |
| 140 | |
| 141 | handler->setCursor(hotspot, size, data.buf, mask.buf); |
| 142 | } |
| 143 | |
| 144 | rdr::U8* CMsgReader::getImageBuf(int required, int requested, int* nPixels) |
| 145 | { |
| 146 | int requiredBytes = required * (handler->cp.pf().bpp / 8); |
| 147 | int requestedBytes = requested * (handler->cp.pf().bpp / 8); |
| 148 | int size = requestedBytes; |
| 149 | if (size > imageBufIdealSize) size = imageBufIdealSize; |
| 150 | |
| 151 | if (size < requiredBytes) |
| 152 | size = requiredBytes; |
| 153 | |
| 154 | if (imageBufSize < size) { |
| 155 | imageBufSize = size; |
| 156 | delete [] imageBuf; |
| 157 | imageBuf = new rdr::U8[imageBufSize]; |
| 158 | } |
| 159 | if (nPixels) |
| 160 | *nPixels = imageBufSize / (handler->cp.pf().bpp / 8); |
| 161 | return imageBuf; |
| 162 | } |
| 163 | |
| 164 | int CMsgReader::bpp() |
| 165 | { |
| 166 | return handler->cp.pf().bpp; |
| 167 | } |