jdk/src/java.desktop/unix/classes/sun/awt/X11/Native.java
author chegar
Wed, 11 Nov 2015 09:19:12 +0000
changeset 33674 566777f73c32
parent 25859 3317bb8137f4
child 35667 ed476aba94de
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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2005, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 25859
diff changeset
    28
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class contains the collection of utility functions to help work with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * native data types on different platforms similarly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class Native {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Unsafe unsafe = XlibWrapper.unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static int longSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static int dataModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
    46
        String dataModelProp = AccessController.doPrivileged(
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
    47
            new PrivilegedAction<String>() {
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
    48
                public String run() {
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
    49
                    return System.getProperty("sun.arch.data.model");
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
    50
                }
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
    51
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            dataModel = Integer.parseInt(dataModelProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            dataModel = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (dataModel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            longSize = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            longSize = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Set of helper function to read data of different PLATFORM types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * from memory pointer by <code>ptr</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Note, names of types in function are NATIVE PLATFORM types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * and they have the same size as they would have in C compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * on the same platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static boolean getBool(long ptr) { return getInt(ptr) != 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static boolean getBool(long ptr, int index) { return getInt(ptr, index) != 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void putBool(long ptr, boolean data) { putInt(ptr, (data)?(1):(0)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void putBool(long ptr, int index, boolean data) { putInt(ptr, index, (data)?(1):(0)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Access to C byte data(one byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static int getByteSize() { return 1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static byte getByte(long ptr) { return unsafe.getByte(ptr); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static byte getByte(long ptr, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return getByte(ptr+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Stores to C byte data(one byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static void putByte(long ptr, byte data) { unsafe.putByte(ptr, data); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static void putByte(long ptr, int index, byte data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        putByte(ptr+index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Converts length bytes of data pointed by <code>data</code> into byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Returns null if data is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param data native pointer to native memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param length size in bytes of native memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static byte[] toBytes(long data, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (data == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        byte[] res = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i = 0; i < length; i++, data++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            res[i] = getByte(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Stores byte array into native memory and returns pointer to this memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns 0 if bytes is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static long toData(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        long res = XlibWrapper.unsafe.allocateMemory(bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (int i = 0; i < bytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            putByte(res+i, bytes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Access to C unsigned byte data(one byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static int getUByteSize() { return 1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static short getUByte(long ptr) { return (short)(0xFF & unsafe.getByte(ptr));  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static short getUByte(long ptr, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return getUByte(ptr+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Stores to C unsigned byte data(one byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static void putUByte(long ptr, short data) { unsafe.putByte(ptr, (byte)data); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static void putUByte(long ptr, int index, short data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        putUByte(ptr+index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Converts length usnigned bytes of data pointed by <code>data</code> into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * short array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns null if data is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param data native pointer to native memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param length size in bytes of native memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static short[] toUBytes(long data, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (data == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        short[] res = new short[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        for (int i = 0; i < length; i++, data++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            res[i] = getUByte(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Stores short array as unsigned bytes into native memory and returns pointer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * to this memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns 0 if bytes is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    static long toUData(short[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        long res = XlibWrapper.unsafe.allocateMemory(bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (int i = 0; i < bytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            putUByte(res+i, bytes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Access to C short data(two bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    static int getShortSize() { return 2; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static short getShort(long ptr) { return unsafe.getShort(ptr); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Stores to C short data(two bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static void putShort(long ptr, short data) { unsafe.putShort(ptr, data); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static void putShort(long ptr, int index, short data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        putShort(ptr + index*getShortSize(), data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static long toData(short[] shorts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (shorts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        long res = XlibWrapper.unsafe.allocateMemory(shorts.length*getShortSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        for (int i = 0; i < shorts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            putShort(res, i, shorts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Access to C unsigned short data(two bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static int getUShortSize() { return 2; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    static int getUShort(long ptr) { return 0xFFFF & unsafe.getShort(ptr); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Stores to C unsigned short data(two bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static void putUShort(long ptr, int data) { unsafe.putShort(ptr, (short)data); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static void putUShort(long ptr, int index, int data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        putUShort(ptr + index*getShortSize(), data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Stores int array as unsigned shorts into native memory and returns pointer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * to this memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Returns 0 if bytes is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    static long toUData(int[] shorts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (shorts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        long res = XlibWrapper.unsafe.allocateMemory(shorts.length*getShortSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (int i = 0; i < shorts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            putUShort(res, i, shorts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Access to C int data(four bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    static int getIntSize() { return 4; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static int getInt(long ptr) { return unsafe.getInt(ptr); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static int getInt(long ptr, int index) { return getInt(ptr +getIntSize()*index); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Stores to C int data(four bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    static void putInt(long ptr, int data) { unsafe.putInt(ptr, data); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    static void putInt(long ptr, int index, int data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        putInt(ptr + index*getIntSize(), data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    static long toData(int[] ints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (ints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        long res = XlibWrapper.unsafe.allocateMemory(ints.length*getIntSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for (int i = 0; i < ints.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            putInt(res, i, ints[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Access to C unsigned int data(four bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static int getUIntSize() { return 4; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    static long getUInt(long ptr) { return 0xFFFFFFFFL & unsafe.getInt(ptr); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    static long getUInt(long ptr, int index) { return getUInt(ptr +getIntSize()*index); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Stores to C unsigned int data(four bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static void putUInt(long ptr, long data) { unsafe.putInt(ptr, (int)data); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    static void putUInt(long ptr, int index, long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        putUInt(ptr + index*getIntSize(), data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Stores long array as unsigned intss into native memory and returns pointer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * to this memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns 0 if bytes is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static long toUData(long[] ints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (ints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        long res = XlibWrapper.unsafe.allocateMemory(ints.length*getIntSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        for (int i = 0; i < ints.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            putUInt(res, i, ints[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Access to C long data(size depends on platform)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static int getLongSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return longSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static long getLong(long ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (XlibWrapper.dataModel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return unsafe.getInt(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return unsafe.getLong(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Stores to C long data(four bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Note: <code>data</code> has <code>long</code> type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * to be able to keep 64-bit C <code>long</code> data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    static void putLong(long ptr, long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (XlibWrapper.dataModel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            unsafe.putInt(ptr, (int)data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            unsafe.putLong(ptr, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static void putLong(long ptr, int index, long data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        putLong(ptr+index*getLongSize(), data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns index's element of the array of native long pointed by ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static long getLong(long ptr, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return getLong(ptr + index*getLongSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Stores Java long[] array into memory. Memory location is treated as array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * of native <code>long</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static void put(long ptr, long[] arr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        for (int i = 0; i < arr.length; i ++, ptr += getLongSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            putLong(ptr, arr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Stores Java Vector of Longs into memory. Memory location is treated as array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * of native <code>long</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
   335
    static void putLong(long ptr, Vector<Long> arr) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        for (int i = 0; i < arr.size(); i ++, ptr += getLongSize()) {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
   337
            putLong(ptr, arr.elementAt(i).longValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Stores Java Vector of Longs into memory. Memory location is treated as array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * of native <code>long</code>s. Array is stored in reverse order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
   345
    static void putLongReverse(long ptr, Vector<Long> arr) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        for (int i = arr.size()-1; i >= 0; i--, ptr += getLongSize()) {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 5506
diff changeset
   347
            putLong(ptr, arr.elementAt(i).longValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Converts length bytes of data pointed by <code>data</code> into byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns null if data is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param data native pointer to native memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param length size in longs(platform dependent) of native memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    static long[] toLongs(long data, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (data == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        long[] res = new long[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        for (int i = 0; i < length; i++, data += getLongSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            res[i] = getLong(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    static long toData(long[] longs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (longs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        long res = XlibWrapper.unsafe.allocateMemory(longs.length*getLongSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        for (int i = 0; i < longs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            putLong(res, i, longs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Access to C "unsigned long" date type, which is XID in X
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    static long getULong(long ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (XlibWrapper.dataModel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // Compensate sign-expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return ((long)unsafe.getInt(ptr)) & 0xFFFFFFFFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // Can't do anything!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return unsafe.getLong(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    static void putULong(long ptr, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        putLong(ptr, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Allocates memory for array of native <code>long</code>s of the size <code>length</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    static long allocateLongArray(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return unsafe.allocateMemory(getLongSize() * length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    static long getWindow(long ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return getLong(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    static long getWindow(long ptr, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return getLong(ptr + getWindowSize()*index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    static void putWindow(long ptr, long window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        putLong(ptr, window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    static void putWindow(long ptr, int index, long window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        putLong(ptr, index, window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Set of function to return sizes of C data of the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    static int getWindowSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return getLongSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Set of function to access CARD32 type. All data which types are derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * from CARD32 should be accessed using this accessors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * These types are: XID(Window, Drawable, Font, Pixmap, Cursor, Colormap, GContext, KeySym),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *                  Atom, Mask, VisualID, Time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    static long getCard32(long ptr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return getLong(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    static void putCard32(long ptr, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        putLong(ptr, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    static long getCard32(long ptr, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return getLong(ptr, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    static void putCard32(long ptr, int index, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        putLong(ptr, index, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    static int getCard32Size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return getLongSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    static long[] card32ToArray(long ptr, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return toLongs(ptr, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    static long card32ToData(long[] arr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return toData(arr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
}