J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2005 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 | #include <stdio.h> |
| 27 | #include <dlfcn.h> |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <jni.h> |
| 31 | #include <jni_util.h> |
| 32 | #include <jvm.h> |
| 33 | #include "gdefs.h" |
| 34 | |
| 35 | #include <sys/param.h> |
| 36 | #include <sys/utsname.h> |
| 37 | |
| 38 | #include "awt_Plugin.h" |
| 39 | |
| 40 | #ifdef DEBUG |
| 41 | #define VERBOSE_AWT_DEBUG |
| 42 | #endif |
| 43 | |
| 44 | static void *awtHandle = NULL; |
| 45 | |
| 46 | typedef JNIEXPORT jint JNICALL JNI_OnLoad_type(JavaVM *vm, void *reserved); |
| 47 | |
| 48 | /* Initialize the Java VM instance variable when the library is |
| 49 | first loaded */ |
| 50 | JavaVM *jvm; |
| 51 | |
| 52 | JNIEXPORT jboolean JNICALL AWTIsHeadless() { |
| 53 | static JNIEnv *env = NULL; |
| 54 | static jboolean isHeadless; |
| 55 | jmethodID headlessFn; |
| 56 | jclass graphicsEnvClass; |
| 57 | |
| 58 | if (env == NULL) { |
| 59 | env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
| 60 | graphicsEnvClass = (*env)->FindClass(env, |
| 61 | "java/awt/GraphicsEnvironment"); |
| 62 | if (graphicsEnvClass == NULL) { |
| 63 | return JNI_TRUE; |
| 64 | } |
| 65 | headlessFn = (*env)->GetStaticMethodID(env, |
| 66 | graphicsEnvClass, "isHeadless", "()Z"); |
| 67 | if (headlessFn == NULL) { |
| 68 | return JNI_TRUE; |
| 69 | } |
| 70 | isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass, |
| 71 | headlessFn); |
| 72 | } |
| 73 | return isHeadless; |
| 74 | } |
| 75 | |
| 76 | jint |
| 77 | AWT_OnLoad(JavaVM *vm, void *reserved) |
| 78 | { |
| 79 | Dl_info dlinfo; |
| 80 | char buf[MAXPATHLEN]; |
| 81 | int32_t len; |
| 82 | char *p; |
| 83 | JNI_OnLoad_type *JNI_OnLoad_ptr; |
| 84 | struct utsname name; |
| 85 | JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2); |
| 86 | void *v; |
| 87 | char *envvar; |
| 88 | int xt_before_xm = 0; |
| 89 | int XAWT = 0; |
| 90 | jstring toolkit = NULL; |
| 91 | jstring propname = NULL; |
| 92 | |
| 93 | if (awtHandle != NULL) { |
| 94 | /* Avoid several loading attempts */ |
| 95 | return JNI_VERSION_1_2; |
| 96 | } |
| 97 | |
| 98 | jvm = vm; |
| 99 | |
| 100 | /* Get address of this library and the directory containing it. */ |
| 101 | dladdr((void *)JNI_OnLoad, &dlinfo); |
| 102 | realpath((char *)dlinfo.dli_fname, buf); |
| 103 | len = strlen(buf); |
| 104 | p = strrchr(buf, '/'); |
| 105 | |
| 106 | /* |
| 107 | * The code below is responsible for: |
| 108 | * 1. Loading appropriate awt library, i.e. xawt/libmawt or headless/libwawt |
| 109 | * 2. Setting "awt.toolkit" system property to use the appropriate Java toolkit class, |
| 110 | * (if user has specified the toolkit in env varialble) |
| 111 | */ |
| 112 | |
| 113 | propname = (*env)->NewStringUTF(env, "awt.toolkit"); |
| 114 | /* Check if toolkit is specified in env variable */ |
| 115 | envvar = getenv("AWT_TOOLKIT"); |
| 116 | if (envvar) { |
| 117 | if (strstr(envvar, "XToolkit")) { |
| 118 | toolkit = (*env)->NewStringUTF(env, "sun.awt.X11.XToolkit"); |
| 119 | } |
| 120 | /* If user specified toolkit then set java system property */ |
| 121 | if (toolkit && propname) { |
| 122 | JNU_CallStaticMethodByName (env, |
| 123 | NULL, |
| 124 | "java/lang/System", |
| 125 | "setProperty", |
| 126 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 127 | propname,toolkit); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Calculate toolkit name, kind of toolkit (XAWT, Motif) and library to load */ |
| 132 | if (AWTIsHeadless()) { |
| 133 | strcpy(p, "/headless/libmawt"); |
| 134 | } else { |
| 135 | /* Default AWT Toolkit on Linux and Solaris is XAWT. */ |
| 136 | strcpy(p, "/xawt/libmawt"); |
| 137 | } |
| 138 | |
| 139 | if (toolkit) { |
| 140 | (*env)->DeleteLocalRef(env, toolkit); |
| 141 | } |
| 142 | if (propname) { |
| 143 | (*env)->DeleteLocalRef(env, propname); |
| 144 | } |
| 145 | |
| 146 | strcat(p, ".so"); |
| 147 | |
| 148 | JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load", |
| 149 | "(Ljava/lang/String;)V", |
| 150 | JNU_NewStringPlatform(env, buf)); |
| 151 | |
| 152 | awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL); |
| 153 | |
| 154 | /* |
| 155 | if (dlsym(awtHandle, "AWTCharRBearing") == NULL) { |
| 156 | printf("========= AWTCharRBearing not found\n"); fflush(stdout); |
| 157 | } |
| 158 | else { |
| 159 | printf("========= AWTCharRBearing was found\n"); fflush(stdout); |
| 160 | } |
| 161 | */ |
| 162 | |
| 163 | return JNI_VERSION_1_2; |
| 164 | } |
| 165 | |
| 166 | JNIEXPORT jint JNICALL |
| 167 | JNI_OnLoad(JavaVM *vm, void *reserved) |
| 168 | { |
| 169 | return AWT_OnLoad(vm, reserved); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * This entry point must remain in libawt.so as part of a contract |
| 174 | * with the CDE variant of Java Media Framework. (sdtjmplay) |
| 175 | * Reflect this call over to the correct libmawt.so. |
| 176 | */ |
| 177 | JNIEXPORT void JNICALL |
| 178 | Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this, |
| 179 | jobject frame, jstring jcommand) |
| 180 | { |
| 181 | /* type of the old backdoor function */ |
| 182 | typedef JNIEXPORT void JNICALL |
| 183 | XsessionWMcommand_type(JNIEnv *env, jobject this, |
| 184 | jobject frame, jstring jcommand); |
| 185 | |
| 186 | static XsessionWMcommand_type *XsessionWMcommand = NULL; |
| 187 | |
| 188 | if (XsessionWMcommand == NULL && awtHandle == NULL) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | XsessionWMcommand = (XsessionWMcommand_type *) |
| 193 | dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand"); |
| 194 | |
| 195 | if (XsessionWMcommand == NULL) |
| 196 | return; |
| 197 | |
| 198 | (*XsessionWMcommand)(env, this, frame, jcommand); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /* |
| 203 | * This entry point must remain in libawt.so as part of a contract |
| 204 | * with the CDE variant of Java Media Framework. (sdtjmplay) |
| 205 | * Reflect this call over to the correct libmawt.so. |
| 206 | */ |
| 207 | JNIEXPORT void JNICALL |
| 208 | Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv) |
| 209 | { |
| 210 | typedef JNIEXPORT void JNICALL |
| 211 | XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv); |
| 212 | |
| 213 | static XsessionWMcommand_New_type *XsessionWMcommand = NULL; |
| 214 | |
| 215 | if (XsessionWMcommand == NULL && awtHandle == NULL) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | XsessionWMcommand = (XsessionWMcommand_New_type *) |
| 220 | dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New"); |
| 221 | |
| 222 | if (XsessionWMcommand == NULL) |
| 223 | return; |
| 224 | |
| 225 | (*XsessionWMcommand)(env, jargv); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | #define REFLECT_VOID_FUNCTION(name, arglist, paramlist) \ |
| 230 | typedef name##_type arglist; \ |
| 231 | void name arglist \ |
| 232 | { \ |
| 233 | static name##_type *name##_ptr = NULL; \ |
| 234 | if (name##_ptr == NULL && awtHandle == NULL) { \ |
| 235 | return; \ |
| 236 | } \ |
| 237 | name##_ptr = (name##_type *) \ |
| 238 | dlsym(awtHandle, #name); \ |
| 239 | if (name##_ptr == NULL) { \ |
| 240 | return; \ |
| 241 | } \ |
| 242 | (*name##_ptr)paramlist; \ |
| 243 | } |
| 244 | |
| 245 | #define REFLECT_FUNCTION(return_type, name, arglist, paramlist) \ |
| 246 | typedef return_type name##_type arglist; \ |
| 247 | return_type name arglist \ |
| 248 | { \ |
| 249 | static name##_type *name##_ptr = NULL; \ |
| 250 | if (name##_ptr == NULL && awtHandle == NULL) { \ |
| 251 | return NULL; \ |
| 252 | } \ |
| 253 | name##_ptr = (name##_type *) \ |
| 254 | dlsym(awtHandle, #name); \ |
| 255 | if (name##_ptr == NULL) { \ |
| 256 | return NULL; \ |
| 257 | } \ |
| 258 | return (*name##_ptr)paramlist; \ |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * These entry point must remain in libawt.so ***for Java Plugin ONLY*** |
| 264 | * Reflect this call over to the correct libmawt.so. |
| 265 | */ |
| 266 | |
| 267 | REFLECT_VOID_FUNCTION(getAwtLockFunctions, |
| 268 | (void (**AwtLock)(JNIEnv *), void (**AwtUnlock)(JNIEnv *), |
| 269 | void (**AwtNoFlushUnlock)(JNIEnv *), void *reserved), |
| 270 | (AwtLock, AwtUnlock, AwtNoFlushUnlock, reserved)) |
| 271 | |
| 272 | REFLECT_VOID_FUNCTION(getAwtData, |
| 273 | (int32_t *awt_depth, Colormap *awt_cmap, Visual **awt_visual, |
| 274 | int32_t *awt_num_colors, void *pReserved), |
| 275 | (awt_depth, awt_cmap, awt_visual, |
| 276 | awt_num_colors, pReserved)) |
| 277 | |
| 278 | REFLECT_FUNCTION(Display *, getAwtDisplay, (void), ()) |