Added an initialization function, which must be called at every entry point of the...
authorfliep <fliep@ddfedd41-794f-dd11-ae45-00112f111e67>
Sun, 13 Sep 2009 18:26:02 +0000 (18:26 +0000)
committerfliep <fliep@ddfedd41-794f-dd11-ae45-00112f111e67>
Sun, 13 Sep 2009 18:26:02 +0000 (18:26 +0000)
Got multisession to work, fixing #19.

libchimara/Makefile.am
libchimara/chimara-glk.c
libchimara/init.c [new file with mode: 0644]
libchimara/init.h [new file with mode: 0644]
tests/main.c
tests/test-multisession.c

index 00d856c15bbde5ff68e8f49a2fdfb71576b8faf9..bbaf6a78cb9ca09610c6ba546a667692d4b9fa3d 100644 (file)
@@ -17,6 +17,7 @@ libchimara_la_SOURCES = \
        glk.c glk.h \
        glkstart.h \
        glkunix.c glkunix.h \
+       init.c init.h \
        input.c input.h \
        magic.c magic.h \
        mouse.c \
index f7887a94a7485a5e434e2a3e8345f0d0d3539c22..ae39f168c892e49639e39af559b9ebaf5cb0c3c8 100644 (file)
@@ -2,7 +2,8 @@
 
 #include <math.h>
 #include <gtk/gtk.h>
-#include <glib/gi18n.h>
+#include <config.h>
+#include <glib/gi18n-lib.h>
 #include <gmodule.h>
 #include <pango/pango.h>
 #include "chimara-glk.h"
@@ -12,6 +13,7 @@
 #include "window.h"
 #include "glkstart.h"
 #include "glkunix.h"
+#include "init.h"
 
 #define CHIMARA_GLK_MIN_WIDTH 0
 #define CHIMARA_GLK_MIN_HEIGHT 0
@@ -673,6 +675,9 @@ chimara_glk_class_init(ChimaraGlkClass *klass)
 GtkWidget *
 chimara_glk_new(void)
 {
+       /* This is a library entry point; initialize the library */
+       chimara_init();
+
     ChimaraGlk *self = CHIMARA_GLK(g_object_new(CHIMARA_TYPE_GLK, NULL));
     ChimaraGlkPrivate *priv = CHIMARA_GLK_PRIVATE(self);
     
@@ -1021,10 +1026,6 @@ chimara_glk_run(ChimaraGlk *glk, gchar *plugin, int argc, char *argv[], GError *
                /* Set the program name */
                startup->args.argv[0] = g_strdup(plugin);
     }
-
-       /* Initialize thread-private data */
-       extern GPrivate *glk_data_key;
-       glk_data_key = g_private_new(NULL);
        startup->glk_data = priv;
        
     /* Run in a separate thread */
diff --git a/libchimara/init.c b/libchimara/init.c
new file mode 100644 (file)
index 0000000..bfc67dd
--- /dev/null
@@ -0,0 +1,30 @@
+#include <config.h>
+#include <glib.h>
+#include <glib/gi18n-lib.h>
+
+static gboolean chimara_initialized = FALSE;
+
+/* This function is called at every entry point of the library, to set up
+threads and gettext. It is NOT called from Glk functions. */
+void
+chimara_init(void)
+{
+       if( G_UNLIKELY(!chimara_initialized) )
+       {
+               /* Setup gettext */
+               bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
+               bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
+               
+               /* Make sure threads have been initialized */
+               if(!g_thread_supported())
+                       g_error(_("In order to use the Chimara library, you must initialize"
+                               " the thread system by calling g_threads_init() and "
+                               "gdk_threads_init() BEFORE the initial call to gtk_init() in "
+                               "your main program."));
+               
+               /* Initialize thread-private data */
+               extern GPrivate *glk_data_key;
+               glk_data_key = g_private_new(NULL);
+       }
+}
+
diff --git a/libchimara/init.h b/libchimara/init.h
new file mode 100644 (file)
index 0000000..8ccf344
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef __CHIMARA_INIT_H__
+#define __CHIMARA_INIT_H__
+
+G_GNUC_INTERNAL void chimara_init(void);
+
+#endif
+
index 62adca94cdecd58e4c24028f2c83c47866a7234a..94755c8a6c2b0c33fbc74b270caa4cccd5e6a56c 100644 (file)
@@ -145,10 +145,7 @@ main(int argc, char *argv[])
 
        if( !g_thread_supported() )
                g_thread_init(NULL);
-
        gdk_threads_init();
-    
-       gtk_set_locale();
        gtk_init(&argc, &argv);
 
        create_window();
index 7f984c837ea136ed80086852d5387d3d149aa5f3..0bf7a3ed93dbd76bc2ac867d2257e2b302a0b40b 100644 (file)
@@ -17,25 +17,30 @@ on_stopped(ChimaraGlk *glk, const gchar *data)
     g_printerr("%s stopped!\n", data);
 }
 
+static gboolean
+on_delete_event(void)
+{
+       gtk_main_quit();
+       return TRUE;
+}
+
 int
 main(int argc, char **argv)
 {
        if( !g_thread_supported() )
                g_thread_init(NULL);
-
        gdk_threads_init();
-    
-       gtk_set_locale();
        gtk_init(&argc, &argv);
 
        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_widget_set_size_request(window, 800, 500);
+       g_signal_connect(window, "delete_event", G_CALLBACK(on_delete_event), NULL);
        
        GtkWidget *hpaned = gtk_hpaned_new();
        gtk_paned_set_position(GTK_PANED(hpaned), 400);
        
        GtkWidget *frotz = chimara_glk_new();
-       chimara_glk_set_default_font_string(CHIMARA_GLK(frotz), "Lucida Sans Unicode 12");
+       chimara_glk_set_default_font_string(CHIMARA_GLK(frotz), "Lucida Sans 12");
        chimara_glk_set_monospace_font_string(CHIMARA_GLK(frotz), "Lucida Console 12");
        g_signal_connect(frotz, "started", G_CALLBACK(on_started), "Frotz");
        g_signal_connect(frotz, "stopped", G_CALLBACK(on_stopped), "Frotz");
@@ -56,11 +61,11 @@ main(int argc, char **argv)
                return 1;
        if(!chimara_glk_run(CHIMARA_GLK(nitfol), "../interpreters/nitfol/.libs/nitfol.so", argc, argv, NULL))
                return 1;
-
+       
     gdk_threads_enter();
        gtk_main();
        gdk_threads_leave();
-
+       
        chimara_glk_stop(CHIMARA_GLK(frotz));
        chimara_glk_stop(CHIMARA_GLK(nitfol));