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

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