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