aboutsummaryrefslogtreecommitdiffstats
path: root/vncviewer/CViewOptions.cxx
blob: d4c647a1b76e9fe7730a9565ad80eef7d6b794d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Copyright (C) 2002-2004 RealVNC Ltd.  All Rights Reserved.
 *    
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#include <vncviewer/CViewOptions.h>
#include <rfb/Configuration.h>
#include <rfb/encodings.h>
#include <rfb/vncAuth.h>
#include <rfb/LogWriter.h>
#include <rfb_win32/Win32Util.h>
#include <rfb_win32/Registry.h>
#include <rdr/HexInStream.h>
#include <rdr/HexOutStream.h>
#include <stdlib.h>

using namespace rfb;
using namespace rfb::win32;


static BoolParameter useLocalCursor("UseLocalCursor", "Render the mouse cursor locally", true);
static BoolParameter useDesktopResize("UseDesktopResize", "Support dynamic desktop resizing", true);

static BoolParameter fullColour("FullColour",
				"Use full colour", true);
static IntParameter lowColourLevel("LowColourLevel",
                         "Colour level to use on slow connections. "
                         "0 = Very Low (8 colours), 1 = Low (64 colours), 2 = Medium (256 colours)",
                         2);
static BoolParameter fullScreen("FullScreen",
                         "Use the whole display to show the remote desktop."
                         "(Press F8 to access the viewer menu)",
                         false);
static StringParameter preferredEncoding("PreferredEncoding",
                         "Preferred graphical encoding to use - overridden by AutoSelect if set. "
                         "(ZRLE, Hextile or Raw)", "ZRLE");

static BoolParameter autoSelect("AutoSelect", "Auto select pixel format and encoding", true);
static BoolParameter sharedConnection("Shared",
                         "Allow existing connections to the server to continue."
                         "(Default is to disconnect all other clients)",
                         false);

static BoolParameter sendPtrEvents("SendPointerEvents",
                         "Send pointer (mouse) events to the server.", true);
static BoolParameter sendKeyEvents("SendKeyEvents",
                         "Send key presses (and releases) to the server.", true);

static BoolParameter clientCutText("ClientCutText",
                         "Send clipboard changes to the server.", true);
static BoolParameter serverCutText("ServerCutText",
                         "Accept clipboard changes from the server.", true);

static BoolParameter protocol3_3("Protocol3.3",
                         "Only use protocol version 3.3", false);

static IntParameter ptrEventInterval("PointerEventInterval",
                         "The interval to delay between sending one pointer event "
                         "and the next.", 0);
static BoolParameter emulate3("Emulate3",
                         "Emulate middle mouse button when left and right buttons "
                         "are used simulatenously.", false);

static BoolParameter acceptBell("AcceptBell",
                         "Produce a system beep when requested to by the server.",
                         true);

static StringParameter monitor("Monitor", "The monitor to open the VNC Viewer window on, if available.", "");
static StringParameter menuKey("MenuKey", "The key which brings up the popup menu", "F8");

static IntParameter qualityLevel("QualityLevel",
				 "JPEG quality level. "
				 "0 = Low, 9 = High",
				 5);

CViewOptions::CViewOptions()
: useLocalCursor(::useLocalCursor), useDesktopResize(::useDesktopResize),
autoSelect(::autoSelect), fullColour(::fullColour), fullScreen(::fullScreen),
shared(::sharedConnection), sendPtrEvents(::sendPtrEvents), sendKeyEvents(::sendKeyEvents),
preferredEncoding(encodingZRLE), clientCutText(::clientCutText), serverCutText(::serverCutText),
protocol3_3(::protocol3_3), acceptBell(::acceptBell), lowColourLevel(::lowColourLevel),
pointerEventInterval(ptrEventInterval), emulate3(::emulate3), monitor(::monitor.getData()),
qualityLevel(::qualityLevel)
{
  CharArray encodingName(::preferredEncoding.getData());
  preferredEncoding = encodingNum(encodingName.buf);
  setMenuKey(CharArray(::menuKey.getData()).buf);
}


