jdk/src/solaris/classes/sun/awt/X11/XSelection.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 439 3488710b02f8
child 5506 202f599c92aa
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
110
dc692e475ed0 6607163: Linux: Cannot copy image from Java to OpenOffice
son
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.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
                if (!dataGetter.isExecuted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    throw new IOException("Owner timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (dataGetter.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    throw new IOException("Owner failed to convert data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                // Handle incremental transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (dataGetter.getActualType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    XDataTransferer.INCR_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    if (dataGetter.getActualFormat() != 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        throw new IOException("Unsupported INCR format: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                              dataGetter.getActualFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   321
                    int count = dataGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    if (count <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        throw new IOException("INCR data is missed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    long ptr = dataGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        // Following Xt sources use the last element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        long longLength = Native.getLong(ptr, count-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        if (longLength <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        if (longLength > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                            throw new IOException("Can't handle large data block: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                                  + longLength + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        len = (int)longLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    dataGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    ByteArrayOutputStream dataStream = new ByteArrayOutputStream(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        WindowPropertyGetter incrDataGetter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                                     selectionPropertyAtom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                                     0, MAX_LENGTH, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   356
                                                     XConstants.AnyPropertyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                            XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                                        incrementalTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                            propertyGetter = incrDataGetter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                XlibWrapper.XDeleteProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                                            XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                                            selectionPropertyAtom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                // If the owner doesn't respond within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                // SELECTION_TIMEOUT, we terminate incremental
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                // transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                waitForSelectionNotify(incrDataGetter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                XToolkit.removeEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                                               incrementalTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            // The owner didn't respond - terminate the transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                            if (!incrDataGetter.isExecuted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                throw new IOException("Owner timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            if (incrDataGetter.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                throw new IOException("Owner failed to convert data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                            if (incrDataGetter.getActualFormat() != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                throw new IOException("Unsupported data format: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                                      incrDataGetter.getActualFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   397
                            count = incrDataGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                            if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                            if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                ptr = incrDataGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                for (int index = 0; index < count; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                    dataStream.write(Native.getByte(ptr + index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                            data = dataStream.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            incrDataGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        XlibWrapper.XDeleteProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                                    XWindow.getXAWTRootWindow().getWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                                    selectionPropertyAtom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    if (dataGetter.getActualFormat() != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        throw new IOException("Unsupported data format: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                              dataGetter.getActualFormat());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   431
                    int count = dataGetter.getNumberOfItems();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        data = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        long ptr = dataGetter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        for (int index = 0; index < count; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                            data[index] = Native.getByte(ptr + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                dataGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return data != null ? data : new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    // To be MT-safe this method should be called under awtLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    boolean isOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return isOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   453
    // To be MT-safe this method should be called under awtLock.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   454
    private void setOwnerProp(boolean f) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   455
        isOwner = f;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   456
        fireOwnershipChanges(isOwner);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   457
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   458
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   459
    private void lostOwnership() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   460
        setOwnerProp(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        formatMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        formats = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        appContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        ownershipTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    // Converts the data to the 'format' and if the conversion succeeded stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // the data in the 'property' on the 'requestor' window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    // Returns true if the conversion succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private boolean convertAndStore(long requestor, long format, long property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        int dataFormat = 8; /* Can choose between 8,16,32. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        byte[] byteData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        long nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            SunToolkit.insertTargetMapping(this, appContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            byteData = DataTransferer.getInstance().convertData(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                                                contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                                                format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                                                formatMap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                                                XToolkit.isToolkitThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (byteData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        count = byteData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (count <= MAX_PROPERTY_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    nativeDataPtr = Native.toData(byteData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    // Initiate incremental data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    new IncrementalDataProvider(requestor, property, format, 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                                byteData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    nativeDataPtr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        XlibWrapper.unsafe.allocateMemory(XAtom.getAtomSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    Native.putLong(nativeDataPtr, (long)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    format = XDataTransferer.INCR_ATOM.getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    dataFormat = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                XlibWrapper.XChangeProperty(XToolkit.getDisplay(), requestor, property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                            format, dataFormat,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   523
                                            XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                            nativeDataPtr, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (nativeDataPtr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                XlibWrapper.unsafe.freeMemory(nativeDataPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    private void handleSelectionRequest(XSelectionRequestEvent xsre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        long property = xsre.get_property();
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   540
        final long requestor = xsre.get_requestor();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   541
        final long requestTime = xsre.get_time();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   542
        final long format = xsre.get_target();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        boolean conversionSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (ownershipTime != 0 &&
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   546
            (requestTime == XConstants.CurrentTime || requestTime >= ownershipTime))
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   547
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            // Handle MULTIPLE requests as per ICCCM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (format == XDataTransferer.MULTIPLE_ATOM.getAtom()) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   550
                conversionSucceeded = handleMultipleRequest(requestor, property);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                // Support for obsolete clients as per ICCCM.
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   553
                if (property == XConstants.None) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    property = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                if (format == XDataTransferer.TARGETS_ATOM.getAtom()) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   558
                    conversionSucceeded = handleTargetsRequest(property, requestor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                } else {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   560
                    conversionSucceeded = convertAndStore(requestor, format, property);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (!conversionSucceeded) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   566
            // None property indicates conversion failure.
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   567
            property = XConstants.None;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        XSelectionEvent xse = new XSelectionEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        try {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   572
            xse.set_type(XConstants.SelectionNotify);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            xse.set_send_event(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            xse.set_requestor(requestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            xse.set_selection(selectionAtom.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            xse.set_target(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            xse.set_property(property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            xse.set_time(requestTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                XlibWrapper.XSendEvent(XToolkit.getDisplay(), requestor, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   583
                                       XConstants.NoEventMask, xse.pData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            xse.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   592
    private boolean handleMultipleRequest(final long requestor, long property) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   593
        if (XConstants.None == property) {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   594
            // The property cannot be None for a MULTIPLE request.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   595
            return false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   596
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   597
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   598
        boolean conversionSucceeded = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   599
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   600
        // First retrieve the list of requested targets.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   601
        WindowPropertyGetter wpg =
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   602
                new WindowPropertyGetter(requestor, XAtom.get(property),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   603
                                         0, MAX_LENGTH, false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   604
                                         XConstants.AnyPropertyType);
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   605
        try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   606
            wpg.execute();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   607
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   608
            if (wpg.getActualFormat() == 32 && (wpg.getNumberOfItems() % 2) == 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   609
                final long count = wpg.getNumberOfItems() / 2;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   610
                final long pairsPtr = wpg.getData();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   611
                boolean writeBack = false;
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
                for (int i = 0; i < count; i++) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   614
                    long target = Native.getLong(pairsPtr, 2 * i);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   615
                    long prop = Native.getLong(pairsPtr, 2 * i + 1);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   616
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   617
                    if (!convertAndStore(requestor, target, prop)) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   618
                        // To report failure, we should replace the
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   619
                        // target atom with 0 in the MULTIPLE property.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   620
                        Native.putLong(pairsPtr, 2 * i, 0);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   621
                        writeBack = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   622
                    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   623
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   624
                if (writeBack) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   625
                    XToolkit.awtLock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   626
                    try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   627
                        XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   628
                                                    requestor,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   629
                                                    property,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   630
                                                    wpg.getActualType(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   631
                                                    wpg.getActualFormat(),
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   632
                                                                XConstants.PropModeReplace,
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   633
                                                    wpg.getData(),
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   634
                                                    wpg.getNumberOfItems());
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   635
                    } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   636
                        XToolkit.awtUnlock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   637
                    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   638
                }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   639
                conversionSucceeded = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   640
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   641
        } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   642
            wpg.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   645
        return conversionSucceeded;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   646
    }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   647
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   648
    private boolean handleTargetsRequest(long property, long requestor)
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   649
            throws IllegalStateException
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   650
    {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   651
        boolean conversionSucceeded = false;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   652
        // Use a local copy to avoid synchronization.
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   653
        long[] formatsLocal = formats;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   654
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   655
        if (formatsLocal == null) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   656
            throw new IllegalStateException("Not an owner.");
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   657
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   658
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   659
        long nativeDataPtr = 0;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   660
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   661
        try {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   662
            final int count = formatsLocal.length;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   663
            final int dataFormat = 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   665
            if (count > 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   666
                nativeDataPtr = Native.allocateLongArray(count);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   667
                Native.put(nativeDataPtr, formatsLocal);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   668
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   669
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   670
            conversionSucceeded = true;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   671
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   672
            XToolkit.awtLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            try {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   674
                XlibWrapper.XChangeProperty(XToolkit.getDisplay(), requestor,
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   675
                                            property, XAtom.XA_ATOM, dataFormat,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   676
                                            XConstants.PropModeReplace,
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   677
                                            nativeDataPtr, count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            } finally {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   679
                XToolkit.awtUnlock();
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   680
            }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   681
        } finally {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   682
            if (nativeDataPtr != 0) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   683
                XlibWrapper.unsafe.freeMemory(nativeDataPtr);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   684
                nativeDataPtr = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   687
        return conversionSucceeded;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   688
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   690
    private void fireOwnershipChanges(final boolean isOwner) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   691
        OwnershipListener l = null;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   692
        synchronized (stateLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   693
            l = ownershipListener;
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   694
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   695
        if (null != l) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   696
            l.ownershipChanged(isOwner);
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   697
        }
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   698
    }
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
    void registerOwershipListener(OwnershipListener l) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   701
        synchronized (stateLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   702
            ownershipListener = l;
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
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   706
    void unregisterOwnershipListener() {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   707
        synchronized (stateLock) {
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   708
            ownershipListener = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    private static class SelectionEventHandler implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            switch (ev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   715
            case XConstants.SelectionNotify: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                try {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 110
diff changeset
   718
                    XSelectionEvent xse = ev.get_xselection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    // Ignore the SelectionNotify event if it is not the response to our last request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    if (propertyGetter != null && xse.get_time() == lastRequestServerTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        // The property will be None in case of convertion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        if (xse.get_property() == selectionPropertyAtom.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            propertyGetter.execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        } else if (xse.get_property() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                            propertyGetter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                            propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   736
            case XConstants.SelectionRequest: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                XSelectionRequestEvent xsre = ev.get_xselectionrequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                long atom = xsre.get_selection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                XSelection selection = XSelection.getSelection(XAtom.get(atom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    selection.handleSelectionRequest(xsre);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   746
            case XConstants.SelectionClear: {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                XSelectionClearEvent xsce = ev.get_xselectionclear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                long atom = xsce.get_selection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                XSelection selection = XSelection.getSelection(XAtom.get(atom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    selection.lostOwnership();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    private static class IncrementalDataProvider implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        private final long requestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        private final long property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        private final long target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        private final int format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        private final byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        private int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // NOTE: formats other than 8 are not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        public IncrementalDataProvider(long requestor, long property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                       long target, int format, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            if (format != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                throw new IllegalArgumentException("Unsupported format: " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            this.requestor = requestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            this.property = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            XWindowAttributes wattr = new XWindowAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), requestor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                                     wattr.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    XlibWrapper.XSelectInput(XToolkit.getDisplay(), requestor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                             wattr.get_your_event_mask() |
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   796
                                             XConstants.PropertyChangeMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                wattr.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            XToolkit.addEventDispatcher(requestor, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            switch (ev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   808
            case XConstants.PropertyNotify:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                XPropertyEvent xpe = ev.get_xproperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                if (xpe.get_window() == requestor &&
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   811
                    xpe.get_state() == XConstants.PropertyDelete &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    xpe.get_atom() == property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    int count = data.length - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    long nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    if (count > MAX_PROPERTY_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        count = MAX_PROPERTY_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        nativeDataPtr = XlibWrapper.unsafe.allocateMemory(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                            Native.putByte(nativeDataPtr+i, data[offset + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        assert (count == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                        // All data has been transferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                        // This zero-length data indicates end of transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        XToolkit.removeEventDispatcher(requestor, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                                    requestor, property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                                    target, format,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   837
                                                    XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                                    nativeDataPtr, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    if (nativeDataPtr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        XlibWrapper.unsafe.freeMemory(nativeDataPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        nativeDataPtr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    offset += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    private static class IncrementalTransferHandler implements XEventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        public void dispatchEvent(XEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            switch (ev.get_type()) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   856
            case XConstants.PropertyNotify:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                XPropertyEvent xpe = ev.get_xproperty();
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   858
                if (xpe.get_state() == XConstants.PropertyNewValue &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    xpe.get_atom() == selectionPropertyAtom.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        if (propertyGetter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                            propertyGetter.execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                            propertyGetter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        XToolkit.awtLockNotifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                        XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
}