< prev index next >

src/java.desktop/unix/native/libawt_xawt/awt/gtk_interface.c

Print this page
rev 56159 : 8230480: check malloc/calloc results in java.desktop
   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


  49         JNI_LIB_NAME("gtk-3"),
  50         VERSIONED_JNI_LIB_NAME("gtk-3", "0"),
  51         &gtk3_load,
  52         &gtk3_check
  53     },
  54     {
  55         GTK_2,
  56         JNI_LIB_NAME("gtk-x11-2.0"),
  57         VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0"),
  58         &gtk2_load,
  59         &gtk2_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 = calloc(n_libs + 1, sizeof(GtkLib *));



  69     }
  70     int i, first = 0;
  71     for (i = 0; i < n_libs; i++) {
  72         load_order[i] = &gtk_libs[i];
  73         if (load_order[i]->version == version) {
  74             first = i;
  75         }
  76     }
  77     if (first) {
  78         for (i = first; i > 0; i--) {
  79             load_order[i] = load_order[i - 1];
  80         }
  81         load_order[0] = &gtk_libs[first];
  82     }
  83     return load_order;
  84 }
  85 
  86 static GtkLib* get_loaded() {
  87     GtkLib** libs = get_libs_order(GTK_ANY);

  88     while(!gtk && *libs) {
  89         GtkLib* lib = *libs++;
  90         if (lib->check(lib->vname, /* load = */FALSE)) {
  91             return lib;
  92         }
  93         if (lib->check(lib->name, /* load = */FALSE)) {
  94             return lib;
  95         }
  96     }
  97     return NULL;
  98 }
  99 
 100 gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) {
 101     if (gtk == NULL) {
 102         GtkLib* lib = get_loaded();
 103         if (lib) {
 104             if (verbose) {
 105                 fprintf(stderr, "Looking for GTK%d library...\n",
 106                                                                  lib->version);
 107             }
 108             gtk = lib->load(env, lib->vname);
 109             if (!gtk) {
 110                 gtk = lib->load(env, lib->name);
 111             }
 112         } else {
 113             GtkLib** libs = get_libs_order(version);
 114             while (!gtk && *libs) {
 115                 lib = *libs++;
 116                 if (version == GTK_ANY || lib->version == version) {
 117                     if (verbose) {
 118                         fprintf(stderr, "Looking for GTK%d library...\n",
 119                                                                   lib->version);
 120                     }
 121                     gtk = lib->load(env, lib->vname);
 122                     if (!gtk) {
 123                         gtk = lib->load(env, lib->name);
 124                     }
 125                     if (verbose && !gtk) {
 126                         fprintf(stderr, "Not found.\n");
 127                     }
 128                 }
 129             }
 130         }
 131         if (verbose) {
 132             if (gtk) {
 133                 fprintf(stderr, "GTK%d library loaded.\n", lib->version);
 134             } else {
 135                 fprintf(stderr, "Failed to load GTK library.\n");
 136             }
 137         }
 138     }
 139     return gtk != NULL;
 140 }
 141 
 142 static gboolean check_version(GtkVersion version) {
 143     GtkLib** libs = get_libs_order(version);

 144     while (*libs) {
 145         GtkLib* lib = *libs++;
 146         if (lib->check(lib->vname, /* load = */TRUE)) {
 147             return TRUE;
 148         }
 149         if (lib->check(lib->name, /* load = */TRUE)) {
 150             return TRUE;
 151         }
 152     }
 153     return FALSE;
 154 }
 155 
 156 gboolean gtk_check_version(GtkVersion version) {
 157     if (gtk || get_loaded()) {
 158         return TRUE;
 159     }
 160     return check_version(version);
 161 }
 162 
   1 /*
   2  * Copyright (c) 2005, 2019, 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


  49         JNI_LIB_NAME("gtk-3"),
  50         VERSIONED_JNI_LIB_NAME("gtk-3", "0"),
  51         &gtk3_load,
  52         &gtk3_check
  53     },
  54     {
  55         GTK_2,
  56         JNI_LIB_NAME("gtk-x11-2.0"),
  57         VERSIONED_JNI_LIB_NAME("gtk-x11-2.0", "0"),
  58         &gtk2_load,
  59         &gtk2_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 = calloc(n_libs + 1, sizeof(GtkLib *));
  69         if (load_order == NULL) {
  70           return NULL;
  71         }
  72     }
  73     int i, first = 0;
  74     for (i = 0; i < n_libs; i++) {
  75         load_order[i] = &gtk_libs[i];
  76         if (load_order[i]->version == version) {
  77             first = i;
  78         }
  79     }
  80     if (first) {
  81         for (i = first; i > 0; i--) {
  82             load_order[i] = load_order[i - 1];
  83         }
  84         load_order[0] = &gtk_libs[first];
  85     }
  86     return load_order;
  87 }
  88 
  89 static GtkLib* get_loaded() {
  90     GtkLib** libs = get_libs_order(GTK_ANY);
  91     if (libs == NULL) return NULL;
  92     while(!gtk && *libs) {
  93         GtkLib* lib = *libs++;
  94         if (lib->check(lib->vname, /* load = */FALSE)) {
  95             return lib;
  96         }
  97         if (lib->check(lib->name, /* load = */FALSE)) {
  98             return lib;
  99         }
 100     }
 101     return NULL;
 102 }
 103 
 104 gboolean gtk_load(JNIEnv *env, GtkVersion version, gboolean verbose) {
 105     if (gtk == NULL) {
 106         GtkLib* lib = get_loaded();
 107         if (lib) {
 108             if (verbose) {
 109                 fprintf(stderr, "Looking for GTK%d library...\n",
 110                                                                  lib->version);
 111             }
 112             gtk = lib->load(env, lib->vname);
 113             if (!gtk) {
 114                 gtk = lib->load(env, lib->name);
 115             }
 116         } else {
 117             GtkLib** libs = get_libs_order(version);
 118             while (!gtk && libs && *libs) {
 119                 lib = *libs++;
 120                 if (version == GTK_ANY || lib->version == version) {
 121                     if (verbose) {
 122                         fprintf(stderr, "Looking for GTK%d library...\n",
 123                                                                   lib->version);
 124                     }
 125                     gtk = lib->load(env, lib->vname);
 126                     if (!gtk) {
 127                         gtk = lib->load(env, lib->name);
 128                     }
 129                     if (verbose && !gtk) {
 130                         fprintf(stderr, "Not found.\n");
 131                     }
 132                 }
 133             }
 134         }
 135         if (verbose) {
 136             if (gtk) {
 137                 fprintf(stderr, "GTK%d library loaded.\n", lib->version);
 138             } else {
 139                 fprintf(stderr, "Failed to load GTK library.\n");
 140             }
 141         }
 142     }
 143     return gtk != NULL;
 144 }
 145 
 146 static gboolean check_version(GtkVersion version) {
 147     GtkLib** libs = get_libs_order(version);
 148     if (libs == NULL) return FALSE;
 149     while (*libs) {
 150         GtkLib* lib = *libs++;
 151         if (lib->check(lib->vname, /* load = */TRUE)) {
 152             return TRUE;
 153         }
 154         if (lib->check(lib->name, /* load = */TRUE)) {
 155             return TRUE;
 156         }
 157     }
 158     return FALSE;
 159 }
 160 
 161 gboolean gtk_check_version(GtkVersion version) {
 162     if (gtk || get_loaded()) {
 163         return TRUE;
 164     }
 165     return check_version(version);
 166 }
 167 
< prev index next >