Added GtkSupport.FORCE_GTK2, in case the developer needs to force it to use GTK2 (in which case, appindicator1 is tried first). Removed optional dispatch event loop.

This commit is contained in:
nathan 2016-02-13 17:39:10 +01:00
parent 176f64fd6e
commit f888de6482
2 changed files with 58 additions and 34 deletions

View File

@ -41,9 +41,21 @@ class AppIndicatorQuery {
// NOTE: GtkSupport uses this info to figure out WHAT VERSION OF GTK to use: appindiactor1 -> GTk2, appindicator3 -> GTK3. // NOTE: GtkSupport uses this info to figure out WHAT VERSION OF GTK to use: appindiactor1 -> GTk2, appindicator3 -> GTK3.
if (GtkSupport.FORCE_GTK2) {
// try loading appindicator1 first, maybe it's there?
try {
library = Native.loadLibrary("appindicator1", AppIndicator.class);
if (library != null) {
return (AppIndicator) library;
}
} catch (Throwable ignored) {
}
}
// start with base version // start with base version
try { try {
library = Native.loadLibrary("appindicator3", AppIndicator.class); library = Native.loadLibrary("appindicator", AppIndicator.class);
if (library != null) { if (library != null) {
String s = library.toString(); String s = library.toString();
if (s.indexOf("appindicator3") > 0) { if (s.indexOf("appindicator3") > 0) {
@ -58,7 +70,6 @@ class AppIndicatorQuery {
// whoops. Symbolic links are bugged out. Look manually for it... // whoops. Symbolic links are bugged out. Look manually for it...
// version 1 is better than version 3, because of dumb shit redhat did.
try { try {
library = Native.loadLibrary("appindicator1", AppIndicator.class); library = Native.loadLibrary("appindicator1", AppIndicator.class);
if (library != null) { if (library != null) {

View File

@ -34,8 +34,8 @@ class GtkSupport {
private static volatile Thread gtkDispatchThread; private static volatile Thread gtkDispatchThread;
@Property @Property
/** Enables/Disables the creation of a native GTK event loop. Useful if you are already creating one via SWT/etc. */ /** Forces the system to always choose GTK2 (even when GTK3 might be available). SWT & JavaFX both use GTK2! */
public static boolean CREATE_EVENT_LOOP = true; public static boolean FORCE_GTK2 = false;
/** /**
* must call get() before accessing this! Only "Gtk" interface should access this! * must call get() before accessing this! Only "Gtk" interface should access this!
@ -45,13 +45,30 @@ class GtkSupport {
public static volatile boolean isGtk2 = false; public static volatile boolean isGtk2 = false;
/** /**
* Helper for GTK, because we could have v3 or v2 * Helper for GTK, because we could have v3 or v2.
*
* Observations: SWT & JavaFX both use GTK2, and we can't load GTK3 if GTK2 symbols are loaded
*/ */
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public static public static
Gtk get() { Gtk get() {
Object library; Object library;
boolean shouldUseGtk2 = GtkSupport.FORCE_GTK2;
// in some cases, we ALWAYS want to try GTK2 first
if (shouldUseGtk2) {
try {
gtk_status_icon_position_menu = Function.getFunction("gtk-x11-2.0", "gtk_status_icon_position_menu");
library = Native.loadLibrary("gtk-x11-2.0", Gtk.class);
if (library != null) {
isGtk2 = true;
return (Gtk) library;
}
} catch (Throwable ignored) {
}
}
if (AppIndicatorQuery.isLoaded) { if (AppIndicatorQuery.isLoaded) {
if (AppIndicatorQuery.isVersion3) { if (AppIndicatorQuery.isVersion3) {
// appindicator3 requires GTK3 // appindicator3 requires GTK3
@ -133,36 +150,34 @@ class GtkSupport {
gtkDispatchThread.start(); gtkDispatchThread.start();
if (CREATE_EVENT_LOOP) { // startup the GTK GUI event loop. There can be multiple/nested loops.
// startup the GTK GUI event loop. There can be multiple/nested loops. final CountDownLatch blockUntilStarted = new CountDownLatch(1);
final CountDownLatch blockUntilStarted = new CountDownLatch(1); Thread gtkUpdateThread = new Thread() {
Thread gtkUpdateThread = new Thread() { @Override
@Override public
public void run() {
void run() { Gtk instance = Gtk.INSTANCE;
Gtk instance = Gtk.INSTANCE;
// prep for the event loop. // prep for the event loop.
instance.gdk_threads_init(); instance.gdk_threads_init();
instance.gtk_init(0, null); instance.gtk_init(0, null);
GThread.INSTANCE.g_thread_init(null); GThread.INSTANCE.g_thread_init(null);
// notify our main thread to continue // notify our main thread to continue
blockUntilStarted.countDown(); blockUntilStarted.countDown();
// blocks unit quit // blocks unit quit
instance.gtk_main(); instance.gtk_main();
}
};
gtkUpdateThread.setName("GTK Event Loop (Native)");
gtkUpdateThread.start();
try {
// we CANNOT continue until the GTK thread has started!
blockUntilStarted.await();
} catch (InterruptedException e) {
e.printStackTrace();
} }
};
gtkUpdateThread.setName("GTK Event Loop (Native)");
gtkUpdateThread.start();
try {
// we CANNOT continue until the GTK thread has started!
blockUntilStarted.await();
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
} }
@ -181,9 +196,7 @@ class GtkSupport {
public static public static
void shutdownGui() { void shutdownGui() {
if (CREATE_EVENT_LOOP) { Gtk.INSTANCE.gtk_main_quit();
Gtk.INSTANCE.gtk_main_quit();
}
started = false; started = false;
gtkDispatchThread.interrupt(); gtkDispatchThread.interrupt();