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.

SMsgWriter.cxx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2009-2017 Pierre Ossman for Cendio AB
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #include <stdio.h>
  21. #include <rdr/OutStream.h>
  22. #include <rfb/msgTypes.h>
  23. #include <rfb/fenceTypes.h>
  24. #include <rfb/Exception.h>
  25. #include <rfb/ClientParams.h>
  26. #include <rfb/UpdateTracker.h>
  27. #include <rfb/Encoder.h>
  28. #include <rfb/SMsgWriter.h>
  29. #include <rfb/LogWriter.h>
  30. #include <rfb/ledStates.h>
  31. using namespace rfb;
  32. static LogWriter vlog("SMsgWriter");
  33. SMsgWriter::SMsgWriter(ClientParams* client_, rdr::OutStream* os_)
  34. : client(client_), os(os_),
  35. nRectsInUpdate(0), nRectsInHeader(0),
  36. needSetDesktopName(false), needCursor(false),
  37. needLEDState(false), needQEMUKeyEvent(false)
  38. {
  39. }
  40. SMsgWriter::~SMsgWriter()
  41. {
  42. }
  43. void SMsgWriter::writeServerInit(rdr::U16 width, rdr::U16 height,
  44. const PixelFormat& pf, const char* name)
  45. {
  46. os->writeU16(width);
  47. os->writeU16(height);
  48. pf.write(os);
  49. os->writeString(name);
  50. endMsg();
  51. }
  52. void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
  53. const rdr::U16 red[],
  54. const rdr::U16 green[],
  55. const rdr::U16 blue[])
  56. {
  57. startMsg(msgTypeSetColourMapEntries);
  58. os->pad(1);
  59. os->writeU16(firstColour);
  60. os->writeU16(nColours);
  61. for (int i = firstColour; i < firstColour+nColours; i++) {
  62. os->writeU16(red[i]);
  63. os->writeU16(green[i]);
  64. os->writeU16(blue[i]);
  65. }
  66. endMsg();
  67. }
  68. void SMsgWriter::writeBell()
  69. {
  70. startMsg(msgTypeBell);
  71. endMsg();
  72. }
  73. void SMsgWriter::writeServerCutText(const char* str, int len)
  74. {
  75. if (memchr(str, '\r', len) != NULL)
  76. throw Exception("Invalid carriage return in clipboard data");
  77. startMsg(msgTypeServerCutText);
  78. os->pad(3);
  79. os->writeU32(len);
  80. os->writeBytes(str, len);
  81. endMsg();
  82. }
  83. void SMsgWriter::writeFence(rdr::U32 flags, unsigned len, const char data[])
  84. {
  85. if (!client->supportsEncoding(pseudoEncodingFence))
  86. throw Exception("Client does not support fences");
  87. if (len > 64)
  88. throw Exception("Too large fence payload");
  89. if ((flags & ~fenceFlagsSupported) != 0)
  90. throw Exception("Unknown fence flags");
  91. startMsg(msgTypeServerFence);
  92. os->pad(3);
  93. os->writeU32(flags);
  94. os->writeU8(len);
  95. if (len > 0)
  96. os->writeBytes(data, len);
  97. endMsg();
  98. }
  99. void SMsgWriter::writeEndOfContinuousUpdates()
  100. {
  101. if (!client->supportsEncoding(pseudoEncodingContinuousUpdates))
  102. throw Exception("Client does not support continuous updates");
  103. startMsg(msgTypeEndOfContinuousUpdates);
  104. endMsg();
  105. }
  106. void SMsgWriter::writeDesktopSize(rdr::U16 reason, rdr::U16 result)
  107. {
  108. ExtendedDesktopSizeMsg msg;
  109. if (!client->supportsEncoding(pseudoEncodingDesktopSize) &&
  110. !client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  111. throw Exception("Client does not support desktop size changes");
  112. msg.reason = reason;
  113. msg.result = result;
  114. extendedDesktopSizeMsgs.push_back(msg);
  115. }
  116. void SMsgWriter::writeSetDesktopName()
  117. {
  118. if (!client->supportsEncoding(pseudoEncodingDesktopName))
  119. throw Exception("Client does not support desktop name changes");
  120. needSetDesktopName = true;
  121. }
  122. void SMsgWriter::writeCursor()
  123. {
  124. if (!client->supportsEncoding(pseudoEncodingCursor) &&
  125. !client->supportsEncoding(pseudoEncodingXCursor) &&
  126. !client->supportsEncoding(pseudoEncodingCursorWithAlpha) &&
  127. !client->supportsEncoding(pseudoEncodingVMwareCursor))
  128. throw Exception("Client does not support local cursor");
  129. needCursor = true;
  130. }
  131. void SMsgWriter::writeLEDState()
  132. {
  133. if (!client->supportsEncoding(pseudoEncodingLEDState) &&
  134. !client->supportsEncoding(pseudoEncodingVMwareLEDState))
  135. throw Exception("Client does not support LED state");
  136. if (client->ledState() == ledUnknown)
  137. throw Exception("Server has not specified LED state");
  138. needLEDState = true;
  139. }
  140. void SMsgWriter::writeQEMUKeyEvent()
  141. {
  142. if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
  143. throw Exception("Client does not support QEMU key events");
  144. needQEMUKeyEvent = true;
  145. }
  146. bool SMsgWriter::needFakeUpdate()
  147. {
  148. if (needSetDesktopName)
  149. return true;
  150. if (needCursor)
  151. return true;
  152. if (needLEDState)
  153. return true;
  154. if (needQEMUKeyEvent)
  155. return true;
  156. if (needNoDataUpdate())
  157. return true;
  158. return false;
  159. }
  160. bool SMsgWriter::needNoDataUpdate()
  161. {
  162. if (!extendedDesktopSizeMsgs.empty())
  163. return true;
  164. return false;
  165. }
  166. void SMsgWriter::writeNoDataUpdate()
  167. {
  168. int nRects;
  169. nRects = 0;
  170. if (!extendedDesktopSizeMsgs.empty()) {
  171. if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  172. nRects += extendedDesktopSizeMsgs.size();
  173. else
  174. nRects++;
  175. }
  176. writeFramebufferUpdateStart(nRects);
  177. writeNoDataRects();
  178. writeFramebufferUpdateEnd();
  179. }
  180. void SMsgWriter::writeFramebufferUpdateStart(int nRects)
  181. {
  182. startMsg(msgTypeFramebufferUpdate);
  183. os->pad(1);
  184. if (nRects != 0xFFFF) {
  185. if (needSetDesktopName)
  186. nRects++;
  187. if (needCursor)
  188. nRects++;
  189. if (needLEDState)
  190. nRects++;
  191. if (needQEMUKeyEvent)
  192. nRects++;
  193. }
  194. os->writeU16(nRects);
  195. nRectsInUpdate = 0;
  196. if (nRects == 0xFFFF)
  197. nRectsInHeader = 0;
  198. else
  199. nRectsInHeader = nRects;
  200. writePseudoRects();
  201. }
  202. void SMsgWriter::writeFramebufferUpdateEnd()
  203. {
  204. if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
  205. throw Exception("SMsgWriter::writeFramebufferUpdateEnd: "
  206. "nRects out of sync");
  207. if (nRectsInHeader == 0) {
  208. // Send last rect. marker
  209. os->writeS16(0);
  210. os->writeS16(0);
  211. os->writeU16(0);
  212. os->writeU16(0);
  213. os->writeU32(pseudoEncodingLastRect);
  214. }
  215. endMsg();
  216. }
  217. void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
  218. {
  219. startRect(r,encodingCopyRect);
  220. os->writeU16(srcX);
  221. os->writeU16(srcY);
  222. endRect();
  223. }
  224. void SMsgWriter::startRect(const Rect& r, int encoding)
  225. {
  226. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  227. throw Exception("SMsgWriter::startRect: nRects out of sync");
  228. os->writeS16(r.tl.x);
  229. os->writeS16(r.tl.y);
  230. os->writeU16(r.width());
  231. os->writeU16(r.height());
  232. os->writeU32(encoding);
  233. }
  234. void SMsgWriter::endRect()
  235. {
  236. os->flush();
  237. }
  238. void SMsgWriter::startMsg(int type)
  239. {
  240. os->writeU8(type);
  241. }
  242. void SMsgWriter::endMsg()
  243. {
  244. os->flush();
  245. }
  246. void SMsgWriter::writePseudoRects()
  247. {
  248. if (needCursor) {
  249. const Cursor& cursor = client->cursor();
  250. if (client->supportsEncoding(pseudoEncodingCursorWithAlpha)) {
  251. writeSetCursorWithAlphaRect(cursor.width(), cursor.height(),
  252. cursor.hotspot().x, cursor.hotspot().y,
  253. cursor.getBuffer());
  254. } else if (client->supportsEncoding(pseudoEncodingVMwareCursor)) {
  255. writeSetVMwareCursorRect(cursor.width(), cursor.height(),
  256. cursor.hotspot().x, cursor.hotspot().y,
  257. cursor.getBuffer());
  258. } else if (client->supportsEncoding(pseudoEncodingCursor)) {
  259. rdr::U8Array data(cursor.width()*cursor.height() * (client->pf().bpp/8));
  260. rdr::U8Array mask(cursor.getMask());
  261. const rdr::U8* in;
  262. rdr::U8* out;
  263. in = cursor.getBuffer();
  264. out = data.buf;
  265. for (int i = 0;i < cursor.width()*cursor.height();i++) {
  266. client->pf().bufferFromRGB(out, in, 1);
  267. in += 4;
  268. out += client->pf().bpp/8;
  269. }
  270. writeSetCursorRect(cursor.width(), cursor.height(),
  271. cursor.hotspot().x, cursor.hotspot().y,
  272. data.buf, mask.buf);
  273. } else if (client->supportsEncoding(pseudoEncodingXCursor)) {
  274. rdr::U8Array bitmap(cursor.getBitmap());
  275. rdr::U8Array mask(cursor.getMask());
  276. writeSetXCursorRect(cursor.width(), cursor.height(),
  277. cursor.hotspot().x, cursor.hotspot().y,
  278. bitmap.buf, mask.buf);
  279. } else {
  280. throw Exception("Client does not support local cursor");
  281. }
  282. needCursor = false;
  283. }
  284. if (needSetDesktopName) {
  285. writeSetDesktopNameRect(client->name());
  286. needSetDesktopName = false;
  287. }
  288. if (needLEDState) {
  289. writeLEDStateRect(client->ledState());
  290. needLEDState = false;
  291. }
  292. if (needQEMUKeyEvent) {
  293. writeQEMUKeyEventRect();
  294. needQEMUKeyEvent = false;
  295. }
  296. }
  297. void SMsgWriter::writeNoDataRects()
  298. {
  299. if (!extendedDesktopSizeMsgs.empty()) {
  300. if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize)) {
  301. std::list<ExtendedDesktopSizeMsg>::const_iterator ri;
  302. for (ri = extendedDesktopSizeMsgs.begin();ri != extendedDesktopSizeMsgs.end();++ri) {
  303. // FIXME: We can probably skip multiple reasonServer entries
  304. writeExtendedDesktopSizeRect(ri->reason, ri->result,
  305. client->width(), client->height(),
  306. client->screenLayout());
  307. }
  308. } else if (client->supportsEncoding(pseudoEncodingDesktopSize)) {
  309. // Some clients assume this is the last rectangle so don't send anything
  310. // more after this
  311. writeSetDesktopSizeRect(client->width(), client->height());
  312. } else {
  313. throw Exception("Client does not support desktop size changes");
  314. }
  315. extendedDesktopSizeMsgs.clear();
  316. }
  317. }
  318. void SMsgWriter::writeSetDesktopSizeRect(int width, int height)
  319. {
  320. if (!client->supportsEncoding(pseudoEncodingDesktopSize))
  321. throw Exception("Client does not support desktop resize");
  322. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  323. throw Exception("SMsgWriter::writeSetDesktopSizeRect: nRects out of sync");
  324. os->writeS16(0);
  325. os->writeS16(0);
  326. os->writeU16(width);
  327. os->writeU16(height);
  328. os->writeU32(pseudoEncodingDesktopSize);
  329. }
  330. void SMsgWriter::writeExtendedDesktopSizeRect(rdr::U16 reason,
  331. rdr::U16 result,
  332. int fb_width,
  333. int fb_height,
  334. const ScreenSet& layout)
  335. {
  336. ScreenSet::const_iterator si;
  337. if (!client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  338. throw Exception("Client does not support extended desktop resize");
  339. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  340. throw Exception("SMsgWriter::writeExtendedDesktopSizeRect: nRects out of sync");
  341. os->writeU16(reason);
  342. os->writeU16(result);
  343. os->writeU16(fb_width);
  344. os->writeU16(fb_height);
  345. os->writeU32(pseudoEncodingExtendedDesktopSize);
  346. os->writeU8(layout.num_screens());
  347. os->pad(3);
  348. for (si = layout.begin();si != layout.end();++si) {
  349. os->writeU32(si->id);
  350. os->writeU16(si->dimensions.tl.x);
  351. os->writeU16(si->dimensions.tl.y);
  352. os->writeU16(si->dimensions.width());
  353. os->writeU16(si->dimensions.height());
  354. os->writeU32(si->flags);
  355. }
  356. }
  357. void SMsgWriter::writeSetDesktopNameRect(const char *name)
  358. {
  359. if (!client->supportsEncoding(pseudoEncodingDesktopName))
  360. throw Exception("Client does not support desktop rename");
  361. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  362. throw Exception("SMsgWriter::writeSetDesktopNameRect: nRects out of sync");
  363. os->writeS16(0);
  364. os->writeS16(0);
  365. os->writeU16(0);
  366. os->writeU16(0);
  367. os->writeU32(pseudoEncodingDesktopName);
  368. os->writeString(name);
  369. }
  370. void SMsgWriter::writeSetCursorRect(int width, int height,
  371. int hotspotX, int hotspotY,
  372. const void* data, const void* mask)
  373. {
  374. if (!client->supportsEncoding(pseudoEncodingCursor))
  375. throw Exception("Client does not support local cursors");
  376. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  377. throw Exception("SMsgWriter::writeSetCursorRect: nRects out of sync");
  378. os->writeS16(hotspotX);
  379. os->writeS16(hotspotY);
  380. os->writeU16(width);
  381. os->writeU16(height);
  382. os->writeU32(pseudoEncodingCursor);
  383. os->writeBytes(data, width * height * (client->pf().bpp/8));
  384. os->writeBytes(mask, (width+7)/8 * height);
  385. }
  386. void SMsgWriter::writeSetXCursorRect(int width, int height,
  387. int hotspotX, int hotspotY,
  388. const void* data, const void* mask)
  389. {
  390. if (!client->supportsEncoding(pseudoEncodingXCursor))
  391. throw Exception("Client does not support local cursors");
  392. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  393. throw Exception("SMsgWriter::writeSetXCursorRect: nRects out of sync");
  394. os->writeS16(hotspotX);
  395. os->writeS16(hotspotY);
  396. os->writeU16(width);
  397. os->writeU16(height);
  398. os->writeU32(pseudoEncodingXCursor);
  399. if (width * height > 0) {
  400. os->writeU8(255);
  401. os->writeU8(255);
  402. os->writeU8(255);
  403. os->writeU8(0);
  404. os->writeU8(0);
  405. os->writeU8(0);
  406. os->writeBytes(data, (width+7)/8 * height);
  407. os->writeBytes(mask, (width+7)/8 * height);
  408. }
  409. }
  410. void SMsgWriter::writeSetCursorWithAlphaRect(int width, int height,
  411. int hotspotX, int hotspotY,
  412. const rdr::U8* data)
  413. {
  414. if (!client->supportsEncoding(pseudoEncodingCursorWithAlpha))
  415. throw Exception("Client does not support local cursors");
  416. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  417. throw Exception("SMsgWriter::writeSetCursorWithAlphaRect: nRects out of sync");
  418. os->writeS16(hotspotX);
  419. os->writeS16(hotspotY);
  420. os->writeU16(width);
  421. os->writeU16(height);
  422. os->writeU32(pseudoEncodingCursorWithAlpha);
  423. // FIXME: Use an encoder with compression?
  424. os->writeU32(encodingRaw);
  425. // Alpha needs to be pre-multiplied
  426. for (int i = 0;i < width*height;i++) {
  427. os->writeU8((unsigned)data[0] * data[3] / 255);
  428. os->writeU8((unsigned)data[1] * data[3] / 255);
  429. os->writeU8((unsigned)data[2] * data[3] / 255);
  430. os->writeU8(data[3]);
  431. data += 4;
  432. }
  433. }
  434. void SMsgWriter::writeSetVMwareCursorRect(int width, int height,
  435. int hotspotX, int hotspotY,
  436. const rdr::U8* data)
  437. {
  438. if (!client->supportsEncoding(pseudoEncodingVMwareCursor))
  439. throw Exception("Client does not support local cursors");
  440. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  441. throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");
  442. os->writeS16(hotspotX);
  443. os->writeS16(hotspotY);
  444. os->writeU16(width);
  445. os->writeU16(height);
  446. os->writeU32(pseudoEncodingVMwareCursor);
  447. os->writeU8(1); // Alpha cursor
  448. os->pad(1);
  449. // FIXME: Should alpha be premultiplied?
  450. os->writeBytes(data, width*height*4);
  451. }
  452. void SMsgWriter::writeLEDStateRect(rdr::U8 state)
  453. {
  454. if (!client->supportsEncoding(pseudoEncodingLEDState) &&
  455. !client->supportsEncoding(pseudoEncodingVMwareLEDState))
  456. throw Exception("Client does not support LED state updates");
  457. if (client->ledState() == ledUnknown)
  458. throw Exception("Server does not support LED state updates");
  459. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  460. throw Exception("SMsgWriter::writeLEDStateRect: nRects out of sync");
  461. os->writeS16(0);
  462. os->writeS16(0);
  463. os->writeU16(0);
  464. os->writeU16(0);
  465. if (client->supportsEncoding(pseudoEncodingLEDState)) {
  466. os->writeU32(pseudoEncodingLEDState);
  467. os->writeU8(state);
  468. } else {
  469. os->writeU32(pseudoEncodingVMwareLEDState);
  470. os->writeU32(state);
  471. }
  472. }
  473. void SMsgWriter::writeQEMUKeyEventRect()
  474. {
  475. if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
  476. throw Exception("Client does not support QEMU extended key events");
  477. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  478. throw Exception("SMsgWriter::writeQEMUKeyEventRect: nRects out of sync");
  479. os->writeS16(0);
  480. os->writeS16(0);
  481. os->writeU16(0);
  482. os->writeU16(0);
  483. os->writeU32(pseudoEncodingQEMUKeyEvent);
  484. }