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