void CViewOptions::readFromFile(const char* filename) {
  FILE* f = fopen(filename, "r");
  if (!f)
    throw rdr::Exception("Failed to read configuration file");

  try { 
    char line[4096];
    CharArray section;

    CharArray hostTmp;
    int portTmp = 0;

    while (!feof(f)) {
      // Read the next line
      if (!fgets(line, sizeof(line), f)) {
        if (feof(f))
          break;
        throw rdr::SystemException("fgets", ferror(f));
      }
      int len=strlen(line);
      if (line[len-1] == '\n') {
        line[len-1] = 0;
        len--;
      }

      // Process the line
      if (line[0] == ';') {
        // Comment
      } else if (line[0] == '[') {
        // Entering a new section
        if (!strSplit(&line[1], ']', &section.buf, 0))
          throw rdr::Exception("bad Section");
      } else {
        // Reading an option
        CharArray name;
        CharArray value;
        if (!strSplit(line, '=', &name.buf, &value.buf))
          throw rdr::Exception("bad Name/Value pair");

        if (stricmp(section.buf, "Connection") == 0) {
          if (stricmp(name.buf, "Host") == 0) {
            hostTmp.replaceBuf(value.takeBuf());
          } else if (stricmp(name.buf, "Port") == 0) {
            portTmp = atoi(value.buf);
          } else if (stricmp(name.buf, "UserName") == 0) {
            userName.replaceBuf(value.takeBuf());
          } else if (stricmp(name.buf, "Password") == 0) {
            int len = 0;
            CharArray obfuscated;
            rdr::HexInStream::hexStrToBin(value.buf, &obfuscated.buf, &len);
            if (len == 8) {
              password.replaceBuf(new char[9]);
              memcpy(password.buf, obfuscated.buf, 8);
              vncAuthUnobfuscatePasswd(password.buf);
              password.buf[8] = 0;
            }
          }
        } else if (stricmp(section.buf, "Options") == 0) {
            // V4 options
          if (stricmp(name.buf, "UseLocalCursor") == 0) {
            useLocalCursor = atoi(value.buf);
          } else if (stricmp(name.buf, "UseDesktopResize") == 0) {
            useDesktopResize = atoi(value.buf);
          } else if (stricmp(name.buf, "FullScreen") == 0) {
            fullScreen = atoi(value.buf);
          } else if (stricmp(name.buf, "FullColour") == 0) {
            fullColour = atoi(value.buf);
          } else if (stricmp(name.buf, "LowColourLevel") == 0) {
            lowColourLevel = atoi(value.buf);
          } else if (stricmp(name.buf, "PreferredEncoding") == 0) {
            preferredEncoding = encodingNum(value.buf);
          } else if ((stricmp(name.buf, "AutoDetect") == 0) ||
                     (stricmp(name.buf, "AutoSelect") == 0)) {
            autoSelect = atoi(value.buf);
          } else if (stricmp(name.buf, "Shared") == 0) {
            shared = atoi(value.buf);
          } else if (stricmp(name.buf, "SendPtrEvents") == 0) {
            sendPtrEvents = atoi(value.buf);
          } else if (stricmp(name.buf, "SendKeyEvents") == 0) {
            sendKeyEvents = atoi(value.buf);
          } else if (stricmp(name.buf, "SendCutText") == 0) {
            clientCutText = atoi(value.buf);
          } else if (stricmp(name.buf, "AcceptCutText") == 0) {
            serverCutText = atoi(value.buf);
          } else if (stricmp(name.buf, "Emulate3") == 0) {
            emulate3 = atoi(value.buf);
          } else if (stricmp(name.buf, "PointerEventInterval") == 0) {
            pointerEventInterval = atoi(value.buf);
          } else if (stricmp(name.buf, "Monitor") == 0) {
            monitor.replaceBuf(value.takeBuf());
          } else if (stricmp(name.buf, "MenuKey") == 0) {
            setMenuKey(value.buf);
          } else if (stricmp(name.buf, "QualityLevel") == 0) {
	    qualityLevel = atoi(value.buf);

            // Legacy options
          } else if (stricmp(name.buf, "Preferred_Encoding") == 0) {
            preferredEncoding = atoi(value.buf);
          } else if (stricmp(name.buf, "8bit") == 0) {
            fullColour = !atoi(value.buf);
          } else if (stricmp(name.buf, "FullScreen") == 0) {
            fullScreen = atoi(value.buf);
          } else if (stricmp(name.buf, "ViewOnly") == 0) {
            sendPtrEvents = sendKeyEvents = !atoi(value.buf);
          } else if (stricmp(name.buf, "DisableClipboard") == 0) {
            clientCutText = serverCutText = !atoi(value.buf);
          }
        }
      }
    }
    fclose(f); f=0;

    // Process the Host and Port
    if (hostTmp.buf) {
      int hostLen = strlen(hostTmp.buf) + 2 + 17;
      host.replaceBuf(new char[hostLen]);
      strCopy(host.buf, hostTmp.buf, hostLen);
      if (portTmp) {
        strncat(host.buf, "::", hostLen-1);
        char tmp[16];
        sprintf(tmp, "%d", portTmp);
        strncat(host.buf, tmp, hostLen-1);
      }
    }

    setConfigFileName(filename);
  } catch (rdr::Exception&) {
    if (f) fclose(f);
    throw;
  }
}

