1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
5 import java.awt.image.*;
6 import gnu.gcj.RawData;
8 import java.lang.reflect.*;
11 import java.awt.peer.*;
12 import org.xwt.util.*;
15 /** Platform implementation for POSIX compliant operating systems with an X11 Server */
16 public class X11 extends POSIX {
18 // Static Data ///////////////////////////////////////////////////////////
21 * When the user reads from the clipboard, the main thread blocks
22 * on this semaphore until we get an X11 SelectionNotify. Crude,
23 * but effective. We know that only one thread will ever block on
24 * this, since only one thread can ever be running JavaScript.
26 public static Semaphore waiting_for_selection_event = new Semaphore();
28 /** our local (in-process) copy of the clipboard */
29 public static String clipboard = null;
31 /** map from Window's (casted to jlong, wrapped in java.lang.Long) to X11Surface objects */
32 public static Hashtable windowToSurfaceMap = new Hashtable();
35 // General Methods ///////////////////////////////////////////////////////
37 protected String _getAltKeyName() { return System.getProperty("os.name", "").indexOf("SunOS") != -1 ? "Meta" : "Alt"; }
38 protected String[] _listFonts() { return fontList; }
40 protected Picture _createPicture(int[] data, int w, int h) { return new X11Picture(data, w, h); }
41 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new X11DoubleBuffer(w, h); }
42 protected Surface _createSurface(Box b, boolean framed) { return new X11Surface(b, framed); }
43 protected boolean _needsAutoClick() { return true; }
44 protected native int _getScreenWidth();
45 protected native int _getScreenHeight();
46 protected native String _getClipBoard();
47 protected native void _setClipBoard(String s);
48 protected native int _stringWidth(String font, String text);
49 protected native int _getMaxAscent(String font);
50 protected native int _getMaxDescent(String font);
51 protected boolean _needsAutoDoubleClick() { return true; }
52 protected native void eventThread();
53 private native void natInit();
58 (new Thread() { public void run() { eventThread(); } }).start();
62 // X11Surface /////////////////////////////////////////////////////
64 /** Implements a Surface as an X11 Window */
65 public static class X11Surface extends Surface {
67 gnu.gcj.RawData window;
69 boolean framed = false;
70 Semaphore waitForCreation = new Semaphore();
72 public native void setInvisible(boolean i);
73 public void _setMaximized(boolean m) { if (Log.on) Log.log(this, "X11 can't maximize windows"); }
74 public native void setIcon(Picture p);
75 public native void _setMinimized(boolean b);
76 public native void setTitleBarText(String s);
77 public native void setSize(int w, int h);
78 public native void setLocation(int x, int y);
79 public native void natInit();
80 public native void toFront();
81 public native void toBack();
82 public native void syncCursor();
83 public native void _dispose();
84 public native void setLimits(int minw, int minh, int maxw, int maxh);
85 public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
86 public native void dispatchEvent(gnu.gcj.RawData ev);
88 public X11Surface(Box root, boolean framed) {
97 // Our Subclass of Picture ///////////////////////////////////////////////
100 * Implements a Picture. No special X11 structure is created
101 * unless the image has no alpha (in which case a
102 * non-shared-pixmap DoubleBuffer is created), or all-or-nothing
103 * alpha (in which case a non-shared-pixmap DoubleBuffer with a
104 * stipple bitmap is created).
106 public static class X11Picture extends Picture {
111 public X11DoubleBuffer doublebuf = null;
113 public int getWidth() { return width; }
114 public int getHeight() { return height; }
116 public X11Picture(int[] data, int w, int h) {
120 boolean needsStipple = false;
122 // if we have any non-0x00, non-0xFF alphas, we can't double buffer ourselves
123 for(int i=0; i<w*h; i++)
124 if ((data[i] & 0xFF000000) == 0xFF000000)
126 else if ((data[i] & 0xFF000000) != 0x00)
129 buildDoubleBuffer(needsStipple);
132 void buildDoubleBuffer(boolean needsStipple) {
133 if (doublebuf != null) return;
134 // no point in using a shared pixmap since we'll only write to this image once
135 X11DoubleBuffer b = new X11DoubleBuffer(width, height, false);
136 b.drawPicture(this, 0, 0);
137 if (needsStipple) b.createStipple(this);
144 * An X11DoubleBuffer is implemented as an X11 pixmap. "Normal"
145 * DoubleBuffers will use XShm shared pixmaps if
146 * available. X11DoubleBuffers created to accelerate Pictures
147 * with all-or-nothing alpha will not use shared pixmaps, however
148 * (since they are only written to once.
150 public static class X11DoubleBuffer extends DoubleBuffer {
152 int clipx, clipy, clipw, cliph;
156 /** DoubleBuffers of X11Pictures can have stipples -- the stipple of the Picture */
157 RawData stipple = null;
159 /** Sets the DoubleBuffer's internal stipple to the alpha==0x00 regions of xpi */
160 public native void createStipple(X11Picture xpi);
162 RawData pm; // Pixmap (if any) representing this Picture
163 boolean shared_pixmap = false; // true if pm is a ShmPixmap
164 RawData fake_ximage = null; // a 'fake' XImage corresponding to the shared pixmap; gives us the address and depth parameters
165 RawData shm_segment = null; // XShmSegmentInfo
167 RawData gc; // Graphics Context on pm (never changes, so it's fast)
168 RawData clipped_gc; // Graphics Context on pm, use this one if you need a clip/stipple
170 /** DoubleBuffer mode */
171 public X11DoubleBuffer(int w, int h) { this(w, h, true); }
172 public X11DoubleBuffer(int w, int h, boolean shared_pixmap) {
176 this.shared_pixmap = shared_pixmap;
180 public void setClip(int x, int y, int x2, int y2) {
181 clipx = x; if (clipx < 0) clipx = 0;
182 clipy = y; if (clipy < 0) clipy = 0;
183 clipw = x2 - x; if (clipw < 0) clipw = 0;
184 cliph = y2 - y; if (cliph < 0) cliph = 0;
187 public void drawPicture(Picture source, int x, int y) {
188 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
191 public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
192 if (!(dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1) && ((X11Picture)source).doublebuf != null)
193 fastDrawPicture(source, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
195 slowDrawPicture(source, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
198 /** fast path for image drawing (no scaling, all-or-nothing alpha) */
199 public native void fastDrawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
201 /** slow path for image drawing */
202 public native void slowDrawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
204 public int getWidth() { return width; }
205 public int getHeight() { return height; }
206 public native void natInit();
207 public native void fillRect(int x, int y, int x2, int y2, int color);
208 public native void drawString(String font, String text, int x, int y, int color);
209 public native void finalize();
214 // Font Handling ////////////////////////////////////////////////////////////////////
216 static String[] fontList = null;
218 /** hashtable of all built in X11 fonts; key is XWT font spec, value is X11 font string */
219 static Hashtable nativeFontList = new Hashtable();
221 /** cache of all already-looked-up X11 fonts; key is XWT font name, value is a WrappedRawData */
222 static Hashtable xwtFontToFontStruct = new Hashtable();
224 /** dumps a list of X11 font strings */
225 private native String[] listNativeFonts();
227 /** load native font list */
228 public void initFonts() {
229 // use the font list to build nativeFontList
230 String[] fonts = listNativeFonts();
232 Vector v = new Vector();
233 for(int k=0; k<fonts.length; k++) {
235 String s = fonts[k].toLowerCase();
239 s = s.substring(s.indexOf('-') + 1);
240 while (s.indexOf('-') != -1) {
241 v.addElement(s.substring(0, s.indexOf('-')));
242 s = s.substring(s.indexOf('-') + 1);
245 String[] font = new String[v.size()];
248 // limit to iso8559 until we can do I18N properly....
249 if (font.length > 12) {
250 if (!font[12].equals("iso8859") && !font[12].equals("")) continue;
251 if (font.length < 14 || !font[13].equals("1")) continue;
254 String name = font[1];
255 String size = font[6];
256 String slant = (font[3].equals("i") || font[3].equals("o")) ? "i" : "";
257 String bold = font[2].equals("bold") ? "b" : "";
258 String tail = s2.substring(1 + font[0].length() + 1 + font[1].length() + 1 + font[2].length() + 1 +
259 font[3].length() + 1 + font[4].length() + 1);
261 if (bold.equals("*") && slant.equals("*")) {
262 nativeFontList.put(name + size, font[0] + "-" + font[1] + "-regular-r-" + font[4] + "-" + tail);
263 nativeFontList.put(name + size + "b", font[0] + "-" + font[1] + "-bold-r-" + font[4] + "-" + tail);
264 nativeFontList.put(name + size + "i", font[0] + "-" + font[1] + "-regular-i-" + font[4] + "-" + tail);
265 nativeFontList.put(name + size + "bi", font[0] + "-" + font[1] + "-bold-i-" + font[4] + "-" + tail);
267 } else if (bold.equals("*")) {
268 nativeFontList.put(name + size + slant, font[0] + "-" + font[1] + "-regular-" + font[3] + "-" + font[4] + "-" + tail);
269 nativeFontList.put(name + size + "b" + slant, font[0] + "-" + font[1] + "-bold-" + font[3] + "-" + font[4] + "-" + tail);
271 } else if (slant.equals("*")) {
272 nativeFontList.put(name + size + bold, font[0] + "-" + font[1] + "-" + font[2] + "-r-" + font[4] + "-" + tail);
273 nativeFontList.put(name + size + bold + "i", font[0] + "-" + font[1] + "-" + font[2] + "-i-" + font[4] + "-" + tail);
276 nativeFontList.put(name + size + bold + slant, s2);
279 } catch (ArrayIndexOutOfBoundsException e) {
280 if (Log.on) Log.log(this, "skipping incomplete font string " + s2);
284 fontList = new String[nativeFontList.size()];
285 Enumeration e = nativeFontList.keys();
286 for(int i=0; e.hasMoreElements(); i++) fontList[i] = (String)e.nextElement();
289 /** so we can put XFontStruct's into Hashtables */
290 private static class WrappedRawData {
291 public RawData wrapee = null;
292 public WrappedRawData(RawData r) { wrapee = r; }
295 /** translates an X11 font string into an XFontStruct* */
296 public static native gnu.gcj.RawData fontStringToStruct(String s);
298 /** translates an XWT font string into an XFontStruct*, performing caching as well */
299 public static RawData fontToXFont(String s) {
300 if (s == null) s = "sansserif";
303 WrappedRawData wrap = (WrappedRawData)xwtFontToFontStruct.get(s);
304 if (wrap != null) return wrap.wrapee;
306 String bestmatch = "";
307 int metric = -1 * Integer.MAX_VALUE;
308 ParsedFont arg = new ParsedFont(s);
309 ParsedFont pf = new ParsedFont();
311 if (arg.size == -1) arg.size = 10;
312 Enumeration e = nativeFontList.keys();
313 while(e.hasMoreElements()) {
314 String jfont = (String)e.nextElement();
317 if (!pf.name.equals(arg.name)) {
318 if (pf.name.equals("lucidabright") && arg.name.equals("serif")) thismetric -= 1000;
319 else if (pf.name.equals("times") && arg.name.equals("serif")) thismetric -= 2000;
320 else if (pf.name.equals("helvetica") && arg.name.equals("sansserif")) thismetric -= 1000;
321 else if (pf.name.equals("courier") && arg.name.equals("monospaced")) thismetric -= 1000;
322 else if (pf.name.equals("lucida") && arg.name.equals("dialog")) thismetric -= 1000;
323 else if (pf.name.equals("helvetica") && arg.name.equals("dialog")) thismetric -= 2000;
324 else if (pf.name.equals("fixed") && arg.name.equals("tty")) thismetric -= 1000;
325 else if (pf.name.equals("sansserif")) thismetric -= 4000;
326 else thismetric -= 4004;
328 if (pf.size != 0) thismetric -= Math.abs(pf.size - arg.size) * 4;
329 if (pf.bold != arg.bold) thismetric -= 1;
330 if (pf.italic != arg.italic) thismetric -= 1;
331 if (thismetric > metric) {
338 String target = (String)nativeFontList.get(bestmatch);
341 for(int j=0; j<6; j++) i = target.indexOf('-', i + 1);
342 target = target.substring(0, i + 1) + arg.size + target.substring(target.indexOf('-', i+1));
344 if (Log.on) Log.log(X11.class, "mapping font \"" + s + "\" to \"" + target + "\"");
345 RawData ret = fontStringToStruct(target);
346 if (ret == null) ret = fontStringToStruct("fixed");
347 xwtFontToFontStruct.put(s, new WrappedRawData(ret));