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