Change I/O signals to pass a unique window ID
[projects/chimara/chimara.git] / libchimara / chimara-if.c
index 76edcf645c6252d07da503fb4b9895d099c70a73..2fb74c85e3477e5f845730abc5985904ec978ba2 100644 (file)
 #include "chimara-marshallers.h"
 #include "init.h"
 
+#ifndef PLUGINDIR
+#define PLUGINDIR "."
+#endif
+
 /**
  * SECTION:chimara-if
  * @short_description: Widget which plays an interactive fiction game
  * @stability: Unstable
- * @include: libchimara/chimara-if.h
  *
  * The #ChimaraIF widget, given an interactive fiction game file to run, selects
  * an appropriate interpreter plugin and runs it. Interpreter options are set by
  */
 
 static gboolean supported_formats[CHIMARA_IF_NUM_FORMATS][CHIMARA_IF_NUM_INTERPRETERS] = {
-       /* Frotz Nitfol Glulxe Git */
-       { TRUE,  TRUE,  FALSE, FALSE }, /* Z5 */
-       { TRUE,  TRUE,  FALSE, FALSE }, /* Z6 */
-       { TRUE,  TRUE,  FALSE, FALSE }, /* Z8 */
-       { TRUE,  TRUE,  FALSE, FALSE }, /* Zblorb */
-       { FALSE, FALSE, TRUE,  TRUE  }, /* Glulx */
-       { FALSE, FALSE, TRUE,  TRUE  }  /* Gblorb */
+       /* Frotz Nitfol Glulxe Git    Bocfel */
+       { TRUE,  TRUE,  FALSE, FALSE, TRUE  }, /* Z5 */
+       { TRUE,  TRUE,  FALSE, FALSE, TRUE  }, /* Z6 */
+       { TRUE,  TRUE,  FALSE, FALSE, TRUE  }, /* Z8 */
+       { TRUE,  TRUE,  FALSE, FALSE, TRUE  }, /* Zblorb */
+       { FALSE, FALSE, TRUE,  TRUE,  FALSE }, /* Glulx */
+       { FALSE, FALSE, TRUE,  TRUE,  FALSE }  /* Gblorb */
 };
 static gchar *format_names[CHIMARA_IF_NUM_FORMATS] = {
        N_("Z-code version 5"),
@@ -45,10 +48,10 @@ static gchar *format_names[CHIMARA_IF_NUM_FORMATS] = {
        N_("Blorbed Glulx")
 };
 static gchar *interpreter_names[CHIMARA_IF_NUM_INTERPRETERS] = {
-       N_("Frotz"), N_("Nitfol"), N_("Glulxe"), N_("Git")
+       N_("Frotz"), N_("Nitfol"), N_("Glulxe"), N_("Git"), N_("Bocfel")
 };
 static gchar *plugin_names[CHIMARA_IF_NUM_INTERPRETERS] = {
-       "frotz", "nitfol", "glulxe", "git"
+       "frotz", "nitfol", "glulxe", "git", "bocfel"
 };
 
 typedef enum _ChimaraIFFlags {
@@ -67,6 +70,7 @@ typedef struct _ChimaraIFPrivate {
        ChimaraIFZmachineVersion interpreter_number;
        gint random_seed;
        gboolean random_seed_set;
+       gchar *graphics_file;
        /* Holding buffers for input and response */
        gchar *input;
        GString *response;
@@ -84,7 +88,8 @@ enum {
        PROP_TYPO_CORRECTION,
        PROP_INTERPRETER_NUMBER,
        PROP_RANDOM_SEED,
-       PROP_RANDOM_SEED_SET
+       PROP_RANDOM_SEED_SET,
+       PROP_GRAPHICS_FILE
 };
 
 enum {
@@ -126,7 +131,7 @@ chimara_if_stopped(ChimaraGlk *glk)
 }
 
 static void
-chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, guint keysym)
+chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, char *string_id, unsigned keysym)
 {
        CHIMARA_IF_USE_PRIVATE(glk, priv);
        g_assert(priv->input == NULL);
@@ -137,7 +142,7 @@ chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, guint keysym)
 }
 
 static void
-chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, gchar *input)
+chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, char *string_id, char *input)
 {
        CHIMARA_IF_USE_PRIVATE(glk, priv);
        g_assert(priv->input == NULL);
@@ -145,7 +150,7 @@ chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, gchar *input)
 }
 
 static void
-chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, gchar *output)
+chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, char *string_id, char *output)
 {
        CHIMARA_IF_USE_PRIVATE(glk, priv);
        g_string_append(priv->response, output);
@@ -154,9 +159,11 @@ chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, gchar *output)
 static void
 chimara_if_init(ChimaraIF *self)
 {
+       chimara_init(); /* This is a library entry point */
+
        CHIMARA_IF_USE_PRIVATE(self, priv);
        priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z5] = CHIMARA_IF_INTERPRETER_FROTZ;
-       priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z6] = CHIMARA_IF_INTERPRETER_FROTZ;
+       priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z6] = CHIMARA_IF_INTERPRETER_NITFOL;
        priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z8] = CHIMARA_IF_INTERPRETER_FROTZ;
        priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z_BLORB] = CHIMARA_IF_INTERPRETER_FROTZ;
        priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX] = CHIMARA_IF_INTERPRETER_GLULXE;
@@ -165,8 +172,6 @@ chimara_if_init(ChimaraIF *self)
        priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
        priv->flags = CHIMARA_IF_TYPO_CORRECTION;
        priv->interpreter_number = CHIMARA_IF_ZMACHINE_DEFAULT;
-       priv->random_seed_set = FALSE;
-       priv->input = NULL;
        priv->response = g_string_new("");
 
        /* Connect to signals of ChimaraGlk parent */
@@ -217,6 +222,10 @@ chimara_if_set_property(GObject *object, guint prop_id, const GValue *value, GPa
                priv->random_seed_set = g_value_get_boolean(value);
                g_object_notify(object, "random-seed-set");
                break;
+               case PROP_GRAPHICS_FILE:
+                       priv->graphics_file = g_strdup(g_value_get_string(value));
+                       g_object_notify(object, "graphics-file");
+                       break;
         default:
             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     }
@@ -252,6 +261,9 @@ chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpe
        case PROP_RANDOM_SEED_SET:
                g_value_set_boolean(value, priv->random_seed_set);
                break;
+               case PROP_GRAPHICS_FILE:
+                       g_value_set_string(value, priv->graphics_file);
+                       break;
         default:
             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
     }
@@ -260,27 +272,11 @@ chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpe
 static void
 chimara_if_finalize(GObject *object)
 {
+       CHIMARA_IF_USE_PRIVATE(object, priv);
+       g_free(priv->graphics_file);
     G_OBJECT_CLASS(chimara_if_parent_class)->finalize(object);
 }
 
-static void
-chimara_if_command(ChimaraIF *self, gchar *input, gchar *response)
-{
-       /* Default signal handler */
-}
-
-/* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
-#ifndef G_PARAM_STATIC_STRINGS
-
-/* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
-#if GTK_CHECK_VERSION(2,8,0)
-#define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
-#else
-#define G_PARAM_STATIC_STRINGS (0)
-#endif
-
-#endif
-
 static void
 chimara_if_class_init(ChimaraIFClass *klass)
 {
@@ -291,7 +287,6 @@ chimara_if_class_init(ChimaraIFClass *klass)
        object_class->finalize = chimara_if_finalize;
 
        /* Signals */
