blob: c2fa6f6938e0869575ceae5337b9c8202e2ed7cb [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
52 Frame vncFrame;
53 Container vncContainer;
54 ScrollPane desktopScrollPane;
55 GridBagLayout gridbag;
56 ButtonPanel buttonPanel;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000057 VncCanvas vc;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000058
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +000059 String sessionURL;
Constantin Kaplinskyc833e012002-05-30 17:59:22 +000060 URL url;
Constantin Kaplinsky972c2572002-05-30 14:19:02 +000061 long initialTimeOffset;
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +000062 double playbackSpeed;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +000063 boolean autoPlay;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000064 boolean showControls;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000065 int deferScreenUpdates;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000066
67 //
68 // init()
69 //
70
71 public void init() {
72
73 readParameters();
74
75 if (inSeparateFrame) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000076 vncFrame = new Frame("RFB Session Player");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000077 if (!inAnApplet) {
78 vncFrame.add("Center", this);
79 }
80 vncContainer = vncFrame;
81 } else {
82 vncContainer = this;
83 }
84
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000085 if (inSeparateFrame)
86 vncFrame.addWindowListener(this);
87
88 rfbThread = new Thread(this);
89 rfbThread.start();
90 }
91
92 public void update(Graphics g) {
93 }
94
95 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000096 // run() - executed by the rfbThread to read RFB data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000097 //
98
99 public void run() {
100
101 gridbag = new GridBagLayout();
102 vncContainer.setLayout(gridbag);
103
104 GridBagConstraints gbc = new GridBagConstraints();
105 gbc.gridwidth = GridBagConstraints.REMAINDER;
106 gbc.anchor = GridBagConstraints.NORTHWEST;
107
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000108 if (showControls) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000109 buttonPanel = new ButtonPanel(this);
110 buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
111 gridbag.setConstraints(buttonPanel, gbc);
112 vncContainer.add(buttonPanel);
113 }
114
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000115 if (inSeparateFrame) {
116 vncFrame.pack();
117 vncFrame.show();
118 } else {
119 validate();
120 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000121
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000122 try {
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000123 url = new URL(sessionURL);
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000124 rfb = new RfbProto(url);
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 Kaplinsky5a7133a2002-07-24 17:02:04 +0000162 setPaused(!autoPlay);
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000163 rfb.fbs.setSpeed(playbackSpeed);
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000164 setPos(initialTimeOffset);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000165 vc.processNormalProtocol();
166 } catch (EOFException e) {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000167 initialTimeOffset = 0;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000168 autoPlay = false;
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000169 rfb.newSession(url);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000170 }
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 Kaplinsky37cc43e2002-05-30 17:30:11 +0000185 if (paused) {
186 rfb.fbs.pausePlayback();
187 } else {
188 rfb.fbs.resumePlayback();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000189 }
190 }
191
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000192 public double getSpeed() {
193 return playbackSpeed;
194 }
195
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000196 public void setSpeed(double speed) {
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000197 playbackSpeed = speed;
Constantin Kaplinsky37cc43e2002-05-30 17:30:11 +0000198 rfb.fbs.setSpeed(speed);
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000199 }
200
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000201 public void setPos(long pos) {
Constantin Kaplinsky5b1b92a2002-05-30 18:02:34 +0000202 rfb.fbs.setTimeOffset(pos);
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000203 }
204
Constantin Kaplinskyce39d3b2002-05-30 15:54:56 +0000205
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000206 public void updatePos() {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000207 if (showControls)
Constantin Kaplinskyc833e012002-05-30 17:59:22 +0000208 buttonPanel.setPos(rfb.fbs.getTimeOffset());
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000209 }
210
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000211 //
212 // readParameters() - read parameters from the html source or from the
213 // command line. On the command line, the arguments are just a sequence of
214 // param_name/param_value pairs where the names and values correspond to
215 // those expected in the html applet tag source.
216 //
217
218 public void readParameters() {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000219
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000220 sessionURL = readParameter("URL", true);
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000221
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000222 initialTimeOffset = readLongParameter("Position", 0);
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000223 if (initialTimeOffset < 0)
224 initialTimeOffset = 0;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000225
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000226 playbackSpeed = readDoubleParameter("Speed", 1.0);
227 if (playbackSpeed <= 0.0)
228 playbackSpeed = 1.0;
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000229
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000230 autoPlay = false;
231 String str = readParameter("Autoplay", false);
232 if (str != null && str.equalsIgnoreCase("Yes"))
233 autoPlay = true;
234
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000235 showControls = true;
Constantin Kaplinsky5a7133a2002-07-24 17:02:04 +0000236 str = readParameter("Show Controls", false);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000237 if (str != null && str.equalsIgnoreCase("No"))
238 showControls = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000239
240 if (inAnApplet) {
241 str = readParameter("Open New Window", false);
242 if (str != null && str.equalsIgnoreCase("Yes"))
243 inSeparateFrame = true;
244 }
245
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000246 // Fine tuning options.
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000247 deferScreenUpdates = (int)readLongParameter("Defer screen updates", 20);
248 if (deferScreenUpdates < 0)
249 deferScreenUpdates = 0; // Just in case.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000250 }
251
252 public String readParameter(String name, boolean required) {
253 if (inAnApplet) {
254 String s = getParameter(name);
255 if ((s == null) && required) {
256 fatalError(name + " parameter not specified");
257 }
258 return s;
259 }
260
261 for (int i = 0; i < mainArgs.length; i += 2) {
262 if (mainArgs[i].equalsIgnoreCase(name)) {
263 try {
264 return mainArgs[i+1];
265 } catch (Exception e) {
266 if (required) {
267 fatalError(name + " parameter not specified");
268 }
269 return null;
270 }
271 }
272 }
273 if (required) {
274 fatalError(name + " parameter not specified");
275 }
276 return null;
277 }
278
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000279 long readLongParameter(String name, long defaultValue) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000280 String str = readParameter(name, false);
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000281 long result = defaultValue;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000282 if (str != null) {
283 try {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000284 result = Long.parseLong(str);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000285 } catch (NumberFormatException e) { }
286 }
287 return result;
288 }
289
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000290 double readDoubleParameter(String name, double defaultValue) {
291 String str = readParameter(name, false);
292 double result = defaultValue;
293 if (str != null) {
294 try {
Constantin Kaplinsky3044ecb2002-06-04 18:29:05 +0000295 result = Double.valueOf(str).doubleValue();
Constantin Kaplinsky7957ca12002-05-30 16:19:11 +0000296 } catch (NumberFormatException e) { }
297 }
298 return result;
299 }
300
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000301 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000302 // fatalError() - print out a fatal error message.
303 //
304
305 public void fatalError(String str) {
306 System.out.println(str);
307
308 if (inAnApplet) {
309 vncContainer.removeAll();
310 if (rfb != null) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000311 rfb = null;
312 }
313 Label errLabel = new Label(str);
314 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
315 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
316 vncContainer.add(errLabel);
317 if (inSeparateFrame) {
318 vncFrame.pack();
319 } else {
320 validate();
321 }
322 Thread.currentThread().stop();
323 } else {
324 System.exit(1);
325 }
326 }
327
328
329 //
330 // This method is called before the applet is destroyed.
331 //
332
333 public void destroy() {
334 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000335 if (rfb != null) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000336 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000337 }
338 if (inSeparateFrame) {
339 vncFrame.dispose();
340 }
341 }
342
343
344 //
345 // Close application properly on window close event.
346 //
347
348 public void windowClosing(WindowEvent evt) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000349 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000350 if (rfb != null)
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000351 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000352
353 vncFrame.dispose();
354 if (!inAnApplet) {
355 System.exit(0);
356 }
357 }
358
359 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000360 // Ignore window events we're not interested in.
361 //
362
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000363 public void windowActivated (WindowEvent evt) {}
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000364 public void windowDeactivated (WindowEvent evt) {}
365 public void windowOpened(WindowEvent evt) {}
366 public void windowClosed(WindowEvent evt) {}
367 public void windowIconified(WindowEvent evt) {}
368 public void windowDeiconified(WindowEvent evt) {}
369}