comparison src/java.desktop/unix/native/libawt_xawt/awt/awt_UNIXToolkit.c @ 14339:6d60dfa5e3f8

8145547: [AWT/Swing] Conditional support for GTK 3 on Linux Reviewed-by: prr, alexsch
author ssadetsky
date Mon, 25 Apr 2016 15:37:31 +0300
parents e66f69113b89
children 251eb9ac75dd
comparison
equal deleted inserted replaced
0:97b6f1c1c04b 1:cf1850e70b68
1 /* 1 /*
2 * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. 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. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
32 #include <sizecalc.h> 32 #include <sizecalc.h>
33 #include "sun_awt_UNIXToolkit.h" 33 #include "sun_awt_UNIXToolkit.h"
34 34
35 #ifndef HEADLESS 35 #ifndef HEADLESS
36 #include "awt.h" 36 #include "awt.h"
37 #include "gtk2_interface.h" 37 #include "gtk_interface.h"
38 #endif /* !HEADLESS */ 38 #endif /* !HEADLESS */
39 39
40 40
41 static jclass this_class = NULL; 41 static jclass this_class = NULL;
42 static jmethodID icon_upcall_method = NULL; 42 static jmethodID icon_upcall_method = NULL;
43 43
44 44
45 /* 45 /*
46 * Class: sun_awt_UNIXToolkit 46 * Class: sun_awt_UNIXToolkit
47 * Method: check_gtk 47 * Method: check_gtk
48 * Signature: ()Z 48 * Signature: (I)Z
49 */ 49 */
50 JNIEXPORT jboolean JNICALL 50 JNIEXPORT jboolean JNICALL
51 Java_sun_awt_UNIXToolkit_check_1gtk(JNIEnv *env, jclass klass) 51 Java_sun_awt_UNIXToolkit_check_1gtk(JNIEnv *env, jclass klass, jint version) {
52 { 52 #ifndef HEADLESS
53 #ifndef HEADLESS 53 return (jboolean)gtk_check_version(version);
54 return (jboolean)gtk2_check_version();
55 #else 54 #else
56 return JNI_FALSE; 55 return JNI_FALSE;
57 #endif /* !HEADLESS */ 56 #endif /* !HEADLESS */
58 } 57 }
59 58
60 59
61 /* 60 /*
62 * Class: sun_awt_UNIXToolkit 61 * Class: sun_awt_UNIXToolkit
63 * Method: load_gtk 62 * Method: load_gtk
64 * Signature: ()Z 63 * Signature: (I)Z
65 */ 64 */
66 JNIEXPORT jboolean JNICALL 65 JNIEXPORT jboolean JNICALL
67 Java_sun_awt_UNIXToolkit_load_1gtk(JNIEnv *env, jclass klass) 66 Java_sun_awt_UNIXToolkit_load_1gtk(JNIEnv *env, jclass klass, jint version,
68 { 67 jboolean verbose) {
69 #ifndef HEADLESS 68 #ifndef HEADLESS
70 return (jboolean)gtk2_load(env); 69 return (jboolean)gtk_load(env, version, verbose);
71 #else 70 #else
72 return JNI_FALSE; 71 return JNI_FALSE;
73 #endif /* !HEADLESS */ 72 #endif /* !HEADLESS */
74 } 73 }
75 74
81 */ 80 */
82 JNIEXPORT jboolean JNICALL 81 JNIEXPORT jboolean JNICALL
83 Java_sun_awt_UNIXToolkit_unload_1gtk(JNIEnv *env, jclass klass) 82 Java_sun_awt_UNIXToolkit_unload_1gtk(JNIEnv *env, jclass klass)
84 { 83 {
85 #ifndef HEADLESS 84 #ifndef HEADLESS
86 return (jboolean)gtk2_unload(); 85 return (jboolean)gtk->unload();
87 #else 86 #else
88 return JNI_FALSE; 87 return JNI_FALSE;
89 #endif /* !HEADLESS */ 88 #endif /* !HEADLESS */
90 } 89 }
91 90
92 jboolean _icon_upcall(JNIEnv *env, jobject this, GdkPixbuf *pixbuf) 91 jboolean init_method(JNIEnv *env, jobject this)
93 { 92 {
94 jboolean result = JNI_FALSE;
95
96 if (this_class == NULL) { 93 if (this_class == NULL) {
97 this_class = (*env)->NewGlobalRef(env, 94 this_class = (*env)->NewGlobalRef(env,
98 (*env)->GetObjectClass(env, this)); 95 (*env)->GetObjectClass(env, this));
99 icon_upcall_method = (*env)->GetMethodID(env, this_class, 96 icon_upcall_method = (*env)->GetMethodID(env, this_class,
100 "loadIconCallback", "([BIIIIIZ)V"); 97 "loadIconCallback", "([BIIIIIZ)V");
101 CHECK_NULL_RETURN(icon_upcall_method, JNI_FALSE); 98 CHECK_NULL_RETURN(icon_upcall_method, JNI_FALSE);
102 } 99 }
103 100 return JNI_TRUE;
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 } 101 }
131 102
132 /* 103 /*
133 * Class: sun_awt_UNIXToolkit 104 * Class: sun_awt_UNIXToolkit
134 * Method: load_gtk_icon 105 * Method: load_gtk_icon
142 { 113 {
143 #ifndef HEADLESS 114 #ifndef HEADLESS
144 int len; 115 int len;
145 char *filename_str = NULL; 116 char *filename_str = NULL;
146 GError **error = NULL; 117 GError **error = NULL;
147 GdkPixbuf *pixbuf;
148 118
149 if (filename == NULL) 119 if (filename == NULL)
150 { 120 {
151 return JNI_FALSE; 121 return JNI_FALSE;
152 } 122 }
156 sizeof(char), len + 1); 126 sizeof(char), len + 1);
157 if (filename_str == NULL) { 127 if (filename_str == NULL) {
158 JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError"); 128 JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
159 return JNI_FALSE; 129 return JNI_FALSE;
160 } 130 }
131 if (!init_method(env, this) ) {
132 return JNI_FALSE;
133 }
161 (*env)->GetStringUTFRegion(env, filename, 0, len, filename_str); 134 (*env)->GetStringUTFRegion(env, filename, 0, len, filename_str);
162 pixbuf = (*fp_gdk_pixbuf_new_from_file)(filename_str, error); 135 jboolean result = gtk->get_file_icon_data(env, filename_str, error,
136 icon_upcall_method, this);
163 137
164 /* Release the strings we've allocated. */ 138 /* Release the strings we've allocated. */
165 free(filename_str); 139 free(filename_str);
166 140
167 return _icon_upcall(env, this, pixbuf); 141 return result;
168 #else /* HEADLESS */ 142 #else /* HEADLESS */
169 return JNI_FALSE; 143 return JNI_FALSE;
170 #endif /* !HEADLESS */ 144 #endif /* !HEADLESS */
171 } 145 }
172 146
184 { 158 {
185 #ifndef HEADLESS 159 #ifndef HEADLESS
186 int len; 160 int len;
187 char *stock_id_str = NULL; 161 char *stock_id_str = NULL;
188 char *detail_str = NULL; 162 char *detail_str = NULL;
189 GdkPixbuf *pixbuf;
190 163
191 if (stock_id == NULL) 164 if (stock_id == NULL)
192 { 165 {
193 return JNI_FALSE; 166 return JNI_FALSE;
194 } 167 }
213 return JNI_FALSE; 186 return JNI_FALSE;
214 } 187 }
215 (*env)->GetStringUTFRegion(env, detail, 0, len, detail_str); 188 (*env)->GetStringUTFRegion(env, detail, 0, len, detail_str);
216 } 189 }
217 190
218 pixbuf = gtk2_get_stock_icon(widget_type, stock_id_str, icon_size, 191 if (!init_method(env, this) ) {
219 text_direction, detail_str); 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);
220 197
221 /* Release the strings we've allocated. */ 198 /* Release the strings we've allocated. */
222 free(stock_id_str); 199 free(stock_id_str);
223 if (detail_str != NULL) 200 if (detail_str != NULL)
224 { 201 {
225 free(detail_str); 202 free(detail_str);
226 } 203 }
227 204 return result;
228 return _icon_upcall(env, this, pixbuf);
229 #else /* HEADLESS */ 205 #else /* HEADLESS */
230 return JNI_FALSE; 206 return JNI_FALSE;
231 #endif /* !HEADLESS */ 207 #endif /* !HEADLESS */
232 } 208 }
233 209
277 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this, 253 Java_sun_awt_UNIXToolkit_gtkCheckVersionImpl(JNIEnv *env, jobject this,
278 jint major, jint minor, jint micro) 254 jint major, jint minor, jint micro)
279 { 255 {
280 char *ret; 256 char *ret;
281 257
282 ret = fp_gtk_check_version(major, minor, micro); 258 ret = gtk->gtk_check_version(major, minor, micro);
283 if (ret == NULL) { 259 if (ret == NULL) {
284 return TRUE; 260 return TRUE;
285 } 261 }
286 262
287 free(ret);
288 return FALSE; 263 return FALSE;
289 } 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 }