blob: 34bc3be4ee34a35af857b69140be823a397fda4e [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCb65bb932011-06-24 03:17:00 +00003 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossman5156d5e2011-03-09 09:42:34 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
Peter Åstrandc359f362011-08-23 12:04:46 +000021#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
Pierre Ossman5156d5e2011-03-09 09:42:34 +000025#include <string.h>
26#include <stdio.h>
Pierre Ossman2a7a8d62013-02-15 08:33:39 +000027#include <ctype.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000028#include <stdlib.h>
29#include <errno.h>
30#include <signal.h>
31#include <locale.h>
Pierre Ossman5bf17ad2015-02-17 13:23:00 +010032#include <fcntl.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000033#include <sys/stat.h>
34
35#ifdef WIN32
Peter Åstrand (astrand)11167e12015-02-05 11:10:32 +010036#include <os/winerrno.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000037#include <direct.h>
38#define mkdir(path, mode) _mkdir(path)
39#endif
40
Pierre Ossman6b9622d2014-07-21 16:42:12 +020041#if !defined(WIN32) && !defined(__APPLE__)
42#include <X11/Xlib.h>
43#include <X11/XKBlib.h>
44#endif
45
Pierre Ossman5156d5e2011-03-09 09:42:34 +000046#include <rfb/Logger_stdio.h>
47#include <rfb/SecurityClient.h>
48#include <rfb/Security.h>
49#ifdef HAVE_GNUTLS
50#include <rfb/CSecurityTLS.h>
51#endif
52#include <rfb/LogWriter.h>
53#include <rfb/Timer.h>
Peter Åstrand8a2b0812012-08-08 11:49:01 +000054#include <rfb/Exception.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000055#include <network/TcpSocket.h>
DRC4426f002011-10-12 20:02:55 +000056#include <os/os.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000057
58#include <FL/Fl.H>
59#include <FL/Fl_Widget.H>
Pierre Ossman8eb35082012-03-27 12:50:54 +000060#include <FL/Fl_PNG_Image.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000061#include <FL/fl_ask.H>
Pierre Ossmanb8858222011-04-29 11:51:38 +000062#include <FL/x.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000063
64#include "i18n.h"
65#include "parameters.h"
66#include "CConn.h"
Pierre Ossman561ff0c2011-05-13 14:04:59 +000067#include "ServerDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000068#include "UserDialog.h"
Adam Tkac8ac4b302013-01-23 13:55:46 +000069#include "vncviewer.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000070
DRCb65bb932011-06-24 03:17:00 +000071#ifdef WIN32
Pierre Ossman8eb35082012-03-27 12:50:54 +000072#include "resource.h"
DRCb65bb932011-06-24 03:17:00 +000073#include "win32.h"
74#endif
75
Pierre Ossman5156d5e2011-03-09 09:42:34 +000076rfb::LogWriter vlog("main");
77
78using namespace network;
79using namespace rfb;
80using namespace std;
81
Pierre Ossmandc96cb42014-09-22 12:19:26 +020082static char aboutText[1024];
83
Adam Tkac8ac4b302013-01-23 13:55:46 +000084char vncServerName[VNCSERVERNAMELEN] = { '\0' };
85
Pierre Ossman5156d5e2011-03-09 09:42:34 +000086static bool exitMainloop = false;
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000087static const char *exitError = NULL;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000088
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000089void exit_vncviewer(const char *error)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000090{
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000091 // Prioritise the first error we get as that is probably the most
92 // relevant one.
93 if ((error != NULL) && (exitError == NULL))
94 exitError = strdup(error);
95
Pierre Ossman5156d5e2011-03-09 09:42:34 +000096 exitMainloop = true;
97}
98
Pierre Ossmanb8858222011-04-29 11:51:38 +000099void about_vncviewer()
100{
101 fl_message_title(_("About TigerVNC Viewer"));
Pierre Ossmandc96cb42014-09-22 12:19:26 +0200102 fl_message("%s", aboutText);
Pierre Ossmanb8858222011-04-29 11:51:38 +0000103}
104
Pierre Ossman8d713a92015-03-04 09:54:27 +0100105#ifdef __APPLE__
Pierre Ossmanb8858222011-04-29 11:51:38 +0000106static void about_callback(Fl_Widget *widget, void *data)
107{
108 about_vncviewer();
109}
Pierre Ossman8d713a92015-03-04 09:54:27 +0100110#endif
Pierre Ossmanb8858222011-04-29 11:51:38 +0000111
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000112static void CleanupSignalHandler(int sig)
113{
114 // CleanupSignalHandler allows C++ object cleanup to happen because it calls
115 // exit() rather than the default which is to abort.
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100116 vlog.info(_("Termination signal %d has been received. TigerVNC Viewer will now exit."), sig);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000117 exit(1);
118}
119
120static void init_fltk()
121{
122 // Basic text size (10pt @ 96 dpi => 13px)
123 FL_NORMAL_SIZE = 13;
124
125#ifndef __APPLE__
126 // Select a FLTK scheme and background color that looks somewhat
127 // close to modern Linux and Windows.
128 Fl::scheme("gtk+");
129 Fl::background(220, 220, 220);
130#else
131 // On Mac OS X there is another scheme that fits better though.
132 Fl::scheme("plastic");
133#endif
134
Henrik Andersson3b837032011-09-19 13:46:55 +0000135 // Proper Gnome Shell integration requires that we set a sensible
136 // WM_CLASS for the window.
137 Fl_Window::default_xclass("vncviewer");
138
Pierre Ossman8eb35082012-03-27 12:50:54 +0000139 // Set the default icon for all windows.
140#ifdef HAVE_FLTK_ICONS
141#ifdef WIN32
142 HICON lg, sm;
143
144 lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
145 IMAGE_ICON, GetSystemMetrics(SM_CXICON),
146 GetSystemMetrics(SM_CYICON),
147 LR_DEFAULTCOLOR | LR_SHARED);
148 sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
149 IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
150 GetSystemMetrics(SM_CYSMICON),
151 LR_DEFAULTCOLOR | LR_SHARED);
152
153 Fl_Window::default_icons(lg, sm);
154#elif ! defined(__APPLE__)
155 const int icon_sizes[] = {48, 32, 24, 16};
156
157 Fl_PNG_Image *icons[4];
158 int count;
159
160 count = 0;
161
162 // FIXME: Follow icon theme specification
163 for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) {
164 char icon_path[PATH_MAX];
165 bool exists;
166
Pierre Ossman6a007bd2012-09-11 10:56:21 +0000167 sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png",
Pierre Ossman8eb35082012-03-27 12:50:54 +0000168 DATA_DIR, icon_sizes[i], icon_sizes[i]);
169
170#ifndef WIN32
171 struct stat st;
172 if (stat(icon_path, &st) != 0)
173#else
174 struct _stat st;
175 if (_stat(icon_path, &st) != 0)
176 return(false);
177#endif
178 exists = false;
179 else
180 exists = true;
181
182 if (exists) {
183 icons[count] = new Fl_PNG_Image(icon_path);
184 if (icons[count]->w() == 0 ||
185 icons[count]->h() == 0 ||
186 icons[count]->d() != 4) {
187 delete icons[count];
188 continue;
189 }
190
191 count++;
192 }
193 }
194
195 Fl_Window::default_icons((const Fl_RGB_Image**)icons, count);
196
197 for (int i = 0;i < count;i++)
198 delete icons[i];
199#endif
200#endif // FLTK_HAVE_ICONS
201
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000202 // This makes the "icon" in dialogs rounded, which fits better
203 // with the above schemes.
204 fl_message_icon()->box(FL_UP_BOX);
205
206 // Turn off the annoying behaviour where popups track the mouse.
207 fl_message_hotspot(false);
208
209 // Avoid empty titles for popups
210 fl_message_title_default(_("TigerVNC Viewer"));
211
212#ifdef WIN32
213 // Most "normal" Windows apps use this font for UI elements.
214 Fl::set_font(FL_HELVETICA, "Tahoma");
215#endif
216
217 // FLTK exposes these so that we can translate them.
218 fl_no = _("No");
219 fl_yes = _("Yes");
220 fl_ok = _("OK");
221 fl_cancel = _("Cancel");
222 fl_close = _("Close");
Pierre Ossmanb8858222011-04-29 11:51:38 +0000223
224#ifdef __APPLE__
Peter Åstrandb7c55242011-08-29 13:14:51 +0000225 /* Needs trailing space */
226 static char fltk_about[16];
227 snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
228 Fl_Mac_App_Menu::about = fltk_about;
229 static char fltk_hide[16];
230 snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
231 Fl_Mac_App_Menu::hide = fltk_hide;
232 static char fltk_quit[16];
233 snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
234 Fl_Mac_App_Menu::quit = fltk_quit;
235
Pierre Ossman41ba6032011-06-16 11:09:31 +0000236 Fl_Mac_App_Menu::print = ""; // Don't want the print item
237 Fl_Mac_App_Menu::services = _("Services");
Pierre Ossman41ba6032011-06-16 11:09:31 +0000238 Fl_Mac_App_Menu::hide_others = _("Hide Others");
239 Fl_Mac_App_Menu::show = _("Show All");
Pierre Ossman41ba6032011-06-16 11:09:31 +0000240
Pierre Ossmanb8858222011-04-29 11:51:38 +0000241 fl_mac_set_about(about_callback, NULL);
242#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000243}
244
245static void mkvnchomedir()
246{
247 // Create .vnc in the user's home directory if it doesn't already exist
248 char* homeDir = NULL;
249
250 if (getvnchomedir(&homeDir) == -1) {
251 vlog.error(_("Could not create VNC home directory: can't obtain home "
252 "directory path."));
253 } else {
254 int result = mkdir(homeDir, 0755);
255 if (result == -1 && errno != EEXIST)
256 vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
257 delete [] homeDir;
258 }
259}
260
261static void usage(const char *programName)
262{
Pierre Ossman5bf17ad2015-02-17 13:23:00 +0100263#ifdef WIN32
264 // If we don't have a console then we need to create one for output
265 if (GetConsoleWindow() == NULL) {
266 HANDLE handle;
267 int fd;
268
269 AllocConsole();
270
271 handle = GetStdHandle(STD_ERROR_HANDLE);
272 fd = _open_osfhandle((intptr_t)handle, O_TEXT);
273 *stderr = *fdopen(fd, "w");
274 }
275#endif
276
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000277 fprintf(stderr,
278 "\nusage: %s [parameters] [host:displayNum] [parameters]\n"
279 " %s [parameters] -listen [port] [parameters]\n",
280 programName, programName);
281 fprintf(stderr,"\n"
282 "Parameters can be turned on with -<param> or off with -<param>=0\n"
283 "Parameters which take a value can be specified as "
284 "-<param> <value>\n"
285 "Other valid forms are <param>=<value> -<param>=<value> "
286 "--<param>=<value>\n"
287 "Parameter names are case-insensitive. The parameters are:\n\n");
288 Configuration::listParams(79, 14);
Pierre Ossman5bf17ad2015-02-17 13:23:00 +0100289
290#ifdef WIN32
291 // Just wait for the user to kill the console window
292 Sleep(INFINITE);
293#endif
294
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000295 exit(1);
296}
297
Adam Tkac8ac4b302013-01-23 13:55:46 +0000298#ifndef WIN32
299static int
300interpretViaParam(char *remoteHost, int *remotePort, int localPort)
301{
302 const int SERVER_PORT_OFFSET = 5900;
303 char *pos = strchr(vncServerName, ':');
304 if (pos == NULL)
305 *remotePort = SERVER_PORT_OFFSET;
306 else {
307 int portOffset = SERVER_PORT_OFFSET;
308 size_t len;
309 *pos++ = '\0';
310 len = strlen(pos);
311 if (*pos == ':') {
312 /* Two colons is an absolute port number, not an offset. */
313 pos++;
314 len--;
315 portOffset = 0;
316 }
317 if (!len || strspn (pos, "-0123456789") != len )
318 return 1;
319 *remotePort = atoi(pos) + portOffset;
320 }
321
322 if (*vncServerName != '\0')
323 strncpy(remoteHost, vncServerName, VNCSERVERNAMELEN);
324 else
325 strncpy(remoteHost, "localhost", VNCSERVERNAMELEN);
326
327 remoteHost[VNCSERVERNAMELEN - 1] = '\0';
328
329 snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort);
330 vncServerName[VNCSERVERNAMELEN - 1] = '\0';
Adam Tkac8ac4b302013-01-23 13:55:46 +0000331
332 return 0;
333}
334
335static void
336createTunnel(const char *gatewayHost, const char *remoteHost,
337 int remotePort, int localPort)
338{
Pierre Ossmanf8d525b2014-07-09 17:02:27 +0200339 const char *cmd = getenv("VNC_VIA_CMD");
340 char *cmd2, *percent;
Adam Tkac8ac4b302013-01-23 13:55:46 +0000341 char lport[10], rport[10];
342 sprintf(lport, "%d", localPort);
343 sprintf(rport, "%d", remotePort);
344 setenv("G", gatewayHost, 1);
345 setenv("H", remoteHost, 1);
346 setenv("R", rport, 1);
347 setenv("L", lport, 1);
Adam Tkac8ac4b302013-01-23 13:55:46 +0000348 if (!cmd)
349 cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20";
350 /* Compatibility with TigerVNC's method. */
Pierre Ossmanf8d525b2014-07-09 17:02:27 +0200351 cmd2 = strdup(cmd);
352 while ((percent = strchr(cmd2, '%')) != NULL)
Adam Tkac8ac4b302013-01-23 13:55:46 +0000353 *percent = '$';
Pierre Ossmanf8d525b2014-07-09 17:02:27 +0200354 system(cmd2);
355 free(cmd2);
Adam Tkac8ac4b302013-01-23 13:55:46 +0000356}
357
358static int mktunnel()
359{
360 const char *gatewayHost;
361 char remoteHost[VNCSERVERNAMELEN];
362 int localPort = findFreeTcpPort();
363 int remotePort;
364
365 gatewayHost = strDup(via.getValueStr());
366 if (interpretViaParam(remoteHost, &remotePort, localPort) != 0)
367 return 1;
368 createTunnel(gatewayHost, remoteHost, remotePort, localPort);
369
370 return 0;
371}
372#endif /* !WIN32 */
373
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000374int main(int argc, char** argv)
375{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000376 UserDialog dlg;
377
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000378 setlocale(LC_ALL, "");
Pierre Ossman0878eca2012-03-27 13:03:22 +0000379 bindtextdomain(PACKAGE_NAME, LOCALE_DIR);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000380 textdomain(PACKAGE_NAME);
381
Pierre Ossmandc96cb42014-09-22 12:19:26 +0200382 // Generate the about string now that we get the proper translation
Pierre Ossman63471c92015-03-03 16:49:50 +0100383 snprintf(aboutText, sizeof(aboutText),
384 _("TigerVNC Viewer %d-bit v%s\n"
385 "Built on: %s\n"
386 "Copyright (C) 1999-%d TigerVNC Team and many others (see README.txt)\n"
387 "See http://www.tigervnc.org for information on TigerVNC."),
Pierre Ossman5945d732014-09-22 13:13:15 +0200388 (int)sizeof(size_t)*8, PACKAGE_VERSION,
Pierre Ossmanc1341682015-02-20 17:12:57 +0100389 BUILD_TIMESTAMP, 2015);
Pierre Ossmandc96cb42014-09-22 12:19:26 +0200390
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000391 rfb::SecurityClient::setDefaults();
392
393 // Write about text to console, still using normal locale codeset
Pierre Ossmandc96cb42014-09-22 12:19:26 +0200394 fprintf(stderr,"\n%s\n", aboutText);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000395
396 // Set gettext codeset to what our GUI toolkit uses. Since we are
397 // passing strings from strerror/gai_strerror to the GUI, these must
398 // be in GUI codeset as well.
399 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
400 bind_textdomain_codeset("libc", "UTF-8");
401
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000402 rfb::initStdIOLoggers();
Pierre Ossmanc8719ad2012-04-26 14:27:52 +0000403#ifdef WIN32
404 rfb::initFileLogger("C:\\temp\\vncviewer.log");
405#else
406 rfb::initFileLogger("/tmp/vncviewer.log");
407#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000408 rfb::LogWriter::setLogParams("*:stderr:30");
409
410#ifdef SIGHUP
411 signal(SIGHUP, CleanupSignalHandler);
412#endif
413 signal(SIGINT, CleanupSignalHandler);
414 signal(SIGTERM, CleanupSignalHandler);
415
416 init_fltk();
417
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200418#if !defined(WIN32) && !defined(__APPLE__)
419 fl_open_display();
420 XkbSetDetectableAutoRepeat(fl_display, True, NULL);
421#endif
422
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000423 Configuration::enableViewerParams();
424
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000425 /* Load the default parameter settings */
426 const char* defaultServerName;
427 try {
428 defaultServerName = loadViewerParameters(NULL);
429 } catch (rfb::Exception& e) {
Pierre Ossman3620e192015-03-03 16:50:15 +0100430 defaultServerName = "";
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000431 fl_alert("%s", e.str());
432 }
433
DRC5aa06502011-06-23 22:04:46 +0000434 int i = 1;
435 if (!Fl::args(argc, argv, i) || i < argc)
436 for (; i < argc; i++) {
437 if (Configuration::setParam(argv[i]))
438 continue;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000439
DRC5aa06502011-06-23 22:04:46 +0000440 if (argv[i][0] == '-') {
441 if (i+1 < argc) {
442 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
443 i++;
444 continue;
445 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000446 }
DRC5aa06502011-06-23 22:04:46 +0000447 usage(argv[0]);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000448 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000449
Adam Tkac8ac4b302013-01-23 13:55:46 +0000450 strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
451 vncServerName[VNCSERVERNAMELEN - 1] = '\0';
DRC5aa06502011-06-23 22:04:46 +0000452 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000453
454 if (!::autoSelect.hasBeenSet()) {
455 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000456 if (::preferredEncoding.hasBeenSet() || ::fullColour.hasBeenSet() ||
457 ::fullColourAlias.hasBeenSet()) {
458 ::autoSelect.setParam(false);
459 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000460 }
461 if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) {
462 // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set
463 if (!::autoSelect && (::lowColourLevel.hasBeenSet() ||
464 ::lowColourLevelAlias.hasBeenSet())) {
465 ::fullColour.setParam(false);
466 }
467 }
468 if (!::customCompressLevel.hasBeenSet()) {
469 // Default to CustomCompressLevel=1 if CompressLevel is used.
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000470 if(::compressLevel.hasBeenSet()) {
471 ::customCompressLevel.setParam(true);
472 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000473 }
474
475 mkvnchomedir();
476
477 CSecurity::upg = &dlg;
478#ifdef HAVE_GNUTLS
479 CSecurityTLS::msg = &dlg;
480#endif
481
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000482 Socket *sock = NULL;
483
DRC3e7ed812013-02-26 10:34:22 +0000484#ifndef WIN32
Adam Tkac571089b2013-02-19 14:30:32 +0000485 /* Specifying -via and -listen together is nonsense */
486 if (listenMode && strlen(via.getValueStr()) > 0) {
Pierre Ossman744e55c2014-12-03 14:00:54 +0100487 // TRANSLATORS: "Parameters" are command line arguments, or settings
488 // from a file or the Windows registry.
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200489 vlog.error(_("Parameters -listen and -via are incompatible"));
490 fl_alert(_("Parameters -listen and -via are incompatible"));
Adam Tkac571089b2013-02-19 14:30:32 +0000491 exit_vncviewer();
492 return 1;
493 }
DRC3e7ed812013-02-26 10:34:22 +0000494#endif
Adam Tkac571089b2013-02-19 14:30:32 +0000495
496 if (listenMode) {
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000497 try {
498 int port = 5500;
499 if (isdigit(vncServerName[0]))
500 port = atoi(vncServerName);
501
502 TcpListener listener(NULL, port);
503
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200504 vlog.info(_("Listening on port %d\n"), port);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000505 sock = listener.accept();
506 } catch (rdr::Exception& e) {
507 vlog.error("%s", e.str());
508 fl_alert("%s", e.str());
509 exit_vncviewer();
510 return 1;
511 }
512
513 } else {
514 if (vncServerName[0] == '\0') {
515 ServerDialog::run(defaultServerName, vncServerName);
516 if (vncServerName[0] == '\0')
517 return 1;
518 }
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000519
Adam Tkac8ac4b302013-01-23 13:55:46 +0000520#ifndef WIN32
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000521 if (strlen (via.getValueStr()) > 0 && mktunnel() != 0)
522 usage(argv[0]);
Adam Tkac8ac4b302013-01-23 13:55:46 +0000523#endif
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000524 }
Adam Tkac8ac4b302013-01-23 13:55:46 +0000525
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000526 CConn *cc = new CConn(vncServerName, sock);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000527
528 while (!exitMainloop) {
529 int next_timer;
530
531 next_timer = Timer::checkTimeouts();
532 if (next_timer == 0)
533 next_timer = INT_MAX;
534
535 if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
536 vlog.error(_("Internal FLTK error. Exiting."));
537 break;
538 }
539 }
540
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000541 delete cc;
542
543 if (exitError != NULL)
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000544 fl_alert("%s", exitError);
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000545
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000546 return 0;
547}