[Vala] Running Gtk.main() in a separate thread



Hi all!

I want to create a modular application with libpeas. One of it's modules is a GTK+ user interface. The 
problem is that the user interface should run in a separate thread, so the main application will continue 
it's execution. The threads should be managed in the extension code, not the main application's code.
So basically what I need is to start Gtk.main() in separate thread. Evan said yesterday that this would be a 
bad design, so if it is possible to make it better, please share your thoughts.
Here is a code example (excuse me for a long message, I could not make the example any smaller):

main.vala
-----
    using Peas;
    public class Test {
        private ExtensionSet extensions { get; set; }
        public Test() {
            var engine = Engine.get_default();
            string dir = Environment.get_current_dir();
            engine.add_search_path(dir, dir);
            extensions = new ExtensionSet(engine, typeof(Gui));
            extensions.extension_added.connect((info, extension) => {
                (extension as Gui).activate();
            });
            extensions.extension_removed.connect((info, extension) => {
                (extension as Gui).deactivate();
            });
            foreach (var plugin in engine.get_plugin_list()) {
                if (engine.try_load_plugin(plugin)) {
                    print("Sucessfully loaded " + plugin.get_name() + "\n");
                } else {
                    print("Error loading " + plugin.get_name() + "\n");
                }
            }
        }
    }
    public interface Gui : Object {
        public abstract void activate();
        public abstract void deactivate();
    }
-----
gtk.vala
-----
    using Peas;
    using Gtk;
    public class GtkGui : Object, Gui {
        public void activate() {
            unowned string[]? a = null;
            Gtk.init(ref a);
            var window = new GuiWindow();
            window.show();
            print("activating gtk...\n");
            // run this in a separate thread?
            Gtk.main();
        }
        public void deactivate() {
            if (Gtk.main_level() > 0)
                Gtk.main_quit();
            print("deactivating gtk...\n");
        }
    }
    public class GuiWindow : Window {
        public GuiWindow() {
            this.title = "GtkGui";
            this.destroy.connect(Gtk.main_quit);
        }
    }
    [ModuleInit]
    public void peas_register_types(TypeModule module) {
        var objmodule = module as ObjectModule;
        objmodule.register_extension_type(typeof(Gui), typeof(GtkGui));
    }
-----
launcher.vala
-----
    void main(string[] args) {
        new Test();
    }
-----
gtk.plugin
-----
    [Plugin]
    Module=gtk.so
    Name=GTK+ UI
-----
Makefile
-----
    all: main libgtk.so
    main: libmain.so launcher.vala
        valac -o main launcher.vala --vapidir . --pkg main -X -I. -X -L. -X -lmain
    libmain.so: main.vala
        valac -o libmain.so --library main -H main.h -X -shared -X -fPIC --pkg libpeas-1.0 main.vala
    libgtk.so: libmain.so gtk.vala
        valac -o libgtk.so --library gtk gtk.vala -X -shared -X -fPIC --vapidir . --thread --target-glib 2.32 
--pkg gtk+-3.0 --pkg libpeas-1.0 --pkg main -X -I. -X -L. -X -lmain       
    clean:
        rm -f main libmain.so main.h main.vapi libgtk.so gtk.vapi
    run: main libgtk.so
        LD_LIBRARY_PATH=. ./main
-----
Just save all 5 files under given filenames and execute `make run`.

Regards,
Dmitry


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]