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