001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import sun.misc.Unsafe;
029: import java.util.Vector;
030: import java.security.AccessController;
031: import java.security.PrivilegedAction;
032:
033: /**
034: * This class contains the collection of utility functions to help work with
035: * native data types on different platforms similarly.
036: */
037:
038: class Native {
039:
040: private static Unsafe unsafe = XlibWrapper.unsafe;
041:
042: static int longSize;
043:
044: static int dataModel;
045: static {
046: String dataModelProp = (String) AccessController
047: .doPrivileged(new PrivilegedAction() {
048: public Object run() {
049: return System
050: .getProperty("sun.arch.data.model");
051: }
052: });
053: try {
054: dataModel = Integer.parseInt(dataModelProp);
055: } catch (Exception e) {
056: dataModel = 32;
057: }
058: if (dataModel == 32) {
059: longSize = 4;
060: } else {
061: longSize = 8;
062: }
063: }
064:
065: /**
066: * Set of helper function to read data of different PLATFORM types
067: * from memory pointer by <code>ptr</code>
068: * Note, names of types in function are NATIVE PLATFORM types
069: * and they have the same size as they would have in C compiler
070: * on the same platform.
071: */
072:
073: static boolean getBool(long ptr) {
074: return getInt(ptr) != 0;
075: }
076:
077: static boolean getBool(long ptr, int index) {
078: return getInt(ptr, index) != 0;
079: }
080:
081: static void putBool(long ptr, boolean data) {
082: putInt(ptr, (data) ? (1) : (0));
083: }
084:
085: static void putBool(long ptr, int index, boolean data) {
086: putInt(ptr, index, (data) ? (1) : (0));
087: }
088:
089: /**
090: * Access to C byte data(one byte)
091: */
092: static int getByteSize() {
093: return 1;
094: }
095:
096: static byte getByte(long ptr) {
097: return unsafe.getByte(ptr);
098: }
099:
100: static byte getByte(long ptr, int index) {
101: return getByte(ptr + index);
102: }
103:
104: /**
105: * Stores to C byte data(one byte)
106: */
107: static void putByte(long ptr, byte data) {
108: unsafe.putByte(ptr, data);
109: }
110:
111: static void putByte(long ptr, int index, byte data) {
112: putByte(ptr + index, data);
113: }
114:
115: /**
116: * Converts length bytes of data pointed by <code>data</code> into byte array
117: * Returns null if data is zero
118: * @param data native pointer to native memory
119: * @param length size in bytes of native memory
120: */
121: static byte[] toBytes(long data, int length) {
122: if (data == 0) {
123: return null;
124: }
125: byte[] res = new byte[length];
126: for (int i = 0; i < length; i++, data++) {
127: res[i] = getByte(data);
128: }
129: return res;
130: }
131:
132: /**
133: * Stores byte array into native memory and returns pointer to this memory
134: * Returns 0 if bytes is null
135: */
136: static long toData(byte[] bytes) {
137: if (bytes == null) {
138: return 0;
139: }
140: long res = XlibWrapper.unsafe.allocateMemory(bytes.length);
141: for (int i = 0; i < bytes.length; i++) {
142: putByte(res + i, bytes[i]);
143: }
144: return res;
145: }
146:
147: /**
148: * Access to C unsigned byte data(one byte)
149: */
150: static int getUByteSize() {
151: return 1;
152: }
153:
154: static short getUByte(long ptr) {
155: return (short) (0xFF & unsafe.getByte(ptr));
156: }
157:
158: static short getUByte(long ptr, int index) {
159: return getUByte(ptr + index);
160: }
161:
162: /**
163: * Stores to C unsigned byte data(one byte)
164: */
165: static void putUByte(long ptr, short data) {
166: unsafe.putByte(ptr, (byte) data);
167: }
168:
169: static void putUByte(long ptr, int index, short data) {
170: putUByte(ptr + index, data);
171: }
172:
173: /**
174: * Converts length usnigned bytes of data pointed by <code>data</code> into
175: * short array
176: * Returns null if data is zero
177: * @param data native pointer to native memory
178: * @param length size in bytes of native memory
179: */
180: static short[] toUBytes(long data, int length) {
181: if (data == 0) {
182: return null;
183: }
184: short[] res = new short[length];
185: for (int i = 0; i < length; i++, data++) {
186: res[i] = getUByte(data);
187: }
188: return res;
189: }
190:
191: /**
192: * Stores short array as unsigned bytes into native memory and returns pointer
193: * to this memory
194: * Returns 0 if bytes is null
195: */
196: static long toUData(short[] bytes) {
197: if (bytes == null) {
198: return 0;
199: }
200: long res = XlibWrapper.unsafe.allocateMemory(bytes.length);
201: for (int i = 0; i < bytes.length; i++) {
202: putUByte(res + i, bytes[i]);
203: }
204: return res;
205: }
206:
207: /**
208: * Access to C short data(two bytes)
209: */
210: static int getShortSize() {
211: return 2;
212: }
213:
214: static short getShort(long ptr) {
215: return unsafe.getShort(ptr);
216: }
217:
218: /**
219: * Stores to C short data(two bytes)
220: */
221: static void putShort(long ptr, short data) {
222: unsafe.putShort(ptr, data);
223: }
224:
225: static void putShort(long ptr, int index, short data) {
226: putShort(ptr + index * getShortSize(), data);
227: }
228:
229: static long toData(short[] shorts) {
230: if (shorts == null) {
231: return 0;
232: }
233: long res = XlibWrapper.unsafe.allocateMemory(shorts.length
234: * getShortSize());
235: for (int i = 0; i < shorts.length; i++) {
236: putShort(res, i, shorts[i]);
237: }
238: return res;
239: }
240:
241: /**
242: * Access to C unsigned short data(two bytes)
243: */
244: static int getUShortSize() {
245: return 2;
246: }
247:
248: static int getUShort(long ptr) {
249: return 0xFFFF & unsafe.getShort(ptr);
250: }
251:
252: /**
253: * Stores to C unsigned short data(two bytes)
254: */
255: static void putUShort(long ptr, int data) {
256: unsafe.putShort(ptr, (short) data);
257: }
258:
259: static void putUShort(long ptr, int index, int data) {
260: putUShort(ptr + index * getShortSize(), data);
261: }
262:
263: /**
264: * Stores int array as unsigned shorts into native memory and returns pointer
265: * to this memory
266: * Returns 0 if bytes is null
267: */
268: static long toUData(int[] shorts) {
269: if (shorts == null) {
270: return 0;
271: }
272: long res = XlibWrapper.unsafe.allocateMemory(shorts.length
273: * getShortSize());
274: for (int i = 0; i < shorts.length; i++) {
275: putUShort(res, i, shorts[i]);
276: }
277: return res;
278: }
279:
280: /**
281: * Access to C int data(four bytes)
282: */
283: static int getIntSize() {
284: return 4;
285: }
286:
287: static int getInt(long ptr) {
288: return unsafe.getInt(ptr);
289: }
290:
291: static int getInt(long ptr, int index) {
292: return getInt(ptr + getIntSize() * index);
293: }
294:
295: /**
296: * Stores to C int data(four bytes)
297: */
298: static void putInt(long ptr, int data) {
299: unsafe.putInt(ptr, data);
300: }
301:
302: static void putInt(long ptr, int index, int data) {
303: putInt(ptr + index * getIntSize(), data);
304: }
305:
306: static long toData(int[] ints) {
307: if (ints == null) {
308: return 0;
309: }
310: long res = XlibWrapper.unsafe.allocateMemory(ints.length
311: * getIntSize());
312: for (int i = 0; i < ints.length; i++) {
313: putInt(res, i, ints[i]);
314: }
315: return res;
316: }
317:
318: /**
319: * Access to C unsigned int data(four bytes)
320: */
321: static int getUIntSize() {
322: return 4;
323: }
324:
325: static long getUInt(long ptr) {
326: return 0xFFFFFFFFL & unsafe.getInt(ptr);
327: }
328:
329: static long getUInt(long ptr, int index) {
330: return getUInt(ptr + getIntSize() * index);
331: }
332:
333: /**
334: * Stores to C unsigned int data(four bytes)
335: */
336: static void putUInt(long ptr, long data) {
337: unsafe.putInt(ptr, (int) data);
338: }
339:
340: static void putUInt(long ptr, int index, long data) {
341: putUInt(ptr + index * getIntSize(), data);
342: }
343:
344: /**
345: * Stores long array as unsigned intss into native memory and returns pointer
346: * to this memory
347: * Returns 0 if bytes is null
348: */
349: static long toUData(long[] ints) {
350: if (ints == null) {
351: return 0;
352: }
353: long res = XlibWrapper.unsafe.allocateMemory(ints.length
354: * getIntSize());
355: for (int i = 0; i < ints.length; i++) {
356: putUInt(res, i, ints[i]);
357: }
358: return res;
359: }
360:
361: /**
362: * Access to C long data(size depends on platform)
363: */
364: static int getLongSize() {
365: return longSize;
366: }
367:
368: static long getLong(long ptr) {
369: if (XlibWrapper.dataModel == 32) {
370: return unsafe.getInt(ptr);
371: } else {
372: return unsafe.getLong(ptr);
373: }
374: }
375:
376: /**
377: * Stores to C long data(four bytes)
378: * Note: <code>data</code> has <code>long</code> type
379: * to be able to keep 64-bit C <code>long</code> data
380: */
381: static void putLong(long ptr, long data) {
382: if (XlibWrapper.dataModel == 32) {
383: unsafe.putInt(ptr, (int) data);
384: } else {
385: unsafe.putLong(ptr, data);
386: }
387: }
388:
389: static void putLong(long ptr, int index, long data) {
390: putLong(ptr + index * getLongSize(), data);
391: }
392:
393: /**
394: * Returns index's element of the array of native long pointed by ptr
395: */
396: static long getLong(long ptr, int index) {
397: return getLong(ptr + index * getLongSize());
398: }
399:
400: /**
401: * Stores Java long[] array into memory. Memory location is treated as array
402: * of native <code>long</code>s
403: */
404: static void put(long ptr, long[] arr) {
405: for (int i = 0; i < arr.length; i++, ptr += getLongSize()) {
406: putLong(ptr, arr[i]);
407: }
408: }
409:
410: /**
411: * Stores Java Vector of Longs into memory. Memory location is treated as array
412: * of native <code>long</code>s
413: */
414: static void putLong(long ptr, Vector arr) {
415: for (int i = 0; i < arr.size(); i++, ptr += getLongSize()) {
416: putLong(ptr, ((Long) arr.elementAt(i)).longValue());
417: }
418: }
419:
420: /**
421: * Stores Java Vector of Longs into memory. Memory location is treated as array
422: * of native <code>long</code>s. Array is stored in reverse order
423: */
424: static void putLongReverse(long ptr, Vector arr) {
425: for (int i = arr.size() - 1; i >= 0; i--, ptr += getLongSize()) {
426: putLong(ptr, ((Long) arr.elementAt(i)).longValue());
427: }
428: }
429:
430: /**
431: * Converts length bytes of data pointed by <code>data</code> into byte array
432: * Returns null if data is zero
433: * @param data native pointer to native memory
434: * @param length size in longs(platform dependent) of native memory
435: */
436: static long[] toLongs(long data, int length) {
437: if (data == 0) {
438: return null;
439: }
440: long[] res = new long[length];
441: for (int i = 0; i < length; i++, data += getLongSize()) {
442: res[i] = getLong(data);
443: }
444: return res;
445: }
446:
447: static long toData(long[] longs) {
448: if (longs == null) {
449: return 0;
450: }
451: long res = XlibWrapper.unsafe.allocateMemory(longs.length
452: * getLongSize());
453: for (int i = 0; i < longs.length; i++) {
454: putLong(res, i, longs[i]);
455: }
456: return res;
457: }
458:
459: /**
460: * Access to C "unsigned long" date type, which is XID in X
461: */
462: static long getULong(long ptr) {
463: if (XlibWrapper.dataModel == 32) {
464: // Compensate sign-expansion
465: return ((long) unsafe.getInt(ptr)) & 0xFFFFFFFFL;
466: } else {
467: // Can't do anything!!!
468: return unsafe.getLong(ptr);
469: }
470: }
471:
472: static void putULong(long ptr, long value) {
473: putLong(ptr, value);
474: }
475:
476: /**
477: * Allocates memory for array of native <code>long</code>s of the size <code>length</code>
478: */
479: static long allocateLongArray(int length) {
480: return unsafe.allocateMemory(getLongSize() * length);
481: }
482:
483: static long getWindow(long ptr) {
484: return getLong(ptr);
485: }
486:
487: static long getWindow(long ptr, int index) {
488: return getLong(ptr + getWindowSize() * index);
489: }
490:
491: static void putWindow(long ptr, long window) {
492: putLong(ptr, window);
493: }
494:
495: static void putWindow(long ptr, int index, long window) {
496: putLong(ptr, index, window);
497: }
498:
499: /**
500: * Set of function to return sizes of C data of the appropriate
501: * type.
502: */
503: static int getWindowSize() {
504: return getLongSize();
505: }
506:
507: /**
508: * Set of function to access CARD32 type. All data which types are derived
509: * from CARD32 should be accessed using this accessors.
510: * These types are: XID(Window, Drawable, Font, Pixmap, Cursor, Colormap, GContext, KeySym),
511: * Atom, Mask, VisualID, Time
512: */
513: static long getCard32(long ptr) {
514: return getLong(ptr);
515: }
516:
517: static void putCard32(long ptr, long value) {
518: putLong(ptr, value);
519: }
520:
521: static long getCard32(long ptr, int index) {
522: return getLong(ptr, index);
523: }
524:
525: static void putCard32(long ptr, int index, long value) {
526: putLong(ptr, index, value);
527: }
528:
529: static int getCard32Size() {
530: return getLongSize();
531: }
532:
533: static long[] card32ToArray(long ptr, int length) {
534: return toLongs(ptr, length);
535: }
536:
537: static long card32ToData(long[] arr) {
538: return toData(arr);
539: }
540: }
|