1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
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
14 import java.awt.image.*;
15 import gnu.gcj.RawData;
17 import java.lang.reflect.*;
20 import java.awt.peer.*;
21 import org.xwt.util.*;
24 /** Platform implementation for POSIX compliant operating systems with an X11 Server */
25 public class POSIX extends GCJ {
27 // Static Data ///////////////////////////////////////////////////////////
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.
35 public static Semaphore waiting_for_selection_event = new Semaphore();
37 /** our local (in-process) copy of the clipboard */
38 public static String clipboard = null;
40 /** map from Window's (casted to jlong, wrapped in java.lang.Long) to X11Surface objects */
41 public static Hashtable windowToSurfaceMap = new Hashtable();
44 // General Methods ///////////////////////////////////////////////////////
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();
66 (new Thread() { public void run() { eventThread(); } }).start();
70 // X11Surface /////////////////////////////////////////////////////
72 /** Implements a Surface as an X11 Window */
73 public static class X11Surface extends Surface {
75 gnu.gcj.RawData window;
77 boolean framed = false;
78 Semaphore waitForCreation = new Semaphore();
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);
96 public X11Surface(Box root, boolean framed) {
105 // Our Subclass of Picture ///////////////////////////////////////////////
107 // FIXME: what if display server runs out of pixmap space? Think resource conservation...
109 /** Implements a Picture as an X11 Pixmap */
110 public static class X11Picture implements Picture {
115 public X11DoubleBuffer doublebuf = null;
117 public int getWidth() { return width; }
118 public int getHeight() { return height; }
120 public X11Picture(int[] data, int w, int h) {
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)
130 X11DoubleBuffer b = new X11DoubleBuffer(w, h);
131 b.drawPicture(this, 0, 0);
132 b.createStipple(this);
138 // FIXME: finalizer to free X resources
139 public static class X11DoubleBuffer implements DoubleBuffer {
141 int clipx, clipy, clipw, cliph;
145 /** DoubleBuffers of X11Pictures can have stipples -- the stipple of the Picture */
146 RawData stipple = null;
148 /** Sets the DoubleBuffer's internal stipple to the alpha==0x00 regions of xpi */
149 public native void createStipple(X11Picture xpi);
151 static int old_shmsize = 0;
152 static RawData shm_ximage;
153 static int shmsegs = 0;
154 int force_slowpath = 0;
156 int shared_pixmap = 0;
158 boolean usePixmap = false;
160 /** Pixmap (if any) representing this Picture */
163 /** Graphics Context on pm (never changes, so it's fast) */
166 /** Graphics Context on pm, use this one if you need a clip/stipple */
169 /** DoubleBuffer mode */
170 public X11DoubleBuffer(int w, int h) {
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;
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());
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);
199 // Font Handling ////////////////////////////////////////////////////////////////////
201 static String[] fontList = null;
203 /** hashtable of all built in X11 fonts; key is XWT font spec, value is X11 font string */
204 static Hashtable nativeFontList = new Hashtable();
206 /** cache of all already-looked-up X11 fonts; key is XWT font name, value is a WrappedRawData */
207 static Hashtable xwtFontToFontStruct = new Hashtable();
209 /** dumps a list of X11 font strings */
210 private native String[] listNativeFonts();
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()];
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);
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);
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);
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);
245 nativeFontList.put(name + size + bold + slant, s);
248 } catch (ArrayIndexOutOfBoundsException e) {
249 if (Log.on) Log.log(this, "skipping incomplete font string " + s);
253 fontList = new String[nativeFontList.size()];
254 Enumeration e = nativeFontList.keys();
255 for(int i=0; e.hasMoreElements(); i++) fontList[i] = (String)e.nextElement();
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; }
264 /** translates an X11 font string into an XFontStruct* */
265 public static native gnu.gcj.RawData fontStringToStruct(String s);
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";
272 WrappedRawData wrap = (WrappedRawData)xwtFontToFontStruct.get(s);
273 if (wrap != null) return wrap.wrapee;
275 String bestmatch = "";
276 int metric = -1 * Integer.MAX_VALUE;
277 ParsedFont arg = new ParsedFont(s);
278 ParsedFont pf = new ParsedFont();
280 if (arg.size == -1) arg.size = 10;
281 Enumeration e = nativeFontList.keys();
282 while(e.hasMoreElements()) {
283 String jfont = (String)e.nextElement();
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;
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) {
307 String target = (String)nativeFontList.get(bestmatch);
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));
313 RawData ret = fontStringToStruct(target);
314 if (ret == null) ret = fontStringToStruct("fixed");
315 xwtFontToFontStruct.put(s, new WrappedRawData(ret));