1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
4 import gnu.gcj.RawData;
6 import java.lang.reflect.*;
12 /** Platform implementation for POSIX compliant operating systems with an X11 Server */
13 public class X11 extends POSIX {
15 // Static Data ///////////////////////////////////////////////////////////
18 * When the user reads from the clipboard, the main thread blocks
19 * on this semaphore until we get an X11 SelectionNotify. Crude,
20 * but effective. We know that only one thread will ever block on
21 * this, since only one thread can ever be running JavaScript.
23 public static Semaphore waiting_for_selection_event = new Semaphore();
25 /** our local (in-process) copy of the clipboard */
26 public static String clipboard = null;
28 /** map from Window's (casted to jlong, wrapped in java.lang.Long) to X11Surface objects */
29 public static Hashtable windowToSurfaceMap = new Hashtable();
32 // General Methods ///////////////////////////////////////////////////////
34 protected String _getAltKeyName() { return System.getProperty("os.name", "").indexOf("SunOS") != -1 ? "Meta" : "Alt"; }
35 protected String[] _listFonts() { return fontList; }
37 protected Picture _createPicture(int[] data, int w, int h) { return new X11Picture(data, w, h); }
38 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new X11PixelBuffer(w, h); }
39 protected Surface _createSurface(Box b, boolean framed) { return new X11Surface(b, framed); }
40 protected boolean _needsAutoClick() { return true; }
41 protected native int _getScreenWidth();
42 protected native int _getScreenHeight();
43 protected native String _getClipBoard();
44 protected native void _setClipBoard(String s);
45 protected native int _stringWidth(String font, String text);
46 protected native int _getMaxAscent(String font);
47 protected native int _getMaxDescent(String font);
48 protected boolean _needsAutoDoubleClick() { return true; }
49 protected native void eventThread();
50 private native void natInit();
55 (new Thread() { public void run() { eventThread(); } }).start();
59 // X11Surface /////////////////////////////////////////////////////
61 /** Implements a Surface as an X11 Window */
62 public static class X11Surface extends Surface {
64 gnu.gcj.RawData window;
66 boolean framed = false;
67 Semaphore waitForCreation = new Semaphore();
69 public native void setInvisible(boolean i);
70 public void _setMaximized(boolean m) { if (Log.on) Log.log(this, "X11 can't maximize windows"); }
71 public native void setIcon(Picture p);
72 public native void _setMinimized(boolean b);
73 public native void setTitleBarText(String s);
74 public native void setSize(int w, int h);
75 public native void setLocation(int x, int y);
76 public native void natInit();
77 public native void toFront();
78 public native void toBack();
79 public native void syncCursor();
80 public native void _dispose();
81 public native void setLimits(int minw, int minh, int maxw, int maxh);
82 public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
83 public native void dispatchEvent(gnu.gcj.RawData ev);
85 public X11Surface(Box root, boolean framed) {
94 // Our Subclass of Picture ///////////////////////////////////////////////
97 * Implements a Picture. No special X11 structure is created
98 * unless the image has no alpha (in which case a
99 * non-shared-pixmap PixelBuffer is created), or all-or-nothing
100 * alpha (in which case a non-shared-pixmap PixelBuffer with a
101 * stipple bitmap is created).
103 public static class X11Picture extends Picture {
108 public X11PixelBuffer doublebuf = null;
110 public int getWidth() { return width; }
111 public int getHeight() { return height; }
113 public X11Picture(int[] data, int w, int h) {
117 boolean needsStipple = false;
119 // if we have any non-0x00, non-0xFF alphas, we can't double buffer ourselves
120 for(int i=0; i<w*h; i++)
121 if ((data[i] & 0xFF000000) == 0xFF000000)
123 else if ((data[i] & 0xFF000000) != 0x00)
126 buildPixelBuffer(needsStipple);
129 void buildPixelBuffer(boolean needsStipple) {
130 if (doublebuf != null) return;
131 // no point in using a shared pixmap since we'll only write to this image once
132 X11PixelBuffer b = new X11PixelBuffer(width, height, false);
133 b.drawPicture(this, 0, 0);
134 if (needsStipple) b.createStipple(this);
141 * An X11PixelBuffer is implemented as an X11 pixmap. "Normal"
142 * PixelBuffers will use XShm shared pixmaps if
143 * available. X11PixelBuffers created to accelerate Pictures
144 * with all-or-nothing alpha will not use shared pixmaps, however
145 * (since they are only written to once.
147 public static class X11PixelBuffer extends PixelBuffer {
149 int clipx, clipy, clipw, cliph;
153 /** PixelBuffers of X11Pictures can have stipples -- the stipple of the Picture */
154 RawData stipple = null;
156 /** Sets the PixelBuffer's internal stipple to the alpha==0x00 regions of xpi */
157 public native void createStipple(X11Picture xpi);
159 RawData pm; // Pixmap (if any) representing this Picture
160 boolean shared_pixmap = false; // true if pm is a ShmPixmap
161 RawData fake_ximage = null; // a 'fake' XImage corresponding to the shared pixmap; gives us the address and depth parameters
162 RawData shm_segment = null; // XShmSegmentInfo
164 RawData gc; // Graphics Context on pm (never changes, so it's fast)
165 RawData clipped_gc; // Graphics Context on pm, use this one if you need a clip/stipple
167 /** PixelBuffer mode */
168 public X11PixelBuffer(int w, int h) { this(w, h, true); }
169 public X11PixelBuffer(int w, int h, boolean shared_pixmap) {
173 this.shared_pixmap = shared_pixmap;
177 public void setClip(int x, int y, int x2, int y2) {
178 clipx = x; if (clipx < 0) clipx = 0;
179 clipy = y; if (clipy < 0) clipy = 0;
180 clipw = x2 - x; if (clipw < 0) clipw = 0;
181 cliph = y2 - y; if (cliph < 0) cliph = 0;
184 public void drawPicture(Picture source, int x, int y) {
185 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
188 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
189 if (!(dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1) && ((X11Picture)source).doublebuf != null)
190 fastDrawPicture(source, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
192 slowDrawPicture(source, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
195 /** fast path for image drawing (no scaling, all-or-nothing alpha) */
196 public native void fastDrawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
198 /** slow path for image drawing */
199 public native void slowDrawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
201 public int getWidth() { return width; }
202 public int getHeight() { return height; }
203 public native void natInit();
204 public native void fillRect(int x, int y, int x2, int y2, int color);
205 public native void drawString(String font, String text, int x, int y, int color);
206 public native void finalize();
211 // Font Handling ////////////////////////////////////////////////////////////////////
213 static String[] fontList = null;
215 /** hashtable of all built in X11 fonts; key is XWT font spec, value is X11 font string */
216 static Hashtable nativeFontList = new Hashtable();
218 /** cache of all already-looked-up X11 fonts; key is XWT font name, value is a WrappedRawData */
219 static Hashtable xwtFontToFontStruct = new Hashtable();
221 /** dumps a list of X11 font strings */
222 private native String[] listNativeFonts();
224 /** load native font list */
225 public void initFonts() {
226 // use the font list to build nativeFontList
227 String[] fonts = listNativeFonts();
229 Vector v = new Vector();
230 for(int k=0; k<fonts.length; k++) {
232 String s = fonts[k].toLowerCase();
236 s = s.substring(s.indexOf('-') + 1);
237 while (s.indexOf('-') != -1) {
238 v.addElement(s.substring(0, s.indexOf('-')));
239 s = s.substring(s.indexOf('-') + 1);
242 String[] font = new String[v.size()];
245 // limit to iso8559 until we can do I18N properly....
246 if (font.length > 12) {
247 if (!font[12].equals("iso8859") && !font[12].equals("")) continue;
248 if (font.length < 14 || !font[13].equals("1")) continue;
251 String name = font[1];
252 String size = font[6];
253 String slant = (font[3].equals("i") || font[3].equals("o")) ? "i" : "";
254 String bold = font[2].equals("bold") ? "b" : "";
255 String tail = s2.substring(1 + font[0].length() + 1 + font[1].length() + 1 + font[2].length() + 1 +
256 font[3].length() + 1 + font[4].length() + 1);
258 if (bold.equals("*") && slant.equals("*")) {
259 nativeFontList.put(name + size, font[0] + "-" + font[1] + "-regular-r-" + font[4] + "-" + tail);
260 nativeFontList.put(name + size + "b", font[0] + "-" + font[1] + "-bold-r-" + font[4] + "-" + tail);
261 nativeFontList.put(name + size + "i", font[0] + "-" + font[1] + "-regular-i-" + font[4] + "-" + tail);
262 nativeFontList.put(name + size + "bi", font[0] + "-" + font[1] + "-bold-i-" + font[4] + "-" + tail);
264 } else if (bold.equals("*")) {
265 nativeFontList.put(name + size + slant, font[0] + "-" + font[1] + "-regular-" + font[3] + "-" + font[4] + "-" + tail);
266 nativeFontList.put(name + size + "b" + slant, font[0] + "-" + font[1] + "-bold-" + font[3] + "-" + font[4] + "-" + tail);
268 } else if (slant.equals("*")) {
269 nativeFontList.put(name + size + bold, font[0] + "-" + font[1] + "-" + font[2] + "-r-" + font[4] + "-" + tail);
270 nativeFontList.put(name + size + bold + "i", font[0] + "-" + font[1] + "-" + font[2] + "-i-" + font[4] + "-" + tail);
273 nativeFontList.put(name + size + bold + slant, s2);
276 } catch (ArrayIndexOutOfBoundsException e) {
277 if (Log.on) Log.log(this, "skipping incomplete font string " + s2);
281 fontList = new String[nativeFontList.size()];
282 Enumeration e = nativeFontList.keys();
283 for(int i=0; e.hasMoreElements(); i++) fontList[i] = (String)e.nextElement();
286 /** so we can put XFontStruct's into Hashtables */
287 private static class WrappedRawData {
288 public RawData wrapee = null;
289 public WrappedRawData(RawData r) { wrapee = r; }
292 /** translates an X11 font string into an XFontStruct* */
293 public static native gnu.gcj.RawData fontStringToStruct(String s);
295 /** translates an XWT font string into an XFontStruct*, performing caching as well */
296 public static RawData fontToXFont(String s) {
297 if (s == null) s = "sansserif";
300 WrappedRawData wrap = (WrappedRawData)xwtFontToFontStruct.get(s);
301 if (wrap != null) return wrap.wrapee;
303 String bestmatch = "";
304 int metric = -1 * Integer.MAX_VALUE;
305 ParsedFont arg = new ParsedFont(s);
306 ParsedFont pf = new ParsedFont();
308 if (arg.size == -1) arg.size = 10;
309 Enumeration e = nativeFontList.keys();
310 while(e.hasMoreElements()) {
311 String jfont = (String)e.nextElement();
314 if (!pf.name.equals(arg.name)) {
315 if (pf.name.equals("lucidabright") && arg.name.equals("serif")) thismetric -= 1000;
316 else if (pf.name.equals("times") && arg.name.equals("serif")) thismetric -= 2000;
317 else if (pf.name.equals("helvetica") && arg.name.equals("sansserif")) thismetric -= 1000;
318 else if (pf.name.equals("courier") && arg.name.equals("monospaced")) thismetric -= 1000;
319 else if (pf.name.equals("lucida") && arg.name.equals("dialog")) thismetric -= 1000;
320 else if (pf.name.equals("helvetica") && arg.name.equals("dialog")) thismetric -= 2000;
321 else if (pf.name.equals("fixed") && arg.name.equals("tty")) thismetric -= 1000;
322 else if (pf.name.equals("sansserif")) thismetric -= 4000;
323 else thismetric -= 4004;
325 if (pf.size != 0) thismetric -= Math.abs(pf.size - arg.size) * 4;
326 if (pf.bold != arg.bold) thismetric -= 1;
327 if (pf.italic != arg.italic) thismetric -= 1;
328 if (thismetric > metric) {
335 String target = (String)nativeFontList.get(bestmatch);
338 for(int j=0; j<6; j++) i = target.indexOf('-', i + 1);
339 target = target.substring(0, i + 1) + arg.size + target.substring(target.indexOf('-', i+1));
341 if (Log.on) Log.log(X11.class, "mapping font \"" + s + "\" to \"" + target + "\"");
342 RawData ret = fontStringToStruct(target);
343 if (ret == null) ret = fontStringToStruct("fixed");
344 xwtFontToFontStruct.put(s, new WrappedRawData(ret));