4242import com .sun .javafx .logging .PlatformLogger ;
4343
4444import java .io .File ;
45- import java .lang .reflect .Method ;
4645import java .nio .ByteBuffer ;
4746import java .nio .IntBuffer ;
4847import java .security .AccessController ;
5150import java .util .concurrent .CountDownLatch ;
5251import java .lang .annotation .Native ;
5352
53+
5454final 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 ();
0 commit comments