blob: cb561930dd7bbcea3b727ff3eeef177a6fbfb0ad [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <vncviewer/CConnOptions.h>
20#include <rfb/Configuration.h>
21#include <rfb/encodings.h>
22#include <rfb/LogWriter.h>
george82c439ebb2007-04-30 05:21:41 +000023#include <rfb/ScaleFilters.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000024#include <rfb_win32/MsgBox.h>
25#include <rfb_win32/Registry.h>
26#include <rdr/HexInStream.h>
27#include <rdr/HexOutStream.h>
28#include <stdlib.h>
29
30using namespace rfb;
31using namespace rfb::win32;
32
33static StringParameter passwordFile("PasswordFile",
34 "Password file for VNC authentication", "");
35
36// - Settings stored in the registry & in .vnc files, by Save Defaults and
37// Save Configuration respectively.
38
39static BoolParameter useLocalCursor("UseLocalCursor", "Render the mouse cursor locally", true);
40static BoolParameter useDesktopResize("UseDesktopResize", "Support dynamic desktop resizing", true);
41
42static BoolParameter fullColour("FullColor",
43 "Use full color", true);
44static AliasParameter fullColourAlias("FullColour", "Alias for FullColor", &fullColour);
45
46static IntParameter lowColourLevel("LowColorLevel",
47 "Color level to use on slow connections. "
48 "0 = Very Low (8 colors), 1 = Low (64 colors), 2 = Medium (256 colors)",
49 2);
50static AliasParameter lowColourLevelAlias("LowColourLevel", "Alias for LowColorLevel", &lowColourLevel);
51
52static BoolParameter fullScreen("FullScreen",
53 "Use the whole display to show the remote desktop."
54 "(Press F8 to access the viewer menu)",
55 false);
56static StringParameter preferredEncoding("PreferredEncoding",
57 "Preferred encoding to use (Tight, ZRLE, Hextile or"
58 " Raw)", "Tight");
59static BoolParameter autoSelect("AutoSelect",
60 "Auto select pixel format and encoding. "
61 "Default if PreferredEncoding and FullColor are not specified.",
62 true);
63static BoolParameter sharedConnection("Shared",
64 "Allow existing connections to the server to continue."
65 "(Default is to disconnect all other clients)",
66 false);
67
68static BoolParameter sendPtrEvents("SendPointerEvents",
69 "Send pointer (mouse) events to the server.", true);
70static BoolParameter sendKeyEvents("SendKeyEvents",
71 "Send key presses (and releases) to the server.", true);
72
73static BoolParameter clientCutText("ClientCutText",
74 "Send clipboard changes to the server.", true);
75static BoolParameter serverCutText("ServerCutText",
76 "Accept clipboard changes from the server.", true);
77
78static BoolParameter disableWinKeys("DisableWinKeys",
79 "Pass special Windows keys directly to the server.", true);
80
81static BoolParameter protocol3_3("Protocol3.3",
82 "Only use protocol version 3.3", false);
83
84static IntParameter ptrEventInterval("PointerEventInterval",
85 "The interval to delay between sending one pointer event "
86 "and the next.", 0);
87static BoolParameter emulate3("Emulate3",
88 "Emulate middle mouse button when left and right buttons "
89 "are used simulatenously.", false);
90
91static BoolParameter acceptBell("AcceptBell",
92 "Produce a system beep when requested to by the server.",
93 true);
94
95static BoolParameter showToolbar("ShowToolbar", "Show toolbar by default.", true);
96
97static StringParameter monitor("Monitor", "The monitor to open the VNC Viewer window on, if available.", "");
98static StringParameter menuKey("MenuKey", "The key which brings up the popup menu", "F8");
99static BoolParameter autoReconnect("AutoReconnect", "Offer to reconnect to the remote server if the connection"
100 "is dropped because an error occurs.", true);
101
102static BoolParameter customCompressLevel("CustomCompressLevel",
103 "Use custom compression level. "
104 "Default if CompressLevel is specified.", false);
105
106static IntParameter compressLevel("CompressLevel",
107 "Use specified compression level"
108 "0 = Low, 9 = High",
109 6);
110
111static BoolParameter noJpeg("NoJPEG",
112 "Disable lossy JPEG compression in Tight encoding.",
113 false);
114
115static IntParameter qualityLevel("QualityLevel",
116 "JPEG quality level. "
117 "0 = Low, 9 = High",
Pierre Ossman7dfa22e2009-03-12 13:03:22 +0000118 8);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000119
george82444e8862006-05-27 10:21:28 +0000120static BoolParameter autoScaling("AutoScaling",
121 "Auto rescale local copy of the remote desktop to the client window.",
122 false);
123static IntParameter scale("Scale",
124 "Scale local copy of the remote desktop, in percent",
125 100);
126
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000127CConnOptions::CConnOptions()
Peter Åstranda850e642008-12-11 09:04:02 +0000128: useLocalCursor (::useLocalCursor),
129useDesktopResize(::useDesktopResize),
130fullScreen(::fullScreen),
131fullColour(::fullColour),
132lowColourLevel(::lowColourLevel),
Pierre Ossman7dfa22e2009-03-12 13:03:22 +0000133preferredEncoding(encodingTight),
Peter Åstranda850e642008-12-11 09:04:02 +0000134autoSelect(::autoSelect),
135shared(::sharedConnection),
136sendPtrEvents(::sendPtrEvents),
137sendKeyEvents(::sendKeyEvents),
138showToolbar(::showToolbar),
139clientCutText(::clientCutText),
140serverCutText(::serverCutText),
141disableWinKeys(::disableWinKeys),
142emulate3(::emulate3),
143pointerEventInterval(ptrEventInterval),
144protocol3_3(::protocol3_3),
145acceptBell(::acceptBell),
146autoScaling(::autoScaling),
147scale(::scale),
148monitor(::monitor.getData()),
149autoReconnect(::autoReconnect),
150customCompressLevel(::customCompressLevel),
151compressLevel(::compressLevel),
152noJpeg(::noJpeg),
Peter Åstrandf7d04362008-12-11 10:13:02 +0000153qualityLevel(::qualityLevel),
154passwordFile(::passwordFile.getData())
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000155{
156 if (autoSelect) {
Pierre Ossman7dfa22e2009-03-12 13:03:22 +0000157 preferredEncoding = encodingTight;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000158 } else {
159 CharArray encodingName(::preferredEncoding.getData());
160 preferredEncoding = encodingNum(encodingName.buf);
161 }
162 setMenuKey(CharArray(::menuKey.getData()).buf);
163
164 if (!::autoSelect.hasBeenSet()) {
165 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
166 autoSelect = (!::preferredEncoding.hasBeenSet()
167 && !::fullColour.hasBeenSet()
168 && !::fullColourAlias.hasBeenSet());
169 }
170 if (!::customCompressLevel.hasBeenSet()) {
171 // Default to CustomCompressLevel=1 if CompressLevel is used.
172 customCompressLevel = ::compressLevel.hasBeenSet();
173 }
174}
175
176
177void CConnOptions::readFromFile(const char* filename) {
178 FILE* f = fopen(filename, "r");
179 if (!f)
180 throw rdr::Exception("Failed to read configuration file");
181
182 try {
183 char line[4096];
184 CharArray section;
185
186 CharArray hostTmp;
187 int portTmp = 0;
188
189 while (!feof(f)) {
190 // Read the next line
191 if (!fgets(line, sizeof(line), f)) {
192 if (feof(f))
193 break;
194 throw rdr::SystemException("fgets", ferror(f));
195 }
196 int len=strlen(line);
197 if (line[len-1] == '\n') {
198 line[len-1] = 0;
199 len--;
200 }
201
202 // Process the line
203 if (line[0] == ';') {
204 // Comment
205 } else if (line[0] == '[') {
206 // Entering a new section
207 if (!strSplit(&line[1], ']', &section.buf, 0))
208 throw rdr::Exception("bad Section");
209 } else {
210 // Reading an option
211 CharArray name;
212 CharArray value;
213 if (!strSplit(line, '=', &name.buf, &value.buf))
214 throw rdr::Exception("bad Name/Value pair");
215
216 if (stricmp(section.buf, "Connection") == 0) {
217 if (stricmp(name.buf, "Host") == 0) {
218 hostTmp.replaceBuf(value.takeBuf());
219 } else if (stricmp(name.buf, "Port") == 0) {
220 portTmp = atoi(value.buf);
221 } else if (stricmp(name.buf, "UserName") == 0) {
222 userName.replaceBuf(value.takeBuf());
223 } else if (stricmp(name.buf, "Password") == 0) {
224 ObfuscatedPasswd obfPwd;
225 rdr::HexInStream::hexStrToBin(value.buf, (char**)&obfPwd.buf, &obfPwd.length);
226 PlainPasswd passwd(obfPwd);
227 password.replaceBuf(passwd.takeBuf());
228 }
229 } else if (stricmp(section.buf, "Options") == 0) {
230 // V4 options
231 if (stricmp(name.buf, "UseLocalCursor") == 0) {
232 useLocalCursor = atoi(value.buf);
233 } else if (stricmp(name.buf, "UseDesktopResize") == 0) {
234 useDesktopResize = atoi(value.buf);
235 } else if (stricmp(name.buf, "FullScreen") == 0) {
236 fullScreen = atoi(value.buf);
237 } else if (stricmp(name.buf, "FullColour") == 0) {
238 fullColour = atoi(value.buf);
239 } else if (stricmp(name.buf, "LowColourLevel") == 0) {
240 lowColourLevel = atoi(value.buf);
241 } else if (stricmp(name.buf, "PreferredEncoding") == 0) {
242 preferredEncoding = encodingNum(value.buf);
243 } else if ((stricmp(name.buf, "AutoDetect") == 0) ||
244 (stricmp(name.buf, "AutoSelect") == 0)) {
245 autoSelect = atoi(value.buf);
246 } else if (stricmp(name.buf, "Shared") == 0) {
247 shared = atoi(value.buf);
248 } else if (stricmp(name.buf, "SendPtrEvents") == 0) {
249 sendPtrEvents = atoi(value.buf);
250 } else if (stricmp(name.buf, "SendKeyEvents") == 0) {
251 sendKeyEvents = atoi(value.buf);
252 } else if (stricmp(name.buf, "SendCutText") == 0) {
253 clientCutText = atoi(value.buf);
254 } else if (stricmp(name.buf, "AcceptCutText") == 0) {
255 serverCutText = atoi(value.buf);
256 } else if (stricmp(name.buf, "DisableWinKeys") == 0) {
257 disableWinKeys = atoi(value.buf);
258 } else if (stricmp(name.buf, "AcceptBell") == 0) {
259 acceptBell = atoi(value.buf);
260 } else if (stricmp(name.buf, "Emulate3") == 0) {
261 emulate3 = atoi(value.buf);
262 } else if (stricmp(name.buf, "ShowToolbar") == 0) {
263 showToolbar = atoi(value.buf);
264 } else if (stricmp(name.buf, "PointerEventInterval") == 0) {
265 pointerEventInterval = atoi(value.buf);
266 } else if (stricmp(name.buf, "Monitor") == 0) {
267 monitor.replaceBuf(value.takeBuf());
268 } else if (stricmp(name.buf, "MenuKey") == 0) {
269 setMenuKey(value.buf);
270 } else if (stricmp(name.buf, "AutoReconnect") == 0) {
271 autoReconnect = atoi(value.buf);
272
273 } else if (stricmp(name.buf, "CustomCompressLevel") == 0) {
274 customCompressLevel = atoi(value.buf);
275 } else if (stricmp(name.buf, "CompressLevel") == 0) {
276 compressLevel = atoi(value.buf);
277 } else if (stricmp(name.buf, "NoJPEG") == 0) {
278 noJpeg = atoi(value.buf);
279 } else if (stricmp(name.buf, "QualityLevel") == 0) {
280 qualityLevel = atoi(value.buf);
281 // Legacy options
282 } else if (stricmp(name.buf, "Preferred_Encoding") == 0) {
283 preferredEncoding = atoi(value.buf);
284 } else if (stricmp(name.buf, "8bit") == 0) {
285 fullColour = !atoi(value.buf);
286 } else if (stricmp(name.buf, "FullScreen") == 0) {
287 fullScreen = atoi(value.buf);
288 } else if (stricmp(name.buf, "ViewOnly") == 0) {
289 sendPtrEvents = sendKeyEvents = !atoi(value.buf);
290 } else if (stricmp(name.buf, "DisableClipboard") == 0) {
291 clientCutText = serverCutText = !atoi(value.buf);
george82444e8862006-05-27 10:21:28 +0000292 } else if (stricmp(name.buf, "AutoScaling") == 0) {
293 autoScaling = atoi(value.buf);
294 } else if (stricmp(name.buf, "Scale") == 0) {
295 scale = atoi(value.buf);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000296 }
297 }
298 }
299 }
300 fclose(f); f=0;
301
302 // Process the Host and Port
303 if (hostTmp.buf) {
304 int hostLen = strlen(hostTmp.buf) + 2 + 17;
305 host.replaceBuf(new char[hostLen]);
306 strCopy(host.buf, hostTmp.buf, hostLen);
307 if (portTmp) {
308 strncat(host.buf, "::", hostLen-1);
309 char tmp[16];
310 sprintf(tmp, "%d", portTmp);
311 strncat(host.buf, tmp, hostLen-1);
312 }
313 }
314
315 // If AutoSelect is enabled then override the preferred encoding
316 if (autoSelect)
317 preferredEncoding = encodingZRLE;
318
319 setConfigFileName(filename);
320 } catch (rdr::Exception&) {
321 if (f) fclose(f);
322 throw;
323 }
324}
325
326void CConnOptions::writeToFile(const char* filename) {
327 FILE* f = fopen(filename, "w");
328 if (!f)
329 throw rdr::Exception("Failed to write configuration file");
330
331 try {
332 // - Split server into host and port and save
333 fprintf(f, "[Connection]\n");
334
335 fprintf(f, "Host=%s\n", host.buf);
336 if (userName.buf)
337 fprintf(f, "UserName=%s\n", userName.buf);
338 if (password.buf) {
339 // - Warn the user before saving the password
340 if (MsgBox(0, _T("Do you want to include the VNC Password in this configuration file?\n")
341 _T("Storing the password is more convenient but poses a security risk."),
342 MB_YESNO | MB_DEFBUTTON2 | MB_ICONWARNING) == IDYES) {
343 ObfuscatedPasswd obfPwd(password);
Peter Åstrandb22dbef2008-12-09 14:57:53 +0000344 CharArray obfuscatedHex(rdr::HexOutStream::binToHexStr(obfPwd.buf, obfPwd.length));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000345 fprintf(f, "Password=%s\n", obfuscatedHex.buf);
346 }
347 }
348
349 // - Save the other options
350 fprintf(f, "[Options]\n");
351
352 fprintf(f, "UseLocalCursor=%d\n", (int)useLocalCursor);
353 fprintf(f, "UseDesktopResize=%d\n", (int)useDesktopResize);
354 fprintf(f, "FullScreen=%d\n", (int)fullScreen);
355 fprintf(f, "FullColour=%d\n", (int)fullColour);
356 fprintf(f, "LowColourLevel=%d\n", lowColourLevel);
357 fprintf(f, "PreferredEncoding=%s\n", encodingName(preferredEncoding));
358 fprintf(f, "AutoSelect=%d\n", (int)autoSelect);
359 fprintf(f, "Shared=%d\n", (int)shared);
360 fprintf(f, "SendPtrEvents=%d\n", (int)sendPtrEvents);
361 fprintf(f, "SendKeyEvents=%d\n", (int)sendKeyEvents);
362 fprintf(f, "SendCutText=%d\n", (int)clientCutText);
363 fprintf(f, "AcceptCutText=%d\n", (int)serverCutText);
364 fprintf(f, "DisableWinKeys=%d\n", (int)disableWinKeys);
365 fprintf(f, "AcceptBell=%d\n", (int)acceptBell);
366 fprintf(f, "Emulate3=%d\n", (int)emulate3);
367 fprintf(f, "ShowToolbar=%d\n", (int)showToolbar);
368 fprintf(f, "PointerEventInterval=%d\n", pointerEventInterval);
369 if (monitor.buf)
370 fprintf(f, "Monitor=%s\n", monitor.buf);
371 fprintf(f, "MenuKey=%s\n", CharArray(menuKeyName()).buf);
372 fprintf(f, "AutoReconnect=%d\n", (int)autoReconnect);
373 fprintf(f, "CustomCompressLevel=%d\n", customCompressLevel);
374 fprintf(f, "CompressLevel=%d\n", compressLevel);
375 fprintf(f, "NoJPEG=%d\n", noJpeg);
376 fprintf(f, "QualityLevel=%d\n", qualityLevel);
george82444e8862006-05-27 10:21:28 +0000377 fprintf(f, "AutoScaling=%d\n", (int)autoScaling);
378 fprintf(f, "Scale=%d\n", scale);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000379 fclose(f); f=0;
380
381 setConfigFileName(filename);
382 } catch (rdr::Exception&) {
383 if (f) fclose(f);
384 throw;
385 }
386}
387
388
389void CConnOptions::writeDefaults() {
390 RegKey key;
Peter Åstrand4eacc022009-02-27 10:12:14 +0000391 key.createKey(HKEY_CURRENT_USER, _T("Software\\TigerVNC\\VNCviewer4"));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000392 key.setBool(_T("UseLocalCursor"), useLocalCursor);
393 key.setBool(_T("UseDesktopResize"), useDesktopResize);
394 key.setBool(_T("FullScreen"), fullScreen);
395 key.setBool(_T("FullColour"), fullColour);
396 key.setInt(_T("LowColourLevel"), lowColourLevel);
397 key.setString(_T("PreferredEncoding"), TStr(encodingName(preferredEncoding)));
398 key.setBool(_T("AutoSelect"), autoSelect);
399 key.setBool(_T("Shared"), shared);
400 key.setBool(_T("SendPointerEvents"), sendPtrEvents);
401 key.setBool(_T("SendKeyEvents"), sendKeyEvents);
402 key.setBool(_T("ClientCutText"), clientCutText);
403 key.setBool(_T("ServerCutText"), serverCutText);
404 key.setBool(_T("DisableWinKeys"), disableWinKeys);
405 key.setBool(_T("Protocol3.3"), protocol3_3);
406 key.setBool(_T("AcceptBell"), acceptBell);
407 key.setBool(_T("ShowToolbar"), showToolbar);
408 key.setBool(_T("Emulate3"), emulate3);
409 key.setInt(_T("PointerEventInterval"), pointerEventInterval);
410 if (monitor.buf)
411 key.setString(_T("Monitor"), TStr(monitor.buf));
412 key.setString(_T("MenuKey"), TCharArray(menuKeyName()).buf);
413 key.setBool(_T("AutoReconnect"), autoReconnect);
414 key.setInt(_T("CustomCompressLevel"), customCompressLevel);
415 key.setInt(_T("CompressLevel"), compressLevel);
416 key.setInt(_T("NoJPEG"), noJpeg);
417 key.setInt(_T("QualityLevel"), qualityLevel);
george82444e8862006-05-27 10:21:28 +0000418 key.setBool(_T("AutoScaling"), autoScaling);
419 key.setInt(_T("Scale"), scale);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000420}
421
422
423void CConnOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));}
424void CConnOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));}
425void CConnOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));}
426void CConnOptions::setHost(const char* h) {host.replaceBuf(strDup(h));}
427void CConnOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));}
428
429void CConnOptions::setMenuKey(const char* keyName) {
430 if (!keyName[0]) {
431 menuKey = 0;
432 } else {
433 menuKey = VK_F8;
434 if (keyName[0] == 'F') {
435 UINT fKey = atoi(&keyName[1]);
436 if (fKey >= 1 && fKey <= 12)
437 menuKey = fKey-1 + VK_F1;
438 }
439 }
440}
441char* CConnOptions::menuKeyName() {
442 int fNum = (menuKey-VK_F1)+1;
443 if (fNum<1 || fNum>12)
444 return strDup("");
445 CharArray menuKeyStr(4);
446 sprintf(menuKeyStr.buf, "F%d", fNum);
447 return menuKeyStr.takeBuf();
448}
449
450
451CConnOptions& CConnOptions::operator=(const CConnOptions& o) {
452 useLocalCursor = o.useLocalCursor;
453 useDesktopResize = o.useDesktopResize;
454 fullScreen = o.fullScreen;
455 fullColour = o.fullColour;
456 lowColourLevel = o.lowColourLevel;
457 preferredEncoding = o.preferredEncoding;
458 autoSelect = o.autoSelect;
459 shared = o.shared;
460 sendPtrEvents = o.sendPtrEvents;
461 sendKeyEvents = o.sendKeyEvents;
462 clientCutText = o.clientCutText;
463 serverCutText = o.serverCutText;
464 disableWinKeys = o.disableWinKeys;
465 emulate3 = o.emulate3;
466 pointerEventInterval = o.pointerEventInterval;
467 protocol3_3 = o.protocol3_3;
468 acceptBell = o.acceptBell;
469 showToolbar = o.showToolbar;
470 setUserName(o.userName.buf);
471 setPassword(o.password.buf);
472 setConfigFileName(o.configFileName.buf);
473 setHost(o.host.buf);
474 setMonitor(o.monitor.buf);
475 menuKey = o.menuKey;
476 autoReconnect = o.autoReconnect;
477 customCompressLevel = o.customCompressLevel;
478 compressLevel = o.compressLevel;
479 noJpeg = o.noJpeg;
480 qualityLevel = o.qualityLevel;
george82444e8862006-05-27 10:21:28 +0000481 autoScaling = o.autoScaling;
482 scale = o.scale;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000483
484 return *this;
485}