1 /*
   2  * Copyright (c) 2004, 2018, 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     char *filename_str = NULL;
 117     GError **error = NULL;
 118 
 119     if (filename == NULL)
 120     {
 121         return JNI_FALSE;
 122     }
 123 
 124     len = (*env)->GetStringUTFLength(env, filename);

 125     filename_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 126             sizeof(char), len + 1);
 127     if (filename_str == NULL) {
 128         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 129         return JNI_FALSE;
 130     }
 131     if (!init_method(env, this) ) {
 132         free(filename_str);
 133         return JNI_FALSE;
 134     }
 135     (*env)->GetStringUTFRegion(env, filename, 0, len, filename_str);
 136     jboolean result = gtk->get_file_icon_data(env, filename_str, error,
 137                                             icon_upcall_method, this);
 138 
 139     /* Release the strings we've allocated. */
 140     free(filename_str);
 141 
 142     return result;
 143 #else /* HEADLESS */
 144     return JNI_FALSE;
 145 #endif /* !HEADLESS */
 146 }
 147 
 148 /*
 149  * Class:     sun_awt_UNIXToolkit
 150  * Method:    load_stock_icon
 151  * Signature: (ILjava/lang/String;IILjava/lang/String;)Z
 152  *
 153  * This method assumes that GTK libs are present.
 154  */
 155 JNIEXPORT jboolean JNICALL
 156 Java_sun_awt_UNIXToolkit_load_1stock_1icon(JNIEnv *env, jobject this,
 157         jint widget_type, jstring stock_id, jint icon_size,
 158         jint text_direction, jstring detail)
 159 {
 160 #ifndef HEADLESS
 161     int len;

 162     char *stock_id_str = NULL;
 163     char *detail_str = NULL;
 164     jboolean result = JNI_FALSE;
 165 
 166     if (stock_id == NULL)
 167     {
 168         return JNI_FALSE;
 169     }
 170 
 171     len = (*env)->GetStringUTFLength(env, stock_id);

 172     stock_id_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 173             sizeof(char), len + 1);
 174     if (stock_id_str == NULL) {
 175         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 176         return JNI_FALSE;
 177     }
 178     (*env)->GetStringUTFRegion(env, stock_id, 0, len, stock_id_str);
 179 
 180     /* Detail isn't required so check for NULL. */
 181     if (detail != NULL)
 182     {
 183         len = (*env)->GetStringUTFLength(env, detail);

 184         detail_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 185                 sizeof(char), len + 1);
 186         if (detail_str == NULL) {
 187             free(stock_id_str);
 188             JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 189             return JNI_FALSE;
 190         }
 191         (*env)->GetStringUTFRegion(env, detail, 0, len, detail_str);
 192     }
 193 
 194     if (init_method(env, this)) {
 195         result = gtk->get_icon_data(env, widget_type, stock_id_str,
 196                                     icon_size, text_direction, detail_str,
 197                                     icon_upcall_method, this);
 198     }
 199     /* Release the strings we've allocated. */
 200     free(stock_id_str);
 201     free(detail_str);
 202 
 203     return result;
 204 #else /* HEADLESS */
 205     return JNI_FALSE;
 206 #endif /* !HEADLESS */
 207 }
 208 
 209 /*
 210  * Class:     sun_awt_UNIXToolkit
 211  * Method:    nativeSync
 212  * Signature: ()V
 213  */
 214 JNIEXPORT void JNICALL
 215 Java_sun_awt_UNIXToolkit_nativeSync(JNIEnv *env, jobject this)
 216 {
 217 #ifndef HEADLESS
 218     AWT_LOCK();
 219     XSync(awt_display, False);
 220     AWT_UNLOCK();
 221 #endif /* !HEADLESS */
 222 }
 223 
 224 /*
 225  * Class:     sun_awt_SunToolkit
 226  * Method:    closeSplashScreen
 227  * Signature: ()V
 228  */
 229 JNIEXPORT void JNICALL
 230 Java_sun_awt_SunToolkit_closeSplashScreen(JNIEnv *env, jclass cls)
 231 {
 232     typedef void (*SplashClose_t)();
 233     SplashClose_t splashClose;
 234     void* hSplashLib = dlopen(0, RTLD_LAZY);
 235     if (!hSplashLib) {
 236         return;
 237     }
 238     splashClose = (SplashClose_t)dlsym(hSplashLib,
 239         "SplashClose");
 240     if (splashClose) {
 241         splashClose();
 242     }
 243     dlclose(hSplashLib);
 244 }
 245 
 246 /*
 247  * Class:     sun_awt_UNIXToolkit
 248  * Method:    gtkCheckVersionImpl
 249  * Signature: (III)Ljava/lang/String;
 250  */
 251 JNIEXPORT jboolean JNICALL
 252 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this,
 253         jint major, jint minor, jint micro)
 254 {
 255     char *ret;
 256 
 257     ret = gtk->gtk_check_version(major, minor, micro);
 258     if (ret == NULL) {
 259         return TRUE;
 260     }
 261 
 262     return FALSE;
 263 }
 264 
 265 /*
 266  * Class:     sun_awt_UNIXToolkit
 267  * Method:    get_gtk_version
 268  * Signature: ()I
 269  */
 270 JNIEXPORT jint JNICALL
 271 Java_sun_awt_UNIXToolkit_get_1gtk_1version(JNIEnv *env, jclass klass)
 272 {
 273 #ifndef HEADLESS
 274     return gtk ? gtk->version : GTK_ANY;
 275 #else
 276     return GTK_ANY;
 277 #endif /* !HEADLESS */
 278 }
--- EOF ---