jdk/src/solaris/classes/sun/awt/X11/XSelection.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8134 fb60ca70b79e
child 20464 e0f599d49ea8
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8134
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 439
diff changeset
    23
 * questions.
2
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.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.UNIXToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A class which interfaces with the X11 selection service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
    45
public final class XSelection {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* Maps atoms to XSelection instances. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final Hashtable<XAtom, XSelection> table = new Hashtable<XAtom, XSelection>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /* Prevents from parallel selection data request processing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* The property in which the owner should place the requested data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final XAtom selectionPropertyAtom = XAtom.get("XAWT_SELECTION");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* The maximal length of the property data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final long MAX_LENGTH = 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The maximum data size for ChangeProperty request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * 100 is for the structure prepended to the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int MAX_PROPERTY_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            MAX_PROPERTY_SIZE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                (int)(XlibWrapper.XMaxRequestSize(XToolkit.getDisplay()) * 4 - 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* The PropertyNotify event handler for incremental data transfer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final XEventDispatcher incrementalTransferHandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        new IncrementalTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* The context for the current request - protected with awtLock. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static WindowPropertyGetter propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // The orders of the lock acquisition:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    //   XClipboard -> XSelection -> awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    //   lock -> awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* The X atom for the underlying selection. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final XAtom selectionAtom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Owner-related variables - protected with synchronized (this).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* The contents supplied by the current owner. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private Transferable contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* The format-to-flavor map for the current owner. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Map formatMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /* The formats supported by the current owner was set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private long[] formats = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* The AppContext in which the current owner was set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private AppContext appContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // The X server time of the last XConvertSelection() call;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // protected with 'lock' and awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static long lastRequestServerTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* The time at which the current owner was set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private long ownershipTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // True if we are the owner of this selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private boolean isOwner;
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   102
    private OwnershipListener ownershipListener = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   103
    private final Object stateLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                    new SelectionEventHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns the XSelection object for the specified selection atom or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>null</code> if none exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static XSelection getSelection(XAtom atom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return table.get(atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Creates a selection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param atom   the selection atom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param clpbrd the corresponding clipoboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception NullPointerException if atom is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   125
    public XSelection(XAtom atom) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (atom == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new NullPointerException("Null atom");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        selectionAtom = atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        table.put(selectionAtom, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public XAtom getSelectionAtom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return selectionAtom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public synchronized boolean setOwner(Transferable contents, Map formatMap,
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   138
                                         long[] formats, long time)
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   139
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        long owner = XWindow.getXAWTRootWindow().getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        long selection = selectionAtom.getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // ICCCM prescribes that CurrentTime should not be used for SetSelectionOwner.
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   144
        if (time == XConstants.CurrentTime) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            time = XToolkit.getCurrentServerTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.contents = contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.formatMap = formatMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.formats = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.ownershipTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            XlibWrapper.XSetSelectionOwner(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                           selection, owner, time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (XlibWrapper.XGetSelectionOwner(XToolkit.getDisplay(),
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   159
                                               selection) != owner)
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   160
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   164
            setOwnerProp(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Blocks the current thread till SelectionNotify or PropertyNotify (in case of INCR transfer) arrives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static void waitForSelectionNotify(WindowPropertyGetter dataGetter) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                DataTransferer.getInstance().processDataConversionRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                XToolkit.awtLockWait(250);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   181
            } while (propertyGetter == dataGetter && System.currentTimeMillis() < startTime + UNIXToolkit.getDatatransferTimeout());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns the list of atoms that represent the targets for which an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * to convert the current selection will succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public long[] getTargets(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (XToolkit.isToolkitThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new Error("UNIMPLEMENTED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   196
        long[] targets = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            WindowPropertyGetter targetsGetter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                         selectionPropertyAtom, 0, MAX_LENGTH,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   202
                                         true, XConstants.AnyPropertyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    propertyGetter = targetsGetter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    lastRequestServerTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    XlibWrapper.XConvertSelection(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                                  getSelectionAtom().getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                                  XDataTransferer.TARGETS_ATOM.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                                  selectionPropertyAtom.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                  XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                                  time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    // If the owner doesn't respond within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    // SELECTION_TIMEOUT, we report conversion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        waitForSelectionNotify(targetsGetter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        return new long[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   229
                targets = getFormats(targetsGetter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                targetsGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   234
        return targets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   237
    static long[] getFormats(WindowPropertyGetter targetsGetter) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        long[] formats = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (targetsGetter.isExecuted() && !targetsGetter.isDisposed() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                (targetsGetter.getActualType() == XAtom.XA_ATOM ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                 targetsGetter.getActualType() == XDataTransferer.TARGETS_ATOM.getAtom()) &&
110
dc692e475ed0 6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents: 2
diff changeset
   243
                targetsGetter.getActualFormat() == 32)
dc692e475ed0 6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents: 2
diff changeset
   244
        {
dc692e475ed0 6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents: 2
diff changeset
   245
            // we accept property with TARGETS type to be compatible with old jdks
dc692e475ed0 6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents: 2
diff changeset
   246
            // see 6607163
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   247
            int count = targetsGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                long atoms = targetsGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                formats = new long[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                for (int index = 0; index < count; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    formats[index] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            Native.getLong(atoms+index*XAtom.getAtomSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return formats != null ? formats : new long[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Requests the selection data in the specified format and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the data provided by the owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public byte[] getData(long format, long time) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (XToolkit.isToolkitThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new Error("UNIMPLEMENTED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            WindowPropertyGetter dataGetter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                         selectionPropertyAtom, 0, MAX_LENGTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                         false, // don't delete to handle INCR properly.
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   277
                                         XConstants.AnyPropertyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    propertyGetter = dataGetter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    lastRequestServerTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    XlibWrapper.XConvertSelection(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                  getSelectionAtom().getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                                  format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                                  selectionPropertyAtom.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                                  XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                  time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    // If the owner doesn't respond within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    // SELECTION_TIMEOUT, we report conversion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        waitForSelectionNotify(dataGetter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
8134
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   305
                validateDataGetter(dataGetter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                // Handle incremental transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (dataGetter.getActualType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    XDataTransferer.INCR_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    if (dataGetter.getActualFormat() != 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        throw new IOException("Unsupported INCR format: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                              dataGetter.getActualFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   316
                    int count = dataGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    if (count <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        throw new IOException("INCR data is missed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    long ptr = dataGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        // Following Xt sources use the last element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        long longLength = Native.getLong(ptr, count-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        if (longLength <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        if (longLength > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                            throw new IOException("Can't handle large data block: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                                  + longLength + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        len = (int)longLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    dataGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    ByteArrayOutputStream dataStream = new ByteArrayOutputStream(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        WindowPropertyGetter incrDataGetter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                                     selectionPropertyAtom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                     0, MAX_LENGTH, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   351
                                                     XConstants.AnyPropertyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                                        incrementalTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            propertyGetter = incrDataGetter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                XlibWrapper.XDeleteProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                                            XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                                            selectionPropertyAtom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                // If the owner doesn't respond within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                // SELECTION_TIMEOUT, we terminate incremental
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                // transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                waitForSelectionNotify(incrDataGetter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                XToolkit.removeEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                                               incrementalTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
8134
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   378
                            validateDataGetter(dataGetter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                            if (incrDataGetter.getActualFormat() != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                throw new IOException("Unsupported data format: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                                      incrDataGetter.getActualFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   385
                            count = incrDataGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                            if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                ptr = incrDataGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                for (int index = 0; index < count; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                    dataStream.write(Native.getByte(ptr + index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                            data = dataStream.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            incrDataGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        XlibWrapper.XDeleteProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                                    XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                                    selectionPropertyAtom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    if (dataGetter.getActualFormat() != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        throw new IOException("Unsupported data format: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                              dataGetter.getActualFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   419
                    int count = dataGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        data = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        long ptr = dataGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        for (int index = 0; index < count; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                            data[index] = Native.getByte(ptr + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                dataGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return data != null ? data : new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
8134
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   436
    void validateDataGetter(WindowPropertyGetter propertyGetter)
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   437
            throws IOException
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   438
    {
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   439
        // The order of checks is important because a property getter
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   440
        // has not been executed in case of timeout as well as in case of
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   441
        // changed selection owner.
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   442
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   443
        if (propertyGetter.isDisposed()) {
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   444
            throw new IOException("Owner failed to convert data");
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   445
        }
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   446
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   447
        // The owner didn't respond - terminate the transfer.
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   448
        if (!propertyGetter.isExecuted()) {
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   449
            throw new IOException("Owner timed out");
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   450
        }
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   451
    }
fb60ca70b79e 6340263: Regression testcase java/awt/dnd/DnDClipboardDeadlockTest throughs IOException: Owner timed out
denis
parents: 5506
diff changeset
   452
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // To be MT-safe this method should be called under awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    boolean isOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return isOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   458
    // To be MT-safe this method should be called under awtLock.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   459
    private void setOwnerProp(boolean f) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   460
        isOwner = f;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   461
        fireOwnershipChanges(isOwner);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   462
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   463
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   464
    private void lostOwnership() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   465
        setOwnerProp(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        formatMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        formats = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        appContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        ownershipTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    // Converts the data to the 'format' and if the conversion succeeded stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    // the data in the 'property' on the 'requestor' window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    // Returns true if the conversion succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private boolean convertAndStore(long requestor, long format, long property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        int dataFormat = 8; /* Can choose between 8,16,32. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        byte[] byteData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        long nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            SunToolkit.insertTargetMapping(this, appContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            byteData = DataTransferer.getInstance().convertData(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                                                contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                                                format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                                                formatMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                                                XToolkit.isToolkitThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (byteData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        count = byteData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                if (count <= MAX_PROPERTY_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    nativeDataPtr = Native.toData(byteData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    // Initiate incremental data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    new IncrementalDataProvider(requestor, property, format, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                                byteData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    nativeDataPtr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        XlibWrapper.unsafe.allocateMemory(XAtom.getAtomSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    Native.putLong(nativeDataPtr, (long)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    format = XDataTransferer.INCR_ATOM.getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    dataFormat = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                XlibWrapper.XChangeProperty(XToolkit.getDisplay(), requestor, property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                            format, dataFormat,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   528
                                            XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                            nativeDataPtr, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (nativeDataPtr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                XlibWrapper.unsafe.freeMemory(nativeDataPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private void handleSelectionRequest(XSelectionRequestEvent xsre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        long property = xsre.get_property();
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   545
        final long requestor = xsre.get_requestor();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   546
        final long requestTime = xsre.get_time();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   547
        final long format = xsre.get_target();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        boolean conversionSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (ownershipTime != 0 &&
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   551
            (requestTime == XConstants.CurrentTime || requestTime >= ownershipTime))
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   552
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            // Handle MULTIPLE requests as per ICCCM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (format == XDataTransferer.MULTIPLE_ATOM.getAtom()) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   555
                conversionSucceeded = handleMultipleRequest(requestor, property);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                // Support for obsolete clients as per ICCCM.
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   558
                if (property == XConstants.None) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    property = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                if (format == XDataTransferer.TARGETS_ATOM.getAtom()) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   563
                    conversionSucceeded = handleTargetsRequest(property, requestor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                } else {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   565
                    conversionSucceeded = convertAndStore(requestor, format, property);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (!conversionSucceeded) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   571
            // None property indicates conversion failure.
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   572
            property = XConstants.None;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        XSelectionEvent xse = new XSelectionEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        try {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   577
            xse.set_type(XConstants.SelectionNotify);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            xse.set_send_event(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            xse.set_requestor(requestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            xse.set_selection(selectionAtom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            xse.set_target(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            xse.set_property(property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            xse.set_time(requestTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                XlibWrapper.XSendEvent(XToolkit.getDisplay(), requestor, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   588
                                       XConstants.NoEventMask, xse.pData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            xse.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   597
    private boolean handleMultipleRequest(final long requestor, long property) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   598
        if (XConstants.None == property) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   599
            // The property cannot be None for a MULTIPLE request.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   600
            return false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   601
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   602
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   603
        boolean conversionSucceeded = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   604
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   605
        // First retrieve the list of requested targets.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   606
        WindowPropertyGetter wpg =
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   607
                new WindowPropertyGetter(requestor, XAtom.get(property),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   608
                                         0, MAX_LENGTH, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   609
                                         XConstants.AnyPropertyType);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   610
        try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   611
            wpg.execute();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   612
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   613
            if (wpg.getActualFormat() == 32 && (wpg.getNumberOfItems() % 2) == 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   614
                final long count = wpg.getNumberOfItems() / 2;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   615
                final long pairsPtr = wpg.getData();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   616
                boolean writeBack = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   617
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   618
                for (int i = 0; i < count; i++) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   619
                    long target = Native.getLong(pairsPtr, 2 * i);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   620
                    long prop = Native.getLong(pairsPtr, 2 * i + 1);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   621
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   622
                    if (!convertAndStore(requestor, target, prop)) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   623
                        // To report failure, we should replace the
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   624
                        // target atom with 0 in the MULTIPLE property.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   625
                        Native.putLong(pairsPtr, 2 * i, 0);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   626
                        writeBack = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   627
                    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   628
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   629
                if (writeBack) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   630
                    XToolkit.awtLock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   631
                    try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   632
                        XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   633
                                                    requestor,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   634
                                                    property,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   635
                                                    wpg.getActualType(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   636
                                                    wpg.getActualFormat(),
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   637
                                                                XConstants.PropModeReplace,
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   638
                                                    wpg.getData(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   639
                                                    wpg.getNumberOfItems());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   640
                    } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   641
                        XToolkit.awtUnlock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   642
                    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   643
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   644
                conversionSucceeded = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   645
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   646
        } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   647
            wpg.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   650
        return conversionSucceeded;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   651
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   652
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   653
    private boolean handleTargetsRequest(long property, long requestor)
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   654
            throws IllegalStateException
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   655
    {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   656
        boolean conversionSucceeded = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   657
        // Use a local copy to avoid synchronization.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   658
        long[] formatsLocal = formats;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   659
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   660
        if (formatsLocal == null) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   661
            throw new IllegalStateException("Not an owner.");
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   662
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   663
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   664
        long nativeDataPtr = 0;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   665
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   666
        try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   667
            final int count = formatsLocal.length;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   668
            final int dataFormat = 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   670
            if (count > 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   671
                nativeDataPtr = Native.allocateLongArray(count);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   672
                Native.put(nativeDataPtr, formatsLocal);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   673
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   674
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   675
            conversionSucceeded = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   676
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   677
            XToolkit.awtLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            try {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   679
                XlibWrapper.XChangeProperty(XToolkit.getDisplay(), requestor,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   680
                                            property, XAtom.XA_ATOM, dataFormat,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   681
                                            XConstants.PropModeReplace,
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   682
                                            nativeDataPtr, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            } finally {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   684
                XToolkit.awtUnlock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   685
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   686
        } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   687
            if (nativeDataPtr != 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   688
                XlibWrapper.unsafe.freeMemory(nativeDataPtr);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   689
                nativeDataPtr = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   692
        return conversionSucceeded;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   693
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   695
    private void fireOwnershipChanges(final boolean isOwner) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   696
        OwnershipListener l = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   697
        synchronized (stateLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   698
            l = ownershipListener;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   699
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   700
        if (null != l) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   701
            l.ownershipChanged(isOwner);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   702
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   703
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   704
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   705
    void registerOwershipListener(OwnershipListener l) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   706
        synchronized (stateLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   707
            ownershipListener = l;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   708
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   709
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   710
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   711
    void unregisterOwnershipListener() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   712
        synchronized (stateLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   713
            ownershipListener = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    private static class SelectionEventHandler implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            switch (ev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   720
            case XConstants.SelectionNotify: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                try {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   723
                    XSelectionEvent xse = ev.get_xselection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    // Ignore the SelectionNotify event if it is not the response to our last request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    if (propertyGetter != null && xse.get_time() == lastRequestServerTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        // The property will be None in case of convertion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        if (xse.get_property() == selectionPropertyAtom.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                            propertyGetter.execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                            propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        } else if (xse.get_property() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                            propertyGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                            propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   741
            case XConstants.SelectionRequest: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                XSelectionRequestEvent xsre = ev.get_xselectionrequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                long atom = xsre.get_selection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                XSelection selection = XSelection.getSelection(XAtom.get(atom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    selection.handleSelectionRequest(xsre);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   751
            case XConstants.SelectionClear: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                XSelectionClearEvent xsce = ev.get_xselectionclear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                long atom = xsce.get_selection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                XSelection selection = XSelection.getSelection(XAtom.get(atom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    selection.lostOwnership();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    private static class IncrementalDataProvider implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        private final long requestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        private final long property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        private final long target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        private final int format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        private final byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        private int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // NOTE: formats other than 8 are not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        public IncrementalDataProvider(long requestor, long property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                                       long target, int format, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (format != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                throw new IllegalArgumentException("Unsupported format: " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            this.requestor = requestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            this.property = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            XWindowAttributes wattr = new XWindowAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), requestor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                                     wattr.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    XlibWrapper.XSelectInput(XToolkit.getDisplay(), requestor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                             wattr.get_your_event_mask() |
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   801
                                             XConstants.PropertyChangeMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                wattr.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            XToolkit.addEventDispatcher(requestor, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            switch (ev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   813
            case XConstants.PropertyNotify:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                XPropertyEvent xpe = ev.get_xproperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                if (xpe.get_window() == requestor &&
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   816
                    xpe.get_state() == XConstants.PropertyDelete &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    xpe.get_atom() == property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    int count = data.length - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    long nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    if (count > MAX_PROPERTY_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        count = MAX_PROPERTY_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        nativeDataPtr = XlibWrapper.unsafe.allocateMemory(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                            Native.putByte(nativeDataPtr+i, data[offset + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                        assert (count == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        // All data has been transferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        // This zero-length data indicates end of transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        XToolkit.removeEventDispatcher(requestor, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                                    requestor, property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                                    target, format,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   842
                                                    XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                                    nativeDataPtr, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    if (nativeDataPtr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        XlibWrapper.unsafe.freeMemory(nativeDataPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    offset += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    private static class IncrementalTransferHandler implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            switch (ev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   861
            case XConstants.PropertyNotify:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                XPropertyEvent xpe = ev.get_xproperty();
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   863
                if (xpe.get_state() == XConstants.PropertyNewValue &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    xpe.get_atom() == selectionPropertyAtom.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                        if (propertyGetter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                            propertyGetter.execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                            propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
}