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