You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMsgReader.cxx 4.2KB

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