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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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(rdr::U16 width, rdr::U16 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(name, strlen(name));
  58. endMsg();
  59. }
  60. void SMsgWriter::writeSetColourMapEntries(int firstColour, int nColours,
  61. const rdr::U16 red[],
  62. const rdr::U16 green[],
  63. const rdr::U16 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(str, len);
  91. endMsg();
  92. }
  93. void SMsgWriter::writeClipboardCaps(rdr::U32 caps,
  94. const rdr::U32* 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(rdr::U32 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(rdr::U32 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(rdr::U32 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(rdr::U32 flags,
  152. const size_t* lengths,
  153. const rdr::U8* 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(rdr::U32 flags, unsigned len, const char data[])
  180. {
  181. if (!client->supportsEncoding(pseudoEncodingFence))
  182. throw Exception("Client does not support fences");
  183. if (len > 64)
  184. throw Exception("Too large fence payload");
  185. if ((flags & ~fenceFlagsSupported) != 0)
  186. throw Exception("Unknown fence flags");
  187. startMsg(msgTypeServerFence);
  188. os->pad(3);
  189. os->writeU32(flags);
  190. os->writeU8(len);
  191. if (len > 0)
  192. os->writeBytes(data, len);
  193. endMsg();
  194. }
  195. void SMsgWriter::writeEndOfContinuousUpdates()
  196. {
  197. if (!client->supportsEncoding(pseudoEncodingContinuousUpdates))
  198. throw Exception("Client does not support continuous updates");
  199. startMsg(msgTypeEndOfContinuousUpdates);
  200. endMsg();
  201. }
  202. void SMsgWriter::writeDesktopSize(rdr::U16 reason, rdr::U16 result)
  203. {
  204. ExtendedDesktopSizeMsg msg;
  205. if (!client->supportsEncoding(pseudoEncodingDesktopSize) &&
  206. !client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  207. throw Exception("Client does not support desktop size changes");
  208. msg.reason = reason;
  209. msg.result = result;
  210. extendedDesktopSizeMsgs.push_back(msg);
  211. }
  212. void SMsgWriter::writeSetDesktopName()
  213. {
  214. if (!client->supportsEncoding(pseudoEncodingDesktopName))
  215. throw Exception("Client does not support desktop name changes");
  216. needSetDesktopName = true;
  217. }
  218. void SMsgWriter::writeCursor()
  219. {
  220. if (!client->supportsEncoding(pseudoEncodingCursor) &&
  221. !client->supportsEncoding(pseudoEncodingXCursor) &&
  222. !client->supportsEncoding(pseudoEncodingCursorWithAlpha) &&
  223. !client->supportsEncoding(pseudoEncodingVMwareCursor))
  224. throw Exception("Client does not support local cursor");
  225. needCursor = true;
  226. }
  227. void SMsgWriter::writeCursorPos()
  228. {
  229. if (!client->supportsEncoding(pseudoEncodingVMwareCursorPosition))
  230. throw Exception("Client does not support cursor position");
  231. needCursorPos = true;
  232. }
  233. void SMsgWriter::writeLEDState()
  234. {
  235. if (!client->supportsEncoding(pseudoEncodingLEDState) &&
  236. !client->supportsEncoding(pseudoEncodingVMwareLEDState))
  237. throw Exception("Client does not support LED state");
  238. if (client->ledState() == ledUnknown)
  239. throw Exception("Server has not specified LED state");
  240. needLEDState = true;
  241. }
  242. void SMsgWriter::writeQEMUKeyEvent()
  243. {
  244. if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
  245. throw Exception("Client does not support QEMU key events");
  246. needQEMUKeyEvent = true;
  247. }
  248. bool SMsgWriter::needFakeUpdate()
  249. {
  250. if (needSetDesktopName)
  251. return true;
  252. if (needCursor)
  253. return true;
  254. if (needCursorPos)
  255. return true;
  256. if (needLEDState)
  257. return true;
  258. if (needQEMUKeyEvent)
  259. return true;
  260. if (needNoDataUpdate())
  261. return true;
  262. return false;
  263. }
  264. bool SMsgWriter::needNoDataUpdate()
  265. {
  266. if (!extendedDesktopSizeMsgs.empty())
  267. return true;
  268. return false;
  269. }
  270. void SMsgWriter::writeNoDataUpdate()
  271. {
  272. int nRects;
  273. nRects = 0;
  274. if (!extendedDesktopSizeMsgs.empty()) {
  275. if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  276. nRects += extendedDesktopSizeMsgs.size();
  277. else
  278. nRects++;
  279. }
  280. writeFramebufferUpdateStart(nRects);
  281. writeNoDataRects();
  282. writeFramebufferUpdateEnd();
  283. }
  284. void SMsgWriter::writeFramebufferUpdateStart(int nRects)
  285. {
  286. startMsg(msgTypeFramebufferUpdate);
  287. os->pad(1);
  288. if (nRects != 0xFFFF) {
  289. if (needSetDesktopName)
  290. nRects++;
  291. if (needCursor)
  292. nRects++;
  293. if (needCursorPos)
  294. nRects++;
  295. if (needLEDState)
  296. nRects++;
  297. if (needQEMUKeyEvent)
  298. nRects++;
  299. }
  300. os->writeU16(nRects);
  301. nRectsInUpdate = 0;
  302. if (nRects == 0xFFFF)
  303. nRectsInHeader = 0;
  304. else
  305. nRectsInHeader = nRects;
  306. writePseudoRects();
  307. }
  308. void SMsgWriter::writeFramebufferUpdateEnd()
  309. {
  310. if (nRectsInUpdate != nRectsInHeader && nRectsInHeader)
  311. throw Exception("SMsgWriter::writeFramebufferUpdateEnd: "
  312. "nRects out of sync");
  313. if (nRectsInHeader == 0) {
  314. // Send last rect. marker
  315. os->writeS16(0);
  316. os->writeS16(0);
  317. os->writeU16(0);
  318. os->writeU16(0);
  319. os->writeU32(pseudoEncodingLastRect);
  320. }
  321. endMsg();
  322. }
  323. void SMsgWriter::writeCopyRect(const Rect& r, int srcX, int srcY)
  324. {
  325. startRect(r,encodingCopyRect);
  326. os->writeU16(srcX);
  327. os->writeU16(srcY);
  328. endRect();
  329. }
  330. void SMsgWriter::startRect(const Rect& r, int encoding)
  331. {
  332. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  333. throw Exception("SMsgWriter::startRect: nRects out of sync");
  334. os->writeS16(r.tl.x);
  335. os->writeS16(r.tl.y);
  336. os->writeU16(r.width());
  337. os->writeU16(r.height());
  338. os->writeU32(encoding);
  339. }
  340. void SMsgWriter::endRect()
  341. {
  342. os->flush();
  343. }
  344. void SMsgWriter::startMsg(int type)
  345. {
  346. os->writeU8(type);
  347. }
  348. void SMsgWriter::endMsg()
  349. {
  350. os->flush();
  351. }
  352. void SMsgWriter::writePseudoRects()
  353. {
  354. if (needCursor) {
  355. const Cursor& cursor = client->cursor();
  356. if (client->supportsEncoding(pseudoEncodingCursorWithAlpha)) {
  357. writeSetCursorWithAlphaRect(cursor.width(), cursor.height(),
  358. cursor.hotspot().x, cursor.hotspot().y,
  359. cursor.getBuffer());
  360. } else if (client->supportsEncoding(pseudoEncodingVMwareCursor)) {
  361. writeSetVMwareCursorRect(cursor.width(), cursor.height(),
  362. cursor.hotspot().x, cursor.hotspot().y,
  363. cursor.getBuffer());
  364. } else if (client->supportsEncoding(pseudoEncodingCursor)) {
  365. rdr::U8Array data(cursor.width()*cursor.height() * (client->pf().bpp/8));
  366. rdr::U8Array mask(cursor.getMask());
  367. const rdr::U8* in;
  368. rdr::U8* out;
  369. in = cursor.getBuffer();
  370. out = data.buf;
  371. for (int i = 0;i < cursor.width()*cursor.height();i++) {
  372. client->pf().bufferFromRGB(out, in, 1);
  373. in += 4;
  374. out += client->pf().bpp/8;
  375. }
  376. writeSetCursorRect(cursor.width(), cursor.height(),
  377. cursor.hotspot().x, cursor.hotspot().y,
  378. data.buf, mask.buf);
  379. } else if (client->supportsEncoding(pseudoEncodingXCursor)) {
  380. rdr::U8Array bitmap(cursor.getBitmap());
  381. rdr::U8Array mask(cursor.getMask());
  382. writeSetXCursorRect(cursor.width(), cursor.height(),
  383. cursor.hotspot().x, cursor.hotspot().y,
  384. bitmap.buf, mask.buf);
  385. } else {
  386. throw Exception("Client does not support local cursor");
  387. }
  388. needCursor = false;
  389. }
  390. if (needCursorPos) {
  391. const Point& cursorPos = client->cursorPos();
  392. if (client->supportsEncoding(pseudoEncodingVMwareCursorPosition)) {
  393. writeSetVMwareCursorPositionRect(cursorPos.x, cursorPos.y);
  394. } else {
  395. throw Exception("Client does not support cursor position");
  396. }
  397. needCursorPos = false;
  398. }
  399. if (needSetDesktopName) {
  400. writeSetDesktopNameRect(client->name());
  401. needSetDesktopName = false;
  402. }
  403. if (needLEDState) {
  404. writeLEDStateRect(client->ledState());
  405. needLEDState = false;
  406. }
  407. if (needQEMUKeyEvent) {
  408. writeQEMUKeyEventRect();
  409. needQEMUKeyEvent = false;
  410. }
  411. }
  412. void SMsgWriter::writeNoDataRects()
  413. {
  414. if (!extendedDesktopSizeMsgs.empty()) {
  415. if (client->supportsEncoding(pseudoEncodingExtendedDesktopSize)) {
  416. std::list<ExtendedDesktopSizeMsg>::const_iterator ri;
  417. for (ri = extendedDesktopSizeMsgs.begin();ri != extendedDesktopSizeMsgs.end();++ri) {
  418. // FIXME: We can probably skip multiple reasonServer entries
  419. writeExtendedDesktopSizeRect(ri->reason, ri->result,
  420. client->width(), client->height(),
  421. client->screenLayout());
  422. }
  423. } else if (client->supportsEncoding(pseudoEncodingDesktopSize)) {
  424. // Some clients assume this is the last rectangle so don't send anything
  425. // more after this
  426. writeSetDesktopSizeRect(client->width(), client->height());
  427. } else {
  428. throw Exception("Client does not support desktop size changes");
  429. }
  430. extendedDesktopSizeMsgs.clear();
  431. }
  432. }
  433. void SMsgWriter::writeSetDesktopSizeRect(int width, int height)
  434. {
  435. if (!client->supportsEncoding(pseudoEncodingDesktopSize))
  436. throw Exception("Client does not support desktop resize");
  437. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  438. throw Exception("SMsgWriter::writeSetDesktopSizeRect: nRects out of sync");
  439. os->writeS16(0);
  440. os->writeS16(0);
  441. os->writeU16(width);
  442. os->writeU16(height);
  443. os->writeU32(pseudoEncodingDesktopSize);
  444. }
  445. void SMsgWriter::writeExtendedDesktopSizeRect(rdr::U16 reason,
  446. rdr::U16 result,
  447. int fb_width,
  448. int fb_height,
  449. const ScreenSet& layout)
  450. {
  451. ScreenSet::const_iterator si;
  452. if (!client->supportsEncoding(pseudoEncodingExtendedDesktopSize))
  453. throw Exception("Client does not support extended desktop resize");
  454. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  455. throw Exception("SMsgWriter::writeExtendedDesktopSizeRect: nRects out of sync");
  456. os->writeU16(reason);
  457. os->writeU16(result);
  458. os->writeU16(fb_width);
  459. os->writeU16(fb_height);
  460. os->writeU32(pseudoEncodingExtendedDesktopSize);
  461. os->writeU8(layout.num_screens());
  462. os->pad(3);
  463. for (si = layout.begin();si != layout.end();++si) {
  464. os->writeU32(si->id);
  465. os->writeU16(si->dimensions.tl.x);
  466. os->writeU16(si->dimensions.tl.y);
  467. os->writeU16(si->dimensions.width());
  468. os->writeU16(si->dimensions.height());
  469. os->writeU32(si->flags);
  470. }
  471. }
  472. void SMsgWriter::writeSetDesktopNameRect(const char *name)
  473. {
  474. if (!client->supportsEncoding(pseudoEncodingDesktopName))
  475. throw Exception("Client does not support desktop rename");
  476. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  477. throw Exception("SMsgWriter::writeSetDesktopNameRect: nRects out of sync");
  478. os->writeS16(0);
  479. os->writeS16(0);
  480. os->writeU16(0);
  481. os->writeU16(0);
  482. os->writeU32(pseudoEncodingDesktopName);
  483. os->writeU32(strlen(name));
  484. os->writeBytes(name, strlen(name));
  485. }
  486. void SMsgWriter::writeSetCursorRect(int width, int height,
  487. int hotspotX, int hotspotY,
  488. const void* data, const void* mask)
  489. {
  490. if (!client->supportsEncoding(pseudoEncodingCursor))
  491. throw Exception("Client does not support local cursors");
  492. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  493. throw Exception("SMsgWriter::writeSetCursorRect: nRects out of sync");
  494. os->writeS16(hotspotX);
  495. os->writeS16(hotspotY);
  496. os->writeU16(width);
  497. os->writeU16(height);
  498. os->writeU32(pseudoEncodingCursor);
  499. os->writeBytes(data, width * height * (client->pf().bpp/8));
  500. os->writeBytes(mask, (width+7)/8 * height);
  501. }
  502. void SMsgWriter::writeSetXCursorRect(int width, int height,
  503. int hotspotX, int hotspotY,
  504. const void* data, const void* mask)
  505. {
  506. if (!client->supportsEncoding(pseudoEncodingXCursor))
  507. throw Exception("Client does not support local cursors");
  508. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  509. throw Exception("SMsgWriter::writeSetXCursorRect: nRects out of sync");
  510. os->writeS16(hotspotX);
  511. os->writeS16(hotspotY);
  512. os->writeU16(width);
  513. os->writeU16(height);
  514. os->writeU32(pseudoEncodingXCursor);
  515. if (width * height > 0) {
  516. os->writeU8(255);
  517. os->writeU8(255);
  518. os->writeU8(255);
  519. os->writeU8(0);
  520. os->writeU8(0);
  521. os->writeU8(0);
  522. os->writeBytes(data, (width+7)/8 * height);
  523. os->writeBytes(mask, (width+7)/8 * height);
  524. }
  525. }
  526. void SMsgWriter::writeSetCursorWithAlphaRect(int width, int height,
  527. int hotspotX, int hotspotY,
  528. const rdr::U8* data)
  529. {
  530. if (!client->supportsEncoding(pseudoEncodingCursorWithAlpha))
  531. throw Exception("Client does not support local cursors");
  532. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  533. throw Exception("SMsgWriter::writeSetCursorWithAlphaRect: nRects out of sync");
  534. os->writeS16(hotspotX);
  535. os->writeS16(hotspotY);
  536. os->writeU16(width);
  537. os->writeU16(height);
  538. os->writeU32(pseudoEncodingCursorWithAlpha);
  539. // FIXME: Use an encoder with compression?
  540. os->writeU32(encodingRaw);
  541. // Alpha needs to be pre-multiplied
  542. for (int i = 0;i < width*height;i++) {
  543. os->writeU8((unsigned)data[0] * data[3] / 255);
  544. os->writeU8((unsigned)data[1] * data[3] / 255);
  545. os->writeU8((unsigned)data[2] * data[3] / 255);
  546. os->writeU8(data[3]);
  547. data += 4;
  548. }
  549. }
  550. void SMsgWriter::writeSetVMwareCursorRect(int width, int height,
  551. int hotspotX, int hotspotY,
  552. const rdr::U8* data)
  553. {
  554. if (!client->supportsEncoding(pseudoEncodingVMwareCursor))
  555. throw Exception("Client does not support local cursors");
  556. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  557. throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");
  558. os->writeS16(hotspotX);
  559. os->writeS16(hotspotY);
  560. os->writeU16(width);
  561. os->writeU16(height);
  562. os->writeU32(pseudoEncodingVMwareCursor);
  563. os->writeU8(1); // Alpha cursor
  564. os->pad(1);
  565. // FIXME: Should alpha be premultiplied?
  566. os->writeBytes(data, width*height*4);
  567. }
  568. void SMsgWriter::writeSetVMwareCursorPositionRect(int hotspotX, int hotspotY)
  569. {
  570. if (!client->supportsEncoding(pseudoEncodingVMwareCursorPosition))
  571. throw Exception("Client does not support cursor position");
  572. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  573. throw Exception("SMsgWriter::writeSetVMwareCursorRect: nRects out of sync");
  574. os->writeS16(hotspotX);
  575. os->writeS16(hotspotY);
  576. os->writeU16(0);
  577. os->writeU16(0);
  578. os->writeU32(pseudoEncodingVMwareCursorPosition);
  579. }
  580. void SMsgWriter::writeLEDStateRect(rdr::U8 state)
  581. {
  582. if (!client->supportsEncoding(pseudoEncodingLEDState) &&
  583. !client->supportsEncoding(pseudoEncodingVMwareLEDState))
  584. throw Exception("Client does not support LED state updates");
  585. if (client->ledState() == ledUnknown)
  586. throw Exception("Server does not support LED state updates");
  587. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  588. throw Exception("SMsgWriter::writeLEDStateRect: nRects out of sync");
  589. os->writeS16(0);
  590. os->writeS16(0);
  591. os->writeU16(0);
  592. os->writeU16(0);
  593. if (client->supportsEncoding(pseudoEncodingLEDState)) {
  594. os->writeU32(pseudoEncodingLEDState);
  595. os->writeU8(state);
  596. } else {
  597. os->writeU32(pseudoEncodingVMwareLEDState);
  598. os->writeU32(state);
  599. }
  600. }
  601. void SMsgWriter::writeQEMUKeyEventRect()
  602. {
  603. if (!client->supportsEncoding(pseudoEncodingQEMUKeyEvent))
  604. throw Exception("Client does not support QEMU extended key events");
  605. if (++nRectsInUpdate > nRectsInHeader && nRectsInHeader)
  606. throw Exception("SMsgWriter::writeQEMUKeyEventRect: nRects out of sync");
  607. os->writeS16(0);
  608. os->writeS16(0);
  609. os->writeU16(0);
  610. os->writeU16(0);
  611. os->writeU32(pseudoEncodingQEMUKeyEvent);
  612. }