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