void CViewOptions::writeToFile(const char* filename) {
  FILE* f = fopen(filename, "w");
  if (!f)
    throw rdr::Exception("Failed to write configuration file");

  try {
    // - Split server into host and port and save
    fprintf(f, "[Connection]\n");

    fprintf(f, "Host=%s\n", host.buf);
    if (userName.buf)
      fprintf(f, "UserName=%s\n", userName.buf);
    if (password.buf) {
      // - Warn the user before saving the password
      if (MsgBox(0, _T("Do you want to include the VNC Password in this configuration file?\n")
                    _T("Storing the password is more convenient but poses a security risk."),
                    MB_YESNO | MB_DEFBUTTON2 | MB_ICONWARNING) == IDYES) {
        char obfuscated[9];
        memset(obfuscated, 0, sizeof(obfuscated));
        strCopy(obfuscated, password.buf, sizeof(obfuscated));
        vncAuthObfuscatePasswd(obfuscated);
        CharArray obfuscatedHex = rdr::HexOutStream::binToHexStr(obfuscated, 8);
        fprintf(f, "Password=%s\n", obfuscatedHex.buf);
      }
    }

    // - Save the other options
    fprintf(f, "[Options]\n");

    fprintf(f, "UseLocalCursor=%d\n", (int)useLocalCursor);
    fprintf(f, "UseDesktopResize=%d\n", (int)useDesktopResize);
    fprintf(f, "FullScreen=%d\n", (int)fullScreen);
    fprintf(f, "FullColour=%d\n", (int)fullColour);
    fprintf(f, "LowColourLevel=%d\n", lowColourLevel);
    fprintf(f, "PreferredEncoding=%s\n", encodingName(preferredEncoding));
    fprintf(f, "AutoSelect=%d\n", (int)autoSelect);
    fprintf(f, "Shared=%d\n", (int)shared);
    fprintf(f, "SendPtrEvents=%d\n", (int)sendPtrEvents);
    fprintf(f, "SendKeyEvents=%d\n", (int)sendKeyEvents);
    fprintf(f, "SendCutText=%d\n", (int)clientCutText);
    fprintf(f, "AcceptCutText=%d\n", (int)serverCutText);
    fprintf(f, "Emulate3=%d\n", (int)emulate3);
    fprintf(f, "PointerEventInterval=%d\n", pointerEventInterval);
    if (monitor.buf)
      fprintf(f, "Monitor=%s\n", monitor.buf);
    fprintf(f, "MenuKey=%s\n", CharArray(menuKeyName()).buf);
    fprintf(f, "QualityLevel=%d\n", qualityLevel);
    fclose(f); f=0;

    setConfigFileName(filename);
  } catch (rdr::Exception&) {
    if (f) fclose(f);
    throw;
  }
}


