blob: f9258b90639f640202a00df9a08be7f160e7bdf6 [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 Ossmana71a2762015-03-04 09:57:06 +010033#include <unistd.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000034#include <sys/stat.h>
35
36#ifdef WIN32
Peter Åstrand (astrand)11167e12015-02-05 11:10:32 +010037#include <os/winerrno.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000038#include <direct.h>
39#define mkdir(path, mode) _mkdir(path)
40#endif
41
Pierre Ossman6b9622d2014-07-21 16:42:12 +020042#if !defined(WIN32) && !defined(__APPLE__)
43#include <X11/Xlib.h>
44#include <X11/XKBlib.h>
45#endif
46
Pierre Ossman5156d5e2011-03-09 09:42:34 +000047#include <rfb/Logger_stdio.h>
48#include <rfb/SecurityClient.h>
49#include <rfb/Security.h>
50#ifdef HAVE_GNUTLS
51#include <rfb/CSecurityTLS.h>
52#endif
53#include <rfb/LogWriter.h>
54#include <rfb/Timer.h>
Peter Åstrand8a2b0812012-08-08 11:49:01 +000055#include <rfb/Exception.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000056#include <network/TcpSocket.h>
DRC4426f002011-10-12 20:02:55 +000057#include <os/os.h>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000058
59#include <FL/Fl.H>
60#include <FL/Fl_Widget.H>
Pierre Ossman8eb35082012-03-27 12:50:54 +000061#include <FL/Fl_PNG_Image.H>
Pierre Ossmana71a2762015-03-04 09:57:06 +010062#include <FL/Fl_Sys_Menu_Bar.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000063#include <FL/fl_ask.H>
Pierre Ossmanb8858222011-04-29 11:51:38 +000064#include <FL/x.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000065
66#include "i18n.h"
67#include "parameters.h"
68#include "CConn.h"
Pierre Ossman561ff0c2011-05-13 14:04:59 +000069#include "ServerDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000070#include "UserDialog.h"
Adam Tkac8ac4b302013-01-23 13:55:46 +000071#include "vncviewer.h"
Pierre Ossmana71a2762015-03-04 09:57:06 +010072#include "fltk_layout.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000073
DRCb65bb932011-06-24 03:17:00 +000074#ifdef WIN32
Pierre Ossman8eb35082012-03-27 12:50:54 +000075#include "resource.h"
DRCb65bb932011-06-24 03:17:00 +000076#include "win32.h"
77#endif
78
Pierre Ossman5156d5e2011-03-09 09:42:34 +000079rfb::LogWriter vlog("main");
80
81using namespace network;
82using namespace rfb;
83using namespace std;
84
Adam Tkac8ac4b302013-01-23 13:55:46 +000085char vncServerName[VNCSERVERNAMELEN] = { '\0' };
86
Pierre Ossmana71a2762015-03-04 09:57:06 +010087static const char *argv0 = NULL;
88
Pierre Ossman5156d5e2011-03-09 09:42:34 +000089static bool exitMainloop = false;
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000090static const char *exitError = NULL;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000091
Pierre Ossmanccd09ba2015-04-07 14:58:45 +020092static const char *about_text()
93{
94 static char buffer[1024];
95
96 // This is used in multiple places with potentially different
97 // encodings, so we need to make sure we get a fresh string every
98 // time.
99 snprintf(buffer, sizeof(buffer),
100 _("TigerVNC Viewer %d-bit v%s\n"
101 "Built on: %s\n"
102 "Copyright (C) 1999-%d TigerVNC Team and many others (see README.txt)\n"
103 "See http://www.tigervnc.org for information on TigerVNC."),
104 (int)sizeof(size_t)*8, PACKAGE_VERSION,
105 BUILD_TIMESTAMP, 2015);
106
107 return buffer;
108}
109
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000110void exit_vncviewer(const char *error)
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000111{
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000112 // Prioritise the first error we get as that is probably the most
113 // relevant one.
114 if ((error != NULL) && (exitError == NULL))
115 exitError = strdup(error);
116
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000117 exitMainloop = true;
118}
119
Pierre Ossman1db73242015-09-23 12:20:32 +0200120bool should_exit()
121{
122 return exitMainloop;
123}
124
Pierre Ossmanb8858222011-04-29 11:51:38 +0000125void about_vncviewer()
126{
127 fl_message_title(_("About TigerVNC Viewer"));
Pierre Ossmanccd09ba2015-04-07 14:58:45 +0200128 fl_message("%s", about_text());
Pierre Ossmanb8858222011-04-29 11:51:38 +0000129}
130
Pierre Ossman8d713a92015-03-04 09:54:27 +0100131#ifdef __APPLE__
Pierre Ossmanb8858222011-04-29 11:51:38 +0000132static void about_callback(Fl_Widget *widget, void *data)
133{
134 about_vncviewer();
135}
Pierre Ossmana71a2762015-03-04 09:57:06 +0100136
137static void new_connection_cb(Fl_Widget *widget, void *data)
138{
139 const char *argv[2];
140 pid_t pid;
141
142 pid = fork();
143 if (pid == -1) {
144 vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno));
145 return;
146 }
147
148 if (pid != 0)
149 return;
150
151 argv[0] = argv0;
152 argv[1] = NULL;
153
154 execvp(argv[0], (char * const *)argv);
155
156 vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno));
157 _exit(1);
158}
Pierre Ossman8d713a92015-03-04 09:54:27 +0100159#endif
Pierre Ossmanb8858222011-04-29 11:51:38 +0000160
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000161static void CleanupSignalHandler(int sig)
162{
163 // CleanupSignalHandler allows C++ object cleanup to happen because it calls
164 // exit() rather than the default which is to abort.
Pierre Ossmanc76bd2b2014-12-03 14:01:31 +0100165 vlog.info(_("Termination signal %d has been received. TigerVNC Viewer will now exit."), sig);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000166 exit(1);
167}
168
169static void init_fltk()
170{
171 // Basic text size (10pt @ 96 dpi => 13px)
172 FL_NORMAL_SIZE = 13;
173
174#ifndef __APPLE__
175 // Select a FLTK scheme and background color that looks somewhat
176 // close to modern Linux and Windows.
177 Fl::scheme("gtk+");
178 Fl::background(220, 220, 220);
179#else
180 // On Mac OS X there is another scheme that fits better though.
181 Fl::scheme("plastic");
182#endif
183
Henrik Andersson3b837032011-09-19 13:46:55 +0000184 // Proper Gnome Shell integration requires that we set a sensible
185 // WM_CLASS for the window.
186 Fl_Window::default_xclass("vncviewer");
187
Pierre Ossman8eb35082012-03-27 12:50:54 +0000188 // Set the default icon for all windows.
Pierre Ossman8eb35082012-03-27 12:50:54 +0000189#ifdef WIN32
190 HICON lg, sm;
191
192 lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
193 IMAGE_ICON, GetSystemMetrics(SM_CXICON),
194 GetSystemMetrics(SM_CYICON),
195 LR_DEFAULTCOLOR | LR_SHARED);
196 sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
197 IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
198 GetSystemMetrics(SM_CYSMICON),
199 LR_DEFAULTCOLOR | LR_SHARED);
200
201 Fl_Window::default_icons(lg, sm);
202#elif ! defined(__APPLE__)
203 const int icon_sizes[] = {48, 32, 24, 16};
204
205 Fl_PNG_Image *icons[4];
206 int count;
207
208 count = 0;
209
210 // FIXME: Follow icon theme specification
211 for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) {
212 char icon_path[PATH_MAX];
213 bool exists;
214
Pierre Ossman6a007bd2012-09-11 10:56:21 +0000215 sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png",
Pierre Ossman8eb35082012-03-27 12:50:54 +0000216 DATA_DIR, icon_sizes[i], icon_sizes[i]);
217
218#ifndef WIN32
219 struct stat st;
220 if (stat(icon_path, &st) != 0)
221#else
222 struct _stat st;
223 if (_stat(icon_path, &st) != 0)
224 return(false);
225#endif
226 exists = false;
227 else
228 exists = true;
229
230 if (exists) {
231 icons[count] = new Fl_PNG_Image(icon_path);
232 if (icons[count]->w() == 0 ||
233 icons[count]->h() == 0 ||
234 icons[count]->d() != 4) {
235 delete icons[count];
236 continue;
237 }
238
239 count++;
240 }
241 }
242
243 Fl_Window::default_icons((const Fl_RGB_Image**)icons, count);
244
245 for (int i = 0;i < count;i++)
246 delete icons[i];
247#endif
Pierre Ossman8eb35082012-03-27 12:50:54 +0000248
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000249 // This makes the "icon" in dialogs rounded, which fits better
250 // with the above schemes.
251 fl_message_icon()->box(FL_UP_BOX);
252
253 // Turn off the annoying behaviour where popups track the mouse.
254 fl_message_hotspot(false);
255
256 // Avoid empty titles for popups
257 fl_message_title_default(_("TigerVNC Viewer"));
258
259#ifdef WIN32
260 // Most "normal" Windows apps use this font for UI elements.
261 Fl::set_font(FL_HELVETICA, "Tahoma");
262#endif
263
264 // FLTK exposes these so that we can translate them.
265 fl_no = _("No");
266 fl_yes = _("Yes");
267 fl_ok = _("OK");
268 fl_cancel = _("Cancel");
269 fl_close = _("Close");
Pierre Ossmanb8858222011-04-29 11:51:38 +0000270
271#ifdef __APPLE__
Peter Åstrandb7c55242011-08-29 13:14:51 +0000272 /* Needs trailing space */
273 static char fltk_about[16];
274 snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
275 Fl_Mac_App_Menu::about = fltk_about;
276 static char fltk_hide[16];
277 snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
278 Fl_Mac_App_Menu::hide = fltk_hide;
279 static char fltk_quit[16];
280 snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
281 Fl_Mac_App_Menu::quit = fltk_quit;
282
Pierre Ossman41ba6032011-06-16 11:09:31 +0000283 Fl_Mac_App_Menu::print = ""; // Don't want the print item
284 Fl_Mac_App_Menu::services = _("Services");
Pierre Ossman41ba6032011-06-16 11:09:31 +0000285 Fl_Mac_App_Menu::hide_others = _("Hide Others");
286 Fl_Mac_App_Menu::show = _("Show All");
Pierre Ossman41ba6032011-06-16 11:09:31 +0000287
Pierre Ossmanb8858222011-04-29 11:51:38 +0000288 fl_mac_set_about(about_callback, NULL);
Pierre Ossmana71a2762015-03-04 09:57:06 +0100289
290 Fl_Sys_Menu_Bar *menubar;
291 char buffer[1024];
292 menubar = new Fl_Sys_Menu_Bar(0, 0, 500, 25);
293 // Fl_Sys_Menu_Bar overrides methods without them being virtual,
294 // which means we cannot use our generic Fl_Menu_ helpers.
295 if (fltk_menu_escape(_("&File"), buffer, sizeof(buffer)) < sizeof(buffer))
296 menubar->add(buffer, 0, 0, 0, FL_SUBMENU);
297 if (fltk_menu_escape(_("&New Connection"), buffer, sizeof(buffer)) < sizeof(buffer))
298 menubar->insert(1, buffer, FL_COMMAND | 'n', new_connection_cb);
Pierre Ossmanb8858222011-04-29 11:51:38 +0000299#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000300}
301
302static void mkvnchomedir()
303{
304 // Create .vnc in the user's home directory if it doesn't already exist
305 char* homeDir = NULL;
306
307 if (getvnchomedir(&homeDir) == -1) {
308 vlog.error(_("Could not create VNC home directory: can't obtain home "
309 "directory path."));
310 } else {
311 int result = mkdir(homeDir, 0755);
312 if (result == -1 && errno != EEXIST)
313 vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
314 delete [] homeDir;
315 }
316}
317
318static void usage(const char *programName)
319{
Pierre Ossman5bf17ad2015-02-17 13:23:00 +0100320#ifdef WIN32
321 // If we don't have a console then we need to create one for output
322 if (GetConsoleWindow() == NULL) {
323 HANDLE handle;
324 int fd;
325
326 AllocConsole();
327
328 handle = GetStdHandle(STD_ERROR_HANDLE);
329 fd = _open_osfhandle((intptr_t)handle, O_TEXT);
330 *stderr = *fdopen(fd, "w");
331 }
332#endif
333
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000334 fprintf(stderr,
335 "\nusage: %s [parameters] [host:displayNum] [parameters]\n"
336 " %s [parameters] -listen [port] [parameters]\n",
337 programName, programName);
338 fprintf(stderr,"\n"
339 "Parameters can be turned on with -<param> or off with -<param>=0\n"
340 "Parameters which take a value can be specified as "
341 "-<param> <value>\n"
342 "Other valid forms are <param>=<value> -<param>=<value> "
343 "--<param>=<value>\n"
344 "Parameter names are case-insensitive. The parameters are:\n\n");
345 Configuration::listParams(79, 14);
Pierre Ossman5bf17ad2015-02-17 13:23:00 +0100346
347#ifdef WIN32
348 // Just wait for the user to kill the console window
349 Sleep(INFINITE);
350#endif
351
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000352 exit(1);
353}
354
Adam Tkac8ac4b302013-01-23 13:55:46 +0000355#ifndef WIN32
356static int
357interpretViaParam(char *remoteHost, int *remotePort, int localPort)
358{
359 const int SERVER_PORT_OFFSET = 5900;
360 char *pos = strchr(vncServerName, ':');
361 if (pos == NULL)
362 *remotePort = SERVER_PORT_OFFSET;
363 else {
364 int portOffset = SERVER_PORT_OFFSET;
365 size_t len;
366 *pos++ = '\0';
367 len = strlen(pos);
368 if (*pos == ':') {
369 /* Two colons is an absolute port number, not an offset. */
370 pos++;
371 len--;
372 portOffset = 0;
373 }
374 if (!len || strspn (pos, "-0123456789") != len )
375 return 1;
376 *remotePort = atoi(pos) + portOffset;
377 }
378
379 if (*vncServerName != '\0')
380 strncpy(remoteHost, vncServerName, VNCSERVERNAMELEN);
381 else
382 strncpy(remoteHost, "localhost", VNCSERVERNAMELEN);
383
384 remoteHost[VNCSERVERNAMELEN - 1] = '\0';
385
386 snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort);
387 vncServerName[VNCSERVERNAMELEN - 1] = '\0';
Adam Tkac8ac4b302013-01-23 13:55:46 +0000388
389 return 0;
390}
391
392static void
393createTunnel(const char *gatewayHost, const char *remoteHost,
394 int remotePort, int localPort)
395{
Pierre Ossmanf8d525b2014-07-09 17:02:27 +0200396 const char *cmd = getenv("VNC_VIA_CMD");
397 char *cmd2, *percent;
Adam Tkac8ac4b302013-01-23 13:55:46 +0000398 char lport[10], rport[10];
399 sprintf(lport, "%d", localPort);
400 sprintf(rport, "%d", remotePort);
401 setenv("G", gatewayHost, 1);
402 setenv("H", remoteHost, 1);
403 setenv("R", rport, 1);
404 setenv("L", lport, 1);
Adam Tkac8ac4b302013-01-23 13:55:46 +0000405 if (!cmd)
406 cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20";
407 /* Compatibility with TigerVNC's method. */
Pierre Ossmanf8d525b2014-07-09 17:02:27 +0200408 cmd2 = strdup(cmd);
409 while ((percent = strchr(cmd2, '%')) != NULL)
Adam Tkac8ac4b302013-01-23 13:55:46 +0000410 *percent = '$';
Pierre Ossmanf8d525b2014-07-09 17:02:27 +0200411 system(cmd2);
412 free(cmd2);
Adam Tkac8ac4b302013-01-23 13:55:46 +0000413}
414
415static int mktunnel()
416{
417 const char *gatewayHost;
418 char remoteHost[VNCSERVERNAMELEN];
419 int localPort = findFreeTcpPort();
420 int remotePort;
421
422 gatewayHost = strDup(via.getValueStr());
423 if (interpretViaParam(remoteHost, &remotePort, localPort) != 0)
424 return 1;
425 createTunnel(gatewayHost, remoteHost, remotePort, localPort);
426
427 return 0;
428}
429#endif /* !WIN32 */
430
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000431int main(int argc, char** argv)
432{
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000433 UserDialog dlg;
434
Pierre Ossmana71a2762015-03-04 09:57:06 +0100435 argv0 = argv[0];
436
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000437 setlocale(LC_ALL, "");
Pierre Ossman0878eca2012-03-27 13:03:22 +0000438 bindtextdomain(PACKAGE_NAME, LOCALE_DIR);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000439 textdomain(PACKAGE_NAME);
440
441 rfb::SecurityClient::setDefaults();
442
443 // Write about text to console, still using normal locale codeset
Pierre Ossmanccd09ba2015-04-07 14:58:45 +0200444 fprintf(stderr,"\n%s\n", about_text());
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000445
446 // Set gettext codeset to what our GUI toolkit uses. Since we are
447 // passing strings from strerror/gai_strerror to the GUI, these must
448 // be in GUI codeset as well.
449 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
450 bind_textdomain_codeset("libc", "UTF-8");
451
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000452 rfb::initStdIOLoggers();
Pierre Ossmanc8719ad2012-04-26 14:27:52 +0000453#ifdef WIN32
454 rfb::initFileLogger("C:\\temp\\vncviewer.log");
455#else
456 rfb::initFileLogger("/tmp/vncviewer.log");
457#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000458 rfb::LogWriter::setLogParams("*:stderr:30");
459
460#ifdef SIGHUP
461 signal(SIGHUP, CleanupSignalHandler);
462#endif
463 signal(SIGINT, CleanupSignalHandler);
464 signal(SIGTERM, CleanupSignalHandler);
465
466 init_fltk();
467
Pierre Ossman6b9622d2014-07-21 16:42:12 +0200468#if !defined(WIN32) && !defined(__APPLE__)
469 fl_open_display();
470 XkbSetDetectableAutoRepeat(fl_display, True, NULL);
471#endif
472
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000473 Configuration::enableViewerParams();
474
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000475 /* Load the default parameter settings */
476 const char* defaultServerName;
477 try {
478 defaultServerName = loadViewerParameters(NULL);
479 } catch (rfb::Exception& e) {
Pierre Ossman3620e192015-03-03 16:50:15 +0100480 defaultServerName = "";
Peter Åstrand8a2b0812012-08-08 11:49:01 +0000481 fl_alert("%s", e.str());
482 }
483
DRC5aa06502011-06-23 22:04:46 +0000484 int i = 1;
485 if (!Fl::args(argc, argv, i) || i < argc)
486 for (; i < argc; i++) {
487 if (Configuration::setParam(argv[i]))
488 continue;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000489
DRC5aa06502011-06-23 22:04:46 +0000490 if (argv[i][0] == '-') {
491 if (i+1 < argc) {
492 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
493 i++;
494 continue;
495 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000496 }
DRC5aa06502011-06-23 22:04:46 +0000497 usage(argv[0]);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000498 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000499
Adam Tkac8ac4b302013-01-23 13:55:46 +0000500 strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
501 vncServerName[VNCSERVERNAMELEN - 1] = '\0';
DRC5aa06502011-06-23 22:04:46 +0000502 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000503
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000504 mkvnchomedir();
505
506 CSecurity::upg = &dlg;
507#ifdef HAVE_GNUTLS
508 CSecurityTLS::msg = &dlg;
509#endif
510
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000511 Socket *sock = NULL;
512
DRC3e7ed812013-02-26 10:34:22 +0000513#ifndef WIN32
Adam Tkac571089b2013-02-19 14:30:32 +0000514 /* Specifying -via and -listen together is nonsense */
515 if (listenMode && strlen(via.getValueStr()) > 0) {
Pierre Ossman744e55c2014-12-03 14:00:54 +0100516 // TRANSLATORS: "Parameters" are command line arguments, or settings
517 // from a file or the Windows registry.
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +0200518 vlog.error(_("Parameters -listen and -via are incompatible"));
519 fl_alert(_("Parameters -listen and -via are incompatible"));
Adam Tkac571089b2013-02-19 14:30:32 +0000520 exit_vncviewer();
521 return 1;
522 }
DRC3e7ed812013-02-26 10:34:22 +0000523#endif
Adam Tkac571089b2013-02-19 14:30:32 +0000524
525 if (listenMode) {
Tim Waugh892d10a2015-03-11 13:12:07 +0000526 std::list<TcpListener> listeners;
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000527 try {
528 int port = 5500;
529 if (isdigit(vncServerName[0]))
530 port = atoi(vncServerName);
531
Tim Waugh892d10a2015-03-11 13:12:07 +0000532 createTcpListeners(&listeners, 0, port);
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000533
Peter Åstrand (astrand)4ab2f8a2015-03-18 10:31:01 +0100534 vlog.info(_("Listening on port %d"), port);
Tim Waugh892d10a2015-03-11 13:12:07 +0000535
536 /* Wait for a connection */
537 while (sock == NULL) {
538 fd_set rfds;
539 FD_ZERO(&rfds);
540 for (std::list<TcpListener>::iterator i = listeners.begin();
541 i != listeners.end();
542 i++)
543 FD_SET((*i).getFd(), &rfds);
544
545 int n = select(FD_SETSIZE, &rfds, 0, 0, 0);
546 if (n < 0) {
547 if (errno == EINTR) {
548 vlog.debug("Interrupted select() system call");
549 continue;
550 } else {
551 throw rdr::SystemException("select", errno);
552 }
553 }
554
555 for (std::list<TcpListener>::iterator i = listeners.begin ();
556 i != listeners.end();
557 i++)
558 if (FD_ISSET((*i).getFd(), &rfds)) {
559 sock = (*i).accept();
560 if (sock)
561 /* Got a connection */
562 break;
563 }
564 }
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000565 } catch (rdr::Exception& e) {
566 vlog.error("%s", e.str());
567 fl_alert("%s", e.str());
568 exit_vncviewer();
569 return 1;
570 }
571
572 } else {
573 if (vncServerName[0] == '\0') {
574 ServerDialog::run(defaultServerName, vncServerName);
575 if (vncServerName[0] == '\0')
576 return 1;
577 }
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000578
Adam Tkac8ac4b302013-01-23 13:55:46 +0000579#ifndef WIN32
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000580 if (strlen (via.getValueStr()) > 0 && mktunnel() != 0)
581 usage(argv[0]);
Adam Tkac8ac4b302013-01-23 13:55:46 +0000582#endif
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000583 }
Adam Tkac8ac4b302013-01-23 13:55:46 +0000584
Pierre Ossman2a7a8d62013-02-15 08:33:39 +0000585 CConn *cc = new CConn(vncServerName, sock);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000586
587 while (!exitMainloop) {
588 int next_timer;
589
590 next_timer = Timer::checkTimeouts();
591 if (next_timer == 0)
592 next_timer = INT_MAX;
593
594 if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
595 vlog.error(_("Internal FLTK error. Exiting."));
596 break;
597 }
598 }
599
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000600 delete cc;
601
602 if (exitError != NULL)
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000603 fl_alert("%s", exitError);
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000604
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000605 return 0;
606}