blob: d4c647a1b76e9fe7730a9565ad80eef7d6b794d7 [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 Åstranded9d4ae2004-12-07 11:59:14 +000084static IntParameter qualityLevel("QualityLevel",
85 "JPEG quality level. "
86 "0 = Low, 9 = High",
87 5);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000088
89CViewOptions::CViewOptions()
90: useLocalCursor(::useLocalCursor), useDesktopResize(::useDesktopResize),
91autoSelect(::autoSelect), fullColour(::fullColour), fullScreen(::fullScreen),
92shared(::sharedConnection), sendPtrEvents(::sendPtrEvents), sendKeyEvents(::sendKeyEvents),
93preferredEncoding(encodingZRLE), clientCutText(::clientCutText), serverCutText(::serverCutText),
94protocol3_3(::protocol3_3), acceptBell(::acceptBell), lowColourLevel(::lowColourLevel),
Peter Åstranded9d4ae2004-12-07 11:59:14 +000095pointerEventInterval(ptrEventInterval), emulate3(::emulate3), monitor(::monitor.getData()),
96qualityLevel(::qualityLevel)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000097{
98 CharArray encodingName(::preferredEncoding.getData());
99 preferredEncoding = encodingNum(encodingName.buf);
100 setMenuKey(CharArray(::menuKey.getData()).buf);
101}
102
103
104void CViewOptions::readFromFile(const char* filename) {
105 FILE* f = fopen(filename, "r");
106 if (!f)
107 throw rdr::Exception("Failed to read configuration file");
108
109 try {
110 char line[4096];
111 CharArray section;
112
113 CharArray hostTmp;
114 int portTmp = 0;
115
116 while (!feof(f)) {
117 // Read the next line
118 if (!fgets(line, sizeof(line), f)) {
119 if (feof(f))
120 break;
121 throw rdr::SystemException("fgets", ferror(f));
122 }
123 int len=strlen(line);
124 if (line[len-1] == '\n') {
125 line[len-1] = 0;
126 len--;
127 }
128
129 // Process the line
130 if (line[0] == ';') {
131 // Comment
132 } else if (line[0] == '[') {
133 // Entering a new section
134 if (!strSplit(&line[1], ']', &section.buf, 0))
135 throw rdr::Exception("bad Section");
136 } else {
137 // Reading an option
138 CharArray name;
139 CharArray value;
140 if (!strSplit(line, '=', &name.buf, &value.buf))
141 throw rdr::Exception("bad Name/Value pair");
142
143 if (stricmp(section.buf, "Connection") == 0) {
144 if (stricmp(name.buf, "Host") == 0) {
145 hostTmp.replaceBuf(value.takeBuf());
146 } else if (stricmp(name.buf, "Port") == 0) {
147 portTmp = atoi(value.buf);
148 } else if (stricmp(name.buf, "UserName") == 0) {
149 userName.replaceBuf(value.takeBuf());
150 } else if (stricmp(name.buf, "Password") == 0) {
151 int len = 0;
152 CharArray obfuscated;
153 rdr::HexInStream::hexStrToBin(value.buf, &obfuscated.buf, &len);
154 if (len == 8) {
155 password.replaceBuf(new char[9]);
156 memcpy(password.buf, obfuscated.buf, 8);
157 vncAuthUnobfuscatePasswd(password.buf);
158 password.buf[8] = 0;
159 }
160 }
161 } else if (stricmp(section.buf, "Options") == 0) {
162 // V4 options
163 if (stricmp(name.buf, "UseLocalCursor") == 0) {
164 useLocalCursor = atoi(value.buf);
165 } else if (stricmp(name.buf, "UseDesktopResize") == 0) {
166 useDesktopResize = atoi(value.buf);
167 } else if (stricmp(name.buf, "FullScreen") == 0) {
168 fullScreen = atoi(value.buf);
169 } else if (stricmp(name.buf, "FullColour") == 0) {
170 fullColour = atoi(value.buf);
171 } else if (stricmp(name.buf, "LowColourLevel") == 0) {
172 lowColourLevel = atoi(value.buf);
173 } else if (stricmp(name.buf, "PreferredEncoding") == 0) {
174 preferredEncoding = encodingNum(value.buf);
175 } else if ((stricmp(name.buf, "AutoDetect") == 0) ||
176 (stricmp(name.buf, "AutoSelect") == 0)) {
177 autoSelect = atoi(value.buf);
178 } else if (stricmp(name.buf, "Shared") == 0) {
179 shared = atoi(value.buf);
180 } else if (stricmp(name.buf, "SendPtrEvents") == 0) {
181 sendPtrEvents = atoi(value.buf);
182 } else if (stricmp(name.buf, "SendKeyEvents") == 0) {
183 sendKeyEvents = atoi(value.buf);
184 } else if (stricmp(name.buf, "SendCutText") == 0) {
185 clientCutText = atoi(value.buf);
186 } else if (stricmp(name.buf, "AcceptCutText") == 0) {
187 serverCutText = atoi(value.buf);
188 } else if (stricmp(name.buf, "Emulate3") == 0) {
189 emulate3 = atoi(value.buf);
190 } else if (stricmp(name.buf, "PointerEventInterval") == 0) {
191 pointerEventInterval = atoi(value.buf);
192 } else if (stricmp(name.buf, "Monitor") == 0) {
193 monitor.replaceBuf(value.takeBuf());
194 } else if (stricmp(name.buf, "MenuKey") == 0) {
195 setMenuKey(value.buf);
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000196 } else if (stricmp(name.buf, "QualityLevel") == 0) {
197 qualityLevel = atoi(value.buf);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000198
199 // Legacy options
200 } else if (stricmp(name.buf, "Preferred_Encoding") == 0) {
201 preferredEncoding = atoi(value.buf);
202 } else if (stricmp(name.buf, "8bit") == 0) {
203 fullColour = !atoi(value.buf);
204 } else if (stricmp(name.buf, "FullScreen") == 0) {
205 fullScreen = atoi(value.buf);
206 } else if (stricmp(name.buf, "ViewOnly") == 0) {
207 sendPtrEvents = sendKeyEvents = !atoi(value.buf);
208 } else if (stricmp(name.buf, "DisableClipboard") == 0) {
209 clientCutText = serverCutText = !atoi(value.buf);
210 }
211 }
212 }
213 }
214 fclose(f); f=0;
215
216 // Process the Host and Port
217 if (hostTmp.buf) {
218 int hostLen = strlen(hostTmp.buf) + 2 + 17;
219 host.replaceBuf(new char[hostLen]);
220 strCopy(host.buf, hostTmp.buf, hostLen);
221 if (portTmp) {
222 strncat(host.buf, "::", hostLen-1);
223 char tmp[16];
224 sprintf(tmp, "%d", portTmp);
225 strncat(host.buf, tmp, hostLen-1);
226 }
227 }
228
229 setConfigFileName(filename);
230 } catch (rdr::Exception&) {
231 if (f) fclose(f);
232 throw;
233 }
234}
235
236void CViewOptions::writeToFile(const char* filename) {
237 FILE* f = fopen(filename, "w");
238 if (!f)
239 throw rdr::Exception("Failed to write configuration file");
240
241 try {
242 // - Split server into host and port and save
243 fprintf(f, "[Connection]\n");
244
245 fprintf(f, "Host=%s\n", host.buf);
246 if (userName.buf)
247 fprintf(f, "UserName=%s\n", userName.buf);
248 if (password.buf) {
249 // - Warn the user before saving the password
250 if (MsgBox(0, _T("Do you want to include the VNC Password in this configuration file?\n")
251 _T("Storing the password is more convenient but poses a security risk."),
252 MB_YESNO | MB_DEFBUTTON2 | MB_ICONWARNING) == IDYES) {
253 char obfuscated[9];
254 memset(obfuscated, 0, sizeof(obfuscated));
255 strCopy(obfuscated, password.buf, sizeof(obfuscated));
256 vncAuthObfuscatePasswd(obfuscated);
257 CharArray obfuscatedHex = rdr::HexOutStream::binToHexStr(obfuscated, 8);
258 fprintf(f, "Password=%s\n", obfuscatedHex.buf);
259 }
260 }
261
262 // - Save the other options
263 fprintf(f, "[Options]\n");
264
265 fprintf(f, "UseLocalCursor=%d\n", (int)useLocalCursor);
266 fprintf(f, "UseDesktopResize=%d\n", (int)useDesktopResize);
267 fprintf(f, "FullScreen=%d\n", (int)fullScreen);
268 fprintf(f, "FullColour=%d\n", (int)fullColour);
269 fprintf(f, "LowColourLevel=%d\n", lowColourLevel);
270 fprintf(f, "PreferredEncoding=%s\n", encodingName(preferredEncoding));
271 fprintf(f, "AutoSelect=%d\n", (int)autoSelect);
272 fprintf(f, "Shared=%d\n", (int)shared);
273 fprintf(f, "SendPtrEvents=%d\n", (int)sendPtrEvents);
274 fprintf(f, "SendKeyEvents=%d\n", (int)sendKeyEvents);
275 fprintf(f, "SendCutText=%d\n", (int)clientCutText);
276 fprintf(f, "AcceptCutText=%d\n", (int)serverCutText);
277 fprintf(f, "Emulate3=%d\n", (int)emulate3);
278 fprintf(f, "PointerEventInterval=%d\n", pointerEventInterval);
279 if (monitor.buf)
280 fprintf(f, "Monitor=%s\n", monitor.buf);
281 fprintf(f, "MenuKey=%s\n", CharArray(menuKeyName()).buf);
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000282 fprintf(f, "QualityLevel=%d\n", qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000283 fclose(f); f=0;
284
285 setConfigFileName(filename);
286 } catch (rdr::Exception&) {
287 if (f) fclose(f);
288 throw;
289 }
290}
291
292
293void CViewOptions::writeDefaults() {
294 RegKey key;
295 key.createKey(HKEY_CURRENT_USER, _T("Software\\RealVNC\\VNCviewer4"));
296 key.setBool(_T("UseLocalCursor"), useLocalCursor);
297 key.setBool(_T("UseDesktopResize"), useDesktopResize);
298 key.setBool(_T("FullScreen"), fullScreen);
299 key.setBool(_T("FullColour"), fullColour);
300 key.setInt(_T("LowColourLevel"), lowColourLevel);
301 key.setString(_T("PreferredEncoding"), TStr(encodingName(preferredEncoding)));
302 key.setBool(_T("AutoSelect"), autoSelect);
303 key.setBool(_T("Shared"), shared);
304 key.setBool(_T("SendPointerEvents"), sendPtrEvents);
305 key.setBool(_T("SendKeyEvents"), sendKeyEvents);
306 key.setBool(_T("ClientCutText"), clientCutText);
307 key.setBool(_T("ServerCutText"), serverCutText);
308 key.setBool(_T("Protocol3.3"), protocol3_3);
309 key.setBool(_T("AcceptBell"), acceptBell);
310 key.setBool(_T("Emulate3"), emulate3);
311 key.setInt(_T("PointerEventInterval"), pointerEventInterval);
312 if (monitor.buf)
313 key.setString(_T("Monitor"), TStr(monitor.buf));
314 key.setString(_T("MenuKey"), TCharArray(menuKeyName()).buf);
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000315 key.setInt(_T("QualityLevel"), qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000316}
317
318
319void CViewOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));}
320void CViewOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));}
321void CViewOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));}
322void CViewOptions::setHost(const char* h) {host.replaceBuf(strDup(h));}
323void CViewOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));}
324
325void CViewOptions::setMenuKey(const char* keyName) {
326 if (!keyName[0]) {
327 menuKey = 0;
328 } else {
329 menuKey = VK_F8;
330 if (keyName[0] == 'F') {
331 UINT fKey = atoi(&keyName[1]);
332 if (fKey >= 1 && fKey <= 12)
333 menuKey = fKey-1 + VK_F1;
334 }
335 }
336}
337char* CViewOptions::menuKeyName() {
338 int fNum = (menuKey-VK_F1)+1;
339 if (fNum<1 || fNum>12)
340 return strDup("");
341 CharArray menuKeyStr(4);
342 sprintf(menuKeyStr.buf, "F%d", fNum);
343 return menuKeyStr.takeBuf();
344}
345
346
347CViewOptions& CViewOptions::operator=(const CViewOptions& o) {
348 useLocalCursor = o.useLocalCursor;
349 useDesktopResize = o.useDesktopResize;
350 fullScreen = o.fullScreen;
351 fullColour = o.fullColour;
352 lowColourLevel = o.lowColourLevel;
353 preferredEncoding = o.preferredEncoding;
354 autoSelect = o.autoSelect;
355 shared = o.shared;
356 sendPtrEvents = o.sendPtrEvents;
357 sendKeyEvents = o.sendKeyEvents;
358 clientCutText = o.clientCutText;
359 serverCutText = o.serverCutText;
360 emulate3 = o.emulate3;
361 pointerEventInterval = o.pointerEventInterval;
362 protocol3_3 = o.protocol3_3;
363 acceptBell = o.acceptBell;
364 setUserName(o.userName.buf);
365 setPassword(o.password.buf);
366 setConfigFileName(o.configFileName.buf);
367 setHost(o.host.buf);
368 setMonitor(o.monitor.buf);
369 menuKey = o.menuKey;
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000370 qualityLevel = o.qualityLevel;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000371 return *this;
Peter Åstranded9d4ae2004-12-07 11:59:14 +0000372}