Added preferGtk3 to GTK loading. Fail loading GTK if GTK2 is forced, and

GTK2 cannot load. Code polish.
This commit is contained in:
nathan 2017-07-19 00:50:42 +02:00
parent 1abe7b60ab
commit 8d7d35182a
2 changed files with 35 additions and 30 deletions

View File

@ -33,6 +33,7 @@ import dorkbox.util.Swt;
public
class GtkEventDispatch {
static boolean FORCE_GTK2 = false;
static boolean PREFER_GTK3 = false;
static boolean DEBUG = false;
// have to save these in a field to prevent GC on the objects (since they go out-of-scope from java)
@ -47,7 +48,7 @@ class GtkEventDispatch {
}
};
private static volatile boolean started = false;
private static boolean started = false;
@SuppressWarnings("FieldCanBeLocal")
private static Thread gtkUpdateThread = null;
@ -60,14 +61,15 @@ class GtkEventDispatch {
public static
void startGui(final boolean FORCE_GTK2, final boolean DEBUG) {
public static synchronized
void startGui(final boolean forceGtk2, final boolean preferGkt3, final boolean debug) {
// only permit one startup per JVM instance
if (!started) {
started = true;
GtkEventDispatch.FORCE_GTK2 = FORCE_GTK2;
GtkEventDispatch.DEBUG = DEBUG;
GtkEventDispatch.FORCE_GTK2 = forceGtk2;
GtkEventDispatch.PREFER_GTK3 = preferGkt3;
GtkEventDispatch.DEBUG = debug;
// startup the GTK GUI event loop. There can be multiple/nested loops.
@ -80,7 +82,7 @@ class GtkEventDispatch {
public
void run() {
Glib.GLogFunc orig = null;
if (DEBUG) {
if (debug) {
// don't suppress GTK warnings in debug mode
LoggerFactory.getLogger(GtkEventDispatch.class).debug("Running GTK Native Event Loop");
} else {
@ -120,7 +122,7 @@ class GtkEventDispatch {
* Waits for the all posted events to GTK to finish loading
*/
@SuppressWarnings("Duplicates")
public static
public static synchronized
void waitForEventsToComplete() {
final CountDownLatch blockUntilStarted = new CountDownLatch(1);
@ -275,22 +277,6 @@ class GtkEventDispatch {
}
}
public static
void shutdownGui() {
dispatchAndWait(new Runnable() {
@Override
public
void run() {
// If JavaFX/SWT is used, this is UNNECESSARY (and will break SWT/JavaFX shutdown)
if (!GtkLoader.alreadyRunningGTK) {
Gtk2.gtk_main_quit();
}
started = false;
}
});
}
public static
void dispatchAndWait(final Runnable runnable) {
if (isDispatch.get()) {
@ -356,4 +342,20 @@ class GtkEventDispatch {
isDispatch.set(false);
}
}
public static synchronized
void shutdownGui() {
dispatchAndWait(new Runnable() {
@Override
public
void run() {
// If JavaFX/SWT is used, this is UNNECESSARY (and will break SWT/JavaFX shutdown)
if (!GtkLoader.alreadyRunningGTK) {
Gtk2.gtk_main_quit();
}
started = false;
}
});
}
}

View File

@ -62,7 +62,10 @@ class GtkLoader {
* SWT uses GTK2 or GTK3. We do not work with the GTK3 version of SWT.
*/
static {
boolean shouldUseGtk2 = GtkEventDispatch.FORCE_GTK2;
boolean forceGtk2 = GtkEventDispatch.FORCE_GTK2;
// prefer GTK3 and force GTK2 are mutually exclusive
boolean preferGtk3 = !forceGtk2 && GtkEventDispatch.PREFER_GTK3;
boolean _isGtk2 = false;
boolean _isLoaded = false;
boolean _alreadyRunningGTK = false;
@ -76,11 +79,6 @@ class GtkLoader {
}
if (OSUtil.Linux.isKali()) {
// Kali linux has some WEIRD graphical oddities via GTK3. GTK2 looks just fine.
shouldUseGtk2 = true;
}
// we can force the system to use the swing indicator, which WORKS, but doesn't support transparency in the icon. However, there
// are certain GTK functions we might want to use (even if we are Swing or AWT), so we load GTK anyways...
@ -88,7 +86,7 @@ class GtkLoader {
String gtk2LibName = "gtk-x11-2.0";
String gtk3LibName = "libgtk-3.so.0";
if (!_isLoaded && shouldUseGtk2) {
if (!_isLoaded && (forceGtk2 || !preferGtk3)) {
try {
NativeLibrary library = JnaHelper.register(gtk2LibName, Gtk2.class);
@ -113,6 +111,11 @@ class GtkLoader {
LoggerFactory.getLogger(GtkLoader.class).error("Error loading library", e);
}
}
if (forceGtk2) {
// don't try anything else if we forced GTK2
_isLoaded = true;
}
}
// now for the defaults...