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

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