Skip to content

Commit 63635ee

Browse files
author
Thiago Milczarek Sayao
committed
8299595: Remove terminally deprecated JavaFX GTK 2 library
Reviewed-by: kcr, jvos
1 parent 85e529d commit 63635ee

File tree

8 files changed

+98
-182
lines changed

8 files changed

+98
-182
lines changed

buildSrc/linux.gradle

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,9 @@ if (hasProperty('toolchainDir')) {
8282
toolchainDir = ""
8383
}
8484

85-
def gtk2CCFlags = [ "-Wno-deprecated-declarations" ];
8685
def gtk3CCFlags = [ "-Wno-deprecated-declarations" ];
87-
def gtk2LinkFlags = [ ];
8886
def gtk3LinkFlags = [ ];
8987

90-
// Create $buildDir/linux_tools.properties file and load props from it
91-
setupTools("linux_gtk2",
92-
{ propFile ->
93-
ByteArrayOutputStream results1 = new ByteArrayOutputStream();
94-
exec {
95-
commandLine("${toolchainDir}pkg-config", "--cflags", "gtk+-2.0", "gthread-2.0", "xtst")
96-
setStandardOutput(results1);
97-
}
98-
propFile << "cflagsGTK2=" << results1.toString().trim() << "\n";
99-
100-
ByteArrayOutputStream results3 = new ByteArrayOutputStream();
101-
exec {
102-
commandLine("${toolchainDir}pkg-config", "--libs", "gtk+-2.0", "gthread-2.0", "xtst")
103-
setStandardOutput(results3);
104-
}
105-
propFile << "libsGTK2=" << results3.toString().trim() << "\n";
106-
},
107-
{ properties ->
108-
def cflagsGTK2 = properties.getProperty("cflagsGTK2")
109-
def libsGTK2 = properties.getProperty("libsGTK2")
110-
if (cflagsGTK2 && libsGTK2) {
111-
gtk2CCFlags.addAll(cflagsGTK2.split(" "))
112-
gtk2LinkFlags.addAll(libsGTK2.split(" "))
113-
} else {
114-
throw new IllegalStateException("GTK2 development packages not found. If GTK2 packages are installed, please remove the build directory and try again.")
115-
}
116-
}
117-
)
118-
11988
setupTools("linux_gtk3",
12089
{ propFile ->
12190
ByteArrayOutputStream results2 = new ByteArrayOutputStream();
@@ -216,7 +185,7 @@ LINUX.strip = "${toolchainDir}strip"
216185
LINUX.stripArgs = [ "-x" ]
217186

218187
LINUX.glass = [:]
219-
LINUX.glass.variants = ["glass", "glassgtk2", "glassgtk3"]
188+
LINUX.glass.variants = ["glass", "glassgtk3"]
220189

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

237-
LINUX.glass.glassgtk2 = [:]
238-
LINUX.glass.glassgtk2.nativeSource = ft_gtk.getFiles()
239-
LINUX.glass.glassgtk2.compiler = compiler
240-
LINUX.glass.glassgtk2.ccFlags = IS_STATIC_BUILD ?
241-
["-fno-threadsafe-statics", cppFlags, gtk2CCFlags].flatten() :
242-
[cppFlags, gtk2CCFlags, "-Werror"].flatten()
243-
LINUX.glass.glassgtk2.linker = linker
244-
LINUX.glass.glassgtk2.linkFlags = IS_STATIC_BUILD ? linkFlags : [linkFlags, gtk2LinkFlags].flatten()
245-
LINUX.glass.glassgtk2.lib = "glassgtk2"
246-
247206
LINUX.glass.glassgtk3 = [:]
248207
LINUX.glass.glassgtk3.nativeSource = ft_gtk.getFiles()
249208
LINUX.glass.glassgtk3.compiler = compiler

modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java

Lines changed: 33 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.sun.javafx.logging.PlatformLogger;
4343

4444
import java.io.File;
45-
import java.lang.reflect.Method;
4645
import java.nio.ByteBuffer;
4746
import java.nio.IntBuffer;
4847
import java.security.AccessController;
@@ -51,72 +50,31 @@
5150
import java.util.concurrent.CountDownLatch;
5251
import java.lang.annotation.Native;
5352

53+
5454
final class GtkApplication extends Application implements
5555
InvokeLaterDispatcher.InvokeLaterSubmitter {
56-
private static final String SWT_INTERNAL_CLASS =
57-
"org.eclipse.swt.internal.gtk.OS";
5856
private static final int forcedGtkVersion;
59-
private static boolean gtk2WarningIssued = false;
60-
private static final String GTK2_ALREADY_LOADED_WARNING =
61-
"WARNING: Found GTK 2 library already loaded";
62-
private static final String GTK2_SPECIFIED_WARNING =
63-
"WARNING: A command line option has enabled the GTK 2 library";
64-
private static final String GTK2_FALLBACK_WARNING =
65-
"WARNING: Using GTK 2 library because GTK 3 cannot be loaded ";
66-
private static final String GTK2_DEPRECATION_WARNING =
67-
"WARNING: The JavaFX GTK 2 library is deprecated and will be removed in a future release";
57+
private static boolean gtkVersionWarningIssued = false;
58+
private static final String GTK2_REMOVED_WARNING =
59+
"WARNING: A command line option tried to select the GTK 2 library, which was removed from JavaFX.";
6860

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

7065
static {
71-
//check for SWT-GTK lib presence
72-
@SuppressWarnings("removal")
73-
Class<?> OS = AccessController.
74-
doPrivileged((PrivilegedAction<Class<?>>) () -> {
75-
try {
76-
return Class.forName(SWT_INTERNAL_CLASS, true,
77-
ClassLoader.getSystemClassLoader());
78-
} catch (Exception e) {}
79-
try {
80-
return Class.forName(SWT_INTERNAL_CLASS, true,
81-
Thread.currentThread().getContextClassLoader());
82-
} catch (Exception e) {}
83-
return null;
84-
});
85-
if (OS != null) {
66+
String gtkVersion = System.getProperty("org.eclipse.swt.internal.gtk.version");
67+
if (gtkVersion != null && gtkVersion.contains(".")) {
8668
PlatformLogger logger = Logging.getJavaFXLogger();
87-
logger.fine("SWT-GTK library found. Try to obtain GTK version.");
88-
@SuppressWarnings("removal")
89-
Method method = AccessController.
90-
doPrivileged((PrivilegedAction<Method>) () -> {
91-
try {
92-
return OS.getMethod("gtk_major_version");
93-
} catch (Exception e) {
94-
return null;
95-
}
96-
});
97-
int ver = 0;
98-
if (method != null) {
99-
try {
100-
ver = ((Number)method.invoke(OS)).intValue();
101-
} catch (Exception e) {
102-
logger.warning("Method gtk_major_version() of " +
103-
"the org.eclipse.swt.internal.gtk.OS class " +
104-
"returns error. SWT GTK version cannot be detected. " +
105-
"GTK3 will be used as default.");
106-
ver = 3;
107-
}
108-
}
109-
if (ver < 2 || ver > 3) {
110-
logger.warning("SWT-GTK uses unsupported major GTK version "
111-
+ ver + ". GTK3 will be used as default.");
112-
ver = 3;
69+
logger.fine(String.format("SWT-GTK library found. Gtk Version = %s.", gtkVersion));
70+
String[] vers = gtkVersion.split("\\.");
71+
int ver = Integer.parseInt(vers[0]);
72+
73+
if (ver != 3) {
74+
throw new UnsupportedOperationException("SWT-GTK uses unsupported major GTK version " + ver + " .");
11375
}
76+
11477
forcedGtkVersion = ver;
115-
if (ver == 2 && !gtk2WarningIssued) {
116-
System.err.println(GTK2_ALREADY_LOADED_WARNING);
117-
System.err.println(GTK2_DEPRECATION_WARNING);
118-
gtk2WarningIssued = true;
119-
}
12078
} else {
12179
forcedGtkVersion = 0;
12280
}
@@ -162,22 +120,24 @@ private static float getFloat(String propname, float defval, String description)
162120
GtkApplication() {
163121

164122
@SuppressWarnings("removal")
165-
final int gtkVersion = forcedGtkVersion == 0 ?
123+
int gtkVersion = forcedGtkVersion == 0 ?
166124
AccessController.doPrivileged((PrivilegedAction<Integer>) () -> {
167125
String v = System.getProperty("jdk.gtk.version","3");
168-
int ret = 0;
169-
if ("3".equals(v) || v.startsWith("3.")) {
170-
ret = 3;
171-
} else if ("2".equals(v) || v.startsWith("2.")) {
172-
ret = 2;
173-
}
174-
return ret;
126+
return Character.getNumericValue(v.charAt(0));
175127
}) : forcedGtkVersion;
176128

177-
if (gtkVersion == 2 && !gtk2WarningIssued) {
178-
System.err.println(GTK2_SPECIFIED_WARNING);
179-
System.err.println(GTK2_DEPRECATION_WARNING);
180-
gtk2WarningIssued = true;
129+
if (gtkVersion != 3) {
130+
if (!gtkVersionWarningIssued) {
131+
if (gtkVersion == 2) {
132+
System.err.println(GTK2_REMOVED_WARNING);
133+
} else {
134+
System.err.println(GTK_INVALID_VERSION_WARNING);
135+
}
136+
}
137+
138+
System.err.println(GTK3_FALLBACK_WARNING);
139+
gtkVersionWarningIssued = true;
140+
gtkVersion = 3;
181141
}
182142

183143
@SuppressWarnings("removal")
@@ -204,32 +164,18 @@ private static float getFloat(String propname, float defval, String description)
204164
if (gtkVersionVerbose) {
205165
System.out.println("Glass GTK library to load is already loaded");
206166
}
207-
} else if (libraryToLoad == QUERY_LOAD_GTK2) {
208-
if (gtkVersionVerbose) {
209-
System.out.println("Glass GTK library to load is glassgtk2");
210-
}
211-
NativeLibLoader.loadLibrary("glassgtk2");
212-
if (!gtk2WarningIssued) {
213-
System.err.println(GTK2_FALLBACK_WARNING);
214-
System.err.println(GTK2_DEPRECATION_WARNING);
215-
gtk2WarningIssued = true;
216-
}
217167
} else if (libraryToLoad == QUERY_LOAD_GTK3) {
218168
if (gtkVersionVerbose) {
219169
System.out.println("Glass GTK library to load is glassgtk3");
220170
}
221171
NativeLibLoader.loadLibrary("glassgtk3");
222172
} else {
223-
throw new UnsupportedOperationException("Internal Error");
173+
throw new UnsupportedOperationException("Unable to load glass GTK library.");
224174
}
225175
return null;
226176
});
227177

228-
int version = _initGTK(gtkVersion, gtkVersionVerbose, overrideUIScale);
229-
230-
if (version == -1) {
231-
throw new RuntimeException("Error loading GTK libraries");
232-
}
178+
_initGTK(gtkVersion, gtkVersionVerbose, overrideUIScale);
233179

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

257-
private static native int _initGTK(int version, boolean verbose, float overrideUIScale);
202+
private static native void _initGTK(int version, boolean verbose, float overrideUIScale);
258203

259204
private void initDisplay() {
260205
Map ds = getDeviceDetails();

modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ jboolean gtk_verbose = JNI_FALSE;
9595
/*
9696
* Class: com_sun_glass_ui_gtk_GtkApplication
9797
* Method: _initGTK
98-
* Signature: (IZ)I
98+
* Signature: (IZ)V
9999
*/
100-
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
100+
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
101101
(JNIEnv *env, jclass clazz, jint version, jboolean verbose, jfloat uiScale)
102102
{
103103
(void) clazz;
@@ -112,7 +112,20 @@ JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
112112
gdk_threads_enter();
113113
gtk_init(NULL, NULL);
114114

115-
return JNI_TRUE;
115+
// Major version is checked before loading
116+
if (version == 3
117+
&& gtk_check_version(version, GTK_3_MIN_MINOR_VERSION, GTK_3_MIN_MICRO_VERSION)) {
118+
char message[100];
119+
snprintf(message, sizeof(message),
120+
"Minimum GTK version required is %d.%d.%d. System has %d.%d.%d.",
121+
version, GTK_3_MIN_MINOR_VERSION, GTK_3_MIN_MICRO_VERSION,
122+
gtk_major_version, gtk_minor_version, gtk_micro_version);
123+
124+
jclass uoe = env->FindClass("java/lang/UnsupportedOperationException");
125+
env->ThrowNew(uoe, message);
126+
127+
return;
128+
}
116129
}
117130

118131
/*

modules/javafx.graphics/src/main/native-glass/gtk/glass_general.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#define GLASS_GTK3
4343
#endif
4444

45+
#define GTK_3_MIN_MINOR_VERSION 8
46+
#define GTK_3_MIN_MICRO_VERSION 0
47+
4548
#ifndef GDK_TOUCH_MASK
4649
#define GDK_TOUCH_MASK (1 << 22)
4750
#endif

modules/javafx.graphics/src/main/native-glass/gtk/launcher.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@
4747

4848
static jboolean gtk_versionDebug = JNI_FALSE;
4949

50-
static const char * gtk2_chain[] = {
51-
"libglassgtk2.so", "libglassgtk3.so", 0
52-
};
53-
5450
static const char * gtk3_chain[] = {
55-
"libglassgtk3.so", "libglassgtk2.so", 0
51+
"libglassgtk3.so", 0
5652
};
5753

5854
static JavaVM* javaVM;
@@ -91,14 +87,12 @@ static char * gtk3_not_versioned[] = {
9187
};
9288

9389
// our library set orders defined, null terminated
94-
static char ** two_to_three[] = {
95-
gtk2_versioned, gtk2_not_versioned,
90+
static char ** gtk3_lib_options[] = {
9691
gtk3_versioned, gtk3_not_versioned,
9792
0
9893
};
9994

100-
static char ** three_to_two[] = {
101-
gtk3_versioned, gtk3_not_versioned,
95+
static char ** gtk2_lib_options[] = {
10296
gtk2_versioned, gtk2_not_versioned,
10397
0
10498
};
@@ -129,11 +123,11 @@ static int try_libraries_noload(char *names[3])
129123
static int sniffLibs(int wantVersion) {
130124

131125
if (gtk_versionDebug) {
132-
printf("checking GTK version %d\n",wantVersion);
126+
printf("checking GTK version %d\n", wantVersion);
133127
}
134128

135129
int success = 1;
136-
char *** use_chain = three_to_two;
130+
char *** use_chain = gtk3_lib_options;
137131
int i, found = 0;
138132

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

147-
if (!found) {
148-
if (wantVersion == 0 || wantVersion == 3) {
149-
use_chain = three_to_two;
150-
} else if (wantVersion == 2) {
151-
use_chain = two_to_three;
152-
} else {
141+
if (!found) {
142+
char *** bad_use_chain = gtk2_lib_options;
143+
for (i = 0; bad_use_chain[i] && !found; i++) {
144+
found = try_libraries_noload(bad_use_chain[i]);
145+
if (found && gtk_versionDebug) {
146+
printf("found already loaded unsupported GTK library %s\n", bad_use_chain[i][1]);
147+
}
148+
}
149+
150+
if (found) {
151+
return -1;
152+
}
153+
154+
if (wantVersion != 0 && wantVersion != 3) {
153155
// Note, this should never happen, java should be protecting us
154156
if (gtk_versionDebug) {
155157
printf("bad GTK version specified, assuming 3\n");
156158
}
157159
wantVersion = 3;
158-
use_chain = three_to_two;
159160
}
160161

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

210211
char version = sniffLibs(suggestedVersion);
211-
212-
if (version == '2') {
213-
return com_sun_glass_ui_gtk_GtkApplication_QUERY_LOAD_GTK2;
214-
} else if (version == '3') {
212+
if (version == '3') {
215213
return com_sun_glass_ui_gtk_GtkApplication_QUERY_LOAD_GTK3;
216214
}
217215

0 commit comments

Comments
 (0)