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