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

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