blob: 8a741f7b8030d34315b707081c7fdd365ef302ee [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[]) {
123 for (int i=1; i<argc; i++) {
124 try {
125
126 if (strcasecmp(argv[i], "-console") == 0) {
127 close_console = false;
128
129 } else if (((strcasecmp(argv[i], "-config") == 0) ||
130 (strcasecmp(argv[i], "/config") == 0)) && (i < argc-1)) {
131 configFiles.push_back(strDup(argv[i+1]));
132 i++;
133
134 } else if ((strcasecmp(argv[i], "-help") == 0) ||
135 (strcasecmp(argv[i], "--help") == 0) ||
136 (strcasecmp(argv[i], "-h") == 0) ||
137 (strcasecmp(argv[i], "/?") == 0)) {
138 print_usage = true;
139 close_console = false;
140 break;
141
142 } else {
143 // Try to process <option>=<value>, or -<bool>
144 if (Configuration::setParam(argv[i], true))
145 continue;
146 // Try to process -<option> <value>
147 if ((argv[i][0] == '-') && (i+1 < argc)) {
148 if (Configuration::setParam(&argv[i][1], argv[i+1], true)) {
149 i++;
150 continue;
151 }
152 }
153 // If it's -<option> then it's not recognised - error
154 // If it's <host> then add it to the list to connect to.
155 if ((argv[i][0] == '-') || (argv[i][0] == '/')) {
156 const char* fmt = "The option %s was not recognized. Use -help to see VNC Viewer usage";
157 CharArray tmp(strlen(argv[i])+strlen(fmt)+1);
158 sprintf(tmp.buf, fmt, argv[i]);
159 MsgBox(0, TStr(tmp.buf), MB_ICONSTOP | MB_OK);
160 exit(1);
161 } else if (strContains(argv[i], '\\')) {
162 configFiles.push_back(strDup(argv[i]));
163 } else {
164 hosts.push_back(strDup(argv[i]));
165 }
166 }
167
168 } catch (rdr::Exception& e) {
169 vlog.error(e.str());
170 }
171 }
172}
173
174
175//
176// -=- main
177//
178
179int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) {
180 try {
181
182 // - Initialise the available loggers
183 initStdIOLoggers();
184 initFileLogger("C:\\temp\\vncviewer4.log");
185
186 // - By default, just log errors to stderr
187 logParams.setDefault("*:stderr:0");
188
189 // - Process the command-line
190 int argc = __argc;
191 char** argv = __argv;
192 processParams(argc, argv);
193
194 // - By default the console will be closed
195 if (close_console) {
196 if (!FreeConsole())
197 vlog.info("unable to close console:%u", GetLastError());
198 } else {
199 AllocConsole();
200 freopen("CONIN$","rb",stdin);
201 freopen("CONOUT$","wb",stdout);
202 freopen("CONOUT$","wb",stderr);
203 setbuf(stderr, 0);
204 }
205
206#ifdef _DIALOG_CAPTURE
207 if (captureDialogs) {
Peter Åstrand4eacc022009-02-27 10:12:14 +0000208 CConn::userConfigKey.openKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000209 OptionsDialog::global.showDialog(0, true);
210 return 0;
211 }
212#endif
213
214 // - If no clients are specified, bring up a connection dialog
215 if (configFiles.empty() && hosts.empty() && !acceptIncoming && !print_usage)
216 hosts.push_back(0);
217
218 programInfo();
219
220 // - Connect to the clients
221 if (!configFiles.empty() || !hosts.empty() || acceptIncoming) {
222 // - Configure the registry configuration reader
223 win32::RegConfigThread config;
Peter Åstrand4eacc022009-02-27 10:12:14 +0000224 config.start(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000225
226 // - Tell the rest of VNC Viewer where to write config data to
Peter Åstrand4eacc022009-02-27 10:12:14 +0000227 CConn::userConfigKey.createKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCViewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000228
229 if (acceptIncoming) {
230 int port = 5500;
231
232 // Listening viewer
233 if (hosts.size() > 1)
234 programUsage();
235 if (!hosts.empty())
236 port = atoi(hosts.front());
237
238 // Show the tray icon & menu
239 ListenTrayIcon tray;
240
241 // Listen for reverse connections
Adam Tkac93ff5db2010-02-05 15:54:10 +0000242 network::TcpListener sock(NULL, port);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000243 ListenServer listener(&sock);
244
245 // Run the view manager
246 // Also processes the tray icon if necessary
247 MSG msg;
248 while (GetMessage(&msg, NULL, 0, 0) > 0) {
249 TranslateMessage(&msg);
250 DispatchMessage(&msg);
251 }
252 } else {
253 // Read each config file in turn
254 while (!configFiles.empty()) {
255 char* filename = configFiles.front();
Peter Åstrandc4d87f92010-02-10 12:47:43 +0000256 new CConnThread(filename, true);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000257 strFree(filename);
258 configFiles.pop_front();
259 }
260
261 // Connect to each client in turn
262 while (!hosts.empty()) {
263 char* hostinfo = hosts.front();
Peter Åstrandc4d87f92010-02-10 12:47:43 +0000264 new CConnThread(hostinfo);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000265 strFree(hostinfo);
266 hosts.pop_front();
267 }
268
269 // Run the view manager
270 MSG msg;
271 while (CConnThread::getMessage(&msg, NULL, 0, 0) > 0) {
272 TranslateMessage(&msg);
273 DispatchMessage(&msg);
274 }
275
276 vlog.debug("quitting viewer");
277 }
278
279 }
280
281 // - If necessary, print the program's usage info
282 if (print_usage)
283 programUsage();
284
285 if (!close_console) {
286 printf("Press Enter/Return key to continue\n");
Peter Åstrandd0a617e2008-12-12 09:27:12 +0000287 getchar();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000288 }
289
290 } catch (rdr::Exception& e) {
291 MsgBox(0, TStr(e.str()), MB_ICONSTOP | MB_OK);
292 }
293
294 return 0;
295}