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