Added hack for opening external Blorb file
[projects/chimara/chimara.git] / libchimara / chimara-if.c
index cb0f425f012afd47c5382012ef765b560c901781..76edcf645c6252d07da503fb4b9895d099c70a73 100644 (file)
@@ -6,6 +6,7 @@
 #include <glib/gi18n-lib.h>
 #include "chimara-if.h"
 #include "chimara-glk.h"
+#include "chimara-glk-private.h"
 #include "chimara-marshallers.h"
 #include "init.h"
 
@@ -103,7 +104,9 @@ chimara_if_waiting(ChimaraGlk *glk)
        gchar *response = g_string_free(priv->response, FALSE);
        priv->response = g_string_new("");
 
+       gdk_threads_enter();
        g_signal_emit_by_name(glk, "command", priv->input, response);
+       gdk_threads_leave();
 
        g_free(priv->input);
        g_free(response);
@@ -122,6 +125,17 @@ chimara_if_stopped(ChimaraGlk *glk)
        priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
 }
 
+static void
+chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, guint keysym)
+{
+       CHIMARA_IF_USE_PRIVATE(glk, priv);
+       g_assert(priv->input == NULL);
+
+       gchar outbuf[6];
+       gint outbuflen = g_unichar_to_utf8(gdk_keyval_to_unicode(keysym), outbuf);
+       priv->input = g_strndup(outbuf, outbuflen);
+}
+
 static void
 chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, gchar *input)
 {
@@ -158,6 +172,7 @@ chimara_if_init(ChimaraIF *self)
        /* Connect to signals of ChimaraGlk parent */
        g_signal_connect(self, "stopped", G_CALLBACK(chimara_if_stopped), NULL);
        g_signal_connect(self, "waiting", G_CALLBACK(chimara_if_waiting), NULL);
+       g_signal_connect(self, "char-input", G_CALLBACK(chimara_if_char_input), NULL);
        g_signal_connect(self, "line-input", G_CALLBACK(chimara_if_line_input), NULL);
        g_signal_connect(self, "text-buffer-output", G_CALLBACK(chimara_if_text_buffer_output), NULL);
 }
@@ -280,17 +295,21 @@ chimara_if_class_init(ChimaraIFClass *klass)
        /**
         * ChimaraIF::command:
         * @self: The widget that received the signal
-        * @input: The command typed into the game
+        * @input: The command typed into the game, or %NULL
         * @response: The game's response to the command
         *
         * Emitted once for each input-response cycle of an interactive fiction
         * game. Note that games with nontraditional input systems (i.e. not all
         * taking place in the same text buffer window) may confuse this signal.
+        *
+        * It may happen that @input is %NULL, in which case @response is not due to
+        * a user command, but contains the text printed at the beginning of the
+        * game, up until the first prompt.
         */
        chimara_if_signals[COMMAND] = g_signal_new("command",
                G_OBJECT_CLASS_TYPE(klass), 0,
                G_STRUCT_OFFSET(ChimaraIFClass, command), NULL, NULL,
-               chimara_marshal_VOID__STRING_STRING,
+               _chimara_marshal_VOID__STRING_STRING,
                G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
 
        /* Properties */
@@ -642,7 +661,34 @@ chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
        GSList *ptr;
        for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
                argv[count] = ptr->data;
-
+               
+       /* Set the story name */
+       /* We peek into ChimaraGlk's private data here, because GObject has no
+       equivalent to "protected" */
+       CHIMARA_GLK_USE_PRIVATE(self, glk_priv);
+       glk_priv->story_name = g_path_get_basename(gamefile);
+       g_object_notify(G_OBJECT(self), "story-name");
+
+       /* Check if an external blorb file is present */
+       /* FIXME: hardcoded path */
+       if(format == CHIMARA_IF_FORMAT_Z5
+           || format == CHIMARA_IF_FORMAT_Z6
+           || format == CHIMARA_IF_FORMAT_Z8
+           || format == CHIMARA_IF_FORMAT_GLULX) {
+               gchar *path = g_path_get_dirname(gamefile);
+               gchar *scratch = g_path_get_basename(gamefile);
+               *(strrchr(scratch, '.')) = '\0';
+               gchar *blorbfile = g_strconcat(path, "/../Resources/", scratch, ".blb", NULL);
+               if(g_file_test(blorbfile, G_FILE_TEST_EXISTS)) {
+                       glk_priv->open_external_blorb = TRUE;
+                       glk_priv->external_blorb_pathname = blorbfile;
+               } else {
+                       g_free(blorbfile);
+               }
+               g_free(path);
+               g_free(scratch);
+       }
+       
        gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
        g_free(argv);
        if(terpnumstr)