You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncviewer.cxx 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  3. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  4. *
  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. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <assert.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <signal.h>
  30. #include <locale.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <sys/stat.h>
  34. #ifdef WIN32
  35. #include <os/winerrno.h>
  36. #include <direct.h>
  37. #define mkdir(path, mode) _mkdir(path)
  38. #endif
  39. #ifdef __APPLE__
  40. #include <Carbon/Carbon.h>
  41. #endif
  42. #if !defined(WIN32) && !defined(__APPLE__)
  43. #include <X11/Xlib.h>
  44. #include <X11/XKBlib.h>
  45. #endif
  46. #include <rfb/Logger_stdio.h>
  47. #ifdef HAVE_GNUTLS
  48. #include <rfb/CSecurityTLS.h>
  49. #endif
  50. #include <rfb/LogWriter.h>
  51. #include <rfb/Timer.h>
  52. #include <rfb/Exception.h>
  53. #include <rdr/Exception.h>
  54. #include <network/TcpSocket.h>
  55. #include <os/os.h>
  56. #include <FL/Fl_PNG_Image.H>
  57. #include <FL/Fl_Sys_Menu_Bar.H>
  58. #include <FL/fl_ask.H>
  59. #include <FL/x.H>
  60. #include "fltk/theme.h"
  61. #include "fltk/util.h"
  62. #include "i18n.h"
  63. #include "parameters.h"
  64. #include "CConn.h"
  65. #include "ServerDialog.h"
  66. #include "UserDialog.h"
  67. #include "touch.h"
  68. #include "vncviewer.h"
  69. #ifdef WIN32
  70. #include "resource.h"
  71. #include "win32.h"
  72. #endif
  73. static rfb::LogWriter vlog("main");
  74. using namespace network;
  75. using namespace rfb;
  76. char vncServerName[VNCSERVERNAMELEN] = { '\0' };
  77. static const char *argv0 = NULL;
  78. static bool inMainloop = false;
  79. static bool exitMainloop = false;
  80. static char *exitError = NULL;
  81. static bool fatalError = false;
  82. static const char *about_text()
  83. {
  84. static char buffer[1024];
  85. // This is used in multiple places with potentially different
  86. // encodings, so we need to make sure we get a fresh string every
  87. // time.
  88. snprintf(buffer, sizeof(buffer),
  89. _("TigerVNC Viewer v%s\n"
  90. "Built on: %s\n"
  91. "Copyright (C) 1999-%d TigerVNC Team and many others (see README.rst)\n"
  92. "See https://www.tigervnc.org for information on TigerVNC."),
  93. PACKAGE_VERSION, BUILD_TIMESTAMP, 2024);
  94. return buffer;
  95. }
  96. void abort_vncviewer(const char *error, ...)
  97. {
  98. fatalError = true;
  99. // Prioritise the first error we get as that is probably the most
  100. // relevant one.
  101. if (exitError == NULL) {
  102. va_list ap;
  103. va_start(ap, error);
  104. exitError = (char*)malloc(1024);
  105. vsnprintf(exitError, 1024, error, ap);
  106. va_end(ap);
  107. }
  108. if (inMainloop)
  109. exitMainloop = true;
  110. else {
  111. // We're early in the startup. Assume we can just exit().
  112. if (alertOnFatalError && (exitError != NULL))
  113. fl_alert("%s", exitError);
  114. exit(EXIT_FAILURE);
  115. }
  116. }
  117. void abort_connection(const char *error, ...)
  118. {
  119. assert(inMainloop);
  120. // Prioritise the first error we get as that is probably the most
  121. // relevant one.
  122. if (exitError == NULL) {
  123. va_list ap;
  124. va_start(ap, error);
  125. exitError = (char*)malloc(1024);
  126. vsnprintf(exitError, 1024, error, ap);
  127. va_end(ap);
  128. }
  129. exitMainloop = true;
  130. }
  131. void abort_connection_with_unexpected_error(const rdr::Exception &e) {
  132. abort_connection(_("An unexpected error occurred when communicating "
  133. "with the server:\n\n%s"), e.str());
  134. }
  135. void disconnect()
  136. {
  137. exitMainloop = true;
  138. }
  139. bool should_disconnect()
  140. {
  141. return exitMainloop;
  142. }
  143. void about_vncviewer()
  144. {
  145. fl_message_title(_("About TigerVNC Viewer"));
  146. fl_message("%s", about_text());
  147. }
  148. static void mainloop(const char* vncserver, network::Socket* sock)
  149. {
  150. while (true) {
  151. CConn *cc;
  152. exitMainloop = false;
  153. cc = new CConn(vncserver, sock);
  154. while (!exitMainloop) {
  155. int next_timer;
  156. next_timer = Timer::checkTimeouts();
  157. if (next_timer == 0)
  158. next_timer = INT_MAX;
  159. if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
  160. vlog.error(_("Internal FLTK error. Exiting."));
  161. exit(-1);
  162. }
  163. }
  164. delete cc;
  165. if (fatalError) {
  166. assert(exitError != NULL);
  167. if (alertOnFatalError)
  168. fl_alert("%s", exitError);
  169. break;
  170. }
  171. if (exitError == NULL)
  172. break;
  173. if(reconnectOnError && (sock == NULL)) {
  174. int ret;
  175. ret = fl_choice(_("%s\n\n"
  176. "Attempt to reconnect?"),
  177. NULL, fl_yes, fl_no, exitError);
  178. free(exitError);
  179. exitError = NULL;
  180. if (ret == 1)
  181. continue;
  182. else
  183. break;
  184. }
  185. if (alertOnFatalError)
  186. fl_alert("%s", exitError);
  187. break;
  188. }
  189. }
  190. #ifdef __APPLE__
  191. static void about_callback(Fl_Widget* /*widget*/, void* /*data*/)
  192. {
  193. about_vncviewer();
  194. }
  195. static void new_connection_cb(Fl_Widget* /*widget*/, void* /*data*/)
  196. {
  197. const char *argv[2];
  198. pid_t pid;
  199. pid = fork();
  200. if (pid == -1) {
  201. vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno));
  202. return;
  203. }
  204. if (pid != 0)
  205. return;
  206. argv[0] = argv0;
  207. argv[1] = NULL;
  208. execvp(argv[0], (char * const *)argv);
  209. vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno));
  210. _exit(1);
  211. }
  212. #endif
  213. static void CleanupSignalHandler(int sig)
  214. {
  215. // CleanupSignalHandler allows C++ object cleanup to happen because it calls
  216. // exit() rather than the default which is to abort.
  217. vlog.info(_("Termination signal %d has been received. TigerVNC Viewer will now exit."), sig);
  218. exit(1);
  219. }
  220. static const char* getlocaledir()
  221. {
  222. #if defined(WIN32)
  223. static char localebuf[PATH_MAX];
  224. char *slash;
  225. GetModuleFileName(NULL, localebuf, sizeof(localebuf));
  226. slash = strrchr(localebuf, '\\');
  227. if (slash == NULL)
  228. return NULL;
  229. *slash = '\0';
  230. if ((strlen(localebuf) + strlen("\\locale")) >= sizeof(localebuf))
  231. return NULL;
  232. strcat(localebuf, "\\locale");
  233. return localebuf;
  234. #elif defined(__APPLE__)
  235. CFBundleRef bundle;
  236. CFURLRef localeurl;
  237. CFStringRef localestr;
  238. Boolean ret;
  239. static char localebuf[PATH_MAX];
  240. bundle = CFBundleGetMainBundle();
  241. if (bundle == NULL)
  242. return NULL;
  243. localeurl = CFBundleCopyResourceURL(bundle, CFSTR("locale"),
  244. NULL, NULL);
  245. if (localeurl == NULL)
  246. return NULL;
  247. localestr = CFURLCopyFileSystemPath(localeurl, kCFURLPOSIXPathStyle);
  248. CFRelease(localeurl);
  249. ret = CFStringGetCString(localestr, localebuf, sizeof(localebuf),
  250. kCFStringEncodingUTF8);
  251. if (!ret)
  252. return NULL;
  253. return localebuf;
  254. #else
  255. return CMAKE_INSTALL_FULL_LOCALEDIR;
  256. #endif
  257. }
  258. static void init_fltk()
  259. {
  260. // Adjust look of FLTK
  261. init_theme();
  262. // Proper Gnome Shell integration requires that we set a sensible
  263. // WM_CLASS for the window.
  264. Fl_Window::default_xclass("vncviewer");
  265. // Set the default icon for all windows.
  266. #ifdef WIN32
  267. HICON lg, sm;
  268. lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
  269. IMAGE_ICON, GetSystemMetrics(SM_CXICON),
  270. GetSystemMetrics(SM_CYICON),
  271. LR_DEFAULTCOLOR | LR_SHARED);
  272. sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
  273. IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
  274. GetSystemMetrics(SM_CYSMICON),
  275. LR_DEFAULTCOLOR | LR_SHARED);
  276. Fl_Window::default_icons(lg, sm);
  277. #elif ! defined(__APPLE__)
  278. const int icon_sizes[] = {128, 64, 48, 32, 24, 22, 16};
  279. Fl_PNG_Image *icons[sizeof(icon_sizes)/sizeof(icon_sizes[0])];
  280. int count;
  281. count = 0;
  282. // FIXME: Follow icon theme specification
  283. for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) {
  284. char icon_path[PATH_MAX];
  285. bool exists;
  286. sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png",
  287. CMAKE_INSTALL_FULL_DATADIR, icon_sizes[i], icon_sizes[i]);
  288. struct stat st;
  289. if (stat(icon_path, &st) != 0)
  290. exists = false;
  291. else
  292. exists = true;
  293. if (exists) {
  294. icons[count] = new Fl_PNG_Image(icon_path);
  295. if (icons[count]->w() == 0 ||
  296. icons[count]->h() == 0 ||
  297. icons[count]->d() != 4) {
  298. delete icons[count];
  299. continue;
  300. }
  301. count++;
  302. }
  303. }
  304. Fl_Window::default_icons((const Fl_RGB_Image**)icons, count);
  305. for (int i = 0;i < count;i++)
  306. delete icons[i];
  307. #endif
  308. // Turn off the annoying behaviour where popups track the mouse.
  309. fl_message_hotspot(false);
  310. // Avoid empty titles for popups
  311. fl_message_title_default(_("TigerVNC Viewer"));
  312. // FLTK exposes these so that we can translate them.
  313. fl_no = _("No");
  314. fl_yes = _("Yes");
  315. fl_ok = _("OK");
  316. fl_cancel = _("Cancel");
  317. fl_close = _("Close");
  318. #ifdef __APPLE__
  319. /* Needs trailing space */
  320. static char fltk_about[16];
  321. snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
  322. Fl_Mac_App_Menu::about = fltk_about;
  323. static char fltk_hide[16];
  324. snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
  325. Fl_Mac_App_Menu::hide = fltk_hide;
  326. static char fltk_quit[16];
  327. snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
  328. Fl_Mac_App_Menu::quit = fltk_quit;
  329. Fl_Mac_App_Menu::print = ""; // Don't want the print item
  330. Fl_Mac_App_Menu::services = _("Services");
  331. Fl_Mac_App_Menu::hide_others = _("Hide Others");
  332. Fl_Mac_App_Menu::show = _("Show All");
  333. fl_mac_set_about(about_callback, NULL);
  334. Fl_Sys_Menu_Bar *menubar;
  335. char buffer[1024];
  336. menubar = new Fl_Sys_Menu_Bar(0, 0, 500, 25);
  337. // Fl_Sys_Menu_Bar overrides methods without them being virtual,
  338. // which means we cannot use our generic Fl_Menu_ helpers.
  339. if (fltk_menu_escape(p_("SysMenu|", "&File"),
  340. buffer, sizeof(buffer)) < sizeof(buffer))
  341. menubar->add(buffer, 0, 0, 0, FL_SUBMENU);
  342. if (fltk_menu_escape(p_("SysMenu|File|", "&New Connection"),
  343. buffer, sizeof(buffer)) < sizeof(buffer))
  344. menubar->insert(1, buffer, FL_COMMAND | 'n', new_connection_cb);
  345. #endif
  346. }
  347. static void mkvnchomedir()
  348. {
  349. // Create .vnc in the user's home directory if it doesn't already exist
  350. const char* homeDir = os::getvnchomedir();
  351. if (homeDir == NULL) {
  352. vlog.error(_("Could not obtain the home directory path"));
  353. } else {
  354. int result = mkdir(homeDir, 0755);
  355. if (result == -1 && errno != EEXIST)
  356. vlog.error(_("Could not create VNC home directory: %s"), strerror(errno));
  357. }
  358. }
  359. static void usage(const char *programName)
  360. {
  361. #ifdef WIN32
  362. // If we don't have a console then we need to create one for output
  363. if (GetConsoleWindow() == NULL) {
  364. HANDLE handle;
  365. int fd;
  366. AllocConsole();
  367. handle = GetStdHandle(STD_ERROR_HANDLE);
  368. fd = _open_osfhandle((intptr_t)handle, O_TEXT);
  369. *stderr = *fdopen(fd, "w");
  370. }
  371. #endif
  372. fprintf(stderr,
  373. "\n"
  374. "usage: %s [parameters] [host][:displayNum]\n"
  375. " %s [parameters] [host][::port]\n"
  376. #ifndef WIN32
  377. " %s [parameters] [unix socket]\n"
  378. #endif
  379. " %s [parameters] -listen [port]\n"
  380. " %s [parameters] [.tigervnc file]\n",
  381. programName, programName,
  382. #ifndef WIN32
  383. programName,
  384. #endif
  385. programName, programName);
  386. #if !defined(WIN32) && !defined(__APPLE__)
  387. fprintf(stderr,"\n"
  388. "Options:\n\n"
  389. " -display Xdisplay - Specifies the X display for the viewer window\n"
  390. " -geometry geometry - Initial position of the main VNC viewer window. See the\n"
  391. " man page for details.\n");
  392. #endif
  393. fprintf(stderr,"\n"
  394. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  395. "Parameters which take a value can be specified as "
  396. "-<param> <value>\n"
  397. "Other valid forms are <param>=<value> -<param>=<value> "
  398. "--<param>=<value>\n"
  399. "Parameter names are case-insensitive. The parameters are:\n\n");
  400. Configuration::listParams(79, 14);
  401. #ifdef WIN32
  402. // Just wait for the user to kill the console window
  403. Sleep(INFINITE);
  404. #endif
  405. exit(1);
  406. }
  407. static void
  408. potentiallyLoadConfigurationFile(char *vncServerName)
  409. {
  410. const bool hasPathSeparator = (strchr(vncServerName, '/') != NULL ||
  411. (strchr(vncServerName, '\\')) != NULL);
  412. if (hasPathSeparator) {
  413. #ifndef WIN32
  414. struct stat sb;
  415. // This might be a UNIX socket, we need to check
  416. if (stat(vncServerName, &sb) == -1) {
  417. // Some access problem; let loadViewerParameters() deal with it...
  418. } else {
  419. if ((sb.st_mode & S_IFMT) == S_IFSOCK)
  420. return;
  421. }
  422. #endif
  423. try {
  424. const char* newServerName;
  425. newServerName = loadViewerParameters(vncServerName);
  426. // This might be empty, but we still need to clear it so we
  427. // don't try to connect to the filename
  428. strncpy(vncServerName, newServerName, VNCSERVERNAMELEN-1);
  429. vncServerName[VNCSERVERNAMELEN-1] = '\0';
  430. } catch (rfb::Exception& e) {
  431. vlog.error("%s", e.str());
  432. abort_vncviewer(_("Unable to load the specified configuration "
  433. "file:\n\n%s"), e.str());
  434. }
  435. }
  436. }
  437. static void
  438. migrateDeprecatedOptions()
  439. {
  440. if (fullScreenAllMonitors) {
  441. vlog.info(_("FullScreenAllMonitors is deprecated, set FullScreenMode to 'all' instead"));
  442. fullScreenMode.setParam("all");
  443. }
  444. }
  445. #ifndef WIN32
  446. static int
  447. interpretViaParam(char *remoteHost, int *remotePort, int localPort)
  448. {
  449. const int SERVER_PORT_OFFSET = 5900;
  450. char *pos = strchr(vncServerName, ':');
  451. if (pos == NULL)
  452. *remotePort = SERVER_PORT_OFFSET;
  453. else {
  454. int portOffset = SERVER_PORT_OFFSET;
  455. size_t len;
  456. *pos++ = '\0';
  457. len = strlen(pos);
  458. if (*pos == ':') {
  459. /* Two colons is an absolute port number, not an offset. */
  460. pos++;
  461. len--;
  462. portOffset = 0;
  463. }
  464. if (!len || strspn (pos, "-0123456789") != len )
  465. return 1;
  466. *remotePort = atoi(pos) + portOffset;
  467. }
  468. if (*vncServerName != '\0')
  469. strcpy(remoteHost, vncServerName);
  470. else
  471. strcpy(remoteHost, "localhost");
  472. snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort);
  473. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  474. return 0;
  475. }
  476. static void
  477. createTunnel(const char *gatewayHost, const char *remoteHost,
  478. int remotePort, int localPort)
  479. {
  480. const char *cmd = getenv("VNC_VIA_CMD");
  481. char *cmd2, *percent;
  482. char lport[10], rport[10];
  483. sprintf(lport, "%d", localPort);
  484. sprintf(rport, "%d", remotePort);
  485. setenv("G", gatewayHost, 1);
  486. setenv("H", remoteHost, 1);
  487. setenv("R", rport, 1);
  488. setenv("L", lport, 1);
  489. if (!cmd)
  490. cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20";
  491. /* Compatibility with TigerVNC's method. */
  492. cmd2 = strdup(cmd);
  493. while ((percent = strchr(cmd2, '%')) != NULL)
  494. *percent = '$';
  495. system(cmd2);
  496. free(cmd2);
  497. }
  498. static int mktunnel()
  499. {
  500. const char *gatewayHost;
  501. char remoteHost[VNCSERVERNAMELEN];
  502. int localPort = findFreeTcpPort();
  503. int remotePort;
  504. if (interpretViaParam(remoteHost, &remotePort, localPort) != 0)
  505. return 1;
  506. gatewayHost = (const char*)via;
  507. createTunnel(gatewayHost, remoteHost, remotePort, localPort);
  508. return 0;
  509. }
  510. #endif /* !WIN32 */
  511. int main(int argc, char** argv)
  512. {
  513. const char *localedir;
  514. UserDialog dlg;
  515. argv0 = argv[0];
  516. setlocale(LC_ALL, "");
  517. localedir = getlocaledir();
  518. if (localedir == NULL)
  519. fprintf(stderr, "Failed to determine locale directory\n");
  520. else
  521. bindtextdomain(PACKAGE_NAME, localedir);
  522. textdomain(PACKAGE_NAME);
  523. // Write about text to console, still using normal locale codeset
  524. fprintf(stderr,"\n%s\n", about_text());
  525. // Set gettext codeset to what our GUI toolkit uses. Since we are
  526. // passing strings from strerror/gai_strerror to the GUI, these must
  527. // be in GUI codeset as well.
  528. bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
  529. bind_textdomain_codeset("libc", "UTF-8");
  530. rfb::initStdIOLoggers();
  531. #ifdef WIN32
  532. rfb::initFileLogger("C:\\temp\\vncviewer.log");
  533. #else
  534. rfb::initFileLogger("/tmp/vncviewer.log");
  535. #endif
  536. rfb::LogWriter::setLogParams("*:stderr:30");
  537. #ifdef SIGHUP
  538. signal(SIGHUP, CleanupSignalHandler);
  539. #endif
  540. signal(SIGINT, CleanupSignalHandler);
  541. signal(SIGTERM, CleanupSignalHandler);
  542. Configuration::enableViewerParams();
  543. /* Load the default parameter settings */
  544. char defaultServerName[VNCSERVERNAMELEN] = "";
  545. try {
  546. const char* configServerName;
  547. configServerName = loadViewerParameters(NULL);
  548. if (configServerName != NULL) {
  549. strncpy(defaultServerName, configServerName, VNCSERVERNAMELEN-1);
  550. defaultServerName[VNCSERVERNAMELEN-1] = '\0';
  551. }
  552. } catch (rfb::Exception& e) {
  553. vlog.error("%s", e.str());
  554. }
  555. for (int i = 1; i < argc;) {
  556. /* We need to resolve an ambiguity for booleans */
  557. if (argv[i][0] == '-' && i+1 < argc) {
  558. VoidParameter *param;
  559. param = Configuration::getParam(&argv[i][1]);
  560. if ((param != NULL) &&
  561. (dynamic_cast<BoolParameter*>(param) != NULL)) {
  562. if ((strcasecmp(argv[i+1], "0") == 0) ||
  563. (strcasecmp(argv[i+1], "1") == 0) ||
  564. (strcasecmp(argv[i+1], "true") == 0) ||
  565. (strcasecmp(argv[i+1], "false") == 0) ||
  566. (strcasecmp(argv[i+1], "yes") == 0) ||
  567. (strcasecmp(argv[i+1], "no") == 0)) {
  568. param->setParam(argv[i+1]);
  569. i += 2;
  570. continue;
  571. }
  572. }
  573. }
  574. if (Configuration::setParam(argv[i])) {
  575. i++;
  576. continue;
  577. }
  578. if (argv[i][0] == '-') {
  579. if (i+1 < argc) {
  580. if (Configuration::setParam(&argv[i][1], argv[i+1])) {
  581. i += 2;
  582. continue;
  583. }
  584. }
  585. usage(argv[0]);
  586. }
  587. strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
  588. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  589. i++;
  590. }
  591. #if !defined(WIN32) && !defined(__APPLE__)
  592. if (strcmp(display, "") != 0) {
  593. Fl::display(display);
  594. }
  595. fl_open_display();
  596. XkbSetDetectableAutoRepeat(fl_display, True, NULL);
  597. #endif
  598. init_fltk();
  599. enable_touch();
  600. // Check if the server name in reality is a configuration file
  601. potentiallyLoadConfigurationFile(vncServerName);
  602. migrateDeprecatedOptions();
  603. mkvnchomedir();
  604. CSecurity::upg = &dlg;
  605. #if defined(HAVE_GNUTLS) || defined(HAVE_NETTLE)
  606. CSecurity::msg = &dlg;
  607. #endif
  608. Socket *sock = NULL;
  609. #ifndef WIN32
  610. /* Specifying -via and -listen together is nonsense */
  611. if (listenMode && strlen(via) > 0) {
  612. // TRANSLATORS: "Parameters" are command line arguments, or settings
  613. // from a file or the Windows registry.
  614. vlog.error(_("Parameters -listen and -via are incompatible"));
  615. abort_vncviewer(_("Parameters -listen and -via are incompatible"));
  616. return 1; /* Not reached */
  617. }
  618. #endif
  619. if (listenMode) {
  620. std::list<SocketListener*> listeners;
  621. try {
  622. int port = 5500;
  623. if (isdigit(vncServerName[0]))
  624. port = atoi(vncServerName);
  625. createTcpListeners(&listeners, 0, port);
  626. if (listeners.empty())
  627. throw Exception(_("Unable to listen for incoming connections"));
  628. vlog.info(_("Listening on port %d"), port);
  629. /* Wait for a connection */
  630. while (sock == NULL) {
  631. fd_set rfds;
  632. FD_ZERO(&rfds);
  633. for (std::list<SocketListener*>::iterator i = listeners.begin();
  634. i != listeners.end();
  635. i++)
  636. FD_SET((*i)->getFd(), &rfds);
  637. int n = select(FD_SETSIZE, &rfds, 0, 0, 0);
  638. if (n < 0) {
  639. if (errno == EINTR) {
  640. vlog.debug("Interrupted select() system call");
  641. continue;
  642. } else {
  643. throw rdr::SystemException("select", errno);
  644. }
  645. }
  646. for (std::list<SocketListener*>::iterator i = listeners.begin ();
  647. i != listeners.end();
  648. i++)
  649. if (FD_ISSET((*i)->getFd(), &rfds)) {
  650. sock = (*i)->accept();
  651. if (sock)
  652. /* Got a connection */
  653. break;
  654. }
  655. }
  656. } catch (rdr::Exception& e) {
  657. vlog.error("%s", e.str());
  658. abort_vncviewer(_("Failure waiting for incoming VNC connection:\n\n%s"), e.str());
  659. return 1; /* Not reached */
  660. }
  661. while (!listeners.empty()) {
  662. delete listeners.back();
  663. listeners.pop_back();
  664. }
  665. } else {
  666. if (vncServerName[0] == '\0') {
  667. ServerDialog::run(defaultServerName, vncServerName);
  668. if (vncServerName[0] == '\0')
  669. return 1;
  670. }
  671. #ifndef WIN32
  672. if (strlen(via) > 0 && mktunnel() != 0)
  673. usage(argv[0]);
  674. #endif
  675. }
  676. inMainloop = true;
  677. mainloop(vncServerName, sock);
  678. inMainloop = false;
  679. return 0;
  680. }