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.

CConn.cxx 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2009 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. //
  20. // CConn.cxx
  21. //
  22. #include <unistd.h>
  23. #include "CConn.h"
  24. #include <rfb/CMsgWriter.h>
  25. #include <rfb/encodings.h>
  26. #include <rfb/secTypes.h>
  27. #include <rfb/CSecurityNone.h>
  28. #include <rfb/CSecurityVncAuth.h>
  29. #include <rfb/Hostname.h>
  30. #include <rfb/LogWriter.h>
  31. #include <rfb/util.h>
  32. #include <rfb/Password.h>
  33. #include <rfb/screenTypes.h>
  34. #include <network/TcpSocket.h>
  35. #include "TXViewport.h"
  36. #include "DesktopWindow.h"
  37. #include "ServerDialog.h"
  38. #include "PasswdDialog.h"
  39. #include "parameters.h"
  40. using namespace rfb;
  41. static rfb::LogWriter vlog("CConn");
  42. IntParameter debugDelay("DebugDelay","Milliseconds to display inverted "
  43. "pixel data - a debugging feature", 0);
  44. StringParameter menuKey("MenuKey", "The key which brings up the popup menu",
  45. "F8");
  46. StringParameter windowName("name", "The X window name", "");
  47. CConn::CConn(Display* dpy_, int argc_, char** argv_, network::Socket* sock_,
  48. char* vncServerName, bool reverse)
  49. : dpy(dpy_), argc(argc_),
  50. argv(argv_), serverHost(0), serverPort(0), sock(sock_), viewport(0),
  51. desktop(0), desktopEventHandler(0),
  52. currentEncoding(encodingTight), lastServerEncoding((unsigned int)-1),
  53. fullColour(::fullColour),
  54. autoSelect(::autoSelect), shared(::shared), formatChange(false),
  55. encodingChange(false), sameMachine(false), fullScreen(::fullScreen),
  56. ctrlDown(false), altDown(false),
  57. menuKeysym(0), menu(dpy, this), options(dpy, this), about(dpy), info(dpy),
  58. reverseConnection(reverse)
  59. {
  60. CharArray menuKeyStr(menuKey.getData());
  61. menuKeysym = XStringToKeysym(menuKeyStr.buf);
  62. setShared(shared);
  63. addSecType(secTypeNone);
  64. addSecType(secTypeVncAuth);
  65. CharArray encStr(preferredEncoding.getData());
  66. int encNum = encodingNum(encStr.buf);
  67. if (encNum != -1) {
  68. currentEncoding = encNum;
  69. }
  70. cp.supportsDesktopResize = true;
  71. cp.supportsExtendedDesktopSize = true;
  72. cp.supportsDesktopRename = true;
  73. cp.supportsLocalCursor = useLocalCursor;
  74. cp.customCompressLevel = customCompressLevel;
  75. cp.compressLevel = compressLevel;
  76. cp.noJpeg = noJpeg;
  77. cp.qualityLevel = qualityLevel;
  78. initMenu();
  79. if (sock) {
  80. char* name = sock->getPeerEndpoint();
  81. vlog.info("Accepted connection from %s", name);
  82. if (name) free(name);
  83. } else {
  84. if (vncServerName) {
  85. getHostAndPort(vncServerName, &serverHost, &serverPort);
  86. } else {
  87. ServerDialog dlg(dpy, &options, &about);
  88. if (!dlg.show() || dlg.entry.getText()[0] == 0) {
  89. exit(1);
  90. }
  91. getHostAndPort(dlg.entry.getText(), &serverHost, &serverPort);
  92. }
  93. sock = new network::TcpSocket(serverHost, serverPort);
  94. vlog.info("connected to host %s port %d", serverHost, serverPort);
  95. }
  96. sameMachine = sock->sameMachine();
  97. sock->inStream().setBlockCallback(this);
  98. setServerName(sock->getPeerEndpoint());
  99. setStreams(&sock->inStream(), &sock->outStream());
  100. initialiseProtocol();
  101. }
  102. CConn::~CConn() {
  103. free(serverHost);
  104. delete desktop;
  105. delete viewport;
  106. delete sock;
  107. }
  108. // deleteWindow() is called when the user closes the desktop or menu windows.
  109. void CConn::deleteWindow(TXWindow* w) {
  110. if (w == &menu) {
  111. menu.unmap();
  112. } else if (w == viewport) {
  113. exit(1);
  114. }
  115. }
  116. // handleEvent() filters all events on the desktop and menu. Most are passed
  117. // straight through. The exception is the F8 key. When pressed on the
  118. // desktop, it is used to bring up the menu. An F8 press or release on the
  119. // menu is passed through as if it were on the desktop.
  120. void CConn::handleEvent(TXWindow* w, XEvent* ev)
  121. {
  122. KeySym ks;
  123. char str[256];
  124. switch (ev->type) {
  125. case KeyPress:
  126. case KeyRelease:
  127. XLookupString(&ev->xkey, str, 256, &ks, NULL);
  128. if (ks == menuKeysym && (ev->xkey.state & (ShiftMask|ControlMask)) == 0) {
  129. if (w == desktop && ev->type == KeyPress) {
  130. showMenu(ev->xkey.x_root, ev->xkey.y_root);
  131. break;
  132. } else if (w == &menu) {
  133. if (ev->type == KeyPress) menu.unmap();
  134. desktopEventHandler->handleEvent(w, ev);
  135. break;
  136. }
  137. }
  138. // drop through
  139. default:
  140. if (w == desktop) desktopEventHandler->handleEvent(w, ev);
  141. else if (w == &menu) menuEventHandler->handleEvent(w, ev);
  142. }
  143. }
  144. // blockCallback() is called when reading from the socket would block. We
  145. // process X events until the socket is ready for reading again.
  146. void CConn::blockCallback() {
  147. fd_set rfds;
  148. do {
  149. struct timeval tv;
  150. struct timeval* tvp = 0;
  151. // Process any incoming X events
  152. TXWindow::handleXEvents(dpy);
  153. // Process expired timers and get the time until the next one
  154. int timeoutMs = Timer::checkTimeouts();
  155. if (timeoutMs) {
  156. tv.tv_sec = timeoutMs / 1000;
  157. tv.tv_usec = (timeoutMs % 1000) * 1000;
  158. tvp = &tv;
  159. }
  160. // If there are X requests pending then poll, don't wait!
  161. if (XPending(dpy)) {
  162. tv.tv_usec = tv.tv_sec = 0;
  163. tvp = &tv;
  164. }
  165. // Wait for X events, VNC traffic, or the next timer expiry
  166. FD_ZERO(&rfds);
  167. FD_SET(ConnectionNumber(dpy), &rfds);
  168. FD_SET(sock->getFd(), &rfds);
  169. int n = select(FD_SETSIZE, &rfds, 0, 0, tvp);
  170. if (n < 0) throw rdr::SystemException("select",errno);
  171. } while (!(FD_ISSET(sock->getFd(), &rfds)));
  172. }
  173. // getPasswd() is called by the CSecurity object when it needs us to read a
  174. // password from the user.
  175. void CConn::getUserPasswd(char** user, char** password)
  176. {
  177. CharArray passwordFileStr(passwordFile.getData());
  178. if (!user && passwordFileStr.buf[0]) {
  179. FILE* fp = fopen(passwordFileStr.buf, "r");
  180. if (!fp) throw rfb::Exception("Opening password file failed");
  181. ObfuscatedPasswd obfPwd(256);
  182. obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp);
  183. fclose(fp);
  184. PlainPasswd passwd(obfPwd);
  185. *password = passwd.takeBuf();
  186. return;
  187. }
  188. const char* secType = secTypeName(getCurrentCSecurity()->getType());
  189. const char* titlePrefix = _("VNC authentication");
  190. unsigned int titleLen = strlen(titlePrefix) + strlen(secType) + 4;
  191. CharArray title(titleLen);
  192. snprintf(title.buf, titleLen, "%s [%s]", titlePrefix, secType);
  193. PasswdDialog dlg(dpy, title.buf, !user);
  194. if (!dlg.show()) throw rfb::Exception("Authentication cancelled");
  195. if (user)
  196. *user = strDup(dlg.userEntry.getText());
  197. *password = strDup(dlg.passwdEntry.getText());
  198. }
  199. // CConnection callback methods
  200. // getCSecurity() gets the appropriate CSecurity object for the security
  201. // types which we support.
  202. CSecurity* CConn::getCSecurity(int secType) {
  203. switch (secType) {
  204. case secTypeNone:
  205. return new CSecurityNone();
  206. case secTypeVncAuth:
  207. return new CSecurityVncAuth(this);
  208. default:
  209. throw rfb::Exception("Unsupported secType?");
  210. }
  211. }
  212. // serverInit() is called when the serverInit message has been received. At
  213. // this point we create the desktop window and display it. We also tell the
  214. // server the pixel format and encodings to use and request the first update.
  215. void CConn::serverInit() {
  216. CConnection::serverInit();
  217. // If using AutoSelect with old servers, start in FullColor
  218. // mode. See comment in autoSelectFormatAndEncoding.
  219. if (cp.beforeVersion(3, 8) && autoSelect) {
  220. fullColour = true;
  221. }
  222. serverPF = cp.pf();
  223. desktop = new DesktopWindow(dpy, cp.width, cp.height, serverPF, this);
  224. desktopEventHandler = desktop->setEventHandler(this);
  225. desktop->addEventMask(KeyPressMask | KeyReleaseMask);
  226. fullColourPF = desktop->getPF();
  227. if (!serverPF.trueColour)
  228. fullColour = true;
  229. recreateViewport();
  230. formatChange = encodingChange = true;
  231. requestNewUpdate();
  232. }
  233. // setDesktopSize() is called when the desktop size changes (including when
  234. // it is set initially).
  235. void CConn::setDesktopSize(int w, int h) {
  236. CConnection::setDesktopSize(w,h);
  237. resizeFramebuffer();
  238. }
  239. // setExtendedDesktopSize() is a more advanced version of setDesktopSize()
  240. void CConn::setExtendedDesktopSize(int reason, int result, int w, int h,
  241. const rfb::ScreenSet& layout) {
  242. CConnection::setExtendedDesktopSize(reason, result, w, h, layout);
  243. if ((reason == reasonClient) && (result != resultSuccess))
  244. return;
  245. resizeFramebuffer();
  246. }
  247. // setName() is called when the desktop name changes
  248. void CConn::setName(const char* name) {
  249. CConnection::setName(name);
  250. CharArray windowNameStr(windowName.getData());
  251. if (!windowNameStr.buf[0]) {
  252. windowNameStr.replaceBuf(new char[256]);
  253. snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), cp.name());
  254. }
  255. if (viewport) {
  256. viewport->setName(windowNameStr.buf);
  257. }
  258. }
  259. // framebufferUpdateEnd() is called at the end of an update.
  260. // For each rectangle, the FdInStream will have timed the speed
  261. // of the connection, allowing us to select format and encoding
  262. // appropriately, and then request another incremental update.
  263. void CConn::framebufferUpdateEnd() {
  264. if (debugDelay != 0) {
  265. XSync(dpy, False);
  266. struct timeval tv;
  267. tv.tv_sec = debugDelay / 1000;
  268. tv.tv_usec = (debugDelay % 1000) * 1000;
  269. select(0, 0, 0, 0, &tv);
  270. std::list<rfb::Rect>::iterator i;
  271. for (i = debugRects.begin(); i != debugRects.end(); i++) {
  272. desktop->invertRect(*i);
  273. }
  274. debugRects.clear();
  275. }
  276. desktop->framebufferUpdateEnd();
  277. if (autoSelect)
  278. autoSelectFormatAndEncoding();
  279. requestNewUpdate();
  280. }
  281. // The rest of the callbacks are fairly self-explanatory...
  282. void CConn::setColourMapEntries(int firstColour, int nColours, rdr::U16* rgbs)
  283. {
  284. desktop->setColourMapEntries(firstColour, nColours, rgbs);
  285. }
  286. void CConn::bell() { XBell(dpy, 0); }
  287. void CConn::serverCutText(const char* str, int len) {
  288. desktop->serverCutText(str,len);
  289. }
  290. // We start timing on beginRect and stop timing on endRect, to
  291. // avoid skewing the bandwidth estimation as a result of the server
  292. // being slow or the network having high latency
  293. void CConn::beginRect(const Rect& r, unsigned int encoding)
  294. {
  295. sock->inStream().startTiming();
  296. if (encoding != encodingCopyRect) {
  297. lastServerEncoding = encoding;
  298. }
  299. }
  300. void CConn::endRect(const Rect& r, unsigned int encoding)
  301. {
  302. sock->inStream().stopTiming();
  303. if (debugDelay != 0) {
  304. desktop->invertRect(r);
  305. debugRects.push_back(r);
  306. }
  307. }
  308. void CConn::fillRect(const rfb::Rect& r, rfb::Pixel p) {
  309. desktop->fillRect(r,p);
  310. }
  311. void CConn::imageRect(const rfb::Rect& r, void* p) {
  312. desktop->imageRect(r,p);
  313. }
  314. void CConn::copyRect(const rfb::Rect& r, int sx, int sy) {
  315. desktop->copyRect(r,sx,sy);
  316. }
  317. void CConn::setCursor(int width, int height, const Point& hotspot,
  318. void* data, void* mask) {
  319. desktop->setCursor(width, height, hotspot, data, mask);
  320. }
  321. // Menu stuff - menuSelect() is called when the user selects a menu option.
  322. enum { ID_OPTIONS, ID_INFO, ID_FULLSCREEN, ID_REFRESH, ID_F8, ID_CTRLALTDEL,
  323. ID_ABOUT, ID_DISMISS, ID_EXIT, ID_NEWCONN, ID_CTRL, ID_ALT };
  324. void CConn::initMenu() {
  325. menuEventHandler = menu.setEventHandler(this);
  326. menu.addEventMask(KeyPressMask | KeyReleaseMask);
  327. menu.addEntry(_("Exit viewer"), ID_EXIT);
  328. menu.addEntry(0, 0);
  329. menu.addEntry(_("Full screen"), ID_FULLSCREEN);
  330. menu.check(ID_FULLSCREEN, fullScreen);
  331. menu.addEntry(0, 0);
  332. menu.addEntry(_("Ctrl"), ID_CTRL);
  333. menu.addEntry(_("Alt"), ID_ALT);
  334. CharArray menuKeyStr(menuKey.getData());
  335. CharArray sendMenuKey(64);
  336. snprintf(sendMenuKey.buf, 64, _("Send %s"), menuKeyStr.buf);
  337. menu.addEntry(sendMenuKey.buf, ID_F8);
  338. menu.addEntry(_("Send Ctrl-Alt-Del"), ID_CTRLALTDEL);
  339. menu.addEntry(0, 0);
  340. menu.addEntry(_("Refresh screen"), ID_REFRESH);
  341. menu.addEntry(0, 0);
  342. menu.addEntry(_("New connection..."), ID_NEWCONN);
  343. menu.addEntry(_("Options..."), ID_OPTIONS);
  344. menu.addEntry(_("Connection info..."), ID_INFO);
  345. menu.addEntry(_("About VNCviewer..."), ID_ABOUT);
  346. menu.addEntry(0, 0);
  347. menu.addEntry(_("Dismiss menu"), ID_DISMISS);
  348. menu.toplevel(_("VNC Menu"), this);
  349. menu.setBorderWidth(1);
  350. }
  351. void CConn::showMenu(int x, int y) {
  352. menu.check(ID_FULLSCREEN, fullScreen);
  353. if (x + menu.width() > viewport->width())
  354. x = viewport->width() - menu.width();
  355. if (y + menu.height() > viewport->height())
  356. y = viewport->height() - menu.height();
  357. menu.move(x, y);
  358. menu.raise();
  359. menu.map();
  360. }
  361. void CConn::menuSelect(long id, TXMenu* m) {
  362. switch (id) {
  363. case ID_NEWCONN:
  364. {
  365. menu.unmap();
  366. if (fullScreen) {
  367. fullScreen = false;
  368. if (viewport) recreateViewport();
  369. }
  370. int pid = fork();
  371. if (pid < 0) { perror("fork"); exit(1); }
  372. if (pid == 0) {
  373. delete sock;
  374. close(ConnectionNumber(dpy));
  375. struct timeval tv;
  376. tv.tv_sec = 0;
  377. tv.tv_usec = 200*1000;
  378. select(0, 0, 0, 0, &tv);
  379. execlp(programName, programName, NULL);
  380. perror("execlp"); exit(1);
  381. }
  382. break;
  383. }
  384. case ID_OPTIONS:
  385. menu.unmap();
  386. options.show();
  387. break;
  388. case ID_INFO:
  389. {
  390. menu.unmap();
  391. char pfStr[100];
  392. char spfStr[100];
  393. cp.pf().print(pfStr, 100);
  394. serverPF.print(spfStr, 100);
  395. int secType = getCurrentCSecurity()->getType();
  396. char infoText[1024];
  397. snprintf(infoText, sizeof(infoText),
  398. _("Desktop name: %.80s\n"
  399. "Host: %.80s port: %d\n"
  400. "Size: %d x %d\n"
  401. "Pixel format: %s\n"
  402. "(server default %s)\n"
  403. "Requested encoding: %s\n"
  404. "Last used encoding: %s\n"
  405. "Line speed estimate: %d kbit/s\n"
  406. "Protocol version: %d.%d\n"
  407. "Security method: %s\n"),
  408. cp.name(), serverHost, serverPort, cp.width, cp.height,
  409. pfStr, spfStr, encodingName(currentEncoding),
  410. encodingName(lastServerEncoding),
  411. sock->inStream().kbitsPerSecond(),
  412. cp.majorVersion, cp.minorVersion,
  413. secTypeName(secType));
  414. info.setText(infoText);
  415. info.show();
  416. break;
  417. }
  418. case ID_FULLSCREEN:
  419. menu.unmap();
  420. fullScreen = !fullScreen;
  421. if (viewport) recreateViewport();
  422. break;
  423. case ID_REFRESH:
  424. menu.unmap();
  425. writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
  426. false);
  427. break;
  428. case ID_F8:
  429. menu.unmap();
  430. if (!viewOnly) {
  431. writer()->keyEvent(menuKeysym, true);
  432. writer()->keyEvent(menuKeysym, false);
  433. }
  434. break;
  435. case ID_CTRLALTDEL:
  436. menu.unmap();
  437. if (!viewOnly) {
  438. writer()->keyEvent(XK_Control_L, true);
  439. writer()->keyEvent(XK_Alt_L, true);
  440. writer()->keyEvent(XK_Delete, true);
  441. writer()->keyEvent(XK_Delete, false);
  442. writer()->keyEvent(XK_Alt_L, false);
  443. writer()->keyEvent(XK_Control_L, false);
  444. }
  445. break;
  446. case ID_CTRL:
  447. menu.unmap();
  448. if (!viewOnly) {
  449. ctrlDown = !ctrlDown;
  450. writer()->keyEvent(XK_Control_L, ctrlDown);
  451. menu.check(ID_CTRL, ctrlDown);
  452. }
  453. break;
  454. case ID_ALT:
  455. menu.unmap();
  456. if (!viewOnly) {
  457. altDown = !altDown;
  458. writer()->keyEvent(XK_Alt_L, altDown);
  459. menu.check(ID_ALT, altDown);
  460. }
  461. break;
  462. case ID_ABOUT:
  463. menu.unmap();
  464. about.show();
  465. break;
  466. case ID_DISMISS:
  467. menu.unmap();
  468. break;
  469. case ID_EXIT:
  470. exit(1);
  471. break;
  472. }
  473. }
  474. // OptionsDialogCallback. setOptions() sets the options dialog's checkboxes
  475. // etc to reflect our flags. getOptions() sets our flags according to the
  476. // options dialog's checkboxes.
  477. void CConn::setOptions() {
  478. char digit[2] = "0";
  479. options.autoSelect.checked(autoSelect);
  480. options.fullColour.checked(fullColour);
  481. options.veryLowColour.checked(!fullColour && lowColourLevel == 0);
  482. options.lowColour.checked(!fullColour && lowColourLevel == 1);
  483. options.mediumColour.checked(!fullColour && lowColourLevel == 2);
  484. options.tight.checked(currentEncoding == encodingTight);
  485. options.zrle.checked(currentEncoding == encodingZRLE);
  486. options.hextile.checked(currentEncoding == encodingHextile);
  487. options.raw.checked(currentEncoding == encodingRaw);
  488. options.customCompressLevel.checked(customCompressLevel);
  489. digit[0] = '0' + compressLevel;
  490. options.compressLevel.setText(digit);
  491. options.noJpeg.checked(!noJpeg);
  492. digit[0] = '0' + qualityLevel;
  493. options.qualityLevel.setText(digit);
  494. options.viewOnly.checked(viewOnly);
  495. options.acceptClipboard.checked(acceptClipboard);
  496. options.sendClipboard.checked(sendClipboard);
  497. options.sendPrimary.checked(sendPrimary);
  498. if (state() == RFBSTATE_NORMAL)
  499. options.shared.disabled(true);
  500. else
  501. options.shared.checked(shared);
  502. options.fullScreen.checked(fullScreen);
  503. options.useLocalCursor.checked(useLocalCursor);
  504. options.dotWhenNoCursor.checked(dotWhenNoCursor);
  505. }
  506. void CConn::getOptions() {
  507. autoSelect = options.autoSelect.checked();
  508. if (fullColour != options.fullColour.checked())
  509. formatChange = true;
  510. fullColour = options.fullColour.checked();
  511. if (!fullColour) {
  512. int newLowColourLevel = (options.veryLowColour.checked() ? 0 :
  513. options.lowColour.checked() ? 1 : 2);
  514. if (newLowColourLevel != lowColourLevel) {
  515. lowColourLevel.setParam(newLowColourLevel);
  516. formatChange = true;
  517. }
  518. }
  519. unsigned int newEncoding = (options.tight.checked() ? encodingTight :
  520. options.zrle.checked() ? encodingZRLE :
  521. options.hextile.checked() ? encodingHextile :
  522. encodingRaw);
  523. if (newEncoding != currentEncoding) {
  524. currentEncoding = newEncoding;
  525. encodingChange = true;
  526. }
  527. customCompressLevel.setParam(options.customCompressLevel.checked());
  528. if (cp.customCompressLevel != customCompressLevel) {
  529. cp.customCompressLevel = customCompressLevel;
  530. encodingChange = true;
  531. }
  532. compressLevel.setParam(options.compressLevel.getText());
  533. if (cp.compressLevel != compressLevel) {
  534. cp.compressLevel = compressLevel;
  535. encodingChange = true;
  536. }
  537. noJpeg.setParam(!options.noJpeg.checked());
  538. if (cp.noJpeg != noJpeg) {
  539. cp.noJpeg = noJpeg;
  540. encodingChange = true;
  541. }
  542. qualityLevel.setParam(options.qualityLevel.getText());
  543. if (cp.qualityLevel != qualityLevel) {
  544. cp.qualityLevel = qualityLevel;
  545. encodingChange = true;
  546. }
  547. viewOnly.setParam(options.viewOnly.checked());
  548. acceptClipboard.setParam(options.acceptClipboard.checked());
  549. sendClipboard.setParam(options.sendClipboard.checked());
  550. sendPrimary.setParam(options.sendPrimary.checked());
  551. shared = options.shared.checked();
  552. setShared(shared);
  553. if (fullScreen != options.fullScreen.checked()) {
  554. fullScreen = options.fullScreen.checked();
  555. if (viewport) recreateViewport();
  556. }
  557. useLocalCursor.setParam(options.useLocalCursor.checked());
  558. if (cp.supportsLocalCursor != useLocalCursor) {
  559. cp.supportsLocalCursor = useLocalCursor;
  560. encodingChange = true;
  561. if (desktop)
  562. desktop->resetLocalCursor();
  563. }
  564. dotWhenNoCursor.setParam(options.dotWhenNoCursor.checked());
  565. checkEncodings();
  566. }
  567. void CConn::resizeFramebuffer()
  568. {
  569. if (!desktop)
  570. return;
  571. if ((desktop->width() == cp.width) && (desktop->height() == cp.height))
  572. return;
  573. desktop->resize(cp.width, cp.height);
  574. recreateViewport();
  575. }
  576. void CConn::recreateViewport()
  577. {
  578. TXViewport* oldViewport = viewport;
  579. viewport = new TXViewport(dpy, cp.width, cp.height);
  580. desktop->setViewport(viewport);
  581. CharArray windowNameStr(windowName.getData());
  582. if (!windowNameStr.buf[0]) {
  583. windowNameStr.replaceBuf(new char[256]);
  584. snprintf(windowNameStr.buf, 256, _("TigerVNC: %.240s"), cp.name());
  585. }
  586. viewport->toplevel(windowNameStr.buf, this, argc, argv);
  587. viewport->setBumpScroll(fullScreen);
  588. XSetWindowAttributes attr;
  589. attr.override_redirect = fullScreen;
  590. XChangeWindowAttributes(dpy, viewport->win(), CWOverrideRedirect, &attr);
  591. XChangeWindowAttributes(dpy, menu.win(), CWOverrideRedirect, &attr);
  592. XChangeWindowAttributes(dpy, options.win(), CWOverrideRedirect, &attr);
  593. XChangeWindowAttributes(dpy, about.win(), CWOverrideRedirect, &attr);
  594. XChangeWindowAttributes(dpy, info.win(), CWOverrideRedirect, &attr);
  595. reconfigureViewport();
  596. menu.setTransientFor(viewport->win());
  597. viewport->map();
  598. if (fullScreen) {
  599. XGrabKeyboard(dpy, desktop->win(), True, GrabModeAsync, GrabModeAsync,
  600. CurrentTime);
  601. } else {
  602. XUngrabKeyboard(dpy, CurrentTime);
  603. }
  604. if (oldViewport) delete oldViewport;
  605. }
  606. void CConn::reconfigureViewport()
  607. {
  608. viewport->setMaxSize(cp.width, cp.height);
  609. if (fullScreen) {
  610. viewport->resize(DisplayWidth(dpy,DefaultScreen(dpy)),
  611. DisplayHeight(dpy,DefaultScreen(dpy)));
  612. } else {
  613. int w = cp.width;
  614. int h = cp.height;
  615. if (w + wmDecorationWidth >= DisplayWidth(dpy,DefaultScreen(dpy)))
  616. w = DisplayWidth(dpy,DefaultScreen(dpy)) - wmDecorationWidth;
  617. if (h + wmDecorationHeight >= DisplayHeight(dpy,DefaultScreen(dpy)))
  618. h = DisplayHeight(dpy,DefaultScreen(dpy)) - wmDecorationHeight;
  619. int x = (DisplayWidth(dpy,DefaultScreen(dpy)) - w - wmDecorationWidth) / 2;
  620. int y = (DisplayHeight(dpy,DefaultScreen(dpy)) - h - wmDecorationHeight)/2;
  621. CharArray geometryStr(geometry.getData());
  622. viewport->setGeometry(geometryStr.buf, x, y, w, h);
  623. }
  624. }
  625. // Note: The method below is duplicated in win/vncviewer/CConn.cxx!
  626. // autoSelectFormatAndEncoding() chooses the format and encoding appropriate
  627. // to the connection speed:
  628. //
  629. // First we wait for at least one second of bandwidth measurement.
  630. //
  631. // Above 16Mbps (i.e. LAN), we choose the second highest JPEG quality,
  632. // which should be perceptually lossless.
  633. //
  634. // If the bandwidth is below that, we choose a more lossy JPEG quality.
  635. //
  636. // If the bandwidth drops below 256 Kbps, we switch to palette mode.
  637. //
  638. // Note: The system here is fairly arbitrary and should be replaced
  639. // with something more intelligent at the server end.
  640. //
  641. void CConn::autoSelectFormatAndEncoding()
  642. {
  643. int kbitsPerSecond = sock->inStream().kbitsPerSecond();
  644. unsigned int timeWaited = sock->inStream().timeWaited();
  645. bool newFullColour = fullColour;
  646. int newQualityLevel = qualityLevel;
  647. // Always use Tight
  648. currentEncoding = encodingTight;
  649. // Check that we have a decent bandwidth measurement
  650. if ((kbitsPerSecond == 0) || (timeWaited < 10000))
  651. return;
  652. // Select appropriate quality level
  653. if (!noJpeg) {
  654. if (kbitsPerSecond > 16000)
  655. newQualityLevel = 8;
  656. else
  657. newQualityLevel = 6;
  658. if (newQualityLevel != qualityLevel) {
  659. vlog.info("Throughput %d kbit/s - changing to quality %d ",
  660. kbitsPerSecond, newQualityLevel);
  661. cp.qualityLevel = newQualityLevel;
  662. qualityLevel.setParam(newQualityLevel);
  663. encodingChange = true;
  664. }
  665. }
  666. if (cp.beforeVersion(3, 8)) {
  667. // Xvnc from TightVNC 1.2.9 sends out FramebufferUpdates with
  668. // cursors "asynchronously". If this happens in the middle of a
  669. // pixel format change, the server will encode the cursor with
  670. // the old format, but the client will try to decode it
  671. // according to the new format. This will lead to a
  672. // crash. Therefore, we do not allow automatic format change for
  673. // old servers.
  674. return;
  675. }
  676. // Select best color level
  677. newFullColour = (kbitsPerSecond > 256);
  678. if (newFullColour != fullColour) {
  679. vlog.info("Throughput %d kbit/s - full color is now %s",
  680. kbitsPerSecond,
  681. newFullColour ? "enabled" : "disabled");
  682. fullColour = newFullColour;
  683. formatChange = true;
  684. }
  685. }
  686. // checkEncodings() sends a setEncodings message if one is needed.
  687. void CConn::checkEncodings()
  688. {
  689. if (encodingChange && writer()) {
  690. vlog.info("Using %s encoding",encodingName(currentEncoding));
  691. writer()->writeSetEncodings(currentEncoding, true);
  692. encodingChange = false;
  693. }
  694. }
  695. // requestNewUpdate() requests an update from the server, having set the
  696. // format and encoding appropriately.
  697. void CConn::requestNewUpdate()
  698. {
  699. if (formatChange) {
  700. if (fullColour) {
  701. desktop->setPF(fullColourPF);
  702. } else {
  703. if (lowColourLevel == 0)
  704. desktop->setPF(PixelFormat(8,3,0,1,1,1,1,2,1,0));
  705. else if (lowColourLevel == 1)
  706. desktop->setPF(PixelFormat(8,6,0,1,3,3,3,4,2,0));
  707. else
  708. desktop->setPF(PixelFormat(8,8,0,0));
  709. }
  710. char str[256];
  711. desktop->getPF().print(str, 256);
  712. vlog.info("Using pixel format %s",str);
  713. cp.setPF(desktop->getPF());
  714. writer()->writeSetPixelFormat(cp.pf());
  715. }
  716. checkEncodings();
  717. writer()->writeFramebufferUpdateRequest(Rect(0, 0, cp.width, cp.height),
  718. !formatChange);
  719. formatChange = false;
  720. }