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