blob: 55ac5686a8b98db9beca2af6157b797b1ca113db [file] [log] [blame]
Constantin Kaplinsky1215b992008-04-18 09:51:44 +00001//
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 Kaplinsky1215b992008-04-18 09:51:44 +000021import java.awt.*;
22import java.awt.event.*;
23import java.io.*;
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +000024import java.net.*;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000025
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000026public class RfbPlayer extends java.applet.Applet
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000027 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 Kaplinsky903009e2002-05-20 10:55:47 +000038 RfbPlayer p = new RfbPlayer();
39 p.mainArgs = argv;
40 p.inAnApplet = false;
41 p.inSeparateFrame = true;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000042
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000043 p.init();
44 p.start();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000045 }
46
47 String[] mainArgs;
48
49 RfbProto rfb;
50 Thread rfbThread;
51
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000052 FbsInputStream fbsStream;
53
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000054 Frame vncFrame;
55 Container vncContainer;
56 ScrollPane desktopScrollPane;
57 GridBagLayout gridbag;
58 ButtonPanel buttonPanel;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000059 VncCanvas vc;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000060
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +000061 String sessionURL;
Constantin Kaplinsky972c2572002-05-30 14:19:02 +000062 long initialTimeOffset;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000063 boolean showControls;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000064 int deferScreenUpdates;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000065
66 //
67 // init()
68 //
69
70 public void init() {
71
72 readParameters();
73
74 if (inSeparateFrame) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000075 vncFrame = new Frame("RFB Session Player");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000076 if (!inAnApplet) {
77 vncFrame.add("Center", this);
78 }
79 vncContainer = vncFrame;
80 } else {
81 vncContainer = this;
82 }
83
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000084 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 Kaplinsky903009e2002-05-20 10:55:47 +000095 // run() - executed by the rfbThread to read RFB data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000096 //
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 Kaplinsky903009e2002-05-20 10:55:47 +0000107 if (showControls) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000108 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 Kaplinsky903009e2002-05-20 10:55:47 +0000114 if (inSeparateFrame) {
115 vncFrame.pack();
116 vncFrame.show();
117 } else {
118 validate();
119 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000120
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000121 try {
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000122 URL url = new URL(sessionURL);
123 fbsStream = new FbsInputStream(url.openStream());
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000124 rfb = new RfbProto(fbsStream);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000125
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 Kaplinsky903009e2002-05-20 10:55:47 +0000160 while (true) {
161 try {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000162 setPaused(true);
163 fbsStream.setTimeOffset(initialTimeOffset);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000164 vc.processNormalProtocol();
165 } catch (EOFException e) {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000166 initialTimeOffset = 0;
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000167 fbsStream.close();
168 fbsStream = new FbsInputStream(url.openStream());
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000169 rfb.newInputStream(fbsStream);
170 }
171 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000172
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000173 } catch (FileNotFoundException e) {
174 fatalError(e.toString());
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000175 } catch (Exception e) {
176 e.printStackTrace();
177 fatalError(e.toString());
178 }
179
180 }
181
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000182 public void setPaused(boolean paused) {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000183 if (showControls)
184 buttonPanel.setPaused(paused);
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000185 if (fbsStream != null) {
186 if (paused) {
187 fbsStream.pausePlayback();
188 } else {
189 fbsStream.resumePlayback();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000190 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000191 }
192 }
193
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000194 public void setSpeed(double speed) {
195 fbsStream.setSpeed(speed);
196 }
197
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000198 public void setPos(int pos) {
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000199 fbsStream.setTimeOffset(pos * 1000);
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000200 }
201
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000202
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000203 public void updatePos() {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000204 if (showControls)
205 buttonPanel.setPos((int)(fbsStream.getTimeOffset() / 1000));
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000206 }
207
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000208 //
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 Kaplinsky1215b992008-04-18 09:51:44 +0000216
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000217 sessionURL = readParameter("URL", true);
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000218 initialTimeOffset = readLongParameter("Position", 0);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000219
220 showControls = true;
221 String str = readParameter("Show Controls", false);
222 if (str != null && str.equalsIgnoreCase("No"))
223 showControls = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000224
225 if (inAnApplet) {
226 str = readParameter("Open New Window", false);
227 if (str != null && str.equalsIgnoreCase("Yes"))
228 inSeparateFrame = true;
229 }
230
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000231 // Fine tuning options.
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000232 deferScreenUpdates = (int)readLongParameter("Defer screen updates", 20);
233 if (deferScreenUpdates < 0)
234 deferScreenUpdates = 0; // Just in case.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000235 }
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 Kaplinsky972c2572002-05-30 14:19:02 +0000264 long readLongParameter(String name, long defaultValue) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000265 String str = readParameter(name, false);
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000266 long result = defaultValue;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000267 if (str != null) {
268 try {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000269 result = Long.parseLong(str);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000270 } catch (NumberFormatException e) { }
271 }
272 return result;
273 }
274
275 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000276 // 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 Kaplinsky1215b992008-04-18 09:51:44 +0000285 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 Kaplinsky1215b992008-04-18 09:51:44 +0000309 if (rfb != null) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000310 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000311 }
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 Kaplinsky903009e2002-05-20 10:55:47 +0000323 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000324 if (rfb != null)
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000325 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000326
327 vncFrame.dispose();
328 if (!inAnApplet) {
329 System.exit(0);
330 }
331 }
332
333 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000334 // Ignore window events we're not interested in.
335 //
336
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000337 public void windowActivated (WindowEvent evt) {}
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000338 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}