0
|
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
|
|
2 // All rights reserved.
|
|
3 //
|
|
4 //-------------------------------------------------------------------------
|
|
5 // This file is part of the TightVNC software. Please visit our Web site:
|
|
6 //
|
|
7 // http://www.tightvnc.com/
|
|
8 //
|
|
9 // This program is free software; you can redistribute it and/or modify
|
|
10 // it under the terms of the GNU General Public License as published by
|
|
11 // the Free Software Foundation; either version 2 of the License, or
|
|
12 // (at your option) any later version.
|
|
13 //
|
|
14 // This program is distributed in the hope that it will be useful,
|
|
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 // GNU General Public License for more details.
|
|
18 //
|
|
19 // You should have received a copy of the GNU General Public License along
|
|
20 // with this program; if not, write to the Free Software Foundation, Inc.,
|
|
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
22 //-------------------------------------------------------------------------
|
|
23 //
|
|
24
|
|
25 package com.glavsoft.rfb.protocol;
|
|
26
|
|
27 import com.glavsoft.core.SettingsChangedEvent;
|
|
28 import com.glavsoft.rfb.CapabilityContainer;
|
|
29 import com.glavsoft.rfb.IChangeSettingsListener;
|
|
30 import com.glavsoft.rfb.RfbCapabilityInfo;
|
|
31 import com.glavsoft.rfb.encoding.EncodingType;
|
|
32 import com.glavsoft.rfb.protocol.auth.SecurityType;
|
|
33
|
|
34 import java.io.Serializable;
|
|
35 import java.util.Iterator;
|
|
36 import java.util.LinkedHashSet;
|
|
37 import java.util.LinkedList;
|
|
38 import java.util.List;
|
|
39
|
|
40 /**
|
|
41 * Protocol Settings class
|
|
42 */
|
|
43 public class ProtocolSettings implements Serializable {
|
|
44 private static final long serialVersionUID = 1L;
|
|
45
|
|
46 private static final EncodingType DEFAULT_PREFERRED_ENCODING = EncodingType.TIGHT;
|
|
47 public static final int DEFAULT_JPEG_QUALITY = 6;
|
|
48 private static final int DEFAULT_COMPRESSION_LEVEL = -6;
|
|
49
|
|
50 // color depth constants
|
|
51 public static final int COLOR_DEPTH_32 = 32;
|
|
52 public static final int COLOR_DEPTH_24 = 24;
|
|
53 public static final int COLOR_DEPTH_16 = 16;
|
|
54 public static final int COLOR_DEPTH_8 = 8;
|
|
55 public static final int COLOR_DEPTH_6 = 6;
|
|
56 public static final int COLOR_DEPTH_3 = 3;
|
|
57
|
|
58 public static final int COLOR_DEPTH_SERVER_SETTINGS = 0;
|
|
59
|
|
60 private static final int DEFAULT_COLOR_DEPTH = COLOR_DEPTH_24;
|
|
61
|
|
62 public static final int CHANGED_VIEW_ONLY = 1; // 1 << 0;
|
|
63 public static final int CHANGED_ENCODINGS = 1 << 1;
|
|
64 public static final int CHANGED_ALLOW_COPY_RECT = 1 << 2;
|
|
65 public static final int CHANGED_SHOW_REMOTE_CURSOR = 1 << 3;
|
|
66 public static final int CHANGED_MOUSE_CURSOR_TRACK = 1 << 4;
|
|
67 public static final int CHANGED_COMPRESSION_LEVEL = 1 << 5;
|
|
68 public static final int CHANGED_JPEG_QUALITY = 1 << 6;
|
|
69 public static final int CHANGED_ALLOW_CLIPBOARD_TRANSFER = 1 << 7;
|
|
70 public static final int CHANGED_CONVERT_TO_ASCII = 1 << 8;
|
|
71 public static final int CHANGED_COLOR_DEPTH = 1 << 9;
|
|
72 public static final int CHANGED_SHARED = 1 << 10;
|
|
73
|
|
74 private transient int changedSettingsMask;
|
|
75
|
|
76 private boolean sharedFlag;
|
|
77 private boolean viewOnly;
|
|
78 private EncodingType preferredEncoding;
|
|
79 private boolean allowCopyRect;
|
|
80 private boolean showRemoteCursor;
|
|
81 private LocalPointer mouseCursorTrack;
|
|
82 private int compressionLevel;
|
|
83 private int jpegQuality;
|
|
84 private boolean allowClipboardTransfer;
|
|
85 private boolean convertToAscii;
|
|
86 private int colorDepth;
|
|
87
|
|
88 public transient LinkedHashSet<EncodingType> encodings;
|
|
89 private transient final List<IChangeSettingsListener> listeners;
|
|
90
|
|
91 public transient CapabilityContainer
|
|
92 tunnelingCapabilities,
|
|
93 authCapabilities,
|
|
94 serverMessagesCapabilities,
|
|
95 clientMessagesCapabilities,
|
|
96 encodingTypesCapabilities;
|
|
97 private transient String remoteCharsetName;
|
|
98
|
|
99 public static ProtocolSettings getDefaultSettings() {
|
|
100 ProtocolSettings settings = new ProtocolSettings();
|
|
101 settings.initKnownAuthCapabilities(settings.authCapabilities);
|
|
102 settings.initKnownEncodingTypesCapabilities(settings.encodingTypesCapabilities);
|
|
103 return settings;
|
|
104 }
|
|
105
|
|
106 private ProtocolSettings() {
|
|
107 sharedFlag = true;
|
|
108 viewOnly = false;
|
|
109 showRemoteCursor = true;
|
|
110 mouseCursorTrack = LocalPointer.ON;
|
|
111 preferredEncoding = DEFAULT_PREFERRED_ENCODING;
|
|
112 allowCopyRect = true;
|
|
113 compressionLevel = DEFAULT_COMPRESSION_LEVEL;
|
|
114 jpegQuality = DEFAULT_JPEG_QUALITY;
|
|
115 convertToAscii = false;
|
|
116 allowClipboardTransfer = true;
|
|
117 colorDepth = COLOR_DEPTH_SERVER_SETTINGS;
|
|
118 refine();
|
|
119
|
|
120 listeners = new LinkedList<IChangeSettingsListener>();
|
|
121 tunnelingCapabilities = new CapabilityContainer();
|
|
122 authCapabilities = new CapabilityContainer();
|
|
123 serverMessagesCapabilities = new CapabilityContainer();
|
|
124 clientMessagesCapabilities = new CapabilityContainer();
|
|
125 encodingTypesCapabilities = new CapabilityContainer();
|
|
126 changedSettingsMask = 0;
|
|
127 }
|
|
128
|
|
129 public ProtocolSettings(ProtocolSettings s) {
|
|
130 this();
|
|
131 copyDataFrom(s);
|
|
132 changedSettingsMask = s.changedSettingsMask;
|
|
133 encodings = s.encodings;
|
|
134 }
|
|
135
|
|
136 public void copyDataFrom(ProtocolSettings s) {
|
|
137 copyDataFrom(s, 0);
|
|
138 }
|
|
139
|
|
140 public void copyDataFrom(ProtocolSettings s, int mask) {
|
|
141 if (null == s) return;
|
|
142 if ((mask & CHANGED_SHARED) == 0) setSharedFlag(s.sharedFlag);
|
|
143 if ((mask & CHANGED_VIEW_ONLY) == 0) setViewOnly(s.viewOnly);
|
|
144 if ((mask & CHANGED_ALLOW_COPY_RECT) == 0) setAllowCopyRect(s.allowCopyRect);
|
|
145 if ((mask & CHANGED_SHOW_REMOTE_CURSOR) == 0) setShowRemoteCursor(s.showRemoteCursor);
|
|
146 if ((mask & CHANGED_ALLOW_CLIPBOARD_TRANSFER) == 0) setAllowClipboardTransfer(s.allowClipboardTransfer);
|
|
147
|
|
148 if ((mask & CHANGED_MOUSE_CURSOR_TRACK) == 0) setMouseCursorTrack(s.mouseCursorTrack);
|
|
149 if ((mask & CHANGED_COMPRESSION_LEVEL) == 0) setCompressionLevel(s.compressionLevel);
|
|
150 if ((mask & CHANGED_JPEG_QUALITY) == 0) setJpegQuality(s.jpegQuality);
|
|
151 if ((mask & CHANGED_CONVERT_TO_ASCII) == 0) setConvertToAscii(s.convertToAscii);
|
|
152 if ((mask & CHANGED_COLOR_DEPTH) == 0) setColorDepth(s.colorDepth);
|
|
153 if ((mask & CHANGED_ENCODINGS) == 0) setPreferredEncoding(s.preferredEncoding);
|
|
154 }
|
|
155
|
|
156 private void initKnownAuthCapabilities(CapabilityContainer cc) {
|
|
157 cc.addEnabled(SecurityType.NONE_AUTHENTICATION.getId(),
|
|
158 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.AUTHENTICATION_NO_AUTH);
|
|
159 cc.addEnabled(SecurityType.VNC_AUTHENTICATION.getId(),
|
|
160 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.AUTHENTICATION_VNC_AUTH);
|
|
161 //cc.addEnabled( 19, "VENC", "VENCRYPT");
|
|
162 //cc.addEnabled( 20, "GTKV", "SASL____");
|
|
163 //cc.addEnabled(129, RfbCapabilityInfo.TIGHT_VNC_VENDOR, "ULGNAUTH");
|
|
164 //cc.addEnabled(130, RfbCapabilityInfo.TIGHT_VNC_VENDOR, "XTRNAUTH");
|
|
165 }
|
|
166
|
|
167 private void initKnownEncodingTypesCapabilities(CapabilityContainer cc) {
|
|
168 cc.add(EncodingType.COPY_RECT.getId(),
|
|
169 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.ENCODING_COPYRECT);
|
|
170 cc.add(EncodingType.HEXTILE.getId(),
|
|
171 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.ENCODING_HEXTILE);
|
|
172 cc.add(EncodingType.ZLIB.getId(),
|
|
173 RfbCapabilityInfo.VENDOR_TRIADA, RfbCapabilityInfo.ENCODING_ZLIB);
|
|
174 cc.add(EncodingType.ZRLE.getId(),
|
|
175 RfbCapabilityInfo.VENDOR_TRIADA, RfbCapabilityInfo.ENCODING_ZRLE);
|
|
176 cc.add(EncodingType.RRE.getId(),
|
|
177 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.ENCODING_RRE);
|
|
178 cc.add(EncodingType.TIGHT.getId(),
|
|
179 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_TIGHT);
|
|
180
|
|
181 cc.add(EncodingType.RICH_CURSOR.getId(),
|
|
182 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_RICH_CURSOR);
|
|
183 cc.add(EncodingType.CURSOR_POS.getId(),
|
|
184 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_CURSOR_POS);
|
|
185 cc.add(EncodingType.DESKTOP_SIZE.getId(),
|
|
186 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_DESKTOP_SIZE);
|
|
187 }
|
|
188
|
|
189 public void addListener(IChangeSettingsListener listener) {
|
|
190 listeners.add(listener);
|
|
191 }
|
|
192
|
|
193 public byte getSharedFlag() {
|
|
194 return (byte) (sharedFlag ? 1 : 0);
|
|
195 }
|
|
196
|
|
197 public boolean isShared() {
|
|
198 return sharedFlag;
|
|
199 }
|
|
200
|
|
201 public void setSharedFlag(boolean sharedFlag) {
|
|
202 if (this.sharedFlag != sharedFlag) {
|
|
203 this.sharedFlag = sharedFlag;
|
|
204 changedSettingsMask |= CHANGED_SHARED;
|
|
205 }
|
|
206 }
|
|
207
|
|
208 public boolean isViewOnly() {
|
|
209 return viewOnly;
|
|
210 }
|
|
211
|
|
212 public void setViewOnly(boolean viewOnly) {
|
|
213 if (this.viewOnly != viewOnly) {
|
|
214 this.viewOnly = viewOnly;
|
|
215 changedSettingsMask |= CHANGED_VIEW_ONLY;
|
|
216 }
|
|
217 }
|
|
218
|
|
219 public void enableAllEncodingCaps() {
|
|
220 encodingTypesCapabilities.setAllEnable(true);
|
|
221
|
|
222 }
|
|
223
|
|
224 public int getColorDepth() {
|
|
225 return colorDepth;
|
|
226 }
|
|
227
|
|
228 /**
|
|
229 * Set depth only in 3, 6, 8, 16, 32. When depth is wrong, it resets to {@link #DEFAULT_COLOR_DEPTH}
|
|
230 */
|
|
231 public void setColorDepth(int depth) {
|
|
232 if (colorDepth != depth) {
|
|
233 changedSettingsMask |= CHANGED_COLOR_DEPTH;
|
|
234 switch (depth) {
|
|
235 case COLOR_DEPTH_32:
|
|
236 colorDepth = COLOR_DEPTH_24;
|
|
237 break;
|
|
238 case COLOR_DEPTH_24:
|
|
239 case COLOR_DEPTH_16:
|
|
240 case COLOR_DEPTH_8:
|
|
241 case COLOR_DEPTH_6:
|
|
242 case COLOR_DEPTH_3:
|
|
243 case COLOR_DEPTH_SERVER_SETTINGS:
|
|
244 colorDepth = depth;
|
|
245 break;
|
|
246 default:
|
|
247 colorDepth = DEFAULT_COLOR_DEPTH;
|
|
248 }
|
|
249 refine();
|
|
250 }
|
|
251 }
|
|
252
|
|
253 public void refine() {
|
|
254 LinkedHashSet<EncodingType> encodings = new LinkedHashSet<EncodingType>();
|
|
255 if (EncodingType.RAW_ENCODING == preferredEncoding) {
|
|
256 // when RAW selected send no ordinary encodings so only default RAW encoding will be enabled
|
|
257 } else {
|
|
258 encodings.add(preferredEncoding); // preferred first
|
|
259 encodings.addAll(EncodingType.ordinaryEncodings);
|
|
260 if (compressionLevel > 0 && compressionLevel < 10) {
|
|
261 encodings.add(EncodingType.byId(
|
|
262 EncodingType.COMPRESS_LEVEL_0.getId() + compressionLevel));
|
|
263 }
|
|
264 if (jpegQuality > 0 && jpegQuality < 10 &&
|
|
265 (colorDepth == COLOR_DEPTH_24 || colorDepth == COLOR_DEPTH_SERVER_SETTINGS)) {
|
|
266 encodings.add(EncodingType.byId(
|
|
267 EncodingType.JPEG_QUALITY_LEVEL_0.getId() + jpegQuality));
|
|
268 }
|
|
269 if (allowCopyRect) {
|
|
270 encodings.add(EncodingType.COPY_RECT);
|
|
271 }
|
|
272 }
|
|
273 switch(mouseCursorTrack) {
|
|
274 case OFF:
|
|
275 setShowRemoteCursor(false);
|
|
276 break;
|
|
277 case HIDE:
|
|
278 setShowRemoteCursor(false);
|
|
279 encodings.add(EncodingType.RICH_CURSOR);
|
|
280 encodings.add(EncodingType.CURSOR_POS);
|
|
281 break;
|
|
282 case ON:
|
|
283 default:
|
|
284 setShowRemoteCursor(true);
|
|
285 encodings.add(EncodingType.RICH_CURSOR);
|
|
286 encodings.add(EncodingType.CURSOR_POS);
|
|
287 }
|
|
288 encodings.add(EncodingType.DESKTOP_SIZE);
|
|
289 if ( isEncodingsChanged(this.encodings, encodings) || isChangedEncodings()) {
|
|
290 this.encodings = encodings;
|
|
291 changedSettingsMask |= CHANGED_ENCODINGS;
|
|
292 }
|
|
293 }
|
|
294
|
|
295 private boolean isEncodingsChanged(LinkedHashSet<EncodingType> encodings1, LinkedHashSet<EncodingType> encodings2) {
|
|
296 if (null == encodings1 || encodings1.size() != encodings2.size()) return true;
|
|
297 Iterator<EncodingType> it1 = encodings1.iterator();
|
|
298 Iterator<EncodingType> it2 = encodings2.iterator();
|
|
299 while (it1.hasNext()) {
|
|
300 EncodingType v1 = it1.next();
|
|
301 EncodingType v2 = it2.next();
|
|
302 if (v1 != v2) return true;
|
|
303 }
|
|
304 return false;
|
|
305 }
|
|
306
|
|
307 public void fireListeners() {
|
|
308 if (null == listeners) return;
|
|
309 final SettingsChangedEvent event = new SettingsChangedEvent(new ProtocolSettings(this));
|
|
310 changedSettingsMask = 0;
|
|
311 for (IChangeSettingsListener listener : listeners) {
|
|
312 listener.settingsChanged(event);
|
|
313 }
|
|
314 }
|
|
315
|
|
316 public static boolean isRfbSettingsChangedFired(SettingsChangedEvent event) {
|
|
317 return event.getSource() instanceof ProtocolSettings;
|
|
318 }
|
|
319
|
|
320 public void setPreferredEncoding(EncodingType preferredEncoding) {
|
|
321 if (this.preferredEncoding != preferredEncoding) {
|
|
322 this.preferredEncoding = preferredEncoding;
|
|
323 changedSettingsMask |= CHANGED_ENCODINGS;
|
|
324 refine();
|
|
325 }
|
|
326 }
|
|
327
|
|
328 public EncodingType getPreferredEncoding() {
|
|
329 return preferredEncoding;
|
|
330 }
|
|
331
|
|
332 public void setAllowCopyRect(boolean allowCopyRect) {
|
|
333 if (this.allowCopyRect != allowCopyRect) {
|
|
334 this.allowCopyRect = allowCopyRect;
|
|
335 changedSettingsMask |= CHANGED_ALLOW_COPY_RECT;
|
|
336 refine();
|
|
337 }
|
|
338 }
|
|
339
|
|
340 public boolean isAllowCopyRect() {
|
|
341 return allowCopyRect;
|
|
342 }
|
|
343
|
|
344 private void setShowRemoteCursor(boolean showRemoteCursor) {
|
|
345 if (this.showRemoteCursor != showRemoteCursor) {
|
|
346 this.showRemoteCursor = showRemoteCursor;
|
|
347 changedSettingsMask |= CHANGED_SHOW_REMOTE_CURSOR;
|
|
348 }
|
|
349 }
|
|
350
|
|
351 public boolean isShowRemoteCursor() {
|
|
352 return showRemoteCursor;
|
|
353 }
|
|
354
|
|
355 public void setMouseCursorTrack(LocalPointer mouseCursorTrack) {
|
|
356 if (this.mouseCursorTrack != mouseCursorTrack) {
|
|
357 this.mouseCursorTrack = mouseCursorTrack;
|
|
358 changedSettingsMask |= CHANGED_MOUSE_CURSOR_TRACK;
|
|
359 refine();
|
|
360 }
|
|
361 }
|
|
362
|
|
363 public LocalPointer getMouseCursorTrack() {
|
|
364 return mouseCursorTrack;
|
|
365 }
|
|
366
|
|
367 public void setCompressionLevel(int compressionLevel) {
|
|
368 if (this.compressionLevel != compressionLevel) {
|
|
369 this.compressionLevel = compressionLevel;
|
|
370 changedSettingsMask |= CHANGED_COMPRESSION_LEVEL;
|
|
371 refine();
|
|
372 }
|
|
373 }
|
|
374
|
|
375 public int getCompressionLevel() {
|
|
376 return compressionLevel;
|
|
377 }
|
|
378
|
|
379 public void setJpegQuality(int jpegQuality) {
|
|
380 if (this.jpegQuality != jpegQuality) {
|
|
381 this.jpegQuality = jpegQuality;
|
|
382 changedSettingsMask |= CHANGED_JPEG_QUALITY;
|
|
383 refine();
|
|
384 }
|
|
385 }
|
|
386
|
|
387 public int getJpegQuality() {
|
|
388 return jpegQuality;
|
|
389 }
|
|
390
|
|
391 public void setAllowClipboardTransfer(boolean enable) {
|
|
392 if (this.allowClipboardTransfer != enable) {
|
|
393 this.allowClipboardTransfer = enable;
|
|
394 changedSettingsMask |= CHANGED_ALLOW_CLIPBOARD_TRANSFER;
|
|
395 }
|
|
396 }
|
|
397
|
|
398 public boolean isAllowClipboardTransfer() {
|
|
399 return allowClipboardTransfer;
|
|
400 }
|
|
401
|
|
402 public boolean isConvertToAscii() {
|
|
403 return convertToAscii;
|
|
404 }
|
|
405
|
|
406 public void setConvertToAscii(boolean convertToAscii) {
|
|
407 if (this.convertToAscii != convertToAscii) {
|
|
408 this.convertToAscii = convertToAscii;
|
|
409 changedSettingsMask |= CHANGED_CONVERT_TO_ASCII;
|
|
410 }
|
|
411 }
|
|
412
|
|
413 public boolean isChangedEncodings() {
|
|
414 return (changedSettingsMask & CHANGED_ENCODINGS) == CHANGED_ENCODINGS;
|
|
415 }
|
|
416
|
|
417 public boolean isChangedColorDepth() {
|
|
418 return (changedSettingsMask & CHANGED_COLOR_DEPTH) == CHANGED_COLOR_DEPTH;
|
|
419 }
|
|
420
|
|
421 public void setRemoteCharsetName(String remoteCharsetName) {
|
|
422 this.remoteCharsetName = remoteCharsetName;
|
|
423 }
|
|
424
|
|
425 public String getRemoteCharsetName() {
|
|
426 return remoteCharsetName;
|
|
427 }
|
|
428
|
|
429 @Override
|
|
430 public String toString() {
|
|
431 return "ProtocolSettings{" +
|
|
432 "sharedFlag=" + sharedFlag +
|
|
433 ", viewOnly=" + viewOnly +
|
|
434 ", preferredEncoding=" + preferredEncoding +
|
|
435 ", allowCopyRect=" + allowCopyRect +
|
|
436 ", showRemoteCursor=" + showRemoteCursor +
|
|
437 ", mouseCursorTrack=" + mouseCursorTrack +
|
|
438 ", compressionLevel=" + compressionLevel +
|
|
439 ", jpegQuality=" + jpegQuality +
|
|
440 ", allowClipboardTransfer=" + allowClipboardTransfer +
|
|
441 ", convertToAscii=" + convertToAscii +
|
|
442 ", colorDepth=" + colorDepth +
|
|
443 '}';
|
|
444 }
|
|
445 }
|