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

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