blob: cfe5cfae665bdc301e9f30796af9c8b2b20365f5 [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"
DRC07baad72011-06-28 05:42:30 +0000183 "Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)\n"
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000184 "See http://www.tigervnc.org for information on TigerVNC.");
185
186 setlocale(LC_ALL, "");
187 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
188 textdomain(PACKAGE_NAME);
189
190 rfb::SecurityClient::setDefaults();
191
192 // Write about text to console, still using normal locale codeset
193 snprintf(aboutText, sizeof(aboutText),
194 gettext(englishAbout), PACKAGE_VERSION);
195 fprintf(stderr,"\n%s\n", aboutText);
196
197 // Set gettext codeset to what our GUI toolkit uses. Since we are
198 // passing strings from strerror/gai_strerror to the GUI, these must
199 // be in GUI codeset as well.
200 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
201 bind_textdomain_codeset("libc", "UTF-8");
202
203 // Re-create the aboutText for the GUI, now using GUI codeset
204 snprintf(aboutText, sizeof(aboutText),
205 gettext(englishAbout), PACKAGE_VERSION);
206
207 rfb::initStdIOLoggers();
208 rfb::LogWriter::setLogParams("*:stderr:30");
209
210#ifdef SIGHUP
211 signal(SIGHUP, CleanupSignalHandler);
212#endif
213 signal(SIGINT, CleanupSignalHandler);
214 signal(SIGTERM, CleanupSignalHandler);
215
216 init_fltk();
217
218 Configuration::enableViewerParams();
219
DRC5aa06502011-06-23 22:04:46 +0000220 int i = 1;
221 if (!Fl::args(argc, argv, i) || i < argc)
222 for (; i < argc; i++) {
223 if (Configuration::setParam(argv[i]))
224 continue;
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000225
DRC5aa06502011-06-23 22:04:46 +0000226 if (argv[i][0] == '-') {
227 if (i+1 < argc) {
228 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
229 i++;
230 continue;
231 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000232 }
DRC5aa06502011-06-23 22:04:46 +0000233 usage(argv[0]);
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000234 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000235
DRC5aa06502011-06-23 22:04:46 +0000236 vncServerName = argv[i];
237 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000238
239 if (!::autoSelect.hasBeenSet()) {
240 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
241 ::autoSelect.setParam(!::preferredEncoding.hasBeenSet() &&
242 !::fullColour.hasBeenSet() &&
243 !::fullColourAlias.hasBeenSet());
244 }
245 if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) {
246 // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set
247 if (!::autoSelect && (::lowColourLevel.hasBeenSet() ||
248 ::lowColourLevelAlias.hasBeenSet())) {
249 ::fullColour.setParam(false);
250 }
251 }
252 if (!::customCompressLevel.hasBeenSet()) {
253 // Default to CustomCompressLevel=1 if CompressLevel is used.
254 ::customCompressLevel.setParam(::compressLevel.hasBeenSet());
255 }
256
257 mkvnchomedir();
258
259 CSecurity::upg = &dlg;
260#ifdef HAVE_GNUTLS
261 CSecurityTLS::msg = &dlg;
262#endif
263
Pierre Ossman561ff0c2011-05-13 14:04:59 +0000264 if (vncServerName == NULL) {
265 vncServerName = ServerDialog::run();
266 if ((vncServerName == NULL) || (vncServerName[0] == '\0'))
267 return 1;
268 }
269
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000270 CConn cc(vncServerName);
271
272 while (!exitMainloop) {
273 int next_timer;
274
275 next_timer = Timer::checkTimeouts();
276 if (next_timer == 0)
277 next_timer = INT_MAX;
278
279 if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
280 vlog.error(_("Internal FLTK error. Exiting."));
281 break;
282 }
283 }
284
285 return 0;
286}