comparison src/viewer_swing/java/com/glavsoft/viewer/swing/ParametersHandler.java @ 0:daa24f8a557b

TightVNC original
author YU
date Thu, 11 Sep 2014 07:30:03 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:daa24f8a557b
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.viewer.swing;
26
27 import com.glavsoft.rfb.encoding.EncodingType;
28 import com.glavsoft.rfb.protocol.LocalPointer;
29 import com.glavsoft.rfb.protocol.ProtocolSettings;
30 import com.glavsoft.utils.Strings;
31 import com.glavsoft.viewer.UiSettings;
32 import com.glavsoft.viewer.cli.Parser;
33
34 import javax.swing.*;
35
36 public class ParametersHandler {
37 public static final String ARG_LOCAL_POINTER = "LocalPointer";
38 public static final String ARG_SCALING_FACTOR = "ScalingFactor";
39 public static final String ARG_FULL_SCREEN = "FullScreen";
40 public static final String ARG_COLOR_DEPTH = "ColorDepth";
41 public static final String ARG_JPEG_IMAGE_QUALITY = "JpegImageQuality";
42 public static final String ARG_COMPRESSION_LEVEL = "CompressionLevel";
43 public static final String ARG_ENCODING = "Encoding";
44 public static final String ARG_SHARE_DESKTOP = "ShareDesktop";
45 public static final String ARG_ALLOW_COPY_RECT = "AllowCopyRect";
46 public static final String ARG_VIEW_ONLY = "ViewOnly";
47 public static final String ARG_SHOW_CONTROLS = "ShowControls";
48 public static final String ARG_OPEN_NEW_WINDOW = "OpenNewWindow";
49 public static final String ARG_PASSWORD = "password";
50 public static final String ARG_PORT = "port";
51 public static final String ARG_HOST = "host";
52 public static final String ARG_HELP = "help";
53 public static final String ARG_VERBOSE = "v";
54 public static final String ARG_VERBOSE_MORE = "vv";
55 public static final String ARG_CONVERT_TO_ASCII = "ConvertToASCII";
56 public static final String ARG_ALLOW_CLIPBOARD_TRANSFER = "AllowClipboardTransfer";
57 public static final String ARG_REMOTE_CHARSET = "RemoteCharset";
58 public static final String ARG_SSH_HOST = "sshHost";
59 public static final String ARG_SSH_USER = "sshUser";
60 public static final String ARG_SSH_PORT = "sshPort";
61 public static final String ARG_ALLOW_APPLET_INTERACTIVE_CONNECTIONS = "AllowAppletInteractiveConnections";
62
63 public static boolean isSeparateFrame;
64 public static boolean allowAppletInteractiveConnections;
65
66 public static void completeParserOptions(Parser parser) {
67 parser.addOption(ARG_HELP, null, "Print this help.");
68 parser.addOption(ARG_HOST, "", "Server host name.");
69 parser.addOption(ARG_PORT, "0", "Port number.");
70 parser.addOption(ARG_PASSWORD, null, "Password to the server.");
71 parser.addOption(ARG_SHOW_CONTROLS, null, "Set to \"No\" if you want to get rid of that " +
72 "button panel at the top. Default: \"Yes\".");
73 parser.addOption(ARG_VIEW_ONLY, null, "When set to \"Yes\", then all keyboard and mouse " +
74 "events in the desktop window will be silently ignored and will not be passed " +
75 "to the remote side. Default: \"No\".");
76 parser.addOption(ARG_ALLOW_CLIPBOARD_TRANSFER, null, "When set to \"Yes\", transfer of clipboard contents is allowed. " +
77 "Default: \"Yes\".");
78 parser.addOption(ARG_REMOTE_CHARSET, null, "Charset encoding is used on remote system. Use this option to specify character encoding will be used for encoding clipboard text content to. Default value: local system default character encoding. Set the value to 'standard' for using 'Latin-1' charset which is only specified by rfb standard for clipboard transfers.");
79 parser.addOption(ARG_SHARE_DESKTOP, null, "Share the connection with other clients " +
80 "on the same VNC server. The exact behaviour in each case depends on the server " +
81 "configuration. Default: \"Yes\".");
82 parser.addOption(ARG_ALLOW_COPY_RECT, null, "The \"CopyRect\" encoding saves bandwidth " +
83 "and drawing time when parts of the remote screen are moving around. " +
84 "Most likely, you don't want to change this setting. Default: \"Yes\".");
85 parser.addOption(ARG_ENCODING, null, "The preferred encoding. Possible values: \"Tight\", " +
86 "\"Hextile\", \"ZRLE\", and \"Raw\". Default: \"Tight\".");
87 parser.addOption(ARG_COMPRESSION_LEVEL, null, "Use specified compression level for " +
88 "\"Tight\" and \"Zlib\" encodings. Values: 1-9. Level 1 uses minimum of CPU " +
89 "time on the server but achieves weak compression ratios. Level 9 offers best " +
90 "compression but may be slow.");
91 //noinspection ConstantConditions
92 parser.addOption(ARG_JPEG_IMAGE_QUALITY, null, "Use the specified image quality level " +
93 "in \"Tight\" encoding. Values: 1-9, Lossless. Default value: " +
94 (ProtocolSettings.DEFAULT_JPEG_QUALITY > 0 ?
95 String.valueOf(ProtocolSettings.DEFAULT_JPEG_QUALITY) :
96 "\"Lossless\"")
97 + ". To prevent server of using " +
98 "lossy JPEG compression in \"Tight\" encoding, use \"Lossless\" value here.");
99 parser.addOption(ARG_LOCAL_POINTER, null, "Possible values: on/yes/true (draw pointer locally), off/no/false (let server draw pointer), hide). " +
100 "Default: \"On\".");
101 parser.addOption(ARG_CONVERT_TO_ASCII, null, "Whether to convert keyboard input to ASCII ignoring locale. Possible values: yes/true, no/false). " +
102 "Default: \"No\".");
103 parser.addOption(ARG_COLOR_DEPTH, null, "Bits per pixel color format. Possible values: 3 (for 8 colors), 6 (64 colors), 8 (256 colors), 16 (65 536 colors), 24 (16 777 216 colors), 32 (same as 24).");
104 parser.addOption(ARG_SCALING_FACTOR, null, "Scale local representation of the remote desktop on startup. " +
105 "The value is interpreted as scaling factor in percents. The default value of 100% " +
106 "corresponds to the original framebuffer size.");
107 parser.addOption(ARG_FULL_SCREEN, null, "Full screen mode. Possible values: yes/true and no/false. Default: no.");
108 parser.addOption(ARG_SSH_HOST, "", "SSH host name.");
109 parser.addOption(ARG_SSH_PORT, "0",
110 "SSH port number. When empty, standard SSH port number (" + ConnectionParams.DEFAULT_SSH_PORT + ") is used.");
111 parser.addOption(ARG_SSH_USER, "", "SSH user name.");
112 parser.addOption(ARG_ALLOW_APPLET_INTERACTIVE_CONNECTIONS, null, "Allow applet interactively connect to other hosts then in HostName param or hostbase. Possible values: yes/true, no/false. Default: false.");
113 parser.addOption(ARG_VERBOSE, null, "Verbose console output.");
114 parser.addOption(ARG_VERBOSE_MORE, null, "More verbose console output.");
115
116 }
117
118 public static int completeSettingsFromCLI(final Parser parser, ConnectionParams connectionParams, ProtocolSettings rfbSettings, UiSettings uiSettings) {
119 int mask = completeSettings(
120 new ParamsRetriever() {
121 @Override
122 public String getParamByName(String name) {
123 return parser.getValueFor(name);
124 }
125 },
126 connectionParams, rfbSettings, uiSettings);
127 // when hostName == a.b.c.d:3 where :3 is display num (X Window) we need add display num to port number
128 if ( ! Strings.isTrimmedEmpty(connectionParams.hostName)) {
129 splitConnectionParams(connectionParams, connectionParams.hostName);
130 }
131 if (parser.isSetPlainOptions()) {
132 splitConnectionParams(connectionParams, parser.getPlainOptionAt(0));
133 if (parser.getPlainOptionsNumber() > 1) {
134 try {
135 connectionParams.parseRfbPortNumber(parser.getPlainOptionAt(1));
136 } catch (WrongParameterException e) {
137 //nop
138 }
139 }
140 }
141 return mask;
142 }
143
144
145 /**
146 * Split host string into hostName + port number and set ConnectionParans.
147 * a.b.c.d:5000 -> hostName == a.b.c.d, portNumber == 5000
148 * a.b.c.d::5000 -> hostName == a.b.c.d, portNumber == 5000
149 */
150 public static void splitConnectionParams(final ConnectionParams connectionParams, String host) {
151 int indexOfColon = host.indexOf(':');
152 if (indexOfColon > 0) {
153 String[] splitted = host.split(":");
154 connectionParams.hostName = splitted[0];
155 if (splitted.length > 1) {
156 try {
157 connectionParams.parseRfbPortNumber(splitted[splitted.length - 1]);
158 } catch (WrongParameterException e) {
159 //nop
160 }
161 }
162 } else {
163 connectionParams.hostName = host;
164 }
165 }
166
167 interface ParamsRetriever {
168 String getParamByName(String name);
169 }
170 private static int completeSettings(ParamsRetriever pr, ConnectionParams connectionParams, ProtocolSettings rfbSettings, UiSettings uiSettings) {
171 String hostName = pr.getParamByName(ARG_HOST);
172 String portNumber = pr.getParamByName(ARG_PORT);
173 String showControlsParam = pr.getParamByName(ARG_SHOW_CONTROLS);
174 String viewOnlyParam = pr.getParamByName(ARG_VIEW_ONLY);
175 String allowClipboardTransfer = pr.getParamByName(ARG_ALLOW_CLIPBOARD_TRANSFER);
176 String remoteCharsetName = pr.getParamByName(ARG_REMOTE_CHARSET);
177 String allowCopyRectParam = pr.getParamByName(ARG_ALLOW_COPY_RECT);
178 String shareDesktopParam = pr.getParamByName(ARG_SHARE_DESKTOP);
179 String encodingParam = pr.getParamByName(ARG_ENCODING);
180 String compressionLevelParam = pr.getParamByName(ARG_COMPRESSION_LEVEL);
181 String jpegQualityParam = pr.getParamByName(ARG_JPEG_IMAGE_QUALITY);
182 String colorDepthParam = pr.getParamByName(ARG_COLOR_DEPTH);
183 String scaleFactorParam = pr.getParamByName(ARG_SCALING_FACTOR);
184 String fullScreenParam = pr.getParamByName(ARG_FULL_SCREEN);
185 String localPointerParam = pr.getParamByName(ARG_LOCAL_POINTER);
186 String convertToAsciiParam = pr.getParamByName(ARG_CONVERT_TO_ASCII);
187 String sshHostNameParam = pr.getParamByName(ARG_SSH_HOST);
188 String sshPortNumberParam = pr.getParamByName(ARG_SSH_PORT);
189 String sshUserNameParam = pr.getParamByName(ARG_SSH_USER);
190
191 connectionParams.hostName = hostName;
192 try {
193 connectionParams.parseRfbPortNumber(portNumber);
194 } catch (WrongParameterException e) {
195 //nop
196 }
197
198 connectionParams.sshHostName = sshHostNameParam;
199 connectionParams.setUseSsh( ! Strings.isTrimmedEmpty(sshHostNameParam));
200 connectionParams.parseSshPortNumber(sshPortNumberParam);
201 connectionParams.sshUserName = sshUserNameParam;
202
203 int rfbMask = 0;
204 uiSettings.showControls = parseBooleanOrDefault(showControlsParam, true);
205 allowAppletInteractiveConnections =
206 parseBooleanOrDefault(pr.getParamByName(ARG_ALLOW_APPLET_INTERACTIVE_CONNECTIONS), false);
207 rfbSettings.setViewOnly(parseBooleanOrDefault(viewOnlyParam, false));
208 if (isGiven(viewOnlyParam)) rfbMask |= ProtocolSettings.CHANGED_VIEW_ONLY;
209 rfbSettings.setAllowClipboardTransfer(parseBooleanOrDefault(allowClipboardTransfer, true));
210 if (isGiven(allowClipboardTransfer)) rfbMask |= ProtocolSettings.CHANGED_ALLOW_CLIPBOARD_TRANSFER;
211 rfbSettings.setRemoteCharsetName(remoteCharsetName);
212 rfbSettings.setAllowCopyRect(parseBooleanOrDefault(allowCopyRectParam, true));
213 if (isGiven(allowCopyRectParam)) rfbMask |= ProtocolSettings.CHANGED_ALLOW_COPY_RECT;
214 rfbSettings.setSharedFlag(parseBooleanOrDefault(shareDesktopParam, true));
215 if (isGiven(shareDesktopParam)) rfbMask |= ProtocolSettings.CHANGED_SHARED;
216 rfbSettings.setConvertToAscii(parseBooleanOrDefault(convertToAsciiParam, false));
217 if (isGiven(convertToAsciiParam)) rfbMask |= ProtocolSettings.CHANGED_CONVERT_TO_ASCII;
218 if (EncodingType.TIGHT.getName().equalsIgnoreCase(encodingParam)) {
219 rfbSettings.setPreferredEncoding(EncodingType.TIGHT);
220 rfbMask |= ProtocolSettings.CHANGED_ENCODINGS;
221 }
222 if (EncodingType.HEXTILE.getName().equalsIgnoreCase(encodingParam)) {
223 rfbSettings.setPreferredEncoding(EncodingType.HEXTILE);
224 rfbMask |= ProtocolSettings.CHANGED_ENCODINGS;
225 }
226 if (EncodingType.ZRLE.getName().equalsIgnoreCase(encodingParam)) {
227 rfbSettings.setPreferredEncoding(EncodingType.ZRLE);
228 rfbMask |= ProtocolSettings.CHANGED_ENCODINGS;
229 }
230 if (EncodingType.RAW_ENCODING.getName().equalsIgnoreCase(encodingParam)) {
231 rfbSettings.setPreferredEncoding(EncodingType.RAW_ENCODING);
232 rfbMask |= ProtocolSettings.CHANGED_ENCODINGS;
233 }
234 try {
235 int compLevel = Integer.parseInt(compressionLevelParam);
236 if (compLevel > 0 && compLevel <= 9) {
237 rfbSettings.setCompressionLevel(compLevel);
238 rfbMask |= ProtocolSettings.CHANGED_COMPRESSION_LEVEL;
239 }
240 } catch (NumberFormatException e) { /* nop */ }
241 try {
242 int jpegQuality = Integer.parseInt(jpegQualityParam);
243 if (jpegQuality > 0 && jpegQuality <= 9) {
244 rfbSettings.setJpegQuality(jpegQuality);
245 rfbMask |= ProtocolSettings.CHANGED_JPEG_QUALITY;
246 }
247 } catch (NumberFormatException e) {
248 if ("lossless".equalsIgnoreCase(jpegQualityParam)) {
249 rfbSettings.setJpegQuality( - Math.abs(rfbSettings.getJpegQuality()));
250 }
251 }
252 try {
253 int colorDepth = Integer.parseInt(colorDepthParam);
254 rfbSettings.setColorDepth(colorDepth);
255 rfbMask |= ProtocolSettings.CHANGED_COLOR_DEPTH;
256 } catch (NumberFormatException e) { /* nop */ }
257 int uiMask = 0;
258 if (scaleFactorParam != null) {
259 try {
260 int scaleFactor = Integer.parseInt(scaleFactorParam.replaceAll("\\D", ""));
261 if (scaleFactor >= 10 && scaleFactor <= 200) {
262 uiSettings.setScalePercent(scaleFactor);
263 uiMask |= UiSettings.CHANGED_SCALE_FACTOR;
264 }
265 } catch (NumberFormatException e) { /* nop */ }
266 }
267 uiSettings.setFullScreen(parseBooleanOrDefault(fullScreenParam, false));
268 if (isGiven(fullScreenParam)) uiMask |= UiSettings.CHANGED_FULL_SCREEN;
269
270 if ("on".equalsIgnoreCase(localPointerParam) ||
271 "true".equalsIgnoreCase(localPointerParam) ||
272 "yes".equalsIgnoreCase(localPointerParam)) {
273 rfbSettings.setMouseCursorTrack(LocalPointer.ON);
274 rfbMask |= ProtocolSettings.CHANGED_MOUSE_CURSOR_TRACK;
275 }
276 if ("off".equalsIgnoreCase(localPointerParam) ||
277 "no".equalsIgnoreCase(localPointerParam) ||
278 "false".equalsIgnoreCase(localPointerParam)) {
279 rfbSettings.setMouseCursorTrack(LocalPointer.OFF);
280 rfbMask |= ProtocolSettings.CHANGED_MOUSE_CURSOR_TRACK;
281 }
282 if ("hide".equalsIgnoreCase(localPointerParam) ||
283 "hidden".equalsIgnoreCase(localPointerParam)) {
284 rfbSettings.setMouseCursorTrack(LocalPointer.HIDE);
285 rfbMask |= ProtocolSettings.CHANGED_MOUSE_CURSOR_TRACK;
286 }
287 return (uiMask << 16) | rfbMask;
288 }
289
290 private static boolean isGiven(String param) {
291 return ! Strings.isTrimmedEmpty(param);
292 }
293
294 static boolean parseBooleanOrDefault(String param, boolean defaultValue) {
295 return defaultValue ?
296 ! ("no".equalsIgnoreCase(param) || "false".equalsIgnoreCase(param)) :
297 "yes".equalsIgnoreCase(param) || "true".equalsIgnoreCase(param);
298 }
299
300 public static int completeSettingsFromApplet(final JApplet applet,
301 ConnectionParams connectionParams, ProtocolSettings rfbSettings, UiSettings uiSettings) {
302 isSeparateFrame = parseBooleanOrDefault(applet.getParameter(ARG_OPEN_NEW_WINDOW), true);
303 final int paramsMask = completeSettings(
304 new ParamsRetriever() {
305 @Override
306 public String getParamByName(String name) {
307 return applet.getParameter(name);
308 }
309 },
310 connectionParams, rfbSettings, uiSettings);
311 if ( ! allowAppletInteractiveConnections && connectionParams.isHostNameEmpty()) {
312 connectionParams.hostName = applet.getCodeBase().getHost();
313 }
314 return paramsMask;
315 }
316
317
318 }