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