Brought documentation up to date
[rodin/chimara.git] / libchimara / chimara-if.c
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <glib.h>
4 #include <glib-object.h>
5 #include <config.h>
6 #include <glib/gi18n-lib.h>
7 #include "chimara-if.h"
8 #include "chimara-glk.h"
9 #include "chimara-marshallers.h"
10 #include "init.h"
11
12 /**
13  * SECTION:chimara-if
14  * @short_description: Widget which plays an interactive fiction game
15  * @stability: Unstable
16  * @include: chimara/chimara-if.h
17  *
18  * The #ChimaraIF widget, given an interactive fiction game file to run, selects
19  * an appropriate interpreter plugin and runs it. Interpreter options are set by
20  * setting properties on the widget.
21  */
22
23 static gboolean supported_formats[CHIMARA_IF_NUM_FORMATS][CHIMARA_IF_NUM_INTERPRETERS] = {
24         /* Frotz Nitfol Glulxe Git */
25         { TRUE,  TRUE,  FALSE, FALSE }, /* Z5 */
26         { TRUE,  TRUE,  FALSE, FALSE }, /* Z6 */
27         { TRUE,  TRUE,  FALSE, FALSE }, /* Z8 */
28         { TRUE,  TRUE,  FALSE, FALSE }, /* Zblorb */
29         { FALSE, FALSE, TRUE,  TRUE  }, /* Glulx */
30         { FALSE, FALSE, TRUE,  TRUE  }  /* Gblorb */
31 };
32 static gchar *format_names[CHIMARA_IF_NUM_FORMATS] = {
33         N_("Z-code version 5"),
34         N_("Z-code version 6"),
35         N_("Z-code version 8"),
36         N_("Blorbed Z-code"),
37         N_("Glulx"),
38         N_("Blorbed Glulx")
39 };
40 static gchar *interpreter_names[CHIMARA_IF_NUM_INTERPRETERS] = {
41         N_("Frotz"), N_("Nitfol"), N_("Glulxe"), N_("Git")
42 };
43 static gchar *plugin_names[CHIMARA_IF_NUM_INTERPRETERS] = {
44         "frotz", "nitfol", "glulxe", "git"
45 };
46
47 typedef enum _ChimaraIFFlags {
48         CHIMARA_IF_PIRACY_MODE = 1 << 0,
49         CHIMARA_IF_TANDY_BIT = 1 << 1,
50         CHIMARA_IF_EXPAND_ABBREVIATIONS = 1 << 2,
51         CHIMARA_IF_IGNORE_ERRORS = 1 << 3,
52         CHIMARA_IF_TYPO_CORRECTION = 1 << 4
53 } ChimaraIFFlags;
54
55 typedef struct _ChimaraIFPrivate {
56         ChimaraIFInterpreter preferred_interpreter[CHIMARA_IF_NUM_FORMATS];
57         ChimaraIFFormat format;
58         ChimaraIFInterpreter interpreter;
59         ChimaraIFFlags flags;
60         ChimaraIFZmachineVersion interpreter_number;
61         gint random_seed;
62         gboolean random_seed_set;
63         /* Holding buffers for input and response */
64         gchar *input;
65         GString *response;
66 } ChimaraIFPrivate;
67
68 #define CHIMARA_IF_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), CHIMARA_TYPE_IF, ChimaraIFPrivate))
69 #define CHIMARA_IF_USE_PRIVATE(o, n) ChimaraIFPrivate *n = CHIMARA_IF_PRIVATE(o)
70
71 enum {
72         PROP_0,
73         PROP_PIRACY_MODE,
74         PROP_TANDY_BIT,
75         PROP_EXPAND_ABBREVIATIONS,
76         PROP_IGNORE_ERRORS,
77         PROP_TYPO_CORRECTION,
78         PROP_INTERPRETER_NUMBER,
79         PROP_RANDOM_SEED,
80         PROP_RANDOM_SEED_SET
81 };
82
83 enum {
84         COMMAND,
85         LAST_SIGNAL
86 };
87
88 static guint chimara_if_signals[LAST_SIGNAL] = { 0 };
89
90 G_DEFINE_TYPE(ChimaraIF, chimara_if, CHIMARA_TYPE_GLK);
91
92 static void
93 chimara_if_waiting(ChimaraGlk *glk)
94 {
95         CHIMARA_IF_USE_PRIVATE(glk, priv);
96
97         gchar *response = g_string_free(priv->response, FALSE);
98         priv->response = g_string_new("");
99
100         g_signal_emit_by_name(glk, "command", priv->input, response);
101
102         g_free(priv->input);
103         g_free(response);
104         priv->input = NULL;
105 }
106
107 static void
108 chimara_if_stopped(ChimaraGlk *glk)
109 {
110         CHIMARA_IF_USE_PRIVATE(glk, priv);
111
112         if(priv->input || priv->response->len > 0)
113                 chimara_if_waiting(glk); /* Send one last command signal */
114
115         priv->format = CHIMARA_IF_FORMAT_NONE;
116         priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
117 }
118
119 static void
120 chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, gchar *input)
121 {
122         CHIMARA_IF_USE_PRIVATE(glk, priv);
123         g_assert(priv->input == NULL);
124         priv->input = g_strdup(input);
125 }
126
127 static void
128 chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, gchar *output)
129 {
130         CHIMARA_IF_USE_PRIVATE(glk, priv);
131         g_string_append(priv->response, output);
132 }
133
134 static void
135 chimara_if_init(ChimaraIF *self)
136 {
137         CHIMARA_IF_USE_PRIVATE(self, priv);
138         priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z5] = CHIMARA_IF_INTERPRETER_FROTZ;
139         priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z6] = CHIMARA_IF_INTERPRETER_FROTZ;
140         priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z8] = CHIMARA_IF_INTERPRETER_FROTZ;
141         priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z_BLORB] = CHIMARA_IF_INTERPRETER_FROTZ;
142         priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX] = CHIMARA_IF_INTERPRETER_GLULXE;
143         priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX_BLORB] = CHIMARA_IF_INTERPRETER_GLULXE;
144         priv->format = CHIMARA_IF_FORMAT_NONE;
145         priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
146         priv->flags = CHIMARA_IF_TYPO_CORRECTION;
147         priv->interpreter_number = CHIMARA_IF_ZMACHINE_DEFAULT;
148         priv->random_seed_set = FALSE;
149         priv->input = NULL;
150         priv->response = g_string_new("");
151
152         /* Connect to signals of ChimaraGlk parent */
153         g_signal_connect(self, "stopped", G_CALLBACK(chimara_if_stopped), NULL);
154         g_signal_connect(self, "waiting", G_CALLBACK(chimara_if_waiting), NULL);
155         g_signal_connect(self, "line-input", G_CALLBACK(chimara_if_line_input), NULL);
156         g_signal_connect(self, "text-buffer-output", G_CALLBACK(chimara_if_text_buffer_output), NULL);
157 }
158
159 #define PROCESS_FLAG(flags, flag, val) (flags) = (val)? (flags) | (flag) : (flags) & ~(flag)
160
161 static void
162 chimara_if_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
163 {
164         CHIMARA_IF_USE_PRIVATE(object, priv);
165     switch(prop_id)
166     {
167         case PROP_PIRACY_MODE:
168                 PROCESS_FLAG(priv->flags, CHIMARA_IF_PIRACY_MODE, g_value_get_boolean(value));
169                 g_object_notify(object, "piracy-mode");
170                 break;
171         case PROP_TANDY_BIT:
172                 PROCESS_FLAG(priv->flags, CHIMARA_IF_TANDY_BIT, g_value_get_boolean(value));
173                 g_object_notify(object, "tandy-bit");
174                 break;
175         case PROP_EXPAND_ABBREVIATIONS:
176                 PROCESS_FLAG(priv->flags, CHIMARA_IF_EXPAND_ABBREVIATIONS, g_value_get_boolean(value));
177                 g_object_notify(object, "expand-abbreviations");
178                 break;
179         case PROP_IGNORE_ERRORS:
180                 PROCESS_FLAG(priv->flags, CHIMARA_IF_IGNORE_ERRORS, g_value_get_boolean(value));
181                 g_object_notify(object, "ignore-errors");
182                 break;
183         case PROP_TYPO_CORRECTION:
184                 PROCESS_FLAG(priv->flags, CHIMARA_IF_TYPO_CORRECTION, g_value_get_boolean(value));
185                 g_object_notify(object, "typo-correction");
186                 break;
187         case PROP_INTERPRETER_NUMBER:
188                 priv->interpreter_number = g_value_get_uint(value);
189                 g_object_notify(object, "interpreter-number");
190                 break;
191         case PROP_RANDOM_SEED:
192                 priv->random_seed = g_value_get_int(value);
193                 g_object_notify(object, "random-seed");
194                 break;
195         case PROP_RANDOM_SEED_SET:
196                 priv->random_seed_set = g_value_get_boolean(value);
197                 g_object_notify(object, "random-seed-set");
198                 break;
199         default:
200             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
201     }
202 }
203
204 static void
205 chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
206 {
207         CHIMARA_IF_USE_PRIVATE(object, priv);
208     switch(prop_id)
209     {
210         case PROP_PIRACY_MODE:
211                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_PIRACY_MODE);
212                 break;
213         case PROP_TANDY_BIT:
214                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TANDY_BIT);
215                 break;
216         case PROP_EXPAND_ABBREVIATIONS:
217                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS);
218                 break;
219         case PROP_IGNORE_ERRORS:
220                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_IGNORE_ERRORS);
221                 break;
222         case PROP_TYPO_CORRECTION:
223                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TYPO_CORRECTION);
224                 break;
225         case PROP_INTERPRETER_NUMBER:
226                 g_value_set_uint(value, priv->interpreter_number);
227                 break;
228         case PROP_RANDOM_SEED:
229                 g_value_set_int(value, priv->random_seed);
230                 break;
231         case PROP_RANDOM_SEED_SET:
232                 g_value_set_boolean(value, priv->random_seed_set);
233                 break;
234         default:
235             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
236     }
237 }
238
239 static void
240 chimara_if_finalize(GObject *object)
241 {
242     G_OBJECT_CLASS(chimara_if_parent_class)->finalize(object);
243 }
244
245 static void
246 chimara_if_command(ChimaraIF *self, gchar *input, gchar *response)
247 {
248         /* Default signal handler */
249 }
250
251 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
252 #ifndef G_PARAM_STATIC_STRINGS
253
254 /* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
255 #if GTK_CHECK_VERSION(2,8,0)
256 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
257 #else
258 #define G_PARAM_STATIC_STRINGS (0)
259 #endif
260
261 #endif
262
263 static void
264 chimara_if_class_init(ChimaraIFClass *klass)
265 {
266         /* Override methods of parent classes */
267         GObjectClass *object_class = G_OBJECT_CLASS(klass);
268         object_class->set_property = chimara_if_set_property;
269         object_class->get_property = chimara_if_get_property;
270         object_class->finalize = chimara_if_finalize;
271
272         /* Signals */
273         klass->command = chimara_if_command;
274         /**
275          * ChimaraIF::command:
276          * @self: The widget that received the signal
277          * @input: The command typed into the game
278          * @response: The game's response to the command
279          *
280          * Emitted once for each input-response cycle of an interactive fiction
281          * game. Note that games with nontraditional input systems (i.e. not all
282          * taking place in the same text buffer window) may confuse this signal.
283          */
284         chimara_if_signals[COMMAND] = g_signal_new("command",
285                 G_OBJECT_CLASS_TYPE(klass), 0,
286                 G_STRUCT_OFFSET(ChimaraIFClass, command), NULL, NULL,
287                 chimara_marshal_VOID__STRING_STRING,
288                 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
289
290         /* Properties */
291         /**
292          * ChimaraIF:piracy-mode:
293          *
294          * The Z-machine specification defines a facility for games to ask the
295          * interpreter they are running on whether this copy of the game is pirated.
296          * How the interpreter is supposed to magically determine that it is running
297          * pirate software is unclear, and so the majority of games and interpreters
298          * ignore this feature. Set this property to %TRUE if you want the
299          * interpreter to pretend it has detected a pirated game.
300          *
301          * Only affects Z-machine interpreters.
302          */
303         g_object_class_install_property(object_class, PROP_PIRACY_MODE,
304                 g_param_spec_boolean("piracy-mode", _("Piracy mode"),
305                 _("Pretend the game is pirated"), FALSE,
306                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
307         /**
308          * ChimaraIF:tandy-bit:
309          *
310          * Some early Infocom games were sold by the Tandy Corporation. Setting this
311          * property to %TRUE changes the wording of some Version 3 Infocom games
312          * slightly, so as to be less offensive. See <ulink
313          * url="http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html">
314          * http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html</ulink>.
315          *
316          * Only affects Z-machine interpreters.
317          */
318         g_object_class_install_property(object_class, PROP_TANDY_BIT,
319                 g_param_spec_boolean("tandy-bit", _("Tandy bit"),
320                 _("Censor certain Infocom games"), FALSE,
321                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
322         /**
323          * ChimaraIF:expand-abbreviations:
324          *
325          * Most Z-machine games, in particular ones compiled with the Inform
326          * library, support the following one-letter abbreviations:
327          * <simplelist>
328          * <member>D &mdash; Down</member>
329          * <member>E &mdash; East</member>
330          * <member>G &mdash; aGain</member>
331          * <member>I &mdash; Inventory</member>
332          * <member>L &mdash; Look</member>
333          * <member>N &mdash; North</member>
334          * <member>O &mdash; Oops</member>
335          * <member>Q &mdash; Quit</member>
336          * <member>S &mdash; South</member>
337          * <member>U &mdash; Up</member>
338          * <member>W &mdash; West</member>
339          * <member>X &mdash; eXamine</member>
340          * <member>Y &mdash; Yes</member>
341          * <member>Z &mdash; wait (ZZZZ...)</member>
342          * </simplelist>
343          * Some early Infocom games might not recognize these abbreviations. Setting
344          * this property to %TRUE will cause the interpreter to expand the
345          * abbreviations to the full words before passing the commands on to the
346          * game. Frotz only expands G, X, and Z; Nitfol expands all of the above
347          * plus the following nonstandard ones:
348          * <simplelist>
349          * <member>C &mdash; Close</member>
350          * <member>K &mdash; attacK</member>
351          * <member>P &mdash; oPen</member>
352          * <member>R &mdash; dRop</member>
353          * <member>T &mdash; Take</member>
354          * </simplelist>
355          *
356          * Only affects Z-machine interpreters. Behaves differently on Frotz and
357          * Nitfol.
358          */
359         g_object_class_install_property(object_class, PROP_EXPAND_ABBREVIATIONS,
360                 g_param_spec_boolean("expand-abbreviations", _("Expand abbreviations"),
361                 _("Expand abbreviations such as X for EXAMINE"), FALSE,
362                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
363         /**
364          * ChimaraIF:ignore-errors:
365          *
366          * Setting this property to %TRUE will cause the interpreter to ignore
367          * certain Z-machine runtime errors. Frotz will ignore any fatal errors.
368          * Nitfol by default warns about any shady behavior, and this property will
369          * turn those warnings off.
370          *
371          * Only affects Z-machine interpreters. Behaves differently on Frotz and
372          * Nitfol.
373          */
374         g_object_class_install_property(object_class, PROP_IGNORE_ERRORS,
375                 g_param_spec_boolean("ignore-errors", _("Ignore errors"),
376                 _("Do not warn the user about Z-machine errors"), FALSE,
377                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
378         /**
379          * ChimaraIF:typo-correction:
380          *
381          * Nitfol has an automatic typo-correction facility, where it searches the
382          * game dictionary for words which differ by one letter from any unknown
383          * input words. Set this property to %FALSE to turn this feature off.
384          *
385          * Only affects Nitfol.
386          */
387         g_object_class_install_property(object_class, PROP_TYPO_CORRECTION,
388                 g_param_spec_boolean("typo-correction", _("Typo correction"),
389                 _("Try to remedy typos if the interpreter supports it"), TRUE,
390                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
391         /**
392          * ChimaraIF:interpreter-number:
393          *
394          * Infocom gave each port of their interpreter a different number. The Frotz
395          * and Nitfol plugins can emulate any of these platforms. Some games behave
396          * slightly differently depending on what platform they are on. Set this
397          * property to a #ChimaraIFZmachineVersion value to emulate a certain
398          * platform.
399          *
400          * Note that Nitfol pretends to be an Apple IIe by default.
401          *
402          * Only affects Z-machine interpreters.
403          */
404         g_object_class_install_property(object_class, PROP_INTERPRETER_NUMBER,
405                 g_param_spec_uint("interpreter-number", _("Interpreter number"),
406                 _("Platform the Z-machine should pretend it is running on"),
407                 CHIMARA_IF_ZMACHINE_DEFAULT, CHIMARA_IF_ZMACHINE_MAXVAL, CHIMARA_IF_ZMACHINE_DEFAULT,
408                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
409         /**
410          * ChimaraIF:random-seed:
411          *
412          * If the #ChimaraIF:random-seed-set property is %TRUE, then the interpreter
413          * will use the value of this property as a seed for the random number
414          * generator. Use this feature to duplicate sequences of random numbers
415          * for testing games.
416          *
417          * Note that the value -1 is a valid random number seed for
418          * Nitfol, whereas it will cause Frotz to pick an arbitrary seed even when
419          * #ChimaraIF:random-seed-set is %TRUE.
420          *
421          * Only affects Z-machine interpreters. Behaves slightly differently on
422          * Frotz and Nitfol.
423          */
424         g_object_class_install_property(object_class, PROP_RANDOM_SEED,
425                 g_param_spec_int("random-seed", _("Random seed"),
426                 _("Seed for the random number generator"), G_MININT, G_MAXINT, 0,
427                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
428         /**
429          * ChimaraIF:random-seed-set:
430          *
431          * Whether to use or ignore the #ChimaraIF:random-seed property.
432          *
433          * Only affects Z-machine interpreters.
434          */
435         g_object_class_install_property(object_class, PROP_RANDOM_SEED_SET,
436                 g_param_spec_boolean("random-seed-set", _("Random seed set"),
437                 _("Whether the seed for the random number generator should be set manually"), FALSE,
438                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
439
440         /* Private data */
441         g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
442 }
443
444 /* PUBLIC FUNCTIONS */
445
446 /**
447  * chimara_if_new:
448  *
449  * Creates and initializes a new #ChimaraIF widget.
450  *
451  * Return value: a #ChimaraIF widget, with a floating reference.
452  */
453 GtkWidget *
454 chimara_if_new(void)
455 {
456         /* This is a library entry point; initialize the library */
457         chimara_init();
458     return GTK_WIDGET(g_object_new(CHIMARA_TYPE_IF, NULL));
459 }
460
461 /**
462  * chimara_if_set_preferred_interpreter:
463  * @self: A #ChimaraIF widget.
464  * @format: The game format to set the preferred interpreter plugin for.
465  * @interpreter: The preferred interpreter plugin for @format.
466  *
467  * The function chimara_if_run_game() picks an appropriate interpreter for the
468  * type of game file it is given. This function sets which interpreter is picked
469  * for a certain game file format. Most formats, notably the Z-machine, have
470  * had many different interpreters written for them over the years, all with
471  * slightly different quirks and capabilities, so there is plenty of choice.
472  */
473 void
474 chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter)
475 {
476         g_return_if_fail(self && CHIMARA_IS_IF(self));
477         g_return_if_fail(format < CHIMARA_IF_NUM_FORMATS);
478         g_return_if_fail(interpreter < CHIMARA_IF_NUM_INTERPRETERS);
479
480         CHIMARA_IF_USE_PRIVATE(self, priv);
481
482         if(supported_formats[format][interpreter])
483                 priv->preferred_interpreter[format] = interpreter;
484         else
485                 g_warning("Format '%s' is not supported by interpreter '%s'", format_names[format], interpreter_names[interpreter]);
486 }
487
488 /**
489  * chimara_if_get_preferred_interpreter:
490  * @self: A #ChimaraIF widget.
491  * @format: The game format to query the preferred interpreter plugin for.
492  *
493  * Looks up the preferred interpreter for the game file format @format. See
494  * chimara_if_set_preferred_interpreter().
495  *
496  * Returns: a #ChimaraIFInterpreter value representing the preferred interpreter
497  * plugin for @format.
498  */
499 ChimaraIFInterpreter
500 chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
501 {
502         g_return_val_if_fail(self && CHIMARA_IS_IF(self), -1);
503         g_return_val_if_fail(format < CHIMARA_IF_NUM_FORMATS, -1);
504         CHIMARA_IF_USE_PRIVATE(self, priv);
505         return priv->preferred_interpreter[format];
506 }
507
508 /**
509  * chimara_if_run_game:
510  * @self: A #ChimaraIF widget.
511  * @gamefile: Path to an interactive fiction game file.
512  * @error: Return location for an error, or %NULL.
513  *
514  * Autodetects the type of a game file and runs it using an appropriate
515  * interpreter plugin. If there is more than one interpreter that supports the
516  * file format, the preferred one will be picked, according to
517  * chimara_if_set_preferred_interpreter().
518  *
519  * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
520  * case @error is set.
521  */
522 gboolean
523 chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
524 {
525         g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE);
526         g_return_val_if_fail(gamefile, FALSE);
527
528         CHIMARA_IF_USE_PRIVATE(self, priv);
529
530         /* Find out what format the game is */
531         /* TODO: Look inside the file instead of just looking at the extension */
532         ChimaraIFFormat format = CHIMARA_IF_FORMAT_Z5;
533         if(g_str_has_suffix(gamefile, ".z5"))
534                 format = CHIMARA_IF_FORMAT_Z5;
535         else if(g_str_has_suffix(gamefile, ".z6"))
536                 format = CHIMARA_IF_FORMAT_Z6;
537         else if(g_str_has_suffix(gamefile, ".z8"))
538                 format = CHIMARA_IF_FORMAT_Z8;
539         else if(g_str_has_suffix(gamefile, ".zlb") || g_str_has_suffix(gamefile, ".zblorb"))
540                 format = CHIMARA_IF_FORMAT_Z_BLORB;
541         else if(g_str_has_suffix(gamefile, ".ulx"))
542                 format = CHIMARA_IF_FORMAT_GLULX;
543         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"))
544                 format = CHIMARA_IF_FORMAT_GLULX_BLORB;
545
546         /* Now decide what interpreter to use */
547         ChimaraIFInterpreter interpreter = priv->preferred_interpreter[format];
548         gchar *pluginfile = g_strconcat(plugin_names[interpreter], "." G_MODULE_SUFFIX, NULL);
549
550         gchar *pluginpath;
551 #ifdef DEBUG
552 #ifndef LT_OBJDIR
553 #define LT_OBJDIR ".libs" /* Pre-2.2 libtool, so take a wild guess */
554 #endif /* LT_OBJDIR */
555         /* If there is a plugin in the source tree, use that */
556         pluginpath = g_build_filename(PLUGINSOURCEDIR, plugin_names[interpreter], LT_OBJDIR, pluginfile, NULL);
557         if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
558         {
559                 g_free(pluginpath);
560 #endif /* DEBUG */
561                 pluginpath = g_build_filename(PLUGINDIR, pluginfile, NULL);
562                 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
563                 {
564                         g_free(pluginpath);
565                         g_free(pluginfile);
566                         g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_NOT_FOUND, _("No appropriate %s interpreter plugin was found"), interpreter_names[interpreter]);
567                         return FALSE;
568                 }
569 #ifdef DEBUG
570         }
571 #endif
572         g_free(pluginfile);
573
574         /* Decide what arguments to pass to the interpreters; currently only the
575         Z-machine interpreters accept command line arguments other than the game */
576         GSList *args = NULL;
577         gchar *terpnumstr = NULL, *randomstr = NULL;
578         args = g_slist_prepend(args, pluginpath);
579         args = g_slist_prepend(args, gamefile);
580         switch(interpreter)
581         {
582                 case CHIMARA_IF_INTERPRETER_FROTZ:
583                         if(priv->flags & CHIMARA_IF_PIRACY_MODE)
584                                 args = g_slist_prepend(args, "-P");
585                         if(priv->flags & CHIMARA_IF_TANDY_BIT)
586                                 args = g_slist_prepend(args, "-t");
587                         if(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS)
588                                 args = g_slist_prepend(args, "-x");
589                         if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
590                                 args = g_slist_prepend(args, "-i");
591                         if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
592                         {
593                                 terpnumstr = g_strdup_printf("-I%u", priv->interpreter_number);
594                                 args = g_slist_prepend(args, terpnumstr);
595                         }
596                         if(priv->random_seed_set)
597                         {
598                                 randomstr = g_strdup_printf("-s%d", priv->random_seed);
599                                 args = g_slist_prepend(args, randomstr);
600                         }
601                         break;
602                 case CHIMARA_IF_INTERPRETER_NITFOL:
603                         if(priv->flags & CHIMARA_IF_PIRACY_MODE)
604                                 args = g_slist_prepend(args, "-pirate");
605                         if(priv->flags & CHIMARA_IF_TANDY_BIT)
606                                 args = g_slist_prepend(args, "-tandy");
607                         if(!(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS))
608                                 args = g_slist_prepend(args, "-no-expand");
609                         if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
610                                 args = g_slist_prepend(args, "-ignore");
611                         if(!(priv->flags & CHIMARA_IF_TYPO_CORRECTION))
612                                 args = g_slist_prepend(args, "-no-spell");
613                         if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
614                         {
615                                 terpnumstr = g_strdup_printf("-terpnum%u", priv->interpreter_number);
616                                 args = g_slist_prepend(args, terpnumstr);
617                         }
618                         if(priv->random_seed_set)
619                         {
620                                 randomstr = g_strdup_printf("-random%d", priv->random_seed);
621                                 args = g_slist_prepend(args, randomstr);
622                         }
623                         break;
624                 default:
625                         ;
626         }
627
628         /* Allocate argv to hold the arguments */
629         int argc = g_slist_length(args);
630         args = g_slist_prepend(args, NULL);
631         char **argv = g_new0(char *, argc + 1);
632
633         /* Fill argv */
634         args = g_slist_reverse(args);
635         int count;
636         GSList *ptr;
637         for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
638                 argv[count] = ptr->data;
639
640         gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
641         g_free(argv);
642         if(terpnumstr)
643                 g_free(terpnumstr);
644         if(randomstr)
645                 g_free(randomstr);
646         g_free(pluginpath);
647
648         /* Set current format and interpreter if plugin was started successfully */
649         if(retval)
650         {
651                 priv->format = format;
652                 priv->interpreter = interpreter;
653         }
654         return retval;
655 }
656
657 /**
658  * chimara_if_get_format:
659  * @self: A #ChimaraIF widget.
660  *
661  * Returns the file format of the currently running game.
662  *
663  * Returns: a #ChimaraIFFormat constant.
664  */
665 ChimaraIFFormat
666 chimara_if_get_format(ChimaraIF *self)
667 {
668         g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
669         CHIMARA_IF_USE_PRIVATE(self, priv);
670         return priv->format;
671 }
672
673 /**
674  * chimara_if_get_interpreter:
675  * @self: A #ChimaraIF widget.
676  *
677  * Returns the interpreter plugin currently running.
678  *
679  * Returns: a #ChimaraIFInterpreter constant.
680  */
681 ChimaraIFInterpreter
682 chimara_if_get_interpreter(ChimaraIF *self)
683 {
684         g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
685         CHIMARA_IF_USE_PRIVATE(self, priv);
686         return priv->interpreter;
687 }