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