jdk/src/solaris/classes/sun/awt/X11/WindowPropertyGetter.java
author asaha
Wed, 21 Oct 2009 11:28:46 -0700
changeset 4214 0fa32d38146b
parent 2802 d05a9dcc8296
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class WindowPropertyGetter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    private static Unsafe unsafe = XlibWrapper.unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private final long actual_type = unsafe.allocateMemory(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private final long actual_format = unsafe.allocateMemory(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private final long nitems_ptr = unsafe.allocateMemory(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final long bytes_after = unsafe.allocateMemory(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private final long data = unsafe.allocateMemory(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private final long window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private final XAtom property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final long offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final long length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private final boolean auto_delete;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final long type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private boolean executed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public WindowPropertyGetter(long window, XAtom property, long offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                long length, boolean auto_delete, long type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (property.getAtom() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            throw new IllegalArgumentException("Property ATOM should be initialized first:" + property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // Zero is AnyPropertyType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // if (type == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        //     throw new IllegalArgumentException("Type ATOM shouldn't be zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (window == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            throw new IllegalArgumentException("Window must not be zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.window = window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.property = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.auto_delete = auto_delete;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Native.putLong(data, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        sun.java2d.Disposer.addRecord(this, disposer = new UnsafeXDisposerRecord("WindowPropertyGetter", new long[] {actual_type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                                                                 actual_format, nitems_ptr, bytes_after}, new long[] {data}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    UnsafeXDisposerRecord disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public WindowPropertyGetter(long window, XAtom property, long offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                long length, boolean auto_delete, XAtom type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this(window, property, offset, length, auto_delete, type.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public int execute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return execute(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
2802
d05a9dcc8296 6678385: Random java.lang.StackOverflowError from various JDKs
art
parents: 439
diff changeset
    78
    public int execute(XErrorHandler errorHandler) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new IllegalStateException("Disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (executed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                throw new IllegalStateException("Already executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            executed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (isCachingSupported() && isCached()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                readFromCache();
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
    92
                return XConstants.Success;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // Fix for performance problem - IgnodeBadWindowHandler is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // used too much without reason, just ignore it
2802
d05a9dcc8296 6678385: Random java.lang.StackOverflowError from various JDKs
art
parents: 439
diff changeset
    97
            if (errorHandler instanceof XErrorHandler.IgnoreBadWindowHandler) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                errorHandler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (errorHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                XToolkit.WITH_XERROR_HANDLER(errorHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Native.putLong(data, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            int status = XlibWrapper.XGetWindowProperty(XToolkit.getDisplay(), window, property.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                                        offset, length, (auto_delete?1:0), type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                                        actual_type, actual_format, nitems_ptr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                                        bytes_after, data);
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   109
            if (isCachingSupported() &&  status == XConstants.Success && getData() != 0 && isCacheableProperty(property)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // Property has some data, we cache them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                cacheProperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (errorHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                XToolkit.RESTORE_XERROR_HANDLER();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public boolean isExecuted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return executed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public boolean isDisposed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return disposer.disposed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public int getActualFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new IllegalStateException("Disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (!executed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalStateException("Not executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return unsafe.getInt(actual_format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public long getActualType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new IllegalStateException("Disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (!executed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalStateException("Not executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return XAtom.getAtom(actual_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public int getNumberOfItems() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IllegalStateException("Disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!executed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalStateException("Not executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return (int)Native.getLong(nitems_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public long getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new IllegalStateException("Disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return Native.getLong(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public long getBytesAfter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new IllegalStateException("Disposed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!executed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new IllegalStateException("Not executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return Native.getLong(bytes_after);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            disposer.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static boolean isCachingSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return XPropertyCache.isCachingSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static Set<XAtom> cacheableProperties = new HashSet<XAtom>(Arrays.asList(new XAtom[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            XAtom.get("_NET_WM_STATE"), XAtom.get("WM_STATE"), XAtom.get("_MOTIF_WM_HINTS")}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static boolean isCacheableProperty(XAtom property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return cacheableProperties.contains(property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    boolean isCached() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return XPropertyCache.isCached(window, property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    int getDataLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return getActualFormat() / 8 * getNumberOfItems();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    void readFromCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        property.putAtom(actual_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        XPropertyCache.PropertyCacheEntry entry = XPropertyCache.getCacheEntry(window, property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        Native.putInt(actual_format, entry.getFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Native.putLong(nitems_ptr, entry.getNumberOfItems());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Native.putLong(bytes_after, entry.getBytesAfter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Native.putLong(data, unsafe.allocateMemory(getDataLength()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        XlibWrapper.memcpy(getData(), entry.getData(), getDataLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    void cacheProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        XPropertyCache.storeCache(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            new XPropertyCache.PropertyCacheEntry(getActualFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                                  getNumberOfItems(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                                  getBytesAfter(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                                  getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                                  getDataLength()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}