1 /* 2 * Copyright (c) 2005, 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 #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, gboolean load); 34 gboolean gtk3_check(const char* lib_name, gboolean load); 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, gboolean load); 44 } GtkLib; 45 46 static GtkLib gtk_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 63 static GtkLib** get_libs_order(GtkVersion version) { 64 static GtkLib** load_order; 65 static int n_libs = 0; 66 if (!n_libs) { 67 n_libs = sizeof(gtk_libs) / sizeof(GtkLib); 68 load_order = malloc(1 + n_libs * sizeof(GtkLib *)); 69 load_order[n_libs] = 0; 70 } 71 int i, first = 0; 72 for (i = 0; i < n_libs; i++) { 73 load_order[i] = >k_libs[i]; 74 if (load_order[i]->version == version) { 75 first = i; 76 } 77 } 78 if (first) { 79 for (i = first; i > 0; i--) { 80 load_order[i] = load_order[i - 1]; 81 } 82 load_order[0] = >k_libs[first]; 83 } 84 return load_order; 85 } 86 87 static GtkLib* get_loaded() { 88 GtkLib** libs = get_libs_order(GTK_ANY); 89 while(!gtk && *libs) { 90 GtkLib* lib = *libs++; 91 if (lib->check(lib->vname, /* load = */FALSE)) { 92 return lib; 93 } 94 if (lib->check(lib->name, /* load = */FALSE)) { 95 return lib; 96 } 97 } 98 return NULL; 99 } 100 101 gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) { 102 if (gtk == NULL) { 103 GtkLib* lib = get_loaded(); 104 if (lib) { 105 if (verbose) { 106 fprintf(stderr, "Looking for GTK%d library...\n", 107 lib->version); 108 } 109 gtk = lib->load(env, lib->vname); 110 if (!gtk) { 111 gtk = lib->load(env, lib->name); 112 } 113 } else { 114 GtkLib** libs = get_libs_order(version); 115 while (!gtk && *libs) { 116 lib = *libs++; 117 if (version == GTK_ANY || lib->version == version) { 118 if (verbose) { 119 fprintf(stderr, "Looking for GTK%d library...\n", 120 lib->version); 121 } 122 gtk = lib->load(env, lib->vname); 123 if (!gtk) { 124 gtk = lib->load(env, lib->name); 125 } 126 if (verbose && !gtk) { 127 fprintf(stderr, "Not found.\n"); 128 } 129 } 130 } 131 } 132 if (verbose) { 133 if (gtk) { 134 fprintf(stderr, "GTK%d library loaded.\n", lib->version); 135 } else { 136 fprintf(stderr, "Failed to load GTK library.\n"); 137 } 138 } 139 } 140 return gtk != NULL; 141 } 142 143 static gboolean check_version(GtkVersion version) { 144 GtkLib** libs = get_libs_order(GTK_ANY); 145 while (*libs) { 146 GtkLib* lib = *libs++; 147 if (lib->check(lib->vname, /* load = */TRUE)) { 148 return TRUE; 149 } 150 if (lib->check(lib->name, /* load = */TRUE)) { 151 return TRUE; 152 } 153 } 154 return FALSE; 155 } 156 157 gboolean gtk_check_version(GtkVersion version) { 158 if (gtk) { 159 return TRUE; 160 } 161 return check_version(version); 162 } 163