blob: 3af422ee509b7439596060efe9ae3b839e3a092c [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",
118 6);
119
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);
george82c439ebb2007-04-30 05:21:41 +0000126static IntParameter scaleFilter("ScaleFilter",
127 "Filter used for the remote desktop scaling. "
128 "0 = Nearest Neighbor, 1 = Bilinear, 2 = Bicubic, 3 = Sinc.",
129 rfb::defaultScaleFilter);
george82444e8862006-05-27 10:21:28 +0000130
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000131CConnOptions::CConnOptions()
132: useLocalCursor(::useLocalCursor), useDesktopResize(::useDesktopResize),
133autoSelect(::autoSelect), fullColour(::fullColour), fullScreen(::fullScreen),
134shared(::sharedConnection), sendPtrEvents(::sendPtrEvents), sendKeyEvents(::sendKeyEvents),
135preferredEncoding(encodingZRLE), clientCutText(::clientCutText), serverCutText(::serverCutText),
136disableWinKeys(::disableWinKeys), protocol3_3(::protocol3_3), acceptBell(::acceptBell),
137lowColourLevel(::lowColourLevel), pointerEventInterval(ptrEventInterval),
138emulate3(::emulate3), monitor(::monitor.getData()), showToolbar(::showToolbar),
139customCompressLevel(::customCompressLevel), compressLevel(::compressLevel),
140noJpeg(::noJpeg), qualityLevel(::qualityLevel), passwordFile(::passwordFile.getData()),
george82c439ebb2007-04-30 05:21:41 +0000141autoReconnect(::autoReconnect), autoScaling(::autoScaling), scale(::scale), scaleFilter(::scaleFilter)
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000142{
143 if (autoSelect) {
144 preferredEncoding = encodingZRLE;
145 } else {
146 CharArray encodingName(::preferredEncoding.getData());
147 preferredEncoding = encodingNum(encodingName.buf);
148 }
149 setMenuKey(CharArray(::menuKey.getData()).buf);
150
151 if (!::autoSelect.hasBeenSet()) {
152 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
153 autoSelect = (!::preferredEncoding.hasBeenSet()
154 && !::fullColour.hasBeenSet()
155 && !::fullColourAlias.hasBeenSet());
156 }
157 if (!::customCompressLevel.hasBeenSet()) {
158 // Default to CustomCompressLevel=1 if CompressLevel is used.
159 customCompressLevel = ::compressLevel.hasBeenSet();
160 }
161}
162
163
164void CConnOptions::readFromFile(const char* filename) {
165 FILE* f = fopen(filename, "r");
166 if (!f)
167 throw rdr::Exception("Failed to read configuration file");
168
169 try {
170 char line[4096];
171 CharArray section;
172
173 CharArray hostTmp;
174 int portTmp = 0;
175
176 while (!feof(f)) {
177 // Read the next line
178 if (!fgets(line, sizeof(line), f)) {
179 if (feof(f))
180 break;
181 throw rdr::SystemException("fgets", ferror(f));
182 }
183 int len=strlen(line);
184 if (line[len-1] == '\n') {
185 line[len-1] = 0;
186 len--;
187 }
188
189 // Process the line
190 if (line[0] == ';') {
191 // Comment
192 } else if (line[0] == '[') {
193 // Entering a new section
194 if (!strSplit(&line[1], ']', &section.buf, 0))
195 throw rdr::Exception("bad Section");
196 } else {
197 // Reading an option
198 CharArray name;
199 CharArray value;
200 if (!strSplit(line, '=', &name.buf, &value.buf))
201 throw rdr::Exception("bad Name/Value pair");
202
203 if (stricmp(section.buf, "Connection") == 0) {
204 if (stricmp(name.buf, "Host") == 0) {
205 hostTmp.replaceBuf(value.takeBuf());
206 } else if (stricmp(name.buf, "Port") == 0) {
207 portTmp = atoi(value.buf);
208 } else if (stricmp(name.buf, "UserName") == 0) {
209 userName.replaceBuf(value.takeBuf());
210 } else if (stricmp(name.buf, "Password") == 0) {
211 ObfuscatedPasswd obfPwd;
212 rdr::HexInStream::hexStrToBin(value.buf, (char**)&obfPwd.buf, &obfPwd.length);
213 PlainPasswd passwd(obfPwd);
214 password.replaceBuf(passwd.takeBuf());
215 }
216 } else if (stricmp(section.buf, "Options") == 0) {
217 // V4 options
218 if (stricmp(name.buf, "UseLocalCursor") == 0) {
219 useLocalCursor = atoi(value.buf);
220 } else if (stricmp(name.buf, "UseDesktopResize") == 0) {
221 useDesktopResize = atoi(value.buf);
222 } else if (stricmp(name.buf, "FullScreen") == 0) {
223 fullScreen = atoi(value.buf);
224 } else if (stricmp(name.buf, "FullColour") == 0) {
225 fullColour = atoi(value.buf);
226 } else if (stricmp(name.buf, "LowColourLevel") == 0) {
227 lowColourLevel = atoi(value.buf);
228 } else if (stricmp(name.buf, "PreferredEncoding") == 0) {
229 preferredEncoding = encodingNum(value.buf);
230 } else if ((stricmp(name.buf, "AutoDetect") == 0) ||
231 (stricmp(name.buf, "AutoSelect") == 0)) {
232 autoSelect = atoi(value.buf);
233 } else if (stricmp(name.buf, "Shared") == 0) {
234 shared = atoi(value.buf);
235 } else if (stricmp(name.buf, "SendPtrEvents") == 0) {
236 sendPtrEvents = atoi(value.buf);
237 } else if (stricmp(name.buf, "SendKeyEvents") == 0) {
238 sendKeyEvents = atoi(value.buf);
239 } else if (stricmp(name.buf, "SendCutText") == 0) {
240 clientCutText = atoi(value.buf);
241 } else if (stricmp(name.buf, "AcceptCutText") == 0) {
242 serverCutText = atoi(value.buf);
243 } else if (stricmp(name.buf, "DisableWinKeys") == 0) {
244 disableWinKeys = atoi(value.buf);
245 } else if (stricmp(name.buf, "AcceptBell") == 0) {
246 acceptBell = atoi(value.buf);
247 } else if (stricmp(name.buf, "Emulate3") == 0) {
248 emulate3 = atoi(value.buf);
249 } else if (stricmp(name.buf, "ShowToolbar") == 0) {
250 showToolbar = atoi(value.buf);
251 } else if (stricmp(name.buf, "PointerEventInterval") == 0) {
252 pointerEventInterval = atoi(value.buf);
253 } else if (stricmp(name.buf, "Monitor") == 0) {
254 monitor.replaceBuf(value.takeBuf());
255 } else if (stricmp(name.buf, "MenuKey") == 0) {
256 setMenuKey(value.buf);
257 } else if (stricmp(name.buf, "AutoReconnect") == 0) {
258 autoReconnect = atoi(value.buf);
259
260 } else if (stricmp(name.buf, "CustomCompressLevel") == 0) {
261 customCompressLevel = atoi(value.buf);
262 } else if (stricmp(name.buf, "CompressLevel") == 0) {
263 compressLevel = atoi(value.buf);
264 } else if (stricmp(name.buf, "NoJPEG") == 0) {
265 noJpeg = atoi(value.buf);
266 } else if (stricmp(name.buf, "QualityLevel") == 0) {
267 qualityLevel = atoi(value.buf);
268 // Legacy options
269 } else if (stricmp(name.buf, "Preferred_Encoding") == 0) {
270 preferredEncoding = atoi(value.buf);
271 } else if (stricmp(name.buf, "8bit") == 0) {
272 fullColour = !atoi(value.buf);
273 } else if (stricmp(name.buf, "FullScreen") == 0) {
274 fullScreen = atoi(value.buf);
275 } else if (stricmp(name.buf, "ViewOnly") == 0) {
276 sendPtrEvents = sendKeyEvents = !atoi(value.buf);
277 } else if (stricmp(name.buf, "DisableClipboard") == 0) {
278 clientCutText = serverCutText = !atoi(value.buf);
george82444e8862006-05-27 10:21:28 +0000279 } else if (stricmp(name.buf, "AutoScaling") == 0) {
280 autoScaling = atoi(value.buf);
281 } else if (stricmp(name.buf, "Scale") == 0) {
282 scale = atoi(value.buf);
george82c439ebb2007-04-30 05:21:41 +0000283 } else if (stricmp(name.buf, "ScaleFilter") == 0) {
284 int scaleFilterID = atoi(value.buf);
285 if (scaleFilterID > rfb::scaleFilterMaxNumber || scaleFilterID < 0 ) {
286 scaleFilter = rfb::defaultScaleFilter;
287 } else scaleFilter = scaleFilterID;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000288 }
289 }
290 }
291 }
292 fclose(f); f=0;
293
294 // Process the Host and Port
295 if (hostTmp.buf) {
296 int hostLen = strlen(hostTmp.buf) + 2 + 17;
297 host.replaceBuf(new char[hostLen]);
298 strCopy(host.buf, hostTmp.buf, hostLen);
299 if (portTmp) {
300 strncat(host.buf, "::", hostLen-1);
301 char tmp[16];
302 sprintf(tmp, "%d", portTmp);
303 strncat(host.buf, tmp, hostLen-1);
304 }
305 }
306
307 // If AutoSelect is enabled then override the preferred encoding
308 if (autoSelect)
309 preferredEncoding = encodingZRLE;
310
311 setConfigFileName(filename);
312 } catch (rdr::Exception&) {
313 if (f) fclose(f);
314 throw;
315 }
316}
317
318void CConnOptions::writeToFile(const char* filename) {
319 FILE* f = fopen(filename, "w");
320 if (!f)
321 throw rdr::Exception("Failed to write configuration file");
322
323 try {
324 // - Split server into host and port and save
325 fprintf(f, "[Connection]\n");
326
327 fprintf(f, "Host=%s\n", host.buf);
328 if (userName.buf)
329 fprintf(f, "UserName=%s\n", userName.buf);
330 if (password.buf) {
331 // - Warn the user before saving the password
332 if (MsgBox(0, _T("Do you want to include the VNC Password in this configuration file?\n")
333 _T("Storing the password is more convenient but poses a security risk."),
334 MB_YESNO | MB_DEFBUTTON2 | MB_ICONWARNING) == IDYES) {
335 ObfuscatedPasswd obfPwd(password);
336 CharArray obfuscatedHex = rdr::HexOutStream::binToHexStr(obfPwd.buf, obfPwd.length);
337 fprintf(f, "Password=%s\n", obfuscatedHex.buf);
338 }
339 }
340
341 // - Save the other options
342 fprintf(f, "[Options]\n");
343
344 fprintf(f, "UseLocalCursor=%d\n", (int)useLocalCursor);
345 fprintf(f, "UseDesktopResize=%d\n", (int)useDesktopResize);
346 fprintf(f, "FullScreen=%d\n", (int)fullScreen);
347 fprintf(f, "FullColour=%d\n", (int)fullColour);
348 fprintf(f, "LowColourLevel=%d\n", lowColourLevel);
349 fprintf(f, "PreferredEncoding=%s\n", encodingName(preferredEncoding));
350 fprintf(f, "AutoSelect=%d\n", (int)autoSelect);
351 fprintf(f, "Shared=%d\n", (int)shared);
352 fprintf(f, "SendPtrEvents=%d\n", (int)sendPtrEvents);
353 fprintf(f, "SendKeyEvents=%d\n", (int)sendKeyEvents);
354 fprintf(f, "SendCutText=%d\n", (int)clientCutText);
355 fprintf(f, "AcceptCutText=%d\n", (int)serverCutText);
356 fprintf(f, "DisableWinKeys=%d\n", (int)disableWinKeys);
357 fprintf(f, "AcceptBell=%d\n", (int)acceptBell);
358 fprintf(f, "Emulate3=%d\n", (int)emulate3);
359 fprintf(f, "ShowToolbar=%d\n", (int)showToolbar);
360 fprintf(f, "PointerEventInterval=%d\n", pointerEventInterval);
361 if (monitor.buf)
362 fprintf(f, "Monitor=%s\n", monitor.buf);
363 fprintf(f, "MenuKey=%s\n", CharArray(menuKeyName()).buf);
364 fprintf(f, "AutoReconnect=%d\n", (int)autoReconnect);
365 fprintf(f, "CustomCompressLevel=%d\n", customCompressLevel);
366 fprintf(f, "CompressLevel=%d\n", compressLevel);
367 fprintf(f, "NoJPEG=%d\n", noJpeg);
368 fprintf(f, "QualityLevel=%d\n", qualityLevel);
george82444e8862006-05-27 10:21:28 +0000369 fprintf(f, "AutoScaling=%d\n", (int)autoScaling);
370 fprintf(f, "Scale=%d\n", scale);
george82c439ebb2007-04-30 05:21:41 +0000371 fprintf(f, "ScaleFilter=%d\n", scaleFilter);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000372 fclose(f); f=0;
373
374 setConfigFileName(filename);
375 } catch (rdr::Exception&) {
376 if (f) fclose(f);
377 throw;
378 }
379}
380
381
382void CConnOptions::writeDefaults() {
383 RegKey key;
384 key.createKey(HKEY_CURRENT_USER, _T("Software\\TightVNC\\VNCviewer4"));
385 key.setBool(_T("UseLocalCursor"), useLocalCursor);
386 key.setBool(_T("UseDesktopResize"), useDesktopResize);
387 key.setBool(_T("FullScreen"), fullScreen);
388 key.setBool(_T("FullColour"), fullColour);
389 key.setInt(_T("LowColourLevel"), lowColourLevel);
390 key.setString(_T("PreferredEncoding"), TStr(encodingName(preferredEncoding)));
391 key.setBool(_T("AutoSelect"), autoSelect);
392 key.setBool(_T("Shared"), shared);
393 key.setBool(_T("SendPointerEvents"), sendPtrEvents);
394 key.setBool(_T("SendKeyEvents"), sendKeyEvents);
395 key.setBool(_T("ClientCutText"), clientCutText);
396 key.setBool(_T("ServerCutText"), serverCutText);
397 key.setBool(_T("DisableWinKeys"), disableWinKeys);
398 key.setBool(_T("Protocol3.3"), protocol3_3);
399 key.setBool(_T("AcceptBell"), acceptBell);
400 key.setBool(_T("ShowToolbar"), showToolbar);
401 key.setBool(_T("Emulate3"), emulate3);
402 key.setInt(_T("PointerEventInterval"), pointerEventInterval);
403 if (monitor.buf)
404 key.setString(_T("Monitor"), TStr(monitor.buf));
405 key.setString(_T("MenuKey"), TCharArray(menuKeyName()).buf);
406 key.setBool(_T("AutoReconnect"), autoReconnect);
407 key.setInt(_T("CustomCompressLevel"), customCompressLevel);
408 key.setInt(_T("CompressLevel"), compressLevel);
409 key.setInt(_T("NoJPEG"), noJpeg);
410 key.setInt(_T("QualityLevel"), qualityLevel);
george82444e8862006-05-27 10:21:28 +0000411 key.setBool(_T("AutoScaling"), autoScaling);
412 key.setInt(_T("Scale"), scale);
george82c439ebb2007-04-30 05:21:41 +0000413 key.setInt(_T("ScaleFilter"), scaleFilter);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000414}
415
416
417void CConnOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));}
418void CConnOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));}
419void CConnOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));}
420void CConnOptions::setHost(const char* h) {host.replaceBuf(strDup(h));}
421void CConnOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));}
422
423void CConnOptions::setMenuKey(const char* keyName) {
424 if (!keyName[0]) {
425 menuKey = 0;
426 } else {
427 menuKey = VK_F8;
428 if (keyName[0] == 'F') {
429 UINT fKey = atoi(&keyName[1]);
430 if (fKey >= 1 && fKey <= 12)
431 menuKey = fKey-1 + VK_F1;
432 }
433 }
434}
435char* CConnOptions::menuKeyName() {
436 int fNum = (menuKey-VK_F1)+1;
437 if (fNum<1 || fNum>12)
438 return strDup("");
439 CharArray menuKeyStr(4);
440 sprintf(menuKeyStr.buf, "F%d", fNum);
441 return menuKeyStr.takeBuf();
442}
443
444
445CConnOptions& CConnOptions::operator=(const CConnOptions& o) {
446 useLocalCursor = o.useLocalCursor;
447 useDesktopResize = o.useDesktopResize;
448 fullScreen = o.fullScreen;
449 fullColour = o.fullColour;
450 lowColourLevel = o.lowColourLevel;
451 preferredEncoding = o.preferredEncoding;
452 autoSelect = o.autoSelect;
453 shared = o.shared;
454 sendPtrEvents = o.sendPtrEvents;
455 sendKeyEvents = o.sendKeyEvents;
456 clientCutText = o.clientCutText;
457 serverCutText = o.serverCutText;
458 disableWinKeys = o.disableWinKeys;
459 emulate3 = o.emulate3;
460 pointerEventInterval = o.pointerEventInterval;
461 protocol3_3 = o.protocol3_3;
462 acceptBell = o.acceptBell;
463 showToolbar = o.showToolbar;
464 setUserName(o.userName.buf);
465 setPassword(o.password.buf);
466 setConfigFileName(o.configFileName.buf);
467 setHost(o.host.buf);
468 setMonitor(o.monitor.buf);
469 menuKey = o.menuKey;
470 autoReconnect = o.autoReconnect;
471 customCompressLevel = o.customCompressLevel;
472 compressLevel = o.compressLevel;
473 noJpeg = o.noJpeg;
474 qualityLevel = o.qualityLevel;
george82444e8862006-05-27 10:21:28 +0000475 autoScaling = o.autoScaling;
476 scale = o.scale;
george82c439ebb2007-04-30 05:21:41 +0000477 scaleFilter = o.scaleFilter;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000478
479 return *this;
480}