From f888de648296a44fa66d60e188703b58b6337027 Mon Sep 17 00:00:00 2001 From: nathan Date: Sat, 13 Feb 2016 17:39:10 +0100 Subject: [PATCH] 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. --- .../util/jna/linux/AppIndicatorQuery.java | 15 +++- .../dorkbox/util/jna/linux/GtkSupport.java | 77 +++++++++++-------- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/jna/linux/AppIndicatorQuery.java b/Dorkbox-Util/src/dorkbox/util/jna/linux/AppIndicatorQuery.java index 92ce8bb..e3d93b9 100644 --- a/Dorkbox-Util/src/dorkbox/util/jna/linux/AppIndicatorQuery.java +++ b/Dorkbox-Util/src/dorkbox/util/jna/linux/AppIndicatorQuery.java @@ -41,9 +41,21 @@ class AppIndicatorQuery { // 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 try { - library = Native.loadLibrary("appindicator3", AppIndicator.class); + library = Native.loadLibrary("appindicator", AppIndicator.class); if (library != null) { String s = library.toString(); if (s.indexOf("appindicator3") > 0) { @@ -58,7 +70,6 @@ class AppIndicatorQuery { // whoops. Symbolic links are bugged out. Look manually for it... - // version 1 is better than version 3, because of dumb shit redhat did. try { library = Native.loadLibrary("appindicator1", AppIndicator.class); if (library != null) { diff --git a/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java b/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java index 047a112..219c16a 100644 --- a/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java +++ b/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java @@ -34,8 +34,8 @@ class GtkSupport { private static volatile Thread gtkDispatchThread; @Property - /** Enables/Disables the creation of a native GTK event loop. Useful if you are already creating one via SWT/etc. */ - public static boolean CREATE_EVENT_LOOP = true; + /** Forces the system to always choose GTK2 (even when GTK3 might be available). SWT & JavaFX both use GTK2! */ + public static boolean FORCE_GTK2 = false; /** * must call get() before accessing this! Only "Gtk" interface should access this! @@ -45,13 +45,30 @@ class GtkSupport { 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") public static Gtk get() { 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.isVersion3) { // appindicator3 requires GTK3 @@ -133,36 +150,34 @@ class GtkSupport { gtkDispatchThread.start(); - if (CREATE_EVENT_LOOP) { - // startup the GTK GUI event loop. There can be multiple/nested loops. - final CountDownLatch blockUntilStarted = new CountDownLatch(1); - Thread gtkUpdateThread = new Thread() { - @Override - public - void run() { - Gtk instance = Gtk.INSTANCE; + // startup the GTK GUI event loop. There can be multiple/nested loops. + final CountDownLatch blockUntilStarted = new CountDownLatch(1); + Thread gtkUpdateThread = new Thread() { + @Override + public + void run() { + Gtk instance = Gtk.INSTANCE; - // prep for the event loop. - instance.gdk_threads_init(); - instance.gtk_init(0, null); - GThread.INSTANCE.g_thread_init(null); + // prep for the event loop. + instance.gdk_threads_init(); + instance.gtk_init(0, null); + GThread.INSTANCE.g_thread_init(null); - // notify our main thread to continue - blockUntilStarted.countDown(); + // notify our main thread to continue + blockUntilStarted.countDown(); - // blocks unit quit - 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(); + // blocks unit quit + 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(); } } } @@ -181,9 +196,7 @@ class GtkSupport { public static void shutdownGui() { - if (CREATE_EVENT_LOOP) { - Gtk.INSTANCE.gtk_main_quit(); - } + Gtk.INSTANCE.gtk_main_quit(); started = false; gtkDispatchThread.interrupt();