2002/05/28 18:30:30
[org.ibex.core.git] / src / org / xwt / plat / POSIX.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
2 package org.xwt.plat;
3
4 // FIXME: protected void _newBrowserWindow(String url)
5 // FIXME: When should I use RootWindow versus DefaultRootWindow?
6 // FIXME: Solaris: xwt.altKeyName -> "Meta"
7 // FIXME: minimize/taskbar icon
8 // FIXME: 15bpp? can't assume depth/8 == bytespp
9 // FIXME: check for x resource / memory leaks
10 // FIXME: WM_HINTS flags: icon pixmap, icon window [[ have to wait until we have gnome/kde wm's installed ]]
11 // FIXME: code-review POSIX.cc
12
13 import java.awt.*;
14 import java.awt.image.*;
15 import gnu.gcj.RawData;
16 import java.net.*;
17 import java.lang.reflect.*;
18 import java.io.*;
19 import java.util.*;
20 import java.awt.peer.*;
21 import org.xwt.util.*;
22 import org.xwt.*;
23
24 /** Platform implementation for POSIX compliant operating systems with an X11 Server */
25 public class POSIX extends GCJ {
26
27     // Static Data ///////////////////////////////////////////////////////////
28
29     /**
30      *  When the user reads from the clipboard, the main thread blocks
31      *  on this semaphore until we get an X11 SelectionNotify. Crude,
32      *  but effective. We know that only one thread will ever block on
33      *  this, since only one thread can ever be running JavaScript.
34      */
35     public static Semaphore waiting_for_selection_event = new Semaphore();
36
37     /** our local (in-process) copy of the clipboard */
38     public static String clipboard = null;
39
40     /** map from Window's (casted to jlong, wrapped in java.lang.Long) to X11Surface objects */
41     public static Hashtable windowToSurfaceMap = new Hashtable();
42
43
44     // General Methods ///////////////////////////////////////////////////////
45
46     protected String[] _listFonts() { return fontList; }
47     protected String getDescriptiveName() { return "GCJ Linux Binary"; }
48     protected Picture _createPicture(int[] data, int w, int h) { return new POSIX.X11Picture(data, w, h); }
49     protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new POSIX.X11DoubleBuffer(w, h); }
50     protected Surface _createSurface(Box b, boolean framed) { return new X11Surface(b, framed); }
51     protected boolean _needsAutoClick() { return true; }
52     protected native int _getScreenWidth();
53     protected native int _getScreenHeight();
54     protected native String _getClipBoard();
55     protected native void _setClipBoard(String s);
56     protected native int _stringWidth(String font, String text);
57     protected native int _getMaxAscent(String font);
58     protected native int _getMaxDescent(String font);
59     protected boolean _needsAutoDoubleClick() { return true; }
60     protected native void eventThread();
61     private native void natInit();
62
63     public POSIX() { }
64     public void init() {
65         natInit();
66         (new Thread() { public void run() { eventThread(); } }).start();
67         initFonts();
68     }
69
70     // X11Surface /////////////////////////////////////////////////////
71
72     /** Implements a Surface as an X11 Window */
73     public static class X11Surface extends Surface {
74         
75         gnu.gcj.RawData window;
76         gnu.gcj.RawData gc;
77         boolean framed = false;
78         Semaphore waitForCreation = new Semaphore();
79         
80         public void setIcon(Picture p) { /* FIXME */ }
81         public void setInvisible(boolean i) { /* FIXME */ }
82         public void _setMaximized(boolean m) { if (Log.on) Log.log(this, "POSIX/X11 can't maximize windows"); }
83         public native void _setMinimized(boolean b);
84         public native void setTitleBarText(String s);
85         public native void setSize(int w, int h);
86         public native void setLocation(int x, int y);
87         public native void natInit();
88         public native void toFront();
89         public native void toBack();
90         public native void syncCursor();
91         public native void _dispose();
92         public native void setLimits(int minw, int minh, int maxw, int maxh);
93         public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
94         public native void dispatchEvent(gnu.gcj.RawData ev);
95
96         public X11Surface(Box root, boolean framed) {
97             super(root);
98             this.framed = framed;
99             natInit();
100         }        
101     
102     }
103
104
105     // Our Subclass of Picture ///////////////////////////////////////////////
106
107     // FIXME: what if display server runs out of pixmap space? Think resource conservation...
108
109     /** Implements a Picture as an X11 Pixmap */
110     public static class X11Picture implements Picture {
111         
112         int width;
113         int height;
114         int[] data = null;
115         public X11DoubleBuffer doublebuf = null;
116
117         public int getWidth() { return width; }
118         public int getHeight() { return height; }
119
120         public X11Picture(int[] data, int w, int h) {
121             this.data = data;
122             this.width = w;
123             this.height = h;
124
125             // if we have any non-0x00, non-0xFF alphas, we can't double buffer ourselves
126             for(int i=0; i<w*h; i++)
127                 if ((data[i] & 0xFF000000) != 0xFF000000 && (data[i] & 0xFF000000) != 0x00)
128                     return;
129
130             X11DoubleBuffer b = new X11DoubleBuffer(w, h);
131             b.drawPicture(this, 0, 0);
132             b.createStipple(this);
133             doublebuf = b;
134         }
135
136     }
137
138     // FIXME: finalizer to free X resources
139     public static class X11DoubleBuffer implements DoubleBuffer {
140
141         int clipx, clipy, clipw, cliph;
142         int width;
143         int height;
144
145         /** DoubleBuffers of X11Pictures can have stipples -- the stipple of the Picture */
146         RawData stipple = null;
147
148         /** Sets the DoubleBuffer's internal stipple to the alpha==0x00 regions of xpi */
149         public native void createStipple(X11Picture xpi);
150
151         static int old_shmsize = 0;
152         static RawData shm_ximage;
153         static int shmsegs = 0;
154         int force_slowpath = 0;
155         RawData mxi = null;
156         int shared_pixmap = 0;
157
158         boolean usePixmap = false;
159         
160         /** Pixmap (if any) representing this Picture */
161         RawData pm;
162
163         /** Graphics Context on pm (never changes, so it's fast) */
164         RawData gc;
165
166         /** Graphics Context on pm, use this one if you need a clip/stipple */
167         RawData clipped_gc;
168
169         /** DoubleBuffer mode */
170         public X11DoubleBuffer(int w, int h) {
171             width = clipw = w;
172             height = cliph = h;
173             clipx = clipy = 0;
174             shared_pixmap = 1;
175             natInit();
176         }
177
178         public void setClip(int x, int y, int x2, int y2) {
179             clipx = x; if (clipx < 0) clipx = 0;
180             clipy = y; if (clipy < 0) clipy = 0;
181             clipw = x2 - x; if (clipw < 0) clipw = 0;
182             cliph = y2 - y; if (cliph < 0) cliph = 0;
183         }
184         
185         public void drawPicture(Picture source, int x, int y) {
186             drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
187         }
188
189         public int getWidth() { return width; }
190         public int getHeight() { return height; }
191         public native void natInit();
192         public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
193         public native void fillRect(int x, int y, int x2, int y2, int color);
194         public native void drawString(String font, String text, int x, int y, int color);
195
196     }
197
198
199     // Font Handling ////////////////////////////////////////////////////////////////////
200
201     static String[] fontList = null;
202
203     /** hashtable of all built in X11 fonts; key is XWT font spec, value is X11 font string */
204     static Hashtable nativeFontList = new Hashtable();
205
206     /** cache of all already-looked-up X11 fonts; key is XWT font name, value is a WrappedRawData */
207     static Hashtable xwtFontToFontStruct = new Hashtable();
208
209     /** dumps a list of X11 font strings */
210     private native String[] listNativeFonts();
211
212     /** load native font list */
213     public void initFonts() {
214         // use the font list to build nativeFontList
215         String[] fonts = listNativeFonts();
216         for(int k=0; k<fonts.length; k++) {
217             String s = fonts[k].toLowerCase();
218             StringTokenizer st = new StringTokenizer(s, "-", false);
219             String[] font = new String[st.countTokens()];
220             
221             try {
222                 for(int i=0; st.hasMoreTokens(); i++) font[i] = st.nextToken();
223                 String name = font[1];
224                 String size = font[6];
225                 String slant = font[3].equals("i") ? "i" : "";
226                 String bold = font[2].equals("bold") ? "b" : "";
227                 String tail = s.substring(1 + font[0].length() + 1 + font[1].length() + 1 + font[2].length() + 1 +
228                                           font[3].length() + 1 + font[4].length() + 1);
229                 
230                 if (bold.equals("*") && slant.equals("*")) {
231                     nativeFontList.put(name + size, font[0] + "-" + font[1] + "-regular-r-" + font[4] + "-" + tail);
232                     nativeFontList.put(name + size + "b", font[0] + "-" + font[1] + "-bold-r-" + font[4] + "-" + tail);
233                     nativeFontList.put(name + size + "i", font[0] + "-" + font[1] + "-regular-i-" + font[4] + "-" + tail);
234                     nativeFontList.put(name + size + "bi", font[0] + "-" + font[1] + "-bold-i-" + font[4] + "-" + tail);
235                     
236                 } else if (bold.equals("*")) {
237                     nativeFontList.put(name + size + slant, font[0] + "-" + font[1] + "-regular-" + font[3] + "-" + font[4] + "-" + tail);
238                     nativeFontList.put(name + size + "b" + slant, font[0] + "-" + font[1] + "-bold-" + font[3] + "-" + font[4] + "-" + tail);
239                     
240                 } else if (slant.equals("*")) {
241                     nativeFontList.put(name + size + bold, font[0] + "-" + font[1] + "-" + font[2] + "-r-" + font[4] + "-" + tail);
242                     nativeFontList.put(name + size + bold + "i", font[0] + "-" + font[1] + "-" + font[2] + "-i-" + font[4] + "-" + tail);
243                     
244                 } else {
245                     nativeFontList.put(name + size + bold + slant, s);
246                     
247                 }
248             } catch (ArrayIndexOutOfBoundsException e) {
249                 if (Log.on) Log.log(this, "skipping incomplete font string " + s);
250                 continue;
251             }
252         }
253         fontList = new String[nativeFontList.size()];
254         Enumeration e = nativeFontList.keys();
255         for(int i=0; e.hasMoreElements(); i++) fontList[i] = (String)e.nextElement();
256     }
257
258     /** so we can put XFontStruct's into Hashtables */
259     private static class WrappedRawData {
260         public RawData wrapee = null;
261         public WrappedRawData(RawData r) { wrapee = r; }
262     }
263
264     /** translates an X11 font string into an XFontStruct* */
265     public static native gnu.gcj.RawData fontStringToStruct(String s);
266
267     /** translates an XWT font string into an XFontStruct*, performing caching as well */
268     public static RawData fontToXFont(String s) {
269         if (s == null) s = "sansserif";
270         s = s.toLowerCase();
271         
272         WrappedRawData wrap = (WrappedRawData)xwtFontToFontStruct.get(s);
273         if (wrap != null) return wrap.wrapee;
274
275         String bestmatch = "";
276         int metric = -1 * Integer.MAX_VALUE;
277         ParsedFont arg = new ParsedFont(s);
278         ParsedFont pf = new ParsedFont();
279
280         if (arg.size == -1) arg.size = 10;
281         Enumeration e = nativeFontList.keys();
282         while(e.hasMoreElements()) {
283             String jfont = (String)e.nextElement();
284             pf.parse(jfont);
285             int thismetric = 0;
286             if (!pf.name.equals(arg.name)) {
287                 if (pf.name.equals("lucidabright") && arg.name.equals("serif")) thismetric -= 1000;
288                 else if (pf.name.equals("times") && arg.name.equals("serif")) thismetric -= 2000;
289                 else if (pf.name.equals("helvetica") && arg.name.equals("sansserif")) thismetric -= 1000;
290                 else if (pf.name.equals("courier") && arg.name.equals("monospaced")) thismetric -= 1000;
291                 else if (pf.name.equals("lucida") && arg.name.equals("dialog")) thismetric -= 1000;
292                 else if (pf.name.equals("helvetica") && arg.name.equals("dialog")) thismetric -= 2000;
293                 else if (pf.name.equals("fixed") && arg.name.equals("tty")) thismetric -= 1000;
294                 else if (pf.name.equals("sansserif")) thismetric -= 4000;
295                 else thismetric -= 4004;
296             }
297             if (pf.size != 0) thismetric -= Math.abs(pf.size - arg.size) * 4;
298             if (pf.bold != arg.bold) thismetric -= 1;
299             if (pf.italic != arg.italic) thismetric -= 1;
300             if (thismetric > metric) {
301                 metric = thismetric;
302                 bestmatch = jfont;
303             }
304         }
305         
306         pf.parse(bestmatch);
307         String target = (String)nativeFontList.get(bestmatch);
308         if (pf.size == 0) {
309             int i = 0;
310             for(int j=0; j<6; j++) i = target.indexOf('-', i + 1);
311             target = target.substring(0, i + 1) + arg.size + target.substring(target.indexOf('-', i+1));
312         }
313         RawData ret = fontStringToStruct(target);
314         if (ret == null) ret = fontStringToStruct("fixed");
315         xwtFontToFontStruct.put(s, new WrappedRawData(ret));
316         return ret;
317     }
318
319
320 }