Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 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 | // All-new VNC viewer for X. |
| 20 | // |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <stdio.h> |
| 24 | #include <ctype.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
| 32 | #include <signal.h> |
Adam Tkac | 04b7fd2 | 2008-03-19 16:14:48 +0000 | [diff] [blame] | 33 | #include <locale.h> |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 34 | #include <rfb/Logger_stdio.h> |
| 35 | #include <rfb/LogWriter.h> |
| 36 | #include <network/TcpSocket.h> |
| 37 | #include "TXWindow.h" |
| 38 | #include "TXMsgBox.h" |
| 39 | #include "CConn.h" |
| 40 | |
Adam Tkac | 0c65d8a | 2008-04-17 16:37:08 +0000 | [diff] [blame] | 41 | #include "gettext.h" |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 42 | #define _(String) gettext (String) |
| 43 | #define gettext_noop(String) String |
| 44 | #define N_(String) gettext_noop (String) |
| 45 | |
| 46 | rfb::LogWriter vlog("main"); |
| 47 | |
| 48 | using namespace network; |
| 49 | using namespace rfb; |
| 50 | using namespace std; |
| 51 | |
| 52 | IntParameter pointerEventInterval("PointerEventInterval", |
| 53 | "Time in milliseconds to rate-limit" |
| 54 | " successive pointer events", 0); |
| 55 | IntParameter wmDecorationWidth("WMDecorationWidth", "Width of window manager " |
| 56 | "decoration around a window", 6); |
| 57 | IntParameter wmDecorationHeight("WMDecorationHeight", "Height of window " |
| 58 | "manager decoration around a window", 24); |
| 59 | StringParameter passwordFile("PasswordFile", |
| 60 | "Password file for VNC authentication", ""); |
| 61 | AliasParameter rfbauth("passwd", "Alias for PasswordFile", &passwordFile); |
| 62 | |
| 63 | BoolParameter useLocalCursor("UseLocalCursor", |
| 64 | "Render the mouse cursor locally", true); |
| 65 | BoolParameter dotWhenNoCursor("DotWhenNoCursor", |
| 66 | "Show the dot cursor when the server sends an " |
| 67 | "invisible cursor", true); |
| 68 | BoolParameter autoSelect("AutoSelect", |
| 69 | "Auto select pixel format and encoding. " |
| 70 | "Default if PreferredEncoding and FullColor are not specified.", |
| 71 | true); |
| 72 | BoolParameter fullColour("FullColor", |
| 73 | "Use full color", true); |
| 74 | AliasParameter fullColourAlias("FullColour", "Alias for FullColor", &fullColour); |
| 75 | IntParameter lowColourLevel("LowColorLevel", |
| 76 | "Color level to use on slow connections. " |
| 77 | "0 = Very Low (8 colors), 1 = Low (64 colors), " |
| 78 | "2 = Medium (256 colors)", 2); |
| 79 | AliasParameter lowColourLevelAlias("LowColourLevel", "Alias for LowColorLevel", &lowColourLevel); |
| 80 | StringParameter preferredEncoding("PreferredEncoding", |
| 81 | "Preferred encoding to use (Tight, ZRLE, Hextile or" |
| 82 | " Raw)", "Tight"); |
| 83 | BoolParameter fullScreen("FullScreen", "Full screen mode", false); |
| 84 | BoolParameter viewOnly("ViewOnly", |
| 85 | "Don't send any mouse or keyboard events to the server", |
| 86 | false); |
| 87 | BoolParameter shared("Shared", |
| 88 | "Don't disconnect other viewers upon connection - " |
| 89 | "share the desktop instead", |
| 90 | false); |
| 91 | BoolParameter acceptClipboard("AcceptClipboard", |
| 92 | "Accept clipboard changes from the server", |
| 93 | true); |
| 94 | BoolParameter sendClipboard("SendClipboard", |
| 95 | "Send clipboard changes to the server", true); |
| 96 | BoolParameter sendPrimary("SendPrimary", |
| 97 | "Send the primary selection and cut buffer to the " |
| 98 | "server as well as the clipboard selection", |
| 99 | true); |
| 100 | |
| 101 | BoolParameter listenMode("listen", "Listen for connections from VNC servers", |
| 102 | false); |
| 103 | StringParameter geometry("geometry", "X geometry specification", ""); |
| 104 | StringParameter displayname("display", "The X display", ""); |
| 105 | |
| 106 | StringParameter via("via", "Gateway to tunnel via", ""); |
| 107 | |
| 108 | BoolParameter customCompressLevel("CustomCompressLevel", |
| 109 | "Use custom compression level. " |
| 110 | "Default if CompressLevel is specified.", false); |
| 111 | |
| 112 | IntParameter compressLevel("CompressLevel", |
| 113 | "Use specified compression level" |
| 114 | "0 = Low, 9 = High", |
| 115 | 6); |
| 116 | |
| 117 | BoolParameter noJpeg("NoJPEG", |
| 118 | "Disable lossy JPEG compression in Tight encoding.", |
| 119 | false); |
| 120 | |
| 121 | IntParameter qualityLevel("QualityLevel", |
| 122 | "JPEG quality level. " |
| 123 | "0 = Low, 9 = High", |
Pierre Ossman | 7dfa22e | 2009-03-12 13:03:22 +0000 | [diff] [blame^] | 124 | 8); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 125 | |
| 126 | char aboutText[1024]; |
| 127 | char* programName; |
| 128 | extern char buildtime[]; |
| 129 | |
| 130 | static void CleanupSignalHandler(int sig) |
| 131 | { |
| 132 | // CleanupSignalHandler allows C++ object cleanup to happen because it calls |
| 133 | // exit() rather than the default which is to abort. |
| 134 | vlog.info("CleanupSignalHandler called"); |
| 135 | exit(1); |
| 136 | } |
| 137 | |
| 138 | // XLoginIconifier is a class which iconifies the XDM login window when it has |
| 139 | // grabbed the keyboard, thus releasing the grab, allowing the viewer to use |
| 140 | // the keyboard. It remaps the xlogin window on exit. |
| 141 | class XLoginIconifier { |
| 142 | public: |
| 143 | Display* dpy; |
| 144 | Window xlogin; |
| 145 | XLoginIconifier() : dpy(0), xlogin(0) {} |
| 146 | void iconify(Display* dpy_) { |
| 147 | dpy = dpy_; |
| 148 | if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), False, GrabModeSync, |
| 149 | GrabModeSync, CurrentTime) == GrabSuccess) { |
| 150 | XUngrabKeyboard(dpy, CurrentTime); |
| 151 | } else { |
| 152 | xlogin = TXWindow::windowWithName(dpy, DefaultRootWindow(dpy), "xlogin"); |
| 153 | if (xlogin) { |
| 154 | XIconifyWindow(dpy, xlogin, DefaultScreen(dpy)); |
| 155 | XSync(dpy, False); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | ~XLoginIconifier() { |
| 160 | if (xlogin) { |
| 161 | fprintf(stderr,"~XLoginIconifier remapping xlogin\n"); |
| 162 | XMapWindow(dpy, xlogin); |
| 163 | XFlush(dpy); |
| 164 | sleep(1); |
| 165 | } |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | static XLoginIconifier xloginIconifier; |
| 170 | |
| 171 | static void usage() |
| 172 | { |
| 173 | fprintf(stderr, |
| 174 | "\nusage: %s [parameters] [host:displayNum] [parameters]\n" |
| 175 | " %s [parameters] -listen [port] [parameters]\n", |
| 176 | programName,programName); |
| 177 | fprintf(stderr,"\n" |
| 178 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 179 | "Parameters which take a value can be specified as " |
| 180 | "-<param> <value>\n" |
| 181 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 182 | "--<param>=<value>\n" |
| 183 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 184 | Configuration::listParams(79, 14); |
| 185 | exit(1); |
| 186 | } |
| 187 | |
| 188 | /* Tunnelling support. */ |
| 189 | static void |
| 190 | interpretViaParam (char **gatewayHost, char **remoteHost, |
| 191 | int *remotePort, char **vncServerName, |
| 192 | int localPort) |
| 193 | { |
| 194 | const int SERVER_PORT_OFFSET = 5900; |
| 195 | char *pos = strchr (*vncServerName, ':'); |
| 196 | if (pos == NULL) |
| 197 | *remotePort = SERVER_PORT_OFFSET; |
| 198 | else { |
| 199 | int portOffset = SERVER_PORT_OFFSET; |
| 200 | size_t len; |
| 201 | *pos++ = '\0'; |
| 202 | len = strlen (pos); |
| 203 | if (*pos == ':') { |
| 204 | /* Two colons is an absolute port number, not an offset. */ |
| 205 | pos++; |
| 206 | len--; |
| 207 | portOffset = 0; |
| 208 | } |
| 209 | if (!len || strspn (pos, "-0123456789") != len ) |
| 210 | usage (); |
| 211 | *remotePort = atoi (pos) + portOffset; |
| 212 | } |
| 213 | |
| 214 | if (**vncServerName != '\0') |
| 215 | *remoteHost = *vncServerName; |
| 216 | |
| 217 | *gatewayHost = strDup (via.getValueStr ()); |
| 218 | *vncServerName = new char[50]; |
| 219 | sprintf (*vncServerName, "localhost::%d", localPort); |
| 220 | } |
| 221 | |
| 222 | #ifndef HAVE_SETENV |
| 223 | int |
| 224 | setenv(const char *envname, const char * envval, int overwrite) |
| 225 | { |
| 226 | if (envname && envval) { |
| 227 | char * envp = NULL; |
| 228 | envp = (char*)malloc(strlen(envname) + strlen(envval) + 2); |
| 229 | if (envp) { |
| 230 | // The putenv API guarantees memory leaks when |
| 231 | // changing environment variables repeatedly. |
| 232 | sprintf(envp, "%s=%s", envname, envval); |
| 233 | |
| 234 | // Cannot free envp |
| 235 | putenv(envp); |
| 236 | return(0); |
| 237 | } |
| 238 | } |
| 239 | return(-1); |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | static void |
| 244 | createTunnel (const char *gatewayHost, const char *remoteHost, |
| 245 | int remotePort, int localPort) |
| 246 | { |
| 247 | char *cmd = getenv ("VNC_VIA_CMD"); |
| 248 | char *percent; |
| 249 | char lport[10], rport[10]; |
| 250 | sprintf (lport, "%d", localPort); |
| 251 | sprintf (rport, "%d", remotePort); |
| 252 | setenv ("G", gatewayHost, 1); |
| 253 | setenv ("H", remoteHost, 1); |
| 254 | setenv ("R", rport, 1); |
| 255 | setenv ("L", lport, 1); |
| 256 | if (!cmd) |
| 257 | cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20"; |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 258 | /* Compatibility with TigerVNC's method. */ |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 259 | while ((percent = strchr (cmd, '%')) != NULL) |
| 260 | *percent = '$'; |
| 261 | system (cmd); |
| 262 | } |
| 263 | |
| 264 | int main(int argc, char** argv) |
| 265 | { |
| 266 | setlocale(LC_ALL, ""); |
Adam Tkac | 932e09b | 2008-04-17 15:45:58 +0000 | [diff] [blame] | 267 | bindtextdomain(PACKAGE_NAME, LOCALEDIR); |
| 268 | textdomain(PACKAGE_NAME); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 269 | |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 270 | const char englishAbout[] = N_("TigerVNC Viewer for X version %s - built %s\n" |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 271 | "Copyright (C) 2002-2005 RealVNC Ltd.\n" |
| 272 | "Copyright (C) 2000-2006 TightVNC Group\n" |
Peter Åstrand | a87caa6 | 2009-02-25 15:01:07 +0000 | [diff] [blame] | 273 | "Copyright (C) 2004-2009 Peter Astrand for Cendio AB\n" |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 274 | "See http://www.tigervnc.org for information on TigerVNC."); |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 275 | |
| 276 | // Write about text to console, still using normal locale codeset |
| 277 | snprintf(aboutText, sizeof(aboutText), |
| 278 | gettext(englishAbout), PACKAGE_VERSION, buildtime); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 279 | fprintf(stderr,"\n%s\n", aboutText); |
| 280 | |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 281 | // Set gettext codeset to what our GUI toolkit uses. Since we are |
| 282 | // passing strings from strerror/gai_strerror to the GUI, these must |
| 283 | // be in GUI codeset as well. |
Adam Tkac | 932e09b | 2008-04-17 15:45:58 +0000 | [diff] [blame] | 284 | bind_textdomain_codeset(PACKAGE_NAME, "iso-8859-1"); |
Peter Åstrand | aa409f1 | 2009-01-26 13:17:27 +0000 | [diff] [blame] | 285 | bind_textdomain_codeset("libc", "iso-8859-1"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 286 | |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 287 | // Re-create the aboutText for the GUI, now using GUI codeset |
| 288 | snprintf(aboutText, sizeof(aboutText), |
| 289 | gettext(englishAbout), PACKAGE_VERSION, buildtime); |
| 290 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 291 | rfb::initStdIOLoggers(); |
| 292 | rfb::LogWriter::setLogParams("*:stderr:30"); |
| 293 | |
| 294 | signal(SIGHUP, CleanupSignalHandler); |
| 295 | signal(SIGINT, CleanupSignalHandler); |
| 296 | signal(SIGTERM, CleanupSignalHandler); |
| 297 | |
| 298 | programName = argv[0]; |
| 299 | char* vncServerName = 0; |
| 300 | Display* dpy = 0; |
| 301 | |
| 302 | for (int i = 1; i < argc; i++) { |
| 303 | if (Configuration::setParam(argv[i])) |
| 304 | continue; |
| 305 | |
| 306 | if (argv[i][0] == '-') { |
| 307 | if (i+1 < argc) { |
| 308 | if (Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 309 | i++; |
| 310 | continue; |
| 311 | } |
| 312 | } |
| 313 | usage(); |
| 314 | } |
| 315 | |
| 316 | vncServerName = argv[i]; |
| 317 | } |
| 318 | |
| 319 | // Create .vnc in the user's home directory if it doesn't already exist |
| 320 | char* homeDir = getenv("HOME"); |
| 321 | if (homeDir) { |
| 322 | CharArray vncDir(strlen(homeDir)+6); |
| 323 | sprintf(vncDir.buf, "%s/.vnc", homeDir); |
| 324 | int result = mkdir(vncDir.buf, 0755); |
| 325 | if (result == -1 && errno != EEXIST) |
| 326 | vlog.error("Could not create .vnc directory: %s.", strerror(errno)); |
| 327 | } else |
| 328 | vlog.error("Could not create .vnc directory: environment variable $HOME not set."); |
| 329 | |
| 330 | if (!::autoSelect.hasBeenSet()) { |
| 331 | // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used |
| 332 | ::autoSelect.setParam(!::preferredEncoding.hasBeenSet() |
| 333 | && !::fullColour.hasBeenSet() |
| 334 | && !::fullColourAlias.hasBeenSet()); |
| 335 | } |
| 336 | if (!::customCompressLevel.hasBeenSet()) { |
| 337 | // Default to CustomCompressLevel=1 if CompressLevel is used. |
| 338 | ::customCompressLevel.setParam(::compressLevel.hasBeenSet()); |
| 339 | } |
| 340 | |
| 341 | try { |
| 342 | /* Tunnelling support. */ |
| 343 | if (strlen (via.getValueStr ()) > 0) { |
| 344 | char *gatewayHost = ""; |
| 345 | char *remoteHost = "localhost"; |
| 346 | int localPort = findFreeTcpPort (); |
| 347 | int remotePort; |
| 348 | if (!vncServerName) |
| 349 | usage(); |
| 350 | interpretViaParam (&gatewayHost, &remoteHost, &remotePort, |
| 351 | &vncServerName, localPort); |
| 352 | createTunnel (gatewayHost, remoteHost, remotePort, localPort); |
| 353 | } |
| 354 | |
| 355 | Socket* sock = 0; |
| 356 | |
| 357 | if (listenMode) { |
| 358 | int port = 5500; |
| 359 | if (vncServerName && isdigit(vncServerName[0])) |
| 360 | port = atoi(vncServerName); |
| 361 | |
| 362 | TcpListener listener(port); |
| 363 | |
| 364 | vlog.info("Listening on port %d\n",port); |
| 365 | |
| 366 | while (true) { |
| 367 | sock = listener.accept(); |
| 368 | int pid = fork(); |
| 369 | if (pid < 0) { perror("fork"); exit(1); } |
| 370 | if (pid == 0) break; // child |
| 371 | delete sock; |
| 372 | int status; |
| 373 | while (wait3(&status, WNOHANG, 0) > 0) ; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | CharArray displaynameStr(displayname.getData()); |
| 378 | if (!(dpy = XOpenDisplay(TXWindow::strEmptyToNull(displaynameStr.buf)))) { |
| 379 | fprintf(stderr,"%s: unable to open display \"%s\"\n", |
| 380 | programName, XDisplayName(displaynameStr.buf)); |
| 381 | exit(1); |
| 382 | } |
| 383 | |
| 384 | TXWindow::init(dpy, "Vncviewer"); |
| 385 | xloginIconifier.iconify(dpy); |
| 386 | CConn cc(dpy, argc, argv, sock, vncServerName, listenMode); |
| 387 | |
| 388 | // X events are processed whenever reading from the socket would block. |
| 389 | |
| 390 | while (true) { |
| 391 | cc.getInStream()->check(1); |
| 392 | cc.processMsg(); |
| 393 | } |
| 394 | |
| 395 | } catch (rdr::EndOfStream& e) { |
| 396 | vlog.info(e.str()); |
| 397 | } catch (rdr::Exception& e) { |
| 398 | vlog.error(e.str()); |
| 399 | if (dpy) { |
| 400 | TXMsgBox msgBox(dpy, e.str(), MB_OK, "VNC Viewer: Information"); |
| 401 | msgBox.show(); |
| 402 | } |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |