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