blob: 7b9434ff07f1e5d53db73dfd00e3506ee5377cf9 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCb65bb932011-06-24 03:17:00 +00003 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossman5156d5e2011-03-09 09:42:34 +00004 *
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 Ossmanb8858222011-04-29 11:51:38 +000048#include <FL/x.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000049
50#include "i18n.h"
51#include "parameters.h"
52#include "CConn.h"
Pierre Ossman561ff0c2011-05-13 14:04:59 +000053#include "ServerDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054#include "UserDialog.h"
55
DRCb65bb932011-06-24 03:17:00 +000056#ifdef WIN32
57#include "win32.h"
58#endif
59
Pierre Ossman5156d5e2011-03-09 09:42:34 +000060rfb::LogWriter vlog("main");
61
62using namespace network;
63using namespace rfb;
64using namespace std;
65
Pierre Ossmanb8858222011-04-29 11:51:38 +000066static char aboutText[1024];
Pierre Ossman5156d5e2011-03-09 09:42:34 +000067
68static bool exitMainloop = false;
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000069static const char *exitError = NULL;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000070
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000071void exit_vncviewer(const char *error)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000072{
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000073 // Prioritise the first error we get as that is probably the most
74 // relevant one.
75 if ((error != NULL) && (exitError == NULL))
76 exitError = strdup(error);
77
Pierre Ossman5156d5e2011-03-09 09:42:34 +000078 exitMainloop = true;
79}
80
Pierre Ossmanb8858222011-04-29 11:51:38 +000081void about_vncviewer()
82{
83 fl_message_title(_("About TigerVNC Viewer"));
84 fl_message(aboutText);
85}
86
87static void about_callback(Fl_Widget *widget, void *data)
88{
89 about_vncviewer();
90}
91
Pierre Ossman5156d5e2011-03-09 09:42:34 +000092static void CleanupSignalHandler(int sig)
93{
94 // CleanupSignalHandler allows C++ object cleanup to happen because it calls
95 // exit() rather than the default which is to abort.
96 vlog.info("CleanupSignalHandler called");
97 exit(1);
98}
99
100static void init_fltk()
101{
102 // Basic text size (10pt @ 96 dpi => 13px)
103 FL_NORMAL_SIZE = 13;
104
105#ifndef __APPLE__
106 // Select a FLTK scheme and background color that looks somewhat
107 // close to modern Linux and Windows.
108 Fl::scheme("gtk+");
109 Fl::background(220, 220, 220);
110#else
111 // On Mac OS X there is another scheme that fits better though.
112 Fl::scheme("plastic");
113#endif
114
115 // This makes the "icon" in dialogs rounded, which fits better
116 // with the above schemes.
117 fl_message_icon()->box(FL_UP_BOX);
118
119 // Turn off the annoying behaviour where popups track the mouse.
120 fl_message_hotspot(false);
121
122 // Avoid empty titles for popups
123 fl_message_title_default(_("TigerVNC Viewer"));
124
125#ifdef WIN32
126 // Most "normal" Windows apps use this font for UI elements.
127 Fl::set_font(FL_HELVETICA, "Tahoma");
128#endif
129
130 // FLTK exposes these so that we can translate them.
131 fl_no = _("No");
132 fl_yes = _("Yes");
133 fl_ok = _("OK");
134 fl_cancel = _("Cancel");
135 fl_close = _("Close");
Pierre Ossmanb8858222011-04-29 11:51:38 +0000136
137#ifdef __APPLE__
Pierre Ossman41ba6032011-06-16 11:09:31 +0000138 Fl_Mac_App_Menu::about = _("About ");
139 Fl_Mac_App_Menu::print = ""; // Don't want the print item
140 Fl_Mac_App_Menu::services = _("Services");
141 Fl_Mac_App_Menu::hide = _("Hide ");
142 Fl_Mac_App_Menu::hide_others = _("Hide Others");
143 Fl_Mac_App_Menu::show = _("Show All");
144 Fl_Mac_App_Menu::quit = _("Quit ");
145
Pierre Ossmanb8858222011-04-29 11:51:38 +0000146 fl_mac_set_about(about_callback, NULL);
147#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000148}
149
150static void mkvnchomedir()
151{
152 // Create .vnc in the user's home directory if it doesn't already exist
153 char* homeDir = NULL;
154
155 if (getvnchomedir(&homeDir) == -1) {
156 vlog.error(_("Could not create VNC home directory: can't obtain home "
157 "directory path."));
158 } else {
159 int result = mkdir(homeDir, 0755);
160 if (result == -1 && errno != EEXIST)
161 vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
162 delete [] homeDir;
163 }
164}
165
166static void usage(const char *programName)
167{
168 fprintf(stderr,
169 "\nusage: %s [parameters] [host:displayNum] [parameters]\n"
170 " %s [parameters] -listen [port] [parameters]\n",
171 programName, programName);
172 fprintf(stderr,"\n"
173 "Parameters can be turned on with -<param> or off with -<param>=0\n"
174 "Parameters which take a value can be specified as "
175 "-<param> <value>\n"
176 "Other valid forms are <param>=<value> -<param>=<value> "
177 "--<param>=<value>\n"
178 "Parameter names are case-insensitive. The parameters are:\n\n");
179 Configuration::listParams(79, 14);
180 exit(1);
181}
182
183int main(int argc, char** argv)
184{
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000185 const char* vncServerName = NULL;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000186 UserDialog dlg;
187
188 const char englishAbout[] = N_("TigerVNC Viewer version %s\n"
DRC07baad72011-06-28 05:42:30 +0000189 "Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)\n"
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000190 "See http://www.tigervnc.org for information on TigerVNC.");
191
192 setlocale(LC_ALL, "");
193 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
194 textdomain(PACKAGE_NAME);
195
196 rfb::SecurityClient::setDefaults();
197
198 // Write about text to console, still using normal locale codeset
199 snprintf(aboutText, sizeof(aboutText),
200 gettext(englishAbout), PACKAGE_VERSION);
201 fprintf(stderr,"\n%s\n", aboutText);
202
203 // Set gettext codeset to what our GUI toolkit uses. Since we are
204 // passing strings from strerror/gai_strerror to the GUI, these must
205 // be in GUI codeset as well.
206 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
207 bind_textdomain_codeset("libc", "UTF-8");
208
209 // Re-create the aboutText for the GUI, now using GUI codeset
210 snprintf(aboutText, sizeof(aboutText),
211 gettext(englishAbout), PACKAGE_VERSION);
212
213 rfb::initStdIOLoggers();
214 rfb::LogWriter::setLogParams("*:stderr:30");
215
216#ifdef SIGHUP
217 signal(SIGHUP, CleanupSignalHandler);
218#endif
219 signal(SIGINT, CleanupSignalHandler);
220 signal(SIGTERM, CleanupSignalHandler);
221
222 init_fltk();
223
224 Configuration::enableViewerParams();
225
DRC5aa06502011-06-23 22:04:46 +0000226 int i = 1;
227 if (!Fl::args(argc, argv, i) || i < argc)
228 for (; i < argc; i++) {
229 if (Configuration::setParam(argv[i]))
230 continue;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000231
DRC5aa06502011-06-23 22:04:46 +0000232 if (argv[i][0] == '-') {
233 if (i+1 < argc) {
234 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
235 i++;
236 continue;
237 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000238 }
DRC5aa06502011-06-23 22:04:46 +0000239 usage(argv[0]);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000240 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000241
DRC5aa06502011-06-23 22:04:46 +0000242 vncServerName = argv[i];
243 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000244
245 if (!::autoSelect.hasBeenSet()) {
246 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
247 ::autoSelect.setParam(!::preferredEncoding.hasBeenSet() &&
248 !::fullColour.hasBeenSet() &&
249 !::fullColourAlias.hasBeenSet());
250 }
251 if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) {
252 // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set
253 if (!::autoSelect && (::lowColourLevel.hasBeenSet() ||
254 ::lowColourLevelAlias.hasBeenSet())) {
255 ::fullColour.setParam(false);
256 }
257 }
258 if (!::customCompressLevel.hasBeenSet()) {
259 // Default to CustomCompressLevel=1 if CompressLevel is used.
260 ::customCompressLevel.setParam(::compressLevel.hasBeenSet());
261 }
262
263 mkvnchomedir();
264
265 CSecurity::upg = &dlg;
266#ifdef HAVE_GNUTLS
267 CSecurityTLS::msg = &dlg;
268#endif
269
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000270 if (vncServerName == NULL) {
271 vncServerName = ServerDialog::run();
272 if ((vncServerName == NULL) || (vncServerName[0] == '\0'))
273 return 1;
274 }
275
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000276 CConn *cc = new CConn(vncServerName);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000277
278 while (!exitMainloop) {
279 int next_timer;
280
281 next_timer = Timer::checkTimeouts();
282 if (next_timer == 0)
283 next_timer = INT_MAX;
284
285 if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
286 vlog.error(_("Internal FLTK error. Exiting."));
287 break;
288 }
289 }
290
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000291 delete cc;
292
293 if (exitError != NULL)
294 fl_alert(exitError);
295
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000296 return 0;
297}