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