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

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