1 /* 2 * Copyright (c) 2004, 2014, 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 java.security.PrivilegedAction; 33 import sun.security.action.GetIntegerAction; 34 import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection; 35 import sun.java2d.opengl.OGLRenderQueue; 36 import java.lang.reflect.InvocationTargetException; 37 38 public 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 = 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 */ 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 { | 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 */ 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 { |