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.

CConnection.cxx 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <rfb/Exception.h>
  26. #include <rfb/clipboardTypes.h>
  27. #include <rfb/fenceTypes.h>
  28. #include <rfb/CMsgReader.h>
  29. #include <rfb/CMsgWriter.h>
  30. #include <rfb/CSecurity.h>
  31. #include <rfb/Decoder.h>
  32. #include <rfb/Security.h>
  33. #include <rfb/SecurityClient.h>
  34. #include <rfb/CConnection.h>
  35. #include <rfb/util.h>
  36. #include <rfb/LogWriter.h>
  37. #include <rdr/InStream.h>
  38. #include <rdr/OutStream.h>
  39. using namespace rfb;
  40. static LogWriter vlog("CConnection");
  41. CConnection::CConnection()
  42. : csecurity(0),
  43. supportsLocalCursor(false), supportsCursorPosition(false),
  44. supportsDesktopResize(false), supportsLEDState(false),
  45. is(0), os(0), reader_(0), writer_(0),
  46. shared(false),
  47. state_(RFBSTATE_UNINITIALISED),
  48. pendingPFChange(false), preferredEncoding(encodingTight),
  49. compressLevel(2), qualityLevel(-1),
  50. formatChange(false), encodingChange(false),
  51. firstUpdate(true), pendingUpdate(false), continuousUpdates(false),
  52. forceNonincremental(true),
  53. framebuffer(NULL), decoder(this),
  54. hasRemoteClipboard(false), hasLocalClipboard(false)
  55. {
  56. }
  57. CConnection::~CConnection()
  58. {
  59. close();
  60. }
  61. void CConnection::setServerName(const char* name_)
  62. {
  63. if (name_ == NULL)
  64. name_ = "";
  65. serverName = name_;
  66. }
  67. void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
  68. {
  69. is = is_;
  70. os = os_;
  71. }
  72. void CConnection::setFramebuffer(ModifiablePixelBuffer* fb)
  73. {
  74. decoder.flush();
  75. if (fb) {
  76. assert(fb->width() == server.width());
  77. assert(fb->height() == server.height());
  78. }
  79. if ((framebuffer != NULL) && (fb != NULL)) {
  80. Rect rect;
  81. const uint8_t* data;
  82. int stride;
  83. const uint8_t black[4] = { 0, 0, 0, 0 };
  84. // Copy still valid area
  85. rect.setXYWH(0, 0,
  86. __rfbmin(fb->width(), framebuffer->width()),
  87. __rfbmin(fb->height(), framebuffer->height()));
  88. data = framebuffer->getBuffer(framebuffer->getRect(), &stride);
  89. fb->imageRect(rect, data, stride);
  90. // Black out any new areas
  91. if (fb->width() > framebuffer->width()) {
  92. rect.setXYWH(framebuffer->width(), 0,
  93. fb->width() - framebuffer->width(),
  94. fb->height());
  95. fb->fillRect(rect, black);
  96. }
  97. if (fb->height() > framebuffer->height()) {
  98. rect.setXYWH(0, framebuffer->height(),
  99. fb->width(),
  100. fb->height() - framebuffer->height());
  101. fb->fillRect(rect, black);
  102. }
  103. }
  104. delete framebuffer;
  105. framebuffer = fb;
  106. }
  107. void CConnection::initialiseProtocol()
  108. {
  109. state_ = RFBSTATE_PROTOCOL_VERSION;
  110. }
  111. bool CConnection::processMsg()
  112. {
  113. switch (state_) {
  114. case RFBSTATE_PROTOCOL_VERSION: return processVersionMsg(); break;
  115. case RFBSTATE_SECURITY_TYPES: return processSecurityTypesMsg(); break;
  116. case RFBSTATE_SECURITY: return processSecurityMsg(); break;
  117. case RFBSTATE_SECURITY_RESULT: return processSecurityResultMsg(); break;
  118. case RFBSTATE_SECURITY_REASON: return processSecurityReasonMsg(); break;
  119. case RFBSTATE_INITIALISATION: return processInitMsg(); break;
  120. case RFBSTATE_NORMAL: return reader_->readMsg(); break;
  121. case RFBSTATE_CLOSING:
  122. throw Exception("CConnection::processMsg: called while closing");
  123. case RFBSTATE_UNINITIALISED:
  124. throw Exception("CConnection::processMsg: not initialised yet?");
  125. default:
  126. throw Exception("CConnection::processMsg: invalid state");
  127. }
  128. }
  129. bool CConnection::processVersionMsg()
  130. {
  131. char verStr[27]; // FIXME: gcc has some bug in format-overflow
  132. int majorVersion;
  133. int minorVersion;
  134. vlog.debug("reading protocol version");
  135. if (!is->hasData(12))
  136. return false;
  137. is->readBytes((uint8_t*)verStr, 12);
  138. verStr[12] = '\0';
  139. if (sscanf(verStr, "RFB %03d.%03d\n",
  140. &majorVersion, &minorVersion) != 2) {
  141. state_ = RFBSTATE_INVALID;
  142. throw Exception("reading version failed: not an RFB server?");
  143. }
  144. server.setVersion(majorVersion, minorVersion);
  145. vlog.info("Server supports RFB protocol version %d.%d",
  146. server.majorVersion, server.minorVersion);
  147. // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8
  148. if (server.beforeVersion(3,3)) {
  149. vlog.error("Server gave unsupported RFB protocol version %d.%d",
  150. server.majorVersion, server.minorVersion);
  151. state_ = RFBSTATE_INVALID;
  152. throw Exception("Server gave unsupported RFB protocol version %d.%d",
  153. server.majorVersion, server.minorVersion);
  154. } else if (server.beforeVersion(3,7)) {
  155. server.setVersion(3,3);
  156. } else if (server.afterVersion(3,8)) {
  157. server.setVersion(3,8);
  158. }
  159. sprintf(verStr, "RFB %03d.%03d\n",
  160. server.majorVersion, server.minorVersion);
  161. os->writeBytes((const uint8_t*)verStr, 12);
  162. os->flush();
  163. state_ = RFBSTATE_SECURITY_TYPES;
  164. vlog.info("Using RFB protocol version %d.%d",
  165. server.majorVersion, server.minorVersion);
  166. return true;
  167. }
  168. bool CConnection::processSecurityTypesMsg()
  169. {
  170. vlog.debug("processing security types message");
  171. int secType = secTypeInvalid;
  172. std::list<uint8_t> secTypes;
  173. secTypes = security.GetEnabledSecTypes();
  174. if (server.isVersion(3,3)) {
  175. // legacy 3.3 server may only offer "vnc authentication" or "none"
  176. if (!is->hasData(4))
  177. return false;
  178. secType = is->readU32();
  179. if (secType == secTypeInvalid) {
  180. state_ = RFBSTATE_SECURITY_REASON;
  181. return true;
  182. } else if (secType == secTypeNone || secType == secTypeVncAuth) {
  183. std::list<uint8_t>::iterator i;
  184. for (i = secTypes.begin(); i != secTypes.end(); i++)
  185. if (*i == secType) {
  186. secType = *i;
  187. break;
  188. }
  189. if (i == secTypes.end())
  190. secType = secTypeInvalid;
  191. } else {
  192. vlog.error("Unknown 3.3 security type %d", secType);
  193. throw Exception("Unknown 3.3 security type");
  194. }
  195. } else {
  196. // >=3.7 server will offer us a list
  197. if (!is->hasData(1))
  198. return false;
  199. is->setRestorePoint();
  200. int nServerSecTypes = is->readU8();
  201. if (!is->hasDataOrRestore(nServerSecTypes))
  202. return false;
  203. is->clearRestorePoint();
  204. if (nServerSecTypes == 0) {
  205. state_ = RFBSTATE_SECURITY_REASON;
  206. return true;
  207. }
  208. std::list<uint8_t>::iterator j;
  209. for (int i = 0; i < nServerSecTypes; i++) {
  210. uint8_t serverSecType = is->readU8();
  211. vlog.debug("Server offers security type %s(%d)",
  212. secTypeName(serverSecType), serverSecType);
  213. /*
  214. * Use the first type sent by server which matches client's type.
  215. * It means server's order specifies priority.
  216. */
  217. if (secType == secTypeInvalid) {
  218. for (j = secTypes.begin(); j != secTypes.end(); j++)
  219. if (*j == serverSecType) {
  220. secType = *j;
  221. break;
  222. }
  223. }
  224. }
  225. // Inform the server of our decision
  226. if (secType != secTypeInvalid) {
  227. os->writeU8(secType);
  228. os->flush();
  229. vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType);
  230. }
  231. }
  232. if (secType == secTypeInvalid) {
  233. state_ = RFBSTATE_INVALID;
  234. vlog.error("No matching security types");
  235. throw Exception("No matching security types");
  236. }
  237. state_ = RFBSTATE_SECURITY;
  238. csecurity = security.GetCSecurity(this, secType);
  239. return true;
  240. }
  241. bool CConnection::processSecurityMsg()
  242. {
  243. vlog.debug("processing security message");
  244. if (!csecurity->processMsg())
  245. return false;
  246. state_ = RFBSTATE_SECURITY_RESULT;
  247. return true;
  248. }
  249. bool CConnection::processSecurityResultMsg()
  250. {
  251. vlog.debug("processing security result message");
  252. int result;
  253. if (server.beforeVersion(3,8) && csecurity->getType() == secTypeNone) {
  254. result = secResultOK;
  255. } else {
  256. if (!is->hasData(4))
  257. return false;
  258. result = is->readU32();
  259. }
  260. switch (result) {
  261. case secResultOK:
  262. securityCompleted();
  263. return true;
  264. case secResultFailed:
  265. vlog.debug("auth failed");
  266. break;
  267. case secResultTooMany:
  268. vlog.debug("auth failed - too many tries");
  269. break;
  270. default:
  271. throw Exception("Unknown security result from server");
  272. }
  273. if (server.beforeVersion(3,8)) {
  274. state_ = RFBSTATE_INVALID;
  275. throw AuthFailureException();
  276. }
  277. state_ = RFBSTATE_SECURITY_REASON;
  278. return true;
  279. }
  280. bool CConnection::processSecurityReasonMsg()
  281. {
  282. vlog.debug("processing security reason message");
  283. if (!is->hasData(4))
  284. return false;
  285. is->setRestorePoint();
  286. uint32_t len = is->readU32();
  287. if (!is->hasDataOrRestore(len))
  288. return false;
  289. is->clearRestorePoint();
  290. std::vector<char> reason(len + 1);
  291. is->readBytes((uint8_t*)reason.data(), len);
  292. reason[len] = '\0';
  293. state_ = RFBSTATE_INVALID;
  294. throw AuthFailureException(reason.data());
  295. }
  296. bool CConnection::processInitMsg()
  297. {
  298. vlog.debug("reading server initialisation");
  299. return reader_->readServerInit();
  300. }
  301. void CConnection::securityCompleted()
  302. {
  303. state_ = RFBSTATE_INITIALISATION;
  304. reader_ = new CMsgReader(this, is);
  305. writer_ = new CMsgWriter(&server, os);
  306. vlog.debug("Authentication success!");
  307. authSuccess();
  308. writer_->writeClientInit(shared);
  309. }
  310. void CConnection::close()
  311. {
  312. state_ = RFBSTATE_CLOSING;
  313. /*
  314. * We're already shutting down, so just log any pending decoder
  315. * problems
  316. */
  317. try {
  318. decoder.flush();
  319. } catch (rdr::Exception& e) {
  320. vlog.error("%s", e.str());
  321. }
  322. setFramebuffer(NULL);
  323. delete csecurity;
  324. csecurity = NULL;
  325. delete reader_;
  326. reader_ = NULL;
  327. delete writer_;
  328. writer_ = NULL;
  329. }
  330. void CConnection::setDesktopSize(int w, int h)
  331. {
  332. decoder.flush();
  333. CMsgHandler::setDesktopSize(w,h);
  334. if (continuousUpdates)
  335. writer()->writeEnableContinuousUpdates(true, 0, 0,
  336. server.width(),
  337. server.height());
  338. resizeFramebuffer();
  339. assert(framebuffer != NULL);
  340. assert(framebuffer->width() == server.width());
  341. assert(framebuffer->height() == server.height());
  342. }
  343. void CConnection::setExtendedDesktopSize(unsigned reason,
  344. unsigned result,
  345. int w, int h,
  346. const ScreenSet& layout)
  347. {
  348. decoder.flush();
  349. CMsgHandler::setExtendedDesktopSize(reason, result, w, h, layout);
  350. if (continuousUpdates)
  351. writer()->writeEnableContinuousUpdates(true, 0, 0,
  352. server.width(),
  353. server.height());
  354. resizeFramebuffer();
  355. assert(framebuffer != NULL);
  356. assert(framebuffer->width() == server.width());
  357. assert(framebuffer->height() == server.height());
  358. }
  359. void CConnection::endOfContinuousUpdates()
  360. {
  361. CMsgHandler::endOfContinuousUpdates();
  362. // We've gotten the marker for a format change, so make the pending
  363. // one active
  364. if (pendingPFChange) {
  365. server.setPF(pendingPF);
  366. pendingPFChange = false;
  367. // We might have another change pending
  368. if (formatChange)
  369. requestNewUpdate();
  370. }
  371. }
  372. void CConnection::serverInit(int width, int height,
  373. const PixelFormat& pf,
  374. const char* name)
  375. {
  376. CMsgHandler::serverInit(width, height, pf, name);
  377. state_ = RFBSTATE_NORMAL;
  378. vlog.debug("initialisation done");
  379. initDone();
  380. assert(framebuffer != NULL);
  381. assert(framebuffer->width() == server.width());
  382. assert(framebuffer->height() == server.height());
  383. // We want to make sure we call SetEncodings at least once
  384. encodingChange = true;
  385. requestNewUpdate();
  386. // This initial update request is a bit of a corner case, so we need
  387. // to help out setting the correct format here.
  388. if (pendingPFChange) {
  389. server.setPF(pendingPF);
  390. pendingPFChange = false;
  391. }
  392. }
  393. bool CConnection::readAndDecodeRect(const Rect& r, int encoding,
  394. ModifiablePixelBuffer* pb)
  395. {
  396. if (!decoder.decodeRect(r, encoding, pb))
  397. return false;
  398. decoder.flush();
  399. return true;
  400. }
  401. void CConnection::framebufferUpdateStart()
  402. {
  403. CMsgHandler::framebufferUpdateStart();
  404. assert(framebuffer != NULL);
  405. // Note: This might not be true if continuous updates are supported
  406. pendingUpdate = false;
  407. requestNewUpdate();
  408. }
  409. void CConnection::framebufferUpdateEnd()
  410. {
  411. decoder.flush();
  412. CMsgHandler::framebufferUpdateEnd();
  413. // A format change has been scheduled and we are now past the update
  414. // with the old format. Time to active the new one.
  415. if (pendingPFChange && !continuousUpdates) {
  416. server.setPF(pendingPF);
  417. pendingPFChange = false;
  418. }
  419. if (firstUpdate) {
  420. if (server.supportsContinuousUpdates) {
  421. vlog.info("Enabling continuous updates");
  422. continuousUpdates = true;
  423. writer()->writeEnableContinuousUpdates(true, 0, 0,
  424. server.width(),
  425. server.height());
  426. }
  427. firstUpdate = false;
  428. }
  429. }
  430. bool CConnection::dataRect(const Rect& r, int encoding)
  431. {
  432. return decoder.decodeRect(r, encoding, framebuffer);
  433. }
  434. void CConnection::serverCutText(const char* str)
  435. {
  436. hasLocalClipboard = false;
  437. serverClipboard = str;
  438. hasRemoteClipboard = true;
  439. handleClipboardAnnounce(true);
  440. }
  441. void CConnection::handleClipboardCaps(uint32_t flags,
  442. const uint32_t* lengths)
  443. {
  444. uint32_t sizes[] = { 0 };
  445. CMsgHandler::handleClipboardCaps(flags, lengths);
  446. writer()->writeClipboardCaps(rfb::clipboardUTF8 |
  447. rfb::clipboardRequest |
  448. rfb::clipboardPeek |
  449. rfb::clipboardNotify |
  450. rfb::clipboardProvide,
  451. sizes);
  452. }
  453. void CConnection::handleClipboardRequest(uint32_t flags)
  454. {
  455. if (!(flags & rfb::clipboardUTF8)) {
  456. vlog.debug("Ignoring clipboard request for unsupported formats 0x%x", flags);
  457. return;
  458. }
  459. if (!hasLocalClipboard) {
  460. vlog.debug("Ignoring unexpected clipboard request");
  461. return;
  462. }
  463. handleClipboardRequest();
  464. }
  465. void CConnection::handleClipboardPeek()
  466. {
  467. if (server.clipboardFlags() & rfb::clipboardNotify)
  468. writer()->writeClipboardNotify(hasLocalClipboard ? rfb::clipboardUTF8 : 0);
  469. }
  470. void CConnection::handleClipboardNotify(uint32_t flags)
  471. {
  472. hasRemoteClipboard = false;
  473. if (flags & rfb::clipboardUTF8) {
  474. hasLocalClipboard = false;
  475. handleClipboardAnnounce(true);
  476. } else {
  477. handleClipboardAnnounce(false);
  478. }
  479. }
  480. void CConnection::handleClipboardProvide(uint32_t flags,
  481. const size_t* lengths,
  482. const uint8_t* const* data)
  483. {
  484. if (!(flags & rfb::clipboardUTF8)) {
  485. vlog.debug("Ignoring clipboard provide with unsupported formats 0x%x", flags);
  486. return;
  487. }
  488. // FIXME: This conversion magic should be in CMsgReader
  489. if (!isValidUTF8((const char*)data[0], lengths[0])) {
  490. vlog.error("Invalid UTF-8 sequence in clipboard - ignoring");
  491. return;
  492. }
  493. serverClipboard = convertLF((const char*)data[0], lengths[0]);
  494. hasRemoteClipboard = true;
  495. // FIXME: Should probably verify that this data was actually requested
  496. handleClipboardData(serverClipboard.c_str());
  497. }
  498. void CConnection::authSuccess()
  499. {
  500. }
  501. void CConnection::initDone()
  502. {
  503. }
  504. void CConnection::resizeFramebuffer()
  505. {
  506. assert(false);
  507. }
  508. void CConnection::handleClipboardRequest()
  509. {
  510. }
  511. void CConnection::handleClipboardAnnounce(bool /*available*/)
  512. {
  513. }
  514. void CConnection::handleClipboardData(const char* /*data*/)
  515. {
  516. }
  517. void CConnection::requestClipboard()
  518. {
  519. if (hasRemoteClipboard) {
  520. handleClipboardData(serverClipboard.c_str());
  521. return;
  522. }
  523. if (server.clipboardFlags() & rfb::clipboardRequest)
  524. writer()->writeClipboardRequest(rfb::clipboardUTF8);
  525. }
  526. void CConnection::announceClipboard(bool available)
  527. {
  528. hasLocalClipboard = available;
  529. unsolicitedClipboardAttempt = false;
  530. // Attempt an unsolicited transfer?
  531. if (available &&
  532. (server.clipboardSize(rfb::clipboardUTF8) > 0) &&
  533. (server.clipboardFlags() & rfb::clipboardProvide)) {
  534. vlog.debug("Attempting unsolicited clipboard transfer...");
  535. unsolicitedClipboardAttempt = true;
  536. handleClipboardRequest();
  537. return;
  538. }
  539. if (server.clipboardFlags() & rfb::clipboardNotify) {
  540. writer()->writeClipboardNotify(available ? rfb::clipboardUTF8 : 0);
  541. return;
  542. }
  543. if (available)
  544. handleClipboardRequest();
  545. }
  546. void CConnection::sendClipboardData(const char* data)
  547. {
  548. if (server.clipboardFlags() & rfb::clipboardProvide) {
  549. // FIXME: This conversion magic should be in CMsgWriter
  550. std::string filtered(convertCRLF(data));
  551. size_t sizes[1] = { filtered.size() + 1 };
  552. const uint8_t* data[1] = { (const uint8_t*)filtered.c_str() };
  553. if (unsolicitedClipboardAttempt) {
  554. unsolicitedClipboardAttempt = false;
  555. if (sizes[0] > server.clipboardSize(rfb::clipboardUTF8)) {
  556. vlog.debug("Clipboard was too large for unsolicited clipboard transfer");
  557. if (server.clipboardFlags() & rfb::clipboardNotify)
  558. writer()->writeClipboardNotify(rfb::clipboardUTF8);
  559. return;
  560. }
  561. }
  562. writer()->writeClipboardProvide(rfb::clipboardUTF8, sizes, data);
  563. } else {
  564. writer()->writeClientCutText(data);
  565. }
  566. }
  567. void CConnection::refreshFramebuffer()
  568. {
  569. forceNonincremental = true;
  570. // Without continuous updates we have to make sure we only have a
  571. // single update in flight, so we'll have to wait to do the refresh
  572. if (continuousUpdates)
  573. requestNewUpdate();
  574. }
  575. void CConnection::setPreferredEncoding(int encoding)
  576. {
  577. if (preferredEncoding == encoding)
  578. return;
  579. preferredEncoding = encoding;
  580. encodingChange = true;
  581. }
  582. int CConnection::getPreferredEncoding()
  583. {
  584. return preferredEncoding;
  585. }
  586. void CConnection::setCompressLevel(int level)
  587. {
  588. if (compressLevel == level)
  589. return;
  590. compressLevel = level;
  591. encodingChange = true;
  592. }
  593. void CConnection::setQualityLevel(int level)
  594. {
  595. if (qualityLevel == level)
  596. return;
  597. qualityLevel = level;
  598. encodingChange = true;
  599. }
  600. void CConnection::setPF(const PixelFormat& pf)
  601. {
  602. if (server.pf() == pf && !formatChange)
  603. return;
  604. nextPF = pf;
  605. formatChange = true;
  606. }
  607. void CConnection::fence(uint32_t flags, unsigned len, const uint8_t data[])
  608. {
  609. CMsgHandler::fence(flags, len, data);
  610. if (!(flags & fenceFlagRequest))
  611. return;
  612. // We cannot guarantee any synchronisation at this level
  613. flags = 0;
  614. writer()->writeFence(flags, len, data);
  615. }
  616. // requestNewUpdate() requests an update from the server, having set the
  617. // format and encoding appropriately.
  618. void CConnection::requestNewUpdate()
  619. {
  620. if (formatChange && !pendingPFChange) {
  621. /* Catch incorrect requestNewUpdate calls */
  622. assert(!pendingUpdate || continuousUpdates);
  623. // We have to make sure we switch the internal format at a safe
  624. // time. For continuous updates we temporarily disable updates and
  625. // look for a EndOfContinuousUpdates message to see when to switch.
  626. // For classical updates we just got a new update right before this
  627. // function was called, so we need to make sure we finish that
  628. // update before we can switch.
  629. pendingPFChange = true;
  630. pendingPF = nextPF;
  631. if (continuousUpdates)
  632. writer()->writeEnableContinuousUpdates(false, 0, 0, 0, 0);
  633. writer()->writeSetPixelFormat(pendingPF);
  634. if (continuousUpdates)
  635. writer()->writeEnableContinuousUpdates(true, 0, 0,
  636. server.width(),
  637. server.height());
  638. formatChange = false;
  639. }
  640. if (encodingChange) {
  641. updateEncodings();
  642. encodingChange = false;
  643. }
  644. if (forceNonincremental || !continuousUpdates) {
  645. pendingUpdate = true;
  646. writer()->writeFramebufferUpdateRequest(Rect(0, 0,
  647. server.width(),
  648. server.height()),
  649. !forceNonincremental);
  650. }
  651. forceNonincremental = false;
  652. }
  653. // Ask for encodings based on which decoders are supported. Assumes higher
  654. // encoding numbers are more desirable.
  655. void CConnection::updateEncodings()
  656. {
  657. std::list<uint32_t> encodings;
  658. if (supportsLocalCursor) {
  659. encodings.push_back(pseudoEncodingCursorWithAlpha);
  660. encodings.push_back(pseudoEncodingVMwareCursor);
  661. encodings.push_back(pseudoEncodingCursor);
  662. encodings.push_back(pseudoEncodingXCursor);
  663. }
  664. if (supportsCursorPosition) {
  665. encodings.push_back(pseudoEncodingVMwareCursorPosition);
  666. }
  667. if (supportsDesktopResize) {
  668. encodings.push_back(pseudoEncodingDesktopSize);
  669. encodings.push_back(pseudoEncodingExtendedDesktopSize);
  670. }
  671. if (supportsLEDState) {
  672. encodings.push_back(pseudoEncodingLEDState);
  673. encodings.push_back(pseudoEncodingVMwareLEDState);
  674. }
  675. encodings.push_back(pseudoEncodingDesktopName);
  676. encodings.push_back(pseudoEncodingLastRect);
  677. encodings.push_back(pseudoEncodingExtendedClipboard);
  678. encodings.push_back(pseudoEncodingContinuousUpdates);
  679. encodings.push_back(pseudoEncodingFence);
  680. encodings.push_back(pseudoEncodingQEMUKeyEvent);
  681. if (Decoder::supported(preferredEncoding)) {
  682. encodings.push_back(preferredEncoding);
  683. }
  684. encodings.push_back(encodingCopyRect);
  685. for (int i = encodingMax; i >= 0; i--) {
  686. if ((i != preferredEncoding) && Decoder::supported(i))
  687. encodings.push_back(i);
  688. }
  689. if (compressLevel >= 0 && compressLevel <= 9)
  690. encodings.push_back(pseudoEncodingCompressLevel0 + compressLevel);
  691. if (qualityLevel >= 0 && qualityLevel <= 9)
  692. encodings.push_back(pseudoEncodingQualityLevel0 + qualityLevel);
  693. writer()->writeSetEncodings(encodings);
  694. }