|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2025, JetBrains s.r.o.. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | + |
| 27 | +#ifdef HEADLESS |
| 28 | + #error This file should not be included in headless library |
| 29 | +#endif |
| 30 | + |
| 31 | +#include <dlfcn.h> |
| 32 | + |
| 33 | +#include <jni.h> |
| 34 | +#include <jvm_md.h> |
| 35 | + |
| 36 | +#include "Trace.h" |
| 37 | +#include "jni_util.h" |
| 38 | +#include "gtk_interface.h" |
| 39 | + |
| 40 | +typedef gboolean (GNOME_URL_SHOW_TYPE)(const char *, void **); |
| 41 | +typedef gboolean (GNOME_VFS_INIT_TYPE)(void); |
| 42 | + |
| 43 | +static GNOME_URL_SHOW_TYPE *gnome_url_show = NULL; |
| 44 | + |
| 45 | +static gboolean gnome_load(void) { |
| 46 | + void *vfs_handle; |
| 47 | + void *gnome_handle; |
| 48 | + const char *errmsg; |
| 49 | + GNOME_VFS_INIT_TYPE *gnome_vfs_init; |
| 50 | + |
| 51 | + vfs_handle = dlopen(VERSIONED_JNI_LIB_NAME("gnomevfs-2", "0"), RTLD_LAZY); |
| 52 | + if (vfs_handle == NULL) { |
| 53 | + vfs_handle = dlopen(JNI_LIB_NAME("gnomevfs-2"), RTLD_LAZY); |
| 54 | + if (vfs_handle == NULL) { |
| 55 | + J2dTraceLn(J2D_TRACE_ERROR, "can not load libgnomevfs-2.so"); |
| 56 | + return FALSE; |
| 57 | + } |
| 58 | + } |
| 59 | + dlerror(); /* Clear errors */ |
| 60 | + gnome_vfs_init = dlsym(vfs_handle, "gnome_vfs_init"); |
| 61 | + if (gnome_vfs_init == NULL){ |
| 62 | + J2dTraceLn(J2D_TRACE_ERROR, "dlsym( gnome_vfs_init) returned NULL"); |
| 63 | + return FALSE; |
| 64 | + } |
| 65 | + if ((errmsg = dlerror()) != NULL) { |
| 66 | + J2dTraceLn1(J2D_TRACE_ERROR, "can not find symbol gnome_vfs_init %s", errmsg); |
| 67 | + return FALSE; |
| 68 | + } |
| 69 | + // call gonme_vfs_init() |
| 70 | + (*gnome_vfs_init)(); |
| 71 | + |
| 72 | + gnome_handle = dlopen(VERSIONED_JNI_LIB_NAME("gnome-2", "0"), RTLD_LAZY); |
| 73 | + if (gnome_handle == NULL) { |
| 74 | + gnome_handle = dlopen(JNI_LIB_NAME("gnome-2"), RTLD_LAZY); |
| 75 | + if (gnome_handle == NULL) { |
| 76 | + J2dTraceLn(J2D_TRACE_ERROR, "can not load libgnome-2.so"); |
| 77 | + return FALSE; |
| 78 | + } |
| 79 | + } |
| 80 | + dlerror(); /* Clear errors */ |
| 81 | + gnome_url_show = dlsym(gnome_handle, "gnome_url_show"); |
| 82 | + if ((errmsg = dlerror()) != NULL) { |
| 83 | + J2dTraceLn(J2D_TRACE_ERROR, "can not find symble gnome_url_show"); |
| 84 | + return FALSE; |
| 85 | + } |
| 86 | + return TRUE; |
| 87 | +} |
| 88 | + |
| 89 | +static gboolean gtk_has_been_loaded = FALSE; |
| 90 | +static gboolean gnome_has_been_loaded = FALSE; |
| 91 | + |
| 92 | +JNIEXPORT jboolean JNICALL Java_sun_awt_wl_WLDesktopPeer_init |
| 93 | + (JNIEnv *env, jclass cls, jint version, jboolean verbose) |
| 94 | +{ |
| 95 | + if (gtk_has_been_loaded || gnome_has_been_loaded) { |
| 96 | + return JNI_TRUE; |
| 97 | + } |
| 98 | + |
| 99 | + if (gtk_load(env, version, verbose) && gtk->show_uri_load(env)) { |
| 100 | + gtk_has_been_loaded = TRUE; |
| 101 | + return JNI_TRUE; |
| 102 | + } else if (gnome_load()) { |
| 103 | + gnome_has_been_loaded = TRUE; |
| 104 | + return JNI_TRUE; |
| 105 | + } |
| 106 | + |
| 107 | + return JNI_FALSE; |
| 108 | +} |
| 109 | + |
| 110 | +JNIEXPORT jboolean JNICALL Java_sun_awt_wl_WLDesktopPeer_gnome_1url_1show |
| 111 | + (JNIEnv *env, jobject obj, jbyteArray url_j) |
| 112 | +{ |
| 113 | + gboolean success = FALSE; |
| 114 | + const gchar* url_c = (gchar*)(*env)->GetByteArrayElements(env, url_j, NULL); |
| 115 | + if (url_c == NULL) { |
| 116 | + JNU_ThrowOutOfMemoryError(env, 0); |
| 117 | + return JNI_FALSE; |
| 118 | + } |
| 119 | + |
| 120 | + if (gtk_has_been_loaded) { |
| 121 | + gtk->gdk_threads_enter(); |
| 122 | + success = gtk->gtk_show_uri_on_window(NULL, url_c, GDK_CURRENT_TIME, NULL); |
| 123 | + gtk->gdk_threads_leave(); |
| 124 | + } else if (gnome_has_been_loaded) { |
| 125 | + success = (*gnome_url_show)(url_c, NULL); |
| 126 | + } |
| 127 | + |
| 128 | + (*env)->ReleaseByteArrayElements(env, url_j, (jbyte*)url_c, 0); |
| 129 | + |
| 130 | + return success ? JNI_TRUE : JNI_FALSE; |
| 131 | +} |
| 132 | + |
0 commit comments