blob: 23e39cabe497f0f929830dc4f1867303b3e866c5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package sun.awt;
26
27import java.awt.RenderingHints;
28import static java.awt.RenderingHints.*;
29import java.awt.color.ColorSpace;
30import java.awt.image.*;
31import java.security.AccessController;
32import java.security.PrivilegedAction;
33import sun.security.action.GetIntegerAction;
34import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection;
35import sun.java2d.opengl.OGLRenderQueue;
36import java.lang.reflect.InvocationTargetException;
37
38public abstract class UNIXToolkit extends SunToolkit
39{
40 /** All calls into GTK should be synchronized on this lock */
41 public static final Object GTK_LOCK = new Object();
42
43 private static final int[] BAND_OFFSETS = { 0, 1, 2 };
44 private static final int[] BAND_OFFSETS_ALPHA = { 0, 1, 2, 3 };
45 private static final int DEFAULT_DATATRANSFER_TIMEOUT = 10000;
46
47 private Boolean nativeGTKAvailable;
48 private Boolean nativeGTKLoaded;
49 private BufferedImage tmpImage = null;
50
51 public static int getDatatransferTimeout() {
52 Integer dt = (Integer)AccessController.doPrivileged(
53 new GetIntegerAction("sun.awt.datatransfer.timeout"));
54 if (dt == null || dt <= 0) {
55 return DEFAULT_DATATRANSFER_TIMEOUT;
56 } else {
57 return dt;
58 }
59 }
60
61 /**
62 * Returns true if the native GTK libraries are capable of being
63 * loaded and are expected to work properly, false otherwise. Note
64 * that this method will not leave the native GTK libraries loaded if
65 * they haven't already been loaded. This allows, for example, Swing's
66 * GTK L&F to test for the presence of native GTK support without
67 * leaving the native libraries loaded. To attempt long-term loading
68 * of the native GTK libraries, use the loadGTK() method instead.
69 */
70 @Override
71 public boolean isNativeGTKAvailable() {
72 synchronized (GTK_LOCK) {
73 if (nativeGTKLoaded != null) {
74 // We've already attempted to load GTK, so just return the
75 // status of that attempt.
76 return nativeGTKLoaded.booleanValue();
77
78 } else if (nativeGTKAvailable != null) {
79 // We've already checked the availability of the native GTK
80 // libraries, so just return the status of that attempt.
81 return nativeGTKAvailable.booleanValue();
82
83 } else {
84 boolean success = check_gtk();
85 nativeGTKAvailable = Boolean.valueOf(success);
86 return success;
87 }
88 }
89 }
90
91 /**
92 * Loads the GTK libraries, if necessary. The first time this method
93 * is called, it will attempt to load the native GTK library. If
94 * successful, it leaves the library open and returns true; otherwise,
95 * the library is left closed and returns false. On future calls to
96 * this method, the status of the first attempt is returned (a simple
97 * lightweight boolean check, no native calls required).
98 */
99 public boolean loadGTK() {
100 synchronized (GTK_LOCK) {
101 if (nativeGTKLoaded == null) {
102 boolean success = load_gtk();
103 nativeGTKLoaded = Boolean.valueOf(success);
104 }
105 }
106 return nativeGTKLoaded.booleanValue();
107 }
108
109 /**
110 * Overridden to handle GTK icon loading
111 */
112 protected Object lazilyLoadDesktopProperty(String name) {
113 if (name.startsWith("gtk.icon.")) {
114 return lazilyLoadGTKIcon(name);
115 }
116 return super.lazilyLoadDesktopProperty(name);
117 }
118
119 /**
120 * Load a native Gtk stock icon.
121 *
122 * @param longname a desktop property name. This contains icon name, size
123 * and orientation, e.g. <code>"gtk.icon.gtk-add.4.rtl"</code>
124 * @return an <code>Image</code> for the icon, or <code>null</code> if the
125 * icon could not be loaded
126 */
127 protected Object lazilyLoadGTKIcon(String longname) {
128 // Check if we have already loaded it.
129 Object result = desktopProperties.get(longname);
130 if (result != null) {
131 return result;
132 }
133
134 // We need to have at least gtk.icon.<stock_id>.<size>.<orientation>
135 String str[] = longname.split("\\.");
136 if (str.length != 5) {
137 return null;
138 }
139
140 // Parse out the stock icon size we are looking for.
141 int size = 0;
142 try {
143 size = Integer.parseInt(str[3]);
144 } catch (NumberFormatException nfe) {
145 return null;
146 }
147
148 // Direction.
149 TextDirection dir = ("ltr".equals(str[4]) ? TextDirection.LTR :
150 TextDirection.RTL);
151
152 // Load the stock icon.
153 BufferedImage img = getStockIcon(-1, str[2], size, dir.ordinal(), null);
154 if (img != null) {
155 // Create the desktop property for the icon.
156 setDesktopProperty(longname, img);
157 }
158 return img;
159 }
160
161 /**
162 * Returns a BufferedImage which contains the Gtk icon requested. If no
163 * such icon exists or an error occurs loading the icon the result will
164 * be null.
165 *
166 * @param filename
167 * @return The icon or null if it was not found or loaded.
168 */
169 public BufferedImage getGTKIcon(final String filename) {
170 if (!loadGTK()) {
171 return null;
172
173 } else {
174 // Call the native method to load the icon.
175 synchronized (GTK_LOCK) {
176 if (!load_gtk_icon(filename)) {
177 tmpImage = null;
178 }
179 }
180 }
181 // Return local image the callback loaded the icon into.
182 return tmpImage;
183 }
184
185 /**
186 * Returns a BufferedImage which contains the Gtk stock icon requested.
187 * If no such stock icon exists the result will be null.
188 *
189 * @param widgetType one of WidgetType values defined in GTKNativeEngine or
190 * -1 for system default stock icon.
191 * @param stockId String which defines the stock id of the gtk item.
192 * For a complete list reference the API at www.gtk.org for StockItems.
193 * @param iconSize One of the GtkIconSize values defined in GTKConstants
194 * @param textDirection One of the TextDirection values defined in
195 * GTKConstants
196 * @param detail Render detail that is passed to the native engine (feel
197 * free to pass null)
198 * @return The stock icon or null if it was not found or loaded.
199 */
200 public BufferedImage getStockIcon(final int widgetType, final String stockId,
201 final int iconSize, final int direction,
202 final String detail) {
203 if (!loadGTK()) {
204 return null;
205
206 } else {
207 // Call the native method to load the icon.
208 synchronized (GTK_LOCK) {
209 if (!load_stock_icon(widgetType, stockId, iconSize, direction, detail)) {
210 tmpImage = null;
211 }
212 }
213 }
214 // Return local image the callback loaded the icon into.
215 return tmpImage; // set by loadIconCallback
216 }
217
218 /**
219 * This method is used by JNI as a callback from load_stock_icon.
220 * Image data is passed back to us via this method and loaded into the
221 * local BufferedImage and then returned via getStockIcon.
222 *
223 * Do NOT call this method directly.
224 */
225 public void loadIconCallback(byte[] data, int width, int height,
226 int rowStride, int bps, int channels, boolean alpha) {
227 // Reset the stock image to null.
228 tmpImage = null;
229
230 // Create a new BufferedImage based on the data returned from the
231 // JNI call.
232 DataBuffer dataBuf = new DataBufferByte(data, (rowStride * height));
233 // Maybe test # channels to determine band offsets?
234 WritableRaster raster = Raster.createInterleavedRaster(dataBuf,
235 width, height, rowStride, channels,
236 (alpha ? BAND_OFFSETS_ALPHA : BAND_OFFSETS), null);
237 ColorModel colorModel = new ComponentColorModel(
238 ColorSpace.getInstance(ColorSpace.CS_sRGB), alpha, false,
239 ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
240
241 // Set the local image so we can return it later from
242 // getStockIcon().
243 tmpImage = new BufferedImage(colorModel, raster, false, null);
244 }
245
246 private static native boolean check_gtk();
247 private static native boolean load_gtk();
248 private static native boolean unload_gtk();
249 private native boolean load_gtk_icon(String filename);
250 private native boolean load_stock_icon(int widget_type, String stock_id,
251 int iconSize, int textDirection, String detail);
252
253 private native void nativeSync();
254
255 public void sync() {
256 // flush the X11 buffer
257 nativeSync();
258 // now flush the OGL pipeline (this is a no-op if OGL is not enabled)
259 OGLRenderQueue.sync();
260 }
261
262 /*
263 * This returns the value for the desktop property "awt.font.desktophints"
264 * It builds this by querying the Gnome desktop properties to return
265 * them as platform independent hints.
266 * This requires that the Gnome properties have already been gathered.
267 */
268 public static final String FONTCONFIGAAHINT = "fontconfig/Antialias";
269 protected RenderingHints getDesktopAAHints() {
270
271 Object aaValue = getDesktopProperty("gnome.Xft/Antialias");
272
273 if (aaValue == null) {
274 /* On a KDE desktop running KWin the rendering hint will
275 * have been set as property "fontconfig/Antialias".
276 * No need to parse further in this case.
277 */
278 aaValue = getDesktopProperty(FONTCONFIGAAHINT);
279 if (aaValue != null) {
280 return new RenderingHints(KEY_TEXT_ANTIALIASING, aaValue);
281 } else {
282 return null; // no Gnome or KDE Desktop properties available.
283 }
284 }
285
286 /* 0 means off, 1 means some ON. What would any other value mean?
287 * If we require "1" to enable AA then some new value would cause
288 * us to default to "OFF". I don't think that's the best guess.
289 * So if its !=0 then lets assume AA.
290 */
291 boolean aa = Boolean.valueOf(((aaValue instanceof Number) &&
292 ((Number)aaValue).intValue() != 0));
293 Object aaHint;
294 if (aa) {
295 String subpixOrder =
296 (String)getDesktopProperty("gnome.Xft/RGBA");
297
298 if (subpixOrder == null || subpixOrder.equals("none")) {
299 aaHint = VALUE_TEXT_ANTIALIAS_ON;
300 } else if (subpixOrder.equals("rgb")) {
301 aaHint = VALUE_TEXT_ANTIALIAS_LCD_HRGB;
302 } else if (subpixOrder.equals("bgr")) {
303 aaHint = VALUE_TEXT_ANTIALIAS_LCD_HBGR;
304 } else if (subpixOrder.equals("vrgb")) {
305 aaHint = VALUE_TEXT_ANTIALIAS_LCD_VRGB;
306 } else if (subpixOrder.equals("vbgr")) {
307 aaHint = VALUE_TEXT_ANTIALIAS_LCD_VBGR;
308 } else {
309 /* didn't recognise the string, but AA is requested */
310 aaHint = VALUE_TEXT_ANTIALIAS_ON;
311 }
312 } else {
313 aaHint = VALUE_TEXT_ANTIALIAS_DEFAULT;
314 }
315 return new RenderingHints(KEY_TEXT_ANTIALIASING, aaHint);
316 }
317}