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")) {
 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 #ifndef MACOSX
 131     if (AWTIsHeadless()) {
 132         strcpy(p, "/headless/libmawt");
 133     } else if (tk) {
 134 #endif
 135         strcpy(p, tk);
 136 #ifndef MACOSX
 137     }
 138 #endif
 139 
 140 #ifdef MACOSX
 141     strcat(p, ".dylib");
 142 #else
 143     strcat(p, ".so");
 144 #endif
 145 
 146     if (tk) {
 147         JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",
 148                                    "(Ljava/lang/String;)V",
 149                                    JNU_NewStringPlatform(env, buf));
 150             awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
 151     }
 152 
 153     return JNI_VERSION_1_2;
 154 }
 155 
 156 JNIEXPORT jint JNICALL
 157 JNI_OnLoad(JavaVM *vm, void *reserved)
 158 {
 159     return AWT_OnLoad(vm, reserved);
 160 }
 161 
 162 /*
 163  * This entry point must remain in libawt.so as part of a contract
 164  * with the CDE variant of Java Media Framework. (sdtjmplay)
 165  * Reflect this call over to the correct libmawt.so.
 166  */
 167 JNIEXPORT void JNICALL
 168 Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,
 169                                      jobject frame, jstring jcommand)
 170 {
 171     /* type of the old backdoor function */
 172     typedef JNIEXPORT void JNICALL
 173         XsessionWMcommand_type(JNIEnv *env, jobject this,
 174                                jobject frame, jstring jcommand);
 175 
 176     static XsessionWMcommand_type *XsessionWMcommand = NULL;
 177 
 178     if (XsessionWMcommand == NULL && awtHandle == NULL) {
 179         return;
 180     }
 181 
 182     XsessionWMcommand = (XsessionWMcommand_type *)
 183         dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand");
 184 
 185     if (XsessionWMcommand == NULL)
 186         return;
 187 
 188     (*XsessionWMcommand)(env, this, frame, jcommand);
 189 }
 190 
 191 
 192 /*
 193  * This entry point must remain in libawt.so as part of a contract
 194  * with the CDE variant of Java Media Framework. (sdtjmplay)
 195  * Reflect this call over to the correct libmawt.so.
 196  */
 197 JNIEXPORT void JNICALL
 198 Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv)
 199 {
 200     typedef JNIEXPORT void JNICALL
 201         XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv);
 202 
 203     static XsessionWMcommand_New_type *XsessionWMcommand = NULL;
 204 
 205     if (XsessionWMcommand == NULL && awtHandle == NULL) {
 206         return;
 207     }
 208 
 209     XsessionWMcommand = (XsessionWMcommand_New_type *)
 210         dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New");
 211 
 212     if (XsessionWMcommand == NULL)
 213         return;
 214 
 215     (*XsessionWMcommand)(env, jargv);
 216 }
 217 
 218 
 219 #define REFLECT_VOID_FUNCTION(name, arglist, paramlist)                 \
 220 typedef name##_type arglist;                                            \
 221 void name arglist                                                       \
 222 {                                                                       \
 223     static name##_type *name##_ptr = NULL;                              \
 224     if (name##_ptr == NULL && awtHandle == NULL) {                      \
 225         return;                                                         \
 226     }                                                                   \
 227     name##_ptr = (name##_type *)                                        \
 228         dlsym(awtHandle, #name);                                        \
 229     if (name##_ptr == NULL) {                                           \
 230         return;                                                         \
 231     }                                                                   \
 232     (*name##_ptr)paramlist;                                             \
 233 }
 234 
 235 #define REFLECT_FUNCTION(return_type, name, arglist, paramlist)         \
 236 typedef return_type name##_type arglist;                                \
 237 return_type name arglist                                                \
 238 {                                                                       \
 239     static name##_type *name##_ptr = NULL;                              \
 240     if (name##_ptr == NULL && awtHandle == NULL) {                      \
 241         return NULL;                                                    \
 242     }                                                                   \
 243     name##_ptr = (name##_type *)                                        \
 244         dlsym(awtHandle, #name);                                        \
 245     if (name##_ptr == NULL) {                                           \
 246         return NULL;                                                    \
 247     }                                                                   \
 248     return (*name##_ptr)paramlist;                                      \
 249 }
 250 
 251 
 252 /*
 253  * These entry point must remain in libawt.so ***for Java Plugin ONLY***
 254  * Reflect this call over to the correct libmawt.so.
 255  */
 256 
 257 /*
 258  * TODO: implement for LWCToolkit, MacOSX
 259  */
 260 
 261 REFLECT_VOID_FUNCTION(getAwtLockFunctions,
 262                       (void (**AwtLock)(JNIEnv *), void (**AwtUnlock)(JNIEnv *),
 263                        void (**AwtNoFlushUnlock)(JNIEnv *), void *reserved),
 264                       (AwtLock, AwtUnlock, AwtNoFlushUnlock, reserved))
 265 
 266 REFLECT_VOID_FUNCTION(getAwtData,
 267                       (int32_t *awt_depth, Colormap *awt_cmap, Visual **awt_visual,
 268                        int32_t *awt_num_colors, void *pReserved),
 269                       (awt_depth, awt_cmap, awt_visual,
 270                        awt_num_colors, pReserved))
 271 
 272 REFLECT_FUNCTION(Display *, getAwtDisplay, (void), ())