1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
4 import gnu.gcj.RawData;
7 import org.ibex.util.*;
10 /** Platform implementation for POSIX compliant operating systems with an X11 Server */
11 public class X11 extends POSIX {
13 // Static Data ///////////////////////////////////////////////////////////
16 * When the user reads from the clipboard, the main thread blocks
17 * on this semaphore until we get an X11 SelectionNotify. Crude,
18 * but effective. We know that only one thread will ever block on
19 * this, since only one thread can ever be running JavaScript.
21 public static Semaphore waiting_for_selection_event = new Semaphore();
23 /** our local (in-process) copy of the clipboard */
24 public static String clipboard = null;
26 /** map from Window's (casted to jlong, wrapped in java.lang.Long) to X11Surface objects */
27 public static Hashtable windowToSurfaceMap = new Hashtable();
30 // General Methods ///////////////////////////////////////////////////////
32 protected String _getAltKeyName() { return System.getProperty("os.name", "").indexOf("SunOS") != -1 ? "Meta" : "Alt"; }
34 protected Picture _createPicture(JS r) { return new X11Picture(r); }
35 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new X11PixelBuffer(w, h); }
36 protected Surface _createSurface(Box b, boolean framed) { return new X11Surface(b, framed); }
37 protected boolean _needsAutoClick() { return true; }
38 protected native int _getScreenWidth();
39 protected native int _getScreenHeight();
40 protected native String _getClipBoard();
41 protected native void _setClipBoard(String s);
42 protected boolean _needsAutoDoubleClick() { return true; }
43 protected native String _fileDialog(String suggestedFileName, boolean write);
44 protected native void eventThread();
45 private native void natInit();
47 public void postInit() {
49 (new Thread() { public void run() { eventThread(); } }).start();
52 // X11Surface /////////////////////////////////////////////////////
54 /** Implements a Surface as an X11 Window */
55 public static class X11Surface extends Surface.DoubleBufferedSurface {
57 gnu.gcj.RawData window;
59 boolean framed = false;
60 Semaphore waitForCreation = new Semaphore();
62 public native void setInvisible(boolean i);
63 public void _setMaximized(boolean m) { if (Log.on) Log.warn(this, "X11 can't maximize windows"); }
64 public native void setIcon(Picture p);
65 public native void _setMinimized(boolean b);
66 public native void setTitleBarText(String s);
67 public native void _setSize(int w, int h);
68 public native void setLocation();
69 public native void natInit();
70 public native void toFront();
71 public native void toBack();
72 public native void syncCursor();
73 public native void _dispose();
74 public native void setLimits(int minw, int minh, int maxw, int maxh);
75 public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
76 public native void dispatchEvent(gnu.gcj.RawData ev);
77 public void setMinimumSize(int minx, int miny, boolean resizable) {
78 setLimits(minx, miny, resizable ? Short.MAX_VALUE : minx, resizable ? Short.MAX_VALUE : miny);
80 public X11Surface(Box root, boolean framed) {
89 // Our Subclass of Picture ///////////////////////////////////////////////
92 * Implements a Picture. No special X11 structure is created
93 * unless the image has no alpha (in which case a
94 * non-shared-pixmap PixelBuffer is created), or all-or-nothing
95 * alpha (in which case a non-shared-pixmap PixelBuffer with a
96 * stipple bitmap is created).
98 public static class X11Picture extends Picture {
100 public X11PixelBuffer doublebuf = null;
102 public int getWidth() { return width; }
103 public int getHeight() { return height; }
105 boolean initialized = false;
106 public X11Picture(JS r) { super(r); }
108 if (initialized) return;
110 boolean needsStipple = false;
112 // if we have any non-0x00, non-0xFF alphas, we can't double buffer ourselves
113 for(int i=0; i<width*height; i++)
114 if ((data[i] & 0xFF000000) == 0xFF000000)
116 else if ((data[i] & 0xFF000000) != 0x00)
119 buildPixelBuffer(needsStipple);
122 void buildPixelBuffer(boolean needsStipple) {
123 if (doublebuf != null) return;
124 // no point in using a shared pixmap since we'll only write to this image once
125 X11PixelBuffer b = new X11PixelBuffer(width, height, false);
126 b.drawPicture(this, 0, 0, 0, 0, width, height);
127 if (needsStipple) b.createStipple(this);
133 * An X11PixelBuffer is implemented as an X11 pixmap. "Normal"
134 * PixelBuffers will use XShm shared pixmaps if
135 * available. X11PixelBuffers created to accelerate Pictures
136 * with all-or-nothing alpha will not use shared pixmaps, however
137 * (since they are only written to once.
139 public static class X11PixelBuffer extends PixelBuffer {
141 int clipx, clipy, clipw, cliph;
145 /** PixelBuffers of X11Pictures can have stipples -- the stipple of the Picture */
146 RawData stipple = null;
148 /** Sets the PixelBuffer's internal stipple to the alpha==0x00 regions of xpi */
149 public native void createStipple(X11Picture xpi);
151 RawData pm; // Pixmap (if any) representing this Picture
152 boolean shared_pixmap = false; // true if pm is a ShmPixmap
153 RawData fake_ximage = null; // a 'fake' XImage corresponding to the shared pixmap; gives us the address and depth parameters
154 RawData shm_segment = null; // XShmSegmentInfo
156 RawData gc; // Graphics Interpreter on pm (never changes, so it's fast)
157 RawData clipped_gc; // Graphics Interpreter on pm, use this one if you need a clip/stipple
159 /** PixelBuffer mode */
160 public X11PixelBuffer(int w, int h) { this(w, h, true); }
161 public X11PixelBuffer(int w, int h, boolean shared_pixmap) {
165 this.shared_pixmap = shared_pixmap;
169 public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
170 cx1 = Math.max(dx, cx1);
171 cy1 = Math.max(dy, cy1);
172 cx2 = Math.min(dx + source.width, cx2);
173 cy2 = Math.min(dy + source.height, cy2);
174 if (cx1 >= cx2 || cy1 >= cy2) return;
175 X11Picture pic = (X11Picture)((Platform.DefaultGlyph)source).getPicture();
177 slowDrawPicture(pic, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
179 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
180 cx1 = Math.max(dx, cx1);
181 cy1 = Math.max(dy, cy1);
182 cx2 = Math.min(dx + source.width, cx2);
183 cy2 = Math.min(dy + source.height, cy2);
184 if (cx1 >= cx2 || cy1 >= cy2) return;
185 ((X11Picture)source).init();
186 if (((X11Picture)source).doublebuf != null)
187 fastDrawPicture(source, dx, dy, cx1, cy1, cx2, cy2);
189 slowDrawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
192 /** fast path for image drawing (no scaling, all-or-nothing alpha) */
193 public native void fastDrawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2);
195 /** slow path for image drawing */
196 public native void slowDrawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb, boolean alphaOnly);
198 public int getWidth() { return width; }
199 public int getHeight() { return height; }
200 public native void natInit();
201 public native void fillRect(int x, int y, int x2, int y2, int color);
202 public native void finalize();
204 // FIXME: try to use os acceleration
205 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
206 if (x1 == x3 && x2 == x4) {
207 fillRect(x1, y1, x4, y2, argb);
208 } else for(int y=y1; y<y2; y++) {
209 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
210 int _y1 = (int)Math.floor(y);
211 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
212 int _y2 = (int)Math.floor(y) + 1;
213 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
214 fillRect(_x1, _y1, _x2, _y2, argb);