1 /* 2 * Copyright (c) 2000, 2005, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * 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, *tk = 0; 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 jstring fmanager = NULL; 89 jstring fmProp = NULL; 90 91 if (awtHandle != NULL) { 92 /* Avoid several loading attempts */ 93 return JNI_VERSION_1_2; 94 } 95 96 jvm = vm; 97 98 /* Get address of this library and the directory containing it. */ 99 dladdr((void *)JNI_OnLoad, &dlinfo); 100 realpath((char *)dlinfo.dli_fname, buf); 101 len = strlen(buf); 102 p = strrchr(buf, '/'); 103 104 /* 105 * 1. Set the "sun.font.fontmanager" system property, 106 * 2. Choose the library image name. 107 */ 108 109 fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager"); 110 /* Check if toolkit is specified in env variable */ 111 #ifdef MACOSX 112 envvar = getenv("AWT_TOOLKIT"); 113 if ((envvar && strstr(envvar, "XToolkit")) || AWTIsHeadless()) { 114 #endif 115 fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager"); 116 tk = "/xawt/libmawt"; 117 #ifdef MACOSX 118 } else { 119 fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager"); 120 tk = "/lwawt/liblwawt"; 121 } 122 #endif 123 if (fmanager && fmProp) { 124 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty", 125 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 126 fmProp, fmanager); 127 } 128 129 /* Calculate library name to load */ 130 if (AWTIsHeadless()) { 131 strcpy(p, "/headless/libmawt"); 132 } else if (tk) { 133 strcpy(p, tk); 134 } 135 136 #ifdef MACOSX 137 strcat(p, ".dylib"); 138 #else 139 strcat(p, ".so"); 140 #endif 141 142 if (tk) { 143 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load", 144 "(Ljava/lang/String;)V", 145 JNU_NewStringPlatform(env, buf)); 146 awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL); 147 } 148 149 return JNI_VERSION_1_2; 150 } 151 152 JNIEXPORT jint JNICALL 153 JNI_OnLoad(JavaVM *vm, void *reserved) 154 { 155 return AWT_OnLoad(vm, reserved); 156 } 157 158 /* 159 * This entry point must remain in libawt.so as part of a contract 160 * with the CDE variant of Java Media Framework. (sdtjmplay) 161 * Reflect this call over to the correct libmawt.so. 162 */ 163 JNIEXPORT void JNICALL 164 Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this, 165 jobject frame, jstring jcommand) 166 { 167 /* type of the old backdoor function */ 168 typedef JNIEXPORT void JNICALL 169 XsessionWMcommand_type(JNIEnv *env, jobject this, 170 jobject frame, jstring jcommand); 171 172 static XsessionWMcommand_type *XsessionWMcommand = NULL; 173 174 if (XsessionWMcommand == NULL && awtHandle == NULL) { 175 return; 176 } 177 178 XsessionWMcommand = (XsessionWMcommand_type *) 179 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand"); 180 181 if (XsessionWMcommand == NULL) 182 return; 183 184 (*XsessionWMcommand)(env, this, frame, jcommand); 185 } 186 187 188 /* 189 * This entry point must remain in libawt.so as part of a contract 190 * with the CDE variant of Java Media Framework. (sdtjmplay) 191 * Reflect this call over to the correct libmawt.so. 192 */ 193 JNIEXPORT void JNICALL 194 Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv) 195 { 196 typedef JNIEXPORT void JNICALL 197 XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv); 198 199 static XsessionWMcommand_New_type *XsessionWMcommand = NULL; 200 201 if (XsessionWMcommand == NULL && awtHandle == NULL) { 202 return; 203 } 204 205 XsessionWMcommand = (XsessionWMcommand_New_type *) 206 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New"); 207 208 if (XsessionWMcommand == NULL) 209 return; 210 211 (*XsessionWMcommand)(env, jargv); 212 } 213 214 215 #define REFLECT_VOID_FUNCTION(name, arglist, paramlist) \ 216 typedef name##_type arglist; \ 217 void name arglist \ 218 { \ 219 static name##_type *name##_ptr = NULL; \ 220 if (name##_ptr == NULL && awtHandle == NULL) { \ 221 return; \ 222 } \ 223 name##_ptr = (name##_type *) \ 224 dlsym(awtHandle, #name); \ 225 if (name##_ptr == NULL) { \ 226 return; \ 227 } \ 228 (*name##_ptr)paramlist; \ 229 } 230 231 #define REFLECT_FUNCTION(return_type, name, arglist, paramlist) \ 232 typedef return_type name##_type arglist; \ 233 return_type name arglist \ 234 { \ 235 static name##_type *name##_ptr = NULL; \ 236 if (name##_ptr == NULL && awtHandle == NULL) { \ 237 return NULL; \ 238 } \ 239 name##_ptr = (name##_type *) \ 240 dlsym(awtHandle, #name); \ 241 if (name##_ptr == NULL) { \ 242 return NULL; \ 243 } \ 244 return (*name##_ptr)paramlist; \ 245 } 246 247 248 /* 249 * These entry point must remain in libawt.so ***for Java Plugin ONLY*** 250 * Reflect this call over to the correct libmawt.so. 251 */ 252 253 /* 254 * TODO: implement for LWCToolkit, MacOSX 255 */ 256 257 REFLECT_VOID_FUNCTION(getAwtLockFunctions, 258 (void (**AwtLock)(JNIEnv *), void (**AwtUnlock)(JNIEnv *), 259 void (**AwtNoFlushUnlock)(JNIEnv *), void *reserved), 260 (AwtLock, AwtUnlock, AwtNoFlushUnlock, reserved)) 261 262 REFLECT_VOID_FUNCTION(getAwtData, 263 (int32_t *awt_depth, Colormap *awt_cmap, Visual **awt_visual, 264 int32_t *awt_num_colors, void *pReserved), 265 (awt_depth, awt_cmap, awt_visual, 266 awt_num_colors, pReserved)) 267 268 REFLECT_FUNCTION(Display *, getAwtDisplay, (void), ())