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 249 /* 250 * Class: sun_awt_SunToolkit 251 * Method: closeSplashScreen 252 * Signature: ()V 253 */ 254 JNIEXPORT void JNICALL 255 Java_sun_awt_SunToolkit_closeSplashScreen(JNIEnv *env, jclass cls) 256 { 257 typedef void (*SplashClose_t)(); 258 SplashClose_t splashClose; 259 void* hSplashLib = dlopen(0, RTLD_LAZY); 260 if (!hSplashLib) { 261 return; 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 }