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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009-2017 Pierre Ossman for Cendio AB
  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 <assert.h>
  20. #include <stdio.h>
  21. #include <rfb/msgTypes.h>
  22. #include <rdr/InStream.h>
  23. #include <rfb/Exception.h>
  24. #include <rfb/util.h>
  25. #include <rfb/CMsgHandler.h>
  26. #include <rfb/CMsgReader.h>
  27. using namespace rfb;
  28. CMsgReader::CMsgReader(CMsgHandler* handler_, rdr::InStream* is_)
  29. : imageBufIdealSize(0), handler(handler_), is(is_),
  30. nUpdateRectsLeft(0)
  31. {
  32. }
  33. CMsgReader::~CMsgReader()
  34. {
  35. }
  36. void CMsgReader::readServerInit()
  37. {
  38. int width = is->readU16();
  39. int height = is->readU16();
  40. handler->setDesktopSize(width, height);
  41. PixelFormat pf;
  42. pf.read(is);
  43. handler->setPixelFormat(pf);
  44. CharArray name(is->readString());
  45. handler->setName(name.buf);
  46. handler->serverInit();
  47. }
  48. void CMsgReader::readMsg()
  49. {
  50. if (nUpdateRectsLeft == 0) {
  51. int type = is->readU8();
  52. switch (type) {
  53. case msgTypeSetColourMapEntries:
  54. readSetColourMapEntries();
  55. break;
  56. case msgTypeBell:
  57. readBell();
  58. break;
  59. case msgTypeServerCutText:
  60. readServerCutText();
  61. break;
  62. case msgTypeFramebufferUpdate:
  63. readFramebufferUpdate();
  64. break;
  65. case msgTypeServerFence:
  66. readFence();
  67. break;
  68. case msgTypeEndOfContinuousUpdates:
  69. readEndOfContinuousUpdates();
  70. break;
  71. default:
  72. fprintf(stderr, "unknown message type %d\n", type);
  73. throw Exception("unknown message type");
  74. }
  75. } else {
  76. int x = is->readU16();
  77. int y = is->readU16();
  78. int w = is->readU16();
  79. int h = is->readU16();
  80. int encoding = is->readS32();
  81. switch (encoding) {
  82. case pseudoEncodingLastRect:
  83. nUpdateRectsLeft = 1; // this rectangle is the last one
  84. break;
  85. case pseudoEncodingXCursor:
  86. readSetXCursor(w, h, Point(x,y));
  87. break;
  88. case pseudoEncodingCursor:
  89. readSetCursor(w, h, Point(x,y));
  90. break;
  91. case pseudoEncodingCursorWithAlpha:
  92. readSetCursorWithAlpha(w, h, Point(x,y));
  93. break;
  94. case pseudoEncodingDesktopName:
  95. readSetDesktopName(x, y, w, h);
  96. break;
  97. case pseudoEncodingDesktopSize:
  98. handler->setDesktopSize(w, h);
  99. break;
  100. case pseudoEncodingExtendedDesktopSize:
  101. readExtendedDesktopSize(x, y, w, h);
  102. break;
  103. case pseudoEncodingLEDState:
  104. readLEDState();
  105. case pseudoEncodingQEMUKeyEvent:
  106. handler->supportsQEMUKeyEvent();
  107. break;
  108. default:
  109. readRect(Rect(x, y, x+w, y+h), encoding);
  110. break;
  111. };
  112. nUpdateRectsLeft--;
  113. if (nUpdateRectsLeft == 0)
  114. handler->framebufferUpdateEnd();
  115. }
  116. }
  117. void CMsgReader::readSetColourMapEntries()
  118. {
  119. is->skip(1);
  120. int firstColour = is->readU16();
  121. int nColours = is->readU16();
  122. rdr::U16Array rgbs(nColours * 3);
  123. for (int i = 0; i < nColours * 3; i++)
  124. rgbs.buf[i] = is->readU16();
  125. handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
  126. }
  127. void CMsgReader::readBell()
  128. {
  129. handler->bell();
  130. }
  131. void CMsgReader::readServerCutText()
  132. {
  133. is->skip(3);
  134. rdr::U32 len = is->readU32();
  135. if (len > 256*1024) {
  136. is->skip(len);
  137. fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
  138. return;
  139. }
  140. CharArray ca(len+1);
  141. ca.buf[len] = 0;
  142. is->readBytes(ca.buf, len);
  143. handler->serverCutText(ca.buf, len);
  144. }
  145. void CMsgReader::readFence()
  146. {
  147. rdr::U32 flags;
  148. rdr::U8 len;
  149. char data[64];
  150. is->skip(3);
  151. flags = is->readU32();
  152. len = is->readU8();
  153. if (len > sizeof(data)) {
  154. fprintf(stderr, "Ignoring fence with too large payload\n");
  155. is->skip(len);
  156. return;
  157. }
  158. is->readBytes(data, len);
  159. handler->fence(flags, len, data);
  160. }
  161. void CMsgReader::readEndOfContinuousUpdates()
  162. {
  163. handler->endOfContinuousUpdates();
  164. }
  165. void CMsgReader::readFramebufferUpdate()
  166. {
  167. is->skip(1);
  168. nUpdateRectsLeft = is->readU16();
  169. handler->framebufferUpdateStart();
  170. }
  171. void CMsgReader::readRect(const Rect& r, int encoding)
  172. {
  173. if ((r.br.x > handler->cp.width) || (r.br.y > handler->cp.height)) {
  174. fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
  175. r.width(), r.height(), r.tl.x, r.tl.y,
  176. handler->cp.width, handler->cp.height);
  177. throw Exception("Rect too big");
  178. }
  179. if (r.is_empty())
  180. fprintf(stderr, "Warning: zero size rect\n");
  181. handler->dataRect(r, encoding);
  182. }
  183. void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
  184. {
  185. if (width > maxCursorSize || height > maxCursorSize)
  186. throw Exception("Too big cursor");
  187. rdr::U8Array rgba(width*height*4);
  188. if (width * height > 0) {
  189. rdr::U8 pr, pg, pb;
  190. rdr::U8 sr, sg, sb;
  191. int data_len = ((width+7)/8) * height;
  192. int mask_len = ((width+7)/8) * height;
  193. rdr::U8Array data(data_len);
  194. rdr::U8Array mask(mask_len);
  195. int x, y;
  196. rdr::U8* out;
  197. pr = is->readU8();
  198. pg = is->readU8();
  199. pb = is->readU8();
  200. sr = is->readU8();
  201. sg = is->readU8();
  202. sb = is->readU8();
  203. is->readBytes(data.buf, data_len);
  204. is->readBytes(mask.buf, mask_len);
  205. int maskBytesPerRow = (width+7)/8;
  206. out = rgba.buf;
  207. for (y = 0;y < height;y++) {
  208. for (x = 0;x < width;x++) {
  209. int byte = y * maskBytesPerRow + x / 8;
  210. int bit = 7 - x % 8;
  211. if (data.buf[byte] & (1 << bit)) {
  212. out[0] = pr;
  213. out[1] = pg;
  214. out[2] = pb;
  215. } else {
  216. out[0] = sr;
  217. out[1] = sg;
  218. out[2] = sb;
  219. }
  220. if (mask.buf[byte] & (1 << bit))
  221. out[3] = 255;
  222. else
  223. out[3] = 0;
  224. out += 4;
  225. }
  226. }
  227. }
  228. handler->setCursor(width, height, hotspot, rgba.buf);
  229. }
  230. void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
  231. {
  232. if (width > maxCursorSize || height > maxCursorSize)
  233. throw Exception("Too big cursor");
  234. int data_len = width * height * (handler->cp.pf().bpp/8);
  235. int mask_len = ((width+7)/8) * height;
  236. rdr::U8Array data(data_len);
  237. rdr::U8Array mask(mask_len);
  238. int x, y;
  239. rdr::U8Array rgba(width*height*4);
  240. rdr::U8* in;
  241. rdr::U8* out;
  242. is->readBytes(data.buf, data_len);
  243. is->readBytes(mask.buf, mask_len);
  244. int maskBytesPerRow = (width+7)/8;
  245. in = data.buf;
  246. out = rgba.buf;
  247. for (y = 0;y < height;y++) {
  248. for (x = 0;x < width;x++) {
  249. int byte = y * maskBytesPerRow + x / 8;
  250. int bit = 7 - x % 8;
  251. handler->cp.pf().rgbFromBuffer(out, in, 1);
  252. if (mask.buf[byte] & (1 << bit))
  253. out[3] = 255;
  254. else
  255. out[3] = 0;
  256. in += handler->cp.pf().bpp/8;
  257. out += 4;
  258. }
  259. }
  260. handler->setCursor(width, height, hotspot, rgba.buf);
  261. }
  262. void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
  263. {
  264. if (width > maxCursorSize || height > maxCursorSize)
  265. throw Exception("Too big cursor");
  266. int encoding;
  267. const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
  268. ManagedPixelBuffer pb(rgbaPF, width, height);
  269. PixelFormat origPF;
  270. rdr::U8* buf;
  271. int stride;
  272. encoding = is->readS32();
  273. origPF = handler->cp.pf();
  274. handler->cp.setPF(rgbaPF);
  275. handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
  276. handler->cp.setPF(origPF);
  277. // On-wire data has pre-multiplied alpha, but we store it
  278. // non-pre-multiplied
  279. buf = pb.getBufferRW(pb.getRect(), &stride);
  280. assert(stride == width);
  281. for (int i = 0;i < pb.area();i++) {
  282. rdr::U8 alpha;
  283. alpha = buf[3];
  284. if (alpha == 0)
  285. alpha = 1; // Avoid division by zero
  286. buf[0] = (unsigned)buf[0] * 255/alpha;
  287. buf[1] = (unsigned)buf[1] * 255/alpha;
  288. buf[2] = (unsigned)buf[2] * 255/alpha;
  289. buf += 4;
  290. }
  291. pb.commitBufferRW(pb.getRect());
  292. handler->setCursor(width, height, hotspot,
  293. pb.getBuffer(pb.getRect(), &stride));
  294. }
  295. void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
  296. {
  297. char* name = is->readString();
  298. if (x || y || w || h) {
  299. fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
  300. } else {
  301. handler->setName(name);
  302. }
  303. delete [] name;
  304. }
  305. void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
  306. {
  307. unsigned int screens, i;
  308. rdr::U32 id, flags;
  309. int sx, sy, sw, sh;
  310. ScreenSet layout;
  311. screens = is->readU8();
  312. is->skip(3);
  313. for (i = 0;i < screens;i++) {
  314. id = is->readU32();
  315. sx = is->readU16();
  316. sy = is->readU16();
  317. sw = is->readU16();
  318. sh = is->readU16();
  319. flags = is->readU32();
  320. layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
  321. }
  322. handler->setExtendedDesktopSize(x, y, w, h, layout);
  323. }
  324. void CMsgReader::readLEDState()
  325. {
  326. rdr::U8 state;
  327. state = is->readU8();
  328. handler->setLEDState(state);
  329. }