1 /* 2 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.awt; 26 27 import java.awt.RenderingHints; 28 import static java.awt.RenderingHints.*; 29 import java.awt.color.ColorSpace; 30 import java.awt.image.*; 31 import java.security.AccessController; 32 import sun.security.action.GetIntegerAction; 33 import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection; 34 import sun.java2d.opengl.OGLRenderQueue; 35 36 public abstract class UNIXToolkit extends SunToolkit 37 { 38 /** All calls into GTK should be synchronized on this lock */ 39 public static final Object GTK_LOCK = new Object(); 40 41 private static final int[] BAND_OFFSETS = { 0, 1, 2 }; 42 private static final int[] BAND_OFFSETS_ALPHA = { 0, 1, 2, 3 }; 43 private static final int DEFAULT_DATATRANSFER_TIMEOUT = 10000; 44 45 private Boolean nativeGTKAvailable; 46 private Boolean nativeGTKLoaded; 47 private BufferedImage tmpImage = null; 48 49 public static int getDatatransferTimeout() { 50 Integer dt = (Integer)AccessController.doPrivileged( 51 new GetIntegerAction("sun.awt.datatransfer.timeout")); 52 if (dt == null || dt <= 0) { 53 return DEFAULT_DATATRANSFER_TIMEOUT; 54 } else { 55 return dt; 56 } 57 } 58 59 /** 60 * Returns true if the native GTK libraries are capable of being 61 * loaded and are expected to work properly, false otherwise. Note 62 * that this method will not leave the native GTK libraries loaded if 63 * they haven't already been loaded. This allows, for example, Swing's 64 * GTK L&F to test for the presence of native GTK support without 65 * leaving the native libraries loaded. To attempt long-term loading 66 * of the native GTK libraries, use the loadGTK() method instead. 67 */ 68 @Override 69 public boolean isNativeGTKAvailable() { 70 synchronized (GTK_LOCK) { 71 if (nativeGTKLoaded != null) { 72 // We've already attempted to load GTK, so just return the 73 // status of that attempt. 74 return nativeGTKLoaded; 75 76 } else if (nativeGTKAvailable != null) { 77 // We've already checked the availability of the native GTK 78 // libraries, so just return the status of that attempt. 79 return nativeGTKAvailable; 80 81 } else { 82 boolean success = check_gtk(); 83 nativeGTKAvailable = success; 84 return success; 85 } 86 } 87 } 88 89 /** 90 * Loads the GTK libraries, if necessary. The first time this method 91 * is called, it will attempt to load the native GTK library. If 92 * successful, it leaves the library open and returns true; otherwise, 93 * the library is left closed and returns false. On future calls to 94 * this method, the status of the first attempt is returned (a simple 95 * lightweight boolean check, no native calls required). 96 */ 97 public boolean loadGTK() { 98 synchronized (GTK_LOCK) { 99 if (nativeGTKLoaded == null) { 100 nativeGTKLoaded = load_gtk(); 101 } 102 } 103 return nativeGTKLoaded; 104 } 105 106 /** 107 * Overridden to handle GTK icon loading 108 */ 109 protected Object lazilyLoadDesktopProperty(String name) { 110 if (name.startsWith("gtk.icon.")) { 111 return lazilyLoadGTKIcon(name); 112 } 113 return super.lazilyLoadDesktopProperty(name); 114 } 115 116 /** 117 * Load a native Gtk stock icon. 118 * 119 * @param longname a desktop property name. This contains icon name, size 120 * and orientation, e.g. <code>"gtk.icon.gtk-add.4.rtl"</code> 121 * @return an <code>Image</code> for the icon, or <code>null</code> if the 122 * icon could not be loaded 123 */ 124 protected Object lazilyLoadGTKIcon(String longname) { 125 // Check if we have already loaded it. 126 Object result = desktopProperties.get(longname); 127 if (result != null) { 128 return result; 129 } 130 131 // We need to have at least gtk.icon.<stock_id>.<size>.<orientation> 132 String str[] = longname.split("\\."); 133 if (str.length != 5) { 134 return null; 135 } 136 137 // Parse out the stock icon size we are looking for. 138 int size = 0; 139 try { 140 size = Integer.parseInt(str[3]); 141 } catch (NumberFormatException nfe) { 142 return null; 143 } 144 145 // Direction. 146 TextDirection dir = ("ltr".equals(str[4]) ? TextDirection.LTR : 147 TextDirection.RTL); 148 149 // Load the stock icon. 150 BufferedImage img = getStockIcon(-1, str[2], size, dir.ordinal(), null); 151 if (img != null) { 152 // Create the desktop property for the icon. 153 setDesktopProperty(longname, img); 154 } 155 return img; 156 } 157 158 /** 159 * Returns a BufferedImage which contains the Gtk icon requested. If no 160 * such icon exists or an error occurs loading the icon the result will 161 * be null. 162 * 163 * @param filename 164 * @return The icon or null if it was not found or loaded. 165 */ 166 public BufferedImage getGTKIcon(final String filename) { 167 if (!loadGTK()) { 168 return null; 169 170 } else { 171 // Call the native method to load the icon. 172 synchronized (GTK_LOCK) { 173 if (!load_gtk_icon(filename)) { 174 tmpImage = null; 175 } 176 } 177 } 178 // Return local image the callback loaded the icon into. 179 return tmpImage; 180 } 181 182 /** 183 * Returns a BufferedImage which contains the Gtk stock icon requested. 184 * If no such stock icon exists the result will be null. 185 * 186 * @param widgetType one of WidgetType values defined in GTKNativeEngine or 187 * -1 for system default stock icon. 188 * @param stockId String which defines the stock id of the gtk item. 189 * For a complete list reference the API at www.gtk.org for StockItems. 190 * @param iconSize One of the GtkIconSize values defined in GTKConstants 191 * @param textDirection One of the TextDirection values defined in 192 * GTKConstants 193 * @param detail Render detail that is passed to the native engine (feel 194 * free to pass null) 195 * @return The stock icon or null if it was not found or loaded. 196 */ 197 public BufferedImage getStockIcon(final int widgetType, final String stockId, 198 final int iconSize, final int direction, 199 final String detail) { 200 if (!loadGTK()) { 201 return null; 202 203 } else { 204 // Call the native method to load the icon. 205 synchronized (GTK_LOCK) { 206 if (!load_stock_icon(widgetType, stockId, iconSize, direction, detail)) { 207 tmpImage = null; 208 } 209 } 210 } 211 // Return local image the callback loaded the icon into. 212 return tmpImage; // set by loadIconCallback 213 } 214 215 /** 216 * This method is used by JNI as a callback from load_stock_icon. 217 * Image data is passed back to us via this method and loaded into the 218 * local BufferedImage and then returned via getStockIcon. 219 * 220 * Do NOT call this method directly. 221 */ 222 public void loadIconCallback(byte[] data, int width, int height, 223 int rowStride, int bps, int channels, boolean alpha) { 224 // Reset the stock image to null. 225 tmpImage = null; 226 227 // Create a new BufferedImage based on the data returned from the 228 // JNI call. 229 DataBuffer dataBuf = new DataBufferByte(data, (rowStride * height)); 230 // Maybe test # channels to determine band offsets? 231 WritableRaster raster = Raster.createInterleavedRaster(dataBuf, 232 width, height, rowStride, channels, 233 (alpha ? BAND_OFFSETS_ALPHA : BAND_OFFSETS), null); 234 ColorModel colorModel = new ComponentColorModel( 235 ColorSpace.getInstance(ColorSpace.CS_sRGB), alpha, false, 236 ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE); 237 238 // Set the local image so we can return it later from 239 // getStockIcon(). 240 tmpImage = new BufferedImage(colorModel, raster, false, null); 241 } 242 243 private static native boolean check_gtk(); 244 private static native boolean load_gtk(); 245 private static native boolean unload_gtk(); 246 private native boolean load_gtk_icon(String filename); 247 private native boolean load_stock_icon(int widget_type, String stock_id, 248 int iconSize, int textDirection, String detail); 249 250 private native void nativeSync(); 251 252 @Override 253 public void sync() { 254 // flush the X11 buffer 255 nativeSync(); 256 // now flush the OGL pipeline (this is a no-op if OGL is not enabled) 257 OGLRenderQueue.sync(); 258 } 259 260 /* 261 * This returns the value for the desktop property "awt.font.desktophints" 262 * It builds this by querying the Gnome desktop properties to return 263 * them as platform independent hints. 264 * This requires that the Gnome properties have already been gathered. 265 */ 266 public static final String FONTCONFIGAAHINT = "fontconfig/Antialias"; 267 268 @Override 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 = ((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 318 private native boolean gtkCheckVersionImpl(int major, int minor, 319 int micro); 320 321 /** 322 * Returns {@code true} if the GTK+ library is compatible with the given 323 * version. 324 * 325 * @param major 326 * The required major version. 327 * @param minor 328 * The required minor version. 329 * @param micro 330 * The required micro version. 331 * @return {@code true} if the GTK+ library is compatible with the given 332 * version. 333 */ 334 public boolean checkGtkVersion(int major, int minor, int micro) { 335 if (loadGTK()) { 336 return gtkCheckVersionImpl(major, minor, micro); 337 } 338 return false; 339 } 340 }