From ebc849f9d0af5c74c3c2c2ed17c2a1ce14d84073 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 12 Feb 2016 14:24:19 +0100 Subject: [PATCH] Added back threads. They now happen ONLY in GtkSupport --- .../src/dorkbox/util/jna/linux/Gtk.java | 2 ++ .../dorkbox/util/jna/linux/GtkSupport.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/jna/linux/Gtk.java b/Dorkbox-Util/src/dorkbox/util/jna/linux/Gtk.java index 1b7ac40..c4d733b 100644 --- a/Dorkbox-Util/src/dorkbox/util/jna/linux/Gtk.java +++ b/Dorkbox-Util/src/dorkbox/util/jna/linux/Gtk.java @@ -83,6 +83,8 @@ public interface Gtk extends Library { void gtk_main_quit(); void gdk_threads_init(); + void gdk_threads_enter(); + void gdk_threads_leave(); Pointer gtk_menu_new(); Pointer gtk_menu_item_new(); diff --git a/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java b/Dorkbox-Util/src/dorkbox/util/jna/linux/GtkSupport.java index 0dc2e86..8e009b0 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 - /** Disables the GTK event loop, if you are already creating one in SWT/etc. */ - public static boolean DISABLE_EVENT_LOOP = false; + /** 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; static { boolean hasSupport = false; @@ -59,10 +59,15 @@ class GtkSupport { @Override public void run() { + final Gtk gtk = Gtk.INSTANCE; while (started) { try { final Runnable take = dispatchEvents.take(); + + gtk.gdk_threads_enter(); take.run(); + gtk.gdk_threads_leave(); + } catch (InterruptedException e) { e.printStackTrace(); } @@ -73,7 +78,7 @@ class GtkSupport { gtkDispatchThread.start(); - if (!DISABLE_EVENT_LOOP) { + 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() { @@ -82,15 +87,15 @@ class GtkSupport { void run() { Gtk instance = Gtk.INSTANCE; - // notify our main thread to continue - blockUntilStarted.countDown(); - - // 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(); + + // blocks unit quit instance.gtk_main(); } }; @@ -121,7 +126,7 @@ class GtkSupport { public static void shutdownGui() { - if (!DISABLE_EVENT_LOOP) { + if (CREATE_EVENT_LOOP) { Gtk.INSTANCE.gtk_main_quit(); }