1 /* 2 * Copyright (c) 2004, 2015, 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.awt.X11.XToolkit; 35 import sun.java2d.opengl.OGLRenderQueue; 36 37 public abstract class UNIXToolkit extends SunToolkit 38 { 39 /** All calls into GTK should be synchronized on this lock */ 40 public static final Object GTK_LOCK = new Object(); 41 42 private static final int[] BAND_OFFSETS = { 0, 1, 2 }; 43 private static final int[] BAND_OFFSETS_ALPHA = { 0, 1, 2, 3 }; 44 private static final int DEFAULT_DATATRANSFER_TIMEOUT = 10000; 45 46 private Boolean nativeGTKAvailable; 47 private Boolean nativeGTKLoaded; 48 private BufferedImage tmpImage = null; 49 50 public static int getDatatransferTimeout() { 51 Integer dt = AccessController.doPrivileged( 52 new GetIntegerAction("sun.awt.datatransfer.timeout")); 53 if (dt == null || dt <= 0) { 54 return DEFAULT_DATATRANSFER_TIMEOUT; 55 } else { 56 return dt; 57 } 58 } 59 60 /** 61 * Returns true if the native GTK libraries are capable of being 62 * loaded and are expected to work properly, false otherwise. Note 63 * that this method will not leave the native GTK libraries loaded if 64 * they haven't already been loaded. This allows, for example, Swing's 65 * GTK L&F to test for the presence of native GTK support without 66 * leaving the native libraries loaded. To attempt long-term loading 67 * of the native GTK libraries, use the loadGTK() method instead. 68 */ 69 @Override 70 public boolean isNativeGTKAvailable() { 71 synchronized (GTK_LOCK) { 72 if (nativeGTKLoaded != null) { 73 // We've already attempted to load GTK, so just return the 74 // status of that attempt. 75 return nativeGTKLoaded; 76 77 } else if (nativeGTKAvailable != null) { 78 // We've already checked the availability of the native GTK 79 // libraries, so just return the status of that attempt. 80 return nativeGTKAvailable; 81 82 } else { 83 boolean success = check_gtk(); 84 nativeGTKAvailable = success; 85 return success; 86 } 87 } 88 } 89 90 /** 91 * Loads the GTK libraries, if necessary. The first time this method 92 * is called, it will attempt to load the native GTK library. If 93 * successful, it leaves the library open and returns true; otherwise, 94 * the library is left closed and returns false. On future calls to 95 * this method, the status of the first attempt is returned (a simple 96 * lightweight boolean check, no native calls required). 97 */ 98 public boolean loadGTK() { 99 synchronized (GTK_LOCK) { 100 if (nativeGTKLoaded == null) { 101 XToolkit.awtLock(); 102 try { 103 nativeGTKLoaded = load_gtk(); 104 } finally { 105 XToolkit.awtUnlock(); 106 } 107 } 108 } 109 return nativeGTKLoaded; 110 } 111 112 /** 113 * Overridden to handle GTK icon loading 114 */ 115 protected Object lazilyLoadDesktopProperty(String name) { 116 if (name.startsWith("gtk.icon.")) { 117 return lazilyLoadGTKIcon(name); 118 } 119 return super.lazilyLoadDesktopProperty(name); 120 } 121 122 /** 123 * Load a native Gtk stock icon. 124 * 125 * @param longname a desktop property name. This contains icon name, size 126 * and orientation, e.g. <code>"gtk.icon.gtk-add.4.rtl"</code> 127 * @return an <code>Image</code> for the icon, or <code>null</code> if the 128 * icon could not be loaded 129 */ 130 protected Object lazilyLoadGTKIcon(String longname) { 131 // Check if we have already loaded it. 132 Object result = desktopProperties.get(longname); 133 if (result != null) { 134 return result; 135 } 136 137 // We need to have at least gtk.icon.<stock_id>.<size>.<orientation> 138 String str[] = longname.split("\\."); 139 if (str.length != 5) { 140 return null; 141 } 142 143 // Parse out the stock icon size we are looking for. 144 int size = 0; 145 try { 146 size = Integer.parseInt(str[3]); 147 } catch (NumberFormatException nfe) { 148 return null; 149 } 150 151 // Direction. 152 TextDirection dir = ("ltr".equals(str[4]) ? TextDirection.LTR : 153 TextDirection.RTL); 154 155 // Load the stock icon. 156 BufferedImage img = getStockIcon(-1, str[2], size, dir.ordinal(), null); 157 if (img != null) { 158 // Create the desktop property for the icon. 159 setDesktopProperty(longname, img); 160 } 161 return img; 162 } 163 164 /** 165 * Returns a BufferedImage which contains the Gtk icon requested. If no 166 * such icon exists or an error occurs loading the icon the result will 167 * be null. 168 * 169 * @param filename 170 * @return The icon or null if it was not found or loaded. 171 */ 172 public BufferedImage getGTKIcon(final String filename) { 173 if (!loadGTK()) { 174 return null; 175 176 } else { 177 // Call the native method to load the icon. 178 synchronized (GTK_LOCK) { 179 if (!load_gtk_icon(filename)) { 180 tmpImage = null; 181 } 182 } 183 } 184 // Return local image the callback loaded the icon into. 185 return tmpImage; 186 } 187 188 /** 189 * Returns a BufferedImage which contains the Gtk stock icon requested. 190 * If no such stock icon exists the result will be null. 191 * 192 * @param widgetType one of WidgetType values defined in GTKNativeEngine or 193 * -1 for system default stock icon. 194 * @param stockId String which defines the stock id of the gtk item. 195 * For a complete list reference the API at www.gtk.org for StockItems. 196 * @param iconSize One of the GtkIconSize values defined in GTKConstants 197 * @param textDirection One of the TextDirection values defined in 198 * GTKConstants 199 * @param detail Render detail that is passed to the native engine (feel 200 * free to pass null) 201 * @return The stock icon or null if it was not found or loaded. 202 */ 203 public BufferedImage getStockIcon(final int widgetType, final String stockId, 204 final int iconSize, final int direction, 205 final String detail) { 206 if (!loadGTK()) { 207 return null; 208 209 } else { 210 // Call the native method to load the icon. 211 synchronized (GTK_LOCK) { 212 if (!load_stock_icon(widgetType, stockId, iconSize, direction, detail)) { 213 tmpImage = null; 214 } 215 } 216 } 217 // Return local image the callback loaded the icon into. 218 return tmpImage; // set by loadIconCallback 219 } 220 221 /** 222 * This method is used by JNI as a callback from load_stock_icon. 223 * Image data is passed back to us via this method and loaded into the 224 * local BufferedImage and then returned via getStockIcon. 225 * 226 * Do NOT call this method directly. 227 */ 228 public void loadIconCallback(byte[] data, int width, int height, 229 int rowStride, int bps, int channels, boolean alpha) { 230 // Reset the stock image to null. 231 tmpImage = null; 232 233 // Create a new BufferedImage based on the data returned from the 234 // JNI call. 235 DataBuffer dataBuf = new DataBufferByte(data, (rowStride * height)); 236 // Maybe test # channels to determine band offsets? 237 WritableRaster raster = Raster.createInterleavedRaster(dataBuf, 238 width, height, rowStride, channels, 239 (alpha ? BAND_OFFSETS_ALPHA : BAND_OFFSETS), null); 240 ColorModel colorModel = new ComponentColorModel( 241 ColorSpace.getInstance(ColorSpace.CS_sRGB), alpha, false, 242 ColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE); 243 244 // Set the local image so we can return it later from 245 // getStockIcon(). 246 tmpImage = new BufferedImage(colorModel, raster, false, null); 247 } 248 249 private static native boolean check_gtk(); 250 private static native boolean load_gtk(); 251 private static native boolean unload_gtk(); 252 private native boolean load_gtk_icon(String filename); 253 private native boolean load_stock_icon(int widget_type, String stock_id, 254 int iconSize, int textDirection, String detail); 255 256 private native void nativeSync(); 257 258 @Override 259 public void sync() { 260 // flush the X11 buffer 261 nativeSync(); 262 // now flush the OGL pipeline (this is a no-op if OGL is not enabled) 263 OGLRenderQueue.sync(); 264 } 265 266 /* 267 * This returns the value for the desktop property "awt.font.desktophints" 268 * It builds this by querying the Gnome desktop properties to return 269 * them as platform independent hints. 270 * This requires that the Gnome properties have already been gathered. 271 */ 272 public static final String FONTCONFIGAAHINT = "fontconfig/Antialias"; 273 274 @Override 275 protected RenderingHints getDesktopAAHints() { 276 277 Object aaValue = getDesktopProperty("gnome.Xft/Antialias"); 278 279 if (aaValue == null) { 280 /* On a KDE desktop running KWin the rendering hint will 281 * have been set as property "fontconfig/Antialias". 282 * No need to parse further in this case. 283 */ 284 aaValue = getDesktopProperty(FONTCONFIGAAHINT); 285 if (aaValue != null) { 286 return new RenderingHints(KEY_TEXT_ANTIALIASING, aaValue); 287 } else { 288 return null; // no Gnome or KDE Desktop properties available. 289 } 290 } 291 292 /* 0 means off, 1 means some ON. What would any other value mean? 293 * If we require "1" to enable AA then some new value would cause 294 * us to default to "OFF". I don't think that's the best guess. 295 * So if its !=0 then lets assume AA. 296 */ 297 boolean aa = ((aaValue instanceof Number) 298 && ((Number) aaValue).intValue() != 0); 299 Object aaHint; 300 if (aa) { 301 String subpixOrder = 302 (String)getDesktopProperty("gnome.Xft/RGBA"); 303 304 if (subpixOrder == null || subpixOrder.equals("none")) { 305 aaHint = VALUE_TEXT_ANTIALIAS_ON; 306 } else if (subpixOrder.equals("rgb")) { 307 aaHint = VALUE_TEXT_ANTIALIAS_LCD_HRGB; 308 } else if (subpixOrder.equals("bgr")) { 309 aaHint = VALUE_TEXT_ANTIALIAS_LCD_HBGR; 310 } else if (subpixOrder.equals("vrgb")) { 311 aaHint = VALUE_TEXT_ANTIALIAS_LCD_VRGB; 312 } else if (subpixOrder.equals("vbgr")) { 313 aaHint = VALUE_TEXT_ANTIALIAS_LCD_VBGR; 314 } else { 315 /* didn't recognise the string, but AA is requested */ 316 aaHint = VALUE_TEXT_ANTIALIAS_ON; 317 } 318 } else { 319 aaHint = VALUE_TEXT_ANTIALIAS_DEFAULT; 320 } 321 return new RenderingHints(KEY_TEXT_ANTIALIASING, aaHint); 322 } 323 324 private native boolean gtkCheckVersionImpl(int major, int minor, 325 int micro); 326 327 /** 328 * Returns {@code true} if the GTK+ library is compatible with the given 329 * version. 330 * 331 * @param major 332 * The required major version. 333 * @param minor 334 * The required minor version. 335 * @param micro 336 * The required micro version. 337 * @return {@code true} if the GTK+ library is compatible with the given 338 * version. 339 */ 340 public boolean checkGtkVersion(int major, int minor, int micro) { 341 if (loadGTK()) { 342 return gtkCheckVersionImpl(major, minor, micro); 343 } 344 return false; 345 } 346 }