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