Initial commit of newLISP.
[newlisp.git] / guiserver / java / guiserver.java
blob754f44e2e3a6f1abe2c2817d11461bc6a4eb4697
1 //
2 // guiserver.java
3 // guiserver
4 //
5 // Created by Lutz Mueller on 5/10/07.
6 //
7 //
8 // Copyright (C) 2007 Lutz Mueller
9 //
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import java.lang.*;
26 import java.lang.reflect.*;
27 import java.awt.*;
28 import java.awt.event.*;
29 import java.awt.font.*;
30 import java.util.*;
31 import java.io.*;
32 import java.net.*;
33 import javax.swing.*;
35 @SuppressWarnings("unchecked")
36 public class guiserver extends gsObject
38 static BufferedReader in;
39 static PrintWriter out;
40 static boolean listening = true;
41 static boolean debug = false;
42 static boolean MAC_OS_X;
43 static boolean UTF8 = true;
44 static boolean DOUBLE_BUFFERING = true;
45 static boolean connected = false;
46 static SplashWindow splash = null;
47 static Frame frame = null;
48 static double version = 1.15;
50 public static void main (String args[]) throws IOException, InterruptedException
52 int portIn = 64001;
53 int portOut = 64002;
54 String host = null;
55 String tkn = null;
56 String osName;
57 Socket socket = null;
58 String splashImagePath = null;
60 osName = System.getProperty("os.name");
61 MAC_OS_X = osName.toLowerCase().startsWith("mac os x");
63 System.out.println("newLISP-GS v." + version + " on " + osName);
65 if(args.length == 3) splashImagePath = args[2];
67 // put splash screen first
68 if(splashImagePath != null)
70 frame = new Frame();
71 MediaTracker mt = new MediaTracker(frame);
72 Image splashImage = guiserver.getImageFromPath(splashImagePath, frame.getClass());
73 mt.addImage(splashImage, 0);
74 try { mt.waitForID(0); } catch(InterruptedException ie) {}
75 splash = new SplashWindow(frame, splashImage);
78 gsObject gsobject = new guiserver(new StringTokenizer("System"));
80 if(args.length >= 1)
81 portIn = Integer.parseInt(args[0]);
83 portOut = portIn + 1;
85 if(args.length >= 2)
86 execCommand("newlisp " + args[1] + " " + portIn + " javastart &");
88 // open listener and connection to remote
89 System.out.println(" listening on " + portIn);
90 ServerSocket ssocket = new ServerSocket(portIn);
91 Socket client = ssocket.accept();
92 host = ssocket.getInetAddress().getHostAddress();
93 System.out.println(" accepted connection from " + host);
94 in = new BufferedReader(new InputStreamReader(client.getInputStream()));
95 ssocket.close();
97 //System.out.println("->" + System.getProperty("line.separator").length());
98 System.setProperty("line.separator", "\n");
100 System.out.println(" connecting to " + host + ":" + portOut);
102 int count = 0;
103 while(connected == false)
105 try { socket = new Socket(host, portOut); connected = true; } catch (IOException ioe)
107 Thread.sleep(100);
108 if(count == 30)
110 System.out.println(" server could not connect to " + host + ":" + portOut);
111 System.exit(1);
113 count = count + 1;
114 System.out.println(" retrying to connect");
115 continue;
119 out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
120 System.out.println("server connected");
122 Dispatcher.init();
124 String cmd = null;
125 try {
126 while(listening)
128 cmd = in.readLine();
129 if(cmd == null) { System.out.println("server shutdown"); System.exit(0); }
130 if(debug) Dispatcher.dispatch(cmd);
131 else
133 try { Dispatcher.dispatch(cmd); }
134 catch (Exception e) { ErrorDialog.show(cmd, "Missing argument or item not found"); }
137 } catch (IOException IOexc) { System.out.println("server shutdown"); System.exit(0); }
140 public static void disposeSplash()
142 if(splash != null) splash.dispose();
143 if(frame != null) frame.dispose();
146 public static void execCommand(String command) throws IOException
148 String s;
151 System.out.println("guiserver starting newLISP \"" + command + "\"");
152 Process p = Runtime.getRuntime().exec(command);
154 BufferedReader stdInput = new BufferedReader(new
155 InputStreamReader(p.getInputStream()));
157 BufferedReader stdError = new BufferedReader(new
158 InputStreamReader(p.getErrorStream()));
160 // read the output from the command
161 while ((s = stdInput.readLine()) != null) {
162 System.out.println(s);
165 // read any errors from the attempted command
166 while ((s = stdError.readLine()) != null) {
167 System.out.println(s);
170 System.out.println("guiserver finished exec");
174 public static Image getImageFromPath(String path, Class cls)
176 if(path.startsWith("/local/"))
177 return(Toolkit.getDefaultToolkit().getImage(cls.getClass().getResource("/images" + path.substring(6))));
178 else
179 return(Toolkit.getDefaultToolkit().getImage(path));
182 public static ImageIcon getIconFromPath(String path, Class cls)
184 if(path.startsWith("/local/"))
185 return(new ImageIcon(Toolkit.getDefaultToolkit().getImage(cls.getClass().getResource("/images" + path.substring(6)))));
186 else
187 return(new ImageIcon(path));
191 public guiserver(StringTokenizer tokens)
193 id = tokens.nextToken();
195 gsObject.widgets.put(id, this);
197 if(MAC_OS_X)
199 macOSXRegistration();
200 guiserver.DOUBLE_BUFFERING = false; // not necessary on OX X
202 else
204 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
205 GraphicsDevice gd = ge.getDefaultScreenDevice();
206 GraphicsConfiguration gc = gd.getDefaultConfiguration();
207 BufferCapabilities bufCap = gc.getBufferCapabilities();
208 if(bufCap.isPageFlipping())
209 System.out.println(" double buffering supported.");
210 else
211 System.out.println(" double buffering not supported.");
213 try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
214 catch( Exception ex ) { }
219 // System object methods
220 public void setTrace(StringTokenizer tokens)
222 if(tokens.nextToken().equals("true"))
223 guiserver.debug = true;
224 else
225 guiserver.debug = false;
228 public void setLookAndFeel(StringTokenizer tokens)
230 try {
231 UIManager.setLookAndFeel(tokens.nextToken()); }
232 catch(Exception ex) {
233 ErrorDialog.show("set-look-and-feel", "Could not set look and feel");}
236 public void disposeSplash(StringTokenizer tokens)
238 if(splash != null) splash.dispose();
239 if(frame != null) frame.dispose();
242 public void getScreen(StringTokenizer tokens)
244 Toolkit tk = Toolkit.getDefaultToolkit();
246 double screenW = tk.getScreenSize().getWidth();
247 double screenH = tk.getScreenSize().getHeight();
248 int screenRes = tk.getScreenResolution();
250 guiserver.out.println("(set 'gs:screen '(" + screenW + " " + screenH + " " + screenRes + "))\n");
251 guiserver.out.flush();
254 public void getFonts(StringTokenizer tokens)
256 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
257 String[] fontNames = ge.getAvailableFontFamilyNames();
259 guiserver.out.print("(set 'gs:fonts '( ");
260 for(int i = 0; i < fontNames.length; i++)
261 guiserver.out.print("\"" + Base64Coder.encodeString(fontNames[i]) + "\" ");
262 guiserver.out.println(")) ");
263 guiserver.out.flush();
267 public void getVersion(StringTokenizer tokens)
269 guiserver.out.println("(set 'gs:version " + guiserver.version + ")\n");
270 guiserver.out.flush();
273 public void setSyntaxColors(StringTokenizer tokens)
275 SyntaxHighlighter.setSyntaxColors(tokens);
278 public void playSound(StringTokenizer tokens)
280 SoundSystem.playSound(Base64Coder.decodeString(tokens.nextToken()));
283 public void setUTF8(StringTokenizer tokens)
285 if(tokens.nextToken().equals("true"))
286 guiserver.UTF8 = true;
287 else
288 guiserver.UTF8 = false;
292 // Mac OS X specific methods
294 public void macOSXRegistration() {
295 if (MAC_OS_X) {
296 try {
297 OSXAdapter.registerMacOSXApplication(this);
298 OSXAdapter.enablePrefs(true);
299 } catch (Exception e) {
300 System.err.println("Exception while loading the OSXAdapter:");
301 e.printStackTrace();
306 public void about()
308 gsObject gsobject = (gsObject)gsObject.widgets.get("AboutDialog");
309 if(gsobject != null && (gsobject instanceof WindowFrame))
310 ((DialogWidget)gsobject).jdialog.setVisible(true);
311 else
312 JOptionPane.showMessageDialog(null,
313 "Software: copyright (c) 2007 Lutz Mueller http://newlisp.org\n" +
314 "Icons: copyright (c) 2007 Michael Michaels http://neglook.com\nAll rights reserved.",
315 "About newLISP-GS v." + version, JOptionPane.PLAIN_MESSAGE,
316 getIconFromPath("/local/newLISP64.png", this.getClass()));
319 public void preferences()
321 gsObject gsobject = (gsObject)gsObject.widgets.get("PreferencesDialog");
322 if(gsobject != null && (gsobject instanceof WindowFrame))
323 ((DialogWidget)gsobject).jdialog.setVisible(true);
326 public void quit()
328 int option = JOptionPane.showConfirmDialog(null,
329 "Are you sure you want to quit?\nPossibility of losing unsaved content.", "Quit?", JOptionPane.YES_NO_OPTION);
330 if (option == JOptionPane.YES_OPTION)
331 System.exit(0);
336 // the end
339 // eof //