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