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