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