Mercurial > hg > Members > riono > TreeVNC_ja_comment
comparison src/main/java/com/glavsoft/rfb/protocol/ProtocolSettings.java @ 0:4689cc86d6cb
create TreeViewer2 Repository
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2012 13:20:49 +0900 |
parents | |
children | 1a30763734cf 17b702648079 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4689cc86d6cb |
---|---|
1 // Copyright (C) 2010, 2011 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.util.Iterator; | |
35 import java.util.LinkedHashSet; | |
36 import java.util.LinkedList; | |
37 import java.util.List; | |
38 | |
39 /** | |
40 * Protocol Settings class | |
41 */ | |
42 public class ProtocolSettings implements Cloneable { | |
43 private static final EncodingType DEFAULT_PREFERRED_ENCODING = EncodingType.TIGHT; | |
44 public static final int DEFAULT_JPEG_QUALITY = 6; | |
45 private static final int DEFAULT_COMPRESSION_LEVEL = -6; | |
46 | |
47 // bits per pixel constants | |
48 public static final int BPP_32 = 32; | |
49 public static final int BPP_16 = 16; | |
50 public static final int BPP_8 = 8; | |
51 public static final int BPP_6 = 6; | |
52 public static final int BPP_3 = 3; | |
53 | |
54 public static final int BPP_SERVER_SETTINGS = 0; | |
55 | |
56 private static final int DEFAULT_BITS_PER_PIXEL = BPP_32; | |
57 | |
58 public static final int CHANGED_VIEW_ONLY = 1 << 0; | |
59 public static final int CHANGED_ENCODINGS = 1 << 1; | |
60 public static final int CHANGED_ALLOW_COPY_RECT = 1 << 2; | |
61 public static final int CHANGED_SHOW_REMOTE_CURSOR = 1 << 3; | |
62 public static final int CHANGED_MOUSE_CURSOR_TRACK = 1 << 4; | |
63 public static final int CHANGED_COMPRESSION_LEVEL = 1 << 5; | |
64 public static final int CHANGED_JPEG_QUALITY = 1 << 6; | |
65 public static final int CHANGED_ALLOW_CLIPBOARD_TRANSFER = 1 << 7; | |
66 public static final int CHANGED_CONVERT_TO_ASCII = 1 << 8; | |
67 public static final int CHANGED_BITS_PER_PIXEL = 1 << 9; | |
68 | |
69 private int changedSettingsMask; | |
70 | |
71 private boolean sharedFlag; | |
72 private boolean viewOnly; | |
73 private EncodingType preferredEncoding; | |
74 private boolean allowCopyRect; | |
75 private boolean showRemoteCursor; | |
76 private LocalPointer mouseCursorTrack; | |
77 private int compressionLevel; | |
78 private int jpegQuality; | |
79 private boolean allowClipboardTransfer; | |
80 private boolean convertToAscii; | |
81 private int bitsPerPixel; | |
82 | |
83 private String protocolVersion; | |
84 private boolean isTight; | |
85 | |
86 public LinkedHashSet<EncodingType> encodings; | |
87 | |
88 private final List<IChangeSettingsListener> listeners; | |
89 | |
90 public CapabilityContainer | |
91 tunnelingCapabilities, | |
92 authCapabilities, | |
93 serverMessagesCapabilities, | |
94 clientMessagesCapabilities, | |
95 encodingTypesCapabilities; | |
96 private String remoteCharsetName; | |
97 | |
98 public static ProtocolSettings getDefaultSettings() { | |
99 ProtocolSettings settings = new ProtocolSettings(); | |
100 settings.initKnownAuthCapabilities(settings.authCapabilities); | |
101 settings.initKnownEncodingTypesCapabilities(settings.encodingTypesCapabilities); | |
102 | |
103 settings.sharedFlag = true; | |
104 settings.viewOnly = false; | |
105 settings.showRemoteCursor = true; | |
106 settings.mouseCursorTrack = LocalPointer.ON; | |
107 settings.preferredEncoding = DEFAULT_PREFERRED_ENCODING; | |
108 settings.allowCopyRect = true; | |
109 settings.compressionLevel = DEFAULT_COMPRESSION_LEVEL; | |
110 settings.jpegQuality = DEFAULT_JPEG_QUALITY; | |
111 settings.convertToAscii = false; | |
112 settings.allowClipboardTransfer = true; | |
113 settings.bitsPerPixel = 0;//DEFAULT_BITS_PER_PIXEL; | |
114 settings.refine(); | |
115 settings.changedSettingsMask = 0; | |
116 return settings; | |
117 } | |
118 | |
119 private ProtocolSettings() { | |
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 private ProtocolSettings(ProtocolSettings s) { | |
130 this(); | |
131 | |
132 changedSettingsMask = s.changedSettingsMask; | |
133 | |
134 sharedFlag = s.sharedFlag; | |
135 viewOnly = s.viewOnly; | |
136 preferredEncoding = s.preferredEncoding; | |
137 allowCopyRect = s.allowCopyRect; | |
138 showRemoteCursor = s.showRemoteCursor; | |
139 mouseCursorTrack = s.mouseCursorTrack; | |
140 compressionLevel = s.compressionLevel; | |
141 jpegQuality = s.jpegQuality; | |
142 allowClipboardTransfer = s.allowClipboardTransfer; | |
143 convertToAscii = s.convertToAscii; | |
144 bitsPerPixel = s.bitsPerPixel; | |
145 | |
146 encodings = s.encodings; | |
147 | |
148 protocolVersion = s.protocolVersion; | |
149 isTight = s.isTight; | |
150 } | |
151 | |
152 private void initKnownAuthCapabilities(CapabilityContainer cc) { | |
153 cc.addEnabled(SecurityType.NONE_AUTHENTICATION.getId(), | |
154 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.AUTHENTICATION_NO_AUTH); | |
155 cc.addEnabled(SecurityType.VNC_AUTHENTICATION.getId(), | |
156 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.AUTHENTICATION_VNC_AUTH); | |
157 //cc.addEnabled( 19, "VENC", "VENCRYPT"); | |
158 //cc.addEnabled( 20, "GTKV", "SASL____"); | |
159 //cc.addEnabled(129, RfbCapabilityInfo.TIGHT_VNC_VENDOR, "ULGNAUTH"); | |
160 //cc.addEnabled(130, RfbCapabilityInfo.TIGHT_VNC_VENDOR, "XTRNAUTH"); | |
161 } | |
162 | |
163 private void initKnownEncodingTypesCapabilities(CapabilityContainer cc) { | |
164 cc.add(EncodingType.COPY_RECT.getId(), | |
165 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.ENCODING_COPYRECT); | |
166 cc.add(EncodingType.HEXTILE.getId(), | |
167 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.ENCODING_HEXTILE); | |
168 cc.add(EncodingType.ZLIB.getId(), | |
169 RfbCapabilityInfo.VENDOR_TRIADA, RfbCapabilityInfo.ENCODING_ZLIB); | |
170 cc.add(EncodingType.ZRLE.getId(), | |
171 RfbCapabilityInfo.VENDOR_TRIADA, RfbCapabilityInfo.ENCODING_ZRLE); | |
172 cc.add(EncodingType.RRE.getId(), | |
173 RfbCapabilityInfo.VENDOR_STANDARD, RfbCapabilityInfo.ENCODING_RRE); | |
174 cc.add(EncodingType.TIGHT.getId(), | |
175 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_TIGHT); | |
176 | |
177 cc.add(EncodingType.RICH_CURSOR.getId(), | |
178 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_RICH_CURSOR); | |
179 cc.add(EncodingType.CURSOR_POS.getId(), | |
180 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_CURSOR_POS); | |
181 cc.add(EncodingType.DESKTOP_SIZE.getId(), | |
182 RfbCapabilityInfo.VENDOR_TIGHT, RfbCapabilityInfo.ENCODING_DESKTOP_SIZE); | |
183 } | |
184 | |
185 public void addListener(IChangeSettingsListener listener) { | |
186 listeners.add(listener); | |
187 } | |
188 | |
189 public byte getSharedFlag() { | |
190 return (byte) (sharedFlag ? 1 : 0); | |
191 } | |
192 | |
193 public boolean isShared() { | |
194 return sharedFlag; | |
195 } | |
196 | |
197 public void setSharedFlag(boolean sharedFlag) { | |
198 this.sharedFlag = sharedFlag; | |
199 } | |
200 | |
201 public boolean isViewOnly() { | |
202 return viewOnly; | |
203 } | |
204 | |
205 public void setViewOnly(boolean viewOnly) { | |
206 if (this.viewOnly != viewOnly) { | |
207 this.viewOnly = viewOnly; | |
208 changedSettingsMask |= CHANGED_VIEW_ONLY; | |
209 } | |
210 } | |
211 | |
212 public void enableAllEncodingCaps() { | |
213 encodingTypesCapabilities.setAllEnable(true); | |
214 | |
215 } | |
216 | |
217 public int getBitsPerPixel() { | |
218 return bitsPerPixel; | |
219 } | |
220 | |
221 /** | |
222 * Set bpp only in 3, 6, 8, 16, 32. When bpp is wrong, it resets to {@link #DEFAULT_BITS_PER_PIXEL} | |
223 */ | |
224 public void setBitsPerPixel(int bpp) { | |
225 if (bitsPerPixel != bpp) { | |
226 changedSettingsMask |= CHANGED_BITS_PER_PIXEL; | |
227 switch (bpp) { | |
228 case BPP_32: | |
229 case BPP_16: | |
230 case BPP_8: | |
231 case BPP_6: | |
232 case BPP_3: | |
233 case BPP_SERVER_SETTINGS: | |
234 bitsPerPixel = bpp; | |
235 break; | |
236 default: | |
237 bitsPerPixel = DEFAULT_BITS_PER_PIXEL; | |
238 } | |
239 refine(); | |
240 } | |
241 } | |
242 | |
243 private void refine() { | |
244 LinkedHashSet<EncodingType> encodings = new LinkedHashSet<EncodingType>(); | |
245 if (EncodingType.RAW_ENCODING == preferredEncoding) { | |
246 // when RAW selected send no ordinary encodings so only default RAW encoding will be enabled | |
247 } else { | |
248 encodings.add(preferredEncoding); // preferred first | |
249 encodings.addAll(EncodingType.ordinaryEncodings); | |
250 if (compressionLevel > 0 && compressionLevel < 10) { | |
251 encodings.add(EncodingType.byId( | |
252 EncodingType.COMPRESS_LEVEL_0.getId() + compressionLevel)); | |
253 } | |
254 if (jpegQuality > 0 && jpegQuality < 10 && | |
255 (bitsPerPixel == BPP_32 || bitsPerPixel == BPP_SERVER_SETTINGS)) { | |
256 encodings.add(EncodingType.byId( | |
257 EncodingType.JPEG_QUALITY_LEVEL_0.getId() + jpegQuality)); | |
258 } | |
259 if (allowCopyRect) { | |
260 encodings.add(EncodingType.COPY_RECT); | |
261 } | |
262 } | |
263 switch(mouseCursorTrack) { | |
264 case OFF: | |
265 setShowRemoteCursor(false); | |
266 break; | |
267 case HIDE: | |
268 setShowRemoteCursor(false); | |
269 encodings.add(EncodingType.RICH_CURSOR); | |
270 encodings.add(EncodingType.CURSOR_POS); | |
271 break; | |
272 case ON: | |
273 default: | |
274 setShowRemoteCursor(true); | |
275 encodings.add(EncodingType.RICH_CURSOR); | |
276 encodings.add(EncodingType.CURSOR_POS); | |
277 } | |
278 encodings.add(EncodingType.DESKTOP_SIZE); | |
279 if ( isEncodingsChanged(this.encodings, encodings) || isChangedEncodings()) { | |
280 this.encodings = encodings; | |
281 changedSettingsMask |= CHANGED_ENCODINGS; | |
282 } | |
283 } | |
284 | |
285 private boolean isEncodingsChanged(LinkedHashSet<EncodingType> encodings1, LinkedHashSet<EncodingType> encodings2) { | |
286 if (null == encodings1 || encodings1.size() != encodings2.size()) return true; | |
287 Iterator<EncodingType> it1 = encodings1.iterator(); | |
288 Iterator<EncodingType> it2 = encodings2.iterator(); | |
289 while (it1.hasNext()) { | |
290 EncodingType v1 = it1.next(); | |
291 EncodingType v2 = it2.next(); | |
292 if (v1 != v2) return true; | |
293 } | |
294 return false; | |
295 } | |
296 | |
297 public void fireListeners() { | |
298 final SettingsChangedEvent event = new SettingsChangedEvent(new ProtocolSettings(this)); | |
299 changedSettingsMask = 0; | |
300 for (IChangeSettingsListener listener : listeners) { | |
301 listener.settingsChanged(event); | |
302 } | |
303 } | |
304 | |
305 public static boolean isRfbSettingsChangedFired(SettingsChangedEvent event) { | |
306 return event.getSource() instanceof ProtocolSettings; | |
307 } | |
308 | |
309 public void setPreferredEncoding(EncodingType preferredEncoding) { | |
310 if (this.preferredEncoding != preferredEncoding) { | |
311 this.preferredEncoding = preferredEncoding; | |
312 changedSettingsMask |= CHANGED_ENCODINGS; | |
313 refine(); | |
314 } | |
315 } | |
316 | |
317 public EncodingType getPreferredEncoding() { | |
318 return preferredEncoding; | |
319 } | |
320 | |
321 public void setAllowCopyRect(boolean allowCopyRect) { | |
322 if (this.allowCopyRect != allowCopyRect) { | |
323 this.allowCopyRect = allowCopyRect; | |
324 changedSettingsMask |= CHANGED_ALLOW_COPY_RECT; | |
325 refine(); | |
326 } | |
327 } | |
328 | |
329 public boolean isAllowCopyRect() { | |
330 return allowCopyRect; | |
331 } | |
332 | |
333 private void setShowRemoteCursor(boolean showRemoteCursor) { | |
334 if (this.showRemoteCursor != showRemoteCursor) { | |
335 this.showRemoteCursor = showRemoteCursor; | |
336 changedSettingsMask |= CHANGED_SHOW_REMOTE_CURSOR; | |
337 } | |
338 } | |
339 | |
340 public boolean isShowRemoteCursor() { | |
341 return showRemoteCursor; | |
342 } | |
343 | |
344 public void setMouseCursorTrack(LocalPointer mouseCursorTrack) { | |
345 if (this.mouseCursorTrack != mouseCursorTrack) { | |
346 this.mouseCursorTrack = mouseCursorTrack; | |
347 changedSettingsMask |= CHANGED_MOUSE_CURSOR_TRACK; | |
348 refine(); | |
349 } | |
350 } | |
351 | |
352 public LocalPointer getMouseCursorTrack() { | |
353 return mouseCursorTrack; | |
354 } | |
355 | |
356 public void setCompressionLevel(int compressionLevel) { | |
357 if (this.compressionLevel != compressionLevel) { | |
358 this.compressionLevel = compressionLevel; | |
359 changedSettingsMask |= CHANGED_COMPRESSION_LEVEL; | |
360 refine(); | |
361 } | |
362 } | |
363 | |
364 public int getCompressionLevel() { | |
365 return compressionLevel; | |
366 } | |
367 | |
368 public void setJpegQuality(int jpegQuality) { | |
369 if (this.jpegQuality != jpegQuality) { | |
370 this.jpegQuality = jpegQuality; | |
371 changedSettingsMask |= CHANGED_JPEG_QUALITY; | |
372 refine(); | |
373 } | |
374 } | |
375 | |
376 public int getJpegQuality() { | |
377 return jpegQuality; | |
378 } | |
379 | |
380 public void setAllowClipboardTransfer(boolean enable) { | |
381 if (this.allowClipboardTransfer != enable) { | |
382 this.allowClipboardTransfer = enable; | |
383 changedSettingsMask |= CHANGED_ALLOW_CLIPBOARD_TRANSFER; | |
384 } | |
385 } | |
386 | |
387 public boolean isAllowClipboardTransfer() { | |
388 return allowClipboardTransfer; | |
389 } | |
390 | |
391 public void setTight(boolean isTight) { | |
392 this.isTight = isTight; | |
393 } | |
394 | |
395 public boolean isTight() { | |
396 return isTight; | |
397 } | |
398 | |
399 public void setProtocolVersion(String protocolVersion) { | |
400 this.protocolVersion = protocolVersion; | |
401 } | |
402 | |
403 public String getProtocolVersion() { | |
404 return protocolVersion; | |
405 } | |
406 | |
407 public boolean isConvertToAscii() { | |
408 return convertToAscii; | |
409 } | |
410 | |
411 public void setConvertToAscii(boolean convertToAscii) { | |
412 if (this.convertToAscii != convertToAscii) { | |
413 this.convertToAscii = convertToAscii; | |
414 changedSettingsMask |= CHANGED_CONVERT_TO_ASCII; | |
415 } | |
416 } | |
417 | |
418 public boolean isChangedEncodings() { | |
419 return (changedSettingsMask & CHANGED_ENCODINGS) == CHANGED_ENCODINGS; | |
420 } | |
421 | |
422 public boolean changedBitsPerPixel() { | |
423 return (changedSettingsMask & CHANGED_BITS_PER_PIXEL) == CHANGED_BITS_PER_PIXEL; | |
424 } | |
425 | |
426 public void setRemoteCharsetName(String remoteCharsetName) { | |
427 this.remoteCharsetName = remoteCharsetName; | |
428 } | |
429 | |
430 public String getRemoteCharsetName() { | |
431 return remoteCharsetName; | |
432 } | |
433 | |
434 } |