1 /*
2  * Copyright (c) 2005, 2020, 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 #ifdef HEADLESS
27     #error This file should not be included in headless library
28 #endif
29 
30 #include <dlfcn.h>
31 #include <stdlib.h>
32 #include "jvm_md.h"
33 #include "gtk_interface.h"
34 
35 GtkApi* gtk2_load(JNIEnv *env, const char* lib_name);
36 GtkApi* gtk3_load(JNIEnv *env, const char* lib_name);
37 
38 gboolean gtk2_check(const char* lib_name, gboolean load);
39 gboolean gtk3_check(const char* lib_name, gboolean load);
40 
41 GtkApi *gtk;
42 
43 typedef struct {
44     GtkVersion version;
45     const char* name;
46     const char* vname;
47     GtkApi* (*load)(JNIEnv *env, const char* lib_name);
48     gboolean (*check)(const char* lib_name, gboolean load);
49 } GtkLib;
50 
51 static GtkLib gtk_libs[] = {
52     {
53         GTK_3,
54         JNI_LIB_NAME("gtk-3"),
55         VERSIONED_JNI_LIB_NAME("gtk-3", "0"),
56         &gtk3_load,
57         &gtk3_check
58     },
59     {
60         GTK_2,
61         JNI_LIB_NAME("gtk-x11-2.0"),
62         VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0"),
63         &gtk2_load,
64         &gtk2_check
65     }
66 };
67 
get_libs_order(GtkVersion version)68 static GtkLib** get_libs_order(GtkVersion version) {
69     static GtkLib** load_order;
70     static int n_libs = 0;
71     if (!n_libs) {
72         n_libs = sizeof(gtk_libs) / sizeof(GtkLib);
73         load_order = calloc(n_libs + 1, sizeof(GtkLib *));
74         if (load_order == NULL) {
75           return NULL;
76         }
77     }
78     int i, first = 0;
79     for (i = 0; i < n_libs; i++) {
80         load_order[i] = &gtk_libs[i];
81         if (load_order[i]->version == version) {
82             first = i;
83         }
84     }
85     if (first) {
86         for (i = first; i > 0; i--) {
87             load_order[i] = load_order[i - 1];
88         }
89         load_order[0] = &gtk_libs[first];
90     }
91     return load_order;
92 }
93 
get_loaded()94 static GtkLib* get_loaded() {
95     GtkLib** libs = get_libs_order(GTK_ANY);
96     if (libs == NULL) return NULL;
97     while(!gtk && *libs) {
98         GtkLib* lib = *libs++;
99         if (lib->check(lib->vname, /* load = */FALSE)) {
100             return lib;
101         }
102         if (lib->check(lib->name, /* load = */FALSE)) {
103             return lib;
104         }
105     }
106     return NULL;
107 }
108 
gtk_load(JNIEnv * env,GtkVersion version,gboolean verbose)109 gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) {
110     if (gtk == NULL) {
111         GtkLib* lib = get_loaded();
112         if (lib) {
113             if (verbose) {
114                 fprintf(stderr, "Looking for GTK%d library...\n",
115                                                                  lib->version);
116             }
117             gtk = lib->load(env, lib->vname);
118             if (!gtk) {
119                 gtk = lib->load(env, lib->name);
120             }
121         } else {
122             GtkLib** libs = get_libs_order(version);
123             while (!gtk && libs && *libs) {
124                 lib = *libs++;
125                 if (version == GTK_ANY || lib->version == version) {
126                     if (verbose) {
127                         fprintf(stderr, "Looking for GTK%d library...\n",
128                                                                   lib->version);
129                     }
130                     gtk = lib->load(env, lib->vname);
131                     if (!gtk) {
132                         gtk = lib->load(env, lib->name);
133                     }
134                     if (verbose && !gtk) {
135                         fprintf(stderr, "Not found.\n");
136                     }
137                 }
138             }
139         }
140         if (verbose) {
141             if (gtk) {
142                 fprintf(stderr, "GTK%d library loaded.\n", lib->version);
143             } else {
144                 fprintf(stderr, "Failed to load GTK library.\n");
145             }
146         }
147     }
148     return gtk != NULL;
149 }
150 
check_version(GtkVersion version)151 static gboolean check_version(GtkVersion version) {
152     GtkLib** libs = get_libs_order(version);
153     if (libs == NULL) return FALSE;
154     while (*libs) {
155         GtkLib* lib = *libs++;
156         if (lib->check(lib->vname, /* load = */TRUE)) {
157             return TRUE;
158         }
159         if (lib->check(lib->name, /* load = */TRUE)) {
160             return TRUE;
161         }
162     }
163     return FALSE;
164 }
165 
gtk_check_version(GtkVersion version)166 gboolean gtk_check_version(GtkVersion version) {
167     if (gtk || get_loaded()) {
168         return TRUE;
169     }
170     return check_version(version);
171 }
172 
173