5 // Created by Lutz Mueller on 5/10/07.
8 // Copyright (C) 2007 Lutz Mueller
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/>.
26 import java
.lang
.reflect
.*;
28 import java
.awt
.event
.*;
29 import java
.awt
.font
.*;
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
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)
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"));
81 portIn
= Integer
.parseInt(args
[0]);
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()));
97 //System.out.println("->" + System.getProperty("line.separator").length());
98 System
.setProperty("line.separator", "\n");
100 System
.out
.println(" connecting to " + host
+ ":" + portOut
);
103 while(connected
== false)
105 try { socket
= new Socket(host
, portOut
); connected
= true; } catch (IOException ioe
)
110 System
.out
.println(" server could not connect to " + host
+ ":" + portOut
);
114 System
.out
.println(" retrying to connect");
119 out
= new PrintWriter(new OutputStreamWriter(socket
.getOutputStream()));
120 System
.out
.println("server connected");
129 if(cmd
== null) { System
.out
.println("server shutdown"); System
.exit(0); }
130 if(debug
) Dispatcher
.dispatch(cmd
);
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
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))));
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)))));
187 return(new ImageIcon(path
));
191 public guiserver(StringTokenizer tokens
)
193 id
= tokens
.nextToken();
195 gsObject
.widgets
.put(id
, this);
199 macOSXRegistration();
200 guiserver
.DOUBLE_BUFFERING
= false; // not necessary on OX X
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.");
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;
225 guiserver
.debug
= false;
228 public void setLookAndFeel(StringTokenizer tokens
)
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;
288 guiserver
.UTF8
= false;
292 // Mac OS X specific methods
294 public void macOSXRegistration() {
297 OSXAdapter
.registerMacOSXApplication(this);
298 OSXAdapter
.enablePrefs(true);
299 } catch (Exception e
) {
300 System
.err
.println("Exception while loading the OSXAdapter:");
308 gsObject gsobject
= (gsObject
)gsObject
.widgets
.get("AboutDialog");
309 if(gsobject
!= null && (gsobject
instanceof WindowFrame
))
310 ((DialogWidget
)gsobject
).jdialog
.setVisible(true);
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);
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
)