blob: 1f551296c44cfffecf295ae2be7bb337c6454763 [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
33
34static BoolParameter useLocalCursor("UseLocalCursor", "Render the mouse cursor locally", true);
35static BoolParameter useDesktopResize("UseDesktopResize", "Support dynamic desktop resizing", true);
36
Peter Åstrandc81a6522004-12-30 11:32:08 +000037static BoolParameter fullColour("FullColor",
38 "Use full color", true);
39static AliasParameter fullColourAlias("FullColour", "Alias for FullColor", &fullColour);
40
41static IntParameter lowColourLevel("LowColorLevel",
42 "Color level to use on slow connections. "
43 "0 = Very Low (8 colors), 1 = Low (64 colors), 2 = Medium (256 colors)",
Peter Åstranddd747d82004-12-21 15:54:44 +000044 2);
Peter Åstrandc81a6522004-12-30 11:32:08 +000045static AliasParameter lowColourLevelAlias("LowColourLevel", "Alias for LowColorLevel", &lowColourLevel);
46
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000047static BoolParameter fullScreen("FullScreen",
48 "Use the whole display to show the remote desktop."
49 "(Press F8 to access the viewer menu)",
50 false);
51static StringParameter preferredEncoding("PreferredEncoding",
Peter Åstrand55855d52005-01-03 12:01:45 +000052 "Preferred encoding to use (Tight, ZRLE, Hextile or"
53 " Raw)", "Tight");
54static BoolParameter autoSelect("AutoSelect",
55 "Auto select pixel format and encoding. "
56 "Default if PreferredEncoding and FullColor are not specified.",
57 true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000058static BoolParameter sharedConnection("Shared",
59 "Allow existing connections to the server to continue."
60 "(Default is to disconnect all other clients)",
61 false);
62
63static BoolParameter sendPtrEvents("SendPointerEvents",
64 "Send pointer (mouse) events to the server.", true);
65static BoolParameter sendKeyEvents("SendKeyEvents",
66 "Send key presses (and releases) to the server.", true);
67
68static BoolParameter clientCutText("ClientCutText",
69 "Send clipboard changes to the server.", true);
70static BoolParameter serverCutText("ServerCutText",
71 "Accept clipboard changes from the server.", true);
72
73static BoolParameter protocol3_3("Protocol3.3",
74 "Only use protocol version 3.3", false);
75
76static IntParameter ptrEventInterval("PointerEventInterval",
77 "The interval to delay between sending one pointer event "
78 "and the next.", 0);
79static BoolParameter emulate3("Emulate3",
80 "Emulate middle mouse button when left and right buttons "
81 "are used simulatenously.", false);
82
83static BoolParameter acceptBell("AcceptBell",
84 "Produce a system beep when requested to by the server.",
85 true);
86
87static StringParameter monitor("Monitor", "The monitor to open the VNC Viewer window on, if available.", "");
88static StringParameter menuKey("MenuKey", "The key which brings up the popup menu", "F8");
89
Peter Åstrand365427a2004-12-29 10:59:03 +000090static BoolParameter customCompressLevel("CustomCompressLevel",
Peter Åstrand55855d52005-01-03 12:01:45 +000091 "Use custom compression level. "
92 "Default if CompressLevel is specified.", false);
Peter Åstrand365427a2004-12-29 10:59:03 +000093
94static IntParameter compressLevel("CompressLevel",
95 "Use specified compression level"
96 "0 = Low, 9 = High",
97 6);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000098
Peter Åstrand0b870262004-12-28 15:55:46 +000099static BoolParameter noJpeg("NoJPEG",
100 "Disable lossy JPEG compression in Tight encoding.",
101 false);
102
Peter Åstrand365427a2004-12-29 10:59:03 +0000103static IntParameter qualityLevel("QualityLevel",
104 "JPEG quality level. "
105 "0 = Low, 9 = High",
106 6);
107
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000108CViewOptions::CViewOptions()
109: useLocalCursor(::useLocalCursor), useDesktopResize(::useDesktopResize),
110autoSelect(::autoSelect), fullColour(::fullColour), fullScreen(::fullScreen),
111shared(::sharedConnection), sendPtrEvents(::sendPtrEvents), sendKeyEvents(::sendKeyEvents),
112preferredEncoding(encodingZRLE), clientCutText(::clientCutText), serverCutText(::serverCutText),
113protocol3_3(::protocol3_3), acceptBell(::acceptBell), lowColourLevel(::lowColourLevel),
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000114pointerEventInterval(ptrEventInterval), emulate3(::emulate3), monitor(::monitor.getData()),
Peter Åstrand365427a2004-12-29 10:59:03 +0000115customCompressLevel(::customCompressLevel), compressLevel(::compressLevel),
116noJpeg(::noJpeg), qualityLevel(::qualityLevel)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000117{
118 CharArray encodingName(::preferredEncoding.getData());
119 preferredEncoding = encodingNum(encodingName.buf);
120 setMenuKey(CharArray(::menuKey.getData()).buf);
Peter Åstrand55855d52005-01-03 12:01:45 +0000121
122 if (!::autoSelect.hasBeenSet()) {
123 // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used
124 autoSelect = (!::preferredEncoding.hasBeenSet()
125 && !::fullColour.hasBeenSet()
126 && !::fullColourAlias.hasBeenSet());
127 }
128 if (!::customCompressLevel.hasBeenSet()) {
129 // Default to CustomCompressLevel=1 if CompressLevel is used.
130 customCompressLevel = ::compressLevel.hasBeenSet();
131 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000132}
133
134
135void CViewOptions::readFromFile(const char* filename) {
136 FILE* f = fopen(filename, "r");
137 if (!f)
138 throw rdr::Exception("Failed to read configuration file");
139
140 try {
141 char line[4096];
142 CharArray section;
143
144 CharArray hostTmp;
145 int portTmp = 0;
146
147 while (!feof(f)) {
148 // Read the next line
149 if (!fgets(line, sizeof(line), f)) {
150 if (feof(f))
151 break;
152 throw rdr::SystemException("fgets", ferror(f));
153 }
154 int len=strlen(line);
155 if (line[len-1] == '\n') {
156 line[len-1] = 0;
157 len--;
158 }
159
160 // Process the line
161 if (line[0] == ';') {
162 // Comment
163 } else if (line[0] == '[') {
164 // Entering a new section
165 if (!strSplit(&line[1], ']', &section.buf, 0))
166 throw rdr::Exception("bad Section");
167 } else {
168 // Reading an option
169 CharArray name;
170 CharArray value;
171 if (!strSplit(line, '=', &name.buf, &value.buf))
172 throw rdr::Exception("bad Name/Value pair");
173
174 if (stricmp(section.buf, "Connection") == 0) {
175 if (stricmp(name.buf, "Host") == 0) {
176 hostTmp.replaceBuf(value.takeBuf());
177 } else if (stricmp(name.buf, "Port") == 0) {
178 portTmp = atoi(value.buf);
179 } else if (stricmp(name.buf, "UserName") == 0) {
180 userName.replaceBuf(value.takeBuf());
181 } else if (stricmp(name.buf, "Password") == 0) {
182 int len = 0;
183 CharArray obfuscated;
184 rdr::HexInStream::hexStrToBin(value.buf, &obfuscated.buf, &len);
185 if (len == 8) {
186 password.replaceBuf(new char[9]);
187 memcpy(password.buf, obfuscated.buf, 8);
188 vncAuthUnobfuscatePasswd(password.buf);
189 password.buf[8] = 0;
190 }
191 }
192 } else if (stricmp(section.buf, "Options") == 0) {
193 // V4 options
194 if (stricmp(name.buf, "UseLocalCursor") == 0) {
195 useLocalCursor = atoi(value.buf);
196 } else if (stricmp(name.buf, "UseDesktopResize") == 0) {
197 useDesktopResize = atoi(value.buf);
198 } else if (stricmp(name.buf, "FullScreen") == 0) {
199 fullScreen = atoi(value.buf);
200 } else if (stricmp(name.buf, "FullColour") == 0) {
201 fullColour = atoi(value.buf);
202 } else if (stricmp(name.buf, "LowColourLevel") == 0) {
203 lowColourLevel = atoi(value.buf);
204 } else if (stricmp(name.buf, "PreferredEncoding") == 0) {
205 preferredEncoding = encodingNum(value.buf);
206 } else if ((stricmp(name.buf, "AutoDetect") == 0) ||
207 (stricmp(name.buf, "AutoSelect") == 0)) {
208 autoSelect = atoi(value.buf);
209 } else if (stricmp(name.buf, "Shared") == 0) {
210 shared = atoi(value.buf);
211 } else if (stricmp(name.buf, "SendPtrEvents") == 0) {
212 sendPtrEvents = atoi(value.buf);
213 } else if (stricmp(name.buf, "SendKeyEvents") == 0) {
214 sendKeyEvents = atoi(value.buf);
215 } else if (stricmp(name.buf, "SendCutText") == 0) {
216 clientCutText = atoi(value.buf);
217 } else if (stricmp(name.buf, "AcceptCutText") == 0) {
218 serverCutText = atoi(value.buf);
219 } else if (stricmp(name.buf, "Emulate3") == 0) {
220 emulate3 = atoi(value.buf);
221 } else if (stricmp(name.buf, "PointerEventInterval") == 0) {
222 pointerEventInterval = atoi(value.buf);
223 } else if (stricmp(name.buf, "Monitor") == 0) {
224 monitor.replaceBuf(value.takeBuf());
225 } else if (stricmp(name.buf, "MenuKey") == 0) {
226 setMenuKey(value.buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000227 } else if (stricmp(name.buf, "CustomCompressLevel") == 0) {
228 customCompressLevel = atoi(value.buf);
229 } else if (stricmp(name.buf, "CompressLevel") == 0) {
230 compressLevel = atoi(value.buf);
Peter Åstrand0b870262004-12-28 15:55:46 +0000231 } else if (stricmp(name.buf, "NoJPEG") == 0) {
232 noJpeg = atoi(value.buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000233 } else if (stricmp(name.buf, "QualityLevel") == 0) {
234 qualityLevel = atoi(value.buf);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000235 // Legacy options
236 } else if (stricmp(name.buf, "Preferred_Encoding") == 0) {
237 preferredEncoding = atoi(value.buf);
238 } else if (stricmp(name.buf, "8bit") == 0) {
239 fullColour = !atoi(value.buf);
240 } else if (stricmp(name.buf, "FullScreen") == 0) {
241 fullScreen = atoi(value.buf);
242 } else if (stricmp(name.buf, "ViewOnly") == 0) {
243 sendPtrEvents = sendKeyEvents = !atoi(value.buf);
244 } else if (stricmp(name.buf, "DisableClipboard") == 0) {
245 clientCutText = serverCutText = !atoi(value.buf);
246 }
247 }
248 }
249 }
250 fclose(f); f=0;
251
252 // Process the Host and Port
253 if (hostTmp.buf) {
254 int hostLen = strlen(hostTmp.buf) + 2 + 17;
255 host.replaceBuf(new char[hostLen]);
256 strCopy(host.buf, hostTmp.buf, hostLen);
257 if (portTmp) {
258 strncat(host.buf, "::", hostLen-1);
259 char tmp[16];
260 sprintf(tmp, "%d", portTmp);
261 strncat(host.buf, tmp, hostLen-1);
262 }
263 }
264
265 setConfigFileName(filename);
266 } catch (rdr::Exception&) {
267 if (f) fclose(f);
268 throw;
269 }
270}
271
272void CViewOptions::writeToFile(const char* filename) {
273 FILE* f = fopen(filename, "w");
274 if (!f)
275 throw rdr::Exception("Failed to write configuration file");
276
277 try {
278 // - Split server into host and port and save
279 fprintf(f, "[Connection]\n");
280
281 fprintf(f, "Host=%s\n", host.buf);
282 if (userName.buf)
283 fprintf(f, "UserName=%s\n", userName.buf);
284 if (password.buf) {
285 // - Warn the user before saving the password
286 if (MsgBox(0, _T("Do you want to include the VNC Password in this configuration file?\n")
287 _T("Storing the password is more convenient but poses a security risk."),
288 MB_YESNO | MB_DEFBUTTON2 | MB_ICONWARNING) == IDYES) {
289 char obfuscated[9];
290 memset(obfuscated, 0, sizeof(obfuscated));
291 strCopy(obfuscated, password.buf, sizeof(obfuscated));
292 vncAuthObfuscatePasswd(obfuscated);
293 CharArray obfuscatedHex = rdr::HexOutStream::binToHexStr(obfuscated, 8);
294 fprintf(f, "Password=%s\n", obfuscatedHex.buf);
295 }
296 }
297
298 // - Save the other options
299 fprintf(f, "[Options]\n");
300
301 fprintf(f, "UseLocalCursor=%d\n", (int)useLocalCursor);
302 fprintf(f, "UseDesktopResize=%d\n", (int)useDesktopResize);
303 fprintf(f, "FullScreen=%d\n", (int)fullScreen);
304 fprintf(f, "FullColour=%d\n", (int)fullColour);
305 fprintf(f, "LowColourLevel=%d\n", lowColourLevel);
306 fprintf(f, "PreferredEncoding=%s\n", encodingName(preferredEncoding));
307 fprintf(f, "AutoSelect=%d\n", (int)autoSelect);
308 fprintf(f, "Shared=%d\n", (int)shared);
309 fprintf(f, "SendPtrEvents=%d\n", (int)sendPtrEvents);
310 fprintf(f, "SendKeyEvents=%d\n", (int)sendKeyEvents);
311 fprintf(f, "SendCutText=%d\n", (int)clientCutText);
312 fprintf(f, "AcceptCutText=%d\n", (int)serverCutText);
313 fprintf(f, "Emulate3=%d\n", (int)emulate3);
314 fprintf(f, "PointerEventInterval=%d\n", pointerEventInterval);
315 if (monitor.buf)
316 fprintf(f, "Monitor=%s\n", monitor.buf);
317 fprintf(f, "MenuKey=%s\n", CharArray(menuKeyName()).buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000318 fprintf(f, "CustomCompressLevel=%d\n", customCompressLevel);
319 fprintf(f, "CompressLevel=%d\n", compressLevel);
Peter Åstrand0b870262004-12-28 15:55:46 +0000320 fprintf(f, "NoJPEG=%d\n", noJpeg);
Peter Åstrand365427a2004-12-29 10:59:03 +0000321 fprintf(f, "QualityLevel=%d\n", qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000322 fclose(f); f=0;
323
324 setConfigFileName(filename);
325 } catch (rdr::Exception&) {
326 if (f) fclose(f);
327 throw;
328 }
329}
330
331
332void CViewOptions::writeDefaults() {
333 RegKey key;
Peter Åstrand9fb4e0e2004-12-30 10:03:00 +0000334 key.createKey(HKEY_CURRENT_USER, _T("Software\\TightVNC\\VNCviewer4"));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000335 key.setBool(_T("UseLocalCursor"), useLocalCursor);
336 key.setBool(_T("UseDesktopResize"), useDesktopResize);
337 key.setBool(_T("FullScreen"), fullScreen);
338 key.setBool(_T("FullColour"), fullColour);
339 key.setInt(_T("LowColourLevel"), lowColourLevel);
340 key.setString(_T("PreferredEncoding"), TStr(encodingName(preferredEncoding)));
341 key.setBool(_T("AutoSelect"), autoSelect);
342 key.setBool(_T("Shared"), shared);
343 key.setBool(_T("SendPointerEvents"), sendPtrEvents);
344 key.setBool(_T("SendKeyEvents"), sendKeyEvents);
345 key.setBool(_T("ClientCutText"), clientCutText);
346 key.setBool(_T("ServerCutText"), serverCutText);
347 key.setBool(_T("Protocol3.3"), protocol3_3);
348 key.setBool(_T("AcceptBell"), acceptBell);
349 key.setBool(_T("Emulate3"), emulate3);
350 key.setInt(_T("PointerEventInterval"), pointerEventInterval);
351 if (monitor.buf)
352 key.setString(_T("Monitor"), TStr(monitor.buf));
353 key.setString(_T("MenuKey"), TCharArray(menuKeyName()).buf);
Peter Åstrand365427a2004-12-29 10:59:03 +0000354 key.setInt(_T("CustomCompressLevel"), customCompressLevel);
355 key.setInt(_T("CompressLevel"), compressLevel);
Peter Åstrand0b870262004-12-28 15:55:46 +0000356 key.setInt(_T("NoJPEG"), noJpeg);
Peter Åstrand365427a2004-12-29 10:59:03 +0000357 key.setInt(_T("QualityLevel"), qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000358}
359
360
361void CViewOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));}
362void CViewOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));}
363void CViewOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));}
364void CViewOptions::setHost(const char* h) {host.replaceBuf(strDup(h));}
365void CViewOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));}
366
367void CViewOptions::setMenuKey(const char* keyName) {
368 if (!keyName[0]) {
369 menuKey = 0;
370 } else {
371 menuKey = VK_F8;
372 if (keyName[0] == 'F') {
373 UINT fKey = atoi(&keyName[1]);
374 if (fKey >= 1 && fKey <= 12)
375 menuKey = fKey-1 + VK_F1;
376 }
377 }
378}
379char* CViewOptions::menuKeyName() {
380 int fNum = (menuKey-VK_F1)+1;
381 if (fNum<1 || fNum>12)
382 return strDup("");
383 CharArray menuKeyStr(4);
384 sprintf(menuKeyStr.buf, "F%d", fNum);
385 return menuKeyStr.takeBuf();
386}
387
388
389CViewOptions& CViewOptions::operator=(const CViewOptions& o) {
390 useLocalCursor = o.useLocalCursor;
391 useDesktopResize = o.useDesktopResize;
392 fullScreen = o.fullScreen;
393 fullColour = o.fullColour;
394 lowColourLevel = o.lowColourLevel;
395 preferredEncoding = o.preferredEncoding;
396 autoSelect = o.autoSelect;
397 shared = o.shared;
398 sendPtrEvents = o.sendPtrEvents;
399 sendKeyEvents = o.sendKeyEvents;
400 clientCutText = o.clientCutText;
401 serverCutText = o.serverCutText;
402 emulate3 = o.emulate3;
403 pointerEventInterval = o.pointerEventInterval;
404 protocol3_3 = o.protocol3_3;
405 acceptBell = o.acceptBell;
406 setUserName(o.userName.buf);
407 setPassword(o.password.buf);
408 setConfigFileName(o.configFileName.buf);
409 setHost(o.host.buf);
410 setMonitor(o.monitor.buf);
411 menuKey = o.menuKey;
Peter Åstrand365427a2004-12-29 10:59:03 +0000412 customCompressLevel = o.customCompressLevel;
413 compressLevel = o.compressLevel;
Peter Åstrand0b870262004-12-28 15:55:46 +0000414 noJpeg = o.noJpeg;
Peter Åstrand365427a2004-12-29 10:59:03 +0000415 qualityLevel = o.qualityLevel;
416
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000417 return *this;
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000418}