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 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 /* 77 * Pathnames to the various awt toolkits 78 */ 79 80 81 #ifdef MACOSX 82 #define XAWT_PATH "/libawt_xawt.dylib" 83 #define LWAWT_PATH "/libawt_lwawt.dylib" 84 #define DEFAULT_PATH LWAWT_PATH 85 #define HEADLESS_PATH "/libawt_headless.dylib" 86 #else 87 #define XAWT_PATH "/libawt_xawt.so" 88 #define DEFAULT_PATH XAWT_PATH 89 #define HEADLESS_PATH "/libawt_headless.so" 90 #endif 91 92 jint 93 AWT_OnLoad(JavaVM *vm, void *reserved) 94 { 95 Dl_info dlinfo; 96 char buf[MAXPATHLEN]; 97 int32_t len; 98 char *p, *tk; 99 JNI_OnLoad_type *JNI_OnLoad_ptr; 100 struct utsname name; 101 JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2); 102 void *v; 103 char *envvar; 104 int xt_before_xm = 0; 105 int XAWT = 0; 106 jstring toolkit = NULL; 107 jstring propname = NULL; 108 jstring fmanager = NULL; 109 jstring fmProp = NULL; 110 111 if (awtHandle != NULL) { 112 /* Avoid several loading attempts */ 113 return JNI_VERSION_1_2; 114 } 115 116 jvm = vm; 117 118 /* Get address of this library and the directory containing it. */ 119 dladdr((void *)JNI_OnLoad, &dlinfo); 120 realpath((char *)dlinfo.dli_fname, buf); 121 len = strlen(buf); 122 p = strrchr(buf, '/'); 123 124 /* 125 * The code below is responsible for: 126 * 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless 127 * 2. Setting "awt.toolkit" system property to use the appropriate Java toolkit class, 128 * (if user has specified the toolkit in env varialble) 129 */ 130 131 propname = (*env)->NewStringUTF(env, "awt.toolkit"); 132 fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager"); 133 tk = DEFAULT_PATH; /* default value, may be changed below */ 134 135 /* Check if toolkit is specified in env variable */ 136 envvar = getenv("AWT_TOOLKIT"); 137 if (envvar && (strstr(envvar, "XToolkit"))) { 138 toolkit = (*env)->NewStringUTF(env, "sun.awt.X11.XToolkit"); 139 tk = XAWT_PATH; 140 fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager"); 141 #ifdef MACOSX 142 } else { 143 fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager"); 144 tk = LWAWT_PATH; 145 #endif 146 } 147 /* If user specified toolkit then set java system property */ 148 if (toolkit && propname) { 149 JNU_CallStaticMethodByName (env, 150 NULL, 151 "java/lang/System", 152 "setProperty", 153 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 154 propname,toolkit); 155 } 156 if (fmanager && fmProp) { 157 JNU_CallStaticMethodByName (env, 158 NULL, 159 "java/lang/System", 160 "setProperty", 161 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", 162 fmProp, fmanager); 163 } 164 165 /* Calculate library name to load */ 166 if (AWTIsHeadless()) { 167 tk = HEADLESS_PATH; 168 } 169 strncpy(p, tk, MAXPATHLEN-len-1); 170 171 if (toolkit) { 172 (*env)->DeleteLocalRef(env, toolkit); 173 } 174 if (propname) { 175 (*env)->DeleteLocalRef(env, propname); 176 } 177 if (fmProp) { 178 (*env)->DeleteLocalRef(env, fmProp); 179 } 180 if (fmanager) { 181 (*env)->DeleteLocalRef(env, fmanager); 182 } 183 184 JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load", 185 "(Ljava/lang/String;)V", 186 JNU_NewStringPlatform(env, buf)); 187 188 awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL); 189 190 return JNI_VERSION_1_2; 191 } 192 193 JNIEXPORT jint JNICALL 194 JNI_OnLoad(JavaVM *vm, void *reserved) 195 { 196 return AWT_OnLoad(vm, reserved); 197 } 198 199 /* 200 * This entry point must remain in libawt.so as part of a contract 201 * with the CDE variant of Java Media Framework. (sdtjmplay) 202 * Reflect this call over to the correct libawt_<toolkit>.so. 203 */ 204 JNIEXPORT void JNICALL 205 Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this, 206 jobject frame, jstring jcommand) 207 { 208 /* type of the old backdoor function */ 209 typedef void JNICALL 210 XsessionWMcommand_type(JNIEnv *env, jobject this, 211 jobject frame, jstring jcommand); 212 213 static XsessionWMcommand_type *XsessionWMcommand = NULL; 214 215 if (XsessionWMcommand == NULL && awtHandle == NULL) { 216 return; 217 } 218 219 XsessionWMcommand = (XsessionWMcommand_type *) 220 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand"); 221 222 if (XsessionWMcommand == NULL) 223 return; 224 225 (*XsessionWMcommand)(env, this, frame, jcommand); 226 } 227 228 229 /* 230 * This entry point must remain in libawt.so as part of a contract 231 * with the CDE variant of Java Media Framework. (sdtjmplay) 232 * Reflect this call over to the correct libawt_<toolkit>.so. 233 */ 234 JNIEXPORT void JNICALL 235 Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv) 236 { 237 typedef void JNICALL 238 XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv); 239 240 static XsessionWMcommand_New_type *XsessionWMcommand = NULL; 241 242 if (XsessionWMcommand == NULL && awtHandle == NULL) { 243 return; 244 } 245 246 XsessionWMcommand = (XsessionWMcommand_New_type *) 247 dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New"); 248 249 if (XsessionWMcommand == NULL) 250 return; 251 252 (*XsessionWMcommand)(env, jargv); 253 } 254 255 256 #define REFLECT_VOID_FUNCTION(name, arglist, paramlist) \ 257 typedef name##_type arglist; \ 258 void name arglist \ 259 { \ 260 static name##_type *name##_ptr = NULL; \ 261 if (name##_ptr == NULL && awtHandle == NULL) { \ 262 return; \ 263 } \ 264 name##_ptr = (name##_type *) \ 265 dlsym(awtHandle, #name); \ 266 if (name##_ptr == NULL) { \ 267 return; \ 268 } \ 269 (*name##_ptr)paramlist; \ 270 } 271 272 #define REFLECT_FUNCTION(return_type, name, arglist, paramlist) \ 273 typedef return_type name##_type arglist; \ 274 return_type name arglist \ 275 { \ 276 static name##_type *name##_ptr = NULL; \ 277 if (name##_ptr == NULL && awtHandle == NULL) { \ 278 return NULL; \ 279 } \ 280 name##_ptr = (name##_type *) \ 281 dlsym(awtHandle, #name); \ 282 if (name##_ptr == NULL) { \ 283 return NULL; \ 284 } \ 285 return (*name##_ptr)paramlist; \ 286 } 287 288 289 /* 290 * These entry point must remain in libawt.so ***for Java Plugin ONLY*** 291 * Reflect this call over to the correct libawt_<toolkit>.so. 292 */ 293 294 REFLECT_VOID_FUNCTION(getAwtLockFunctions, 295 (void (**AwtLock)(JNIEnv *), void (**AwtUnlock)(JNIEnv *), 296 void (**AwtNoFlushUnlock)(JNIEnv *), void *reserved), 297 (AwtLock, AwtUnlock, AwtNoFlushUnlock, reserved)) 298 299 REFLECT_VOID_FUNCTION(getAwtData, 300 (int32_t *awt_depth, Colormap *awt_cmap, Visual **awt_visual, 301 int32_t *awt_num_colors, void *pReserved), 302 (awt_depth, awt_cmap, awt_visual, 303 awt_num_colors, pReserved)) 304 305 REFLECT_FUNCTION(Display *, getAwtDisplay, (void), ())