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