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

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