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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <string.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <signal.h>
  29. #include <locale.h>
  30. #include <sys/stat.h>
  31. #ifdef WIN32
  32. #include <direct.h>
  33. #define mkdir(path, mode) _mkdir(path)
  34. #endif
  35. #include <rfb/Logger_stdio.h>
  36. #include <rfb/SecurityClient.h>
  37. #include <rfb/Security.h>
  38. #ifdef HAVE_GNUTLS
  39. #include <rfb/CSecurityTLS.h>
  40. #endif
  41. #include <rfb/LogWriter.h>
  42. #include <rfb/Timer.h>
  43. #include <rfb/Exception.h>
  44. #include <network/TcpSocket.h>
  45. #include <os/os.h>
  46. #include <FL/Fl.H>
  47. #include <FL/Fl_Widget.H>
  48. #include <FL/Fl_PNG_Image.H>
  49. #include <FL/fl_ask.H>
  50. #include <FL/x.H>
  51. #include "i18n.h"
  52. #include "parameters.h"
  53. #include "CConn.h"
  54. #include "ServerDialog.h"
  55. #include "UserDialog.h"
  56. #include "vncviewer.h"
  57. #ifdef WIN32
  58. #include "resource.h"
  59. #include "win32.h"
  60. #endif
  61. rfb::LogWriter vlog("main");
  62. using namespace network;
  63. using namespace rfb;
  64. using namespace std;
  65. static const char aboutText[] = N_("TigerVNC Viewer %d-bit v%s (%s)\n"
  66. "%s\n"
  67. "Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)\n"
  68. "See http://www.tigervnc.org for information on TigerVNC.");
  69. extern const char* buildTime;
  70. char vncServerName[VNCSERVERNAMELEN] = { '\0' };
  71. static bool exitMainloop = false;
  72. static const char *exitError = NULL;
  73. void exit_vncviewer(const char *error)
  74. {
  75. // Prioritise the first error we get as that is probably the most
  76. // relevant one.
  77. if ((error != NULL) && (exitError == NULL))
  78. exitError = strdup(error);
  79. exitMainloop = true;
  80. }
  81. void about_vncviewer()
  82. {
  83. fl_message_title(_("About TigerVNC Viewer"));
  84. fl_message(gettext(aboutText), (int)sizeof(size_t)*8,
  85. PACKAGE_VERSION, __BUILD__, buildTime);
  86. }
  87. static void about_callback(Fl_Widget *widget, void *data)
  88. {
  89. about_vncviewer();
  90. }
  91. static void CleanupSignalHandler(int sig)
  92. {
  93. // CleanupSignalHandler allows C++ object cleanup to happen because it calls
  94. // exit() rather than the default which is to abort.
  95. vlog.info("CleanupSignalHandler called");
  96. exit(1);
  97. }
  98. static void init_fltk()
  99. {
  100. // Basic text size (10pt @ 96 dpi => 13px)
  101. FL_NORMAL_SIZE = 13;
  102. #ifndef __APPLE__
  103. // Select a FLTK scheme and background color that looks somewhat
  104. // close to modern Linux and Windows.
  105. Fl::scheme("gtk+");
  106. Fl::background(220, 220, 220);
  107. #else
  108. // On Mac OS X there is another scheme that fits better though.
  109. Fl::scheme("plastic");
  110. #endif
  111. // Proper Gnome Shell integration requires that we set a sensible
  112. // WM_CLASS for the window.
  113. Fl_Window::default_xclass("vncviewer");
  114. // Set the default icon for all windows.
  115. #ifdef HAVE_FLTK_ICONS
  116. #ifdef WIN32
  117. HICON lg, sm;
  118. lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
  119. IMAGE_ICON, GetSystemMetrics(SM_CXICON),
  120. GetSystemMetrics(SM_CYICON),
  121. LR_DEFAULTCOLOR | LR_SHARED);
  122. sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
  123. IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
  124. GetSystemMetrics(SM_CYSMICON),
  125. LR_DEFAULTCOLOR | LR_SHARED);
  126. Fl_Window::default_icons(lg, sm);
  127. #elif ! defined(__APPLE__)
  128. const int icon_sizes[] = {48, 32, 24, 16};
  129. Fl_PNG_Image *icons[4];
  130. int count;
  131. count = 0;
  132. // FIXME: Follow icon theme specification
  133. for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) {
  134. char icon_path[PATH_MAX];
  135. bool exists;
  136. sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png",
  137. DATA_DIR, icon_sizes[i], icon_sizes[i]);
  138. #ifndef WIN32
  139. struct stat st;
  140. if (stat(icon_path, &st) != 0)
  141. #else
  142. struct _stat st;
  143. if (_stat(icon_path, &st) != 0)
  144. return(false);
  145. #endif
  146. exists = false;
  147. else
  148. exists = true;
  149. if (exists) {
  150. icons[count] = new Fl_PNG_Image(icon_path);
  151. if (icons[count]->w() == 0 ||
  152. icons[count]->h() == 0 ||
  153. icons[count]->d() != 4) {
  154. delete icons[count];
  155. continue;
  156. }
  157. count++;
  158. }
  159. }
  160. Fl_Window::default_icons((const Fl_RGB_Image**)icons, count);
  161. for (int i = 0;i < count;i++)
  162. delete icons[i];
  163. #endif
  164. #endif // FLTK_HAVE_ICONS
  165. // This makes the "icon" in dialogs rounded, which fits better
  166. // with the above schemes.
  167. fl_message_icon()->box(FL_UP_BOX);
  168. // Turn off the annoying behaviour where popups track the mouse.
  169. fl_message_hotspot(false);
  170. // Avoid empty titles for popups
  171. fl_message_title_default(_("TigerVNC Viewer"));
  172. #ifdef WIN32
  173. // Most "normal" Windows apps use this font for UI elements.
  174. Fl::set_font(FL_HELVETICA, "Tahoma");
  175. #endif
  176. // FLTK exposes these so that we can translate them.
  177. fl_no = _("No");
  178. fl_yes = _("Yes");
  179. fl_ok = _("OK");
  180. fl_cancel = _("Cancel");
  181. fl_close = _("Close");
  182. #ifdef __APPLE__
  183. /* Needs trailing space */
  184. static char fltk_about[16];
  185. snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
  186. Fl_Mac_App_Menu::about = fltk_about;
  187. static char fltk_hide[16];
  188. snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
  189. Fl_Mac_App_Menu::hide = fltk_hide;
  190. static char fltk_quit[16];
  191. snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
  192. Fl_Mac_App_Menu::quit = fltk_quit;
  193. Fl_Mac_App_Menu::print = ""; // Don't want the print item
  194. Fl_Mac_App_Menu::services = _("Services");
  195. Fl_Mac_App_Menu::hide_others = _("Hide Others");
  196. Fl_Mac_App_Menu::show = _("Show All");
  197. fl_mac_set_about(about_callback, NULL);
  198. #endif
  199. }
  200. static void mkvnchomedir()
  201. {
  202. // Create .vnc in the user's home directory if it doesn't already exist
  203. char* homeDir = NULL;
  204. if (getvnchomedir(&homeDir) == -1) {
  205. vlog.error(_("Could not create VNC home directory: can't obtain home "
  206. "directory path."));
  207. } else {
  208. int result = mkdir(homeDir, 0755);
  209. if (result == -1 && errno != EEXIST)
  210. vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
  211. delete [] homeDir;
  212. }
  213. }
  214. static void usage(const char *programName)
  215. {
  216. fprintf(stderr,
  217. "\nusage: %s [parameters] [host:displayNum] [parameters]\n"
  218. " %s [parameters] -listen [port] [parameters]\n",
  219. programName, programName);
  220. fprintf(stderr,"\n"
  221. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  222. "Parameters which take a value can be specified as "
  223. "-<param> <value>\n"
  224. "Other valid forms are <param>=<value> -<param>=<value> "
  225. "--<param>=<value>\n"
  226. "Parameter names are case-insensitive. The parameters are:\n\n");
  227. Configuration::listParams(79, 14);
  228. exit(1);
  229. }
  230. #ifndef WIN32
  231. static int
  232. interpretViaParam(char *remoteHost, int *remotePort, int localPort)
  233. {
  234. const int SERVER_PORT_OFFSET = 5900;
  235. char *pos = strchr(vncServerName, ':');
  236. if (pos == NULL)
  237. *remotePort = SERVER_PORT_OFFSET;
  238. else {
  239. int portOffset = SERVER_PORT_OFFSET;
  240. size_t len;
  241. *pos++ = '\0';
  242. len = strlen(pos);
  243. if (*pos == ':') {
  244. /* Two colons is an absolute port number, not an offset. */
  245. pos++;
  246. len--;
  247. portOffset = 0;
  248. }
  249. if (!len || strspn (pos, "-0123456789") != len )
  250. return 1;
  251. *remotePort = atoi(pos) + portOffset;
  252. }
  253. if (*vncServerName != '\0')
  254. strncpy(remoteHost, vncServerName, VNCSERVERNAMELEN);
  255. else
  256. strncpy(remoteHost, "localhost", VNCSERVERNAMELEN);
  257. remoteHost[VNCSERVERNAMELEN - 1] = '\0';
  258. snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort);
  259. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  260. vlog.error(vncServerName);
  261. return 0;
  262. }
  263. static void
  264. createTunnel(const char *gatewayHost, const char *remoteHost,
  265. int remotePort, int localPort)
  266. {
  267. char *cmd = getenv("VNC_VIA_CMD");
  268. char *percent;
  269. char lport[10], rport[10];
  270. sprintf(lport, "%d", localPort);
  271. sprintf(rport, "%d", remotePort);
  272. setenv("G", gatewayHost, 1);
  273. setenv("H", remoteHost, 1);
  274. setenv("R", rport, 1);
  275. setenv("L", lport, 1);
  276. if (!cmd)
  277. cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20";
  278. /* Compatibility with TigerVNC's method. */
  279. while ((percent = strchr(cmd, '%')) != NULL)
  280. *percent = '$';
  281. system(cmd);
  282. }
  283. static int mktunnel()
  284. {
  285. const char *gatewayHost;
  286. char remoteHost[VNCSERVERNAMELEN];
  287. int localPort = findFreeTcpPort();
  288. int remotePort;
  289. gatewayHost = strDup(via.getValueStr());
  290. if (interpretViaParam(remoteHost, &remotePort, localPort) != 0)
  291. return 1;
  292. createTunnel(gatewayHost, remoteHost, remotePort, localPort);
  293. return 0;
  294. }
  295. #endif /* !WIN32 */
  296. int main(int argc, char** argv)
  297. {
  298. UserDialog dlg;
  299. setlocale(LC_ALL, "");
  300. bindtextdomain(PACKAGE_NAME, LOCALE_DIR);
  301. textdomain(PACKAGE_NAME);
  302. rfb::SecurityClient::setDefaults();
  303. // Write about text to console, still using normal locale codeset
  304. fprintf(stderr,"\n");
  305. fprintf(stderr, gettext(aboutText), (int)sizeof(size_t)*8,
  306. PACKAGE_VERSION, __BUILD__, buildTime);
  307. fprintf(stderr,"\n");
  308. // Set gettext codeset to what our GUI toolkit uses. Since we are
  309. // passing strings from strerror/gai_strerror to the GUI, these must
  310. // be in GUI codeset as well.
  311. bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
  312. bind_textdomain_codeset("libc", "UTF-8");
  313. rfb::initStdIOLoggers();
  314. #ifdef WIN32
  315. rfb::initFileLogger("C:\\temp\\vncviewer.log");
  316. #else
  317. rfb::initFileLogger("/tmp/vncviewer.log");
  318. #endif
  319. rfb::LogWriter::setLogParams("*:stderr:30");
  320. #ifdef SIGHUP
  321. signal(SIGHUP, CleanupSignalHandler);
  322. #endif
  323. signal(SIGINT, CleanupSignalHandler);
  324. signal(SIGTERM, CleanupSignalHandler);
  325. init_fltk();
  326. Configuration::enableViewerParams();
  327. /* Load the default parameter settings */
  328. const char* defaultServerName;
  329. try {
  330. defaultServerName = loadViewerParameters(NULL);
  331. } catch (rfb::Exception& e) {
  332. fl_alert("%s", e.str());
  333. }
  334. int i = 1;
  335. if (!Fl::args(argc, argv, i) || i < argc)
  336. for (; i < argc; i++) {
  337. if (Configuration::setParam(argv[i]))
  338. continue;
  339. if (argv[i][0] == '-') {
  340. if (i+1 < argc) {
  341. if (Configuration::setParam(&argv[i][1], argv[i+1])) {
  342. i++;
  343. continue;
  344. }
  345. }
  346. usage(argv[0]);
  347. }
  348. strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
  349. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  350. }
  351. if (!::autoSelect.hasBeenSet()) {
  352. // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
  353. if (::preferredEncoding.hasBeenSet() || ::fullColour.hasBeenSet() ||
  354. ::fullColourAlias.hasBeenSet()) {
  355. ::autoSelect.setParam(false);
  356. }
  357. }
  358. if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) {
  359. // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set
  360. if (!::autoSelect && (::lowColourLevel.hasBeenSet() ||
  361. ::lowColourLevelAlias.hasBeenSet())) {
  362. ::fullColour.setParam(false);
  363. }
  364. }
  365. if (!::customCompressLevel.hasBeenSet()) {
  366. // Default to CustomCompressLevel=1 if CompressLevel is used.
  367. if(::compressLevel.hasBeenSet()) {
  368. ::customCompressLevel.setParam(true);
  369. }
  370. }
  371. mkvnchomedir();
  372. CSecurity::upg = &dlg;
  373. #ifdef HAVE_GNUTLS
  374. CSecurityTLS::msg = &dlg;
  375. #endif
  376. Socket *sock = NULL;
  377. #ifndef WIN32
  378. /* Specifying -via and -listen together is nonsense */
  379. if (listenMode && strlen(via.getValueStr()) > 0) {
  380. vlog.error("Parameters -listen and -via are incompatible");
  381. fl_alert("Parameters -listen and -via are incompatible");
  382. exit_vncviewer();
  383. return 1;
  384. }
  385. #endif
  386. if (listenMode) {
  387. try {
  388. int port = 5500;
  389. if (isdigit(vncServerName[0]))
  390. port = atoi(vncServerName);
  391. TcpListener listener(NULL, port);
  392. vlog.info("Listening on port %d\n", port);
  393. sock = listener.accept();
  394. } catch (rdr::Exception& e) {
  395. vlog.error("%s", e.str());
  396. fl_alert("%s", e.str());
  397. exit_vncviewer();
  398. return 1;
  399. }
  400. } else {
  401. if (vncServerName[0] == '\0') {
  402. ServerDialog::run(defaultServerName, vncServerName);
  403. if (vncServerName[0] == '\0')
  404. return 1;
  405. }
  406. #ifndef WIN32
  407. if (strlen (via.getValueStr()) > 0 && mktunnel() != 0)
  408. usage(argv[0]);
  409. #endif
  410. }
  411. CConn *cc = new CConn(vncServerName, sock);
  412. while (!exitMainloop) {
  413. int next_timer;
  414. next_timer = Timer::checkTimeouts();
  415. if (next_timer == 0)
  416. next_timer = INT_MAX;
  417. if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
  418. vlog.error(_("Internal FLTK error. Exiting."));
  419. break;
  420. }
  421. }
  422. delete cc;
  423. if (exitError != NULL)
  424. fl_alert("%s", exitError);
  425. return 0;
  426. }