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