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_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;
176 priv->response = g_string_new("");
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);
186 #define PROCESS_FLAG(flags, flag, val) (flags) = (val)? (flags) | (flag) : (flags) & ~(flag)
189 chimara_if_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
191 CHIMARA_IF_USE_PRIVATE(object, priv);
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");
199 PROCESS_FLAG(priv->flags, CHIMARA_IF_TANDY_BIT, g_value_get_boolean(value));
200 g_object_notify(object, "tandy-bit");
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");
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");
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");
214 case PROP_INTERPRETER_NUMBER:
215 priv->interpreter_number = g_value_get_uint(value);
216 g_object_notify(object, "interpreter-number");
218 case PROP_RANDOM_SEED:
219 priv->random_seed = g_value_get_int(value);
220 g_object_notify(object, "random-seed");
222 case PROP_RANDOM_SEED_SET:
223 priv->random_seed_set = g_value_get_boolean(value);
224 g_object_notify(object, "random-seed-set");
226 case PROP_GRAPHICS_FILE:
227 priv->graphics_file = g_strdup(g_value_get_string(value));
228 g_object_notify(object, "graphics-file");
231 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
236 chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
238 CHIMARA_IF_USE_PRIVATE(object, priv);
241 case PROP_PIRACY_MODE:
242 g_value_set_boolean(value, priv->flags & CHIMARA_IF_PIRACY_MODE);
245 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TANDY_BIT);
247 case PROP_EXPAND_ABBREVIATIONS:
248 g_value_set_boolean(value, priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS);
250 case PROP_IGNORE_ERRORS:
251 g_value_set_boolean(value, priv->flags & CHIMARA_IF_IGNORE_ERRORS);
253 case PROP_TYPO_CORRECTION:
254 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TYPO_CORRECTION);
256 case PROP_INTERPRETER_NUMBER:
257 g_value_set_uint(value, priv->interpreter_number);
259 case PROP_RANDOM_SEED:
260 g_value_set_int(value, priv->random_seed);
262 case PROP_RANDOM_SEED_SET:
263 g_value_set_boolean(value, priv->random_seed_set);
265 case PROP_GRAPHICS_FILE:
266 g_value_set_string(value, priv->graphics_file);
269 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
274 chimara_if_finalize(GObject *object)
276 CHIMARA_IF_USE_PRIVATE(object, priv);
277 g_free(priv->graphics_file);
278 G_OBJECT_CLASS(chimara_if_parent_class)->finalize(object);
282 chimara_if_command(ChimaraIF *self, gchar *input, gchar *response)
284 /* Default signal handler */
287 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
288 #ifndef G_PARAM_STATIC_STRINGS
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)
294 #define G_PARAM_STATIC_STRINGS (0)
300 chimara_if_class_init(ChimaraIFClass *klass)
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;
309 klass->command = chimara_if_command;
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
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.
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.
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);
332 * ChimaraIF:piracy-mode:
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.
341 * Only affects Z-machine interpreters.
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));
348 * ChimaraIF:tandy-bit:
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>.
356 * Only affects Z-machine interpreters.
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));
363 * ChimaraIF:expand-abbreviations:
365 * Most Z-machine games, in particular ones compiled with the Inform
366 * library, support the following one-letter abbreviations:
368 * <member>D — Down</member>
369 * <member>E — East</member>
370 * <member>G — aGain</member>
371 * <member>I — Inventory</member>
372 * <member>L — Look</member>
373 * <member>N — North</member>
374 * <member>O — Oops</member>
375 * <member>Q — Quit</member>
376 * <member>S — South</member>
377 * <member>U — Up</member>
378 * <member>W — West</member>
379 * <member>X — eXamine</member>
380 * <member>Y — Yes</member>
381 * <member>Z — wait (ZZZZ...)</member>
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:
389 * <member>C — Close</member>
390 * <member>K — attacK</member>
391 * <member>P — oPen</member>
392 * <member>R — dRop</member>
393 * <member>T — Take</member>
396 * Only affects Z-machine interpreters. Behaves differently on Frotz and
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));
404 * ChimaraIF:ignore-errors:
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.
411 * Only affects Z-machine interpreters. Behaves differently on Frotz and
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));
419 * ChimaraIF:typo-correction:
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.
425 * Only affects Nitfol.
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));
432 * ChimaraIF:interpreter-number:
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
440 * Note that Nitfol pretends to be an Apple IIe by default.
442 * Only affects Z-machine interpreters.
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));
450 * ChimaraIF:random-seed:
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
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.
461 * Only affects Z-machine interpreters. Behaves slightly differently on
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));
469 * ChimaraIF:random-seed-set:
471 * Whether to use or ignore the #ChimaraIF:random-seed property.
473 * Only affects Z-machine interpreters.
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));
480 * ChimaraIF:graphics-file:
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.
488 * Only affects Frotz and Nitfol.
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));
495 g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
498 /* PUBLIC FUNCTIONS */
503 * Creates and initializes a new #ChimaraIF widget.
505 * Return value: a #ChimaraIF widget, with a floating reference.
510 /* This is a library entry point; initialize the library */
512 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_IF, NULL));
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.
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.
528 chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter)
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);
534 CHIMARA_IF_USE_PRIVATE(self, priv);
536 if(supported_formats[format][interpreter])
537 priv->preferred_interpreter[format] = interpreter;
539 g_warning("Format '%s' is not supported by interpreter '%s'", format_names[format], interpreter_names[interpreter]);
543 * chimara_if_get_preferred_interpreter:
544 * @self: A #ChimaraIF widget.
545 * @format: The game format to query the preferred interpreter plugin for.
547 * Looks up the preferred interpreter for the game file format @format. See
548 * chimara_if_set_preferred_interpreter().
550 * Returns: a #ChimaraIFInterpreter value representing the preferred interpreter
551 * plugin for @format.
554 chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
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];
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.
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().
573 * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
574 * case @error is set.
577 chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error)
579 g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE);
580 g_return_val_if_fail(gamefile, FALSE);
582 CHIMARA_IF_USE_PRIVATE(self, priv);
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;
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);
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) )
615 pluginpath = g_build_filename(PLUGINDIR, pluginfile, NULL);
616 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
620 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_NOT_FOUND, _("No appropriate %s interpreter plugin was found"), interpreter_names[interpreter]);
628 /* Decide what arguments to pass to the interpreters; currently only the
629 Z-machine interpreters accept command line arguments other than the game */
631 gchar *terpnumstr = NULL, *randomstr = NULL;
632 args = g_slist_prepend(args, pluginpath);
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)
646 terpnumstr = g_strdup_printf("-I%u", priv->interpreter_number);
647 args = g_slist_prepend(args, terpnumstr);
649 if(priv->random_seed_set)
651 randomstr = g_strdup_printf("-s%d", priv->random_seed);
652 args = g_slist_prepend(args, randomstr);
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)
668 terpnumstr = g_strdup_printf("-terpnum%u", priv->interpreter_number);
669 args = g_slist_prepend(args, terpnumstr);
671 if(priv->random_seed_set)
673 randomstr = g_strdup_printf("-random%d", priv->random_seed);
674 args = g_slist_prepend(args, randomstr);
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);
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);
695 args = g_slist_reverse(args);
698 for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
699 argv[count] = ptr->data;
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");
708 gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
716 /* Set current format and interpreter if plugin was started successfully */
719 priv->format = format;
720 priv->interpreter = interpreter;
726 * chimara_if_get_format:
727 * @self: A #ChimaraIF widget.
729 * Returns the file format of the currently running game.
731 * Returns: a #ChimaraIFFormat constant.
734 chimara_if_get_format(ChimaraIF *self)
736 g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
737 CHIMARA_IF_USE_PRIVATE(self, priv);
742 * chimara_if_get_interpreter:
743 * @self: A #ChimaraIF widget.
745 * Returns the interpreter plugin currently running.
747 * Returns: a #ChimaraIFInterpreter constant.
750 chimara_if_get_interpreter(ChimaraIF *self)
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;