blob: 099148b4e15d94336d5173afdadf6184d037419c [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 Kaplinsky903009e2002-05-20 10:55:47 +000062 boolean showControls;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000063 int deferScreenUpdates;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000064
65 //
66 // init()
67 //
68
69 public void init() {
70
71 readParameters();
72
73 if (inSeparateFrame) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000074 vncFrame = new Frame("RFB Session Player");
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000075 if (!inAnApplet) {
76 vncFrame.add("Center", this);
77 }
78 vncContainer = vncFrame;
79 } else {
80 vncContainer = this;
81 }
82
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000083 if (inSeparateFrame)
84 vncFrame.addWindowListener(this);
85
86 rfbThread = new Thread(this);
87 rfbThread.start();
88 }
89
90 public void update(Graphics g) {
91 }
92
93 //
Constantin Kaplinsky903009e2002-05-20 10:55:47 +000094 // run() - executed by the rfbThread to read RFB data.
Constantin Kaplinsky1215b992008-04-18 09:51:44 +000095 //
96
97 public void run() {
98
99 gridbag = new GridBagLayout();
100 vncContainer.setLayout(gridbag);
101
102 GridBagConstraints gbc = new GridBagConstraints();
103 gbc.gridwidth = GridBagConstraints.REMAINDER;
104 gbc.anchor = GridBagConstraints.NORTHWEST;
105
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000106 if (showControls) {
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000107 buttonPanel = new ButtonPanel(this);
108 buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
109 gridbag.setConstraints(buttonPanel, gbc);
110 vncContainer.add(buttonPanel);
111 }
112
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000113 if (inSeparateFrame) {
114 vncFrame.pack();
115 vncFrame.show();
116 } else {
117 validate();
118 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000119
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000120 try {
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000121 URL url = new URL(sessionURL);
122 fbsStream = new FbsInputStream(url.openStream());
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000123 rfb = new RfbProto(fbsStream);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000124
125 vc = new VncCanvas(this);
126 gbc.weightx = 1.0;
127 gbc.weighty = 1.0;
128
129 if (inSeparateFrame) {
130
131 // Create a panel which itself is resizeable and can hold
132 // non-resizeable VncCanvas component at the top left corner.
133 Panel canvasPanel = new Panel();
134 canvasPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
135 canvasPanel.add(vc);
136
137 // Create a ScrollPane which will hold a panel with VncCanvas
138 // inside.
139 desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
140 gbc.fill = GridBagConstraints.BOTH;
141 gridbag.setConstraints(desktopScrollPane, gbc);
142 desktopScrollPane.add(canvasPanel);
143
144 // Finally, add our ScrollPane to the Frame window.
145 vncFrame.add(desktopScrollPane);
146 vncFrame.setTitle(rfb.desktopName);
147 vncFrame.pack();
148 vc.resizeDesktopFrame();
149
150 } else {
151
152 // Just add the VncCanvas component to the Applet.
153 gridbag.setConstraints(vc, gbc);
154 add(vc);
155 validate();
156
157 }
158
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000159 while (true) {
160 try {
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000161 buttonPanel.setPaused(true);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000162 vc.processNormalProtocol();
163 } catch (EOFException e) {
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000164 fbsStream.close();
165 fbsStream = new FbsInputStream(url.openStream());
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000166 rfb.newInputStream(fbsStream);
167 }
168 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000169
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000170 } catch (FileNotFoundException e) {
171 fatalError(e.toString());
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000172 } catch (Exception e) {
173 e.printStackTrace();
174 fatalError(e.toString());
175 }
176
177 }
178
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000179 public void setPaused(boolean paused) {
180 if (fbsStream != null) {
181 if (paused) {
182 fbsStream.pausePlayback();
183 } else {
184 fbsStream.resumePlayback();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000185 }
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000186 }
187 }
188
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000189 public void setPos(int pos) {
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000190 fbsStream.setTimeOffset(pos * 1000);
Constantin Kaplinsky30f786a2002-05-29 10:59:52 +0000191 }
192
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000193 public void updatePos() {
Constantin Kaplinskyac6420d2002-05-29 17:05:39 +0000194 buttonPanel.setPos((int)(fbsStream.getTimeOffset() / 1000));
Constantin Kaplinskyfe079832002-05-29 00:52:32 +0000195 }
196
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000197 //
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 Kaplinsky1215b992008-04-18 09:51:44 +0000205
Constantin Kaplinsky5d99c952002-05-25 09:49:23 +0000206 sessionURL = readParameter("URL", true);
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000207
208 showControls = true;
209 String str = readParameter("Show Controls", false);
210 if (str != null && str.equalsIgnoreCase("No"))
211 showControls = false;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000212
213 if (inAnApplet) {
214 str = readParameter("Open New Window", false);
215 if (str != null && str.equalsIgnoreCase("Yes"))
216 inSeparateFrame = true;
217 }
218
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000219 // Fine tuning options.
220 deferScreenUpdates = readIntParameter("Defer screen updates", 20);
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000221 }
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 Kaplinsky1215b992008-04-18 09:51:44 +0000262 // 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 Kaplinsky1215b992008-04-18 09:51:44 +0000271 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 Kaplinsky1215b992008-04-18 09:51:44 +0000295 if (rfb != null) {
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000296 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000297 }
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 Kaplinsky903009e2002-05-20 10:55:47 +0000309 vncContainer.removeAll();
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000310 if (rfb != null)
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000311 rfb = null;
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000312
313 vncFrame.dispose();
314 if (!inAnApplet) {
315 System.exit(0);
316 }
317 }
318
319 //
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000320 // Ignore window events we're not interested in.
321 //
322
Constantin Kaplinsky903009e2002-05-20 10:55:47 +0000323 public void windowActivated (WindowEvent evt) {}
Constantin Kaplinsky1215b992008-04-18 09:51:44 +0000324 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}