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