4 #include <glib-object.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"
19 * @short_description: Widget which plays an interactive fiction game
20 * @stability: Unstable
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.
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.
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 */
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"),
50 static gchar *interpreter_names[CHIMARA_IF_NUM_INTERPRETERS] = {
51 N_("Frotz"), N_("Nitfol"), N_("Glulxe"), N_("Git")
53 static gchar *plugin_names[CHIMARA_IF_NUM_INTERPRETERS] = {
54 "frotz", "nitfol", "glulxe", "git"
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
65 typedef struct _ChimaraIFPrivate {
66 ChimaraIFInterpreter preferred_interpreter[CHIMARA_IF_NUM_FORMATS];
67 ChimaraIFFormat format;
68 ChimaraIFInterpreter interpreter;
70 ChimaraIFZmachineVersion interpreter_number;
72 gboolean random_seed_set;
74 /* Holding buffers for input and response */
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)
86 PROP_EXPAND_ABBREVIATIONS,
89 PROP_INTERPRETER_NUMBER,
100 static guint chimara_if_signals[LAST_SIGNAL] = { 0 };
102 G_DEFINE_TYPE(ChimaraIF, chimara_if, CHIMARA_TYPE_GLK);
105 chimara_if_waiting(ChimaraGlk *glk)
107 CHIMARA_IF_USE_PRIVATE(glk, priv);
109 gchar *response = g_string_free(priv->response, FALSE);
110 priv->response = g_string_new("");
113 g_signal_emit_by_name(glk, "command", priv->input, response);
122 chimara_if_stopped(ChimaraGlk *glk)
124 CHIMARA_IF_USE_PRIVATE(glk, priv);
126 if(priv->input || priv->response->len > 0)
127 chimara_if_waiting(glk); /* Send one last command signal */
129 priv->format = CHIMARA_IF_FORMAT_NONE;
130 priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
134 chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, guint keysym)
136 CHIMARA_IF_USE_PRIVATE(glk, priv);
137 g_assert(priv->input == NULL);
140 gint outbuflen = g_unichar_to_utf8(gdk_keyval_to_unicode(keysym), outbuf);
141 priv->input = g_strndup(outbuf, outbuflen);
145 chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, gchar *input)
147 CHIMARA_IF_USE_PRIVATE(glk, priv);
148 g_assert(priv->input == NULL);
149 priv->input = g_strdup(input);
153 chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, gchar *output)
155 CHIMARA_IF_USE_PRIVATE(glk, priv);
156 g_string_append(priv->response, output);
160 chimara_if_init(ChimaraIF *self)
162 chimara_init(); /* This is a library entry point */
164 CHIMARA_IF_USE_PRIVATE(self, priv);
165 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z5] = CHIMARA_IF_INTERPRETER_FROTZ;
166 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z6] = CHIMARA_IF_INTERPRETER_NITFOL;
167 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z8] = CHIMARA_IF_INTERPRETER_FROTZ;
168 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z_BLORB] = CHIMARA_IF_INTERPRETER_FROTZ;
169 priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX] = CHIMARA_IF_INTERPRETER_GLULXE;
170 priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX_BLORB] = CHIMARA_IF_INTERPRETER_GLULXE;
171 priv->format = CHIMARA_IF_FORMAT_NONE;
172 priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
173 priv->flags = CHIMARA_IF_TYPO_CORRECTION;
174 priv->interpreter_number = CHIMARA_IF_ZMACHINE_DEFAULT;
175 priv->random_seed_set = FALSE;
176 priv->graphics_file = NULL;
178 priv->response = g_string_new("");
180 /* Connect to signals of ChimaraGlk parent */
181 g_signal_connect(self, "stopped", G_CALLBACK(chimara_if_stopped), NULL);
182 g_signal_connect(self, "waiting", G_CALLBACK(chimara_if_waiting), NULL);
183 g_signal_connect(self, "char-input", G_CALLBACK(chimara_if_char_input), NULL);
184 g_signal_connect(self, "line-input", G_CALLBACK(chimara_if_line_input), NULL);
185 g_signal_connect(self, "text-buffer-output", G_CALLBACK(chimara_if_text_buffer_output), NULL);
188 #define PROCESS_FLAG(flags, flag, val) (flags) = (val)? (flags) | (flag) : (flags) & ~(flag)
191 chimara_if_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
193 CHIMARA_IF_USE_PRIVATE(object, priv);
196 case PROP_PIRACY_MODE:
197 PROCESS_FLAG(priv->flags, CHIMARA_IF_PIRACY_MODE, g_value_get_boolean(value));
198 g_object_notify(object, "piracy-mode");
201 PROCESS_FLAG(priv->flags, CHIMARA_IF_TANDY_BIT, g_value_get_boolean(value));
202 g_object_notify(object, "tandy-bit");
204 case PROP_EXPAND_ABBREVIATIONS:
205 PROCESS_FLAG(priv->flags, CHIMARA_IF_EXPAND_ABBREVIATIONS, g_value_get_boolean(value));
206 g_object_notify(object, "expand-abbreviations");
208 case PROP_IGNORE_ERRORS:
209 PROCESS_FLAG(priv->flags, CHIMARA_IF_IGNORE_ERRORS, g_value_get_boolean(value));
210 g_object_notify(object, "ignore-errors");
212 case PROP_TYPO_CORRECTION:
213 PROCESS_FLAG(priv->flags, CHIMARA_IF_TYPO_CORRECTION, g_value_get_boolean(value));
214 g_object_notify(object, "typo-correction");
216 case PROP_INTERPRETER_NUMBER:
217 priv->interpreter_number = g_value_get_uint(value);
218 g_object_notify(object, "interpreter-number");
220 case PROP_RANDOM_SEED:
221 priv->random_seed = g_value_get_int(value);
222 g_object_notify(object, "random-seed");
224 case PROP_RANDOM_SEED_SET:
225 priv->random_seed_set = g_value_get_boolean(value);
226 g_object_notify(object, "random-seed-set");
228 case PROP_GRAPHICS_FILE:
229 priv->graphics_file = g_strdup(g_value_get_string(value));
230 g_object_notify(object, "graphics-file");
233 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
238 chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
240 CHIMARA_IF_USE_PRIVATE(object, priv);
243 case PROP_PIRACY_MODE:
244 g_value_set_boolean(value, priv->flags & CHIMARA_IF_PIRACY_MODE);
247 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TANDY_BIT);
249 case PROP_EXPAND_ABBREVIATIONS:
250 g_value_set_boolean(value, priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS);
252 case PROP_IGNORE_ERRORS:
253 g_value_set_boolean(value, priv->flags & CHIMARA_IF_IGNORE_ERRORS);
255 case PROP_TYPO_CORRECTION:
256 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TYPO_CORRECTION);
258 case PROP_INTERPRETER_NUMBER:
259 g_value_set_uint(value, priv->interpreter_number);
261 case PROP_RANDOM_SEED:
262 g_value_set_int(value, priv->random_seed);
264 case PROP_RANDOM_SEED_SET:
265 g_value_set_boolean(value, priv->random_seed_set);
267 case PROP_GRAPHICS_FILE:
268 g_value_set_string(value, priv->graphics_file);
271 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
276 chimara_if_finalize(GObject *object)
278 CHIMARA_IF_USE_PRIVATE(object, priv);
279 g_free(priv->graphics_file);
280 G_OBJECT_CLASS(chimara_if_parent_class)->finalize(object);
284 chimara_if_command(ChimaraIF *self, gchar *input, gchar *response)
286 /* Default signal handler */
289 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
290 #ifndef G_PARAM_STATIC_STRINGS
292 /* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
293 #if GTK_CHECK_VERSION(2,8,0)
294 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
296 #define G_PARAM_STATIC_STRINGS (0)
302 chimara_if_class_init(ChimaraIFClass *klass)
304 /* Override methods of parent classes */
305 GObjectClass *object_class = G_OBJECT_CLASS(klass);
306 object_class->set_property = chimara_if_set_property;
307 object_class->get_property = chimara_if_get_property;
308 object_class->finalize = chimara_if_finalize;
311 klass->command = chimara_if_command;
313 * ChimaraIF::command:
314 * @self: The widget that received the signal
315 * @input: The command typed into the game, or %NULL
316 * @response: The game's response to the command
318 * Emitted once for each input-response cycle of an interactive fiction
319 * game. Note that games with nontraditional input systems (i.e. not all
320 * taking place in the same text buffer window) may confuse this signal.
322 * It may happen that @input is %NULL, in which case @response is not due to
323 * a user command, but contains the text printed at the beginning of the
324 * game, up until the first prompt.
326 chimara_if_signals[COMMAND] = g_signal_new("command",
327 G_OBJECT_CLASS_TYPE(klass), 0,
328 G_STRUCT_OFFSET(ChimaraIFClass, command), NULL, NULL,
329 _chimara_marshal_VOID__STRING_STRING,
330 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
334 * ChimaraIF:piracy-mode:
336 * The Z-machine specification defines a facility for games to ask the
337 * interpreter they are running on whether this copy of the game is pirated.
338 * How the interpreter is supposed to magically determine that it is running
339 * pirate software is unclear, and so the majority of games and interpreters
340 * ignore this feature. Set this property to %TRUE if you want the
341 * interpreter to pretend it has detected a pirated game.
343 * Only affects Z-machine interpreters.
345 g_object_class_install_property(object_class, PROP_PIRACY_MODE,
346 g_param_spec_boolean("piracy-mode", _("Piracy mode"),
347 _("Pretend the game is pirated"), FALSE,
348 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
350 * ChimaraIF:tandy-bit:
352 * Some early Infocom games were sold by the Tandy Corporation. Setting this
353 * property to %TRUE changes the wording of some Version 3 Infocom games
354 * slightly, so as to be less offensive. See <ulink
355 * url="http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html">
356 * http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html</ulink>.
358 * Only affects Z-machine interpreters.
360 g_object_class_install_property(object_class, PROP_TANDY_BIT,
361 g_param_spec_boolean("tandy-bit", _("Tandy bit"),
362 _("Censor certain Infocom games"), FALSE,
363 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
365 * ChimaraIF:expand-abbreviations:
367 * Most Z-machine games, in particular ones compiled with the Inform
368 * library, support the following one-letter abbreviations:
370 * <member>D — Down</member>
371 * <member>E — East</member>
372 * <member>G — aGain</member>
373 * <member>I — Inventory</member>
374 * <member>L — Look</member>
375 * <member>N — North</member>
376 * <member>O — Oops</member>
377 * <member>Q — Quit</member>
378 * <member>S — South</member>
379 * <member>U — Up</member>
380 * <member>W — West</member>
381 * <member>X — eXamine</member>
382 * <member>Y — Yes</member>
383 * <member>Z — wait (ZZZZ...)</member>
385 * Some early Infocom games might not recognize these abbreviations. Setting
386 * this property to %TRUE will cause the interpreter to expand the
387 * abbreviations to the full words before passing the commands on to the
388 * game. Frotz only expands G, X, and Z; Nitfol expands all of the above
389 * plus the following nonstandard ones:
391 * <member>C — Close</member>
392 * <member>K — attacK</member>
393 * <member>P — oPen</member>
394 * <member>R — dRop</member>
395 * <member>T — Take</member>
398 * Only affects Z-machine interpreters. Behaves differently on Frotz and
401 g_object_class_install_property(object_class, PROP_EXPAND_ABBREVIATIONS,
402 g_param_spec_boolean("expand-abbreviations", _("Expand abbreviations"),
403 _("Expand abbreviations such as X for EXAMINE"), FALSE,
404 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
406 * ChimaraIF:ignore-errors:
408 * Setting this property to %TRUE will cause the interpreter to ignore
409 * certain Z-machine runtime errors. Frotz will ignore any fatal errors.
410 * Nitfol by default warns about any shady behavior, and this property will
411 * turn those warnings off.
413 * Only affects Z-machine interpreters. Behaves differently on Frotz and
416 g_object_class_install_property(object_class, PROP_IGNORE_ERRORS,
417 g_param_spec_boolean("ignore-errors", _("Ignore errors"),
418 _("Do not warn the user about Z-machine errors"), FALSE,
419 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
421 * ChimaraIF:typo-correction:
423 * Nitfol has an automatic typo-correction facility, where it searches the
424 * game dictionary for words which differ by one letter from any unknown
425 * input words. Set this property to %FALSE to turn this feature off.
427 * Only affects Nitfol.
429 g_object_class_install_property(object_class, PROP_TYPO_CORRECTION,
430 g_param_spec_boolean("typo-correction", _("Typo correction"),
431 _("Try to remedy typos if the interpreter supports it"), TRUE,
432 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
434 * ChimaraIF:interpreter-number:
436 * Infocom gave each port of their interpreter a different number. The Frotz
437 * and Nitfol plugins can emulate any of these platforms. Some games behave
438 * slightly differently depending on what platform they are on. Set this
439 * property to a #ChimaraIFZmachineVersion value to emulate a certain
442 * Note that Nitfol pretends to be an Apple IIe by default.
444 * Only affects Z-machine interpreters.
446 g_object_class_install_property(object_class, PROP_INTERPRETER_NUMBER,
447 g_param_spec_uint("interpreter-number", _("Interpreter number"),
448 _("Platform the Z-machine should pretend it is running on"),
449 CHIMARA_IF_ZMACHINE_DEFAULT, CHIMARA_IF_ZMACHINE_MAXVAL, CHIMARA_IF_ZMACHINE_DEFAULT,
450 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
452 * ChimaraIF:random-seed:
454 * If the #ChimaraIF:random-seed-set property is %TRUE, then the interpreter
455 * will use the value of this property as a seed for the random number
456 * generator. Use this feature to duplicate sequences of random numbers
459 * Note that the value -1 is a valid random number seed for
460 * Nitfol, whereas it will cause Frotz to pick an arbitrary seed even when
461 * #ChimaraIF:random-seed-set is %TRUE.
463 * Only affects Z-machine interpreters. Behaves slightly differently on
466 g_object_class_install_property(object_class, PROP_RANDOM_SEED,
467 g_param_spec_int("random-seed", _("Random seed"),
468 _("Seed for the random number generator"), G_MININT, G_MAXINT, 0,
469 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
471 * ChimaraIF:random-seed-set:
473 * Whether to use or ignore the #ChimaraIF:random-seed property.
475 * Only affects Z-machine interpreters.
477 g_object_class_install_property(object_class, PROP_RANDOM_SEED_SET,
478 g_param_spec_boolean("random-seed-set", _("Random seed set"),
479 _("Whether the seed for the random number generator should be set manually"), FALSE,
480 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
482 * ChimaraIF:graphics-file:
484 * Some Z-machine interpreters accept an extra argument that indicates a
485 * separate Blorb file containing graphics and sound resources. The
486 * interpreter will check if the file specified in this property really
487 * exists, and if so, use it as a resource file. If this property is set to
488 * %NULL, the interpreter will not look for an extra file.
490 * Only affects Frotz and Nitfol.
492 g_object_class_install_property(object_class, PROP_GRAPHICS_FILE,
493 g_param_spec_string("graphics-file", _("Graphics file"),
494 _("Location in which to look for a separate graphics Blorb file"), NULL,
495 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
497 g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
500 /* PUBLIC FUNCTIONS */
505 * Creates and initializes a new #ChimaraIF widget.
507 * Return value: a #ChimaraIF widget, with a floating reference.
512 /* This is a library entry point; initialize the library */
514 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_IF, NULL));
518 * chimara_if_set_preferred_interpreter:
519 * @self: A #ChimaraIF widget.
520 * @format: The game format to set the preferred interpreter plugin for.
521 * @interpreter: The preferred interpreter plugin for @format.
523 * The function chimara_if_run_game() picks an appropriate interpreter for the
524 * type of game file it is given. This function sets which interpreter is picked
525 * for a certain game file format. Most formats, notably the Z-machine, have
526 * had many different interpreters written for them over the years, all with
527 * slightly different quirks and capabilities, so there is plenty of choice.
530 chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter)
532 g_return_if_fail(self && CHIMARA_IS_IF(self));
533 g_return_if_fail(format < CHIMARA_IF_NUM_FORMATS);
534 g_return_if_fail(interpreter < CHIMARA_IF_NUM_INTERPRETERS);
536 CHIMARA_IF_USE_PRIVATE(self, priv);
538 if(supported_formats[format][interpreter])
539 priv->preferred_interpreter[format] = interpreter;
541 g_warning("Format '%s' is not supported by interpreter '%s'", format_names[format], interpreter_names[interpreter]);
545 * chimara_if_get_preferred_interpreter:
546 * @self: A #ChimaraIF widget.
547 * @format: The game format to query the preferred interpreter plugin for.
549 * Looks up the preferred interpreter for the game file format @format. See
550 * chimara_if_set_preferred_interpreter().
552 * Returns: a #ChimaraIFInterpreter value representing the preferred interpreter
553 * plugin for @format.
556 chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
558 g_return_val_if_fail(self && CHIMARA_IS_IF(self), -1);
559 g_return_val_if_fail(format < CHIMARA_IF_NUM_FORMATS, -1);
560 CHIMARA_IF_USE_PRIVATE(self, priv);
561 return priv->preferred_interpreter[format];
565 * chimara_if_run_game:
566 * @self: A #ChimaraIF widget.
567 * @game_path: Path to an interactive fiction game file.
568 * @error: Return location for an error, or %NULL.
570 * Autodetects the type of a game file and runs it using an appropriate
571 * interpreter plugin. If there is more than one interpreter that supports the
572 * file format, the preferred one will be picked, according to
573 * chimara_if_set_preferred_interpreter().
575 * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
576 * case @error is set.
579 chimara_if_run_game(ChimaraIF *self, const char *game_path, GError **error)
581 g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE);
582 g_return_val_if_fail(game_path, FALSE);
583 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
585 CHIMARA_IF_USE_PRIVATE(self, priv);
587 /* Find out what format the game is */
588 /* TODO: Look inside the file instead of just looking at the extension */
589 ChimaraIFFormat format = CHIMARA_IF_FORMAT_Z5;
590 if(g_str_has_suffix(game_path, ".z5"))
591 format = CHIMARA_IF_FORMAT_Z5;
592 else if(g_str_has_suffix(game_path, ".z6"))
593 format = CHIMARA_IF_FORMAT_Z6;
594 else if(g_str_has_suffix(game_path, ".z8"))
595 format = CHIMARA_IF_FORMAT_Z8;
596 else if(g_str_has_suffix(game_path, ".zlb") || g_str_has_suffix(game_path, ".zblorb"))
597 format = CHIMARA_IF_FORMAT_Z_BLORB;
598 else if(g_str_has_suffix(game_path, ".ulx"))
599 format = CHIMARA_IF_FORMAT_GLULX;
600 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"))
601 format = CHIMARA_IF_FORMAT_GLULX_BLORB;
603 /* Now decide what interpreter to use */
604 ChimaraIFInterpreter interpreter = priv->preferred_interpreter[format];
605 gchar *pluginfile = g_strconcat(plugin_names[interpreter], "." G_MODULE_SUFFIX, NULL);
610 #define LT_OBJDIR ".libs" /* Pre-2.2 libtool, so take a wild guess */
611 #endif /* LT_OBJDIR */
612 /* If there is a plugin in the source tree, use that */
613 pluginpath = g_build_filename(PLUGINSOURCEDIR, plugin_names[interpreter], LT_OBJDIR, pluginfile, NULL);
614 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
618 pluginpath = g_build_filename(PLUGINDIR, pluginfile, NULL);
619 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
623 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_NOT_FOUND, _("No appropriate %s interpreter plugin was found"), interpreter_names[interpreter]);
631 /* Decide what arguments to pass to the interpreters; currently only the
632 Z-machine interpreters accept command line arguments other than the game */
634 gchar *terpnumstr = NULL, *randomstr = NULL;
635 args = g_slist_prepend(args, pluginpath);
638 case CHIMARA_IF_INTERPRETER_FROTZ:
639 if(priv->flags & CHIMARA_IF_PIRACY_MODE)
640 args = g_slist_prepend(args, "-P");
641 if(priv->flags & CHIMARA_IF_TANDY_BIT)
642 args = g_slist_prepend(args, "-t");
643 if(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS)
644 args = g_slist_prepend(args, "-x");
645 if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
646 args = g_slist_prepend(args, "-i");
647 if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
649 terpnumstr = g_strdup_printf("-I%u", priv->interpreter_number);
650 args = g_slist_prepend(args, terpnumstr);
652 if(priv->random_seed_set)
654 randomstr = g_strdup_printf("-s%d", priv->random_seed);
655 args = g_slist_prepend(args, randomstr);
658 case CHIMARA_IF_INTERPRETER_NITFOL:
659 if(priv->flags & CHIMARA_IF_PIRACY_MODE)
660 args = g_slist_prepend(args, "-pirate");
661 if(priv->flags & CHIMARA_IF_TANDY_BIT)
662 args = g_slist_prepend(args, "-tandy");
663 if(!(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS))
664 args = g_slist_prepend(args, "-no-expand");
665 if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
666 args = g_slist_prepend(args, "-ignore");
667 if(!(priv->flags & CHIMARA_IF_TYPO_CORRECTION))
668 args = g_slist_prepend(args, "-no-spell");
669 if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
671 terpnumstr = g_strdup_printf("-terpnum%u", priv->interpreter_number);
672 args = g_slist_prepend(args, terpnumstr);
674 if(priv->random_seed_set)
676 randomstr = g_strdup_printf("-random%d", priv->random_seed);
677 args = g_slist_prepend(args, randomstr);
684 /* Game file and external blorb file */
685 args = g_slist_prepend(args, (gpointer)game_path);
686 if(priv->graphics_file
687 && (interpreter == CHIMARA_IF_INTERPRETER_FROTZ || interpreter == CHIMARA_IF_INTERPRETER_NITFOL)
688 && g_file_test(priv->graphics_file, G_FILE_TEST_EXISTS)) {
689 args = g_slist_prepend(args, priv->graphics_file);
692 /* Allocate argv to hold the arguments */
693 int argc = g_slist_length(args);
694 args = g_slist_prepend(args, NULL);
695 char **argv = g_new0(char *, argc + 1);
698 args = g_slist_reverse(args);
701 for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
702 argv[count] = g_strdup(ptr->data);
704 /* Set the story name */
705 /* We peek into ChimaraGlk's private data here, because GObject has no
706 equivalent to "protected" */
707 CHIMARA_GLK_USE_PRIVATE(self, glk_priv);
708 glk_priv->story_name = g_path_get_basename(game_path);
709 g_object_notify(G_OBJECT(self), "story-name");
711 gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
719 /* Set current format and interpreter if plugin was started successfully */
722 priv->format = format;
723 priv->interpreter = interpreter;
729 * chimara_if_run_game_file:
730 * @self: A #ChimaraIF widget.
731 * @game_file: a #GFile pointing to an interactive fiction game file.
732 * @error: Return location for an error, or %NULL.
734 * Autodetects the type of a game file and runs it using an appropriate
735 * interpreter plugin. See chimara_if_run_game() for more information.
737 * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
738 * case @error is set.
741 chimara_if_run_game_file(ChimaraIF *self, GFile *game_file, GError **error)
743 g_return_val_if_fail(self || CHIMARA_IS_IF(self), FALSE);
744 g_return_val_if_fail(game_file || G_IS_FILE(game_file), FALSE);
745 g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
747 char *path = g_file_get_path(game_file);
748 gboolean retval = chimara_if_run_game(self, path, error);
754 * chimara_if_get_format:
755 * @self: A #ChimaraIF widget.
757 * Returns the file format of the currently running game.
759 * Returns: a #ChimaraIFFormat constant.
762 chimara_if_get_format(ChimaraIF *self)
764 g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
765 CHIMARA_IF_USE_PRIVATE(self, priv);
770 * chimara_if_get_interpreter:
771 * @self: A #ChimaraIF widget.
773 * Returns the interpreter plugin currently running.
775 * Returns: a #ChimaraIFInterpreter constant.
778 chimara_if_get_interpreter(ChimaraIF *self)
780 g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
781 CHIMARA_IF_USE_PRIVATE(self, priv);
782 return priv->interpreter;