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