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.*; |
| 24 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 25 | public class RfbPlayer extends java.applet.Applet |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 26 | implements java.lang.Runnable, WindowListener { |
| 27 | |
| 28 | boolean inAnApplet = true; |
| 29 | boolean inSeparateFrame = false; |
| 30 | |
| 31 | // |
| 32 | // main() is called when run as a java program from the command line. |
| 33 | // It simply runs the applet inside a newly-created frame. |
| 34 | // |
| 35 | |
| 36 | public static void main(String[] argv) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 37 | RfbPlayer p = new RfbPlayer(); |
| 38 | p.mainArgs = argv; |
| 39 | p.inAnApplet = false; |
| 40 | p.inSeparateFrame = true; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 41 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 42 | p.init(); |
| 43 | p.start(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | String[] mainArgs; |
| 47 | |
| 48 | RfbProto rfb; |
| 49 | Thread rfbThread; |
| 50 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 51 | public static final int MODE_STOPPED = 0; |
| 52 | public static final int MODE_PLAYBACK = 1; |
| 53 | public static final int MODE_PAUSED = 2; |
| 54 | protected int mode; |
| 55 | |
| 56 | FbsInputStream fbsStream; |
| 57 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 58 | Frame vncFrame; |
| 59 | Container vncContainer; |
| 60 | ScrollPane desktopScrollPane; |
| 61 | GridBagLayout gridbag; |
| 62 | ButtonPanel buttonPanel; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 63 | VncCanvas vc; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 64 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 65 | String sessionFileName; |
| 66 | boolean showControls; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 67 | int deferScreenUpdates; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 68 | |
| 69 | // |
| 70 | // init() |
| 71 | // |
| 72 | |
| 73 | public void init() { |
| 74 | |
| 75 | readParameters(); |
| 76 | |
| 77 | if (inSeparateFrame) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 78 | vncFrame = new Frame("RFB Session Player"); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 79 | if (!inAnApplet) { |
| 80 | vncFrame.add("Center", this); |
| 81 | } |
| 82 | vncContainer = vncFrame; |
| 83 | } else { |
| 84 | vncContainer = this; |
| 85 | } |
| 86 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 87 | if (inSeparateFrame) |
| 88 | vncFrame.addWindowListener(this); |
| 89 | |
| 90 | rfbThread = new Thread(this); |
| 91 | rfbThread.start(); |
| 92 | } |
| 93 | |
| 94 | public void update(Graphics g) { |
| 95 | } |
| 96 | |
| 97 | // |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 98 | // run() - executed by the rfbThread to read RFB data. |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 99 | // |
| 100 | |
| 101 | public void run() { |
| 102 | |
| 103 | gridbag = new GridBagLayout(); |
| 104 | vncContainer.setLayout(gridbag); |
| 105 | |
| 106 | GridBagConstraints gbc = new GridBagConstraints(); |
| 107 | gbc.gridwidth = GridBagConstraints.REMAINDER; |
| 108 | gbc.anchor = GridBagConstraints.NORTHWEST; |
| 109 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 110 | if (showControls) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 111 | buttonPanel = new ButtonPanel(this); |
| 112 | buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
| 113 | gridbag.setConstraints(buttonPanel, gbc); |
| 114 | vncContainer.add(buttonPanel); |
| 115 | } |
| 116 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 117 | if (inSeparateFrame) { |
| 118 | vncFrame.pack(); |
| 119 | vncFrame.show(); |
| 120 | } else { |
| 121 | validate(); |
| 122 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 123 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 124 | try { |
| 125 | FileInputStream file = new FileInputStream(sessionFileName); |
| 126 | fbsStream = new FbsInputStream(file); |
| 127 | rfb = new RfbProto(fbsStream); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 128 | |
| 129 | vc = new VncCanvas(this); |
| 130 | gbc.weightx = 1.0; |
| 131 | gbc.weighty = 1.0; |
| 132 | |
| 133 | if (inSeparateFrame) { |
| 134 | |
| 135 | // Create a panel which itself is resizeable and can hold |
| 136 | // non-resizeable VncCanvas component at the top left corner. |
| 137 | Panel canvasPanel = new Panel(); |
| 138 | canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
| 139 | canvasPanel.add(vc); |
| 140 | |
| 141 | // Create a ScrollPane which will hold a panel with VncCanvas |
| 142 | // inside. |
| 143 | desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); |
| 144 | gbc.fill = GridBagConstraints.BOTH; |
| 145 | gridbag.setConstraints(desktopScrollPane, gbc); |
| 146 | desktopScrollPane.add(canvasPanel); |
| 147 | |
| 148 | // Finally, add our ScrollPane to the Frame window. |
| 149 | vncFrame.add(desktopScrollPane); |
| 150 | vncFrame.setTitle(rfb.desktopName); |
| 151 | vncFrame.pack(); |
| 152 | vc.resizeDesktopFrame(); |
| 153 | |
| 154 | } else { |
| 155 | |
| 156 | // Just add the VncCanvas component to the Applet. |
| 157 | gridbag.setConstraints(vc, gbc); |
| 158 | add(vc); |
| 159 | validate(); |
| 160 | |
| 161 | } |
| 162 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 163 | while (true) { |
| 164 | try { |
| 165 | buttonPanel.setMode(MODE_STOPPED); |
| 166 | vc.processNormalProtocol(); |
| 167 | } catch (EOFException e) { |
| 168 | file.close(); |
| 169 | file = new FileInputStream(sessionFileName); |
| 170 | fbsStream = new FbsInputStream(file); |
| 171 | rfb.newInputStream(fbsStream); |
| 172 | } |
| 173 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 174 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 175 | } catch (FileNotFoundException e) { |
| 176 | fatalError(e.toString()); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 177 | } catch (Exception e) { |
| 178 | e.printStackTrace(); |
| 179 | fatalError(e.toString()); |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 184 | public int getMode() { |
| 185 | return mode; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 188 | public void setMode(int mode) { |
| 189 | this.mode = mode; |
| 190 | if (vc != null) { |
| 191 | synchronized(vc) { |
| 192 | vc.notify(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 193 | } |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 197 | // |
| 198 | // readParameters() - read parameters from the html source or from the |
| 199 | // command line. On the command line, the arguments are just a sequence of |
| 200 | // param_name/param_value pairs where the names and values correspond to |
| 201 | // those expected in the html applet tag source. |
| 202 | // |
| 203 | |
| 204 | public void readParameters() { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 205 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 206 | sessionFileName = readParameter("FILE", true); |
| 207 | |
| 208 | showControls = true; |
| 209 | String str = readParameter("Show Controls", false); |
| 210 | if (str != null && str.equalsIgnoreCase("No")) |
| 211 | showControls = false; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 212 | |
| 213 | if (inAnApplet) { |
| 214 | str = readParameter("Open New Window", false); |
| 215 | if (str != null && str.equalsIgnoreCase("Yes")) |
| 216 | inSeparateFrame = true; |
| 217 | } |
| 218 | |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 219 | // Fine tuning options. |
| 220 | deferScreenUpdates = readIntParameter("Defer screen updates", 20); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | public String readParameter(String name, boolean required) { |
| 224 | if (inAnApplet) { |
| 225 | String s = getParameter(name); |
| 226 | if ((s == null) && required) { |
| 227 | fatalError(name + " parameter not specified"); |
| 228 | } |
| 229 | return s; |
| 230 | } |
| 231 | |
| 232 | for (int i = 0; i < mainArgs.length; i += 2) { |
| 233 | if (mainArgs[i].equalsIgnoreCase(name)) { |
| 234 | try { |
| 235 | return mainArgs[i+1]; |
| 236 | } catch (Exception e) { |
| 237 | if (required) { |
| 238 | fatalError(name + " parameter not specified"); |
| 239 | } |
| 240 | return null; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | if (required) { |
| 245 | fatalError(name + " parameter not specified"); |
| 246 | } |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | int readIntParameter(String name, int defaultValue) { |
| 251 | String str = readParameter(name, false); |
| 252 | int result = defaultValue; |
| 253 | if (str != null) { |
| 254 | try { |
| 255 | result = Integer.parseInt(str); |
| 256 | } catch (NumberFormatException e) { } |
| 257 | } |
| 258 | return result; |
| 259 | } |
| 260 | |
| 261 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 262 | // fatalError() - print out a fatal error message. |
| 263 | // |
| 264 | |
| 265 | public void fatalError(String str) { |
| 266 | System.out.println(str); |
| 267 | |
| 268 | if (inAnApplet) { |
| 269 | vncContainer.removeAll(); |
| 270 | if (rfb != null) { |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 271 | rfb = null; |
| 272 | } |
| 273 | Label errLabel = new Label(str); |
| 274 | errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12)); |
| 275 | vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30)); |
| 276 | vncContainer.add(errLabel); |
| 277 | if (inSeparateFrame) { |
| 278 | vncFrame.pack(); |
| 279 | } else { |
| 280 | validate(); |
| 281 | } |
| 282 | Thread.currentThread().stop(); |
| 283 | } else { |
| 284 | System.exit(1); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | // |
| 290 | // This method is called before the applet is destroyed. |
| 291 | // |
| 292 | |
| 293 | public void destroy() { |
| 294 | vncContainer.removeAll(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 295 | if (rfb != null) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 296 | rfb = null; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 297 | } |
| 298 | if (inSeparateFrame) { |
| 299 | vncFrame.dispose(); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | // |
| 305 | // Close application properly on window close event. |
| 306 | // |
| 307 | |
| 308 | public void windowClosing(WindowEvent evt) { |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 309 | vncContainer.removeAll(); |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 310 | if (rfb != null) |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 311 | rfb = null; |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 312 | |
| 313 | vncFrame.dispose(); |
| 314 | if (!inAnApplet) { |
| 315 | System.exit(0); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 320 | // Ignore window events we're not interested in. |
| 321 | // |
| 322 | |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame^] | 323 | public void windowActivated (WindowEvent evt) {} |
Constantin Kaplinsky | 1215b99 | 2008-04-18 09:51:44 +0000 | [diff] [blame] | 324 | public void windowDeactivated (WindowEvent evt) {} |
| 325 | public void windowOpened(WindowEvent evt) {} |
| 326 | public void windowClosed(WindowEvent evt) {} |
| 327 | public void windowIconified(WindowEvent evt) {} |
| 328 | public void windowDeiconified(WindowEvent evt) {} |
| 329 | } |