3298c795d397ef84754da5ceecfbb79f15b5e5fc
[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                 break;
170         case PROP_TANDY_BIT:
171                 PROCESS_FLAG(priv->flags, CHIMARA_IF_TANDY_BIT, g_value_get_boolean(value));
172                 break;
173         case PROP_EXPAND_ABBREVIATIONS:
174                 PROCESS_FLAG(priv->flags, CHIMARA_IF_EXPAND_ABBREVIATIONS, g_value_get_boolean(value));
175                 break;
176         case PROP_IGNORE_ERRORS:
177                 PROCESS_FLAG(priv->flags, CHIMARA_IF_IGNORE_ERRORS, g_value_get_boolean(value));
178                 break;
179         case PROP_TYPO_CORRECTION:
180                 PROCESS_FLAG(priv->flags, CHIMARA_IF_TYPO_CORRECTION, g_value_get_boolean(value));
181                 break;
182         case PROP_INTERPRETER_NUMBER:
183                 priv->interpreter_number = g_value_get_uint(value);
184                 break;
185         case PROP_RANDOM_SEED:
186                 priv->random_seed = g_value_get_int(value);
187                 break;
188         case PROP_RANDOM_SEED_SET:
189                 priv->random_seed_set = g_value_get_boolean(value);
190                 break;
191         default:
192             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
193     }
194 }
195
196 static void
197 chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
198 {
199         CHIMARA_IF_USE_PRIVATE(object, priv);
200     switch(prop_id)
201     {
202         case PROP_PIRACY_MODE:
203                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_PIRACY_MODE);
204                 break;
205         case PROP_TANDY_BIT:
206                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TANDY_BIT);
207                 break;
208         case PROP_EXPAND_ABBREVIATIONS:
209                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS);
210                 break;
211         case PROP_IGNORE_ERRORS:
212                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_IGNORE_ERRORS);
213                 break;
214         case PROP_TYPO_CORRECTION:
215                 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TYPO_CORRECTION);
216                 break;
217         case PROP_INTERPRETER_NUMBER:
218                 g_value_set_uint(value, priv->interpreter_number);
219                 break;
220         case PROP_RANDOM_SEED:
221                 g_value_set_int(value, priv->random_seed);
222                 break;
223         case PROP_RANDOM_SEED_SET:
224                 g_value_set_boolean(value, priv->random_seed_set);
225                 break;
226         default:
227             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
228     }
229 }
230
231 static void
232 chimara_if_finalize(GObject *object)
233 {
234     G_OBJECT_CLASS(chimara_if_parent_class)->finalize(object);
235 }
236
237 static void
238 chimara_if_command(ChimaraIF *self, gchar *input, gchar *response)
239 {
240         /* Default signal handler */
241 }
242
243 /* G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
244 #ifndef G_PARAM_STATIC_STRINGS
245 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
246 #endif
247
248 static void
249 chimara_if_class_init(ChimaraIFClass *klass)
250 {
251         /* Override methods of parent classes */
252         GObjectClass *object_class = G_OBJECT_CLASS(klass);
253         object_class->set_property = chimara_if_set_property;
254         object_class->get_property = chimara_if_get_property;
255         object_class->finalize = chimara_if_finalize;
256
257         /* Signals */
258         klass->command = chimara_if_command;
259         /**
260          * ChimaraIF::command:
261          * @self: The widget that received the signal
262          * @input: The command typed into the game
263          * @response: The game's response to the command
264          *
265          * Emitted once for each input-response cycle of an interactive fiction
266          * game. Note that games with nontraditional input systems (i.e. not all
267          * taking place in the same text buffer window) may throw this signal for a
268          * loop.
269          */
270         chimara_if_signals[COMMAND] = g_signal_new("command",
271                 G_OBJECT_CLASS_TYPE(klass), 0,
272                 G_STRUCT_OFFSET(ChimaraIFClass, command), NULL, NULL,
273                 chimara_marshal_VOID__STRING_STRING,
274                 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
275
276         /* Properties */
277         /**
278          * ChimaraIF:piracy-mode:
279          *
280          * The Z-machine specification defines a facility for games to ask the
281          * interpreter they are running on whether this copy of the game is pirated.
282          * How the interpreter is supposed to magically determine that it is running
283          * pirate software is unclear, and so the majority of games and interpreters
284          * ignore this feature. Set this property to %TRUE if you want the
285          * interpreter to pretend it has detected a pirated game.
286          *
287          * Only affects Z-machine interpreters.
288          */
289         g_object_class_install_property(object_class, PROP_PIRACY_MODE,
290                 g_param_spec_boolean("piracy-mode", _("Piracy mode"),
291                 _("Pretend the game is pirated"), FALSE,
292                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
293         /**
294          * ChimaraIF:tandy-bit:
295          *
296          * Some early Infocom games were sold by the Tandy Corporation. Setting this
297          * property to %TRUE changes the wording of some Version 3 Infocom games
298          * slightly, so as to be less offensive. See <ulink
299          * url="http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html">
300          * http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html</ulink>.
301          *
302          * Only affects Z-machine interpreters.
303          */
304         g_object_class_install_property(object_class, PROP_TANDY_BIT,
305                 g_param_spec_boolean("tandy-bit", _("Tandy bit"),
306                 _("Censor certain Infocom games"), FALSE,
307                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
308         /**
309          * ChimaraIF:expand-abbreviations:
310          *
311          * Most Z-machine games, in particular ones compiled with the Inform
312          * library, support the following one-letter abbreviations:
313          * <simplelist>
314          * <member>D &mdash; Down</member>
315          * <member>E &mdash; East</member>
316          * <member>G &mdash; aGain</member>
317          * <member>I &mdash; Inventory</member>
318          * <member>L &mdash; Look</member>
319          * <member>N &mdash; North</member>
320          * <member>O &mdash; Oops</member>
321          * <member>Q &mdash; Quit</member>
322          * <member>S &mdash; South</member>
323          * <member>U &mdash; Up</member>
324          * <member>W &mdash; West</member>
325          * <member>X &mdash; eXamine</member>
326          * <member>Y &mdash; Yes</member>
327          * <member>Z &mdash; wait (ZZZZ...)</member>
328          * </simplelist>
329          * Some early Infocom games might not recognize these abbreviations. Setting
330          * this property to %TRUE will cause the interpreter to expand the
331          * abbreviations to the full words before passing the commands on to the
332          * game. Frotz only expands G, X, and Z; Nitfol expands all of the above
333          * plus the following nonstandard ones:
334          * <simplelist>
335          * <member>C &mdash; Close</member>
336          * <member>K &mdash; attacK</member>
337          * <member>P &mdash; oPen</member>
338          * <member>R &mdash; dRop</member>
339          * <member>T &mdash; Take</member>
340          * </simplelist>
341          *
342          * Only affects Z-machine interpreters. Behaves differently on Frotz and
343          * Nitfol.
344          */
345         g_object_class_install_property(object_class, PROP_EXPAND_ABBREVIATIONS,
346                 g_param_spec_boolean("expand-abbreviations", _("Expand abbreviations"),
347                 _("Expand abbreviations such as X for EXAMINE"), FALSE,
348                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
349         /**
350          * ChimaraIF:ignore-errors:
351          *
352          * Setting this property to %TRUE will cause the interpreter to ignore
353          * certain Z-machine runtime errors. Frotz will ignore any fatal errors.
354          * Nitfol by default warns about any shady behavior, and this property will
355          * turn those warnings off.
356          *
357          * Only affects Z-machine interpreters. Behaves differently on Frotz and
358          * Nitfol.
359          */
360         g_object_class_install_property(object_class, PROP_IGNORE_ERRORS,
361                 g_param_spec_boolean("ignore-errors", _("Ignore errors"),
362                 _("Do not warn the user about Z-machine errors"), FALSE,
363                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
364         /**
365          * ChimaraIF:typo-correction:
366          *
367          * Nitfol has an automatic typo-correction facility, where it searches the
368          * game dictionary for words which differ by one letter from any unknown
369          * input words. Set this property to %FALSE to turn this feature off.
370          *
371          * Only affects Nitfol.
372          */
373         g_object_class_install_property(object_class, PROP_TYPO_CORRECTION,
374                 g_param_spec_boolean("typo-correction", _("Typo correction"),
375                 _("Try to remedy typos if the interpreter supports it"), TRUE,
376                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
377         /**
378          * ChimaraIF:interpreter-number:
379          *
380          * Infocom gave each port of their interpreter a different number. The Frotz
381          * and Nitfol plugins can emulate any of these platforms. Some games behave
382          * slightly differently depending on what platform they are on. Set this
383          * property to a #ChimaraIFZmachineVersion value to emulate a certain
384          * platform.
385          *
386          * Note that Nitfol pretends to be an Apple IIe by default.
387          *
388          * Only affects Z-machine interpreters.
389          */
390         g_object_class_install_property(object_class, PROP_INTERPRETER_NUMBER,
391                 g_param_spec_uint("interpreter-number", _("Interpreter number"),
392                 _("Platform the Z-machine should pretend it is running on"),
393                 CHIMARA_IF_ZMACHINE_DEFAULT, CHIMARA_IF_ZMACHINE_MAXVAL, CHIMARA_IF_ZMACHINE_DEFAULT,
394                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
395         /**
396          * ChimaraIF:random-seed:
397          *
398          * If the #ChimaraIF:random-seed-set property is %TRUE, then the interpreter
399          * will use the value of this property as a seed for the random number
400          * generator. Use this feature to duplicate sequences of random numbers
401          * for testing games.
402          *
403          * Note that the value -1 is a valid random number seed for
404          * Nitfol, whereas it will cause Frotz to pick an arbitrary seed even when
405          * #ChimaraIF:random-seed-set is %TRUE.
406          *
407          * Only affects Z-machine interpreters. Behaves slightly differently on
408          * Frotz and Nitfol.
409          */
410         g_object_class_install_property(object_class, PROP_RANDOM_SEED,
411                 g_param_spec_int("random-seed", _("Random seed"),
412                 _("Seed for the random number generator"), G_MININT, G_MAXINT, 0,
413                 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
414         /**
415          * ChimaraIF:random-seed-set:
416          *
417          * Whether to use or ignore the #ChimaraIF:random-seed property.
418          *
419          * Only affects Z-machine interpreters.
420          */
421         g_object_class_install_property(object_class, PROP_RANDOM_SEED_SET,
422                 g_param_spec_boolean("random-seed-set", _("Random seed set"),
423                 _("Whether the seed for the random number generator should be set manually"), FALSE,
424                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
425
426         /* Private data */
427         g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
428 }
429
430 /* PUBLIC FUNCTIONS */
431
432 /**
433  * chimara_if_new:
434  *
435  * Creates and initializes a new #ChimaraIF widget.
436  *
437  * Return value: a #ChimaraIF widget, with a floating reference.
438  */
439 GtkWidget *
440 chimara_if_new(void)
441 {
442         /* This is a library entry point; initialize the library */
443         chimara_init();
444     return GTK_WIDGET(g_object_new(CHIMARA_TYPE_IF, NULL));
445 }
446
447 /**
448  * chimara_if_set_preferred_interpreter:
449  * @self: A #ChimaraIF widget.
450  * @format: The game format to set the preferred interpreter plugin for.
451  * @interpreter: The preferred interpreter plugin for @format.
452  *
453  * The function chimara_if_run_game() picks an appropriate interpreter for the
454  * type of game file it is given. This function sets which interpreter is picked
455  * for a certain game file format. Most formats, notably the Z-machine, have
456  * had many different interpreters written for them over the years, all with
457  * slightly different quirks and capabilities, so there is plenty of choice.
458  */
459 void
460 chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter)
461 {
462         g_return_if_fail(self && CHIMARA_IS_IF(self));
463         g_return_if_fail(format < CHIMARA_IF_NUM_FORMATS);
464         g_return_if_fail(interpreter < CHIMARA_IF_NUM_INTERPRETERS);
465
466         CHIMARA_IF_USE_PRIVATE(self, priv);
467
468         if(supported_formats[format][interpreter])
469                 priv->preferred_interpreter[format] = interpreter;
470         else
471                 g_warning("Format '%s' is not supported by interpreter '%s'", format_names[format], interpreter_names[interpreter]);
472 }
473
474 /**
475  * chimara_if_get_preferred_interpreter:
476  * @self: A #ChimaraIF widget.
477  * @format: The game format to query the preferred interpreter plugin for.
478  *
479  * Looks up the preferred interpreter for the game file format @format. See
480  * chimara_if_set_preferred_interpreter().
481  *
482  * Returns: a #ChimaraIFInterpreter value representing the preferred interpreter
483  * plugin for @format.
484  */
485 ChimaraIFInterpreter
486 chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
487 {
488         g_return_val_if_fail(self && CHIMARA_IS_IF(self), -1);
489         g_return_val_if_fail(format < CHIMARA_IF_NUM_FORMATS, -1);
490         CHIMARA_IF_USE_PRIVATE(self, priv);
491         return priv->preferred_interpreter[format];
492 }
493
494 /**
495  * chimara_if_run_game:
496  * @self: A #ChimaraIF widget.
497  * @gamefile: Path to an interactive fiction game file.
498  * @error: Return location for an error, or %NULL.
499  *
500  * Autodetects the type of a game file and runs it using an appropriate
501  * interpreter plugin. If there is more than one interpreter that supports the
502  * file format, the preferred one will be picked, according to
503  * chimara_if_set_preferred_interpreter().
504  *
505  * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
506  * case @error is set.
507  */
508 gboolean
509 chimara_if_run_game(ChimaraIF *self, gchar *gamefile, GError **error)
510 {
511         g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE);
512         g_return_val_if_fail(gamefile, FALSE);
513
514         CHIMARA_IF_USE_PRIVATE(self, priv);
515
516         /* Find out what format the game is */
517         /* TODO: Look inside the file instead of just looking at the extension */
518         ChimaraIFFormat format = CHIMARA_IF_FORMAT_Z5;
519         if(g_str_has_suffix(gamefile, ".z5"))
520                 format = CHIMARA_IF_FORMAT_Z5;
521         else if(g_str_has_suffix(gamefile, ".z6"))
522                 format = CHIMARA_IF_FORMAT_Z6;
523         else if(g_str_has_suffix(gamefile, ".z8"))
524                 format = CHIMARA_IF_FORMAT_Z8;
525         else if(g_str_has_suffix(gamefile, ".zlb") || g_str_has_suffix(gamefile, ".zblorb"))
526                 format = CHIMARA_IF_FORMAT_Z_BLORB;
527         else if(g_str_has_suffix(gamefile, ".ulx"))
528                 format = CHIMARA_IF_FORMAT_GLULX;
529         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"))
530                 format = CHIMARA_IF_FORMAT_GLULX_BLORB;
531
532         /* Now decide what interpreter to use */
533         ChimaraIFInterpreter interpreter = priv->preferred_interpreter[format];
534         gchar *pluginfile = g_strconcat(plugin_names[interpreter], "." G_MODULE_SUFFIX, NULL);
535
536         /* If there is a plugin in the source tree, use that */
537         gchar *pluginpath = g_build_filename("..", "interpreters", plugin_names[interpreter], LT_OBJDIR, pluginfile, NULL);
538         if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
539         {
540                 g_free(pluginpath);
541                 pluginpath = g_build_filename(PLUGINDIR, pluginfile, NULL);
542                 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
543                 {
544                         g_free(pluginpath);
545                         g_free(pluginfile);
546                         g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_NOT_FOUND, _("No appropriate %s interpreter plugin was found"), interpreter_names[interpreter]);
547                         return FALSE;
548                 }
549         }
550         g_free(pluginfile);
551
552         /* Decide what arguments to pass to the interpreters; currently only the
553         Z-machine interpreters accept command line arguments other than the game */
554         GSList *args = NULL;
555         gchar *terpnumstr = NULL, *randomstr = NULL;
556         args = g_slist_prepend(args, pluginpath);
557         args = g_slist_prepend(args, gamefile);
558         switch(interpreter)
559         {
560                 case CHIMARA_IF_INTERPRETER_FROTZ:
561                         if(priv->flags & CHIMARA_IF_PIRACY_MODE)
562                                 args = g_slist_prepend(args, "-P");
563                         if(priv->flags & CHIMARA_IF_TANDY_BIT)
564                                 args = g_slist_prepend(args, "-t");
565                         if(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS)
566                                 args = g_slist_prepend(args, "-x");
567                         if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
568                                 args = g_slist_prepend(args, "-i");
569                         if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
570                         {
571                                 terpnumstr = g_strdup_printf("-I%u", priv->interpreter_number);
572                                 args = g_slist_prepend(args, terpnumstr);
573                         }
574                         if(priv->random_seed_set)
575                         {
576                                 randomstr = g_strdup_printf("-s%d", priv->random_seed);
577                                 args = g_slist_prepend(args, randomstr);
578                         }
579                         break;
580                 case CHIMARA_IF_INTERPRETER_NITFOL:
581                         if(priv->flags & CHIMARA_IF_PIRACY_MODE)
582                                 args = g_slist_prepend(args, "-pirate");
583                         if(priv->flags & CHIMARA_IF_TANDY_BIT)
584                                 args = g_slist_prepend(args, "-tandy");
585                         if(!(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS))
586                                 args = g_slist_prepend(args, "-no-expand");
587                         if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
588                                 args = g_slist_prepend(args, "-ignore");
589                         if(!(priv->flags & CHIMARA_IF_TYPO_CORRECTION))
590                                 args = g_slist_prepend(args, "-no-spell");
591                         if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
592                         {
593                                 terpnumstr = g_strdup_printf("-terpnum%u", priv->interpreter_number);
594                                 args = g_slist_prepend(args, terpnumstr);
595                         }
596                         if(priv->random_seed_set)
597                         {
598                                 randomstr = g_strdup_printf("-random%d", priv->random_seed);
599                                 args = g_slist_prepend(args, randomstr);
600                         }
601                         break;
602                 default:
603                         ;
604         }
605
606         /* Allocate argv to hold the arguments */
607         int argc = g_slist_length(args);
608         args = g_slist_prepend(args, NULL);
609         char **argv = g_new0(char *, argc + 1);
610
611         /* Fill argv */
612         args = g_slist_reverse(args);
613         int count;
614         GSList *ptr;
615         for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
616                 argv[count] = ptr->data;
617
618         gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
619         g_free(argv);
620         if(terpnumstr)
621                 g_free(terpnumstr);
622         if(randomstr)
623                 g_free(randomstr);
624         g_free(pluginpath);
625
626         /* Set current format and interpreter if plugin was started successfully */
627         if(retval)
628         {
629                 priv->format = format;
630                 priv->interpreter = interpreter;
631         }
632         return retval;
633 }
634
635 /**
636  * chimara_if_get_format:
637  * @self: A #ChimaraIF widget.
638  *
639  * Returns the file format of the currently running game.
640  *
641  * Returns: a #ChimaraIFFormat constant.
642  */
643 ChimaraIFFormat
644 chimara_if_get_format(ChimaraIF *self)
645 {
646         g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
647         CHIMARA_IF_USE_PRIVATE(self, priv);
648         return priv->format;
649 }
650
651 /**
652  * chimara_if_get_interpreter:
653  * @self: A #ChimaraIF widget.
654  *
655  * Returns the interpreter plugin currently running.
656  *
657  * Returns: a #ChimaraIFInterpreter constant.
658  */
659 ChimaraIFInterpreter
660 chimara_if_get_interpreter(ChimaraIF *self)
661 {
662         g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
663         CHIMARA_IF_USE_PRIVATE(self, priv);
664         return priv->interpreter;
665 }