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