Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CConn.cxx 23KB

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