1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
7 import gnu.gcj.RawData;
10 import org.ibex.util.*;
11 import org.ibex.graphics.*;
12 import org.ibex.core.*;
13 import org.ibex.net.*;
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"; }
39 protected Picture _createPicture(JS r) { return new X11Picture(r); }
40 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new X11PixelBuffer(w, h); }
41 protected Surface _createSurface(Box b, boolean framed) { return new X11Surface(b, framed); }
42 protected boolean _needsAutoClick() { return true; }
43 protected native int _getScreenWidth();
44 protected native int _getScreenHeight();
45 protected native String _getClipBoard();
46 protected native void _setClipBoard(String s);
47 protected boolean _needsAutoDoubleClick() { return true; }
48 protected native String _fileDialog(String suggestedFileName, boolean write);
49 protected native void eventThread();
50 private native void natInit();
52 public void postInit() {
54 (new Thread() { public void run() { eventThread(); } }).start();
57 // X11Surface /////////////////////////////////////////////////////
59 /** Implements a Surface as an X11 Window */
60 public static class X11Surface extends Surface.DoubleBufferedSurface {
62 gnu.gcj.RawData window;
64 boolean framed = false;
65 Semaphore waitForCreation = new Semaphore();
67 public native void setInvisible(boolean i);
68 public void _setMaximized(boolean m) { if (Log.on) Log.warn(this, "X11 can't maximize windows"); }
69 public native void setIcon(Picture p);
70 public native void _setMinimized(boolean b);
71 public native void setTitleBarText(String s);
72 public native void _setSize(int w, int h);
73 public native void setLocation();
74 public native void natInit();
75 public native void toFront();
76 public native void toBack();
77 public native void syncCursor();
78 public native void _dispose();
79 public native void setLimits(int minw, int minh, int maxw, int maxh);
80 public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
81 public native void dispatchEvent(gnu.gcj.RawData ev);
82 public void setMinimumSize(int minx, int miny, boolean resizable) {
83 setLimits(minx, miny, resizable ? Short.MAX_VALUE : minx, resizable ? Short.MAX_VALUE : miny);
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 {
105 public X11PixelBuffer doublebuf = null;
107 public int getWidth() { return width; }
108 public int getHeight() { return height; }
110 boolean initialized = false;
111 public X11Picture(JS r) { super(r); }
113 if (initialized) return;
115 boolean needsStipple = false;
117 // if we have any non-0x00, non-0xFF alphas, we can't double buffer ourselves
118 for(int i=0; i<width*height; i++)
119 if ((data[i] & 0xFF000000) == 0xFF000000)
121 else if ((data[i] & 0xFF000000) != 0x00)
124 buildPixelBuffer(needsStipple);
127 void buildPixelBuffer(boolean needsStipple) {
128 if (doublebuf != null) return;
129 // no point in using a shared pixmap since we'll only write to this image once
130 X11PixelBuffer b = new X11PixelBuffer(width, height, false);
131 b.drawPicture(this, 0, 0, 0, 0, width, height);
132 if (needsStipple) b.createStipple(this);
138 * An X11PixelBuffer is implemented as an X11 pixmap. "Normal"
139 * PixelBuffers will use XShm shared pixmaps if
140 * available. X11PixelBuffers created to accelerate Pictures
141 * with all-or-nothing alpha will not use shared pixmaps, however
142 * (since they are only written to once.
144 public static class X11PixelBuffer extends PixelBuffer {
146 int clipx, clipy, clipw, cliph;
150 /** PixelBuffers of X11Pictures can have stipples -- the stipple of the Picture */
151 RawData stipple = null;
153 /** Sets the PixelBuffer's internal stipple to the alpha==0x00 regions of xpi */
154 public native void createStipple(X11Picture xpi);
156 RawData pm; // Pixmap (if any) representing this Picture
157 boolean shared_pixmap = false; // true if pm is a ShmPixmap
158 RawData fake_ximage = null; // a 'fake' XImage corresponding to the shared pixmap; gives us the address and depth parameters
159 RawData shm_segment = null; // XShmSegmentInfo
161 RawData gc; // Graphics Interpreter on pm (never changes, so it's fast)
162 RawData clipped_gc; // Graphics Interpreter on pm, use this one if you need a clip/stipple
164 /** PixelBuffer mode */
165 public X11PixelBuffer(int w, int h) { this(w, h, true); }
166 public X11PixelBuffer(int w, int h, boolean shared_pixmap) {
170 this.shared_pixmap = shared_pixmap;
174 public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
175 cx1 = Math.max(dx, cx1);
176 cy1 = Math.max(dy, cy1);
177 cx2 = Math.min(dx + source.width, cx2);
178 cy2 = Math.min(dy + source.height, cy2);
179 if (cx1 >= cx2 || cy1 >= cy2) return;
180 X11Picture pic = (X11Picture)((Platform.DefaultGlyph)source).getPicture();
182 slowDrawPicture(pic, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
184 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
185 cx1 = Math.max(dx, cx1);
186 cy1 = Math.max(dy, cy1);
187 cx2 = Math.min(dx + source.width, cx2);
188 cy2 = Math.min(dy + source.height, cy2);
189 if (cx1 >= cx2 || cy1 >= cy2) return;
190 ((X11Picture)source).init();
191 if (((X11Picture)source).doublebuf != null)
192 fastDrawPicture(source, dx, dy, cx1, cy1, cx2, cy2);
194 slowDrawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
197 /** fast path for image drawing (no scaling, all-or-nothing alpha) */
198 public native void fastDrawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2);
200 /** slow path for image drawing */
201 public native void slowDrawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb, boolean alphaOnly);
203 public int getWidth() { return width; }
204 public int getHeight() { return height; }
205 public native void natInit();
206 public native void fillRect(int x, int y, int x2, int y2, int color);
207 public native void finalize();
209 // FIXME: try to use os acceleration
210 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
211 if (x1 == x3 && x2 == x4) {
212 fillRect(x1, y1, x4, y2, argb);
213 } else for(int y=y1; y<y2; y++) {
214 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
215 int _y1 = (int)Math.floor(y);
216 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
217 int _y2 = (int)Math.floor(y) + 1;
218 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
219 fillRect(_x1, _y1, _x2, _y2, argb);