J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | #ifdef HEADLESS |
| 27 | #error This file should not be included in headless library |
| 28 | #endif |
| 29 | |
| 30 | #include "awt_p.h" |
| 31 | #include "java_awt_Cursor.h" |
| 32 | #include "awt_Cursor.h" |
| 33 | #include "sun_awt_motif_MCustomCursor.h" |
| 34 | |
| 35 | #include "jni.h" |
| 36 | #include "jni_util.h" |
| 37 | |
| 38 | extern struct CursorIDs cursorIDs; |
| 39 | static jfieldID widthID; |
| 40 | static jfieldID heightID; |
| 41 | |
| 42 | /* |
| 43 | * Class: sun_awt_motif_MCustomCursor |
| 44 | * Method: cacheInit |
| 45 | * Signature: ()V |
| 46 | */ |
| 47 | JNIEXPORT void JNICALL Java_sun_awt_motif_MCustomCursor_cacheInit |
| 48 | (JNIEnv *env, jclass cls) |
| 49 | { |
| 50 | jclass clsDimension = (*env)->FindClass(env, "java/awt/Dimension"); |
| 51 | widthID = (*env)->GetFieldID(env, clsDimension, "width", "I"); |
| 52 | heightID = (*env)->GetFieldID(env, clsDimension, "height", "I"); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Class: sun_awt_motif_MCustomCursor |
| 57 | * Method: queryBestCursor |
| 58 | * Signature: (Ljava/awt/Dimension;)V |
| 59 | */ |
| 60 | JNIEXPORT void JNICALL Java_sun_awt_motif_MCustomCursor_queryBestCursor |
| 61 | (JNIEnv *env, jclass cls, jobject dimension) |
| 62 | { |
| 63 | Window root; |
| 64 | uint32_t width, height; |
| 65 | |
| 66 | AWT_LOCK(); |
| 67 | root = RootWindow(awt_display, DefaultScreen(awt_display)); |
| 68 | XQueryBestCursor(awt_display, root, |
| 69 | (*env)->GetIntField(env, dimension, widthID), |
| 70 | (*env)->GetIntField(env, dimension, heightID), |
| 71 | &width, &height); |
| 72 | (*env)->SetIntField(env, dimension, widthID, (int32_t) width); |
| 73 | (*env)->SetIntField(env, dimension, heightID, (int32_t) height); |
| 74 | AWT_UNLOCK(); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Class: sun_awt_motif_MCustomCursor |
| 79 | * Method: createCursor |
| 80 | * Signature: ([B[BIIII)V |
| 81 | */ |
| 82 | JNIEXPORT void JNICALL Java_sun_awt_motif_MCustomCursor_createCursor |
| 83 | (JNIEnv *env , jobject this, jbyteArray xorMask, jbyteArray andMask, |
| 84 | jint width, jint height, jint fc, jint bc, jint xHotSpot, jint yHotSpot) |
| 85 | { |
| 86 | Cursor cursor; |
| 87 | char *sourceBits, *maskBits; |
| 88 | Window root; |
| 89 | Pixmap source, mask; |
| 90 | XColor fcolor, bcolor; |
| 91 | AwtGraphicsConfigDataPtr defaultConfig = |
| 92 | getDefaultConfig(DefaultScreen(awt_display)); |
| 93 | |
| 94 | AWT_LOCK(); |
| 95 | |
| 96 | root = RootWindow(awt_display, DefaultScreen(awt_display)); |
| 97 | fcolor.flags = DoRed | DoGreen | DoBlue; |
| 98 | fcolor.red = ((fc >> 16) & 0x000000ff) << 8; |
| 99 | fcolor.green = ((fc >> 8) & 0x000000ff) << 8; |
| 100 | fcolor.blue = ((fc >> 0) & 0x000000ff) << 8; |
| 101 | XAllocColor(awt_display, defaultConfig->awt_cmap, &fcolor); |
| 102 | bcolor.flags = DoRed | DoGreen | DoBlue; |
| 103 | bcolor.red = ((bc >> 16) & 0x000000ff) << 8; |
| 104 | bcolor.green = ((bc >> 8) & 0x000000ff) << 8; |
| 105 | bcolor.blue = ((bc >> 0) & 0x000000ff) << 8; |
| 106 | XAllocColor(awt_display, defaultConfig->awt_cmap, &bcolor); |
| 107 | |
| 108 | /* Create source pixmap. */ |
| 109 | sourceBits = (char *)(*env)->GetPrimitiveArrayCritical(env, xorMask, NULL); |
| 110 | source = XCreateBitmapFromData(awt_display, root, sourceBits, |
| 111 | width, height); |
| 112 | |
| 113 | /* Create mask pixmap */ |
| 114 | maskBits = (char *)(*env)->GetPrimitiveArrayCritical(env, andMask, NULL); |
| 115 | mask = XCreateBitmapFromData(awt_display, root, maskBits, |
| 116 | width, height); |
| 117 | |
| 118 | /* Create cursor */ |
| 119 | cursor = XCreatePixmapCursor(awt_display, source, mask, &fcolor, &bcolor, |
| 120 | xHotSpot, yHotSpot); |
| 121 | |
| 122 | /* Free resources */ |
| 123 | XFreePixmap(awt_display, source); |
| 124 | XFreePixmap(awt_display, mask); |
| 125 | |
| 126 | (*env)->ReleasePrimitiveArrayCritical(env, xorMask, sourceBits, JNI_ABORT); |
| 127 | (*env)->ReleasePrimitiveArrayCritical(env, andMask, maskBits, JNI_ABORT); |
| 128 | |
| 129 | JNU_SetLongFieldFromPtr(env, this, cursorIDs.pData, cursor); |
| 130 | |
| 131 | AWT_FLUSH_UNLOCK(); |
| 132 | } |