Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* 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 | |
| 30 | using namespace rfb; |
| 31 | using namespace rfb::win32; |
| 32 | |
| 33 | |
| 34 | static BoolParameter useLocalCursor("UseLocalCursor", "Render the mouse cursor locally", true); |
| 35 | static BoolParameter useDesktopResize("UseDesktopResize", "Support dynamic desktop resizing", true); |
| 36 | |
| 37 | static 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); |
| 41 | static 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); |
| 45 | static BoolParameter fullScreen("FullScreen", |
| 46 | "Use the whole display to show the remote desktop." |
| 47 | "(Press F8 to access the viewer menu)", |
| 48 | false); |
| 49 | static StringParameter preferredEncoding("PreferredEncoding", |
| 50 | "Preferred graphical encoding to use - overridden by AutoSelect if set. " |
| 51 | "(ZRLE, Hextile or Raw)", "ZRLE"); |
| 52 | |
| 53 | static BoolParameter autoSelect("AutoSelect", "Auto select pixel format and encoding", true); |
| 54 | static BoolParameter sharedConnection("Shared", |
| 55 | "Allow existing connections to the server to continue." |
| 56 | "(Default is to disconnect all other clients)", |
| 57 | false); |
| 58 | |
| 59 | static BoolParameter sendPtrEvents("SendPointerEvents", |
| 60 | "Send pointer (mouse) events to the server.", true); |
| 61 | static BoolParameter sendKeyEvents("SendKeyEvents", |
| 62 | "Send key presses (and releases) to the server.", true); |
| 63 | |
| 64 | static BoolParameter clientCutText("ClientCutText", |
| 65 | "Send clipboard changes to the server.", true); |
| 66 | static BoolParameter serverCutText("ServerCutText", |
| 67 | "Accept clipboard changes from the server.", true); |
| 68 | |
| 69 | static BoolParameter protocol3_3("Protocol3.3", |
| 70 | "Only use protocol version 3.3", false); |
| 71 | |
| 72 | static IntParameter ptrEventInterval("PointerEventInterval", |
| 73 | "The interval to delay between sending one pointer event " |
| 74 | "and the next.", 0); |
| 75 | static BoolParameter emulate3("Emulate3", |
| 76 | "Emulate middle mouse button when left and right buttons " |
| 77 | "are used simulatenously.", false); |
| 78 | |
| 79 | static BoolParameter acceptBell("AcceptBell", |
| 80 | "Produce a system beep when requested to by the server.", |
| 81 | true); |
| 82 | |
| 83 | static StringParameter monitor("Monitor", "The monitor to open the VNC Viewer window on, if available.", ""); |
| 84 | static StringParameter menuKey("MenuKey", "The key which brings up the popup menu", "F8"); |
| 85 | |
Peter Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 86 | static IntParameter qualityLevel("QualityLevel", |
| 87 | "JPEG quality level. " |
| 88 | "0 = Low, 9 = High", |
| 89 | 5); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 90 | |
| 91 | CViewOptions::CViewOptions() |
| 92 | : useLocalCursor(::useLocalCursor), useDesktopResize(::useDesktopResize), |
| 93 | autoSelect(::autoSelect), fullColour(::fullColour), fullScreen(::fullScreen), |
| 94 | shared(::sharedConnection), sendPtrEvents(::sendPtrEvents), sendKeyEvents(::sendKeyEvents), |
| 95 | preferredEncoding(encodingZRLE), clientCutText(::clientCutText), serverCutText(::serverCutText), |
| 96 | protocol3_3(::protocol3_3), acceptBell(::acceptBell), lowColourLevel(::lowColourLevel), |
Peter Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 97 | pointerEventInterval(ptrEventInterval), emulate3(::emulate3), monitor(::monitor.getData()), |
| 98 | qualityLevel(::qualityLevel) |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 99 | { |
| 100 | CharArray encodingName(::preferredEncoding.getData()); |
| 101 | preferredEncoding = encodingNum(encodingName.buf); |
| 102 | setMenuKey(CharArray(::menuKey.getData()).buf); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void 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], ']', §ion.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 Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 198 | } else if (stricmp(name.buf, "QualityLevel") == 0) { |
| 199 | qualityLevel = atoi(value.buf); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 200 | |
| 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 | |
| 238 | void 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 Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 284 | fprintf(f, "QualityLevel=%d\n", qualityLevel); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 285 | fclose(f); f=0; |
| 286 | |
| 287 | setConfigFileName(filename); |
| 288 | } catch (rdr::Exception&) { |
| 289 | if (f) fclose(f); |
| 290 | throw; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | void 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 Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 317 | key.setInt(_T("QualityLevel"), qualityLevel); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
| 321 | void CViewOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));} |
| 322 | void CViewOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));} |
| 323 | void CViewOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));} |
| 324 | void CViewOptions::setHost(const char* h) {host.replaceBuf(strDup(h));} |
| 325 | void CViewOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));} |
| 326 | |
| 327 | void 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 | } |
| 339 | char* 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 | |
| 349 | CViewOptions& 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 Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 372 | qualityLevel = o.qualityLevel; |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 373 | return *this; |
Peter Åstrand | ed9d4ae | 2004-12-07 11:59:14 +0000 | [diff] [blame^] | 374 | } |