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