< prev index next >

src/java.desktop/unix/native/libawt_xawt/awt/awt_UNIXToolkit.c

Print this page


   1 /*
   2  * Copyright (c) 2004, 2014, 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 "gtk2_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: ()Z
  49  */
  50 JNIEXPORT jboolean JNICALL
  51 Java_sun_awt_UNIXToolkit_check_1gtk(JNIEnv *env, jclass klass)
  52 {
  53 #ifndef HEADLESS
  54     return (jboolean)gtk2_check_version();
  55 #else
  56     return JNI_FALSE;
  57 #endif /* !HEADLESS */
  58 }
  59 
  60 
  61 /*
  62  * Class:     sun_awt_UNIXToolkit
  63  * Method:    load_gtk
  64  * Signature: ()Z
  65  */
  66 JNIEXPORT jboolean JNICALL
  67 Java_sun_awt_UNIXToolkit_load_1gtk(JNIEnv *env, jclass klass)
  68 {
  69 #ifndef HEADLESS
  70     return (jboolean)gtk2_load(env);
  71 #else
  72     return JNI_FALSE;
  73 #endif /* !HEADLESS */
  74 }
  75 
  76 
  77 /*
  78  * Class:     sun_awt_UNIXToolkit
  79  * Method:    unload_gtk
  80  * Signature: ()Z
  81  */
  82 JNIEXPORT jboolean JNICALL
  83 Java_sun_awt_UNIXToolkit_unload_1gtk(JNIEnv *env, jclass klass)
  84 {
  85 #ifndef HEADLESS
  86     return (jboolean)gtk2_unload();
  87 #else
  88     return JNI_FALSE;
  89 #endif /* !HEADLESS */
  90 }
  91 
  92 jboolean _icon_upcall(JNIEnv *env, jobject this, GdkPixbuf *pixbuf)
  93 {
  94     jboolean result = JNI_FALSE;
  95 
  96     if (this_class == NULL) {
  97         this_class = (*env)->NewGlobalRef(env,
  98                                           (*env)->GetObjectClass(env, this));
  99         icon_upcall_method = (*env)->GetMethodID(env, this_class,
 100                                  "loadIconCallback", "([BIIIIIZ)V");
 101         CHECK_NULL_RETURN(icon_upcall_method, JNI_FALSE);
 102     }
 103 
 104     if (pixbuf != NULL)
 105     {
 106         guchar *pixbuf_data = (*fp_gdk_pixbuf_get_pixels)(pixbuf);
 107         int row_stride = (*fp_gdk_pixbuf_get_rowstride)(pixbuf);
 108         int width = (*fp_gdk_pixbuf_get_width)(pixbuf);
 109         int height = (*fp_gdk_pixbuf_get_height)(pixbuf);
 110         int bps = (*fp_gdk_pixbuf_get_bits_per_sample)(pixbuf);
 111         int channels = (*fp_gdk_pixbuf_get_n_channels)(pixbuf);
 112         gboolean alpha = (*fp_gdk_pixbuf_get_has_alpha)(pixbuf);
 113 
 114         /* Copy the data array into a Java structure so we can pass it back. */
 115         jbyteArray data = (*env)->NewByteArray(env, (row_stride * height));
 116         JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
 117 
 118         (*env)->SetByteArrayRegion(env, data, 0, (row_stride * height),
 119                                    (jbyte *)pixbuf_data);
 120 
 121         /* Release the pixbuf. */
 122         (*fp_g_object_unref)(pixbuf);
 123 
 124         /* Call the callback method to create the image on the Java side. */
 125         (*env)->CallVoidMethod(env, this, icon_upcall_method, data,
 126                 width, height, row_stride, bps, channels, alpha);
 127         result = JNI_TRUE;
 128     }
 129     return result;
 130 }
 131 
 132 /*
 133  * Class:     sun_awt_UNIXToolkit
 134  * Method:    load_gtk_icon
 135  * Signature: (Ljava/lang/String)Z
 136  *
 137  * This method assumes that GTK libs are present.
 138  */
 139 JNIEXPORT jboolean JNICALL
 140 Java_sun_awt_UNIXToolkit_load_1gtk_1icon(JNIEnv *env, jobject this,
 141         jstring filename)
 142 {
 143 #ifndef HEADLESS
 144     int len;
 145     char *filename_str = NULL;
 146     GError **error = NULL;
 147     GdkPixbuf *pixbuf;
 148 
 149     if (filename == NULL)
 150     {
 151         return JNI_FALSE;
 152     }
 153 
 154     len = (*env)->GetStringUTFLength(env, filename);
 155     filename_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 156             sizeof(char), len + 1);
 157     if (filename_str == NULL) {
 158         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 159         return JNI_FALSE;
 160     }



 161     (*env)->GetStringUTFRegion(env, filename, 0, len, filename_str);
 162     pixbuf = (*fp_gdk_pixbuf_new_from_file)(filename_str, error);

 163 
 164     /* Release the strings we've allocated. */
 165     free(filename_str);
 166 
 167     return _icon_upcall(env, this, pixbuf);
 168 #else /* HEADLESS */
 169     return JNI_FALSE;
 170 #endif /* !HEADLESS */
 171 }
 172 
 173 /*
 174  * Class:     sun_awt_UNIXToolkit
 175  * Method:    load_stock_icon
 176  * Signature: (ILjava/lang/String;IILjava/lang/String;)Z
 177  *
 178  * This method assumes that GTK libs are present.
 179  */
 180 JNIEXPORT jboolean JNICALL
 181 Java_sun_awt_UNIXToolkit_load_1stock_1icon(JNIEnv *env, jobject this,
 182         jint widget_type, jstring stock_id, jint icon_size,
 183         jint text_direction, jstring detail)
 184 {
 185 #ifndef HEADLESS
 186     int len;
 187     char *stock_id_str = NULL;
 188     char *detail_str = NULL;
 189     GdkPixbuf *pixbuf;
 190 
 191     if (stock_id == NULL)
 192     {
 193         return JNI_FALSE;
 194     }
 195 
 196     len = (*env)->GetStringUTFLength(env, stock_id);
 197     stock_id_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 198             sizeof(char), len + 1);
 199     if (stock_id_str == NULL) {
 200         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 201         return JNI_FALSE;
 202     }
 203     (*env)->GetStringUTFRegion(env, stock_id, 0, len, stock_id_str);
 204 
 205     /* Detail isn't required so check for NULL. */
 206     if (detail != NULL)
 207     {
 208         len = (*env)->GetStringUTFLength(env, detail);
 209         detail_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 210                 sizeof(char), len + 1);
 211         if (detail_str == NULL) {
 212             JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 213             return JNI_FALSE;
 214         }
 215         (*env)->GetStringUTFRegion(env, detail, 0, len, detail_str);
 216     }
 217 
 218     pixbuf = gtk2_get_stock_icon(widget_type, stock_id_str, icon_size,
 219                                  text_direction, detail_str);




 220 
 221     /* Release the strings we've allocated. */
 222     free(stock_id_str);
 223     if (detail_str != NULL)
 224     {
 225         free(detail_str);
 226     }
 227 
 228     return _icon_upcall(env, this, pixbuf);
 229 #else /* HEADLESS */
 230     return JNI_FALSE;
 231 #endif /* !HEADLESS */
 232 }
 233 
 234 /*
 235  * Class:     sun_awt_UNIXToolkit
 236  * Method:    nativeSync
 237  * Signature: ()V
 238  */
 239 JNIEXPORT void JNICALL
 240 Java_sun_awt_UNIXToolkit_nativeSync(JNIEnv *env, jobject this)
 241 {
 242 #ifndef HEADLESS
 243     AWT_LOCK();
 244     XSync(awt_display, False);
 245     AWT_UNLOCK();
 246 #endif /* !HEADLESS */
 247 }
 248 


 262     }
 263     splashClose = (SplashClose_t)dlsym(hSplashLib,
 264         "SplashClose");
 265     if (splashClose) {
 266         splashClose();
 267     }
 268     dlclose(hSplashLib);
 269 }
 270 
 271 /*
 272  * Class:     sun_awt_UNIXToolkit
 273  * Method:    gtkCheckVersionImpl
 274  * Signature: (III)Ljava/lang/String;
 275  */
 276 JNIEXPORT jboolean JNICALL
 277 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this,
 278         jint major, jint minor, jint micro)
 279 {
 280     char *ret;
 281 
 282     ret = fp_gtk_check_version(major, minor, micro);
 283     if (ret == NULL) {
 284         return TRUE;
 285     }
 286 
 287     free(ret);
 288     return FALSE;















 289 }
   1 /*
   2  * Copyright (c) 2004, 2016, 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         return JNI_FALSE;
 133     }
 134     (*env)->GetStringUTFRegion(env, filename, 0, len, filename_str);
 135     jboolean result = gtk->get_file_icon_data(env, filename_str, error,
 136                                             icon_upcall_method, this);
 137 
 138     /* Release the strings we've allocated. */
 139     free(filename_str);
 140 
 141     return result;
 142 #else /* HEADLESS */
 143     return JNI_FALSE;
 144 #endif /* !HEADLESS */
 145 }
 146 
 147 /*
 148  * Class:     sun_awt_UNIXToolkit
 149  * Method:    load_stock_icon
 150  * Signature: (ILjava/lang/String;IILjava/lang/String;)Z
 151  *
 152  * This method assumes that GTK libs are present.
 153  */
 154 JNIEXPORT jboolean JNICALL
 155 Java_sun_awt_UNIXToolkit_load_1stock_1icon(JNIEnv *env, jobject this,
 156         jint widget_type, jstring stock_id, jint icon_size,
 157         jint text_direction, jstring detail)
 158 {
 159 #ifndef HEADLESS
 160     int len;
 161     char *stock_id_str = NULL;
 162     char *detail_str = NULL;

 163 
 164     if (stock_id == NULL)
 165     {
 166         return JNI_FALSE;
 167     }
 168 
 169     len = (*env)->GetStringUTFLength(env, stock_id);
 170     stock_id_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 171             sizeof(char), len + 1);
 172     if (stock_id_str == NULL) {
 173         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 174         return JNI_FALSE;
 175     }
 176     (*env)->GetStringUTFRegion(env, stock_id, 0, len, stock_id_str);
 177 
 178     /* Detail isn't required so check for NULL. */
 179     if (detail != NULL)
 180     {
 181         len = (*env)->GetStringUTFLength(env, detail);
 182         detail_str = (char *)SAFE_SIZE_ARRAY_ALLOC(malloc,
 183                 sizeof(char), len + 1);
 184         if (detail_str == NULL) {
 185             JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
 186             return JNI_FALSE;
 187         }
 188         (*env)->GetStringUTFRegion(env, detail, 0, len, detail_str);
 189     }
 190 
 191     if (!init_method(env, this) ) {
 192         return JNI_FALSE;
 193     }
 194     jboolean result = gtk->get_icon_data(env, widget_type, stock_id_str,
 195                   icon_size, text_direction, detail_str,
 196                   icon_upcall_method, this);
 197 
 198     /* Release the strings we've allocated. */
 199     free(stock_id_str);
 200     if (detail_str != NULL)
 201     {
 202         free(detail_str);
 203     }
 204     return result;

 205 #else /* HEADLESS */
 206     return JNI_FALSE;
 207 #endif /* !HEADLESS */
 208 }
 209 
 210 /*
 211  * Class:     sun_awt_UNIXToolkit
 212  * Method:    nativeSync
 213  * Signature: ()V
 214  */
 215 JNIEXPORT void JNICALL
 216 Java_sun_awt_UNIXToolkit_nativeSync(JNIEnv *env, jobject this)
 217 {
 218 #ifndef HEADLESS
 219     AWT_LOCK();
 220     XSync(awt_display, False);
 221     AWT_UNLOCK();
 222 #endif /* !HEADLESS */
 223 }
 224 


 238     }
 239     splashClose = (SplashClose_t)dlsym(hSplashLib,
 240         "SplashClose");
 241     if (splashClose) {
 242         splashClose();
 243     }
 244     dlclose(hSplashLib);
 245 }
 246 
 247 /*
 248  * Class:     sun_awt_UNIXToolkit
 249  * Method:    gtkCheckVersionImpl
 250  * Signature: (III)Ljava/lang/String;
 251  */
 252 JNIEXPORT jboolean JNICALL
 253 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this,
 254         jint major, jint minor, jint micro)
 255 {
 256     char *ret;
 257 
 258     ret = gtk->gtk_check_version(major, minor, micro);
 259     if (ret == NULL) {
 260         return TRUE;
 261     }
 262 

 263     return FALSE;
 264 }
 265 
 266 /*
 267  * Class:     sun_awt_UNIXToolkit
 268  * Method:    get_gtk_version
 269  * Signature: ()I
 270  */
 271 JNIEXPORT jint JNICALL
 272 Java_sun_awt_UNIXToolkit_get_1gtk_1version(JNIEnv *env, jclass klass)
 273 {
 274 #ifndef HEADLESS
 275     return gtk ? gtk->version : GTK_ANY;
 276 #else
 277     return GTK_ANY;
 278 #endif /* !HEADLESS */
 279 }
< prev index next >