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

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