blob: 0d8700602b8a6f09c8517509f85b69c48990815b [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 Kaplinsky30f786a2002-05-29 10:59:52 +0000194 public void setPos(int pos) {
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000195 fbsStream.setTimeOffset(pos * 1000);
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000196 }
197
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000198 public void updatePos() {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000199 if (showControls)
200 buttonPanel.setPos((int)(fbsStream.getTimeOffset() / 1000));
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000201 }
202
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000203 //
204 // readParameters() - read parameters from the html source or from the
205 // command line. On the command line, the arguments are just a sequence of
206 // param_name/param_value pairs where the names and values correspond to
207 // those expected in the html applet tag source.
208 //
209
210 public void readParameters() {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000211
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000212 sessionURL = readParameter("URL", true);
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000213 initialTimeOffset = readLongParameter("Position", 0);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000214
215 showControls = true;
216 String str = readParameter("Show Controls", false);
217 if (str != null && str.equalsIgnoreCase("No"))
218 showControls = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000219
220 if (inAnApplet) {
221 str = readParameter("Open New Window", false);
222 if (str != null && str.equalsIgnoreCase("Yes"))
223 inSeparateFrame = true;
224 }
225
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000226 // Fine tuning options.
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000227 deferScreenUpdates = (int)readLongParameter("Defer screen updates", 20);
228 if (deferScreenUpdates < 0)
229 deferScreenUpdates = 0; // Just in case.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000230 }
231
232 public String readParameter(String name, boolean required) {
233 if (inAnApplet) {
234 String s = getParameter(name);
235 if ((s == null) && required) {
236 fatalError(name + " parameter not specified");
237 }
238 return s;
239 }
240
241 for (int i = 0; i < mainArgs.length; i += 2) {
242 if (mainArgs[i].equalsIgnoreCase(name)) {
243 try {
244 return mainArgs[i+1];
245 } catch (Exception e) {
246 if (required) {
247 fatalError(name + " parameter not specified");
248 }
249 return null;
250 }
251 }
252 }
253 if (required) {
254 fatalError(name + " parameter not specified");
255 }
256 return null;
257 }
258
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000259 long readLongParameter(String name, long defaultValue) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000260 String str = readParameter(name, false);
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000261 long result = defaultValue;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000262 if (str != null) {
263 try {
Constantin Kaplinsky972c2572002-05-30 14:19:02 +0000264 result = Long.parseLong(str);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000265 } catch (NumberFormatException e) { }
266 }
267 return result;
268 }
269
270 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000271 // fatalError() - print out a fatal error message.
272 //
273
274 public void fatalError(String str) {
275 System.out.println(str);
276
277 if (inAnApplet) {
278 vncContainer.removeAll();
279 if (rfb != null) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000280 rfb = null;
281 }
282 Label errLabel = new Label(str);
283 errLabel.setFont(new Font("Helvetica", Font.PLAIN, 12));
284 vncContainer.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
285 vncContainer.add(errLabel);
286 if (inSeparateFrame) {
287 vncFrame.pack();
288 } else {
289 validate();
290 }
291 Thread.currentThread().stop();
292 } else {
293 System.exit(1);
294 }
295 }
296
297
298 //
299 // This method is called before the applet is destroyed.
300 //
301
302 public void destroy() {
303 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000304 if (rfb != null) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000305 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000306 }
307 if (inSeparateFrame) {
308 vncFrame.dispose();
309 }
310 }
311
312
313 //
314 // Close application properly on window close event.
315 //
316
317 public void windowClosing(WindowEvent evt) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000318 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000319 if (rfb != null)
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000320 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000321
322 vncFrame.dispose();
323 if (!inAnApplet) {
324 System.exit(0);
325 }
326 }
327
328 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000329 // Ignore window events we're not interested in.
330 //
331
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000332 public void windowActivated (WindowEvent evt) {}
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000333 public void windowDeactivated (WindowEvent evt) {}
334 public void windowOpened(WindowEvent evt) {}
335 public void windowClosed(WindowEvent evt) {}
336 public void windowIconified(WindowEvent evt) {}
337 public void windowDeiconified(WindowEvent evt) {}
338}