blob: 1efe1d7d16055261b081640ca912b1bab21ddba5 [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
Peter Åstrandc359f362011-08-23 12:04:46 +000021#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
Pierre Ossman5156d5e2011-03-09 09:42:34 +000025#include <string.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <signal.h>
30#include <locale.h>
31#include <sys/stat.h>
32
33#ifdef WIN32
34#include <direct.h>
35#define mkdir(path, mode) _mkdir(path)
36#endif
37
38#include <os/os.h>
39#include <rfb/Logger_stdio.h>
40#include <rfb/SecurityClient.h>
41#include <rfb/Security.h>
42#ifdef HAVE_GNUTLS
43#include <rfb/CSecurityTLS.h>
44#endif
45#include <rfb/LogWriter.h>
46#include <rfb/Timer.h>
47#include <network/TcpSocket.h>
48
49#include <FL/Fl.H>
50#include <FL/Fl_Widget.H>
51#include <FL/fl_ask.H>
Pierre Ossmanb8858222011-04-29 11:51:38 +000052#include <FL/x.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000053
54#include "i18n.h"
55#include "parameters.h"
56#include "CConn.h"
Pierre Ossman561ff0c2011-05-13 14:04:59 +000057#include "ServerDialog.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000058#include "UserDialog.h"
59
DRCb65bb932011-06-24 03:17:00 +000060#ifdef WIN32
61#include "win32.h"
62#endif
63
Pierre Ossman5156d5e2011-03-09 09:42:34 +000064rfb::LogWriter vlog("main");
65
66using namespace network;
67using namespace rfb;
68using namespace std;
69
Pierre Ossmanb8858222011-04-29 11:51:38 +000070static char aboutText[1024];
DRCd8e93dc2011-07-28 22:13:40 +000071extern const char* buildTime;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000072
73static bool exitMainloop = false;
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000074static const char *exitError = NULL;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000075
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000076void exit_vncviewer(const char *error)
Pierre Ossman5156d5e2011-03-09 09:42:34 +000077{
Pierre Ossmane2ef5c12011-07-12 16:56:34 +000078 // Prioritise the first error we get as that is probably the most
79 // relevant one.
80 if ((error != NULL) && (exitError == NULL))
81 exitError = strdup(error);
82
Pierre Ossman5156d5e2011-03-09 09:42:34 +000083 exitMainloop = true;
84}
85
Pierre Ossmanb8858222011-04-29 11:51:38 +000086void about_vncviewer()
87{
88 fl_message_title(_("About TigerVNC Viewer"));
89 fl_message(aboutText);
90}
91
92static void about_callback(Fl_Widget *widget, void *data)
93{
94 about_vncviewer();
95}
96
Pierre Ossman5156d5e2011-03-09 09:42:34 +000097static void CleanupSignalHandler(int sig)
98{
99 // CleanupSignalHandler allows C++ object cleanup to happen because it calls
100 // exit() rather than the default which is to abort.
101 vlog.info("CleanupSignalHandler called");
102 exit(1);
103}
104
105static void init_fltk()
106{
107 // Basic text size (10pt @ 96 dpi => 13px)
108 FL_NORMAL_SIZE = 13;
109
110#ifndef __APPLE__
111 // Select a FLTK scheme and background color that looks somewhat
112 // close to modern Linux and Windows.
113 Fl::scheme("gtk+");
114 Fl::background(220, 220, 220);
115#else
116 // On Mac OS X there is another scheme that fits better though.
117 Fl::scheme("plastic");
118#endif
119
120 // This makes the "icon" in dialogs rounded, which fits better
121 // with the above schemes.
122 fl_message_icon()->box(FL_UP_BOX);
123
124 // Turn off the annoying behaviour where popups track the mouse.
125 fl_message_hotspot(false);
126
127 // Avoid empty titles for popups
128 fl_message_title_default(_("TigerVNC Viewer"));
129
130#ifdef WIN32
131 // Most "normal" Windows apps use this font for UI elements.
132 Fl::set_font(FL_HELVETICA, "Tahoma");
133#endif
134
135 // FLTK exposes these so that we can translate them.
136 fl_no = _("No");
137 fl_yes = _("Yes");
138 fl_ok = _("OK");
139 fl_cancel = _("Cancel");
140 fl_close = _("Close");
Pierre Ossmanb8858222011-04-29 11:51:38 +0000141
142#ifdef __APPLE__
Peter Åstrandb7c55242011-08-29 13:14:51 +0000143 /* Needs trailing space */
144 static char fltk_about[16];
145 snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
146 Fl_Mac_App_Menu::about = fltk_about;
147 static char fltk_hide[16];
148 snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
149 Fl_Mac_App_Menu::hide = fltk_hide;
150 static char fltk_quit[16];
151 snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
152 Fl_Mac_App_Menu::quit = fltk_quit;
153
Pierre Ossman41ba6032011-06-16 11:09:31 +0000154 Fl_Mac_App_Menu::print = ""; // Don't want the print item
155 Fl_Mac_App_Menu::services = _("Services");
Pierre Ossman41ba6032011-06-16 11:09:31 +0000156 Fl_Mac_App_Menu::hide_others = _("Hide Others");
157 Fl_Mac_App_Menu::show = _("Show All");
Pierre Ossman41ba6032011-06-16 11:09:31 +0000158
Pierre Ossmanb8858222011-04-29 11:51:38 +0000159 fl_mac_set_about(about_callback, NULL);
160#endif
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000161}
162
163static void mkvnchomedir()
164{
165 // Create .vnc in the user's home directory if it doesn't already exist
166 char* homeDir = NULL;
167
168 if (getvnchomedir(&homeDir) == -1) {
169 vlog.error(_("Could not create VNC home directory: can't obtain home "
170 "directory path."));
171 } else {
172 int result = mkdir(homeDir, 0755);
173 if (result == -1 && errno != EEXIST)
174 vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
175 delete [] homeDir;
176 }
177}
178
179static void usage(const char *programName)
180{
181 fprintf(stderr,
182 "\nusage: %s [parameters] [host:displayNum] [parameters]\n"
183 " %s [parameters] -listen [port] [parameters]\n",
184 programName, programName);
185 fprintf(stderr,"\n"
186 "Parameters can be turned on with -<param> or off with -<param>=0\n"
187 "Parameters which take a value can be specified as "
188 "-<param> <value>\n"
189 "Other valid forms are <param>=<value> -<param>=<value> "
190 "--<param>=<value>\n"
191 "Parameter names are case-insensitive. The parameters are:\n\n");
192 Configuration::listParams(79, 14);
193 exit(1);
194}
195
196int main(int argc, char** argv)
197{
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000198 const char* vncServerName = NULL;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000199 UserDialog dlg;
200
DRCd8e93dc2011-07-28 22:13:40 +0000201 const char englishAbout[] = N_("TigerVNC Viewer %d-bit v%s (%s)\n"
202 "%s\n"
DRC07baad72011-06-28 05:42:30 +0000203 "Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)\n"
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000204 "See http://www.tigervnc.org for information on TigerVNC.");
205
206 setlocale(LC_ALL, "");
207 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
208 textdomain(PACKAGE_NAME);
209
210 rfb::SecurityClient::setDefaults();
211
212 // Write about text to console, still using normal locale codeset
213 snprintf(aboutText, sizeof(aboutText),
DRCd8e93dc2011-07-28 22:13:40 +0000214 gettext(englishAbout), (int)sizeof(size_t)*8, PACKAGE_VERSION,
215 __BUILD__, buildTime);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000216 fprintf(stderr,"\n%s\n", aboutText);
217
218 // Set gettext codeset to what our GUI toolkit uses. Since we are
219 // passing strings from strerror/gai_strerror to the GUI, these must
220 // be in GUI codeset as well.
221 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
222 bind_textdomain_codeset("libc", "UTF-8");
223
224 // Re-create the aboutText for the GUI, now using GUI codeset
225 snprintf(aboutText, sizeof(aboutText),
DRCd8e93dc2011-07-28 22:13:40 +0000226 gettext(englishAbout), (int)sizeof(size_t)*8, PACKAGE_VERSION,
227 __BUILD__, buildTime);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000228
229 rfb::initStdIOLoggers();
230 rfb::LogWriter::setLogParams("*:stderr:30");
231
232#ifdef SIGHUP
233 signal(SIGHUP, CleanupSignalHandler);
234#endif
235 signal(SIGINT, CleanupSignalHandler);
236 signal(SIGTERM, CleanupSignalHandler);
237
238 init_fltk();
239
240 Configuration::enableViewerParams();
241
DRC5aa06502011-06-23 22:04:46 +0000242 int i = 1;
243 if (!Fl::args(argc, argv, i) || i < argc)
244 for (; i < argc; i++) {
245 if (Configuration::setParam(argv[i]))
246 continue;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000247
DRC5aa06502011-06-23 22:04:46 +0000248 if (argv[i][0] == '-') {
249 if (i+1 < argc) {
250 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
251 i++;
252 continue;
253 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000254 }
DRC5aa06502011-06-23 22:04:46 +0000255 usage(argv[0]);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000256 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000257
DRC5aa06502011-06-23 22:04:46 +0000258 vncServerName = argv[i];
259 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000260
261 if (!::autoSelect.hasBeenSet()) {
262 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
263 ::autoSelect.setParam(!::preferredEncoding.hasBeenSet() &&
264 !::fullColour.hasBeenSet() &&
265 !::fullColourAlias.hasBeenSet());
266 }
267 if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) {
268 // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set
269 if (!::autoSelect && (::lowColourLevel.hasBeenSet() ||
270 ::lowColourLevelAlias.hasBeenSet())) {
271 ::fullColour.setParam(false);
272 }
273 }
274 if (!::customCompressLevel.hasBeenSet()) {
275 // Default to CustomCompressLevel=1 if CompressLevel is used.
276 ::customCompressLevel.setParam(::compressLevel.hasBeenSet());
277 }
278
279 mkvnchomedir();
280
281 CSecurity::upg = &dlg;
282#ifdef HAVE_GNUTLS
283 CSecurityTLS::msg = &dlg;
284#endif
285
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000286 if (vncServerName == NULL) {
287 vncServerName = ServerDialog::run();
288 if ((vncServerName == NULL) || (vncServerName[0] == '\0'))
289 return 1;
290 }
291
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000292 CConn *cc = new CConn(vncServerName);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000293
294 while (!exitMainloop) {
295 int next_timer;
296
297 next_timer = Timer::checkTimeouts();
298 if (next_timer == 0)
299 next_timer = INT_MAX;
300
301 if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
302 vlog.error(_("Internal FLTK error. Exiting."));
303 break;
304 }
305 }
306
Pierre Ossmane2ef5c12011-07-12 16:56:34 +0000307 delete cc;
308
309 if (exitError != NULL)
310 fl_alert(exitError);
311
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000312 return 0;
313}