Implement volume change notifications
[projects/chimara/chimara.git] / libchimara / chimara-if.c
index ee91bf9e57e275b61ac1632d83bdadde310911bd..af1179c49fd0db5219caed2d2bca7d7be8f80cc8 100644 (file)
@@ -14,7 +14,6 @@
  * 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
@@ -67,6 +66,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 +84,8 @@ enum {
        PROP_TYPO_CORRECTION,
        PROP_INTERPRETER_NUMBER,
        PROP_RANDOM_SEED,
-       PROP_RANDOM_SEED_SET
+       PROP_RANDOM_SEED_SET,
+       PROP_GRAPHICS_FILE
 };
 
 enum {
@@ -104,7 +105,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);
@@ -123,6 +126,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)
 {
@@ -143,7 +157,7 @@ chimara_if_init(ChimaraIF *self)
 {
        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;
@@ -153,12 +167,14 @@ chimara_if_init(ChimaraIF *self)
        priv->flags = CHIMARA_IF_TYPO_CORRECTION;
        priv->interpreter_number = CHIMARA_IF_ZMACHINE_DEFAULT;
        priv->random_seed_set = FALSE;
+       priv->graphics_file = NULL;
        priv->input = NULL;
        priv->response = g_string_new("");
 
        /* 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);
 }
@@ -203,6 +219,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);
     }
@@ -238,6 +258,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);
     }
@@ -246,6 +269,8 @@ 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);
 }
 
@@ -281,12 +306,16 @@ 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,
@@ -443,7 +472,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));
 }
@@ -583,7 +626,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:
@@ -632,6 +674,14 @@ chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
                        ;
        }
 
+       /* Game file and external blorb file */
+       args = g_slist_prepend(args, gamefile);
+       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);
@@ -643,7 +693,7 @@ 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" */