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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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/ConnParams.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(ConnParams* cp_, rdr::OutStream* os_)
  34. : cp(cp_), os(os_),
  35. nRectsInUpdate(0), nRectsInHeader(0),
  36. needSetDesktopSize(false), needExtendedDesktopSize(false),
  37. needSetDesktopName(false), needSetCursor(false),
  38. needSetXCursor(false), needSetCursorWithAlpha(false),
  39. needLEDState(false), needQEMUKeyEvent(false)
  40. {
  41. }
  42. SMsgWriter::~SMsgWriter()
  43. {
  44. }
  45. void SMsgWriter::writeServerInit()
  46. {
  47. os->writeU16(cp->width);
  48. os->writeU16(cp->height);
  49. cp->pf().write(os);
  50. os->writeString(cp->name());
  51. endMsg();
  52. }
  53. void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
  54. const rdr::U16 red[],
  55. const rdr::U16 green[],
  56. const rdr::U16 blue[])
  57. {
  58. startMsg(msgTypeSetColourMapEntries);
  59. os->pad(1);
  60. os->writeU16(firstColour);
  61. os->writeU16(nColours);
  62. for (int i = firstColour; i < firstColour+nColours; i++) {
  63. os->writeU16(red[i]);
  64. os->writeU16(green[i]);
  65. os->writeU16(blue[i]);
  66. }
  67. endMsg();
  68. }
  69. void SMsgWriter::writeBell()
  70. {
  71. startMsg(msgTypeBell);
  72. endMsg();
  73. }
  74. void SMsgWriter::writeServerCutText(const char* str, int len)
  75. {
  76. startMsg(msgTypeServerCutText);
  77. os->pad(3);
  78. os->writeU32(len);
  79. os->writeBytes(str, len);
  80. endMsg();
  81. }
  82. void SMsgWriter::writeFence(rdr::U32 flags, unsigned len, const char data[])
  83. {
  84. if (!cp->supportsFence)
  85. throw Exception("Client does not support fences");
  86. if (len > 64)
  87. throw Exception("Too large fence payload");
  88. if ((flags & ~fenceFlagsSupported) != 0)
  89. throw Exception("Unknown fence flags");
  90. startMsg(msgTypeServerFence);
  91. os->pad(3);
  92. os->writeU32(flags);
  93. os->writeU8(len);
  94. if (len > 0)
  95. os->writeBytes(data, len);
  96. endMsg();
  97. }
  98. void SMsgWriter::writeEndOfContinuousUpdates()
  99. {
  100. if (!cp->supportsContinuousUpdates)
  101. throw Exception("Client does not support continuous updates");
  102. startMsg(msgTypeEndOfContinuousUpdates);
  103. endMsg();
  104. }
  105. bool SMsgWriter::writeSetDesktopSize() {
  106. if (!cp->supportsDesktopResize)
  107. return false;
  108. needSetDesktopSize = true;
  109. return true;
  110. }
  111. bool SMsgWriter::writeExtendedDesktopSize() {
  112. if (!cp->supportsExtendedDesktopSize)
  113. return false;
  114. needExtendedDesktopSize = true;
  115. return true;
  116. }
  117. bool SMsgWriter::writeExtendedDesktopSize(rdr::U16 reason, rdr::U16 result,
  118. int fb_width, int fb_height,
  119. const ScreenSet& layout) {
  120. ExtendedDesktopSizeMsg msg;
  121. if (!cp->supportsExtendedDesktopSize)
  122. return false;
  123. msg.reason = reason;
  124. msg.result = result;
  125. msg.fb_width = fb_width;
  126. msg.fb_height = fb_height;
  127. msg.layout = layout;
  128. extendedDesktopSizeMsgs.push_back(msg);
  129. return true;
  130. }
  131. bool SMsgWriter::writeSetDesktopName() {
  132. if (!cp->supportsDesktopRename)
  133. return false;
  134. needSetDesktopName = true;
  135. return true;
  136. }
  137. bool SMsgWriter::writeSetCursor()
  138. {
  139. if (!cp->supportsLocalCursor)
  140. return false;
  141. needSetCursor = true;
  142. return true;
  143. }
  144. bool SMsgWriter::writeSetXCursor()
  145. {
  146. if (!cp->supportsLocalXCursor)
  147. return false;
  148. needSetXCursor = true;
  149. return true;
  150. }
  151. bool SMsgWriter::writeSetCursorWithAlpha()
  152. {
  153. if (!cp->supportsLocalCursorWithAlpha)
  154. return false;
  155. needSetCursorWithAlpha = true;
  156. return true;
  157. }
  158. bool SMsgWriter::writeLEDState()
  159. {
  160. if (!cp->supportsLEDState)
  161. return false;
  162. if (cp->ledState() == ledUnknown)
  163. return false;
  164. needLEDState = true;
  165. return true;
  166. }
  167. bool SMsgWriter::writeQEMUKeyEvent()
  168. {
  169. if (!cp->supportsQEMUKeyEvent)
  170. return false;
  171. needQEMUKeyEvent = true;
  172. return true;
  173. }
  174. bool SMsgWriter::needFakeUpdate()
  175. {
  176. if (needSetDesktopName)
  177. return true;
  178. if (needSetCursor || needSetXCursor || needSetCursorWithAlpha)
  179. return true;
  180. if (needLEDState)
  181. return true;
  182. if (needQEMUKeyEvent)
  183. return true;
  184. if (needNoDataUpdate())
  185. return true;
  186. return false;
  187. }
  188. bool SMsgWriter::needNoDataUpdate()
  189. {
  190. if (needSetDesktopSize)
  191. return true;
  192. if (needExtendedDesktopSize || !extendedDesktopSizeMsgs.empty())
  193. return true;
  194. return false;
  195. }
  196. void SMsgWriter::writeNoDataUpdate()
  197. {
  198. int nRects;
  199. nRects = 0;
  200. if (needSetDesktopSize)
  201. nRects++;
  202. if (needExtendedDesktopSize)
  203. nRects++;
  204. if (!extendedDesktopSizeMsgs.empty())
  205. nRects += extendedDesktopSizeMsgs.size();
  206. writeFramebufferUpdateStart(nRects);
  207. writeNoDataRects();
  208. writeFramebufferUpdateEnd();
  209. }
  210. void SMsgWriter::writeFramebufferUpdateStart(int nRects)
  211. {
  212. startMsg(msgTypeFramebufferUpdate);
  213. os->pad(1);
  214. if (nRects != 0xFFFF) {
  215. if (needSetDesktopName)
  216. nRects++;
  217. if (needSetCursor)
  218. nRects++;
  219. if (needSetXCursor)
  220. nRects++;
  221. if (needSetCursorWithAlpha)
  222. nRects++;
  223. if (needLEDState)
  224. nRects++;
  225. if (needQEMUKeyEvent)
  226. nRects++;
  227. }
  228. os->writeU16(nRects);
  229. nRectsInUpdate = 0;
  230. if (nRects == 0xFFFF)
  231. nRectsInHeader = 0;
  232. else
  233. nRectsInHeader = nRects;
  234. writePseudoRects();
  235. }
  236. void SMsgWriter::writeFramebufferUpdateEnd()
  237. {
  238. if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
  239. throw Exception("SMsgWriter::writeFramebufferUpdateEnd: "
  240. "nRects out of sync");
  241. if (nRectsInHeader == 0) {
  242. // Send last rect. marker
  243. os->writeS16(0);
  244. os->writeS16(0);
  245. os->writeU16(0);
  246. os->writeU16(0);
  247. os->writeU32(pseudoEncodingLastRect);
  248. }
  249. endMsg();
  250. }
  251. void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
  252. {
  253. startRect(r,encodingCopyRect);
  254. os->writeU16(srcX);
  255. os->writeU16(srcY);
  256. endRect();
  257. }
  258. void SMsgWriter::startRect(const Rect& r, int encoding)
  259. {
  260. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  261. throw Exception("SMsgWriter::startRect: nRects out of sync");
  262. os->writeS16(r.tl.x);
  263. os->writeS16(r.tl.y);
  264. os->writeU16(r.width());
  265. os->writeU16(r.height());
  266. os->writeU32(encoding);
  267. }
  268. void SMsgWriter::endRect()
  269. {
  270. os->flush();
  271. }
  272. void SMsgWriter::startMsg(int type)
  273. {
  274. os->writeU8(type);
  275. }
  276. void SMsgWriter::endMsg()
  277. {
  278. os->flush();
  279. }
  280. void SMsgWriter::writePseudoRects()
  281. {
  282. if (needSetCursor) {
  283. const Cursor& cursor = cp->cursor();
  284. rdr::U8Array data(cursor.width()*cursor.height() * cp->pf().bpp/8);
  285. rdr::U8Array mask(cursor.getMask());
  286. const rdr::U8* in;
  287. rdr::U8* out;
  288. in = cursor.getBuffer();
  289. out = data.buf;
  290. for (int i = 0;i < cursor.width()*cursor.height();i++) {
  291. cp->pf().bufferFromRGB(out, in, 1);
  292. in += 4;
  293. out += cp->pf().bpp/8;
  294. }
  295. writeSetCursorRect(cursor.width(), cursor.height(),
  296. cursor.hotspot().x, cursor.hotspot().y,
  297. data.buf, mask.buf);
  298. needSetCursor = false;
  299. }
  300. if (needSetXCursor) {
  301. const Cursor& cursor = cp->cursor();
  302. rdr::U8Array bitmap(cursor.getBitmap());
  303. rdr::U8Array mask(cursor.getMask());
  304. writeSetXCursorRect(cursor.width(), cursor.height(),
  305. cursor.hotspot().x, cursor.hotspot().y,
  306. bitmap.buf, mask.buf);
  307. needSetXCursor = false;
  308. }
  309. if (needSetCursorWithAlpha) {
  310. const Cursor& cursor = cp->cursor();
  311. writeSetCursorWithAlphaRect(cursor.width(), cursor.height(),
  312. cursor.hotspot().x, cursor.hotspot().y,
  313. cursor.getBuffer());
  314. needSetCursorWithAlpha = false;
  315. }
  316. if (needSetDesktopName) {
  317. writeSetDesktopNameRect(cp->name());
  318. needSetDesktopName = false;
  319. }
  320. if (needLEDState) {
  321. writeLEDStateRect(cp->ledState());
  322. needLEDState = false;
  323. }
  324. if (needQEMUKeyEvent) {
  325. writeQEMUKeyEventRect();
  326. needQEMUKeyEvent = false;
  327. }
  328. }
  329. void SMsgWriter::writeNoDataRects()
  330. {
  331. // Start with specific ExtendedDesktopSize messages
  332. if (!extendedDesktopSizeMsgs.empty()) {
  333. std::list<ExtendedDesktopSizeMsg>::const_iterator ri;
  334. for (ri = extendedDesktopSizeMsgs.begin();ri != extendedDesktopSizeMsgs.end();++ri) {
  335. writeExtendedDesktopSizeRect(ri->reason, ri->result,
  336. ri->fb_width, ri->fb_height, ri->layout);
  337. }
  338. extendedDesktopSizeMsgs.clear();
  339. }
  340. // Send this before SetDesktopSize to make life easier on the clients
  341. if (needExtendedDesktopSize) {
  342. writeExtendedDesktopSizeRect(0, 0, cp->width, cp->height,
  343. cp->screenLayout);
  344. needExtendedDesktopSize = false;
  345. }
  346. // Some clients assume this is the last rectangle so don't send anything
  347. // more after this
  348. if (needSetDesktopSize) {
  349. writeSetDesktopSizeRect(cp->width, cp->height);
  350. needSetDesktopSize = false;
  351. }
  352. }
  353. void SMsgWriter::writeSetDesktopSizeRect(int width, int height)
  354. {
  355. if (!cp->supportsDesktopResize)
  356. throw Exception("Client does not support desktop resize");
  357. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  358. throw Exception("SMsgWriter::writeSetDesktopSizeRect: nRects out of sync");
  359. os->writeS16(0);
  360. os->writeS16(0);
  361. os->writeU16(width);
  362. os->writeU16(height);
  363. os->writeU32(pseudoEncodingDesktopSize);
  364. }
  365. void SMsgWriter::writeExtendedDesktopSizeRect(rdr::U16 reason,
  366. rdr::U16 result,
  367. int fb_width,
  368. int fb_height,
  369. const ScreenSet& layout)
  370. {
  371. ScreenSet::const_iterator si;
  372. if (!cp->supportsExtendedDesktopSize)
  373. throw Exception("Client does not support extended desktop resize");
  374. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  375. throw Exception("SMsgWriter::writeExtendedDesktopSizeRect: nRects out of sync");
  376. os->writeU16(reason);
  377. os->writeU16(result);
  378. os->writeU16(fb_width);
  379. os->writeU16(fb_height);
  380. os->writeU32(pseudoEncodingExtendedDesktopSize);
  381. os->writeU8(layout.num_screens());
  382. os->pad(3);
  383. for (si = layout.begin();si != layout.end();++si) {
  384. os->writeU32(si->id);
  385. os->writeU16(si->dimensions.tl.x);
  386. os->writeU16(si->dimensions.tl.y);
  387. os->writeU16(si->dimensions.width());
  388. os->writeU16(si->dimensions.height());
  389. os->writeU32(si->flags);
  390. }
  391. }
  392. void SMsgWriter::writeSetDesktopNameRect(const char *name)
  393. {
  394. if (!cp->supportsDesktopRename)
  395. throw Exception("Client does not support desktop rename");
  396. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  397. throw Exception("SMsgWriter::writeSetDesktopNameRect: nRects out of sync");
  398. os->writeS16(0);
  399. os->writeS16(0);
  400. os->writeU16(0);
  401. os->writeU16(0);
  402. os->writeU32(pseudoEncodingDesktopName);
  403. os->writeString(name);
  404. }
  405. void SMsgWriter::writeSetCursorRect(int width, int height,
  406. int hotspotX, int hotspotY,
  407. const void* data, const void* mask)
  408. {
  409. if (!cp->supportsLocalCursor)
  410. throw Exception("Client does not support local cursors");
  411. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  412. throw Exception("SMsgWriter::writeSetCursorRect: nRects out of sync");
  413. os->writeS16(hotspotX);
  414. os->writeS16(hotspotY);
  415. os->writeU16(width);
  416. os->writeU16(height);
  417. os->writeU32(pseudoEncodingCursor);
  418. os->writeBytes(data, width * height * (cp->pf().bpp/8));
  419. os->writeBytes(mask, (width+7)/8 * height);
  420. }
  421. void SMsgWriter::writeSetXCursorRect(int width, int height,
  422. int hotspotX, int hotspotY,
  423. const void* data, const void* mask)
  424. {
  425. if (!cp->supportsLocalXCursor)
  426. throw Exception("Client does not support local cursors");
  427. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  428. throw Exception("SMsgWriter::writeSetXCursorRect: nRects out of sync");
  429. os->writeS16(hotspotX);
  430. os->writeS16(hotspotY);
  431. os->writeU16(width);
  432. os->writeU16(height);
  433. os->writeU32(pseudoEncodingXCursor);
  434. if (width * height > 0) {
  435. os->writeU8(255);
  436. os->writeU8(255);
  437. os->writeU8(255);
  438. os->writeU8(0);
  439. os->writeU8(0);
  440. os->writeU8(0);
  441. os->writeBytes(data, (width+7)/8 * height);
  442. os->writeBytes(mask, (width+7)/8 * height);
  443. }
  444. }
  445. void SMsgWriter::writeSetCursorWithAlphaRect(int width, int height,
  446. int hotspotX, int hotspotY,
  447. const rdr::U8* data)
  448. {
  449. if (!cp->supportsLocalCursorWithAlpha)
  450. throw Exception("Client does not support local cursors");
  451. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  452. throw Exception("SMsgWriter::writeSetCursorWithAlphaRect: nRects out of sync");
  453. os->writeS16(hotspotX);
  454. os->writeS16(hotspotY);
  455. os->writeU16(width);
  456. os->writeU16(height);
  457. os->writeU32(pseudoEncodingCursorWithAlpha);
  458. // FIXME: Use an encoder with compression?
  459. os->writeU32(encodingRaw);
  460. // Alpha needs to be pre-multiplied
  461. for (int i = 0;i < width*height;i++) {
  462. os->writeU8((unsigned)data[0] * data[3] / 255);
  463. os->writeU8((unsigned)data[1] * data[3] / 255);
  464. os->writeU8((unsigned)data[2] * data[3] / 255);
  465. os->writeU8(data[3]);
  466. data += 4;
  467. }
  468. }
  469. void SMsgWriter::writeLEDStateRect(rdr::U8 state)
  470. {
  471. if (!cp->supportsLEDState)
  472. throw Exception("Client does not support LED state updates");
  473. if (cp->ledState() == ledUnknown)
  474. throw Exception("Server does not support LED state updates");
  475. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  476. throw Exception("SMsgWriter::writeLEDStateRect: nRects out of sync");
  477. os->writeS16(0);
  478. os->writeS16(0);
  479. os->writeU16(0);
  480. os->writeU16(0);
  481. os->writeU32(pseudoEncodingLEDState);
  482. os->writeU8(state);
  483. }
  484. void SMsgWriter::writeQEMUKeyEventRect()
  485. {
  486. if (!cp->supportsQEMUKeyEvent)
  487. throw Exception("Client does not support QEMU extended key events");
  488. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  489. throw Exception("SMsgWriter::writeQEMUKeyEventRect: nRects out of sync");
  490. os->writeS16(0);
  491. os->writeS16(0);
  492. os->writeU16(0);
  493. os->writeU16(0);
  494. os->writeU32(pseudoEncodingQEMUKeyEvent);
  495. }