blob: 39a1a5ca746e480b023ecaad117686d8427aa767 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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/CViewOptions.h>
20#include <rfb/Configuration.h>
21#include <rfb/encodings.h>
22#include <rfb/vncAuth.h>
23#include <rfb/LogWriter.h>
24#include <rfb_win32/Win32Util.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
Peter Åstrand3e3c75e2005-01-03 13:32:28 +000033static StringParameter passwordFile("PasswordFile",
34 "Password file for VNC authentication", "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000035
36static BoolParameter useLocalCursor("UseLocalCursor", "Render the mouse cursor locally", true);
37static BoolParameter useDesktopResize("UseDesktopResize", "Support dynamic desktop resizing", true);
38
Peter Åstrandc81a6522004-12-30 11:32:08 +000039static BoolParameter fullColour("FullColor",
40 "Use full color", true);
41static AliasParameter fullColourAlias("FullColour", "Alias for FullColor", &fullColour);
42
43static IntParameter lowColourLevel("LowColorLevel",
44 "Color level to use on slow connections. "
45 "0 = Very Low (8 colors), 1 = Low (64 colors), 2 = Medium (256 colors)",
Peter Åstranddd747d82004-12-21 15:54:44 +000046 2);
Peter Åstrandc81a6522004-12-30 11:32:08 +000047static AliasParameter lowColourLevelAlias("LowColourLevel", "Alias for LowColorLevel", &lowColourLevel);
48
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000049static BoolParameter fullScreen("FullScreen",
50 "Use the whole display to show the remote desktop."
51 "(Press F8 to access the viewer menu)",
52 false);
53static StringParameter preferredEncoding("PreferredEncoding",
Peter Åstrand55855d52005-01-03 12:01:45 +000054 "Preferred encoding to use (Tight, ZRLE, Hextile or"
55 " Raw)", "Tight");
56static BoolParameter autoSelect("AutoSelect",
57 "Auto select pixel format and encoding. "
58 "Default if PreferredEncoding and FullColor are not specified.",
59 true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000060static BoolParameter sharedConnection("Shared",
61 "Allow existing connections to the server to continue."
62 "(Default is to disconnect all other clients)",
63 false);
64
65static BoolParameter sendPtrEvents("SendPointerEvents",
66 "Send pointer (mouse) events to the server.", true);
67static BoolParameter sendKeyEvents("SendKeyEvents",
68 "Send key presses (and releases) to the server.", true);
Peter Åstrand57dcc452005-01-28 14:52:50 +000069static BoolParameter sendSysKeys("SendSysKeys",
70 "Send system keys (Alt combinations) to the server.", true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000071
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 protocol3_3("Protocol3.3",
78 "Only use protocol version 3.3", false);
79
80static IntParameter ptrEventInterval("PointerEventInterval",
81 "The interval to delay between sending one pointer event "
82 "and the next.", 0);
83static BoolParameter emulate3("Emulate3",
84 "Emulate middle mouse button when left and right buttons "
85 "are used simulatenously.", false);
86
87static BoolParameter acceptBell("AcceptBell",
88 "Produce a system beep when requested to by the server.",
89 true);
90
91static StringParameter monitor("Monitor", "The monitor to open the VNC Viewer window on, if available.", "");
92static StringParameter menuKey("MenuKey", "The key which brings up the popup menu", "F8");
93
Peter Åstrand365427a2004-12-29 10:59:03 +000094static BoolParameter customCompressLevel("CustomCompressLevel",
Peter Åstrand55855d52005-01-03 12:01:45 +000095 "Use custom compression level. "
96 "Default if CompressLevel is specified.", false);
Peter Åstrand365427a2004-12-29 10:59:03 +000097
98static IntParameter compressLevel("CompressLevel",
99 "Use specified compression level"
100 "0 = Low, 9 = High",
101 6);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000102
Peter Åstrand0b870262004-12-28 15:55:46 +0000103static BoolParameter noJpeg("NoJPEG",
104 "Disable lossy JPEG compression in Tight encoding.",
105 false);
106
Peter Åstrand365427a2004-12-29 10:59:03 +0000107static IntParameter qualityLevel("QualityLevel",
108 "JPEG quality level. "
109 "0 = Low, 9 = High",
110 6);
111
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000112CViewOptions::CViewOptions()
113: useLocalCursor(::useLocalCursor), useDesktopResize(::useDesktopResize),
114autoSelect(::autoSelect), fullColour(::fullColour), fullScreen(::fullScreen),
Peter Åstrand57dcc452005-01-28 14:52:50 +0000115shared(::sharedConnection), sendPtrEvents(::sendPtrEvents), sendKeyEvents(::sendKeyEvents), sendSysKeys(::sendSysKeys),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000116preferredEncoding(encodingZRLE), clientCutText(::clientCutText), serverCutText(::serverCutText),
117protocol3_3(::protocol3_3), acceptBell(::acceptBell), lowColourLevel(::lowColourLevel),
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000118pointerEventInterval(ptrEventInterval), emulate3(::emulate3), monitor(::monitor.getData()),
Peter Åstrand365427a2004-12-29 10:59:03 +0000119customCompressLevel(::customCompressLevel), compressLevel(::compressLevel),
Peter Åstrand3e3c75e2005-01-03 13:32:28 +0000120noJpeg(::noJpeg), qualityLevel(::qualityLevel), passwordFile(::passwordFile.getData())
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000121{
122 CharArray encodingName(::preferredEncoding.getData());
123 preferredEncoding = encodingNum(encodingName.buf);
124 setMenuKey(CharArray(::menuKey.getData()).buf);
Peter Åstrand55855d52005-01-03 12:01:45 +0000125
126 if (!::autoSelect.hasBeenSet()) {
127 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
128 autoSelect = (!::preferredEncoding.hasBeenSet()
129 && !::fullColour.hasBeenSet()
130 && !::fullColourAlias.hasBeenSet());
131 }
132 if (!::customCompressLevel.hasBeenSet()) {
133 // Default to CustomCompressLevel=1 if CompressLevel is used.
134 customCompressLevel = ::compressLevel.hasBeenSet();
135 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000136}
137
138
139void CViewOptions::readFromFile(const char* filename) {
140 FILE* f = fopen(filename, "r");
141 if (!f)
142 throw rdr::Exception("Failed to read configuration file");
143
144 try {
145 char line[4096];
146 CharArray section;
147
148 CharArray hostTmp;
149 int portTmp = 0;
150
151 while (!feof(f)) {
152 // Read the next line
153 if (!fgets(line, sizeof(line), f)) {
154 if (feof(f))
155 break;
156 throw rdr::SystemException("fgets", ferror(f));
157 }
158 int len=strlen(line);
159 if (line[len-1] == '\n') {
160 line[len-1] = 0;
161 len--;
162 }
163
164 // Process the line
165 if (line[0] == ';') {
166 // Comment
167 } else if (line[0] == '[') {
168 // Entering a new section
169 if (!strSplit(&line[1], ']', &section.buf, 0))
170 throw rdr::Exception("bad Section");
171 } else {
172 // Reading an option
173 CharArray name;
174 CharArray value;
175 if (!strSplit(line, '=', &name.buf, &value.buf))
176 throw rdr::Exception("bad Name/Value pair");
177
178 if (stricmp(section.buf, "Connection") == 0) {
179 if (stricmp(name.buf, "Host") == 0) {
180 hostTmp.replaceBuf(value.takeBuf());
181 } else if (stricmp(name.buf, "Port") == 0) {
182 portTmp = atoi(value.buf);
183 } else if (stricmp(name.buf, "UserName") == 0) {
184 userName.replaceBuf(value.takeBuf());
185 } else if (stricmp(name.buf, "Password") == 0) {
186 int len = 0;
187 CharArray obfuscated;
188 rdr::HexInStream::hexStrToBin(value.buf, &obfuscated.buf, &len);
189 if (len == 8) {
190 password.replaceBuf(new char[9]);
191 memcpy(password.buf, obfuscated.buf, 8);
192 vncAuthUnobfuscatePasswd(password.buf);
193 password.buf[8] = 0;
194 }
195 }
196 } else if (stricmp(section.buf, "Options") == 0) {
197 // V4 options
198 if (stricmp(name.buf, "UseLocalCursor") == 0) {
199 useLocalCursor = atoi(value.buf);
200 } else if (stricmp(name.buf, "UseDesktopResize") == 0) {
201 useDesktopResize = atoi(value.buf);
202 } else if (stricmp(name.buf, "FullScreen") == 0) {
203 fullScreen = atoi(value.buf);
204 } else if (stricmp(name.buf, "FullColour") == 0) {
205 fullColour = atoi(value.buf);
206 } else if (stricmp(name.buf, "LowColourLevel") == 0) {
207 lowColourLevel = atoi(value.buf);
208 } else if (stricmp(name.buf, "PreferredEncoding") == 0) {
209 preferredEncoding = encodingNum(value.buf);
210 } else if ((stricmp(name.buf, "AutoDetect") == 0) ||
211 (stricmp(name.buf, "AutoSelect") == 0)) {
212 autoSelect = atoi(value.buf);
213 } else if (stricmp(name.buf, "Shared") == 0) {
214 shared = atoi(value.buf);
215 } else if (stricmp(name.buf, "SendPtrEvents") == 0) {
216 sendPtrEvents = atoi(value.buf);
217 } else if (stricmp(name.buf, "SendKeyEvents") == 0) {
218 sendKeyEvents = atoi(value.buf);
Peter Åstrand57dcc452005-01-28 14:52:50 +0000219 } else if (stricmp(name.buf, "SendSysKeys") == 0) {
220 sendSysKeys = atoi(value.buf);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000221 } else if (stricmp(name.buf, "SendCutText") == 0) {
222 clientCutText = atoi(value.buf);
223 } else if (stricmp(name.buf, "AcceptCutText") == 0) {
224 serverCutText = atoi(value.buf);
225 } else if (stricmp(name.buf, "Emulate3") == 0) {
226 emulate3 = atoi(value.buf);
227 } else if (stricmp(name.buf, "PointerEventInterval") == 0) {
228 pointerEventInterval = atoi(value.buf);
229 } else if (stricmp(name.buf, "Monitor") == 0) {
230 monitor.replaceBuf(value.takeBuf());
231 } else if (stricmp(name.buf, "MenuKey") == 0) {
232 setMenuKey(value.buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000233 } else if (stricmp(name.buf, "CustomCompressLevel") == 0) {
234 customCompressLevel = atoi(value.buf);
235 } else if (stricmp(name.buf, "CompressLevel") == 0) {
236 compressLevel = atoi(value.buf);
Peter Åstrand0b870262004-12-28 15:55:46 +0000237 } else if (stricmp(name.buf, "NoJPEG") == 0) {
238 noJpeg = atoi(value.buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000239 } else if (stricmp(name.buf, "QualityLevel") == 0) {
240 qualityLevel = atoi(value.buf);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000241 // Legacy options
242 } else if (stricmp(name.buf, "Preferred_Encoding") == 0) {
243 preferredEncoding = atoi(value.buf);
244 } else if (stricmp(name.buf, "8bit") == 0) {
245 fullColour = !atoi(value.buf);
246 } else if (stricmp(name.buf, "FullScreen") == 0) {
247 fullScreen = atoi(value.buf);
248 } else if (stricmp(name.buf, "ViewOnly") == 0) {
249 sendPtrEvents = sendKeyEvents = !atoi(value.buf);
250 } else if (stricmp(name.buf, "DisableClipboard") == 0) {
251 clientCutText = serverCutText = !atoi(value.buf);
252 }
253 }
254 }
255 }
256 fclose(f); f=0;
257
258 // Process the Host and Port
259 if (hostTmp.buf) {
260 int hostLen = strlen(hostTmp.buf) + 2 + 17;
261 host.replaceBuf(new char[hostLen]);
262 strCopy(host.buf, hostTmp.buf, hostLen);
263 if (portTmp) {
264 strncat(host.buf, "::", hostLen-1);
265 char tmp[16];
266 sprintf(tmp, "%d", portTmp);
267 strncat(host.buf, tmp, hostLen-1);
268 }
269 }
270
271 setConfigFileName(filename);
272 } catch (rdr::Exception&) {
273 if (f) fclose(f);
274 throw;
275 }
276}
277
278void CViewOptions::writeToFile(const char* filename) {
279 FILE* f = fopen(filename, "w");
280 if (!f)
281 throw rdr::Exception("Failed to write configuration file");
282
283 try {
284 // - Split server into host and port and save
285 fprintf(f, "[Connection]\n");
286
287 fprintf(f, "Host=%s\n", host.buf);
288 if (userName.buf)
289 fprintf(f, "UserName=%s\n", userName.buf);
290 if (password.buf) {
291 // - Warn the user before saving the password
292 if (MsgBox(0, _T("Do you want to include the VNC Password in this configuration file?\n")
293 _T("Storing the password is more convenient but poses a security risk."),
294 MB_YESNO | MB_DEFBUTTON2 | MB_ICONWARNING) == IDYES) {
295 char obfuscated[9];
296 memset(obfuscated, 0, sizeof(obfuscated));
297 strCopy(obfuscated, password.buf, sizeof(obfuscated));
298 vncAuthObfuscatePasswd(obfuscated);
299 CharArray obfuscatedHex = rdr::HexOutStream::binToHexStr(obfuscated, 8);
300 fprintf(f, "Password=%s\n", obfuscatedHex.buf);
301 }
302 }
303
304 // - Save the other options
305 fprintf(f, "[Options]\n");
306
307 fprintf(f, "UseLocalCursor=%d\n", (int)useLocalCursor);
308 fprintf(f, "UseDesktopResize=%d\n", (int)useDesktopResize);
309 fprintf(f, "FullScreen=%d\n", (int)fullScreen);
310 fprintf(f, "FullColour=%d\n", (int)fullColour);
311 fprintf(f, "LowColourLevel=%d\n", lowColourLevel);
312 fprintf(f, "PreferredEncoding=%s\n", encodingName(preferredEncoding));
313 fprintf(f, "AutoSelect=%d\n", (int)autoSelect);
314 fprintf(f, "Shared=%d\n", (int)shared);
315 fprintf(f, "SendPtrEvents=%d\n", (int)sendPtrEvents);
316 fprintf(f, "SendKeyEvents=%d\n", (int)sendKeyEvents);
Peter Åstrand57dcc452005-01-28 14:52:50 +0000317 fprintf(f, "SendSysKeys=%d\n", (int)sendSysKeys);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000318 fprintf(f, "SendCutText=%d\n", (int)clientCutText);
319 fprintf(f, "AcceptCutText=%d\n", (int)serverCutText);
320 fprintf(f, "Emulate3=%d\n", (int)emulate3);
321 fprintf(f, "PointerEventInterval=%d\n", pointerEventInterval);
322 if (monitor.buf)
323 fprintf(f, "Monitor=%s\n", monitor.buf);
324 fprintf(f, "MenuKey=%s\n", CharArray(menuKeyName()).buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000325 fprintf(f, "CustomCompressLevel=%d\n", customCompressLevel);
326 fprintf(f, "CompressLevel=%d\n", compressLevel);
Peter Åstrand0b870262004-12-28 15:55:46 +0000327 fprintf(f, "NoJPEG=%d\n", noJpeg);
Peter Åstrand365427a2004-12-29 10:59:03 +0000328 fprintf(f, "QualityLevel=%d\n", qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000329 fclose(f); f=0;
330
331 setConfigFileName(filename);
332 } catch (rdr::Exception&) {
333 if (f) fclose(f);
334 throw;
335 }
336}
337
338
339void CViewOptions::writeDefaults() {
340 RegKey key;
Peter Åstrand9fb4e0e2004-12-30 10:03:00 +0000341 key.createKey(HKEY_CURRENT_USER, _T("Software\\TightVNC\\VNCviewer4"));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000342 key.setBool(_T("UseLocalCursor"), useLocalCursor);
343 key.setBool(_T("UseDesktopResize"), useDesktopResize);
344 key.setBool(_T("FullScreen"), fullScreen);
345 key.setBool(_T("FullColour"), fullColour);
346 key.setInt(_T("LowColourLevel"), lowColourLevel);
347 key.setString(_T("PreferredEncoding"), TStr(encodingName(preferredEncoding)));
348 key.setBool(_T("AutoSelect"), autoSelect);
349 key.setBool(_T("Shared"), shared);
350 key.setBool(_T("SendPointerEvents"), sendPtrEvents);
351 key.setBool(_T("SendKeyEvents"), sendKeyEvents);
Peter Åstrand57dcc452005-01-28 14:52:50 +0000352 key.setBool(_T("SendSysKeys"), sendSysKeys);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000353 key.setBool(_T("ClientCutText"), clientCutText);
354 key.setBool(_T("ServerCutText"), serverCutText);
355 key.setBool(_T("Protocol3.3"), protocol3_3);
356 key.setBool(_T("AcceptBell"), acceptBell);
357 key.setBool(_T("Emulate3"), emulate3);
358 key.setInt(_T("PointerEventInterval"), pointerEventInterval);
359 if (monitor.buf)
360 key.setString(_T("Monitor"), TStr(monitor.buf));
361 key.setString(_T("MenuKey"), TCharArray(menuKeyName()).buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000362 key.setInt(_T("CustomCompressLevel"), customCompressLevel);
363 key.setInt(_T("CompressLevel"), compressLevel);
Peter Åstrand0b870262004-12-28 15:55:46 +0000364 key.setInt(_T("NoJPEG"), noJpeg);
Peter Åstrand365427a2004-12-29 10:59:03 +0000365 key.setInt(_T("QualityLevel"), qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000366}
367
368
369void CViewOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));}
370void CViewOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));}
371void CViewOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));}
372void CViewOptions::setHost(const char* h) {host.replaceBuf(strDup(h));}
373void CViewOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));}
374
375void CViewOptions::setMenuKey(const char* keyName) {
376 if (!keyName[0]) {
377 menuKey = 0;
378 } else {
379 menuKey = VK_F8;
380 if (keyName[0] == 'F') {
381 UINT fKey = atoi(&keyName[1]);
382 if (fKey >= 1 && fKey <= 12)
383 menuKey = fKey-1 + VK_F1;
384 }
385 }
386}
387char* CViewOptions::menuKeyName() {
388 int fNum = (menuKey-VK_F1)+1;
389 if (fNum<1 || fNum>12)
390 return strDup("");
391 CharArray menuKeyStr(4);
392 sprintf(menuKeyStr.buf, "F%d", fNum);
393 return menuKeyStr.takeBuf();
394}
395
396
397CViewOptions& CViewOptions::operator=(const CViewOptions& o) {
398 useLocalCursor = o.useLocalCursor;
399 useDesktopResize = o.useDesktopResize;
400 fullScreen = o.fullScreen;
401 fullColour = o.fullColour;
402 lowColourLevel = o.lowColourLevel;
403 preferredEncoding = o.preferredEncoding;
404 autoSelect = o.autoSelect;
405 shared = o.shared;
406 sendPtrEvents = o.sendPtrEvents;
407 sendKeyEvents = o.sendKeyEvents;
Peter Åstrand57dcc452005-01-28 14:52:50 +0000408 sendSysKeys = o.sendSysKeys;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000409 clientCutText = o.clientCutText;
410 serverCutText = o.serverCutText;
411 emulate3 = o.emulate3;
412 pointerEventInterval = o.pointerEventInterval;
413 protocol3_3 = o.protocol3_3;
414 acceptBell = o.acceptBell;
415 setUserName(o.userName.buf);
416 setPassword(o.password.buf);
417 setConfigFileName(o.configFileName.buf);
418 setHost(o.host.buf);
419 setMonitor(o.monitor.buf);
420 menuKey = o.menuKey;
Peter Åstrand365427a2004-12-29 10:59:03 +0000421 customCompressLevel = o.customCompressLevel;
422 compressLevel = o.compressLevel;
Peter Åstrand0b870262004-12-28 15:55:46 +0000423 noJpeg = o.noJpeg;
Peter Åstrand365427a2004-12-29 10:59:03 +0000424 qualityLevel = o.qualityLevel;
425
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000426 return *this;
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000427}