Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2001,2002 HorizonLive.com, Inc. All Rights Reserved. |
| 3 | // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
| 4 | // |
| 5 | // This is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This software is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this software; if not, write to the Free Software |
| 17 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | // USA. |
| 19 | // |
| 20 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 21 | import java.awt.*; |
| 22 | import java.awt.event.*; |
| 23 | import java.io.*; |
Constantin Kaplinsky | 5d99c95 | 2002-05-25 09:49:23 +0000 | [diff] [blame] | 24 | import java.net.*; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 25 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 26 | public class RfbPlayer extends java.applet.Applet |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 27 | implements java.lang.Runnable, WindowListener { |
| 28 | |
| 29 | boolean inAnApplet = true; |
| 30 | boolean inSeparateFrame = false; |
| 31 | |
| 32 | // |
| 33 | // main() is called when run as a java program from the command line. |
| 34 | // It simply runs the applet inside a newly-created frame. |
| 35 | // |
| 36 | |
| 37 | public static void main(String[] argv) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 38 | RfbPlayer p = new RfbPlayer(); |
| 39 | p.mainArgs = argv; |
| 40 | p.inAnApplet = false; |
| 41 | p.inSeparateFrame = true; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 42 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 43 | p.init(); |
| 44 | p.start(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | String[] mainArgs; |
| 48 | |
| 49 | RfbProto rfb; |
| 50 | Thread rfbThread; |
| 51 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 52 | FbsInputStream fbsStream; |
| 53 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 54 | Frame vncFrame; |
| 55 | Container vncContainer; |
| 56 | ScrollPane desktopScrollPane; |
| 57 | GridBagLayout gridbag; |
| 58 | ButtonPanel buttonPanel; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 59 | VncCanvas vc; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 60 | |
Constantin Kaplinsky | 5d99c95 | 2002-05-25 09:49:23 +0000 | [diff] [blame] | 61 | String sessionURL; |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 62 | long initialTimeOffset; |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 63 | boolean showControls; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 64 | int deferScreenUpdates; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 65 | |
| 66 | // |
| 67 | // init() |
| 68 | // |
| 69 | |
| 70 | public void init() { |
| 71 | |
| 72 | readParameters(); |
| 73 | |
| 74 | if (inSeparateFrame) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 75 | vncFrame = new Frame("RFB Session Player"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 76 | if (!inAnApplet) { |
| 77 | vncFrame.add("Center", this); |
| 78 | } |
| 79 | vncContainer = vncFrame; |
| 80 | } else { |
| 81 | vncContainer = this; |
| 82 | } |
| 83 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 84 | if (inSeparateFrame) |
| 85 | vncFrame.addWindowListener(this); |
| 86 | |
| 87 | rfbThread = new Thread(this); |
| 88 | rfbThread.start(); |
| 89 | } |
| 90 | |
| 91 | public void update(Graphics g) { |
| 92 | } |
| 93 | |
| 94 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 95 | // run() - executed by the rfbThread to read RFB data. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 96 | // |
| 97 | |
| 98 | public void run() { |
| 99 | |
| 100 | gridbag = new GridBagLayout(); |
| 101 | vncContainer.setLayout(gridbag); |
| 102 | |
| 103 | GridBagConstraints gbc = new GridBagConstraints(); |
| 104 | gbc.gridwidth = GridBagConstraints.REMAINDER; |
| 105 | gbc.anchor = GridBagConstraints.NORTHWEST; |
| 106 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 107 | if (showControls) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 108 | buttonPanel = new ButtonPanel(this); |
| 109 | buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
| 110 | gridbag.setConstraints(buttonPanel, gbc); |
| 111 | vncContainer.add(buttonPanel); |
| 112 | } |
| 113 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 114 | if (inSeparateFrame) { |
| 115 | vncFrame.pack(); |
| 116 | vncFrame.show(); |
| 117 | } else { |
| 118 | validate(); |
| 119 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 120 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 121 | try { |
Constantin Kaplinsky | 5d99c95 | 2002-05-25 09:49:23 +0000 | [diff] [blame] | 122 | URL url = new URL(sessionURL); |
| 123 | fbsStream = new FbsInputStream(url.openStream()); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 124 | rfb = new RfbProto(fbsStream); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 125 | |
| 126 | vc = new VncCanvas(this); |
| 127 | gbc.weightx = 1.0; |
| 128 | gbc.weighty = 1.0; |
| 129 | |
| 130 | if (inSeparateFrame) { |
| 131 | |
| 132 | // Create a panel which itself is resizeable and can hold |
| 133 | // non-resizeable VncCanvas component at the top left corner. |
| 134 | Panel canvasPanel = new Panel(); |
| 135 | canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
| 136 | canvasPanel.add(vc); |
| 137 | |
| 138 | // Create a ScrollPane which will hold a panel with VncCanvas |
| 139 | // inside. |
| 140 | desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); |
| 141 | gbc.fill = GridBagConstraints.BOTH; |
| 142 | gridbag.setConstraints(desktopScrollPane, gbc); |
| 143 | desktopScrollPane.add(canvasPanel); |
| 144 | |
| 145 | // Finally, add our ScrollPane to the Frame window. |
| 146 | vncFrame.add(desktopScrollPane); |
| 147 | vncFrame.setTitle(rfb.desktopName); |
| 148 | vncFrame.pack(); |
| 149 | vc.resizeDesktopFrame(); |
| 150 | |
| 151 | } else { |
| 152 | |
| 153 | // Just add the VncCanvas component to the Applet. |
| 154 | gridbag.setConstraints(vc, gbc); |
| 155 | add(vc); |
| 156 | validate(); |
| 157 | |
| 158 | } |
| 159 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 160 | while (true) { |
| 161 | try { |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 162 | setPaused(true); |
| 163 | fbsStream.setTimeOffset(initialTimeOffset); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 164 | vc.processNormalProtocol(); |
| 165 | } catch (EOFException e) { |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 166 | initialTimeOffset = 0; |
Constantin Kaplinsky | 5d99c95 | 2002-05-25 09:49:23 +0000 | [diff] [blame] | 167 | fbsStream.close(); |
| 168 | fbsStream = new FbsInputStream(url.openStream()); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 169 | rfb.newInputStream(fbsStream); |
| 170 | } |
| 171 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 172 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 173 | } catch (FileNotFoundException e) { |
| 174 | fatalError(e.toString()); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 175 | } catch (Exception e) { |
| 176 | e.printStackTrace(); |
| 177 | fatalError(e.toString()); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
Constantin Kaplinsky | ac6420d | 2002-05-29 17:05:39 +0000 | [diff] [blame] | 182 | public void setPaused(boolean paused) { |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 183 | if (showControls) |
| 184 | buttonPanel.setPaused(paused); |
Constantin Kaplinsky | ac6420d | 2002-05-29 17:05:39 +0000 | [diff] [blame] | 185 | if (fbsStream != null) { |
| 186 | if (paused) { |
| 187 | fbsStream.pausePlayback(); |
| 188 | } else { |
| 189 | fbsStream.resumePlayback(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 190 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Constantin Kaplinsky | ce39d3b | 2002-05-30 15:54:56 +0000 | [diff] [blame^] | 194 | public void setSpeed(double speed) { |
| 195 | fbsStream.setSpeed(speed); |
| 196 | } |
| 197 | |
Constantin Kaplinsky | 30f786a | 2002-05-29 10:59:52 +0000 | [diff] [blame] | 198 | public void setPos(int pos) { |
Constantin Kaplinsky | ac6420d | 2002-05-29 17:05:39 +0000 | [diff] [blame] | 199 | fbsStream.setTimeOffset(pos * 1000); |
Constantin Kaplinsky | 30f786a | 2002-05-29 10:59:52 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Constantin Kaplinsky | ce39d3b | 2002-05-30 15:54:56 +0000 | [diff] [blame^] | 202 | |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 203 | public void updatePos() { |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 204 | if (showControls) |
| 205 | buttonPanel.setPos((int)(fbsStream.getTimeOffset() / 1000)); |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 208 | // |
| 209 | // readParameters() - read parameters from the html source or from the |
| 210 | // command line. On the command line, the arguments are just a sequence of |
| 211 | // param_name/param_value pairs where the names and values correspond to |
| 212 | // those expected in the html applet tag source. |
| 213 | // |
| 214 | |
| 215 | public void readParameters() { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 216 | |
Constantin Kaplinsky | 5d99c95 | 2002-05-25 09:49:23 +0000 | [diff] [blame] | 217 | sessionURL = readParameter("URL", true); |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 218 | initialTimeOffset = readLongParameter("Position", 0); |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 219 | |
| 220 | showControls = true; |
| 221 | String str = readParameter("Show Controls", false); |
| 222 | if (str != null && str.equalsIgnoreCase("No")) |
| 223 | showControls = false; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 224 | |
| 225 | if (inAnApplet) { |
| 226 | str = readParameter("Open New Window", false); |
| 227 | if (str != null && str.equalsIgnoreCase("Yes")) |
| 228 | inSeparateFrame = true; |
| 229 | } |
| 230 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 231 | // Fine tuning options. |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 232 | deferScreenUpdates = (int)readLongParameter("Defer screen updates", 20); |
| 233 | if (deferScreenUpdates < 0) |
| 234 | deferScreenUpdates = 0; // Just in case. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | public String readParameter(String name, boolean required) { |
| 238 | if (inAnApplet) { |
| 239 | String s = getParameter(name); |
| 240 | if ((s == null) && required) { |
| 241 | fatalError(name + " parameter not specified"); |
| 242 | } |
| 243 | return s; |
| 244 | } |
| 245 | |
| 246 | for (int i = 0; i < mainArgs.length; i += 2) { |
| 247 | if (mainArgs[i].equalsIgnoreCase(name)) { |
| 248 | try { |
| 249 | return mainArgs[i+1]; |
| 250 | } catch (Exception e) { |
| 251 | if (required) { |
| 252 | fatalError(name + " parameter not specified"); |
| 253 | } |
| 254 | return null; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | if (required) { |
| 259 | fatalError(name + " parameter not specified"); |
| 260 | } |
| 261 | return null; |
| 262 | } |
| 263 | |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 264 | long readLongParameter(String name, long defaultValue) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 265 | String str = readParameter(name, false); |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 266 | long result = defaultValue; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 267 | if (str != null) { |
| 268 | try { |
Constantin Kaplinsky | 972c257 | 2002-05-30 14:19:02 +0000 | [diff] [blame] | 269 | result = Long.parseLong(str); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 270 | } catch (NumberFormatException e) { } |
| 271 | } |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 276 | // fatalError() - print out a fatal error message. |
| 277 | // |
| 278 | |
| 279 | public void fatalError(String str) { |
| 280 | System.out.println(str); |
| 281 | |
| 282 | if (inAnApplet) { |
| 283 | vncContainer.removeAll(); |
| 284 | if (rfb != null) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 285 | rfb = null; |
| 286 | } |
| 287 | Label errLabel = new Label(str); |
| 288 | errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12)); |
| 289 | vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30)); |
| 290 | vncContainer.add(errLabel); |
| 291 | if (inSeparateFrame) { |
| 292 | vncFrame.pack(); |
| 293 | } else { |
| 294 | validate(); |
| 295 | } |
| 296 | Thread.currentThread().stop(); |
| 297 | } else { |
| 298 | System.exit(1); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | // |
| 304 | // This method is called before the applet is destroyed. |
| 305 | // |
| 306 | |
| 307 | public void destroy() { |
| 308 | vncContainer.removeAll(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 309 | if (rfb != null) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 310 | rfb = null; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 311 | } |
| 312 | if (inSeparateFrame) { |
| 313 | vncFrame.dispose(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | // |
| 319 | // Close application properly on window close event. |
| 320 | // |
| 321 | |
| 322 | public void windowClosing(WindowEvent evt) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 323 | vncContainer.removeAll(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 324 | if (rfb != null) |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 325 | rfb = null; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 326 | |
| 327 | vncFrame.dispose(); |
| 328 | if (!inAnApplet) { |
| 329 | System.exit(0); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 334 | // Ignore window events we're not interested in. |
| 335 | // |
| 336 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 337 | public void windowActivated (WindowEvent evt) {} |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 338 | public void windowDeactivated (WindowEvent evt) {} |
| 339 | public void windowOpened(WindowEvent evt) {} |
| 340 | public void windowClosed(WindowEvent evt) {} |
| 341 | public void windowIconified(WindowEvent evt) {} |
| 342 | public void windowDeiconified(WindowEvent evt) {} |
| 343 | } |