void CViewOptions::writeDefaults() {
  RegKey key;
  key.createKey(HKEY_CURRENT_USER, _T("Software\\RealVNC\\VNCviewer4"));
  key.setBool(_T("UseLocalCursor"), useLocalCursor);
  key.setBool(_T("UseDesktopResize"), useDesktopResize);
  key.setBool(_T("FullScreen"), fullScreen);
  key.setBool(_T("FullColour"), fullColour);
  key.setInt(_T("LowColourLevel"), lowColourLevel);
  key.setString(_T("PreferredEncoding"), TStr(encodingName(preferredEncoding)));
  key.setBool(_T("AutoSelect"), autoSelect);
  key.setBool(_T("Shared"), shared);
  key.setBool(_T("SendPointerEvents"), sendPtrEvents);
  key.setBool(_T("SendKeyEvents"), sendKeyEvents);
  key.setBool(_T("ClientCutText"), clientCutText);
  key.setBool(_T("ServerCutText"), serverCutText);
  key.setBool(_T("Protocol3.3"), protocol3_3);
  key.setBool(_T("AcceptBell"), acceptBell);
  key.setBool(_T("Emulate3"), emulate3);
  key.setInt(_T("PointerEventInterval"), pointerEventInterval);
  if (monitor.buf)
    key.setString(_T("Monitor"), TStr(monitor.buf));
  key.setString(_T("MenuKey"), TCharArray(menuKeyName()).buf);
  key.setInt(_T("QualityLevel"), qualityLevel);
}


void CViewOptions::setUserName(const char* user) {userName.replaceBuf(strDup(user));}
void CViewOptions::setPassword(const char* pwd) {password.replaceBuf(strDup(pwd));}
void CViewOptions::setConfigFileName(const char* cfn) {configFileName.replaceBuf(strDup(cfn));}
void CViewOptions::setHost(const char* h) {host.replaceBuf(strDup(h));}
void CViewOptions::setMonitor(const char* m) {monitor.replaceBuf(strDup(m));}

void CViewOptions::setMenuKey(const char* keyName) {
  if (!keyName[0]) {
    menuKey = 0;
  } else {
    menuKey = VK_F8;
    if (keyName[0] == 'F') {
      UINT fKey = atoi(&keyName[1]);
      if (fKey >= 1 && fKey <= 12)
        menuKey = fKey-1 + VK_F1;
    }
  }
}
char* CViewOptions::menuKeyName() {
  int fNum = (menuKey-VK_F1)+1;
  if (fNum<1 || fNum>12)
    return strDup("");
  CharArray menuKeyStr(4);
  sprintf(menuKeyStr.buf, "F%d", fNum);
  return menuKeyStr.takeBuf();
}


CViewOptions& CViewOptions::operator=(const CViewOptions& o) {
  useLocalCursor = o.useLocalCursor;
  useDesktopResize = o.useDesktopResize;
  fullScreen = o.fullScreen;
  fullColour = o.fullColour;
  lowColourLevel = o.lowColourLevel;
  preferredEncoding = o.preferredEncoding;
  autoSelect = o.autoSelect;
  shared = o.shared;
  sendPtrEvents = o.sendPtrEvents;
  sendKeyEvents = o.sendKeyEvents;
  clientCutText = o.clientCutText;
  serverCutText = o.serverCutText;
  emulate3 = o.emulate3;
  pointerEventInterval = o.pointerEventInterval;
  protocol3_3 = o.protocol3_3;
  acceptBell = o.acceptBell;
  setUserName(o.userName.buf);
  setPassword(o.password.buf);
  setConfigFileName(o.configFileName.buf);
  setHost(o.host.buf);
  setMonitor(o.monitor.buf);
  menuKey = o.menuKey;
  qualityLevel = o.qualityLevel;
  return *this;
}