Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8299595: Remove terminally deprecated JavaFX GTK 2 library
Reviewed-by: kcr, jvos
  • Loading branch information
Thiago Milczarek Sayao committed Mar 23, 2023
1 parent 85e529d commit 63635ee
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 182 deletions.
43 changes: 1 addition & 42 deletions buildSrc/linux.gradle
Expand Up @@ -82,40 +82,9 @@ if (hasProperty('toolchainDir')) {
toolchainDir = ""
}

def gtk2CCFlags = [ "-Wno-deprecated-declarations" ];
def gtk3CCFlags = [ "-Wno-deprecated-declarations" ];
def gtk2LinkFlags = [ ];
def gtk3LinkFlags = [ ];

// Create $buildDir/linux_tools.properties file and load props from it
setupTools("linux_gtk2",
{ propFile ->
ByteArrayOutputStream results1 = new ByteArrayOutputStream();
exec {
commandLine("${toolchainDir}pkg-config", "--cflags", "gtk+-2.0", "gthread-2.0", "xtst")
setStandardOutput(results1);
}
propFile << "cflagsGTK2=" << results1.toString().trim() << "\n";

ByteArrayOutputStream results3 = new ByteArrayOutputStream();
exec {
commandLine("${toolchainDir}pkg-config", "--libs", "gtk+-2.0", "gthread-2.0", "xtst")
setStandardOutput(results3);
}
propFile << "libsGTK2=" << results3.toString().trim() << "\n";
},
{ properties ->
def cflagsGTK2 = properties.getProperty("cflagsGTK2")
def libsGTK2 = properties.getProperty("libsGTK2")
if (cflagsGTK2 && libsGTK2) {
gtk2CCFlags.addAll(cflagsGTK2.split(" "))
gtk2LinkFlags.addAll(libsGTK2.split(" "))
} else {
throw new IllegalStateException("GTK2 development packages not found. If GTK2 packages are installed, please remove the build directory and try again.")
}
}
)

