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