-       klass->command = chimara_if_command;
        /**
         * ChimaraIF::command:
         * @self: The widget that received the signal
@@ -461,7 +456,21 @@ chimara_if_class_init(ChimaraIFClass *klass)
                g_param_spec_boolean("random-seed-set", _("Random seed set"),
                _("Whether the seed for the random number generator should be set manually"), FALSE,
                G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
-
+       /**
+        * ChimaraIF:graphics-file:
+        *
+        * Some Z-machine interpreters accept an extra argument that indicates a
+        * separate Blorb file containing graphics and sound resources. The
+        * interpreter will check if the file specified in this property really
+        * exists, and if so, use it as a resource file. If this property is set to 
+        * %NULL, the interpreter will not look for an extra file.
+        *
+        * Only affects Frotz and Nitfol.
+        */
+       g_object_class_install_property(object_class, PROP_GRAPHICS_FILE,
+           g_param_spec_string("graphics-file", _("Graphics file"),
+           _("Location in which to look for a separate graphics Blorb file"), NULL,
+           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
        /* Private data */
        g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
 }
@@ -533,7 +542,7 @@ chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
 /**
  * chimara_if_run_game:
  * @self: A #ChimaraIF widget.
- * @gamefile: Path to an interactive fiction game file.
+ * @game_path: Path to an interactive fiction game file.
  * @error: Return location for an error, or %NULL.
  *
  * Autodetects the type of a game file and runs it using an appropriate
@@ -545,27 +554,28 @@ chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
  * case @error is set.
  */
 gboolean
-chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
+chimara_if_run_game(ChimaraIF *self, const char *game_path, GError **error)
 {
        g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE);
-       g_return_val_if_fail(gamefile, FALSE);
+       g_return_val_if_fail(game_path, FALSE);
+       g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
 
        CHIMARA_IF_USE_PRIVATE(self, priv);
 
        /* Find out what format the game is */
        /* TODO: Look inside the file instead of just looking at the extension */
        ChimaraIFFormat format = CHIMARA_IF_FORMAT_Z5;
-       if(g_str_has_suffix(gamefile, ".z5"))
+       if(g_str_has_suffix(game_path, ".z5"))
                format = CHIMARA_IF_FORMAT_Z5;
-       else if(g_str_has_suffix(gamefile, ".z6"))
+       else if(g_str_has_suffix(game_path, ".z6"))
                format = CHIMARA_IF_FORMAT_Z6;
-       else if(g_str_has_suffix(gamefile, ".z8"))
+       else if(g_str_has_suffix(game_path, ".z8"))
                format = CHIMARA_IF_FORMAT_Z8;
-       else if(g_str_has_suffix(gamefile, ".zlb") || g_str_has_suffix(gamefile, ".zblorb"))
+       else if(g_str_has_suffix(game_path, ".zlb") || g_str_has_suffix(game_path, ".zblorb"))
                format = CHIMARA_IF_FORMAT_Z_BLORB;
-       else if(g_str_has_suffix(gamefile, ".ulx"))
+       else if(g_str_has_suffix(game_path, ".ulx"))
                format = CHIMARA_IF_FORMAT_GLULX;
-       else if(g_str_has_suffix(gamefile, ".blb") || g_str_has_suffix(gamefile, ".blorb") || g_str_has_suffix(gamefile, ".glb") || g_str_has_suffix(gamefile, ".gblorb"))
+       else if(g_str_has_suffix(game_path, ".blb") || g_str_has_suffix(game_path, ".blorb") || g_str_has_suffix(game_path, ".glb") || g_str_has_suffix(game_path, ".gblorb"))
                format = CHIMARA_IF_FORMAT_GLULX_BLORB;
 
        /* Now decide what interpreter to use */
@@ -601,7 +611,6 @@ chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
        GSList *args = NULL;
        gchar *terpnumstr = NULL, *randomstr = NULL;
        args = g_slist_prepend(args, pluginpath);
-       args = g_slist_prepend(args, gamefile);
        switch(interpreter)
        {
                case CHIMARA_IF_INTERPRETER_FROTZ:
@@ -650,6 +659,14 @@ chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
                        ;
        }
 
+       /* Game file and external blorb file */
+       args = g_slist_prepend(args, (gpointer)game_path);
+       if(priv->graphics_file
+               && (interpreter == CHIMARA_IF_INTERPRETER_FROTZ || interpreter == CHIMARA_IF_INTERPRETER_NITFOL)
+           && g_file_test(priv->graphics_file, G_FILE_TEST_EXISTS)) {
+               args = g_slist_prepend(args, priv->graphics_file);
+       }
+
        /* Allocate argv to hold the arguments */
        int argc = g_slist_length(args);
        args = g_slist_prepend(args, NULL);
@@ -660,37 +677,17 @@ chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
        int count;
        GSList *ptr;
        for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
-               argv[count] = ptr->data;
-               
+               argv[count] = g_strdup(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);
+       glk_priv->story_name = g_path_get_basename(game_path);
        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);
+       g_strfreev(argv);
        if(terpnumstr)
                g_free(terpnumstr);
        if(randomstr)
@@ -706,6 +703,31 @@ chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
        return retval;
 }
 
+/**
+ * chimara_if_run_game_file:
+ * @self: A #ChimaraIF widget.
+ * @game_file: a #GFile pointing to an interactive fiction game file.
+ * @error: Return location for an error, or %NULL.
+ *
+ * Autodetects the type of a game file and runs it using an appropriate
+ * interpreter plugin. See chimara_if_run_game() for more information.
+ *
+ * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
+ * case @error is set.
+ */
+gboolean
+chimara_if_run_game_file(ChimaraIF *self, GFile *game_file, GError **error)
+{
+       g_return_val_if_fail(self || CHIMARA_IS_IF(self), FALSE);
+       g_return_val_if_fail(game_file || G_IS_FILE(game_file), FALSE);
+       g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
+
+       char *path = g_file_get_path(game_file);
+       gboolean retval = chimara_if_run_game(self, path, error);
+       g_free(path);
+       return retval;
+}
+
 /**
  * chimara_if_get_format:
  * @self: A #ChimaraIF widget.