Path: blob/master/src/java.desktop/unix/native/libawt_xawt/awt/gtk_interface.c
41155 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifdef HEADLESS26#error This file should not be included in headless library27#endif2829#include <dlfcn.h>30#include <stdlib.h>31#include "jvm_md.h"32#include "gtk_interface.h"3334GtkApi* gtk2_load(JNIEnv *env, const char* lib_name);35GtkApi* gtk3_load(JNIEnv *env, const char* lib_name);3637gboolean gtk2_check(const char* lib_name, gboolean load);38gboolean gtk3_check(const char* lib_name, gboolean load);3940GtkApi *gtk;4142typedef struct {43GtkVersion version;44const char* name;45const char* vname;46GtkApi* (*load)(JNIEnv *env, const char* lib_name);47gboolean (*check)(const char* lib_name, gboolean load);48} GtkLib;4950static GtkLib gtk_libs[] = {51{52GTK_3,53JNI_LIB_NAME("gtk-3"),54VERSIONED_JNI_LIB_NAME("gtk-3", "0"),55>k3_load,56>k3_check57},58{59GTK_2,60JNI_LIB_NAME("gtk-x11-2.0"),61VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0"),62>k2_load,63>k2_check64}65};6667static GtkLib** get_libs_order(GtkVersion version) {68static GtkLib** load_order;69static int n_libs = 0;70if (!n_libs) {71n_libs = sizeof(gtk_libs) / sizeof(GtkLib);72load_order = calloc(n_libs + 1, sizeof(GtkLib *));73if (load_order == NULL) {74return NULL;75}76}77int i, first = 0;78for (i = 0; i < n_libs; i++) {79load_order[i] = >k_libs[i];80if (load_order[i]->version == version) {81first = i;82}83}84if (first) {85for (i = first; i > 0; i--) {86load_order[i] = load_order[i - 1];87}88load_order[0] = >k_libs[first];89}90return load_order;91}9293static GtkLib* get_loaded() {94GtkLib** libs = get_libs_order(GTK_ANY);95if (libs == NULL) return NULL;96while(!gtk && *libs) {97GtkLib* lib = *libs++;98if (lib->check(lib->vname, /* load = */FALSE)) {99return lib;100}101if (lib->check(lib->name, /* load = */FALSE)) {102return lib;103}104}105return NULL;106}107108gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) {109if (gtk == NULL) {110GtkLib* lib = get_loaded();111if (lib) {112if (verbose) {113fprintf(stderr, "Looking for GTK%d library...\n",114lib->version);115}116gtk = lib->load(env, lib->vname);117if (!gtk) {118gtk = lib->load(env, lib->name);119}120} else {121GtkLib** libs = get_libs_order(version);122while (!gtk && libs && *libs) {123lib = *libs++;124if (version == GTK_ANY || lib->version == version) {125if (verbose) {126fprintf(stderr, "Looking for GTK%d library...\n",127lib->version);128}129gtk = lib->load(env, lib->vname);130if (!gtk) {131gtk = lib->load(env, lib->name);132}133if (verbose && !gtk) {134fprintf(stderr, "Not found.\n");135}136}137}138}139if (verbose) {140if (gtk) {141fprintf(stderr, "GTK%d library loaded.\n", lib->version);142} else {143fprintf(stderr, "Failed to load GTK library.\n");144}145}146}147return gtk != NULL;148}149150static gboolean check_version(GtkVersion version) {151GtkLib** libs = get_libs_order(version);152if (libs == NULL) return FALSE;153while (*libs) {154GtkLib* lib = *libs++;155if (lib->check(lib->vname, /* load = */TRUE)) {156return TRUE;157}158if (lib->check(lib->name, /* load = */TRUE)) {159return TRUE;160}161}162return FALSE;163}164165gboolean gtk_check_version(GtkVersion version) {166if (gtk || get_loaded()) {167return TRUE;168}169return check_version(version);170}171172173174