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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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[] = {128, 64, 48, 32, 24, 22, 16};
  289. Fl_PNG_Image *icons[sizeof(icon_sizes)/sizeof(icon_sizes[0])];
  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. struct stat st;
  299. if (stat(icon_path, &st) != 0)
  300. exists = false;
  301. else
  302. exists = true;
  303. if (exists) {
  304. icons[count] = new Fl_PNG_Image(icon_path);
  305. if (icons[count]->w() == 0 ||
  306. icons[count]->h() == 0 ||
  307. icons[count]->d() != 4) {
  308. delete icons[count];
  309. continue;
  310. }
  311. count++;
  312. }
  313. }
  314. Fl_Window::default_icons((const Fl_RGB_Image**)icons, count);
  315. for (int i = 0;i < count;i++)
  316. delete icons[i];
  317. #endif
  318. // This makes the "icon" in dialogs rounded, which fits better
  319. // with the above schemes.
  320. fl_message_icon()->box(FL_UP_BOX);
  321. // Turn off the annoying behaviour where popups track the mouse.
  322. fl_message_hotspot(false);
  323. // Avoid empty titles for popups
  324. fl_message_title_default(_("TigerVNC Viewer"));
  325. #ifdef WIN32
  326. // Most "normal" Windows apps use this font for UI elements.
  327. Fl::set_font(FL_HELVETICA, "Tahoma");
  328. #endif
  329. // FLTK exposes these so that we can translate them.
  330. fl_no = _("No");
  331. fl_yes = _("Yes");
  332. fl_ok = _("OK");
  333. fl_cancel = _("Cancel");
  334. fl_close = _("Close");
  335. #ifdef __APPLE__
  336. /* Needs trailing space */
  337. static char fltk_about[16];
  338. snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
  339. Fl_Mac_App_Menu::about = fltk_about;
  340. static char fltk_hide[16];
  341. snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
  342. Fl_Mac_App_Menu::hide = fltk_hide;
  343. static char fltk_quit[16];
  344. snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
  345. Fl_Mac_App_Menu::quit = fltk_quit;
  346. Fl_Mac_App_Menu::print = ""; // Don't want the print item
  347. Fl_Mac_App_Menu::services = _("Services");
  348. Fl_Mac_App_Menu::hide_others = _("Hide Others");
  349. Fl_Mac_App_Menu::show = _("Show All");
  350. fl_mac_set_about(about_callback, NULL);
  351. Fl_Sys_Menu_Bar *menubar;
  352. char buffer[1024];
  353. menubar = new Fl_Sys_Menu_Bar(0, 0, 500, 25);
  354. // Fl_Sys_Menu_Bar overrides methods without them being virtual,
  355. // which means we cannot use our generic Fl_Menu_ helpers.
  356. if (fltk_menu_escape(p_("SysMenu|", "&File"),
  357. buffer, sizeof(buffer)) < sizeof(buffer))
  358. menubar->add(buffer, 0, 0, 0, FL_SUBMENU);
  359. if (fltk_menu_escape(p_("SysMenu|File|", "&New Connection"),
  360. buffer, sizeof(buffer)) < sizeof(buffer))
  361. menubar->insert(1, buffer, FL_COMMAND | 'n', new_connection_cb);
  362. #endif
  363. }
  364. static void mkvnchomedir()
  365. {
  366. // Create .vnc in the user's home directory if it doesn't already exist
  367. char* homeDir = NULL;
  368. if (getvnchomedir(&homeDir) == -1) {
  369. vlog.error(_("Could not obtain the home directory path"));
  370. } else {
  371. int result = mkdir(homeDir, 0755);
  372. if (result == -1 && errno != EEXIST)
  373. vlog.error(_("Could not create VNC home directory: %s"), strerror(errno));
  374. delete [] homeDir;
  375. }
  376. }
  377. static void usage(const char *programName)
  378. {
  379. #ifdef WIN32
  380. // If we don't have a console then we need to create one for output
  381. if (GetConsoleWindow() == NULL) {
  382. HANDLE handle;
  383. int fd;
  384. AllocConsole();
  385. handle = GetStdHandle(STD_ERROR_HANDLE);
  386. fd = _open_osfhandle((intptr_t)handle, O_TEXT);
  387. *stderr = *fdopen(fd, "w");
  388. }
  389. #endif
  390. fprintf(stderr,
  391. "\n"
  392. "usage: %s [parameters] [host][:displayNum]\n"
  393. " %s [parameters] [host][::port]\n"
  394. #ifndef WIN32
  395. " %s [parameters] [unix socket]\n"
  396. #endif
  397. " %s [parameters] -listen [port]\n"
  398. " %s [parameters] [.tigervnc file]\n",
  399. programName, programName,
  400. #ifndef WIN32
  401. programName,
  402. #endif
  403. programName, programName);
  404. #if !defined(WIN32) && !defined(__APPLE__)
  405. fprintf(stderr,"\n"
  406. "Options:\n\n"
  407. " -display Xdisplay - Specifies the X display for the viewer window\n"
  408. " -geometry geometry - Initial position of the main VNC viewer window. See the\n"
  409. " man page for details.\n");
  410. #endif
  411. fprintf(stderr,"\n"
  412. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  413. "Parameters which take a value can be specified as "
  414. "-<param> <value>\n"
  415. "Other valid forms are <param>=<value> -<param>=<value> "
  416. "--<param>=<value>\n"
  417. "Parameter names are case-insensitive. The parameters are:\n\n");
  418. Configuration::listParams(79, 14);
  419. #ifdef WIN32
  420. // Just wait for the user to kill the console window
  421. Sleep(INFINITE);
  422. #endif
  423. exit(1);
  424. }
  425. static void
  426. potentiallyLoadConfigurationFile(char *vncServerName)
  427. {
  428. const bool hasPathSeparator = (strchr(vncServerName, '/') != NULL ||
  429. (strchr(vncServerName, '\\')) != NULL);
  430. if (hasPathSeparator) {
  431. #ifndef WIN32
  432. struct stat sb;
  433. // This might be a UNIX socket, we need to check
  434. if (stat(vncServerName, &sb) == -1) {
  435. // Some access problem; let loadViewerParameters() deal with it...
  436. } else {
  437. if ((sb.st_mode & S_IFMT) == S_IFSOCK)
  438. return;
  439. }
  440. #endif
  441. try {
  442. const char* newServerName;
  443. newServerName = loadViewerParameters(vncServerName);
  444. // This might be empty, but we still need to clear it so we
  445. // don't try to connect to the filename
  446. strncpy(vncServerName, newServerName, VNCSERVERNAMELEN-1);
  447. vncServerName[VNCSERVERNAMELEN-1] = '\0';
  448. } catch (rfb::Exception& e) {
  449. vlog.error("%s", e.str());
  450. abort_vncviewer(_("Unable to load the specified configuration "
  451. "file:\n\n%s"), e.str());
  452. }
  453. }
  454. }
  455. static void
  456. migrateDeprecatedOptions()
  457. {
  458. if (fullScreenAllMonitors) {
  459. vlog.info(_("FullScreenAllMonitors is deprecated, set FullScreenMode to 'all' instead"));
  460. fullScreenMode.setParam("all");
  461. }
  462. }
  463. #ifndef WIN32
  464. static int
  465. interpretViaParam(char *remoteHost, int *remotePort, int localPort)
  466. {
  467. const int SERVER_PORT_OFFSET = 5900;
  468. char *pos = strchr(vncServerName, ':');
  469. if (pos == NULL)
  470. *remotePort = SERVER_PORT_OFFSET;
  471. else {
  472. int portOffset = SERVER_PORT_OFFSET;
  473. size_t len;
  474. *pos++ = '\0';
  475. len = strlen(pos);
  476. if (*pos == ':') {
  477. /* Two colons is an absolute port number, not an offset. */
  478. pos++;
  479. len--;
  480. portOffset = 0;
  481. }
  482. if (!len || strspn (pos, "-0123456789") != len )
  483. return 1;
  484. *remotePort = atoi(pos) + portOffset;
  485. }
  486. if (*vncServerName != '\0')
  487. strcpy(remoteHost, vncServerName);
  488. else
  489. strcpy(remoteHost, "localhost");
  490. snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort);
  491. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  492. return 0;
  493. }
  494. static void
  495. createTunnel(const char *gatewayHost, const char *remoteHost,
  496. int remotePort, int localPort)
  497. {
  498. const char *cmd = getenv("VNC_VIA_CMD");
  499. char *cmd2, *percent;
  500. char lport[10], rport[10];
  501. sprintf(lport, "%d", localPort);
  502. sprintf(rport, "%d", remotePort);
  503. setenv("G", gatewayHost, 1);
  504. setenv("H", remoteHost, 1);
  505. setenv("R", rport, 1);
  506. setenv("L", lport, 1);
  507. if (!cmd)
  508. cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20";
  509. /* Compatibility with TigerVNC's method. */
  510. cmd2 = strdup(cmd);
  511. while ((percent = strchr(cmd2, '%')) != NULL)
  512. *percent = '$';
  513. system(cmd2);
  514. free(cmd2);
  515. }
  516. static int mktunnel()
  517. {
  518. const char *gatewayHost;
  519. char remoteHost[VNCSERVERNAMELEN];
  520. int localPort = findFreeTcpPort();
  521. int remotePort;
  522. if (interpretViaParam(remoteHost, &remotePort, localPort) != 0)
  523. return 1;
  524. gatewayHost = (const char*)via;
  525. createTunnel(gatewayHost, remoteHost, remotePort, localPort);
  526. return 0;
  527. }
  528. #endif /* !WIN32 */
  529. int main(int argc, char** argv)
  530. {
  531. const char *localedir;
  532. UserDialog dlg;
  533. argv0 = argv[0];
  534. setlocale(LC_ALL, "");
  535. localedir = getlocaledir();
  536. if (localedir == NULL)
  537. fprintf(stderr, "Failed to determine locale directory\n");
  538. else
  539. bindtextdomain(PACKAGE_NAME, localedir);
  540. textdomain(PACKAGE_NAME);
  541. // Write about text to console, still using normal locale codeset
  542. fprintf(stderr,"\n%s\n", about_text());
  543. // Set gettext codeset to what our GUI toolkit uses. Since we are
  544. // passing strings from strerror/gai_strerror to the GUI, these must
  545. // be in GUI codeset as well.
  546. bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
  547. bind_textdomain_codeset("libc", "UTF-8");
  548. rfb::initStdIOLoggers();
  549. #ifdef WIN32
  550. rfb::initFileLogger("C:\\temp\\vncviewer.log");
  551. #else
  552. rfb::initFileLogger("/tmp/vncviewer.log");
  553. #endif
  554. rfb::LogWriter::setLogParams("*:stderr:30");
  555. #ifdef SIGHUP
  556. signal(SIGHUP, CleanupSignalHandler);
  557. #endif
  558. signal(SIGINT, CleanupSignalHandler);
  559. signal(SIGTERM, CleanupSignalHandler);
  560. Configuration::enableViewerParams();
  561. /* Load the default parameter settings */
  562. char defaultServerName[VNCSERVERNAMELEN] = "";
  563. try {
  564. const char* configServerName;
  565. configServerName = loadViewerParameters(NULL);
  566. if (configServerName != NULL) {
  567. strncpy(defaultServerName, configServerName, VNCSERVERNAMELEN-1);
  568. defaultServerName[VNCSERVERNAMELEN-1] = '\0';
  569. }
  570. } catch (rfb::Exception& e) {
  571. vlog.error("%s", e.str());
  572. }
  573. for (int i = 1; i < argc;) {
  574. /* We need to resolve an ambiguity for booleans */
  575. if (argv[i][0] == '-' && i+1 < argc) {
  576. VoidParameter *param;
  577. param = Configuration::getParam(&argv[i][1]);
  578. if ((param != NULL) &&
  579. (dynamic_cast<BoolParameter*>(param) != NULL)) {
  580. if ((strcasecmp(argv[i+1], "0") == 0) ||
  581. (strcasecmp(argv[i+1], "1") == 0) ||
  582. (strcasecmp(argv[i+1], "true") == 0) ||
  583. (strcasecmp(argv[i+1], "false") == 0) ||
  584. (strcasecmp(argv[i+1], "yes") == 0) ||
  585. (strcasecmp(argv[i+1], "no") == 0)) {
  586. param->setParam(argv[i+1]);
  587. i += 2;
  588. continue;
  589. }
  590. }
  591. }
  592. if (Configuration::setParam(argv[i])) {
  593. i++;
  594. continue;
  595. }
  596. if (argv[i][0] == '-') {
  597. if (i+1 < argc) {
  598. if (Configuration::setParam(&argv[i][1], argv[i+1])) {
  599. i += 2;
  600. continue;
  601. }
  602. }
  603. usage(argv[0]);
  604. }
  605. strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
  606. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  607. i++;
  608. }
  609. #if !defined(WIN32) && !defined(__APPLE__)
  610. if (strcmp(display, "") != 0) {
  611. Fl::display(display);
  612. }
  613. fl_open_display();
  614. XkbSetDetectableAutoRepeat(fl_display, True, NULL);
  615. #endif
  616. init_fltk();
  617. enable_touch();
  618. // Check if the server name in reality is a configuration file
  619. potentiallyLoadConfigurationFile(vncServerName);
  620. migrateDeprecatedOptions();
  621. mkvnchomedir();
  622. CSecurity::upg = &dlg;
  623. #ifdef HAVE_GNUTLS
  624. CSecurityTLS::msg = &dlg;
  625. #endif
  626. Socket *sock = NULL;
  627. #ifndef WIN32
  628. /* Specifying -via and -listen together is nonsense */
  629. if (listenMode && strlen(via.getValueStr()) > 0) {
  630. // TRANSLATORS: "Parameters" are command line arguments, or settings
  631. // from a file or the Windows registry.
  632. vlog.error(_("Parameters -listen and -via are incompatible"));
  633. abort_vncviewer(_("Parameters -listen and -via are incompatible"));
  634. return 1; /* Not reached */
  635. }
  636. #endif
  637. if (listenMode) {
  638. std::list<SocketListener*> listeners;
  639. try {
  640. int port = 5500;
  641. if (isdigit(vncServerName[0]))
  642. port = atoi(vncServerName);
  643. createTcpListeners(&listeners, 0, port);
  644. vlog.info(_("Listening on port %d"), port);
  645. /* Wait for a connection */
  646. while (sock == NULL) {
  647. fd_set rfds;
  648. FD_ZERO(&rfds);
  649. for (std::list<SocketListener*>::iterator i = listeners.begin();
  650. i != listeners.end();
  651. i++)
  652. FD_SET((*i)->getFd(), &rfds);
  653. int n = select(FD_SETSIZE, &rfds, 0, 0, 0);
  654. if (n < 0) {
  655. if (errno == EINTR) {
  656. vlog.debug("Interrupted select() system call");
  657. continue;
  658. } else {
  659. throw rdr::SystemException("select", errno);
  660. }
  661. }
  662. for (std::list<SocketListener*>::iterator i = listeners.begin ();
  663. i != listeners.end();
  664. i++)
  665. if (FD_ISSET((*i)->getFd(), &rfds)) {
  666. sock = (*i)->accept();
  667. if (sock)
  668. /* Got a connection */
  669. break;
  670. }
  671. }
  672. } catch (rdr::Exception& e) {
  673. vlog.error("%s", e.str());
  674. abort_vncviewer(_("Failure waiting for incoming VNC connection:\n\n%s"), e.str());
  675. return 1; /* Not reached */
  676. }
  677. while (!listeners.empty()) {
  678. delete listeners.back();
  679. listeners.pop_back();
  680. }
  681. } else {
  682. if (vncServerName[0] == '\0') {
  683. ServerDialog::run(defaultServerName, vncServerName);
  684. if (vncServerName[0] == '\0')
  685. return 1;
  686. }
  687. #ifndef WIN32
  688. if (strlen (via.getValueStr()) > 0 && mktunnel() != 0)
  689. usage(argv[0]);
  690. #endif
  691. }
  692. inMainloop = true;
  693. mainloop(vncServerName, sock);
  694. inMainloop = false;
  695. return 0;
  696. }