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