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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <locale.h>
  26. #include <sys/stat.h>
  27. #ifdef WIN32
  28. #include <direct.h>
  29. #define mkdir(path, mode) _mkdir(path)
  30. #endif
  31. #include <os/os.h>
  32. #include <rfb/Logger_stdio.h>
  33. #include <rfb/SecurityClient.h>
  34. #include <rfb/Security.h>
  35. #ifdef HAVE_GNUTLS
  36. #include <rfb/CSecurityTLS.h>
  37. #endif
  38. #include <rfb/LogWriter.h>
  39. #include <rfb/Timer.h>
  40. #include <network/TcpSocket.h>
  41. #include <FL/Fl.H>
  42. #include <FL/Fl_Widget.H>
  43. #include <FL/fl_ask.H>
  44. #include <FL/x.H>
  45. #include "i18n.h"
  46. #include "parameters.h"
  47. #include "CConn.h"
  48. #include "ServerDialog.h"
  49. #include "UserDialog.h"
  50. #ifdef WIN32
  51. #include "win32.h"
  52. #endif
  53. rfb::LogWriter vlog("main");
  54. using namespace network;
  55. using namespace rfb;
  56. using namespace std;
  57. static char aboutText[1024];
  58. static bool exitMainloop = false;
  59. static const char *exitError = NULL;
  60. void exit_vncviewer(const char *error)
  61. {
  62. // Prioritise the first error we get as that is probably the most
  63. // relevant one.
  64. if ((error != NULL) && (exitError == NULL))
  65. exitError = strdup(error);
  66. exitMainloop = true;
  67. }
  68. void about_vncviewer()
  69. {
  70. fl_message_title(_("About TigerVNC Viewer"));
  71. fl_message(aboutText);
  72. }
  73. static void about_callback(Fl_Widget *widget, void *data)
  74. {
  75. about_vncviewer();
  76. }
  77. static void CleanupSignalHandler(int sig)
  78. {
  79. // CleanupSignalHandler allows C++ object cleanup to happen because it calls
  80. // exit() rather than the default which is to abort.
  81. vlog.info("CleanupSignalHandler called");
  82. exit(1);
  83. }
  84. static void init_fltk()
  85. {
  86. // Basic text size (10pt @ 96 dpi => 13px)
  87. FL_NORMAL_SIZE = 13;
  88. #ifndef __APPLE__
  89. // Select a FLTK scheme and background color that looks somewhat
  90. // close to modern Linux and Windows.
  91. Fl::scheme("gtk+");
  92. Fl::background(220, 220, 220);
  93. #else
  94. // On Mac OS X there is another scheme that fits better though.
  95. Fl::scheme("plastic");
  96. #endif
  97. // This makes the "icon" in dialogs rounded, which fits better
  98. // with the above schemes.
  99. fl_message_icon()->box(FL_UP_BOX);
  100. // Turn off the annoying behaviour where popups track the mouse.
  101. fl_message_hotspot(false);
  102. // Avoid empty titles for popups
  103. fl_message_title_default(_("TigerVNC Viewer"));
  104. #ifdef WIN32
  105. // Most "normal" Windows apps use this font for UI elements.
  106. Fl::set_font(FL_HELVETICA, "Tahoma");
  107. #endif
  108. // FLTK exposes these so that we can translate them.
  109. fl_no = _("No");
  110. fl_yes = _("Yes");
  111. fl_ok = _("OK");
  112. fl_cancel = _("Cancel");
  113. fl_close = _("Close");
  114. #ifdef __APPLE__
  115. Fl_Mac_App_Menu::about = _("About ");
  116. Fl_Mac_App_Menu::print = ""; // Don't want the print item
  117. Fl_Mac_App_Menu::services = _("Services");
  118. Fl_Mac_App_Menu::hide = _("Hide ");
  119. Fl_Mac_App_Menu::hide_others = _("Hide Others");
  120. Fl_Mac_App_Menu::show = _("Show All");
  121. Fl_Mac_App_Menu::quit = _("Quit ");
  122. fl_mac_set_about(about_callback, NULL);
  123. #endif
  124. }
  125. static void mkvnchomedir()
  126. {
  127. // Create .vnc in the user's home directory if it doesn't already exist
  128. char* homeDir = NULL;
  129. if (getvnchomedir(&homeDir) == -1) {
  130. vlog.error(_("Could not create VNC home directory: can't obtain home "
  131. "directory path."));
  132. } else {
  133. int result = mkdir(homeDir, 0755);
  134. if (result == -1 && errno != EEXIST)
  135. vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
  136. delete [] homeDir;
  137. }
  138. }
  139. static void usage(const char *programName)
  140. {
  141. fprintf(stderr,
  142. "\nusage: %s [parameters] [host:displayNum] [parameters]\n"
  143. " %s [parameters] -listen [port] [parameters]\n",
  144. programName, programName);
  145. fprintf(stderr,"\n"
  146. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  147. "Parameters which take a value can be specified as "
  148. "-<param> <value>\n"
  149. "Other valid forms are <param>=<value> -<param>=<value> "
  150. "--<param>=<value>\n"
  151. "Parameter names are case-insensitive. The parameters are:\n\n");
  152. Configuration::listParams(79, 14);
  153. exit(1);
  154. }
  155. int main(int argc, char** argv)
  156. {
  157. const char* vncServerName = NULL;
  158. UserDialog dlg;
  159. const char englishAbout[] = N_("TigerVNC Viewer version %s\n"
  160. "Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)\n"
  161. "See http://www.tigervnc.org for information on TigerVNC.");
  162. setlocale(LC_ALL, "");
  163. bindtextdomain(PACKAGE_NAME, LOCALEDIR);
  164. textdomain(PACKAGE_NAME);
  165. rfb::SecurityClient::setDefaults();
  166. // Write about text to console, still using normal locale codeset
  167. snprintf(aboutText, sizeof(aboutText),
  168. gettext(englishAbout), PACKAGE_VERSION);
  169. fprintf(stderr,"\n%s\n", aboutText);
  170. // Set gettext codeset to what our GUI toolkit uses. Since we are
  171. // passing strings from strerror/gai_strerror to the GUI, these must
  172. // be in GUI codeset as well.
  173. bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
  174. bind_textdomain_codeset("libc", "UTF-8");
  175. // Re-create the aboutText for the GUI, now using GUI codeset
  176. snprintf(aboutText, sizeof(aboutText),
  177. gettext(englishAbout), PACKAGE_VERSION);
  178. rfb::initStdIOLoggers();
  179. rfb::LogWriter::setLogParams("*:stderr:30");
  180. #ifdef SIGHUP
  181. signal(SIGHUP, CleanupSignalHandler);
  182. #endif
  183. signal(SIGINT, CleanupSignalHandler);
  184. signal(SIGTERM, CleanupSignalHandler);
  185. init_fltk();
  186. Configuration::enableViewerParams();
  187. int i = 1;
  188. if (!Fl::args(argc, argv, i) || i < argc)
  189. for (; i < argc; i++) {
  190. if (Configuration::setParam(argv[i]))
  191. continue;
  192. if (argv[i][0] == '-') {
  193. if (i+1 < argc) {
  194. if (Configuration::setParam(&argv[i][1], argv[i+1])) {
  195. i++;
  196. continue;
  197. }
  198. }
  199. usage(argv[0]);
  200. }
  201. vncServerName = argv[i];
  202. }
  203. if (!::autoSelect.hasBeenSet()) {
  204. // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
  205. ::autoSelect.setParam(!::preferredEncoding.hasBeenSet() &&
  206. !::fullColour.hasBeenSet() &&
  207. !::fullColourAlias.hasBeenSet());
  208. }
  209. if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) {
  210. // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set
  211. if (!::autoSelect && (::lowColourLevel.hasBeenSet() ||
  212. ::lowColourLevelAlias.hasBeenSet())) {
  213. ::fullColour.setParam(false);
  214. }
  215. }
  216. if (!::customCompressLevel.hasBeenSet()) {
  217. // Default to CustomCompressLevel=1 if CompressLevel is used.
  218. ::customCompressLevel.setParam(::compressLevel.hasBeenSet());
  219. }
  220. mkvnchomedir();
  221. CSecurity::upg = &dlg;
  222. #ifdef HAVE_GNUTLS
  223. CSecurityTLS::msg = &dlg;
  224. #endif
  225. if (vncServerName == NULL) {
  226. vncServerName = ServerDialog::run();
  227. if ((vncServerName == NULL) || (vncServerName[0] == '\0'))
  228. return 1;
  229. }
  230. CConn *cc = new CConn(vncServerName);
  231. while (!exitMainloop) {
  232. int next_timer;
  233. next_timer = Timer::checkTimeouts();
  234. if (next_timer == 0)
  235. next_timer = INT_MAX;
  236. if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
  237. vlog.error(_("Internal FLTK error. Exiting."));
  238. break;
  239. }
  240. }
  241. delete cc;
  242. if (exitError != NULL)
  243. fl_alert(exitError);
  244. return 0;
  245. }