1 /* 2 * Copyright (c) 2000, 2014, 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 #if defined(AIX) 80 jstring propname = NULL; 81 jstring bootlibpath = NULL; 82 const char* libpath; 83 #else 84 Dl_info dlinfo; 85 #endif 86 char buf[MAXPATHLEN]; 87 int32_t len; 88 char *p, *tk = 0; 89 JNI_OnLoad_type *JNI_OnLoad_ptr; 90 struct utsname name; 91 JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2); 92 void *v; 93 char *envvar; 94 jstring fmanager = NULL; 95 jstring fmProp = NULL; 96 97 if (awtHandle != NULL) { 98 /* Avoid several loading attempts */ 99 return JNI_VERSION_1_2; 100 } 101 102 jvm = vm; 103 104 #if defined(AIX) 105 propname = (*env)->NewStringUTF(env, "sun.boot.library.path"); 106 bootlibpath = JNU_CallStaticMethodByName (env, 107 NULL, 108 "java/lang/System", 109 "getProperty", 110 "(Ljava/lang/String;)Ljava/lang/String;", 111 propname).l; 112 libpath = (*env)->GetStringUTFChars(env,bootlibpath,0); 113 114 strncpy(buf,libpath,MAXPATHLEN-1); 115 (*env)->ReleaseStringUTFChars(env,bootlibpath,libpath); 116 117 // IF condition is added to check if multiple paths are specified in the 118 // sun.boot.library.path as in the case of sidecar VM configurations. 119 // Just assume the presence of : as indicator of multiple paths 120 if(strchr(buf,':') != NULL) 121 { 122 123 char *temp_buf = (char *)malloc(sizeof(buf)+1); 124 char *token = (char *)malloc(sizeof(buf)+1); 125 struct stat sb; 126 127 temp_buf = strtok(buf, ":"); 128 strcpy(token,temp_buf); 129 strcat(token,"/xawt/libmawt.so"); 130 if(stat(token, &sb) == 0) 131 { 132 strcpy(buf,temp_buf); 133 } 134 else 135 { 136 while((temp_buf = strtok(NULL,":")) != NULL) 137 { 138 strcpy(token,temp_buf); 139 strcat(token,"/xawt/libmawt.so"); 140 if(stat(token, &sb) == 0) 141 { 142 strcpy(buf,temp_buf); 143 break; 144 } 145 } 146 } 147 } 148 149 p = buf + strlen(buf); 150 len = strlen(buf); 151 (*env)->DeleteLocalRef(env,bootlibpath); 152 bootlibpath = NULL; 153 (*env)->DeleteLocalRef(env,propname); 154 propname = NULL; 155 #else 156 /* Get address of this library and the directory containing it. */ 157 dladdr((void *)AWT_OnLoad, &dlinfo); 158 realpath((char *)dlinfo.dli_fname, buf); 159 len = strlen(buf); 160 p = strrchr(buf, '/'); 161 #endif 162 163 /* 164 * 1. Set the "sun.font.fontmanager" system property, 165 * 2. Choose the library image name. 166 */ 167 168 fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager"); 169 /* Check if toolkit is specified in env variable */ 170 #ifdef MACOSX 171 envvar = getenv("AWT_TOOLKIT"); 172 if (envvar && strstr(envvar, "XToolkit")) { 173 #endif 174 fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager"); 175 tk = "/xawt/libmawt"; 176 #ifdef MACOSX 177 } else { 178 fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager"); 179 tk = "/lwawt/liblwawt"; 180 } 181 #endif 182 if (fmanager && fmProp) { 183 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty", 184 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 185 fmProp, fmanager); 186 } 187 188 /* Calculate library name to load */ 189 #ifndef MACOSX 190 if (AWTIsHeadless()) { 191 strcpy(p, "/headless/libmawt"); 192 } else if (tk) { 193 #endif 194 strcpy(p, tk); 195 #ifndef MACOSX 196 } 197 #endif 198 199 #ifdef MACOSX 200 strcat(p, ".dylib"); 201 #else 202 strcat(p, ".so"); 203 #endif 204 205 if (tk) { 206 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load", 207 "(Ljava/lang/String;)V", 208 JNU_NewStringPlatform(env, buf)); 209 awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL); 210 } 211 212 return JNI_VERSION_1_2; 213 } 214 215 JNIEXPORT jint JNICALL 216 JNI_OnLoad(JavaVM *vm, void *reserved) 217 { 218 return AWT_OnLoad(vm, reserved); 219 } 220 221 /* 222 * This entry point must remain in libawt.so as part of a contract 223 * with the CDE variant of Java Media Framework. (sdtjmplay) 224 * Reflect this call over to the correct libmawt.so. 225 */ 226 JNIEXPORT void JNICALL 227 Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this, 228 jobject frame, jstring jcommand) 229 { 230 /* type of the old backdoor function */ 231 typedef JNIEXPORT void JNICALL 232 XsessionWMcommand_type(JNIEnv *env, jobject this, 233 jobject frame, jstring jcommand); 234 235 static XsessionWMcommand_type *XsessionWMcommand = NULL; 236 237 if (XsessionWMcommand == NULL && awtHandle == NULL) { 238 return; 239 } 240 241 XsessionWMcommand = (XsessionWMcommand_type *) 242 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand"); 243 244 if (XsessionWMcommand == NULL) 245 return; 246 247 (*XsessionWMcommand)(env, this, frame, jcommand); 248 } 249 250 251 /* 252 * This entry point must remain in libawt.so as part of a contract 253 * with the CDE variant of Java Media Framework. (sdtjmplay) 254 * Reflect this call over to the correct libmawt.so. 255 */ 256 JNIEXPORT void JNICALL 257 Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv) 258 { 259 typedef JNIEXPORT void JNICALL 260 XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv); 261 262 static XsessionWMcommand_New_type *XsessionWMcommand = NULL; 263 264 if (XsessionWMcommand == NULL && awtHandle == NULL) { 265 return; 266 } 267 268 XsessionWMcommand = (XsessionWMcommand_New_type *) 269 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New"); 270 271 if (XsessionWMcommand == NULL) 272 return; 273 274 (*XsessionWMcommand)(env, jargv); 275 } 276 277 278 #define REFLECT_VOID_FUNCTION(name, arglist, paramlist) \ 279 typedef name##_type arglist; \ 280 void name arglist \ 281 { \ 282 static name##_type *name##_ptr = NULL; \ 283 if (name##_ptr == NULL && awtHandle == NULL) { \ 284 return; \ 285 } \ 286 name##_ptr = (name##_type *) \ 287 dlsym(awtHandle, #name); \ 288 if (name##_ptr == NULL) { \ 289 return; \ 290 } \ 291 (*name##_ptr)paramlist; \ 292 } 293 294 #define REFLECT_FUNCTION(return_type, name, arglist, paramlist) \ 295 typedef return_type name##_type arglist; \ 296 return_type name arglist \ 297 { \ 298 static name##_type *name##_ptr = NULL; \ 299 if (name##_ptr == NULL && awtHandle == NULL) { \ 300 return NULL; \ 301 } \ 302 name##_ptr = (name##_type *) \ 303 dlsym(awtHandle, #name); \ 304 if (name##_ptr == NULL) { \ 305 return NULL; \ 306 } \ 307 return (*name##_ptr)paramlist; \ 308 } 309 310 311 /* 312 * These entry point must remain in libawt.so ***for Java Plugin ONLY*** 313 * Reflect this call over to the correct libmawt.so. 314 */ 315 316 /* 317 * TODO: implement for LWCToolkit, MacOSX 318 */ 319 320 REFLECT_VOID_FUNCTION(getAwtLockFunctions, 321 (void (**AwtLock)(JNIEnv *), void (**AwtUnlock)(JNIEnv *), 322 void (**AwtNoFlushUnlock)(JNIEnv *), void *reserved), 323 (AwtLock, AwtUnlock, AwtNoFlushUnlock, reserved)) 324 325 REFLECT_VOID_FUNCTION(getAwtData, 326 (int32_t *awt_depth, Colormap *awt_cmap, Visual **awt_visual, 327 int32_t *awt_num_colors, void *pReserved), 328 (awt_depth, awt_cmap, awt_visual, 329 awt_num_colors, pReserved)) 330 331 REFLECT_FUNCTION(Display *, getAwtDisplay, (void), ())