1 /* 2 * Copyright (c) 2000, 2011, 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 toolkit = NULL, grenv = NULL, fmanager = NULL; 89 jstring tkProp = NULL, geProp = NULL, 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 * The code below is responsible for: 106 * 1. Loading appropriate awt library, i.e. xawt/libmawt or headless/libwawt 107 * 2. Setting "awt.toolkit" system property to use the appropriate Java toolkit class, 108 * (if user has specified the toolkit in env varialble) 109 */ 110 111 tkProp = (*env)->NewStringUTF(env, "awt.toolkit"); 112 geProp = (*env)->NewStringUTF(env, "java.awt.graphicsenv"); 113 fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager"); 114 /* Check if toolkit is specified in env variable */ 115 #ifdef MACOSX 116 envvar = getenv("AWT_TOOLKIT"); 117 if (envvar && strstr(envvar, "XToolkit")) { 118 #endif 119 toolkit = (*env)->NewStringUTF(env, "sun.awt.X11.XToolkit"); 120 grenv = (*env)->NewStringUTF(env, "sun.awt.X11GraphicsEnvironment"); 121 fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager"); 122 tk = "/xawt/libmawt"; 123 #ifdef MACOSX 124 } else { 125 toolkit = (*env)->NewStringUTF(env, "sun.lwawt.macosx.LWCToolkit"); 126 grenv = (*env)->NewStringUTF(env, "sun.awt.CGraphicsEnvironment"); 127 fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager"); 128 tk = "/lwawt/liblwawt"; 129 } 130 #endif 131 if (toolkit && tkProp) { 132 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty", 133 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 134 tkProp, toolkit); 135 } 136 if (grenv && geProp) { 137 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty", 138 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 139 geProp, grenv); 140 } 141 if (fmanager && fmProp) { 142 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty", 143 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 144 fmProp, fmanager); 145 } 146 147 /* Calculate library name to load */ 148 #ifndef MACOSX 149 if (AWTIsHeadless()) { 150 strcpy(p, "/headless/libmawt"); 151 } else if (tk) { 152 #endif 153 strcpy(p, tk); 154 #ifndef MACOSX 155 } 156 #endif 157 158 if (toolkit) { 159 (*env)->DeleteLocalRef(env, toolkit); 160 } 161 if (tkProp) { 162 (*env)->DeleteLocalRef(env, tkProp); 163 } 164 if (grenv) { 165 (*env)->DeleteLocalRef(env, grenv); 166 } 167 if (geProp) { 168 (*env)->DeleteLocalRef(env, geProp); 169 } 170 171 #ifdef MACOSX 172 strcat(p, ".dylib"); 173 #else 174 strcat(p, ".so"); 175 #endif 176 177 if (tk) { 178 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load", 179 "(Ljava/lang/String;)V", 180 JNU_NewStringPlatform(env, buf)); 181 awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL); 182 } 183 184 return JNI_VERSION_1_2; 185 } 186 187 JNIEXPORT jint JNICALL 188 JNI_OnLoad(JavaVM *vm, void *reserved) 189 { 190 return AWT_OnLoad(vm, reserved); 191 } 192 193 /* 194 * This entry point must remain in libawt.so as part of a contract 195 * with the CDE variant of Java Media Framework. (sdtjmplay) 196 * Reflect this call over to the correct libmawt.so. 197 */ 198 JNIEXPORT void JNICALL 199 Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this, 200 jobject frame, jstring jcommand) 201 { 202 /* type of the old backdoor function */ 203 typedef JNIEXPORT void JNICALL 204 XsessionWMcommand_type(JNIEnv *env, jobject this, 205 jobject frame, jstring jcommand); 206 207 static XsessionWMcommand_type *XsessionWMcommand = NULL; 208 209 if (XsessionWMcommand == NULL && awtHandle == NULL) { 210 return; 211 } 212 213 XsessionWMcommand = (XsessionWMcommand_type *) 214 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand"); 215 216 if (XsessionWMcommand == NULL) 217 return; 218 219 (*XsessionWMcommand)(env, this, frame, jcommand); 220 } 221 222 223 /* 224 * This entry point must remain in libawt.so as part of a contract 225 * with the CDE variant of Java Media Framework. (sdtjmplay) 226 * Reflect this call over to the correct libmawt.so. 227 */ 228 JNIEXPORT void JNICALL 229 Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv) 230 { 231 typedef JNIEXPORT void JNICALL 232 XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv); 233 234 static XsessionWMcommand_New_type *XsessionWMcommand = NULL; 235 236 if (XsessionWMcommand == NULL && awtHandle == NULL) { 237 return; 238 } 239 240 XsessionWMcommand = (XsessionWMcommand_New_type *) 241 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New"); 242 243 if (XsessionWMcommand == NULL) 244 return; 245 246 (*XsessionWMcommand)(env, jargv); 247 } 248 249 250 #define REFLECT_VOID_FUNCTION(name, arglist, paramlist) \ 251 typedef name##_type arglist; \ 252 void name arglist \ 253 { \ 254 static name##_type *name##_ptr = NULL; \ 255 if (name##_ptr == NULL && awtHandle == NULL) { \ 256 return; \ 257 } \ 258 name##_ptr = (name##_type *) \ 259 dlsym(awtHandle, #name); \ 260 if (name##_ptr == NULL) { \ 261 return; \ 262 } \ 263 (*name##_ptr)paramlist; \ 264 } 265 266 #define REFLECT_FUNCTION(return_type, name, arglist, paramlist) \ 267 typedef return_type name##_type arglist; \ 268 return_type name arglist \ 269 { \ 270 static name##_type *name##_ptr = NULL; \ 271 if (name##_ptr == NULL && awtHandle == NULL) { \ 272 return NULL; \ 273 } \ 274 name##_ptr = (name##_type *) \ 275 dlsym(awtHandle, #name); \ 276 if (name##_ptr == NULL) { \ 277 return NULL; \ 278 } \ 279 return (*name##_ptr)paramlist; \ 280 } 281 282 283 /* 284 * These entry point must remain in libawt.so ***for Java Plugin ONLY*** 285 * Reflect this call over to the correct libmawt.so. 286 */ 287 288 /* 289 * TODO: implement for LWCToolkit, MacOSX 290 */ 291 292 REFLECT_VOID_FUNCTION(getAwtLockFunctions, 293 (void (**AwtLock)(JNIEnv *), void (**AwtUnlock)(JNIEnv *), 294 void (**AwtNoFlushUnlock)(JNIEnv *), void *reserved), 295 (AwtLock, AwtUnlock, AwtNoFlushUnlock, reserved)) 296 297 REFLECT_VOID_FUNCTION(getAwtData, 298 (int32_t *awt_depth, Colormap *awt_cmap, Visual **awt_visual, 299 int32_t *awt_num_colors, void *pReserved), 300 (awt_depth, awt_cmap, awt_visual, 301 awt_num_colors, pReserved)) 302 303 REFLECT_FUNCTION(Display *, getAwtDisplay, (void), ())