24
|
1 package myVncProxy;
|
0
|
2 //
|
|
3 // Copyright (C) 2001-2004 HorizonLive.com, Inc. All Rights Reserved.
|
|
4 // Copyright (C) 2002 Constantin Kaplinsky. All Rights Reserved.
|
|
5 // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
|
|
6 //
|
|
7 // This is free software; you can redistribute it and/or modify
|
|
8 // it under the terms of the GNU General Public License as published by
|
|
9 // the Free Software Foundation; either version 2 of the License, or
|
|
10 // (at your option) any later version.
|
|
11 //
|
|
12 // This software is distributed in the hope that it will be useful,
|
|
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 // GNU General Public License for more details.
|
|
16 //
|
|
17 // You should have received a copy of the GNU General Public License
|
|
18 // along with this software; if not, write to the Free Software
|
|
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
20 // USA.
|
|
21 //
|
|
22
|
|
23 //
|
|
24 // VncViewer.java - the VNC viewer applet. This class mainly just sets up the
|
|
25 // user interface, leaving it to the VncCanvas to do the actual rendering of
|
|
26 // a VNC desktop.
|
|
27 //
|
|
28
|
|
29 import java.awt.*;
|
|
30 import java.awt.event.*;
|
|
31 import java.io.*;
|
|
32 import java.net.*;
|
|
33
|
4
|
34 public class VncViewer extends java.applet.Applet implements
|
|
35 java.lang.Runnable, WindowListener {
|
|
36
|
|
37 boolean inAnApplet = true;
|
|
38 boolean inSeparateFrame = false;
|
|
39
|
|
40 //
|
|
41 // main() is called when run as a java program from the command line.
|
|
42 // It simply runs the applet inside a newly-created frame.
|
|
43 //
|
0
|
44
|
4
|
45 public static void main(String[] argv) {
|
|
46 VncViewer v = new VncViewer();
|
|
47 v.mainArgs = argv;
|
|
48 v.inAnApplet = false;
|
|
49 v.inSeparateFrame = true;
|
|
50 /*
|
|
51 * if(argv.length > 1){ v.host = argv[0]; v.port =
|
|
52 * Integer.parseInt(argv[1]); }
|
|
53 */
|
|
54 v.init();
|
|
55 v.start();
|
|
56 }
|
0
|
57
|
4
|
58 String[] mainArgs;
|
0
|
59
|
10
|
60 // RfbProto rfb;
|
8
|
61 MyRfbProto rfb;
|
4
|
62 Thread rfbThread;
|
0
|
63
|
4
|
64 Frame vncFrame;
|
|
65 Container vncContainer;
|
|
66 ScrollPane desktopScrollPane;
|
|
67 GridBagLayout gridbag;
|
|
68 ButtonPanel buttonPanel;
|
|
69 Label connStatusLabel;
|
|
70 VncCanvas vc;
|
|
71 OptionsFrame options;
|
|
72 ClipboardFrame clipboard;
|
|
73 RecordingFrame rec;
|
0
|
74
|
4
|
75 // Control session recording.
|
|
76 Object recordingSync;
|
|
77 String sessionFileName;
|
|
78 boolean recordingActive;
|
|
79 boolean recordingStatusChanged;
|
|
80 String cursorUpdatesDef;
|
|
81 String eightBitColorsDef;
|
0
|
82
|
4
|
83 // Variables read from parameter values.
|
|
84 String socketFactory;
|
|
85 String host;
|
|
86 int port;
|
|
87 String passwordParam;
|
|
88 boolean showControls;
|
|
89 boolean offerRelogin;
|
|
90 boolean showOfflineDesktop;
|
|
91 int deferScreenUpdates;
|
|
92 int deferCursorUpdates;
|
|
93 int deferUpdateRequests;
|
|
94 int debugStatsExcludeUpdates;
|
|
95 int debugStatsMeasureUpdates;
|
0
|
96
|
4
|
97 // Reference to this applet for inter-applet communication.
|
|
98 public static java.applet.Applet refApplet;
|
0
|
99
|
4
|
100 //
|
|
101 // init()
|
|
102 //
|
0
|
103
|
4
|
104 public void init() {
|
0
|
105
|
4
|
106 readParameters();
|
0
|
107
|
4
|
108 refApplet = this;
|
0
|
109
|
4
|
110 if (inSeparateFrame) {
|
|
111 vncFrame = new Frame("TightVNC");
|
|
112 if (!inAnApplet) {
|
|
113 vncFrame.add("Center", this);
|
|
114 }
|
|
115 vncContainer = vncFrame;
|
|
116 } else {
|
|
117 vncContainer = this;
|
|
118 }
|
0
|
119
|
4
|
120 recordingSync = new Object();
|
0
|
121
|
4
|
122 options = new OptionsFrame(this);
|
|
123 clipboard = new ClipboardFrame(this);
|
|
124 if (RecordingFrame.checkSecurity())
|
|
125 rec = new RecordingFrame(this);
|
0
|
126
|
4
|
127 sessionFileName = null;
|
|
128 recordingActive = false;
|
|
129 recordingStatusChanged = false;
|
|
130 cursorUpdatesDef = null;
|
|
131 eightBitColorsDef = null;
|
0
|
132
|
4
|
133 if (inSeparateFrame)
|
|
134 vncFrame.addWindowListener(this);
|
0
|
135
|
4
|
136 rfbThread = new Thread(this);
|
|
137 rfbThread.start();
|
|
138 }
|
|
139
|
|
140 public void update(Graphics g) {
|
|
141 }
|
0
|
142
|
4
|
143 //
|
|
144 // run() - executed by the rfbThread to deal with the RFB socket.
|
|
145 //
|
0
|
146
|
4
|
147 public void run() {
|
0
|
148
|
4
|
149 gridbag = new GridBagLayout();
|
|
150 vncContainer.setLayout(gridbag);
|
0
|
151
|
4
|
152 GridBagConstraints gbc = new GridBagConstraints();
|
|
153 gbc.gridwidth = GridBagConstraints.REMAINDER;
|
|
154 gbc.anchor = GridBagConstraints.NORTHWEST;
|
0
|
155
|
4
|
156 if (showControls) {
|
|
157 buttonPanel = new ButtonPanel(this);
|
|
158 gridbag.setConstraints(buttonPanel, gbc);
|
|
159 vncContainer.add(buttonPanel);
|
|
160 }
|
0
|
161
|
4
|
162 try {
|
|
163 connectAndAuthenticate();
|
|
164 doProtocolInitialisation();
|
0
|
165
|
4
|
166 // FIXME: Use auto-scaling not only in a separate frame.
|
|
167 if (options.autoScale && inSeparateFrame) {
|
|
168 Dimension screenSize;
|
|
169 try {
|
|
170 screenSize = vncContainer.getToolkit().getScreenSize();
|
|
171 } catch (Exception e) {
|
|
172 screenSize = new Dimension(0, 0);
|
|
173 }
|
|
174 createCanvas(screenSize.width - 32, screenSize.height - 32);
|
|
175 } else {
|
|
176 createCanvas(0, 0);
|
|
177 }
|
0
|
178
|
4
|
179 gbc.weightx = 1.0;
|
|
180 gbc.weighty = 1.0;
|
|
181
|
|
182 if (inSeparateFrame) {
|
0
|
183
|
4
|
184 // Create a panel which itself is resizeable and can hold
|
|
185 // non-resizeable VncCanvas component at the top left corner.
|
|
186 Panel canvasPanel = new Panel();
|
|
187 canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
|
188 canvasPanel.add(vc);
|
0
|
189
|
4
|
190 // Create a ScrollPane which will hold a panel with VncCanvas
|
|
191 // inside.
|
|
192 desktopScrollPane = new ScrollPane(
|
|
193 ScrollPane.SCROLLBARS_AS_NEEDED);
|
|
194 gbc.fill = GridBagConstraints.BOTH;
|
|
195 gridbag.setConstraints(desktopScrollPane, gbc);
|
|
196 desktopScrollPane.add(canvasPanel);
|
0
|
197
|
4
|
198 // Finally, add our ScrollPane to the Frame window.
|
|
199 vncFrame.add(desktopScrollPane);
|
|
200 vncFrame.setTitle(rfb.desktopName);
|
|
201 vncFrame.pack();
|
|
202 vc.resizeDesktopFrame();
|
0
|
203
|
4
|
204 } else {
|
0
|
205
|
4
|
206 // Just add the VncCanvas component to the Applet.
|
|
207 gridbag.setConstraints(vc, gbc);
|
|
208 add(vc);
|
|
209 validate();
|
0
|
210
|
4
|
211 }
|
0
|
212
|
4
|
213 if (showControls)
|
|
214 buttonPanel.enableButtons();
|
0
|
215
|
4
|
216 moveFocusToDesktop();
|
|
217 processNormalProtocol();// main loop
|
0
|
218
|
4
|
219 } catch (NoRouteToHostException e) {
|
|
220 fatalError("Network error: no route to server: " + host, e);
|
|
221 } catch (UnknownHostException e) {
|
|
222 fatalError("Network error: server name unknown: " + host, e);
|
|
223 } catch (ConnectException e) {
|
|
224 fatalError("Network error: could not connect to server: " + host
|
|
225 + ":" + port, e);
|
|
226 } catch (EOFException e) {
|
|
227 if (showOfflineDesktop) {
|
|
228 e.printStackTrace();
|
|
229 System.out
|
|
230 .println("Network error: remote side closed connection");
|
|
231 if (vc != null) {
|
|
232 vc.enableInput(false);
|
|
233 }
|
|
234 if (inSeparateFrame) {
|
|
235 vncFrame.setTitle(rfb.desktopName + " [disconnected]");
|
|
236 }
|
|
237 if (rfb != null && !rfb.closed())
|
|
238 rfb.close();
|
|
239 if (showControls && buttonPanel != null) {
|
|
240 buttonPanel.disableButtonsOnDisconnect();
|
|
241 if (inSeparateFrame) {
|
|
242 vncFrame.pack();
|
|
243 } else {
|
|
244 validate();
|
|
245 }
|
|
246 }
|
|
247 } else {
|
|
248 fatalError("Network error: remote side closed connection", e);
|
|
249 }
|
|
250 } catch (IOException e) {
|
|
251 String str = e.getMessage();
|
|
252 if (str != null && str.length() != 0) {
|
|
253 fatalError("Network Error: " + str, e);
|
|
254 } else {
|
|
255 fatalError(e.toString(), e);
|
|
256 }
|
|
257 } catch (Exception e) {
|
|
258 String str = e.getMessage();
|
|
259 if (str != null && str.length() != 0) {
|
|
260 fatalError("Error: " + str, e);
|
|
261 } else {
|
|
262 fatalError(e.toString(), e);
|
|
263 }
|
|
264 }
|
|
265
|
0
|
266 }
|
|
267
|
4
|
268 //
|
|
269 // Create a VncCanvas instance.
|
|
270 //
|
0
|
271
|
4
|
272 void createCanvas(int maxWidth, int maxHeight) throws IOException {
|
|
273 // Determine if Java 2D API is available and use a special
|
|
274 // version of VncCanvas if it is present.
|
|
275 vc = null;
|
|
276 try {
|
|
277 // This throws ClassNotFoundException if there is no Java 2D API.
|
|
278 Class cl = Class.forName("java.awt.Graphics2D");
|
|
279 // If we could load Graphics2D class, then we can use VncCanvas2D.
|
|
280 cl = Class.forName("VncCanvas2");
|
|
281 Class[] argClasses = { this.getClass(), Integer.TYPE, Integer.TYPE };
|
|
282 java.lang.reflect.Constructor cstr = cl.getConstructor(argClasses);
|
|
283 Object[] argObjects = { this, new Integer(maxWidth),
|
|
284 new Integer(maxHeight) };
|
|
285 vc = (VncCanvas) cstr.newInstance(argObjects);
|
|
286 } catch (Exception e) {
|
|
287 System.out.println("Warning: Java 2D API is not available");
|
|
288 }
|
0
|
289
|
4
|
290 // If we failed to create VncCanvas2D, use old VncCanvas.
|
|
291 if (vc == null)
|
|
292 vc = new VncCanvas(this, maxWidth, maxHeight);
|
|
293 }
|
0
|
294
|
4
|
295 //
|
|
296 // Process RFB socket messages.
|
|
297 // If the rfbThread is being stopped, ignore any exceptions,
|
|
298 // otherwise rethrow the exception so it can be handled.
|
|
299 //
|
0
|
300
|
4
|
301 void processNormalProtocol() throws Exception {
|
|
302 try {
|
|
303 vc.processNormalProtocol();// main loop
|
|
304 } catch (Exception e) {
|
|
305 if (rfbThread == null) {
|
|
306 System.out.println("Ignoring RFB socket exceptions"
|
|
307 + " because applet is stopping");
|
|
308 } else {
|
|
309 throw e;
|
|
310 }
|
|
311 }
|
|
312 }
|
0
|
313
|
4
|
314 //
|
|
315 // Connect to the RFB server and authenticate the user.
|
|
316 //
|
0
|
317
|
4
|
318 void connectAndAuthenticate() throws Exception {
|
|
319 showConnectionStatus("Initializing...");
|
|
320 if (inSeparateFrame) {
|
|
321 vncFrame.pack();
|
|
322 vncFrame.setVisible(true);
|
|
323 } else {
|
|
324 validate();
|
|
325 }
|
|
326
|
|
327 showConnectionStatus("Connecting to " + host + ", port " + port + "...");
|
0
|
328
|
10
|
329 // rfb = new RfbProto(host, port, this);
|
8
|
330 rfb = new MyRfbProto(host, port, this);
|
4
|
331 showConnectionStatus("Connected to server");
|
0
|
332
|
4
|
333 rfb.readVersionMsg();
|
|
334 showConnectionStatus("RFB server supports protocol version "
|
|
335 + rfb.serverMajor + "." + rfb.serverMinor);
|
0
|
336
|
4
|
337 rfb.writeVersionMsg();
|
|
338 showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
|
|
339 + "." + rfb.clientMinor);
|
0
|
340
|
4
|
341 int secType = rfb.negotiateSecurity();
|
|
342 int authType;
|
|
343 if (secType == RfbProto.SecTypeTight) {
|
|
344 showConnectionStatus("Enabling TightVNC protocol extensions");
|
|
345 rfb.setupTunneling();
|
|
346 authType = rfb.negotiateAuthenticationTight();
|
|
347 } else {
|
|
348 authType = secType;
|
|
349 }
|
0
|
350
|
4
|
351 switch (authType) {
|
|
352 case RfbProto.AuthNone:
|
|
353 showConnectionStatus("No authentication needed");
|
|
354 rfb.authenticateNone();
|
|
355 break;
|
|
356 case RfbProto.AuthVNC:
|
|
357 showConnectionStatus("Performing standard VNC authentication");
|
|
358 if (passwordParam != null) {
|
|
359 rfb.authenticateVNC(passwordParam);
|
|
360 } else {
|
|
361 String pw = askPassword();
|
|
362 rfb.authenticateVNC(pw);
|
|
363 }
|
|
364 break;
|
|
365 default:
|
|
366 throw new Exception("Unknown authentication scheme " + authType);
|
|
367 }
|
|
368 }
|
0
|
369
|
4
|
370 //
|
|
371 // Show a message describing the connection status.
|
|
372 // To hide the connection status label, use (msg == null).
|
|
373 //
|
0
|
374
|
4
|
375 void showConnectionStatus(String msg) {
|
|
376 if (msg == null) {
|
|
377 if (vncContainer.isAncestorOf(connStatusLabel)) {
|
|
378 vncContainer.remove(connStatusLabel);
|
|
379 }
|
|
380 return;
|
|
381 }
|
0
|
382
|
4
|
383 System.out.println(msg);
|
0
|
384
|
4
|
385 if (connStatusLabel == null) {
|
|
386 connStatusLabel = new Label("Status: " + msg);
|
|
387 connStatusLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
|
|
388 } else {
|
|
389 connStatusLabel.setText("Status: " + msg);
|
|
390 }
|
0
|
391
|
4
|
392 if (!vncContainer.isAncestorOf(connStatusLabel)) {
|
|
393 GridBagConstraints gbc = new GridBagConstraints();
|
|
394 gbc.gridwidth = GridBagConstraints.REMAINDER;
|
|
395 gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
396 gbc.anchor = GridBagConstraints.NORTHWEST;
|
|
397 gbc.weightx = 1.0;
|
|
398 gbc.weighty = 1.0;
|
|
399 gbc.insets = new Insets(20, 30, 20, 30);
|
|
400 gridbag.setConstraints(connStatusLabel, gbc);
|
|
401 vncContainer.add(connStatusLabel);
|
|
402 }
|
0
|
403
|
4
|
404 if (inSeparateFrame) {
|
|
405 vncFrame.pack();
|
|
406 } else {
|
|
407 validate();
|
|
408 }
|
|
409 }
|
0
|
410
|
4
|
411 //
|
|
412 // Show an authentication panel.
|
|
413 //
|
0
|
414
|
4
|
415 String askPassword() throws Exception {
|
|
416 showConnectionStatus(null);
|
0
|
417
|
4
|
418 AuthPanel authPanel = new AuthPanel(this);
|
0
|
419
|
4
|
420 GridBagConstraints gbc = new GridBagConstraints();
|
|
421 gbc.gridwidth = GridBagConstraints.REMAINDER;
|
|
422 gbc.anchor = GridBagConstraints.NORTHWEST;
|
|
423 gbc.weightx = 1.0;
|
|
424 gbc.weighty = 1.0;
|
|
425 gbc.ipadx = 100;
|
|
426 gbc.ipady = 50;
|
|
427 gridbag.setConstraints(authPanel, gbc);
|
|
428 vncContainer.add(authPanel);
|
0
|
429
|
4
|
430 if (inSeparateFrame) {
|
|
431 vncFrame.pack();
|
|
432 } else {
|
|
433 validate();
|
|
434 }
|
0
|
435
|
4
|
436 authPanel.moveFocusToDefaultField();
|
|
437 String pw = authPanel.getPassword();
|
|
438 vncContainer.remove(authPanel);
|
0
|
439
|
4
|
440 return pw;
|
|
441 }
|
0
|
442
|
4
|
443 //
|
|
444 // Do the rest of the protocol initialisation.
|
|
445 //
|
0
|
446
|
4
|
447 void doProtocolInitialisation() throws IOException {
|
|
448 rfb.writeClientInit();
|
|
449 rfb.readServerInit();
|
|
450
|
|
451 System.out.println("Desktop name is " + rfb.desktopName);
|
|
452 System.out.println("Desktop size is " + rfb.framebufferWidth + " x "
|
|
453 + rfb.framebufferHeight);
|
0
|
454
|
4
|
455 setEncodings();
|
|
456
|
|
457 showConnectionStatus(null);
|
|
458 }
|
0
|
459
|
4
|
460 //
|
|
461 // Send current encoding list to the RFB server.
|
|
462 //
|
0
|
463
|
4
|
464 int[] encodingsSaved;
|
|
465 int nEncodingsSaved;
|
0
|
466
|
4
|
467 void setEncodings() {
|
|
468 setEncodings(false);
|
|
469 }
|
0
|
470
|
4
|
471 void autoSelectEncodings() {
|
|
472 setEncodings(true);
|
|
473 }
|
0
|
474
|
4
|
475 void setEncodings(boolean autoSelectOnly) {
|
|
476 if (options == null || rfb == null || !rfb.inNormalProtocol)
|
|
477 return;
|
0
|
478
|
4
|
479 int preferredEncoding = options.preferredEncoding;
|
|
480 if (preferredEncoding == -1) {
|
|
481 long kbitsPerSecond = rfb.kbitsPerSecond();
|
|
482 if (nEncodingsSaved < 1) {
|
|
483 // Choose Tight or ZRLE encoding for the very first update.
|
|
484 System.out.println("Using Tight/ZRLE encodings");
|
|
485 preferredEncoding = RfbProto.EncodingTight;
|
|
486 } else if (kbitsPerSecond > 2000
|
|
487 && encodingsSaved[0] != RfbProto.EncodingHextile) {
|
|
488 // Switch to Hextile if the connection speed is above 2Mbps.
|
|
489 System.out.println("Throughput " + kbitsPerSecond
|
|
490 + " kbit/s - changing to Hextile encoding");
|
|
491 preferredEncoding = RfbProto.EncodingHextile;
|
|
492 } else if (kbitsPerSecond < 1000
|
|
493 && encodingsSaved[0] != RfbProto.EncodingTight) {
|
|
494 // Switch to Tight/ZRLE if the connection speed is below 1Mbps.
|
|
495 System.out.println("Throughput " + kbitsPerSecond
|
|
496 + " kbit/s - changing to Tight/ZRLE encodings");
|
|
497 preferredEncoding = RfbProto.EncodingTight;
|
|
498 } else {
|
|
499 // Don't change the encoder.
|
|
500 if (autoSelectOnly)
|
|
501 return;
|
|
502 preferredEncoding = encodingsSaved[0];
|
|
503 }
|
|
504 } else {
|
|
505 // Auto encoder selection is not enabled.
|
|
506 if (autoSelectOnly)
|
|
507 return;
|
|
508 }
|
0
|
509
|
4
|
510 int[] encodings = new int[20];
|
|
511 int nEncodings = 0;
|
0
|
512
|
4
|
513 encodings[nEncodings++] = preferredEncoding;
|
|
514 if (options.useCopyRect) {
|
|
515 encodings[nEncodings++] = RfbProto.EncodingCopyRect;
|
|
516 }
|
0
|
517
|
4
|
518 if (preferredEncoding != RfbProto.EncodingTight) {
|
|
519 encodings[nEncodings++] = RfbProto.EncodingTight;
|
|
520 }
|
|
521 if (preferredEncoding != RfbProto.EncodingZRLE) {
|
|
522 encodings[nEncodings++] = RfbProto.EncodingZRLE;
|
|
523 }
|
|
524 if (preferredEncoding != RfbProto.EncodingHextile) {
|
|
525 encodings[nEncodings++] = RfbProto.EncodingHextile;
|
|
526 }
|
|
527 if (preferredEncoding != RfbProto.EncodingZlib) {
|
|
528 encodings[nEncodings++] = RfbProto.EncodingZlib;
|
|
529 }
|
|
530 if (preferredEncoding != RfbProto.EncodingCoRRE) {
|
|
531 encodings[nEncodings++] = RfbProto.EncodingCoRRE;
|
|
532 }
|
|
533 if (preferredEncoding != RfbProto.EncodingRRE) {
|
|
534 encodings[nEncodings++] = RfbProto.EncodingRRE;
|
|
535 }
|
0
|
536
|
4
|
537 if (options.compressLevel >= 0 && options.compressLevel <= 9) {
|
|
538 encodings[nEncodings++] = RfbProto.EncodingCompressLevel0
|
|
539 + options.compressLevel;
|
|
540 }
|
|
541 if (options.jpegQuality >= 0 && options.jpegQuality <= 9) {
|
|
542 encodings[nEncodings++] = RfbProto.EncodingQualityLevel0
|
|
543 + options.jpegQuality;
|
|
544 }
|
0
|
545
|
4
|
546 if (options.requestCursorUpdates) {
|
|
547 encodings[nEncodings++] = RfbProto.EncodingXCursor;
|
|
548 encodings[nEncodings++] = RfbProto.EncodingRichCursor;
|
|
549 if (!options.ignoreCursorUpdates)
|
|
550 encodings[nEncodings++] = RfbProto.EncodingPointerPos;
|
|
551 }
|
0
|
552
|
4
|
553 encodings[nEncodings++] = RfbProto.EncodingLastRect;
|
|
554 encodings[nEncodings++] = RfbProto.EncodingNewFBSize;
|
0
|
555
|
4
|
556 boolean encodingsWereChanged = false;
|
|
557 if (nEncodings != nEncodingsSaved) {
|
|
558 encodingsWereChanged = true;
|
|
559 } else {
|
|
560 for (int i = 0; i < nEncodings; i++) {
|
|
561 if (encodings[i] != encodingsSaved[i]) {
|
|
562 encodingsWereChanged = true;
|
|
563 break;
|
|
564 }
|
|
565 }
|
|
566 }
|
0
|
567
|
4
|
568 if (encodingsWereChanged) {
|
|
569 try {
|
|
570 rfb.writeSetEncodings(encodings, nEncodings);
|
|
571 if (vc != null) {
|
|
572 vc.softCursorFree();
|
|
573 }
|
|
574 } catch (Exception e) {
|
|
575 e.printStackTrace();
|
|
576 }
|
|
577 encodingsSaved = encodings;
|
|
578 nEncodingsSaved = nEncodings;
|
|
579 }
|
|
580 }
|
0
|
581
|
4
|
582 //
|
|
583 // setCutText() - send the given cut text to the RFB server.
|
|
584 //
|
0
|
585
|
4
|
586 void setCutText(String text) {
|
|
587 try {
|
|
588 if (rfb != null && rfb.inNormalProtocol) {
|
|
589 rfb.writeClientCutText(text);
|
|
590 }
|
|
591 } catch (Exception e) {
|
|
592 e.printStackTrace();
|
|
593 }
|
|
594 }
|
0
|
595
|
4
|
596 //
|
|
597 // Order change in session recording status. To stop recording, pass
|
|
598 // null in place of the fname argument.
|
|
599 //
|
0
|
600
|
4
|
601 void setRecordingStatus(String fname) {
|
|
602 synchronized (recordingSync) {
|
|
603 sessionFileName = fname;
|
|
604 recordingStatusChanged = true;
|
|
605 }
|
|
606 }
|
0
|
607
|
4
|
608 //
|
|
609 // Start or stop session recording. Returns true if this method call
|
|
610 // causes recording of a new session.
|
|
611 //
|
0
|
612
|
4
|
613 boolean checkRecordingStatus() throws IOException {
|
|
614 synchronized (recordingSync) {
|
|
615 if (recordingStatusChanged) {
|
|
616 recordingStatusChanged = false;
|
|
617 if (sessionFileName != null) {
|
|
618 startRecording();
|
|
619 return true;
|
|
620 } else {
|
|
621 stopRecording();
|
|
622 }
|
|
623 }
|
|
624 }
|
|
625 return false;
|
0
|
626 }
|
|
627
|
4
|
628 //
|
|
629 // Start session recording.
|
|
630 //
|
0
|
631
|
4
|
632 protected void startRecording() throws IOException {
|
|
633 synchronized (recordingSync) {
|
|
634 if (!recordingActive) {
|
|
635 // Save settings to restore them after recording the session.
|
|
636 cursorUpdatesDef = options.choices[options.cursorUpdatesIndex]
|
|
637 .getSelectedItem();
|
|
638 eightBitColorsDef = options.choices[options.eightBitColorsIndex]
|
|
639 .getSelectedItem();
|
|
640 // Set options to values suitable for recording.
|
|
641 options.choices[options.cursorUpdatesIndex].select("Disable");
|
|
642 options.choices[options.cursorUpdatesIndex].setEnabled(false);
|
|
643 options.setEncodings();
|
|
644 options.choices[options.eightBitColorsIndex].select("No");
|
|
645 options.choices[options.eightBitColorsIndex].setEnabled(false);
|
|
646 options.setColorFormat();
|
|
647 } else {
|
|
648 rfb.closeSession();
|
|
649 }
|
0
|
650
|
4
|
651 System.out.println("Recording the session in " + sessionFileName);
|
|
652 rfb.startSession(sessionFileName);
|
|
653 recordingActive = true;
|
|
654 }
|
|
655 }
|
0
|
656
|
4
|
657 //
|
|
658 // Stop session recording.
|
|
659 //
|
0
|
660
|
4
|
661 protected void stopRecording() throws IOException {
|
|
662 synchronized (recordingSync) {
|
|
663 if (recordingActive) {
|
|
664 // Restore options.
|
|
665 options.choices[options.cursorUpdatesIndex]
|
|
666 .select(cursorUpdatesDef);
|
|
667 options.choices[options.cursorUpdatesIndex].setEnabled(true);
|
|
668 options.setEncodings();
|
|
669 options.choices[options.eightBitColorsIndex]
|
|
670 .select(eightBitColorsDef);
|
|
671 options.choices[options.eightBitColorsIndex].setEnabled(true);
|
|
672 options.setColorFormat();
|
0
|
673
|
4
|
674 rfb.closeSession();
|
|
675 System.out.println("Session recording stopped.");
|
|
676 }
|
|
677 sessionFileName = null;
|
|
678 recordingActive = false;
|
|
679 }
|
|
680 }
|
0
|
681
|
4
|
682 //
|
|
683 // readParameters() - read parameters from the html source or from the
|
|
684 // command line. On the command line, the arguments are just a sequence of
|
|
685 // param_name/param_value pairs where the names and values correspond to
|
|
686 // those expected in the html applet tag source.
|
|
687 //
|
0
|
688
|
4
|
689 void readParameters() {
|
|
690 // host = readParameter("HOST", !inAnApplet);
|
|
691 if (mainArgs.length > 0)
|
|
692 host = mainArgs[0];
|
|
693 else
|
|
694 host = "hades.cr.ie.u-ryukyu.ac.jp";
|
|
695 /*
|
|
696 * if (host == null) { host = getCodeBase().getHost(); if
|
|
697 * (host.equals("")) { fatalError("HOST parameter not specified"); } }
|
|
698 */
|
0
|
699
|
4
|
700 // port = readIntParameter("PORT", 5900);
|
|
701 if (mainArgs.length > 1)
|
|
702 port = Integer.parseInt(mainArgs[1]);
|
|
703 else
|
|
704 port = 5900;
|
|
705 // Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
|
|
706 readPasswordParameters();
|
0
|
707
|
4
|
708 String str;
|
|
709 if (inAnApplet) {
|
|
710 str = readParameter("Open New Window", false);
|
|
711 if (str != null && str.equalsIgnoreCase("Yes"))
|
|
712 inSeparateFrame = true;
|
|
713 }
|
0
|
714
|
4
|
715 // "Show Controls" set to "No" disables button panel.
|
|
716 showControls = true;
|
|
717 str = readParameter("Show Controls", false);
|
|
718 if (str != null && str.equalsIgnoreCase("No"))
|
|
719 showControls = false;
|
0
|
720
|
4
|
721 // "Offer Relogin" set to "No" disables "Login again" and "Close
|
|
722 // window" buttons under error messages in applet mode.
|
|
723 offerRelogin = true;
|
|
724 str = readParameter("Offer Relogin", false);
|
|
725 if (str != null && str.equalsIgnoreCase("No"))
|
|
726 offerRelogin = false;
|
0
|
727
|
4
|
728 // Do we continue showing desktop on remote disconnect?
|
|
729 showOfflineDesktop = false;
|
|
730 str = readParameter("Show Offline Desktop", false);
|
|
731 if (str != null && str.equalsIgnoreCase("Yes"))
|
|
732 showOfflineDesktop = true;
|
0
|
733
|
4
|
734 // Fine tuning options.
|
|
735 deferScreenUpdates = readIntParameter("Defer screen updates", 20);
|
|
736 deferCursorUpdates = readIntParameter("Defer cursor updates", 10);
|
|
737 deferUpdateRequests = readIntParameter("Defer update requests", 0);
|
0
|
738
|
4
|
739 // Debugging options.
|
|
740 debugStatsExcludeUpdates = readIntParameter("DEBUG_XU", 0);
|
|
741 debugStatsMeasureUpdates = readIntParameter("DEBUG_CU", 0);
|
0
|
742
|
4
|
743 // SocketFactory.
|
|
744 socketFactory = readParameter("SocketFactory", false);
|
|
745 }
|
0
|
746
|
4
|
747 //
|
|
748 // Read password parameters. If an "ENCPASSWORD" parameter is set,
|
|
749 // then decrypt the password into the passwordParam string. Otherwise,
|
|
750 // try to read the "PASSWORD" parameter directly to passwordParam.
|
|
751 //
|
0
|
752
|
4
|
753 private void readPasswordParameters() {
|
|
754 String encPasswordParam = readParameter("ENCPASSWORD", false);
|
|
755 if (encPasswordParam == null) {
|
|
756 passwordParam = readParameter("PASSWORD", false);
|
0
|
757
|
4
|
758 } else {
|
|
759 // ENCPASSWORD is hexascii-encoded. Decode.
|
|
760 byte[] pw = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
761 int len = encPasswordParam.length() / 2;
|
|
762 if (len > 8)
|
|
763 len = 8;
|
|
764 for (int i = 0; i < len; i++) {
|
|
765 String hex = encPasswordParam.substring(i * 2, i * 2 + 2);
|
|
766 Integer x = new Integer(Integer.parseInt(hex, 16));
|
|
767 pw[i] = x.byteValue();
|
|
768 }
|
|
769 // Decrypt the password.
|
|
770 byte[] key = { 23, 82, 107, 6, 35, 78, 88, 7 };
|
|
771 DesCipher des = new DesCipher(key);
|
|
772 des.decrypt(pw, 0, pw, 0);
|
|
773 passwordParam = new String(pw);
|
0
|
774
|
4
|
775 }
|
|
776 }
|
0
|
777
|
4
|
778 public String readParameter(String name, boolean required) {
|
|
779 if (inAnApplet) {
|
|
780 String s = getParameter(name);
|
|
781 if ((s == null) && required) {
|
|
782 fatalError(name + " parameter not specified");
|
|
783 }
|
|
784 return s;
|
|
785 }
|
0
|
786
|
4
|
787 for (int i = 0; i < mainArgs.length; i += 2) {
|
|
788 if (mainArgs[i].equalsIgnoreCase(name)) {
|
|
789 try {
|
|
790 return mainArgs[i + 1];
|
|
791 } catch (Exception e) {
|
|
792 if (required) {
|
|
793 fatalError(name + " parameter not specified");
|
|
794 }
|
|
795 return null;
|
|
796 }
|
|
797 }
|
|
798 }
|
|
799 if (required) {
|
|
800 fatalError(name + " parameter not specified");
|
|
801 }
|
|
802 return null;
|
0
|
803 }
|
|
804
|
4
|
805 int readIntParameter(String name, int defaultValue) {
|
|
806 String str = readParameter(name, false);
|
|
807 int result = defaultValue;
|
|
808 if (str != null) {
|
|
809 try {
|
|
810 result = Integer.parseInt(str);
|
|
811 } catch (NumberFormatException e) {
|
|
812 }
|
|
813 }
|
|
814 return result;
|
|
815 }
|
0
|
816
|
4
|
817 //
|
|
818 // moveFocusToDesktop() - move keyboard focus either to VncCanvas.
|
|
819 //
|
0
|
820
|
4
|
821 void moveFocusToDesktop() {
|
|
822 if (vncContainer != null) {
|
|
823 if (vc != null && vncContainer.isAncestorOf(vc))
|
|
824 vc.requestFocus();
|
|
825 }
|
|
826 }
|
|
827
|
|
828 //
|
|
829 // disconnect() - close connection to server.
|
|
830 //
|
|
831
|
|
832 synchronized public void disconnect() {
|
|
833 System.out.println("Disconnecting");
|
0
|
834
|
4
|
835 if (vc != null) {
|
|
836 double sec = (System.currentTimeMillis() - vc.statStartTime) / 1000.0;
|
|
837 double rate = Math.round(vc.statNumUpdates / sec * 100) / 100.0;
|
|
838 int nRealRects = vc.statNumPixelRects;
|
|
839 int nPseudoRects = vc.statNumTotalRects - vc.statNumPixelRects;
|
|
840 System.out.println("Updates received: " + vc.statNumUpdates + " ("
|
|
841 + nRealRects + " rectangles + " + nPseudoRects
|
|
842 + " pseudo), " + rate + " updates/sec");
|
|
843 int numRectsOther = nRealRects - vc.statNumRectsTight
|
|
844 - vc.statNumRectsZRLE - vc.statNumRectsHextile
|
|
845 - vc.statNumRectsRaw - vc.statNumRectsCopy;
|
|
846 System.out.println("Rectangles:" + " Tight=" + vc.statNumRectsTight
|
|
847 + "(JPEG=" + vc.statNumRectsTightJPEG + ") ZRLE="
|
|
848 + vc.statNumRectsZRLE + " Hextile="
|
|
849 + vc.statNumRectsHextile + " Raw=" + vc.statNumRectsRaw
|
|
850 + " CopyRect=" + vc.statNumRectsCopy + " other="
|
|
851 + numRectsOther);
|
0
|
852
|
4
|
853 int raw = vc.statNumBytesDecoded;
|
|
854 int compressed = vc.statNumBytesEncoded;
|
|
855 if (compressed > 0) {
|
|
856 double ratio = Math.round((double) raw / compressed * 1000) / 1000.0;
|
|
857 System.out.println("Pixel data: " + vc.statNumBytesDecoded
|
|
858 + " bytes, " + vc.statNumBytesEncoded
|
|
859 + " compressed, ratio " + ratio);
|
|
860 }
|
|
861 }
|
0
|
862
|
4
|
863 if (rfb != null && !rfb.closed())
|
|
864 rfb.close();
|
|
865 options.dispose();
|
|
866 clipboard.dispose();
|
|
867 if (rec != null)
|
|
868 rec.dispose();
|
0
|
869
|
4
|
870 if (inAnApplet) {
|
|
871 showMessage("Disconnected");
|
|
872 } else {
|
|
873 System.exit(0);
|
|
874 }
|
|
875 }
|
0
|
876
|
4
|
877 //
|
|
878 // fatalError() - print out a fatal error message.
|
|
879 // FIXME: Do we really need two versions of the fatalError() method?
|
|
880 //
|
0
|
881
|
4
|
882 synchronized public void fatalError(String str) {
|
|
883 System.out.println(str);
|
0
|
884
|
4
|
885 if (inAnApplet) {
|
|
886 // vncContainer null, applet not inited,
|
|
887 // can not present the error to the user.
|
|
888 Thread.currentThread().stop();
|
|
889 } else {
|
|
890 System.exit(1);
|
|
891 }
|
|
892 }
|
|
893
|
|
894 synchronized public void fatalError(String str, Exception e) {
|
0
|
895
|
4
|
896 if (rfb != null && rfb.closed()) {
|
|
897 // Not necessary to show error message if the error was caused
|
|
898 // by I/O problems after the rfb.close() method call.
|
|
899 System.out.println("RFB thread finished");
|
|
900 return;
|
|
901 }
|
|
902
|
|
903 System.out.println(str);
|
|
904 e.printStackTrace();
|
0
|
905
|
4
|
906 if (rfb != null)
|
|
907 rfb.close();
|
0
|
908
|
4
|
909 if (inAnApplet) {
|
|
910 showMessage(str);
|
|
911 } else {
|
|
912 System.exit(1);
|
|
913 }
|
|
914 }
|
0
|
915
|
4
|
916 //
|
|
917 // Show message text and optionally "Relogin" and "Close" buttons.
|
|
918 //
|
0
|
919
|
4
|
920 void showMessage(String msg) {
|
|
921 vncContainer.removeAll();
|
0
|
922
|
4
|
923 Label errLabel = new Label(msg, Label.CENTER);
|
|
924 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
|
0
|
925
|
4
|
926 if (offerRelogin) {
|
0
|
927
|
4
|
928 Panel gridPanel = new Panel(new GridLayout(0, 1));
|
|
929 Panel outerPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
|
|
930 outerPanel.add(gridPanel);
|
|
931 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 16));
|
|
932 vncContainer.add(outerPanel);
|
|
933 Panel textPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
|
|
934 textPanel.add(errLabel);
|
|
935 gridPanel.add(textPanel);
|
|
936 gridPanel.add(new ReloginPanel(this));
|
|
937
|
|
938 } else {
|
0
|
939
|
4
|
940 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
|
|
941 vncContainer.add(errLabel);
|
|
942
|
|
943 }
|
0
|
944
|
4
|
945 if (inSeparateFrame) {
|
|
946 vncFrame.pack();
|
|
947 } else {
|
|
948 validate();
|
|
949 }
|
|
950 }
|
0
|
951
|
4
|
952 //
|
|
953 // Stop the applet.
|
|
954 // Main applet thread will terminate on first exception
|
|
955 // after seeing that rfbThread has been set to null.
|
|
956 //
|
0
|
957
|
4
|
958 public void stop() {
|
|
959 System.out.println("Stopping applet");
|
|
960 rfbThread = null;
|
|
961 }
|
0
|
962
|
4
|
963 //
|
|
964 // This method is called before the applet is destroyed.
|
|
965 //
|
|
966
|
|
967 public void destroy() {
|
|
968 System.out.println("Destroying applet");
|
0
|
969
|
4
|
970 vncContainer.removeAll();
|
|
971 options.dispose();
|
|
972 clipboard.dispose();
|
|
973 if (rec != null)
|
|
974 rec.dispose();
|
|
975 if (rfb != null && !rfb.closed())
|
|
976 rfb.close();
|
|
977 if (inSeparateFrame)
|
|
978 vncFrame.dispose();
|
|
979 }
|
0
|
980
|
4
|
981 //
|
|
982 // Start/stop receiving mouse events.
|
|
983 //
|
|
984
|
|
985 public void enableInput(boolean enable) {
|
|
986 vc.enableInput(enable);
|
|
987 }
|
0
|
988
|
4
|
989 //
|
|
990 // Close application properly on window close event.
|
|
991 //
|
0
|
992
|
4
|
993 public void windowClosing(WindowEvent evt) {
|
|
994 System.out.println("Closing window");
|
|
995 if (rfb != null)
|
|
996 disconnect();
|
0
|
997
|
4
|
998 vncContainer.hide();
|
|
999
|
|
1000 if (!inAnApplet) {
|
|
1001 System.exit(0);
|
|
1002 }
|
|
1003 }
|
0
|
1004
|
4
|
1005 //
|
|
1006 // Ignore window events we're not interested in.
|
|
1007 //
|
0
|
1008
|
4
|
1009 public void windowActivated(WindowEvent evt) {
|
|
1010 }
|
|
1011
|
|
1012 public void windowDeactivated(WindowEvent evt) {
|
|
1013 }
|
0
|
1014
|
4
|
1015 public void windowOpened(WindowEvent evt) {
|
|
1016 }
|
|
1017
|
|
1018 public void windowClosed(WindowEvent evt) {
|
|
1019 }
|
0
|
1020
|
4
|
1021 public void windowIconified(WindowEvent evt) {
|
|
1022 }
|
|
1023
|
|
1024 public void windowDeiconified(WindowEvent evt) {
|
|
1025 }
|
0
|
1026 }
|