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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 pseudoEncodingVMwareCursor:
  92. readSetVMwareCursor(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. break;
  106. case pseudoEncodingVMwareLEDState:
  107. readVMwareLEDState();
  108. break;
  109. case pseudoEncodingQEMUKeyEvent:
  110. handler->supportsQEMUKeyEvent();
  111. break;
  112. default:
  113. readRect(Rect(x, y, x+w, y+h), encoding);
  114. break;
  115. };
  116. nUpdateRectsLeft--;
  117. if (nUpdateRectsLeft == 0)
  118. handler->framebufferUpdateEnd();
  119. }
  120. }
  121. void CMsgReader::readSetColourMapEntries()
  122. {
  123. is->skip(1);
  124. int firstColour = is->readU16();
  125. int nColours = is->readU16();
  126. rdr::U16Array rgbs(nColours * 3);
  127. for (int i = 0; i < nColours * 3; i++)
  128. rgbs.buf[i] = is->readU16();
  129. handler->setColourMapEntries(firstColour, nColours, rgbs.buf);
  130. }
  131. void CMsgReader::readBell()
  132. {
  133. handler->bell();
  134. }
  135. void CMsgReader::readServerCutText()
  136. {
  137. is->skip(3);
  138. rdr::U32 len = is->readU32();
  139. if (len > 256*1024) {
  140. is->skip(len);
  141. fprintf(stderr,"cut text too long (%d bytes) - ignoring\n",len);
  142. return;
  143. }
  144. CharArray ca(len+1);
  145. ca.buf[len] = 0;
  146. is->readBytes(ca.buf, len);
  147. handler->serverCutText(ca.buf, len);
  148. }
  149. void CMsgReader::readFence()
  150. {
  151. rdr::U32 flags;
  152. rdr::U8 len;
  153. char data[64];
  154. is->skip(3);
  155. flags = is->readU32();
  156. len = is->readU8();
  157. if (len > sizeof(data)) {
  158. fprintf(stderr, "Ignoring fence with too large payload\n");
  159. is->skip(len);
  160. return;
  161. }
  162. is->readBytes(data, len);
  163. handler->fence(flags, len, data);
  164. }
  165. void CMsgReader::readEndOfContinuousUpdates()
  166. {
  167. handler->endOfContinuousUpdates();
  168. }
  169. void CMsgReader::readFramebufferUpdate()
  170. {
  171. is->skip(1);
  172. nUpdateRectsLeft = is->readU16();
  173. handler->framebufferUpdateStart();
  174. }
  175. void CMsgReader::readRect(const Rect& r, int encoding)
  176. {
  177. if ((r.br.x > handler->server.width()) ||
  178. (r.br.y > handler->server.height())) {
  179. fprintf(stderr, "Rect too big: %dx%d at %d,%d exceeds %dx%d\n",
  180. r.width(), r.height(), r.tl.x, r.tl.y,
  181. handler->server.width(), handler->server.height());
  182. throw Exception("Rect too big");
  183. }
  184. if (r.is_empty())
  185. fprintf(stderr, "Warning: zero size rect\n");
  186. handler->dataRect(r, encoding);
  187. }
  188. void CMsgReader::readSetXCursor(int width, int height, const Point& hotspot)
  189. {
  190. if (width > maxCursorSize || height > maxCursorSize)
  191. throw Exception("Too big cursor");
  192. rdr::U8Array rgba(width*height*4);
  193. if (width * height > 0) {
  194. rdr::U8 pr, pg, pb;
  195. rdr::U8 sr, sg, sb;
  196. int data_len = ((width+7)/8) * height;
  197. int mask_len = ((width+7)/8) * height;
  198. rdr::U8Array data(data_len);
  199. rdr::U8Array mask(mask_len);
  200. int x, y;
  201. rdr::U8* out;
  202. pr = is->readU8();
  203. pg = is->readU8();
  204. pb = is->readU8();
  205. sr = is->readU8();
  206. sg = is->readU8();
  207. sb = is->readU8();
  208. is->readBytes(data.buf, data_len);
  209. is->readBytes(mask.buf, mask_len);
  210. int maskBytesPerRow = (width+7)/8;
  211. out = rgba.buf;
  212. for (y = 0;y < height;y++) {
  213. for (x = 0;x < width;x++) {
  214. int byte = y * maskBytesPerRow + x / 8;
  215. int bit = 7 - x % 8;
  216. if (data.buf[byte] & (1 << bit)) {
  217. out[0] = pr;
  218. out[1] = pg;
  219. out[2] = pb;
  220. } else {
  221. out[0] = sr;
  222. out[1] = sg;
  223. out[2] = sb;
  224. }
  225. if (mask.buf[byte] & (1 << bit))
  226. out[3] = 255;
  227. else
  228. out[3] = 0;
  229. out += 4;
  230. }
  231. }
  232. }
  233. handler->setCursor(width, height, hotspot, rgba.buf);
  234. }
  235. void CMsgReader::readSetCursor(int width, int height, const Point& hotspot)
  236. {
  237. if (width > maxCursorSize || height > maxCursorSize)
  238. throw Exception("Too big cursor");
  239. int data_len = width * height * (handler->server.pf().bpp/8);
  240. int mask_len = ((width+7)/8) * height;
  241. rdr::U8Array data(data_len);
  242. rdr::U8Array mask(mask_len);
  243. int x, y;
  244. rdr::U8Array rgba(width*height*4);
  245. rdr::U8* in;
  246. rdr::U8* out;
  247. is->readBytes(data.buf, data_len);
  248. is->readBytes(mask.buf, mask_len);
  249. int maskBytesPerRow = (width+7)/8;
  250. in = data.buf;
  251. out = rgba.buf;
  252. for (y = 0;y < height;y++) {
  253. for (x = 0;x < width;x++) {
  254. int byte = y * maskBytesPerRow + x / 8;
  255. int bit = 7 - x % 8;
  256. handler->server.pf().rgbFromBuffer(out, in, 1);
  257. if (mask.buf[byte] & (1 << bit))
  258. out[3] = 255;
  259. else
  260. out[3] = 0;
  261. in += handler->server.pf().bpp/8;
  262. out += 4;
  263. }
  264. }
  265. handler->setCursor(width, height, hotspot, rgba.buf);
  266. }
  267. void CMsgReader::readSetCursorWithAlpha(int width, int height, const Point& hotspot)
  268. {
  269. if (width > maxCursorSize || height > maxCursorSize)
  270. throw Exception("Too big cursor");
  271. int encoding;
  272. const PixelFormat rgbaPF(32, 32, false, true, 255, 255, 255, 16, 8, 0);
  273. ManagedPixelBuffer pb(rgbaPF, width, height);
  274. PixelFormat origPF;
  275. rdr::U8* buf;
  276. int stride;
  277. encoding = is->readS32();
  278. origPF = handler->server.pf();
  279. handler->server.setPF(rgbaPF);
  280. handler->readAndDecodeRect(pb.getRect(), encoding, &pb);
  281. handler->server.setPF(origPF);
  282. // On-wire data has pre-multiplied alpha, but we store it
  283. // non-pre-multiplied
  284. buf = pb.getBufferRW(pb.getRect(), &stride);
  285. assert(stride == width);
  286. for (int i = 0;i < pb.area();i++) {
  287. rdr::U8 alpha;
  288. alpha = buf[3];
  289. if (alpha == 0)
  290. alpha = 1; // Avoid division by zero
  291. buf[0] = (unsigned)buf[0] * 255/alpha;
  292. buf[1] = (unsigned)buf[1] * 255/alpha;
  293. buf[2] = (unsigned)buf[2] * 255/alpha;
  294. buf += 4;
  295. }
  296. pb.commitBufferRW(pb.getRect());
  297. handler->setCursor(width, height, hotspot,
  298. pb.getBuffer(pb.getRect(), &stride));
  299. }
  300. void CMsgReader::readSetVMwareCursor(int width, int height, const Point& hotspot)
  301. {
  302. if (width > maxCursorSize || height > maxCursorSize)
  303. throw Exception("Too big cursor");
  304. rdr::U8 type;
  305. type = is->readU8();
  306. is->skip(1);
  307. if (type == 0) {
  308. int len = width * height * (handler->server.pf().bpp/8);
  309. rdr::U8Array andMask(len);
  310. rdr::U8Array xorMask(len);
  311. rdr::U8Array data(width*height*4);
  312. rdr::U8* andIn;
  313. rdr::U8* xorIn;
  314. rdr::U8* out;
  315. int Bpp;
  316. is->readBytes(andMask.buf, len);
  317. is->readBytes(xorMask.buf, len);
  318. andIn = andMask.buf;
  319. xorIn = xorMask.buf;
  320. out = data.buf;
  321. Bpp = handler->server.pf().bpp/8;
  322. for (int y = 0;y < height;y++) {
  323. for (int x = 0;x < width;x++) {
  324. Pixel andPixel, xorPixel;
  325. andPixel = handler->server.pf().pixelFromBuffer(andIn);
  326. xorPixel = handler->server.pf().pixelFromBuffer(xorIn);
  327. andIn += Bpp;
  328. xorIn += Bpp;
  329. if (andPixel == 0) {
  330. rdr::U8 r, g, b;
  331. // Opaque pixel
  332. handler->server.pf().rgbFromPixel(xorPixel, &r, &g, &b);
  333. *out++ = r;
  334. *out++ = g;
  335. *out++ = b;
  336. *out++ = 0xff;
  337. } else if (xorPixel == 0) {
  338. // Fully transparent pixel
  339. *out++ = 0;
  340. *out++ = 0;
  341. *out++ = 0;
  342. *out++ = 0;
  343. } else if (andPixel == xorPixel) {
  344. // Inverted pixel
  345. // We don't really support this, so just turn the pixel black
  346. // FIXME: Do an outline like WinVNC does?
  347. *out++ = 0;
  348. *out++ = 0;
  349. *out++ = 0;
  350. *out++ = 0xff;
  351. } else {
  352. // Partially transparent/inverted pixel
  353. // We _really_ can't handle this, just make it black
  354. *out++ = 0;
  355. *out++ = 0;
  356. *out++ = 0;
  357. *out++ = 0xff;
  358. }
  359. }
  360. }
  361. handler->setCursor(width, height, hotspot, data.buf);
  362. } else if (type == 1) {
  363. rdr::U8Array data(width*height*4);
  364. // FIXME: Is alpha premultiplied?
  365. is->readBytes(data.buf, width*height*4);
  366. handler->setCursor(width, height, hotspot, data.buf);
  367. } else {
  368. throw Exception("Unknown cursor type");
  369. }
  370. }
  371. void CMsgReader::readSetDesktopName(int x, int y, int w, int h)
  372. {
  373. char* name = is->readString();
  374. if (x || y || w || h) {
  375. fprintf(stderr, "Ignoring DesktopName rect with non-zero position/size\n");
  376. } else {
  377. handler->setName(name);
  378. }
  379. delete [] name;
  380. }
  381. void CMsgReader::readExtendedDesktopSize(int x, int y, int w, int h)
  382. {
  383. unsigned int screens, i;
  384. rdr::U32 id, flags;
  385. int sx, sy, sw, sh;
  386. ScreenSet layout;
  387. screens = is->readU8();
  388. is->skip(3);
  389. for (i = 0;i < screens;i++) {
  390. id = is->readU32();
  391. sx = is->readU16();
  392. sy = is->readU16();
  393. sw = is->readU16();
  394. sh = is->readU16();
  395. flags = is->readU32();
  396. layout.add_screen(Screen(id, sx, sy, sw, sh, flags));
  397. }
  398. handler->setExtendedDesktopSize(x, y, w, h, layout);
  399. }
  400. void CMsgReader::readLEDState()
  401. {
  402. rdr::U8 state;
  403. state = is->readU8();
  404. handler->setLEDState(state);
  405. }
  406. void CMsgReader::readVMwareLEDState()
  407. {
  408. rdr::U32 state;
  409. state = is->readU32();
  410. // As luck has it, this extension uses the same bit definitions,
  411. // so no conversion required
  412. handler->setLEDState(state);
  413. }