You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VncViewer.java 27KB

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