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

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