Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/WindowPropertyGetter.java
32288 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627import java.util.*;28import sun.misc.Unsafe;2930public class WindowPropertyGetter {31private static Unsafe unsafe = XlibWrapper.unsafe;32private final long actual_type = unsafe.allocateMemory(8);33private final long actual_format = unsafe.allocateMemory(4);34private final long nitems_ptr = unsafe.allocateMemory(8);35private final long bytes_after = unsafe.allocateMemory(8);36private final long data = unsafe.allocateMemory(8);37private final long window;38private final XAtom property;39private final long offset;40private final long length;41private final boolean auto_delete;42private final long type;43private boolean executed = false;44public WindowPropertyGetter(long window, XAtom property, long offset,45long length, boolean auto_delete, long type)46{47if (property.getAtom() == 0) {48throw new IllegalArgumentException("Property ATOM should be initialized first:" + property);49}50// Zero is AnyPropertyType.51// if (type == 0) {52// throw new IllegalArgumentException("Type ATOM shouldn't be zero");53// }54if (window == 0) {55throw new IllegalArgumentException("Window must not be zero");56}57this.window = window;58this.property = property;59this.offset = offset;60this.length = length;61this.auto_delete = auto_delete;62this.type = type;6364Native.putLong(data, 0);65sun.java2d.Disposer.addRecord(this, disposer = new UnsafeXDisposerRecord("WindowPropertyGetter", new long[] {actual_type,66actual_format, nitems_ptr, bytes_after}, new long[] {data}));67}68UnsafeXDisposerRecord disposer;69public WindowPropertyGetter(long window, XAtom property, long offset,70long length, boolean auto_delete, XAtom type)71{72this(window, property, offset, length, auto_delete, type.getAtom());73}74public int execute() {75return execute(null);76}77public int execute(XErrorHandler errorHandler) {7879XToolkit.awtLock();80try {81if (isDisposed()) {82throw new IllegalStateException("Disposed");83}84if (executed) {85throw new IllegalStateException("Already executed");86}87executed = true;8889if (isCachingSupported() && isCached()) {90readFromCache();91return XConstants.Success;92}9394// Fix for performance problem - IgnodeBadWindowHandler is95// used too much without reason, just ignore it96if (errorHandler instanceof XErrorHandler.IgnoreBadWindowHandler) {97errorHandler = null;98}99100if (errorHandler != null) {101XErrorHandlerUtil.WITH_XERROR_HANDLER(errorHandler);102}103Native.putLong(data, 0);104int status = XlibWrapper.XGetWindowProperty(XToolkit.getDisplay(), window, property.getAtom(),105offset, length, (auto_delete?1:0), type,106actual_type, actual_format, nitems_ptr,107bytes_after, data);108if (isCachingSupported() && status == XConstants.Success && getData() != 0 && isCacheableProperty(property)) {109// Property has some data, we cache them110cacheProperty();111}112113if (errorHandler != null) {114XErrorHandlerUtil.RESTORE_XERROR_HANDLER();115}116return status;117} finally {118XToolkit.awtUnlock();119}120}121122public boolean isExecuted() {123return executed;124}125126public boolean isDisposed() {127return disposer.disposed;128}129130public int getActualFormat() {131if (isDisposed()) {132throw new IllegalStateException("Disposed");133}134if (!executed) {135throw new IllegalStateException("Not executed");136}137return unsafe.getInt(actual_format);138}139public long getActualType() {140if (isDisposed()) {141throw new IllegalStateException("Disposed");142}143if (!executed) {144throw new IllegalStateException("Not executed");145}146return XAtom.getAtom(actual_type);147}148public int getNumberOfItems() {149if (isDisposed()) {150throw new IllegalStateException("Disposed");151}152if (!executed) {153throw new IllegalStateException("Not executed");154}155return (int)Native.getLong(nitems_ptr);156}157public long getData() {158if (isDisposed()) {159throw new IllegalStateException("Disposed");160}161return Native.getLong(data);162}163public long getBytesAfter() {164if (isDisposed()) {165throw new IllegalStateException("Disposed");166}167if (!executed) {168throw new IllegalStateException("Not executed");169}170return Native.getLong(bytes_after);171}172public void dispose() {173XToolkit.awtLock();174try {175if (isDisposed()) {176return;177}178disposer.dispose();179} finally {180XToolkit.awtUnlock();181}182}183184static boolean isCachingSupported() {185return XPropertyCache.isCachingSupported();186}187188static Set<XAtom> cacheableProperties = new HashSet<XAtom>(Arrays.asList(new XAtom[] {189XAtom.get("_NET_WM_STATE"), XAtom.get("WM_STATE"), XAtom.get("_MOTIF_WM_HINTS")}));190191static boolean isCacheableProperty(XAtom property) {192return cacheableProperties.contains(property);193}194195boolean isCached() {196return XPropertyCache.isCached(window, property);197}198199int getDataLength() {200return getActualFormat() / 8 * getNumberOfItems();201}202203void readFromCache() {204property.putAtom(actual_type);205XPropertyCache.PropertyCacheEntry entry = XPropertyCache.getCacheEntry(window, property);206Native.putInt(actual_format, entry.getFormat());207Native.putLong(nitems_ptr, entry.getNumberOfItems());208Native.putLong(bytes_after, entry.getBytesAfter());209Native.putLong(data, unsafe.allocateMemory(getDataLength()));210XlibWrapper.memcpy(getData(), entry.getData(), getDataLength());211}212213void cacheProperty() {214XPropertyCache.storeCache(215new XPropertyCache.PropertyCacheEntry(getActualFormat(),216getNumberOfItems(),217getBytesAfter(),218getData(),219getDataLength()),220window,221property);222}223224}225226227