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 #include <dlfcn.h> 26 #include <stdlib.h> 27 #include "jvm_md.h" 28 #include "gtk_interface.h" 29 30 GtkApi* gtk2_load(JNIEnv *env, const char* lib_name); 31 GtkApi* gtk3_load(JNIEnv *env, const char* lib_name); 32 33 gboolean gtk2_check(const char* lib_name, int flags); 34 gboolean gtk3_check(const char* lib_name, int flags); 35 36 GtkApi *gtk; 37 38 typedef struct { 39 GtkVersion version; 40 const char* name; 41 const char* vname; 42 GtkApi* (*load)(JNIEnv *env, const char* lib_name); 43 gboolean (*check)(const char* lib_name, int flags); 44 } GtkLib; 45 46 static GtkLib libs[] = { 47 { 48 GTK_2, 49 JNI_LIB_NAME("gtk-x11-2.0"), 50 VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0"), 51 >k2_load, 52 >k2_check 53 }, 54 { 55 GTK_3, 56 JNI_LIB_NAME("gtk-3"), 57 VERSIONED_JNI_LIB_NAME("gtk-3", "0"), 58 >k3_load, 59 >k3_check 60 }, 61 { 62 0, 63 NULL, 64 NULL, 65 NULL, 66 NULL 67 } 68 }; 69 70 static GtkLib* get_loaded() { 71 GtkLib* lib = libs; 72 while(!gtk && lib->version) { 73 if (lib->check(lib->vname, RTLD_NOLOAD)) { 74 return lib; 75 } 76 if (lib->check(lib->name, RTLD_NOLOAD)) { 77 return lib; 78 } 79 lib++; 80 } 81 return NULL; 82 } 83 84 gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) { 85 if (gtk == NULL) { 86 GtkLib* lib = get_loaded(); 87 if (lib) { 88 if (version != GTK_ANY && lib->version != version) { 89 if (verbose) { 90 fprintf(stderr, "WARNING: Cannot load GTK%d library: \ 91 GTK%d has already been loaded\n", version, lib->version); 92 } 93 return FALSE; 94 } 95 if (verbose) { 96 fprintf(stderr, "Looking for GTK%d library...\n", version); 97 } 98 gtk = lib->load(env, lib->vname); 99 if (!gtk) { 100 gtk = lib->load(env, lib->name); 101 } 102 } else { 103 lib = libs; 104 while (!gtk && lib->version) { 105 if (version == GTK_ANY || lib->version == version) { 106 if (verbose) { 107 fprintf(stderr, "Looking for GTK%d library...\n", 108 lib->version); 109 } 110 gtk = lib->load(env, lib->vname); 111 if (!gtk) { 112 gtk = lib->load(env, lib->name); 113 } 114 if (verbose && !gtk) { 115 fprintf(stderr, "Not found.\n"); 116 } 117 } 118 lib++; 119 } 120 lib--; 121 } 122 if (verbose) { 123 if (gtk) { 124 fprintf(stderr, "GTK%d library loaded.\n", lib->version); 125 } else { 126 fprintf(stderr, "Failed to load GTK library.\n"); 127 } 128 } 129 } 130 return gtk != NULL; 131 } 132 133 static gboolean check_version(GtkVersion version, int flags) { 134 GtkLib* lib = libs; 135 while (lib->version) { 136 if (version == GTK_ANY || lib->version == version) { 137 if (lib->check(lib->vname, flags)) { 138 return TRUE; 139 } 140 if (lib->check(lib->name, flags)) { 141 return TRUE; 142 } 143 } 144 lib++; 145 } 146 return FALSE; 147 } 148 149 gboolean gtk_check_version(GtkVersion version) { 150 if (gtk) { 151 return TRUE; 152 } 153 if (check_version(version, RTLD_NOLOAD)) { 154 return TRUE; 155 } 156 return check_version(version, RTLD_LAZY | RTLD_LOCAL); 157 } 158 | 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 #include <dlfcn.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include "jvm_md.h" 29 #include "gtk_interface.h" 30 31 GtkApi* gtk2_load(JNIEnv *env, const char* lib_name); 32 GtkApi* gtk3_load(JNIEnv *env, const char* lib_name); 33 34 gboolean gtk2_check(const char* lib_name); 35 gboolean gtk3_check(const char* lib_name); 36 37 GtkApi *gtk; 38 39 typedef struct { 40 GtkVersion version; 41 const char* name; 42 const char* vname; 43 GtkApi* (*load)(JNIEnv *env, const char* lib_name); 44 gboolean (*check)(const char* lib_name); 45 } GtkLib; 46 47 static GtkLib libs[] = { 48 { 49 GTK_2, 50 JNI_LIB_NAME("gtk-x11-2.0"), 51 VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0"), 52 >k2_load, 53 >k2_check 54 }, 55 { 56 GTK_3, 57 JNI_LIB_NAME("gtk-3"), 58 VERSIONED_JNI_LIB_NAME("gtk-3", "0"), 59 >k3_load, 60 >k3_check 61 }, 62 { 63 0, 64 NULL, 65 NULL, 66 NULL, 67 NULL 68 } 69 }; 70 71 static gboolean isLoadedLib(const char *lib_name) { 72 FILE* fp = fopen("/proc/self/maps", "r"); 73 if (!fp) { 74 return FALSE; 75 } 76 char line[512]; 77 while (fgets(line, 512, fp) != NULL) 78 { 79 if (strstr(line, lib_name)) { 80 fclose(fp); 81 return TRUE; 82 } 83 } 84 fclose(fp); 85 return FALSE; 86 } 87 88 static GtkLib* get_loaded() { 89 GtkLib* lib = libs; 90 while(!gtk && lib->version) { 91 if (isLoadedLib(lib->vname)) { 92 return lib; 93 } 94 if (isLoadedLib(lib->name)) { 95 return lib; 96 } 97 lib++; 98 } 99 return NULL; 100 } 101 102 gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) { 103 if (gtk == NULL) { 104 GtkLib* lib = get_loaded(); 105 if (lib) { 106 if (verbose) { 107 fprintf(stderr,"%s is already loaded\n",lib->name); 108 } 109 if (version != GTK_ANY && lib->version != version) { 110 if (verbose) { 111 fprintf(stderr, "WARNING: Cannot load GTK%d library: \ 112 GTK%d has already been loaded\n", version, lib->version); 113 } 114 return FALSE; 115 } 116 if (verbose) { 117 fprintf(stderr, "Looking for GTK%d library...\n", lib->version); 118 } 119 gtk = lib->load(env, lib->vname); 120 if (!gtk) { 121 gtk = lib->load(env, lib->name); 122 } 123 } else { 124 lib = libs; 125 while (!gtk && lib->version) { 126 if (version == GTK_ANY || lib->version == version) { 127 if (verbose) { 128 fprintf(stderr, "Looking for GTK%d library...\n", 129 lib->version); 130 } 131 gtk = lib->load(env, lib->vname); 132 if (!gtk) { 133 gtk = lib->load(env, lib->name); 134 } 135 if (verbose && !gtk) { 136 fprintf(stderr, "Not found.\n"); 137 } 138 } 139 lib++; 140 } 141 lib--; 142 } 143 if (verbose) { 144 if (gtk) { 145 fprintf(stderr, "GTK%d library loaded.\n", lib->version); 146 } else { 147 fprintf(stderr, "Failed to load GTK library.\n"); 148 } 149 } 150 } 151 return gtk != NULL; 152 } 153 154 static gboolean check_version(GtkVersion version) { 155 GtkLib* lib = libs; 156 while (lib->version) { 157 if (version == GTK_ANY || lib->version == version) { 158 if (lib->check(lib->vname)) { 159 return TRUE; 160 } 161 if (lib->check(lib->name)) { 162 return TRUE; 163 } 164 } 165 lib++; 166 } 167 return FALSE; 168 } 169 170 gboolean gtk_check_version(GtkVersion version) { 171 if (gtk) { 172 return TRUE; 173 } 174 GtkLib* lib; 175 if (lib = get_loaded()) { 176 return lib->check(lib->vname) || lib->check(lib->name); 177 } 178 return check_version(version); 179 } |