setupTools("linux_gtk3",
{ propFile ->
ByteArrayOutputStream results2 = new ByteArrayOutputStream();
Expand Down Expand Up @@ -216,7 +185,7 @@ LINUX.strip = "${toolchainDir}strip"
LINUX.stripArgs = [ "-x" ]

LINUX.glass = [:]
LINUX.glass.variants = ["glass", "glassgtk2", "glassgtk3"]
LINUX.glass.variants = ["glass", "glassgtk3"]

FileTree ft_gtk_launcher = fileTree("${project(":graphics").projectDir}/src/main/native-glass/gtk/") {
include("**/launcher.c")
Expand All @@ -234,16 +203,6 @@ LINUX.glass.glass.linker = linker
LINUX.glass.glass.linkFlags = IS_STATIC_BUILD? linkFlags : [linkFlags, "-lX11", "-ldl"].flatten()
LINUX.glass.glass.lib = "glass"

LINUX.glass.glassgtk2 = [:]
LINUX.glass.glassgtk2.nativeSource = ft_gtk.getFiles()
LINUX.glass.glassgtk2.compiler = compiler
LINUX.glass.glassgtk2.ccFlags = IS_STATIC_BUILD ?
["-fno-threadsafe-statics", cppFlags, gtk2CCFlags].flatten() :
[cppFlags, gtk2CCFlags, "-Werror"].flatten()
LINUX.glass.glassgtk2.linker = linker
LINUX.glass.glassgtk2.linkFlags = IS_STATIC_BUILD ? linkFlags : [linkFlags, gtk2LinkFlags].flatten()
LINUX.glass.glassgtk2.lib = "glassgtk2"

LINUX.glass.glassgtk3 = [:]
LINUX.glass.glassgtk3.nativeSource = ft_gtk.getFiles()
LINUX.glass.glassgtk3.compiler = compiler
Expand Down
Expand Up @@ -42,7 +42,6 @@
import com.sun.javafx.logging.PlatformLogger;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.security.AccessController;
Expand All @@ -51,72 +50,31 @@
import java.util.concurrent.CountDownLatch;
import java.lang.annotation.Native;


final class GtkApplication extends Application implements
InvokeLaterDispatcher.InvokeLaterSubmitter {
private static final String SWT_INTERNAL_CLASS =
"org.eclipse.swt.internal.gtk.OS";
private static final int forcedGtkVersion;
private static boolean gtk2WarningIssued = false;
private static final String GTK2_ALREADY_LOADED_WARNING =
"WARNING: Found GTK 2 library already loaded";
private static final String GTK2_SPECIFIED_WARNING =
"WARNING: A command line option has enabled the GTK 2 library";
private static final String GTK2_FALLBACK_WARNING =
"WARNING: Using GTK 2 library because GTK 3 cannot be loaded ";
private static final String GTK2_DEPRECATION_WARNING =
"WARNING: The JavaFX GTK 2 library is deprecated and will be removed in a future release";
private static boolean gtkVersionWarningIssued = false;
private static final String GTK2_REMOVED_WARNING =
"WARNING: A command line option tried to select the GTK 2 library, which was removed from JavaFX.";

private static final String GTK_INVALID_VERSION_WARNING =
"WARNING: A command line option tried to select an invalid GTK library version.";
private static final String GTK3_FALLBACK_WARNING = "WARNING: The GTK 3 library will be used instead.";

static {
//check for SWT-GTK lib presence
@SuppressWarnings("removal")
Class<?> OS = AccessController.
doPrivileged((PrivilegedAction<Class<?>>) () -> {
try {
return Class.forName(SWT_INTERNAL_CLASS, true,
ClassLoader.getSystemClassLoader());
} catch (Exception e) {}
try {
return Class.forName(SWT_INTERNAL_CLASS, true,
Thread.currentThread().getContextClassLoader());
} catch (Exception e) {}
return null;
});
if (OS != null) {
String gtkVersion = System.getProperty("org.eclipse.swt.internal.gtk.version");
if (gtkVersion != null && gtkVersion.contains(".")) {
PlatformLogger logger = Logging.getJavaFXLogger();
logger.fine("SWT-GTK library found. Try to obtain GTK version.");
@SuppressWarnings("removal")
Method method = AccessController.
doPrivileged((PrivilegedAction<Method>) () -> {
try {
return OS.getMethod("gtk_major_version");
} catch (Exception e) {
return null;
}
});
int ver = 0;
if (method != null) {
try {
ver = ((Number)method.invoke(OS)).intValue();
} catch (Exception e) {
logger.warning("Method gtk_major_version() of " +
"the org.eclipse.swt.internal.gtk.OS class " +
"returns error. SWT GTK version cannot be detected. " +
"GTK3 will be used as default.");
ver = 3;
}
}
if (ver < 2 || ver > 3) {
logger.warning("SWT-GTK uses unsupported major GTK version "
+ ver + ". GTK3 will be used as default.");
ver = 3;
logger.fine(String.format("SWT-GTK library found. Gtk Version = %s.", gtkVersion));
String[] vers = gtkVersion.split("\\.");
int ver = Integer.parseInt(vers[0]);

if (ver != 3) {
throw new UnsupportedOperationException("SWT-GTK uses unsupported major GTK version " + ver + " .");
}

forcedGtkVersion = ver;
if (ver == 2 && !gtk2WarningIssued) {
System.err.println(GTK2_ALREADY_LOADED_WARNING);
System.err.println(GTK2_DEPRECATION_WARNING);
gtk2WarningIssued = true;
}
} else {
forcedGtkVersion = 0;
}
Expand Down Expand Up @@ -162,22 +120,24 @@ private static float getFloat(String propname, float defval, String description)
GtkApplication() {

@SuppressWarnings("removal")
final int gtkVersion = forcedGtkVersion == 0 ?
int gtkVersion = forcedGtkVersion == 0 ?
AccessController.doPrivileged((PrivilegedAction<Integer>) () -> {
String v = System.getProperty("jdk.gtk.version","3");
int ret = 0;
if ("3".equals(v) || v.startsWith("3.")) {
ret = 3;
} else if ("2".equals(v) || v.startsWith("2.")) {
ret = 2;
}
return ret;
return Character.getNumericValue(v.charAt(0));
}) : forcedGtkVersion;

if (gtkVersion == 2 && !gtk2WarningIssued) {
System.err.println(GTK2_SPECIFIED_WARNING);
System.err.println(GTK2_DEPRECATION_WARNING);
gtk2WarningIssued = true;
if (gtkVersion != 3) {
if (!gtkVersionWarningIssued) {
if (gtkVersion == 2) {
System.err.println(GTK2_REMOVED_WARNING);
} else {
System.err.println(GTK_INVALID_VERSION_WARNING);
}
}

System.err.println(GTK3_FALLBACK_WARNING);
gtkVersionWarningIssued = true;
gtkVersion = 3;
}

@SuppressWarnings("removal")
Expand All @@ -204,32 +164,18 @@ private static float getFloat(String propname, float defval, String description)
if (gtkVersionVerbose) {
System.out.println("Glass GTK library to load is already loaded");
}
} else if (libraryToLoad == QUERY_LOAD_GTK2) {
if (gtkVersionVerbose) {
System.out.println("Glass GTK library to load is glassgtk2");
}
NativeLibLoader.loadLibrary("glassgtk2");
if (!gtk2WarningIssued) {
System.err.println(GTK2_FALLBACK_WARNING);
System.err.println(GTK2_DEPRECATION_WARNING);
gtk2WarningIssued = true;
}
} else if (libraryToLoad == QUERY_LOAD_GTK3) {
if (gtkVersionVerbose) {
System.out.println("Glass GTK library to load is glassgtk3");
}
NativeLibLoader.loadLibrary("glassgtk3");
} else {
throw new UnsupportedOperationException("Internal Error");
throw new UnsupportedOperationException("Unable to load glass GTK library.");
}
return null;
});

int version = _initGTK(gtkVersion, gtkVersionVerbose, overrideUIScale);

if (version == -1) {
throw new RuntimeException("Error loading GTK libraries");
}
_initGTK(gtkVersion, gtkVersionVerbose, overrideUIScale);

// Embedded in SWT, with shared event thread
@SuppressWarnings("removal")
Expand All @@ -246,15 +192,14 @@ private static float getFloat(String propname, float defval, String description)
@Native private static final int QUERY_ERROR = -2;
@Native private static final int QUERY_NO_DISPLAY = -1;
@Native private static final int QUERY_USE_CURRENT = 1;
@Native private static final int QUERY_LOAD_GTK2 = 2;
@Native private static final int QUERY_LOAD_GTK3 = 3;
/*
* check the system and return an indication of which library to load
* return values are the QUERY_ constants
*/
private static native int _queryLibrary(int version, boolean verbose);

private static native int _initGTK(int version, boolean verbose, float overrideUIScale);
private static native void _initGTK(int version, boolean verbose, float overrideUIScale);

private void initDisplay() {
Map ds = getDeviceDetails();
Expand Down
Expand Up @@ -95,9 +95,9 @@ jboolean gtk_verbose = JNI_FALSE;
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _initGTK
* Signature: (IZ)I
* Signature: (IZ)V
*/
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
(JNIEnv *env, jclass clazz, jint version, jboolean verbose, jfloat uiScale)
{
(void) clazz;
Expand All @@ -112,7 +112,20 @@ JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
gdk_threads_enter();
gtk_init(NULL, NULL);

return JNI_TRUE;
// Major version is checked before loading
if (version == 3
&& gtk_check_version(version, GTK_3_MIN_MINOR_VERSION, GTK_3_MIN_MICRO_VERSION)) {
char message[100];
snprintf(message, sizeof(message),
"Minimum GTK version required is %d.%d.%d. System has %d.%d.%d.",
version, GTK_3_MIN_MINOR_VERSION, GTK_3_MIN_MICRO_VERSION,
gtk_major_version, gtk_minor_version, gtk_micro_version);

jclass uoe = env->FindClass("java/lang/UnsupportedOperationException");
env->ThrowNew(uoe, message);

return;
}
}

/*
Expand Down
Expand Up @@ -42,6 +42,9 @@
#define GLASS_GTK3
#endif

#define GTK_3_MIN_MINOR_VERSION 8
#define GTK_3_MIN_MICRO_VERSION 0

#ifndef GDK_TOUCH_MASK
#define GDK_TOUCH_MASK (1 << 22)
#endif
Expand Down
42 changes: 20 additions & 22 deletions modules/javafx.graphics/src/main/native-glass/gtk/launcher.c
Expand Up @@ -47,12 +47,8 @@

static jboolean gtk_versionDebug = JNI_FALSE;

static const char * gtk2_chain[] = {
"libglassgtk2.so", "libglassgtk3.so", 0
};

static const char * gtk3_chain[] = {
"libglassgtk3.so", "libglassgtk2.so", 0
"libglassgtk3.so", 0
};

static JavaVM* javaVM;
Expand Down Expand Up @@ -91,14 +87,12 @@ static char * gtk3_not_versioned[] = {
};

// our library set orders defined, null terminated
static char ** two_to_three[] = {
gtk2_versioned, gtk2_not_versioned,
static char ** gtk3_lib_options[] = {
gtk3_versioned, gtk3_not_versioned,
0
};

static char ** three_to_two[] = {
gtk3_versioned, gtk3_not_versioned,
static char ** gtk2_lib_options[] = {
gtk2_versioned, gtk2_not_versioned,
0
};
Expand Down Expand Up @@ -129,11 +123,11 @@ static int try_libraries_noload(char *names[3])
static int sniffLibs(int wantVersion) {

if (gtk_versionDebug) {
printf("checking GTK version %d\n",wantVersion);
printf("checking GTK version %d\n", wantVersion);
}

int success = 1;
char *** use_chain = three_to_two;
char *** use_chain = gtk3_lib_options;
int i, found = 0;

//at first try to detect already loaded GTK version
Expand All @@ -144,18 +138,25 @@ static int sniffLibs(int wantVersion) {
}
}

if (!found) {
if (wantVersion == 0 || wantVersion == 3) {
use_chain = three_to_two;
} else if (wantVersion == 2) {
use_chain = two_to_three;
} else {
if (!found) {
char *** bad_use_chain = gtk2_lib_options;
for (i = 0; bad_use_chain[i] && !found; i++) {
found = try_libraries_noload(bad_use_chain[i]);
if (found && gtk_versionDebug) {
printf("found already loaded unsupported GTK library %s\n", bad_use_chain[i][1]);
}
}

if (found) {
return -1;
}

if (wantVersion != 0 && wantVersion != 3) {
// Note, this should never happen, java should be protecting us
if (gtk_versionDebug) {
printf("bad GTK version specified, assuming 3\n");
}
wantVersion = 3;
use_chain = three_to_two;
}

for (i = 0; use_chain[i] && !found; i++) {
Expand Down Expand Up @@ -208,10 +209,7 @@ JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1queryLibrary
// now check the the presence of the libraries

char version = sniffLibs(suggestedVersion);

if (version == '2') {
return com_sun_glass_ui_gtk_GtkApplication_QUERY_LOAD_GTK2;
} else if (version == '3') {
if (version == '3') {
return com_sun_glass_ui_gtk_GtkApplication_QUERY_LOAD_GTK3;
}

Expand Down

1 comment on commit 63635ee

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.