Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +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 | // -=- VNC Viewer for Win32 |
| 20 | |
| 21 | #include <string.h> |
| 22 | #ifdef WIN32 |
| 23 | #define strcasecmp _stricmp |
| 24 | #endif |
| 25 | #include <list> |
| 26 | |
DRC | 0a60801 | 2010-05-20 07:41:58 +0000 | [diff] [blame] | 27 | #include <vncviewer/ListenServer.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 28 | #include <vncviewer/resource.h> |
| 29 | #include <vncviewer/CConn.h> |
| 30 | #include <vncviewer/CConnThread.h> |
| 31 | #include <vncviewer/OptionsDialog.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 32 | #include <vncviewer/ListenTrayIcon.h> |
| 33 | #include <network/TcpSocket.h> |
Adam Tkac | 8311f4e | 2011-02-07 10:45:50 +0000 | [diff] [blame] | 34 | #include <os/os.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 35 | #include <rfb/Logger_stdio.h> |
| 36 | #include <rfb/Logger_file.h> |
| 37 | #include <rfb/LogWriter.h> |
| 38 | #include <rfb/Exception.h> |
| 39 | #include <rfb_win32/RegConfig.h> |
| 40 | #include <rfb_win32/MsgBox.h> |
| 41 | |
| 42 | #ifdef _DIALOG_CAPTURE |
| 43 | #include <extra/LoadBMP.h> |
| 44 | #endif |
| 45 | |
| 46 | using namespace rfb; |
| 47 | using namespace rfb::win32; |
| 48 | using namespace rdr; |
| 49 | using namespace network; |
| 50 | |
| 51 | static LogWriter vlog("main"); |
| 52 | |
| 53 | TStr rfb::win32::AppName("VNC Viewer"); |
| 54 | |
| 55 | |
| 56 | #ifdef _DIALOG_CAPTURE |
| 57 | BoolParameter captureDialogs("CaptureDialogs", "", false); |
| 58 | #endif |
| 59 | |
| 60 | // |
| 61 | // -=- Listener |
| 62 | // Class to handle listening on a particular port for incoming connections |
| 63 | // from servers, and spawning of clients |
| 64 | // |
| 65 | |
| 66 | static BoolParameter acceptIncoming("Listen", "Accept incoming connections from VNC servers.", false); |
| 67 | |
| 68 | |
| 69 | // |
| 70 | // -=- AboutDialog global values |
| 71 | // |
| 72 | |
| 73 | const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT; |
| 74 | const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT; |
| 75 | const WORD rfb::win32::AboutDialog::Version = IDC_VERSION; |
| 76 | const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME; |
| 77 | const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION; |
| 78 | |
| 79 | |
| 80 | // |
| 81 | // -=- processParams |
| 82 | // Read in the command-line parameters and interpret them. |
| 83 | // |
| 84 | |
| 85 | void |
| 86 | programInfo() { |
| 87 | win32::FileVersionInfo inf; |
| 88 | _tprintf(_T("%s - %s, Version %s\n"), |
| 89 | inf.getVerString(_T("ProductName")), |
| 90 | inf.getVerString(_T("FileDescription")), |
| 91 | inf.getVerString(_T("FileVersion"))); |
| 92 | printf("%s\n", buildTime); |
| 93 | _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright"))); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | programUsage() { |
| 98 | printf("usage: vncviewer <options> <hostname>[:<display>]\n"); |
| 99 | printf("Command-line options:\n"); |
| 100 | printf(" -help - Provide usage information.\n"); |
| 101 | printf(" -config <file> - Load connection settings from VNC Viewer 3.3 settings file\n"); |
| 102 | printf(" -console - Run with a console window visible.\n"); |
| 103 | printf(" <setting>=<value> - Set the named configuration parameter.\n"); |
| 104 | printf(" (Parameter values specified on the command-line override those specified by other configuration methods.)\n"); |
| 105 | printf("\nLog names:\n"); |
| 106 | LogWriter::listLogWriters(); |
| 107 | printf("\nLog destinations:\n"); |
| 108 | Logger::listLoggers(); |
| 109 | printf("\nParameters:\n"); |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 110 | Configuration::listParams(ConfViewer); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 111 | printf("Press Enter/Return key to continue\n"); |
Peter Åstrand | d0a617e | 2008-12-12 09:27:12 +0000 | [diff] [blame] | 112 | getchar(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 113 | exit(1); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | bool print_usage = false; |
| 118 | bool close_console = true; |
| 119 | std::list<char*> hosts; |
| 120 | std::list<char*> configFiles; |
| 121 | |
| 122 | void |
| 123 | processParams(int argc, char* argv[]) { |
Adam Tkac | 9d84654 | 2010-12-08 13:44:38 +0000 | [diff] [blame] | 124 | |
| 125 | Configuration::enableViewerParams(); |
| 126 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 127 | for (int i=1; i<argc; i++) { |
| 128 | try { |
| 129 | |
| 130 | if (strcasecmp(argv[i], "-console") == 0) { |
| 131 | close_console = false; |
| 132 | |
| 133 | } else if (((strcasecmp(argv[i], "-config") == 0) || |
| 134 | (strcasecmp(argv[i], "/config") == 0)) && (i < argc-1)) { |
| 135 | configFiles.push_back(strDup(argv[i+1])); |
| 136 | i++; |
| 137 | |
| 138 | } else if ((strcasecmp(argv[i], "-help") == 0) || |
| 139 | (strcasecmp(argv[i], "--help") == 0) || |
| 140 | (strcasecmp(argv[i], "-h") == 0) || |
| 141 | (strcasecmp(argv[i], "/?") == 0)) { |
| 142 | print_usage = true; |
| 143 | close_console = false; |
| 144 | break; |
| 145 | |
| 146 | } else { |
| 147 | // Try to process <option>=<value>, or -<bool> |
| 148 | if (Configuration::setParam(argv[i], true)) |
| 149 | continue; |
| 150 | // Try to process -<option> <value> |
| 151 | if ((argv[i][0] == '-') && (i+1 < argc)) { |
| 152 | if (Configuration::setParam(&argv[i][1], argv[i+1], true)) { |
| 153 | i++; |
| 154 | continue; |
| 155 | } |
| 156 | } |
| 157 | // If it's -<option> then it's not recognised - error |
| 158 | // If it's <host> then add it to the list to connect to. |
| 159 | if ((argv[i][0] == '-') || (argv[i][0] == '/')) { |
| 160 | const char* fmt = "The option %s was not recognized. Use -help to see VNC Viewer usage"; |
| 161 | CharArray tmp(strlen(argv[i])+strlen(fmt)+1); |
| 162 | sprintf(tmp.buf, fmt, argv[i]); |
| 163 | MsgBox(0, TStr(tmp.buf), MB_ICONSTOP | MB_OK); |
| 164 | exit(1); |
| 165 | } else if (strContains(argv[i], '\\')) { |
| 166 | configFiles.push_back(strDup(argv[i])); |
| 167 | } else { |
| 168 | hosts.push_back(strDup(argv[i])); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } catch (rdr::Exception& e) { |
| 173 | vlog.error(e.str()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | // |
| 180 | // -=- main |
| 181 | // |
| 182 | |
| 183 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) { |
| 184 | try { |
| 185 | |
| 186 | // - Initialise the available loggers |
| 187 | initStdIOLoggers(); |
| 188 | initFileLogger("C:\\temp\\vncviewer4.log"); |
| 189 | |
| 190 | // - By default, just log errors to stderr |
| 191 | logParams.setDefault("*:stderr:0"); |
| 192 | |
| 193 | // - Process the command-line |
| 194 | int argc = __argc; |
| 195 | char** argv = __argv; |
| 196 | processParams(argc, argv); |
| 197 | |
| 198 | // - By default the console will be closed |
| 199 | if (close_console) { |
| 200 | if (!FreeConsole()) |
| 201 | vlog.info("unable to close console:%u", GetLastError()); |
| 202 | } else { |
| 203 | AllocConsole(); |
| 204 | freopen("CONIN$","rb",stdin); |
| 205 | freopen("CONOUT$","wb",stdout); |
| 206 | freopen("CONOUT$","wb",stderr); |
| 207 | setbuf(stderr, 0); |
| 208 | } |
| 209 | |
| 210 | #ifdef _DIALOG_CAPTURE |
| 211 | if (captureDialogs) { |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 212 | CConn::userConfigKey.openKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4")); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 213 | OptionsDialog::global.showDialog(0, true); |
| 214 | return 0; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | // - If no clients are specified, bring up a connection dialog |
| 219 | if (configFiles.empty() && hosts.empty() && !acceptIncoming && !print_usage) |
| 220 | hosts.push_back(0); |
| 221 | |
| 222 | programInfo(); |
| 223 | |
Adam Tkac | 8311f4e | 2011-02-07 10:45:50 +0000 | [diff] [blame] | 224 | // Create vnc in the user's home directory if it doesn't already exist |
| 225 | char* homeDir = NULL; |
| 226 | if (getvnchomedir(&homeDir) == -1) |
| 227 | vlog.error("Could not create vnc directory: can't obtain home directory path"); |
| 228 | else { |
| 229 | int result = CreateDirectory(homeDir, NULL); |
| 230 | if (result <= 0 && GetLastError() != ERROR_ALREADY_EXISTS) |
| 231 | vlog.error("Could not create vnc directory: %u", GetLastError()); |
| 232 | delete [] homeDir; |
| 233 | } |
| 234 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 235 | // - Connect to the clients |
| 236 | if (!configFiles.empty() || !hosts.empty() || acceptIncoming) { |
| 237 | // - Configure the registry configuration reader |
| 238 | win32::RegConfigThread config; |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 239 | config.start(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4")); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 240 | |
| 241 | // - Tell the rest of VNC Viewer where to write config data to |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 242 | CConn::userConfigKey.createKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4")); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 243 | |
| 244 | if (acceptIncoming) { |
| 245 | int port = 5500; |
| 246 | |
| 247 | // Listening viewer |
| 248 | if (hosts.size() > 1) |
| 249 | programUsage(); |
| 250 | if (!hosts.empty()) |
| 251 | port = atoi(hosts.front()); |
| 252 | |
| 253 | // Show the tray icon & menu |
| 254 | ListenTrayIcon tray; |
| 255 | |
| 256 | // Listen for reverse connections |
Adam Tkac | 93ff5db | 2010-02-05 15:54:10 +0000 | [diff] [blame] | 257 | network::TcpListener sock(NULL, port); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 258 | ListenServer listener(&sock); |
| 259 | |
| 260 | // Run the view manager |
| 261 | // Also processes the tray icon if necessary |
| 262 | MSG msg; |
| 263 | while (GetMessage(&msg, NULL, 0, 0) > 0) { |
| 264 | TranslateMessage(&msg); |
| 265 | DispatchMessage(&msg); |
| 266 | } |
| 267 | } else { |
| 268 | // Read each config file in turn |
| 269 | while (!configFiles.empty()) { |
| 270 | char* filename = configFiles.front(); |
Peter Åstrand | c4d87f9 | 2010-02-10 12:47:43 +0000 | [diff] [blame] | 271 | new CConnThread(filename, true); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 272 | strFree(filename); |
| 273 | configFiles.pop_front(); |
| 274 | } |
| 275 | |
| 276 | // Connect to each client in turn |
| 277 | while (!hosts.empty()) { |
| 278 | char* hostinfo = hosts.front(); |
Peter Åstrand | c4d87f9 | 2010-02-10 12:47:43 +0000 | [diff] [blame] | 279 | new CConnThread(hostinfo); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 280 | strFree(hostinfo); |
| 281 | hosts.pop_front(); |
| 282 | } |
| 283 | |
| 284 | // Run the view manager |
| 285 | MSG msg; |
| 286 | while (CConnThread::getMessage(&msg, NULL, 0, 0) > 0) { |
| 287 | TranslateMessage(&msg); |
| 288 | DispatchMessage(&msg); |
| 289 | } |
| 290 | |
| 291 | vlog.debug("quitting viewer"); |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | // - If necessary, print the program's usage info |
| 297 | if (print_usage) |
| 298 | programUsage(); |
| 299 | |
| 300 | if (!close_console) { |
| 301 | printf("Press Enter/Return key to continue\n"); |
Peter Åstrand | d0a617e | 2008-12-12 09:27:12 +0000 | [diff] [blame] | 302 | getchar(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | } catch (rdr::Exception& e) { |
| 306 | MsgBox(0, TStr(e.str()), MB_ICONSTOP | MB_OK); |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |