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

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