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