1 /*
   2  * Copyright (c) 2004, 2019, 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 <stdlib.h>
  27 #include <string.h>
  28 #include <unistd.h>
  29 #include <dlfcn.h>
  30 
  31 #include <jni.h>
  32 #include <sizecalc.h>
  33 #include "sun_awt_UNIXToolkit.h"
  34 
  35 #ifndef HEADLESS
  36 #include "awt.h"
  37 #include "gtk_interface.h"
  38 #endif /* !HEADLESS */
  39 
  40 
  41 static jclass this_class = NULL;
  42 static jmethodID icon_upcall_method = NULL;
  43 
  44 
  45 /*
  46  * Class:     sun_awt_UNIXToolkit
  47  * Method:    check_gtk
  48  * Signature: (I)Z
  49  */
  50 JNIEXPORT jboolean JNICALL
  51 Java_sun_awt_UNIXToolkit_check_1gtk(JNIEnv *env, jclass klass, jint version) {
  52 #ifndef HEADLESS
  53     return (jboolean)gtk_check_version(version);
  54 #else
  55     return JNI_FALSE;
  56 #endif /* !HEADLESS */
  57 }
  58 
  59 
  60 /*
  61  * Class:     sun_awt_UNIXToolkit
  62  * Method:    load_gtk
  63  * Signature: (I)Z
  64  */
  65 JNIEXPORT jboolean JNICALL
  66 Java_sun_awt_UNIXToolkit_load_1gtk(JNIEnv *env, jclass klass, jint version,
  67                                                              jboolean verbose) {
  68 #ifndef HEADLESS
  69     return (jboolean)gtk_load(env, version, verbose);
  70 #else
  71     return JNI_FALSE;
  72 #endif /* !HEADLESS */
  73 }
  74 
  75 
  76 /*
  77  * Class:     sun_awt_UNIXToolkit
  78  * Method:    unload_gtk
  79  * Signature: ()Z
  80  */
  81 JNIEXPORT jboolean JNICALL
  82 Java_sun_awt_UNIXToolkit_unload_1gtk(JNIEnv *env, jclass klass)
  83 {
  84 #ifndef HEADLESS
  85     return (jboolean)gtk->unload();
  86 #else
  87     return JNI_FALSE;
  88 #endif /* !HEADLESS */
  89 }
  90 
  91 jboolean init_method(JNIEnv *env, jobject this)
  92 {
  93     if (this_class == NULL) {
  94         this_class = (*env)->NewGlobalRef(env,
  95                                           (*env)->GetObjectClass(env, this));
  96         icon_upcall_method = (*env)->GetMethodID(env, this_class,
  97                                  "loadIconCallback", "([BIIIIIZ)V");
  98         CHECK_NULL_RETURN(icon_upcall_method, JNI_FALSE);
  99     }
 100     return JNI_TRUE;
 101 }
 102 
 103 /*
 104  * Class:     sun_awt_UNIXToolkit
 105  * Method:    load_gtk_icon
 106  * Signature: (Ljava/lang/String)Z
 107  *
 108  * This method assumes that GTK libs are present.
 109  */
 110 JNIEXPORT jboolean JNICALL
 111 Java_sun_awt_UNIXToolkit_load_1gtk_1icon(JNIEnv *env, jobject this,
 112         jstring filename)
 113 {
 114 #ifndef HEADLESS
 115     int len;
 116     jsize jlen;
 117     char *filename_str = NULL;
 118     GError **error = NULL;
 119 
 120     if (filename == NULL)
 121     {
 122         return JNI_FALSE;
 123     }
 124 
 125     len = (*env)->GetStringUTFLength(env, filename);
 126     jlen = (*env)->GetStringLength(env, filename);
 127     filename_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 128             sizeof(char), len + 1);
 129     if (filename_str == NULL) {
 130         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 131         return JNI_FALSE;
 132     }
 133     if (!init_method(env, this) ) {
 134         free(filename_str);
 135         return JNI_FALSE;
 136     }
 137     (*env)->GetStringUTFRegion(env, filename, 0, jlen, filename_str);
 138     jboolean result = gtk->get_file_icon_data(env, filename_str, error,
 139                                             icon_upcall_method, this);
 140 
 141     /* Release the strings we've allocated. */
 142     free(filename_str);
 143 
 144     return result;
 145 #else /* HEADLESS */
 146     return JNI_FALSE;
 147 #endif /* !HEADLESS */
 148 }
 149 
 150 /*
 151  * Class:     sun_awt_UNIXToolkit
 152  * Method:    load_stock_icon
 153  * Signature: (ILjava/lang/String;IILjava/lang/String;)Z
 154  *
 155  * This method assumes that GTK libs are present.
 156  */
 157 JNIEXPORT jboolean JNICALL
 158 Java_sun_awt_UNIXToolkit_load_1stock_1icon(JNIEnv *env, jobject this,
 159         jint widget_type, jstring stock_id, jint icon_size,
 160         jint text_direction, jstring detail)
 161 {
 162 #ifndef HEADLESS
 163     int len;
 164     jsize jlen;
 165     char *stock_id_str = NULL;
 166     char *detail_str = NULL;
 167     jboolean result = JNI_FALSE;
 168 
 169     if (stock_id == NULL)
 170     {
 171         return JNI_FALSE;
 172     }
 173 
 174     len = (*env)->GetStringUTFLength(env, stock_id);
 175     jlen = (*env)->GetStringLength(env, stock_id);
 176     stock_id_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 177             sizeof(char), len + 1);
 178     if (stock_id_str == NULL) {
 179         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 180         return JNI_FALSE;
 181     }
 182     (*env)->GetStringUTFRegion(env, stock_id, 0, jlen, stock_id_str);
 183 
 184     /* Detail isn't required so check for NULL. */
 185     if (detail != NULL)
 186     {
 187         len = (*env)->GetStringUTFLength(env, detail);
 188         jlen = (*env)->GetStringLength(env, detail);
 189         detail_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 190                 sizeof(char), len + 1);
 191         if (detail_str == NULL) {
 192             free(stock_id_str);
 193             JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 194             return JNI_FALSE;
 195         }
 196         (*env)->GetStringUTFRegion(env, detail, 0, jlen, detail_str);
 197     }
 198 
 199     if (init_method(env, this)) {
 200         result = gtk->get_icon_data(env, widget_type, stock_id_str,
 201                                     icon_size, text_direction, detail_str,
 202                                     icon_upcall_method, this);
 203     }
 204     /* Release the strings we've allocated. */
 205     free(stock_id_str);
 206     free(detail_str);
 207 
 208     return result;
 209 #else /* HEADLESS */
 210     return JNI_FALSE;
 211 #endif /* !HEADLESS */
 212 }
 213 
 214 /*
 215  * Class:     sun_awt_UNIXToolkit
 216  * Method:    nativeSync
 217  * Signature: ()V
 218  */
 219 JNIEXPORT void JNICALL
 220 Java_sun_awt_UNIXToolkit_nativeSync(JNIEnv *env, jobject this)
 221 {
 222 #ifndef HEADLESS
 223     AWT_LOCK();
 224     XSync(awt_display, False);
 225     AWT_UNLOCK();
 226 #endif /* !HEADLESS */
 227 }
 228 
 229 /*
 230  * Class:     sun_awt_SunToolkit
 231  * Method:    closeSplashScreen
 232  * Signature: ()V
 233  */
 234 JNIEXPORT void JNICALL
 235 Java_sun_awt_SunToolkit_closeSplashScreen(JNIEnv *env, jclass cls)
 236 {
 237     typedef void (*SplashClose_t)();
 238     SplashClose_t splashClose;
 239     void* hSplashLib = dlopen(0, RTLD_LAZY);
 240     if (!hSplashLib) {
 241         return;
 242     }
 243     splashClose = (SplashClose_t)dlsym(hSplashLib,
 244         "SplashClose");
 245     if (splashClose) {
 246         splashClose();
 247     }
 248     dlclose(hSplashLib);
 249 }
 250 
 251 /*
 252  * Class:     sun_awt_UNIXToolkit
 253  * Method:    gtkCheckVersionImpl
 254  * Signature: (III)Ljava/lang/String;
 255  */
 256 JNIEXPORT jboolean JNICALL
 257 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this,
 258         jint major, jint minor, jint micro)
 259 {
 260     char *ret;
 261 
 262     ret = gtk->gtk_check_version(major, minor, micro);
 263     if (ret == NULL) {
 264         return TRUE;
 265     }
 266 
 267     return FALSE;
 268 }
 269 
 270 /*
 271  * Class:     sun_awt_UNIXToolkit
 272  * Method:    get_gtk_version
 273  * Signature: ()I
 274  */
 275 JNIEXPORT jint JNICALL
 276 Java_sun_awt_UNIXToolkit_get_1gtk_1version(JNIEnv *env, jclass klass)
 277 {
 278 #ifndef HEADLESS
 279     return gtk ? gtk->version : GTK_ANY;
 280 #else
 281     return GTK_ANY;
 282 #endif /* !HEADLESS */
 283 }
--- EOF ---