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

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