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