From 886ab2ca9847cf645bdc0e74f853c06511992261 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 22 Jan 2015 03:25:11 +0100 Subject: [PATCH] Fixed issue with GTK enter/leave thread and the main loop. Main loop is now handled by GtkSupport --- .../dorkbox/util/jna/linux/GtkSupport.java | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java b/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java index eccc1a5..4aa140c 100644 --- a/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java +++ b/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java @@ -1,10 +1,10 @@ package dorkbox.util.jna.linux; +import java.util.concurrent.CountDownLatch; public class GtkSupport { public static final boolean isSupported; - public static final boolean usesSwtMainLoop; static { if (Gtk.INSTANCE != null && AppIndicator.INSTANCE != null && Gobject.INSTANCE != null && GThread.INSTANCE != null) { @@ -18,20 +18,40 @@ public class GtkSupport { } } catch (Exception ignore) {} - // swt already init's gtk. Maybe this is the wrong way to go about this? + // swt already init's gtk. If we are using GTK, we need to make sure the event loop is runnign if (!hasSwt) { Gtk instance = Gtk.INSTANCE; instance.gtk_init(0, null); GThread.INSTANCE.g_thread_init(null); instance.gdk_threads_init(); - usesSwtMainLoop = false; - } else { - usesSwtMainLoop = true; + final CountDownLatch blockUntilStarted = new CountDownLatch(1); + + Thread gtkUpdateThread = new Thread() { + @Override + public void run() { + Gtk instance = Gtk.INSTANCE; + + // notify our main thread to continue + blockUntilStarted.countDown(); + instance.gdk_threads_enter(); + instance.gtk_main(); + // MUST leave as well! + instance.gdk_threads_leave(); + } + }; + gtkUpdateThread.setName("GTK Event Loop"); + gtkUpdateThread.start(); + + try { + // we CANNOT continue until the GTK thread has started! (ignored if SWT is used) + blockUntilStarted.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } else { isSupported = false; - usesSwtMainLoop = false; } }