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

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