blob: f861e796b37f009ff6a04173477c9f7ed2607f44 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* 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
DRC0a608012010-05-20 07:41:58 +000027#include <vncviewer/ListenServer.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000028#include <vncviewer/resource.h>
29#include <vncviewer/CConn.h>
30#include <vncviewer/CConnThread.h>
31#include <vncviewer/OptionsDialog.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000032#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
45using namespace rfb;
46using namespace rfb::win32;
47using namespace rdr;
48using namespace network;
49
50static LogWriter vlog("main");
51
52TStr rfb::win32::AppName("VNC Viewer");
53
54
55#ifdef _DIALOG_CAPTURE
56BoolParameter 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
65static BoolParameter acceptIncoming("Listen", "Accept incoming connections from VNC servers.", false);
66
67
68//
69// -=- AboutDialog global values
70//
71
72const WORD rfb::win32::AboutDialog::DialogId = IDD_ABOUT;
73const WORD rfb::win32::AboutDialog::Copyright = IDC_COPYRIGHT;
74const WORD rfb::win32::AboutDialog::Version = IDC_VERSION;
75const WORD rfb::win32::AboutDialog::BuildTime = IDC_BUILDTIME;
76const WORD rfb::win32::AboutDialog::Description = IDC_DESCRIPTION;
77
78
79//
80// -=- processParams
81// Read in the command-line parameters and interpret them.
82//
83
84void
85programInfo() {
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
95void
96programUsage() {
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 Tkacc58b3d12010-04-23 13:55:10 +0000109 Configuration::listParams(ConfViewer);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000110 printf("Press Enter/Return key to continue\n");
Peter Åstrandd0a617e2008-12-12 09:27:12 +0000111 getchar();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000112 exit(1);
113}
114
115
116bool print_usage = false;
117bool close_console = true;
118std::list<char*> hosts;
119std::list<char*> configFiles;
120
121void
122processParams(int argc, char* argv[]) {
Adam Tkac9d846542010-12-08 13:44:38 +0000123
124 Configuration::enableViewerParams();
125
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000126 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
182int 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 Åstrand4eacc022009-02-27 10:12:14 +0000211 CConn::userConfigKey.openKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000212 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 Åstrand4eacc022009-02-27 10:12:14 +0000227 config.start(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000228
229 // - Tell the rest of VNC Viewer where to write config data to
Peter Åstrand4eacc022009-02-27 10:12:14 +0000230 CConn::userConfigKey.createKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000231
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 Tkac93ff5db2010-02-05 15:54:10 +0000245 network::TcpListener sock(NULL, port);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000246 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 Åstrandc4d87f92010-02-10 12:47:43 +0000259 new CConnThread(filename, true);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000260 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 Åstrandc4d87f92010-02-10 12:47:43 +0000267 new CConnThread(hostinfo);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000268 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 Åstrandd0a617e2008-12-12 09:27:12 +0000290 getchar();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000291 }
292
293 } catch (rdr::Exception& e) {
294 MsgBox(0, TStr(e.str()), MB_ICONSTOP | MB_OK);
295 }
296
297 return 0;
298}