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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2009-2019 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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <rdr/OutStream.h>
  25. #include <rdr/MemOutStream.h>
  26. #include <rdr/ZlibOutStream.h>
  27. #include <rfb/msgTypes.h>
  28. #include <rfb/fenceTypes.h>
  29. #include <rfb/clipboardTypes.h>
  30. #include <rfb/Exception.h>
  31. #include <rfb/ClientParams.h>
  32. #include <rfb/UpdateTracker.h>
  33. #include <rfb/Encoder.h>
  34. #include <rfb/SMsgWriter.h>
  35. #include <rfb/LogWriter.h>
  36. #include <rfb/ledStates.h>
  37. using namespace rfb;
  38. static LogWriter vlog("SMsgWriter");
  39. SMsgWriter::SMsgWriter(ClientParams* client_, rdr::OutStream* os_)
  40. : client(client_), os(os_),
  41. nRectsInUpdate(0), nRectsInHeader(0),
  42. needSetDesktopName(false), needCursor(false),
  43. needCursorPos(false), needLEDState(false),
  44. needQEMUKeyEvent(false)
  45. {
  46. }
  47. SMsgWriter::~SMsgWriter()
  48. {
  49. }
  50. void SMsgWriter::writeServerInit(uint16_t width, uint16_t height,
  51. const PixelFormat& pf, const char* name)
  52. {
  53. os->writeU16(width);
  54. os->writeU16(height);
  55. pf.write(os);
  56. os->writeU32(strlen(name));
  57. os->writeBytes((const uint8_t*)name, strlen(name));
  58. endMsg();
  59. }
  60. void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
  61. const uint16_t red[],
  62. const uint16_t green[],
  63. const uint16_t blue[])
  64. {
  65. startMsg(msgTypeSetColourMapEntries);
  66. os->pad(1);
  67. os->writeU16(firstColour);
  68. os->writeU16(nColours);
  69. for (int i = firstColour; i < firstColour+nColours; i++) {
  70. os->writeU16(red[i]);
  71. os->writeU16(green[i]);
  72. os->writeU16(blue[i]);
  73. }
  74. endMsg();
  75. }
  76. void SMsgWriter::writeBell()
  77. {
  78. startMsg(msgTypeBell);
  79. endMsg();
  80. }
  81. void SMsgWriter::writeServerCutText(const char* str)
  82. {
  83. size_t len;
  84. if (strchr(str, '\r') != NULL)
  85. throw Exception("Invalid carriage return in clipboard data");
  86. len = strlen(str);
  87. startMsg(msgTypeServerCutText);
  88. os->pad(3);
  89. os->writeU32(len);
  90. os->writeBytes((const uint8_t*)str, len);
  91. endMsg();
  92. }
  93. void SMsgWriter::writeClipboardCaps(uint32_t caps,
  94. const uint32_t* lengths)
  95. {
  96. size_t i, count;
  97. if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
  98. throw Exception("Client does not support extended clipboard");
  99. count = 0;
  100. for (i = 0;i < 16;i++) {
  101. if (caps & (1 << i))
  102. count++;
  103. }
  104. startMsg(msgTypeServerCutText);
  105. os->pad(3);
  106. os->writeS32(-(4 + 4 * count));
  107. os->writeU32(caps | clipboardCaps);
  108. count = 0;
  109. for (i = 0;i < 16;i++) {
  110. if (caps & (1 << i))
  111. os->writeU32(lengths[count++]);
  112. }
  113. endMsg();
  114. }
  115. void SMsgWriter::writeClipboardRequest(uint32_t flags)
  116. {
  117. if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
  118. throw Exception("Client does not support extended clipboard");
  119. if (!(client->clipboardFlags() & clipboardRequest))
  120. throw Exception("Client does not support clipboard \"request\" action");
  121. startMsg(msgTypeServerCutText);
  122. os->pad(3);
  123. os->writeS32(-4);
  124. os->writeU32(flags | clipboardRequest);
  125. endMsg();
  126. }
  127. void SMsgWriter::writeClipboardPeek(uint32_t flags)
  128. {
  129. if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
  130. throw Exception("Client does not support extended clipboard");
  131. if (!(client->clipboardFlags() & clipboardPeek))
  132. throw Exception("Client does not support clipboard \"peek\" action");
  133. startMsg(msgTypeServerCutText);
  134. os->pad(3);
  135. os->writeS32(-4);
  136. os->writeU32(flags | clipboardPeek);
  137. endMsg();
  138. }
  139. void SMsgWriter::writeClipboardNotify(uint32_t flags)
  140. {
  141. if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
  142. throw Exception("Client does not support extended clipboard");
  143. if (!(client->clipboardFlags() & clipboardNotify))
  144. throw Exception("Client does not support clipboard \"notify\" action");
  145. startMsg(msgTypeServerCutText);
  146. os->pad(3);
  147. os->writeS32(-4);
  148. os->writeU32(flags | clipboardNotify);
  149. endMsg();
  150. }
  151. void SMsgWriter::writeClipboardProvide(uint32_t flags,
  152. const size_t* lengths,
  153. const uint8_t* const* data)
  154. {
  155. rdr::MemOutStream mos;
  156. rdr::ZlibOutStream zos;
  157. int i, count;
  158. if (!client->supportsEncoding(pseudoEncodingExtendedClipboard))
  159. throw Exception("Client does not support extended clipboard");
  160. if (!(client->clipboardFlags() & clipboardProvide))
  161. throw Exception("Client does not support clipboard \"provide\" action");
  162. zos.setUnderlying(&mos);
  163. count = 0;
  164. for (i = 0;i < 16;i++) {
  165. if (!(flags & (1 << i)))
  166. continue;
  167. zos.writeU32(lengths[count]);
  168. zos.writeBytes(data[count], lengths[count]);
  169. count++;
  170. }
  171. zos.flush();
  172. startMsg(msgTypeServerCutText);
  173. os->pad(3);
  174. os->writeS32(-(4 + mos.length()));
  175. os->writeU32(flags | clipboardProvide);
  176. os->writeBytes(mos.data(), mos.length());
  177. endMsg();
  178. }
  179. void SMsgWriter::writeFence(uint32_t flags, unsigned len,
  180. const uint8_t data[])
  181. {
  182. if (!client->supportsEncoding(pseudoEncodingFence))
  183. throw Exception("Client does not support fences");
  184. if (len > 64)
  185. throw Exception("Too large fence payload");
  186. if ((flags & ~fenceFlagsSupported) != 0)
  187. throw Exception("Unknown fence flags");
  188. startMsg(msgTypeServerFence);
  189. os->pad(3);
  190. os->writeU32(flags);
  191. os->writeU8(len);
  192. if (len > 0)
  193. os->writeBytes(data, len);
  194. endMsg();
  195. }
  196. void SMsgWriter::writeEndOfContinuousUpdates()
  197. {
  198. if (!client->supportsEncoding(pseudoEncodingContinuousUpdates))
  199. throw Exception("Client does not support continuous updates");
  200. startMsg(msgTypeEndOfContinuousUpdates);
  201. endMsg();
  202. }
  203. void SMsgWriter::writeDesktopSize(uint16_t reason, uint16_t result)
  204. {
  205. ExtendedDesktopSizeMsg msg;
  206. if (!client->supportsEncoding(pseudoEncodingDesktopSize) &&
  207. !client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  208. throw Exception("Client does not support desktop size changes");
  209. msg.reason = reason;
  210. msg.result = result;
  211. extendedDesktopSizeMsgs.push_back(msg);
  212. }
  213. void SMsgWriter::writeSetDesktopName()
  214. {
  215. if (!client->supportsEncoding(pseudoEncodingDesktopName))
  216. throw Exception("Client does not support desktop name changes");
  217. needSetDesktopName = true;
  218. }
  219. void SMsgWriter::writeCursor()
  220. {
  221. if (!client->supportsEncoding(pseudoEncodingCursor) &&
  222. !client->supportsEncoding(pseudoEncodingXCursor) &&
  223. !client->supportsEncoding(pseudoEncodingCursorWithAlpha) &&
  224. !client->supportsEncoding(pseudoEncodingVMwareCursor))
  225. throw Exception("Client does not support local cursor");
  226. needCursor = true;
  227. }
  228. void SMsgWriter::writeCursorPos()
  229. {
  230. if (!client->supportsEncoding(pseudoEncodingVMwareCursorPosition))
  231. throw Exception("Client does not support cursor position");
  232. needCursorPos = true;
  233. }
  234. void SMsgWriter::writeLEDState()
  235. {
  236. if (!client->supportsEncoding(pseudoEncodingLEDState) &&
  237. !client->supportsEncoding(pseudoEncodingVMwareLEDState))
  238. throw Exception("Client does not support LED state");
  239. if (client->ledState() == ledUnknown)
  240. throw Exception("Server has not specified LED state");
  241. needLEDState = true;
  242. }
  243. void SMsgWriter::writeQEMUKeyEvent()
  244. {
  245. if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
  246. throw Exception("Client does not support QEMU key events");
  247. needQEMUKeyEvent = true;
  248. }
  249. bool SMsgWriter::needFakeUpdate()
  250. {
  251. if (needSetDesktopName)
  252. return true;
  253. if (needCursor)
  254. return true;
  255. if (needCursorPos)
  256. return true;
  257. if (needLEDState)
  258. return true;
  259. if (needQEMUKeyEvent)
  260. return true;
  261. if (needNoDataUpdate())
  262. return true;
  263. return false;
  264. }
  265. bool SMsgWriter::needNoDataUpdate()
  266. {
  267. if (!extendedDesktopSizeMsgs.empty())
  268. return true;
  269. return false;
  270. }
  271. void SMsgWriter::writeNoDataUpdate()
  272. {
  273. int nRects;
  274. nRects = 0;
  275. if (!extendedDesktopSizeMsgs.empty()) {
  276. if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  277. nRects += extendedDesktopSizeMsgs.size();
  278. else
  279. nRects++;
  280. }
  281. writeFramebufferUpdateStart(nRects);
  282. writeNoDataRects();
  283. writeFramebufferUpdateEnd();
  284. }
  285. void SMsgWriter::writeFramebufferUpdateStart(int nRects)
  286. {
  287. startMsg(msgTypeFramebufferUpdate);
  288. os->pad(1);
  289. if (nRects != 0xFFFF) {
  290. if (needSetDesktopName)
  291. nRects++;
  292. if (needCursor)
  293. nRects++;
  294. if (needCursorPos)
  295. nRects++;
  296. if (needLEDState)
  297. nRects++;
  298. if (needQEMUKeyEvent)
  299. nRects++;
  300. }
  301. os->writeU16(nRects);
  302. nRectsInUpdate = 0;
  303. if (nRects == 0xFFFF)
  304. nRectsInHeader = 0;
  305. else
  306. nRectsInHeader = nRects;
  307. writePseudoRects();
  308. }
  309. void SMsgWriter::writeFramebufferUpdateEnd()
  310. {
  311. if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
  312. throw Exception("SMsgWriter::writeFramebufferUpdateEnd: "
  313. "nRects out of sync");
  314. if (nRectsInHeader == 0) {
  315. // Send last rect. marker
  316. os->writeS16(0);
  317. os->writeS16(0);
  318. os->writeU16(0);
  319. os->writeU16(0);
  320. os->writeU32(pseudoEncodingLastRect);
  321. }
  322. endMsg();
  323. }
  324. void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
  325. {
  326. startRect(r,encodingCopyRect);
  327. os->writeU16(srcX);
  328. os->writeU16(srcY);
  329. endRect();
  330. }
  331. void SMsgWriter::startRect(const Rect& r, int encoding)
  332. {
  333. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  334. throw Exception("SMsgWriter::startRect: nRects out of sync");
  335. os->writeS16(r.tl.x);
  336. os->writeS16(r.tl.y);
  337. os->writeU16(r.width());
  338. os->writeU16(r.height());
  339. os->writeU32(encoding);
  340. }
  341. void SMsgWriter::endRect()
  342. {
  343. os->flush();
  344. }
  345. void SMsgWriter::startMsg(int type)
  346. {
  347. os->writeU8(type);
  348. }
  349. void SMsgWriter::endMsg()
  350. {
  351. os->flush();
  352. }
  353. void SMsgWriter::writePseudoRects()
  354. {
  355. if (needCursor) {
  356. const Cursor& cursor = client->cursor();
  357. if (client->supportsEncoding(pseudoEncodingCursorWithAlpha)) {
  358. writeSetCursorWithAlphaRect(cursor.width(), cursor.height(),
  359. cursor.hotspot().x, cursor.hotspot().y,
  360. cursor.getBuffer());
  361. } else if (client->supportsEncoding(pseudoEncodingVMwareCursor)) {
  362. writeSetVMwareCursorRect(cursor.width(), cursor.height(),
  363. cursor.hotspot().x, cursor.hotspot().y,
  364. cursor.getBuffer());
  365. } else if (client->supportsEncoding(pseudoEncodingCursor)) {
  366. size_t data_len = cursor.width()*cursor.height() *
  367. (client->pf().bpp/8);
  368. std::vector<uint8_t> data(data_len);
  369. std::vector<uint8_t> mask(cursor.getMask());
  370. const uint8_t* in;
  371. uint8_t* out;
  372. in = cursor.getBuffer();
  373. out = data.data();
  374. for (int i = 0;i < cursor.width()*cursor.height();i++) {
  375. client->pf().bufferFromRGB(out, in, 1);
  376. in += 4;
  377. out += client->pf().bpp/8;
  378. }
  379. writeSetCursorRect(cursor.width(), cursor.height(),
  380. cursor.hotspot().x, cursor.hotspot().y,
  381. data.data(), mask.data());
  382. } else if (client->supportsEncoding(pseudoEncodingXCursor)) {
  383. std::vector<uint8_t> bitmap(cursor.getBitmap());
  384. std::vector<uint8_t> mask(cursor.getMask());
  385. writeSetXCursorRect(cursor.width(), cursor.height(),
  386. cursor.hotspot().x, cursor.hotspot().y,
  387. bitmap.data(), mask.data());
  388. } else {
  389. throw Exception("Client does not support local cursor");
  390. }
  391. needCursor = false;
  392. }
  393. if (needCursorPos) {
  394. const Point& cursorPos = client->cursorPos();
  395. if (client->supportsEncoding(pseudoEncodingVMwareCursorPosition)) {
  396. writeSetVMwareCursorPositionRect(cursorPos.x, cursorPos.y);
  397. } else {
  398. throw Exception("Client does not support cursor position");
  399. }
  400. needCursorPos = false;
  401. }
  402. if (needSetDesktopName) {
  403. writeSetDesktopNameRect(client->name());
  404. needSetDesktopName = false;
  405. }
  406. if (needLEDState) {
  407. writeLEDStateRect(client->ledState());
  408. needLEDState = false;
  409. }
  410. if (needQEMUKeyEvent) {
  411. writeQEMUKeyEventRect();
  412. needQEMUKeyEvent = false;
  413. }
  414. }
  415. void SMsgWriter::writeNoDataRects()
  416. {
  417. if (!extendedDesktopSizeMsgs.empty()) {
  418. if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize)) {
  419. std::list<ExtendedDesktopSizeMsg>::const_iterator ri;
  420. for (ri = extendedDesktopSizeMsgs.begin();ri != extendedDesktopSizeMsgs.end();++ri) {
  421. // FIXME: We can probably skip multiple reasonServer entries
  422. writeExtendedDesktopSizeRect(ri->reason, ri->result,
  423. client->width(), client->height(),
  424. client->screenLayout());
  425. }
  426. } else if (client->supportsEncoding(pseudoEncodingDesktopSize)) {
  427. // Some clients assume this is the last rectangle so don't send anything
  428. // more after this
  429. writeSetDesktopSizeRect(client->width(), client->height());
  430. } else {
  431. throw Exception("Client does not support desktop size changes");
  432. }
  433. extendedDesktopSizeMsgs.clear();
  434. }
  435. }
  436. void SMsgWriter::writeSetDesktopSizeRect(int width, int height)
  437. {
  438. if (!client->supportsEncoding(pseudoEncodingDesktopSize))
  439. throw Exception("Client does not support desktop resize");
  440. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  441. throw Exception("SMsgWriter::writeSetDesktopSizeRect: nRects out of sync");
  442. os->writeS16(0);
  443. os->writeS16(0);
  444. os->writeU16(width);
  445. os->writeU16(height);
  446. os->writeU32(pseudoEncodingDesktopSize);
  447. }
  448. void SMsgWriter::writeExtendedDesktopSizeRect(uint16_t reason,
  449. uint16_t result,
  450. int fb_width,
  451. int fb_height,
  452. const ScreenSet& layout)
  453. {
  454. ScreenSet::const_iterator si;
  455. if (!client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  456. throw Exception("Client does not support extended desktop resize");
  457. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  458. throw Exception("SMsgWriter::writeExtendedDesktopSizeRect: nRects out of sync");
  459. os->writeU16(reason);
  460. os->writeU16(result);
  461. os->writeU16(fb_width);
  462. os->writeU16(fb_height);
  463. os->writeU32(pseudoEncodingExtendedDesktopSize);
  464. os->writeU8(layout.num_screens());
  465. os->pad(3);
  466. for (si = layout.begin();si != layout.end();++si) {
  467. os->writeU32(si->id);
  468. os->writeU16(si->dimensions.tl.x);
  469. os->writeU16(si->dimensions.tl.y);
  470. os->writeU16(si->dimensions.width());
  471. os->writeU16(si->dimensions.height());
  472. os->writeU32(si->flags);
  473. }
  474. }
  475. void SMsgWriter::writeSetDesktopNameRect(const char *name)
  476. {
  477. if (!client->supportsEncoding(pseudoEncodingDesktopName))
  478. throw Exception("Client does not support desktop rename");
  479. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  480. throw Exception("SMsgWriter::writeSetDesktopNameRect: nRects out of sync");
  481. os->writeS16(0);
  482. os->writeS16(0);
  483. os->writeU16(0);
  484. os->writeU16(0);
  485. os->writeU32(pseudoEncodingDesktopName);
  486. os->writeU32(strlen(name));
  487. os->writeBytes((const uint8_t*)name, strlen(name));
  488. }
  489. void SMsgWriter::writeSetCursorRect(int width, int height,
  490. int hotspotX, int hotspotY,
  491. const uint8_t* data,
  492. const uint8_t* mask)
  493. {
  494. if (!client->supportsEncoding(pseudoEncodingCursor))
  495. throw Exception("Client does not support local cursors");
  496. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  497. throw Exception("SMsgWriter::writeSetCursorRect: nRects out of sync");
  498. os->writeS16(hotspotX);
  499. os->writeS16(hotspotY);
  500. os->writeU16(width);
  501. os->writeU16(height);
  502. os->writeU32(pseudoEncodingCursor);
  503. os->writeBytes(data, width * height * (client->pf().bpp/8));
  504. os->writeBytes(mask, (width+7)/8 * height);
  505. }
  506. void SMsgWriter::writeSetXCursorRect(int width, int height,
  507. int hotspotX, int hotspotY,
  508. const uint8_t* data,
  509. const uint8_t* mask)
  510. {
  511. if (!client->supportsEncoding(pseudoEncodingXCursor))
  512. throw Exception("Client does not support local cursors");
  513. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  514. throw Exception("SMsgWriter::writeSetXCursorRect: nRects out of sync");
  515. os->writeS16(hotspotX);
  516. os->writeS16(hotspotY);
  517. os->writeU16(width);
  518. os->writeU16(height);
  519. os->writeU32(pseudoEncodingXCursor);
  520. if (width * height > 0) {
  521. os->writeU8(255);
  522. os->writeU8(255);
  523. os->writeU8(255);
  524. os->writeU8(0);
  525. os->writeU8(0);
  526. os->writeU8(0);
  527. os->writeBytes(data, (width+7)/8 * height);
  528. os->writeBytes(mask, (width+7)/8 * height);
  529. }
  530. }
  531. void SMsgWriter::writeSetCursorWithAlphaRect(int width, int height,
  532. int hotspotX, int hotspotY,
  533. const uint8_t* data)
  534. {
  535. if (!client->supportsEncoding(pseudoEncodingCursorWithAlpha))
  536. throw Exception("Client does not support local cursors");
  537. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  538. throw Exception("SMsgWriter::writeSetCursorWithAlphaRect: nRects out of sync");
  539. os->writeS16(hotspotX);
  540. os->writeS16(hotspotY);
  541. os->writeU16(width);
  542. os->writeU16(height);
  543. os->writeU32(pseudoEncodingCursorWithAlpha);
  544. // FIXME: Use an encoder with compression?
  545. os->writeU32(encodingRaw);
  546. // Alpha needs to be pre-multiplied
  547. for (int i = 0;i < width*height;i++) {
  548. os->writeU8((unsigned)data[0] * data[3] / 255);
  549. os->writeU8((unsigned)data[1] * data[3] / 255);
  550. os->writeU8((unsigned)data[2] * data[3] / 255);
  551. os->writeU8(data[3]);
  552. data += 4;
  553. }
  554. }
  555. void SMsgWriter::writeSetVMwareCursorRect(int width, int height,
  556. int hotspotX, int hotspotY,
  557. const uint8_t* data)
  558. {
  559. if (!client->supportsEncoding(pseudoEncodingVMwareCursor))
  560. throw Exception("Client does not support local cursors");
  561. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  562. throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");
  563. os->writeS16(hotspotX);
  564. os->writeS16(hotspotY);
  565. os->writeU16(width);
  566. os->writeU16(height);
  567. os->writeU32(pseudoEncodingVMwareCursor);
  568. os->writeU8(1); // Alpha cursor
  569. os->pad(1);
  570. // FIXME: Should alpha be premultiplied?
  571. os->writeBytes(data, width*height*4);
  572. }
  573. void SMsgWriter::writeSetVMwareCursorPositionRect(int hotspotX, int hotspotY)
  574. {
  575. if (!client->supportsEncoding(pseudoEncodingVMwareCursorPosition))
  576. throw Exception("Client does not support cursor position");
  577. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  578. throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");
  579. os->writeS16(hotspotX);
  580. os->writeS16(hotspotY);
  581. os->writeU16(0);
  582. os->writeU16(0);
  583. os->writeU32(pseudoEncodingVMwareCursorPosition);
  584. }
  585. void SMsgWriter::writeLEDStateRect(uint8_t state)
  586. {
  587. if (!client->supportsEncoding(pseudoEncodingLEDState) &&
  588. !client->supportsEncoding(pseudoEncodingVMwareLEDState))
  589. throw Exception("Client does not support LED state updates");
  590. if (client->ledState() == ledUnknown)
  591. throw Exception("Server does not support LED state updates");
  592. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  593. throw Exception("SMsgWriter::writeLEDStateRect: nRects out of sync");
  594. os->writeS16(0);
  595. os->writeS16(0);
  596. os->writeU16(0);
  597. os->writeU16(0);
  598. if (client->supportsEncoding(pseudoEncodingLEDState)) {
  599. os->writeU32(pseudoEncodingLEDState);
  600. os->writeU8(state);
  601. } else {
  602. os->writeU32(pseudoEncodingVMwareLEDState);
  603. os->writeU32(state);
  604. }
  605. }
  606. void SMsgWriter::writeQEMUKeyEventRect()
  607. {
  608. if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
  609. throw Exception("Client does not support QEMU extended key events");
  610. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  611. throw Exception("SMsgWriter::writeQEMUKeyEventRect: nRects out of sync");
  612. os->writeS16(0);
  613. os->writeS16(0);
  614. os->writeU16(0);
  615. os->writeU16(0);
  616. os->writeU32(pseudoEncodingQEMUKeyEvent);
  617. }