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