1 /*
   2  * Copyright (c) 2004, 2020, 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 #ifdef HEADLESS
  27     #error This file should not be included in headless library
  28 #endif
  29 
  30 #include <stdlib.h>
  31 #include <string.h>
  32 #include <unistd.h>
  33 #include <dlfcn.h>
  34 
  35 #include <jni.h>
  36 #include <sizecalc.h>
  37 #include "sun_awt_UNIXToolkit.h"
  38 

  39 #include "awt.h"
  40 #include "gtk_interface.h"


  41 
  42 static jclass this_class = NULL;
  43 static jmethodID icon_upcall_method = NULL;
  44 
  45 
  46 /*
  47  * Class:     sun_awt_UNIXToolkit
  48  * Method:    check_gtk
  49  * Signature: (I)Z
  50  */
  51 JNIEXPORT jboolean JNICALL
  52 Java_sun_awt_UNIXToolkit_check_1gtk(JNIEnv *env, jclass klass, jint version) {

  53     return (jboolean)gtk_check_version(version);



  54 }
  55 
  56 
  57 /*
  58  * Class:     sun_awt_UNIXToolkit
  59  * Method:    load_gtk
  60  * Signature: (I)Z
  61  */
  62 JNIEXPORT jboolean JNICALL
  63 Java_sun_awt_UNIXToolkit_load_1gtk(JNIEnv *env, jclass klass, jint version,
  64                                                              jboolean verbose) {

  65     return (jboolean)gtk_load(env, version, verbose);



  66 }
  67 

  68 /*
  69  * Class:     sun_awt_UNIXToolkit
  70  * Method:    unload_gtk
  71  * Signature: ()Z
  72  */
  73 JNIEXPORT jboolean JNICALL
  74 Java_sun_awt_UNIXToolkit_unload_1gtk(JNIEnv *env, jclass klass)
  75 {

  76     return (jboolean)gtk->unload();



  77 }
  78 
  79 jboolean init_method(JNIEnv *env, jobject this)
  80 {
  81     if (this_class == NULL) {
  82         this_class = (*env)->NewGlobalRef(env,
  83                                           (*env)->GetObjectClass(env, this));
  84         icon_upcall_method = (*env)->GetMethodID(env, this_class,
  85                                  "loadIconCallback", "([BIIIIIZ)V");
  86         CHECK_NULL_RETURN(icon_upcall_method, JNI_FALSE);
  87     }
  88     return JNI_TRUE;
  89 }
  90 
  91 /*
  92  * Class:     sun_awt_UNIXToolkit
  93  * Method:    load_gtk_icon
  94  * Signature: (Ljava/lang/String)Z
  95  *
  96  * This method assumes that GTK libs are present.
  97  */
  98 JNIEXPORT jboolean JNICALL
  99 Java_sun_awt_UNIXToolkit_load_1gtk_1icon(JNIEnv *env, jobject this,
 100         jstring filename)
 101 {

 102     int len;
 103     jsize jlen;
 104     char *filename_str = NULL;
 105     GError **error = NULL;
 106 
 107     if (filename == NULL)
 108     {
 109         return JNI_FALSE;
 110     }
 111 
 112     len = (*env)->GetStringUTFLength(env, filename);
 113     jlen = (*env)->GetStringLength(env, filename);
 114     filename_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 115             sizeof(char), len + 1);
 116     if (filename_str == NULL) {
 117         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 118         return JNI_FALSE;
 119     }
 120     if (!init_method(env, this) ) {
 121         free(filename_str);
 122         return JNI_FALSE;
 123     }
 124     (*env)->GetStringUTFRegion(env, filename, 0, jlen, filename_str);
 125     jboolean result = gtk->get_file_icon_data(env, filename_str, error,
 126                                             icon_upcall_method, this);
 127 
 128     /* Release the strings we've allocated. */
 129     free(filename_str);
 130 
 131     return result;



 132 }
 133 
 134 /*
 135  * Class:     sun_awt_UNIXToolkit
 136  * Method:    load_stock_icon
 137  * Signature: (ILjava/lang/String;IILjava/lang/String;)Z
 138  *
 139  * This method assumes that GTK libs are present.
 140  */
 141 JNIEXPORT jboolean JNICALL
 142 Java_sun_awt_UNIXToolkit_load_1stock_1icon(JNIEnv *env, jobject this,
 143         jint widget_type, jstring stock_id, jint icon_size,
 144         jint text_direction, jstring detail)
 145 {

 146     int len;
 147     jsize jlen;
 148     char *stock_id_str = NULL;
 149     char *detail_str = NULL;
 150     jboolean result = JNI_FALSE;
 151 
 152     if (stock_id == NULL)
 153     {
 154         return JNI_FALSE;
 155     }
 156 
 157     len = (*env)->GetStringUTFLength(env, stock_id);
 158     jlen = (*env)->GetStringLength(env, stock_id);
 159     stock_id_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 160             sizeof(char), len + 1);
 161     if (stock_id_str == NULL) {
 162         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 163         return JNI_FALSE;
 164     }
 165     (*env)->GetStringUTFRegion(env, stock_id, 0, jlen, stock_id_str);
 166 
 167     /* Detail isn't required so check for NULL. */
 168     if (detail != NULL)
 169     {
 170         len = (*env)->GetStringUTFLength(env, detail);
 171         jlen = (*env)->GetStringLength(env, detail);
 172         detail_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 173                 sizeof(char), len + 1);
 174         if (detail_str == NULL) {
 175             free(stock_id_str);
 176             JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 177             return JNI_FALSE;
 178         }
 179         (*env)->GetStringUTFRegion(env, detail, 0, jlen, detail_str);
 180     }
 181 
 182     if (init_method(env, this)) {
 183         result = gtk->get_icon_data(env, widget_type, stock_id_str,
 184                                     icon_size, text_direction, detail_str,
 185                                     icon_upcall_method, this);
 186     }
 187     /* Release the strings we've allocated. */
 188     free(stock_id_str);
 189     free(detail_str);
 190 
 191     return result;



 192 }
 193 
 194 /*
 195  * Class:     sun_awt_UNIXToolkit
 196  * Method:    nativeSync
 197  * Signature: ()V
 198  */
 199 JNIEXPORT void JNICALL
 200 Java_sun_awt_UNIXToolkit_nativeSync(JNIEnv *env, jobject this)
 201 {

 202     AWT_LOCK();
 203     XSync(awt_display, False);
 204     AWT_UNLOCK();

 205 }
 206 
 207 /*
 208  * Class:     sun_awt_SunToolkit
 209  * Method:    closeSplashScreen
 210  * Signature: ()V
 211  */
 212 JNIEXPORT void JNICALL
 213 Java_sun_awt_SunToolkit_closeSplashScreen(JNIEnv *env, jclass cls)
 214 {
 215     typedef void (*SplashClose_t)();
 216     SplashClose_t splashClose;
 217     void* hSplashLib = dlopen(0, RTLD_LAZY);
 218     if (!hSplashLib) {
 219         return;
 220     }
 221     splashClose = (SplashClose_t)dlsym(hSplashLib,
 222         "SplashClose");
 223     if (splashClose) {
 224         splashClose();
 225     }
 226     dlclose(hSplashLib);
 227 }
 228 
 229 /*
 230  * Class:     sun_awt_UNIXToolkit
 231  * Method:    gtkCheckVersionImpl
 232  * Signature: (III)Ljava/lang/String;
 233  */
 234 JNIEXPORT jboolean JNICALL
 235 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this,
 236         jint major, jint minor, jint micro)
 237 {
 238     char *ret;
 239 
 240     ret = gtk->gtk_check_version(major, minor, micro);
 241     if (ret == NULL) {
 242         return TRUE;
 243     }
 244 
 245     return FALSE;
 246 }
 247 
 248 /*
 249  * Class:     sun_awt_UNIXToolkit
 250  * Method:    get_gtk_version
 251  * Signature: ()I
 252  */
 253 JNIEXPORT jint JNICALL
 254 Java_sun_awt_UNIXToolkit_get_1gtk_1version(JNIEnv *env, jclass klass)
 255 {

 256     return gtk ? gtk->version : GTK_ANY;



 257 }
--- EOF ---