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

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