jdk/src/java.desktop/unix/classes/sun/awt/X11/MotifDnDConstants.java
author chegar
Wed, 11 Nov 2015 09:19:12 +0000
changeset 33674 566777f73c32
parent 25859 3317bb8137f4
permissions -rw-r--r--
8140606: Update library code to use internal Unsafe Reviewed-by: alanb, mchung, psandoz, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 17679
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2802
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2802
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2802
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2802
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2802
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.dnd.DnDConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 25859
diff changeset
    34
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Motif DnD protocol global constants and convenience routines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class MotifDnDConstants {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    42
    // utility class can not be instantiated
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    43
    private MotifDnDConstants() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // Note that offsets in all native structures below do not depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // architecture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final Unsafe unsafe = XlibWrapper.unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final XAtom XA_MOTIF_ATOM_0 = XAtom.get("_MOTIF_ATOM_0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final XAtom XA_MOTIF_DRAG_WINDOW = XAtom.get("_MOTIF_DRAG_WINDOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final XAtom XA_MOTIF_DRAG_TARGETS = XAtom.get("_MOTIF_DRAG_TARGETS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final XAtom XA_MOTIF_DRAG_INITIATOR_INFO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        XAtom.get("_MOTIF_DRAG_INITIATOR_INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final XAtom XA_MOTIF_DRAG_RECEIVER_INFO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        XAtom.get("_MOTIF_DRAG_RECEIVER_INFO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final XAtom XA_MOTIF_DRAG_AND_DROP_MESSAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        XAtom.get("_MOTIF_DRAG_AND_DROP_MESSAGE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final XAtom XA_XmTRANSFER_SUCCESS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        XAtom.get("XmTRANSFER_SUCCESS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final XAtom XA_XmTRANSFER_FAILURE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        XAtom.get("XmTRANSFER_FAILURE");
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
    60
    static final XSelection MotifDnDSelection = new XSelection(XA_MOTIF_ATOM_0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final byte MOTIF_DND_PROTOCOL_VERSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Supported protocol styles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final int MOTIF_PREFER_PREREGISTER_STYLE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final int MOTIF_PREFER_DYNAMIC_STYLE     = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int MOTIF_DYNAMIC_STYLE            = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final int MOTIF_PREFER_RECEIVER_STYLE    = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* Info structure sizes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static final int MOTIF_INITIATOR_INFO_SIZE      = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int MOTIF_RECEIVER_INFO_SIZE       = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* Sender/reason message masks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final byte MOTIF_MESSAGE_REASON_MASK      = (byte)0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final byte MOTIF_MESSAGE_SENDER_MASK      = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final byte MOTIF_MESSAGE_FROM_RECEIVER    = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final byte MOTIF_MESSAGE_FROM_INITIATOR   = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* Message flags masks and shifts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int MOTIF_DND_ACTION_MASK   = 0x000F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int MOTIF_DND_ACTION_SHIFT  =      0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static final int MOTIF_DND_STATUS_MASK   = 0x00F0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final int MOTIF_DND_STATUS_SHIFT  =      4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final int MOTIF_DND_ACTIONS_MASK  = 0x0F00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int MOTIF_DND_ACTIONS_SHIFT =      8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* message type constants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final byte TOP_LEVEL_ENTER   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final byte TOP_LEVEL_LEAVE   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final byte DRAG_MOTION       = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static final byte DROP_SITE_ENTER   = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final byte DROP_SITE_LEAVE   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final byte DROP_START        = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final byte DROP_FINISH       = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static final byte DRAG_DROP_FINISH  = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final byte OPERATION_CHANGED = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /* drop action constants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final int MOTIF_DND_NOOP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final int MOTIF_DND_MOVE = 1 << 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final int MOTIF_DND_COPY = 1 << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final int MOTIF_DND_LINK = 1 << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /* drop site status constants */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final byte MOTIF_NO_DROP_SITE      = (byte)1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final byte MOTIF_INVALID_DROP_SITE = (byte)2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static final byte MOTIF_VALID_DROP_SITE   = (byte)3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static long readMotifWindow() throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        long defaultScreenNumber = XlibWrapper.DefaultScreen(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        long defaultRootWindow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            XlibWrapper.RootWindow(XToolkit.getDisplay(), defaultScreenNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        long motifWindow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        WindowPropertyGetter wpg = new WindowPropertyGetter(defaultRootWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                            XA_MOTIF_DRAG_WINDOW,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                            0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                                            false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   121
                                                            XConstants.AnyPropertyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
2802
d05a9dcc8296 6678385: Random java.lang.StackOverflowError from various JDKs
art
parents: 439
diff changeset
   123
            int status = wpg.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   125
            if (status == XConstants.Success &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                wpg.getData() != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                wpg.getActualType() == XAtom.XA_WINDOW &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                wpg.getActualFormat() == 32 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                wpg.getNumberOfItems() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                long data = wpg.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // XID is CARD32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                motifWindow = Native.getLong(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return motifWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            wpg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static long createMotifWindow() throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        assert XToolkit.isAWTLockHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        long defaultScreenNumber =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            XlibWrapper.DefaultScreen(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        long defaultRootWindow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            XlibWrapper.RootWindow(XToolkit.getDisplay(), defaultScreenNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        long motifWindow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        long displayString = XlibWrapper.XDisplayString(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (displayString == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new XException("XDisplayString returns NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        long newDisplay = XlibWrapper.XOpenDisplay(displayString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (newDisplay == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new XException("XOpenDisplay returns NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        XlibWrapper.XGrabServer(newDisplay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 17679
diff changeset
   166
            XlibWrapper.XSetCloseDownMode(newDisplay, XConstants.RetainPermanent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            XSetWindowAttributes xwa = new XSetWindowAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                xwa.set_override_redirect(true);
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   172
                xwa.set_event_mask(XConstants.PropertyChangeMask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                motifWindow = XlibWrapper.XCreateWindow(newDisplay, defaultRootWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                        -10, -10, 1, 1, 0, 0,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   176
                                                        XConstants.InputOnly,
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   177
                                                        XConstants.CopyFromParent,
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   178
                                                        (XConstants.CWOverrideRedirect |
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   179
                                                         XConstants.CWEventMask),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                                        xwa.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (motifWindow == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    throw new XException("XCreateWindow returns NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                XlibWrapper.XMapWindow(newDisplay, motifWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                long data = Native.allocateLongArray(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    Native.putLong(data, motifWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   193
                    XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.VerifyChangePropertyHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                                defaultRootWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                                XA_MOTIF_DRAG_WINDOW.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                                XAtom.XA_WINDOW, 32,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   198
                                                XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                                data, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   201
                    XErrorHandlerUtil.RESTORE_XERROR_HANDLER();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   203
                    if ((XErrorHandlerUtil.saved_error != null) &&
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   204
                        (XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        throw new XException("Cannot write motif drag window handle.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    return motifWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    unsafe.freeMemory(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                xwa.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            XlibWrapper.XUngrabServer(newDisplay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            XlibWrapper.XCloseDisplay(newDisplay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static long getMotifWindow() throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * Note: it is unsafe to cache the motif drag window handle, as another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * client can change the _MOTIF_DRAG_WINDOW property on the root, the handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * becomes out-of-sync and all subsequent drag operations will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        long motifWindow = readMotifWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (motifWindow == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            motifWindow = createMotifWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return motifWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static final class Swapper {
117
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   235
        // utility class can not be instantiated
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   236
        private Swapper() {}
766ae458aaf1 6538066: XSelection should be more passive
son
parents: 2
diff changeset
   237
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public static short swap(short s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return (short)(((s & 0xFF00) >>> 8) | ((s & 0xFF) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public static int swap(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return ((i & 0xFF000000) >>> 24) | ((i & 0x00FF0000) >>> 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                ((i & 0x0000FF00) << 8) | ((i & 0x000000FF) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public static short getShort(long data, byte order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            short s = unsafe.getShort(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (order != MotifDnDConstants.getByteOrderByte()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                return swap(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        public static int getInt(long data, byte order) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int i = unsafe.getInt(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (order != MotifDnDConstants.getByteOrderByte()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                return swap(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * DragBSI.h:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *    BYTE          byte_order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *    BYTE          protocol_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *    CARD16        num_target_lists B16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *    CARD32        heap_offset B32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * } xmMotifTargetsPropertyRec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static long[][] getTargetListTable(long motifWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        WindowPropertyGetter wpg = new WindowPropertyGetter(motifWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                            XA_MOTIF_DRAG_TARGETS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                                            0, 100000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                                            false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                            XA_MOTIF_DRAG_TARGETS.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
2802
d05a9dcc8296 6678385: Random java.lang.StackOverflowError from various JDKs
art
parents: 439
diff changeset
   283
            int status = wpg.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   285
            if (status != XConstants.Success
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                || wpg.getActualType() != XA_MOTIF_DRAG_TARGETS.getAtom()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                || wpg.getData() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            long data = wpg.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (unsafe.getByte(data + 1) != MOTIF_DND_PROTOCOL_VERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            boolean swapNeeded = unsafe.getByte(data + 0) != getByteOrderByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            short numTargetLists = unsafe.getShort(data + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (swapNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                numTargetLists = Swapper.swap(numTargetLists);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            long[][] table = new long[numTargetLists][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            ByteOrder byteOrder = ByteOrder.nativeOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (swapNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                byteOrder = (byteOrder == ByteOrder.LITTLE_ENDIAN) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            long bufptr = data + 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            for (short i = 0; i < numTargetLists; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                short numTargets = unsafe.getShort(bufptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                bufptr += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (swapNeeded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    numTargets = Swapper.swap(numTargets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                table[i] = new long[numTargets];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                for (short j = 0; j < numTargets; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    // NOTE: cannot use Unsafe.getInt(), since it crashes on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    // Solaris/Sparc if the address is not a multiple of 4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    int target = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        for (int idx = 0; idx < 4; idx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                            target |= (unsafe.getByte(bufptr + idx) << 8*idx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                & (0xFF << 8*idx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        for (int idx = 0; idx < 4; idx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                            target |= (unsafe.getByte(bufptr + idx) << 8*(3-idx))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                & (0xFF << 8*(3-idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    // NOTE: don't need to swap, since we read it in the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    // order already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    table[i][j] = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    bufptr += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            wpg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private static void putTargetListTable(long motifWindow, long[][] table)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
      throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        assert XToolkit.isAWTLockHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int tableSize = 8; /* The size of leading xmMotifTargetsPropertyRec. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        for (int i = 0; i < table.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            tableSize += table[i].length * 4 + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        long data = unsafe.allocateMemory(tableSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // BYTE          byte_order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            unsafe.putByte(data + 0, getByteOrderByte());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // BYTE          protocol_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            unsafe.putByte(data + 1, MOTIF_DND_PROTOCOL_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // CARD16        num_target_lists B16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            unsafe.putShort(data + 2, (short)table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // CARD32        heap_offset B32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            unsafe.putInt(data + 4, tableSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            long bufptr = data + 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            for (int i = 0; i < table.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                unsafe.putShort(bufptr, (short)table[i].length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                bufptr += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                for (int j = 0; j < table[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    int target = (int)table[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    // NOTE: cannot use Unsafe.putInt(), since it crashes on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    // Solaris/Sparc if the address is not a multiple of 4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        for (int idx = 0; idx < 4; idx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                            byte b = (byte)((target & (0xFF << (8*idx))) >> (8*idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            unsafe.putByte(bufptr + idx, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        for (int idx = 0; idx < 4; idx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            byte b = (byte)((target & (0xFF << (8*idx))) >> (8*idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            unsafe.putByte(bufptr + (3-idx), b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    bufptr += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   397
            XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.VerifyChangePropertyHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                        motifWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                        XA_MOTIF_DRAG_TARGETS.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                        XA_MOTIF_DRAG_TARGETS.getAtom(), 8,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   402
                                        XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                        data, tableSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   405
            XErrorHandlerUtil.RESTORE_XERROR_HANDLER();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   407
            if ((XErrorHandlerUtil.saved_error != null) &&
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   408
                (XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // Create a new motif window and retry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                motifWindow = createMotifWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   413
                XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.VerifyChangePropertyHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                XlibWrapper.XChangeProperty(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                            motifWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                            XA_MOTIF_DRAG_TARGETS.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                            XA_MOTIF_DRAG_TARGETS.getAtom(), 8,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   418
                                            XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                            data, tableSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   421
                XErrorHandlerUtil.RESTORE_XERROR_HANDLER();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   423
                if ((XErrorHandlerUtil.saved_error != null) &&
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   424
                    (XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    throw new XException("Cannot write motif drag targets property.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            unsafe.freeMemory(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    static int getIndexForTargetList(long[] formats) throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        assert XToolkit.isAWTLockHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (formats.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // Make a defensive copy.
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 17679
diff changeset
   438
            formats = formats.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            Arrays.sort(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // NOTE: getMotifWindow() should never be called if the server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // grabbed. This will lock up the application as it grabs the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // Since we don't grab the server before getMotifWindow(), another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // client might replace motif window after we read it from the root, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // before we grab the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // We cannot resolve this problem, but we believe that this scenario is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // very unlikely to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        long motifWindow = getMotifWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        XlibWrapper.XGrabServer(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            long[][] table = getTargetListTable(motifWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (table != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                for (int i = 0; i < table.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    boolean equals = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    if (table[i].length == formats.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        for (int j = 0; j < table[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            if (table[i][j] != formats[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                equals = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        equals = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    if (equals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        XlibWrapper.XUngrabServer(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                // Create a new table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                // The first two entries must always be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                // (see DragBS.c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                table = new long[2][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                table[0] = new long[] { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                table[1] = new long[] { XAtom.XA_STRING };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            /* Index not found - expand the targets table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            long[][] new_table = new long[table.length + 1][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            /* Copy the old contents to the new table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            for (int i = 0; i < table.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                new_table[i] = table[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            /* Fill in the new entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            new_table[new_table.length - 1] = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            putTargetListTable(motifWindow, new_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return new_table.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            XlibWrapper.XUngrabServer(XToolkit.getDisplay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    static long[] getTargetListForIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        long motifWindow = getMotifWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        long[][] table = getTargetListTable(motifWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (index < 0 || index >= table.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return new long[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return table[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    static byte getByteOrderByte() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        // 'l' - for little endian, 'B' - for big endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            (byte)0x6C : (byte)0x42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    static void writeDragInitiatorInfoStruct(long window, int index) throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        assert XToolkit.isAWTLockHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        long structData = unsafe.allocateMemory(MOTIF_INITIATOR_INFO_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // BYTE byte_order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            unsafe.putByte(structData, getByteOrderByte());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            // BYTE protocol_version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            unsafe.putByte(structData + 1, MOTIF_DND_PROTOCOL_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            // CARD16 protocol_version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            unsafe.putShort(structData + 2, (short)index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            // CARD32 icc_handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            unsafe.putInt(structData + 4, (int)XA_MOTIF_ATOM_0.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   537
            XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.VerifyChangePropertyHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                                        XA_MOTIF_ATOM_0.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                        XA_MOTIF_DRAG_INITIATOR_INFO.getAtom(),
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   541
                                        8, XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                                        structData, MOTIF_INITIATOR_INFO_SIZE);
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   543
            XErrorHandlerUtil.RESTORE_XERROR_HANDLER();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   545
            if ((XErrorHandlerUtil.saved_error != null) &&
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   546
                (XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                throw new XException("Cannot write drag initiator info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            unsafe.freeMemory(structData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    static void writeDragReceiverInfoStruct(long window) throws XException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        assert XToolkit.isAWTLockHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        int dataSize = MotifDnDConstants.MOTIF_RECEIVER_INFO_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        long data = unsafe.allocateMemory(dataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            unsafe.putByte(data, MotifDnDConstants.getByteOrderByte()); /* byte order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            unsafe.putByte(data + 1, MotifDnDConstants.MOTIF_DND_PROTOCOL_VERSION); /* protocol version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            unsafe.putByte(data + 2, (byte)MotifDnDConstants.MOTIF_DYNAMIC_STYLE); /* protocol style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            unsafe.putByte(data + 3, (byte)0); /* pad */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            unsafe.putInt(data + 4, (int)window); /* proxy window */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            unsafe.putShort(data + 8, (short)0); /* num_drop_sites */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            unsafe.putShort(data + 10, (short)0); /* pad */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            unsafe.putInt(data + 12, dataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   570
            XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.VerifyChangePropertyHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            XlibWrapper.XChangeProperty(XToolkit.getDisplay(), window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                        XA_MOTIF_DRAG_RECEIVER_INFO.getAtom(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                        XA_MOTIF_DRAG_RECEIVER_INFO.getAtom(),
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 117
diff changeset
   574
                                        8, XConstants.PropModeReplace,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                        data, dataSize);
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   576
            XErrorHandlerUtil.RESTORE_XERROR_HANDLER();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
17679
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   578
            if ((XErrorHandlerUtil.saved_error != null) &&
a81555868357 8005607: Recursion in J2DXErrHandler() Causes a Stack Overflow on Linux
alitvinov
parents: 5506
diff changeset
   579
                (XErrorHandlerUtil.saved_error.get_error_code() != XConstants.Success)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                throw new XException("Cannot write Motif receiver info property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            unsafe.freeMemory(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public static int getMotifActionsForJavaActions(int javaActions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        int motifActions = MOTIF_DND_NOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if ((javaActions & DnDConstants.ACTION_MOVE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            motifActions |= MOTIF_DND_MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if ((javaActions & DnDConstants.ACTION_COPY) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            motifActions |= MOTIF_DND_COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if ((javaActions & DnDConstants.ACTION_LINK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            motifActions |= MOTIF_DND_LINK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return motifActions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public static int getJavaActionsForMotifActions(int motifActions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        int javaActions = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if ((motifActions & MOTIF_DND_MOVE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            javaActions |= DnDConstants.ACTION_MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if ((motifActions & MOTIF_DND_COPY) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            javaActions |= DnDConstants.ACTION_COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if ((motifActions & MOTIF_DND_LINK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            javaActions |= DnDConstants.ACTION_LINK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return javaActions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
}