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