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