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