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"
15 * @short_description: Widget which plays an interactive fiction game
16 * @stability: Unstable
18 * The #ChimaraIF widget, given an interactive fiction game file to run, selects
19 * an appropriate interpreter plugin and runs it. Interpreter options are set by
20 * setting properties on the widget.
22 * Using it in a GTK program is similar to using #ChimaraGlk (which see).
23 * Threads must be initialized before using #ChimaraIF and the call to
24 * gtk_main() must be bracketed between gdk_threads_enter() and
25 * gdk_threads_leave(). Use chimara_if_run_game() to start playing an
26 * interactive fiction game.
29 static gboolean supported_formats[CHIMARA_IF_NUM_FORMATS][CHIMARA_IF_NUM_INTERPRETERS] = {
30 /* Frotz Nitfol Glulxe Git */
31 { TRUE, TRUE, FALSE, FALSE }, /* Z5 */
32 { TRUE, TRUE, FALSE, FALSE }, /* Z6 */
33 { TRUE, TRUE, FALSE, FALSE }, /* Z8 */
34 { TRUE, TRUE, FALSE, FALSE }, /* Zblorb */
35 { FALSE, FALSE, TRUE, TRUE }, /* Glulx */
36 { FALSE, FALSE, TRUE, TRUE } /* Gblorb */
38 static gchar *format_names[CHIMARA_IF_NUM_FORMATS] = {
39 N_("Z-code version 5"),
40 N_("Z-code version 6"),
41 N_("Z-code version 8"),
46 static gchar *interpreter_names[CHIMARA_IF_NUM_INTERPRETERS] = {
47 N_("Frotz"), N_("Nitfol"), N_("Glulxe"), N_("Git")
49 static gchar *plugin_names[CHIMARA_IF_NUM_INTERPRETERS] = {
50 "frotz", "nitfol", "glulxe", "git"
53 typedef enum _ChimaraIFFlags {
54 CHIMARA_IF_PIRACY_MODE = 1 << 0,
55 CHIMARA_IF_TANDY_BIT = 1 << 1,
56 CHIMARA_IF_EXPAND_ABBREVIATIONS = 1 << 2,
57 CHIMARA_IF_IGNORE_ERRORS = 1 << 3,
58 CHIMARA_IF_TYPO_CORRECTION = 1 << 4
61 typedef struct _ChimaraIFPrivate {
62 ChimaraIFInterpreter preferred_interpreter[CHIMARA_IF_NUM_FORMATS];
63 ChimaraIFFormat format;
64 ChimaraIFInterpreter interpreter;
66 ChimaraIFZmachineVersion interpreter_number;
68 gboolean random_seed_set;
70 /* Holding buffers for input and response */
75 #define CHIMARA_IF_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), CHIMARA_TYPE_IF, ChimaraIFPrivate))
76 #define CHIMARA_IF_USE_PRIVATE(o, n) ChimaraIFPrivate *n = CHIMARA_IF_PRIVATE(o)
82 PROP_EXPAND_ABBREVIATIONS,
85 PROP_INTERPRETER_NUMBER,
96 static guint chimara_if_signals[LAST_SIGNAL] = { 0 };
98 G_DEFINE_TYPE(ChimaraIF, chimara_if, CHIMARA_TYPE_GLK);
101 chimara_if_waiting(ChimaraGlk *glk)
103 CHIMARA_IF_USE_PRIVATE(glk, priv);
105 gchar *response = g_string_free(priv->response, FALSE);
106 priv->response = g_string_new("");
109 g_signal_emit_by_name(glk, "command", priv->input, response);
118 chimara_if_stopped(ChimaraGlk *glk)
120 CHIMARA_IF_USE_PRIVATE(glk, priv);
122 if(priv->input || priv->response->len > 0)
123 chimara_if_waiting(glk); /* Send one last command signal */
125 priv->format = CHIMARA_IF_FORMAT_NONE;
126 priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
130 chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, guint keysym)
132 CHIMARA_IF_USE_PRIVATE(glk, priv);
133 g_assert(priv->input == NULL);
136 gint outbuflen = g_unichar_to_utf8(gdk_keyval_to_unicode(keysym), outbuf);
137 priv->input = g_strndup(outbuf, outbuflen);
141 chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, gchar *input)
143 CHIMARA_IF_USE_PRIVATE(glk, priv);
144 g_assert(priv->input == NULL);
145 priv->input = g_strdup(input);
149 chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, gchar *output)
151 CHIMARA_IF_USE_PRIVATE(glk, priv);
152 g_string_append(priv->response, output);
156 chimara_if_init(ChimaraIF *self)
158 CHIMARA_IF_USE_PRIVATE(self, priv);
159 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z5] = CHIMARA_IF_INTERPRETER_FROTZ;
160 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z6] = CHIMARA_IF_INTERPRETER_NITFOL;
161 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z8] = CHIMARA_IF_INTERPRETER_FROTZ;
162 priv->preferred_interpreter[CHIMARA_IF_FORMAT_Z_BLORB] = CHIMARA_IF_INTERPRETER_FROTZ;
163 priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX] = CHIMARA_IF_INTERPRETER_GLULXE;
164 priv->preferred_interpreter[CHIMARA_IF_FORMAT_GLULX_BLORB] = CHIMARA_IF_INTERPRETER_GLULXE;
165 priv->format = CHIMARA_IF_FORMAT_NONE;
166 priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
167 priv->flags = CHIMARA_IF_TYPO_CORRECTION;
168 priv->interpreter_number = CHIMARA_IF_ZMACHINE_DEFAULT;
169 priv->random_seed_set = FALSE;
170 priv->graphics_file = NULL;
172 priv->response = g_string_new("");
174 /* Connect to signals of ChimaraGlk parent */
175 g_signal_connect(self, "stopped", G_CALLBACK(chimara_if_stopped), NULL);
176 g_signal_connect(self, "waiting", G_CALLBACK(chimara_if_waiting), NULL);
177 g_signal_connect(self, "char-input", G_CALLBACK(chimara_if_char_input), NULL);
178 g_signal_connect(self, "line-input", G_CALLBACK(chimara_if_line_input), NULL);
179 g_signal_connect(self, "text-buffer-output", G_CALLBACK(chimara_if_text_buffer_output), NULL);
182 #define PROCESS_FLAG(flags, flag, val) (flags) = (val)? (flags) | (flag) : (flags) & ~(flag)
185 chimara_if_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
187 CHIMARA_IF_USE_PRIVATE(object, priv);
190 case PROP_PIRACY_MODE:
191 PROCESS_FLAG(priv->flags, CHIMARA_IF_PIRACY_MODE, g_value_get_boolean(value));
192 g_object_notify(object, "piracy-mode");
195 PROCESS_FLAG(priv->flags, CHIMARA_IF_TANDY_BIT, g_value_get_boolean(value));
196 g_object_notify(object, "tandy-bit");
198 case PROP_EXPAND_ABBREVIATIONS:
199 PROCESS_FLAG(priv->flags, CHIMARA_IF_EXPAND_ABBREVIATIONS, g_value_get_boolean(value));
200 g_object_notify(object, "expand-abbreviations");
202 case PROP_IGNORE_ERRORS:
203 PROCESS_FLAG(priv->flags, CHIMARA_IF_IGNORE_ERRORS, g_value_get_boolean(value));
204 g_object_notify(object, "ignore-errors");
206 case PROP_TYPO_CORRECTION:
207 PROCESS_FLAG(priv->flags, CHIMARA_IF_TYPO_CORRECTION, g_value_get_boolean(value));
208 g_object_notify(object, "typo-correction");
210 case PROP_INTERPRETER_NUMBER:
211 priv->interpreter_number = g_value_get_uint(value);
212 g_object_notify(object, "interpreter-number");
214 case PROP_RANDOM_SEED:
215 priv->random_seed = g_value_get_int(value);
216 g_object_notify(object, "random-seed");
218 case PROP_RANDOM_SEED_SET:
219 priv->random_seed_set = g_value_get_boolean(value);
220 g_object_notify(object, "random-seed-set");
222 case PROP_GRAPHICS_FILE:
223 priv->graphics_file = g_strdup(g_value_get_string(value));
224 g_object_notify(object, "graphics-file");
227 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
232 chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
234 CHIMARA_IF_USE_PRIVATE(object, priv);
237 case PROP_PIRACY_MODE:
238 g_value_set_boolean(value, priv->flags & CHIMARA_IF_PIRACY_MODE);
241 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TANDY_BIT);
243 case PROP_EXPAND_ABBREVIATIONS:
244 g_value_set_boolean(value, priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS);
246 case PROP_IGNORE_ERRORS:
247 g_value_set_boolean(value, priv->flags & CHIMARA_IF_IGNORE_ERRORS);
249 case PROP_TYPO_CORRECTION:
250 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TYPO_CORRECTION);
252 case PROP_INTERPRETER_NUMBER:
253 g_value_set_uint(value, priv->interpreter_number);
255 case PROP_RANDOM_SEED:
256 g_value_set_int(value, priv->random_seed);
258 case PROP_RANDOM_SEED_SET:
259 g_value_set_boolean(value, priv->random_seed_set);
261 case PROP_GRAPHICS_FILE:
262 g_value_set_string(value, priv->graphics_file);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
270 chimara_if_finalize(GObject *object)
272 CHIMARA_IF_USE_PRIVATE(object, priv);
273 g_free(priv->graphics_file);
274 G_OBJECT_CLASS(chimara_if_parent_class)->finalize(object);
278 chimara_if_command(ChimaraIF *self, gchar *input, gchar *response)
280 /* Default signal handler */
283 /* COMPAT: G_PARAM_STATIC_STRINGS only appeared in GTK 2.13.0 */
284 #ifndef G_PARAM_STATIC_STRINGS
286 /* COMPAT: G_PARAM_STATIC_NAME and friends only appeared in GTK 2.8 */
287 #if GTK_CHECK_VERSION(2,8,0)
288 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
290 #define G_PARAM_STATIC_STRINGS (0)
296 chimara_if_class_init(ChimaraIFClass *klass)
298 /* Override methods of parent classes */
299 GObjectClass *object_class = G_OBJECT_CLASS(klass);
300 object_class->set_property = chimara_if_set_property;
301 object_class->get_property = chimara_if_get_property;
302 object_class->finalize = chimara_if_finalize;
305 klass->command = chimara_if_command;
307 * ChimaraIF::command:
308 * @self: The widget that received the signal
309 * @input: The command typed into the game, or %NULL
310 * @response: The game's response to the command
312 * Emitted once for each input-response cycle of an interactive fiction
313 * game. Note that games with nontraditional input systems (i.e. not all
314 * taking place in the same text buffer window) may confuse this signal.
316 * It may happen that @input is %NULL, in which case @response is not due to
317 * a user command, but contains the text printed at the beginning of the
318 * game, up until the first prompt.
320 chimara_if_signals[COMMAND] = g_signal_new("command",
321 G_OBJECT_CLASS_TYPE(klass), 0,
322 G_STRUCT_OFFSET(ChimaraIFClass, command), NULL, NULL,
323 _chimara_marshal_VOID__STRING_STRING,
324 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
328 * ChimaraIF:piracy-mode:
330 * The Z-machine specification defines a facility for games to ask the
331 * interpreter they are running on whether this copy of the game is pirated.
332 * How the interpreter is supposed to magically determine that it is running
333 * pirate software is unclear, and so the majority of games and interpreters
334 * ignore this feature. Set this property to %TRUE if you want the
335 * interpreter to pretend it has detected a pirated game.
337 * Only affects Z-machine interpreters.
339 g_object_class_install_property(object_class, PROP_PIRACY_MODE,
340 g_param_spec_boolean("piracy-mode", _("Piracy mode"),
341 _("Pretend the game is pirated"), FALSE,
342 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
344 * ChimaraIF:tandy-bit:
346 * Some early Infocom games were sold by the Tandy Corporation. Setting this
347 * property to %TRUE changes the wording of some Version 3 Infocom games
348 * slightly, so as to be less offensive. See <ulink
349 * url="http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html">
350 * http://www.ifarchive.org/if-archive/infocom/info/tandy_bits.html</ulink>.
352 * Only affects Z-machine interpreters.
354 g_object_class_install_property(object_class, PROP_TANDY_BIT,
355 g_param_spec_boolean("tandy-bit", _("Tandy bit"),
356 _("Censor certain Infocom games"), FALSE,
357 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
359 * ChimaraIF:expand-abbreviations:
361 * Most Z-machine games, in particular ones compiled with the Inform
362 * library, support the following one-letter abbreviations:
364 * <member>D — Down</member>
365 * <member>E — East</member>
366 * <member>G — aGain</member>
367 * <member>I — Inventory</member>
368 * <member>L — Look</member>
369 * <member>N — North</member>
370 * <member>O — Oops</member>
371 * <member>Q — Quit</member>
372 * <member>S — South</member>
373 * <member>U — Up</member>
374 * <member>W — West</member>
375 * <member>X — eXamine</member>
376 * <member>Y — Yes</member>
377 * <member>Z — wait (ZZZZ...)</member>
379 * Some early Infocom games might not recognize these abbreviations. Setting
380 * this property to %TRUE will cause the interpreter to expand the
381 * abbreviations to the full words before passing the commands on to the
382 * game. Frotz only expands G, X, and Z; Nitfol expands all of the above
383 * plus the following nonstandard ones:
385 * <member>C — Close</member>
386 * <member>K — attacK</member>
387 * <member>P — oPen</member>
388 * <member>R — dRop</member>
389 * <member>T — Take</member>
392 * Only affects Z-machine interpreters. Behaves differently on Frotz and
395 g_object_class_install_property(object_class, PROP_EXPAND_ABBREVIATIONS,
396 g_param_spec_boolean("expand-abbreviations", _("Expand abbreviations"),
397 _("Expand abbreviations such as X for EXAMINE"), FALSE,
398 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
400 * ChimaraIF:ignore-errors:
402 * Setting this property to %TRUE will cause the interpreter to ignore
403 * certain Z-machine runtime errors. Frotz will ignore any fatal errors.
404 * Nitfol by default warns about any shady behavior, and this property will
405 * turn those warnings off.
407 * Only affects Z-machine interpreters. Behaves differently on Frotz and
410 g_object_class_install_property(object_class, PROP_IGNORE_ERRORS,
411 g_param_spec_boolean("ignore-errors", _("Ignore errors"),
412 _("Do not warn the user about Z-machine errors"), FALSE,
413 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
415 * ChimaraIF:typo-correction:
417 * Nitfol has an automatic typo-correction facility, where it searches the
418 * game dictionary for words which differ by one letter from any unknown
419 * input words. Set this property to %FALSE to turn this feature off.
421 * Only affects Nitfol.
423 g_object_class_install_property(object_class, PROP_TYPO_CORRECTION,
424 g_param_spec_boolean("typo-correction", _("Typo correction"),
425 _("Try to remedy typos if the interpreter supports it"), TRUE,
426 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
428 * ChimaraIF:interpreter-number:
430 * Infocom gave each port of their interpreter a different number. The Frotz
431 * and Nitfol plugins can emulate any of these platforms. Some games behave
432 * slightly differently depending on what platform they are on. Set this
433 * property to a #ChimaraIFZmachineVersion value to emulate a certain
436 * Note that Nitfol pretends to be an Apple IIe by default.
438 * Only affects Z-machine interpreters.
440 g_object_class_install_property(object_class, PROP_INTERPRETER_NUMBER,
441 g_param_spec_uint("interpreter-number", _("Interpreter number"),
442 _("Platform the Z-machine should pretend it is running on"),
443 CHIMARA_IF_ZMACHINE_DEFAULT, CHIMARA_IF_ZMACHINE_MAXVAL, CHIMARA_IF_ZMACHINE_DEFAULT,
444 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
446 * ChimaraIF:random-seed:
448 * If the #ChimaraIF:random-seed-set property is %TRUE, then the interpreter
449 * will use the value of this property as a seed for the random number
450 * generator. Use this feature to duplicate sequences of random numbers
453 * Note that the value -1 is a valid random number seed for
454 * Nitfol, whereas it will cause Frotz to pick an arbitrary seed even when
455 * #ChimaraIF:random-seed-set is %TRUE.
457 * Only affects Z-machine interpreters. Behaves slightly differently on
460 g_object_class_install_property(object_class, PROP_RANDOM_SEED,
461 g_param_spec_int("random-seed", _("Random seed"),
462 _("Seed for the random number generator"), G_MININT, G_MAXINT, 0,
463 G_PARAM_READWRITE | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
465 * ChimaraIF:random-seed-set:
467 * Whether to use or ignore the #ChimaraIF:random-seed property.
469 * Only affects Z-machine interpreters.
471 g_object_class_install_property(object_class, PROP_RANDOM_SEED_SET,
472 g_param_spec_boolean("random-seed-set", _("Random seed set"),
473 _("Whether the seed for the random number generator should be set manually"), FALSE,
474 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
476 * ChimaraIF:graphics-file:
478 * Some Z-machine interpreters accept an extra argument that indicates a
479 * separate Blorb file containing graphics and sound resources. The
480 * interpreter will check if the file specified in this property really
481 * exists, and if so, use it as a resource file. If this property is set to
482 * %NULL, the interpreter will not look for an extra file.
484 * Only affects Frotz and Nitfol.
486 g_object_class_install_property(object_class, PROP_GRAPHICS_FILE,
487 g_param_spec_string("graphics-file", _("Graphics file"),
488 _("Location in which to look for a separate graphics Blorb file"), NULL,
489 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_LAX_VALIDATION | G_PARAM_STATIC_STRINGS));
491 g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
494 /* PUBLIC FUNCTIONS */
499 * Creates and initializes a new #ChimaraIF widget.
501 * Return value: a #ChimaraIF widget, with a floating reference.
506 /* This is a library entry point; initialize the library */
508 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_IF, NULL));
512 * chimara_if_set_preferred_interpreter:
513 * @self: A #ChimaraIF widget.
514 * @format: The game format to set the preferred interpreter plugin for.
515 * @interpreter: The preferred interpreter plugin for @format.
517 * The function chimara_if_run_game() picks an appropriate interpreter for the
518 * type of game file it is given. This function sets which interpreter is picked
519 * for a certain game file format. Most formats, notably the Z-machine, have
520 * had many different interpreters written for them over the years, all with
521 * slightly different quirks and capabilities, so there is plenty of choice.
524 chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter)
526 g_return_if_fail(self && CHIMARA_IS_IF(self));
527 g_return_if_fail(format < CHIMARA_IF_NUM_FORMATS);
528 g_return_if_fail(interpreter < CHIMARA_IF_NUM_INTERPRETERS);
530 CHIMARA_IF_USE_PRIVATE(self, priv);
532 if(supported_formats[format][interpreter])
533 priv->preferred_interpreter[format] = interpreter;
535 g_warning("Format '%s' is not supported by interpreter '%s'", format_names[format], interpreter_names[interpreter]);
539 * chimara_if_get_preferred_interpreter:
540 * @self: A #ChimaraIF widget.
541 * @format: The game format to query the preferred interpreter plugin for.
543 * Looks up the preferred interpreter for the game file format @format. See
544 * chimara_if_set_preferred_interpreter().
546 * Returns: a #ChimaraIFInterpreter value representing the preferred interpreter
547 * plugin for @format.
550 chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
552 g_return_val_if_fail(self && CHIMARA_IS_IF(self), -1);
553 g_return_val_if_fail(format < CHIMARA_IF_NUM_FORMATS, -1);
554 CHIMARA_IF_USE_PRIVATE(self, priv);
555 return priv->preferred_interpreter[format];
559 * chimara_if_run_game:
560 * @self: A #ChimaraIF widget.
561 * @gamefile: Path to an interactive fiction game file.
562 * @error: Return location for an error, or %NULL.
564 * Autodetects the type of a game file and runs it using an appropriate
565 * interpreter plugin. If there is more than one interpreter that supports the
566 * file format, the preferred one will be picked, according to
567 * chimara_if_set_preferred_interpreter().
569 * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
570 * case @error is set.
573 chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error)
575 g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE);
576 g_return_val_if_fail(gamefile, FALSE);
578 CHIMARA_IF_USE_PRIVATE(self, priv);
580 /* Find out what format the game is */
581 /* TODO: Look inside the file instead of just looking at the extension */
582 ChimaraIFFormat format = CHIMARA_IF_FORMAT_Z5;
583 if(g_str_has_suffix(gamefile, ".z5"))
584 format = CHIMARA_IF_FORMAT_Z5;
585 else if(g_str_has_suffix(gamefile, ".z6"))
586 format = CHIMARA_IF_FORMAT_Z6;
587 else if(g_str_has_suffix(gamefile, ".z8"))
588 format = CHIMARA_IF_FORMAT_Z8;
589 else if(g_str_has_suffix(gamefile, ".zlb") || g_str_has_suffix(gamefile, ".zblorb"))
590 format = CHIMARA_IF_FORMAT_Z_BLORB;
591 else if(g_str_has_suffix(gamefile, ".ulx"))
592 format = CHIMARA_IF_FORMAT_GLULX;
593 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"))
594 format = CHIMARA_IF_FORMAT_GLULX_BLORB;
596 /* Now decide what interpreter to use */
597 ChimaraIFInterpreter interpreter = priv->preferred_interpreter[format];
598 gchar *pluginfile = g_strconcat(plugin_names[interpreter], "." G_MODULE_SUFFIX, NULL);
603 #define LT_OBJDIR ".libs" /* Pre-2.2 libtool, so take a wild guess */
604 #endif /* LT_OBJDIR */
605 /* If there is a plugin in the source tree, use that */
606 pluginpath = g_build_filename(PLUGINSOURCEDIR, plugin_names[interpreter], LT_OBJDIR, pluginfile, NULL);
607 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
611 pluginpath = g_build_filename(PLUGINDIR, pluginfile, NULL);
612 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
616 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_NOT_FOUND, _("No appropriate %s interpreter plugin was found"), interpreter_names[interpreter]);
624 /* Decide what arguments to pass to the interpreters; currently only the
625 Z-machine interpreters accept command line arguments other than the game */
627 gchar *terpnumstr = NULL, *randomstr = NULL;
628 args = g_slist_prepend(args, pluginpath);
631 case CHIMARA_IF_INTERPRETER_FROTZ:
632 if(priv->flags & CHIMARA_IF_PIRACY_MODE)
633 args = g_slist_prepend(args, "-P");
634 if(priv->flags & CHIMARA_IF_TANDY_BIT)
635 args = g_slist_prepend(args, "-t");
636 if(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS)
637 args = g_slist_prepend(args, "-x");
638 if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
639 args = g_slist_prepend(args, "-i");
640 if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
642 terpnumstr = g_strdup_printf("-I%u", priv->interpreter_number);
643 args = g_slist_prepend(args, terpnumstr);
645 if(priv->random_seed_set)
647 randomstr = g_strdup_printf("-s%d", priv->random_seed);
648 args = g_slist_prepend(args, randomstr);
651 case CHIMARA_IF_INTERPRETER_NITFOL:
652 if(priv->flags & CHIMARA_IF_PIRACY_MODE)
653 args = g_slist_prepend(args, "-pirate");
654 if(priv->flags & CHIMARA_IF_TANDY_BIT)
655 args = g_slist_prepend(args, "-tandy");
656 if(!(priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS))
657 args = g_slist_prepend(args, "-no-expand");
658 if(priv->flags & CHIMARA_IF_IGNORE_ERRORS)
659 args = g_slist_prepend(args, "-ignore");
660 if(!(priv->flags & CHIMARA_IF_TYPO_CORRECTION))
661 args = g_slist_prepend(args, "-no-spell");
662 if(priv->interpreter_number != CHIMARA_IF_ZMACHINE_DEFAULT)
664 terpnumstr = g_strdup_printf("-terpnum%u", priv->interpreter_number);
665 args = g_slist_prepend(args, terpnumstr);
667 if(priv->random_seed_set)
669 randomstr = g_strdup_printf("-random%d", priv->random_seed);
670 args = g_slist_prepend(args, randomstr);
677 /* Game file and external blorb file */
678 args = g_slist_prepend(args, gamefile);
679 if(priv->graphics_file
680 && (interpreter == CHIMARA_IF_INTERPRETER_FROTZ || interpreter == CHIMARA_IF_INTERPRETER_NITFOL)
681 && g_file_test(priv->graphics_file, G_FILE_TEST_EXISTS)) {
682 args = g_slist_prepend(args, priv->graphics_file);
685 /* Allocate argv to hold the arguments */
686 int argc = g_slist_length(args);
687 args = g_slist_prepend(args, NULL);
688 char **argv = g_new0(char *, argc + 1);
691 args = g_slist_reverse(args);
694 for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
695 argv[count] = ptr->data;
697 /* Set the story name */
698 /* We peek into ChimaraGlk's private data here, because GObject has no
699 equivalent to "protected" */
700 CHIMARA_GLK_USE_PRIVATE(self, glk_priv);
701 glk_priv->story_name = g_path_get_basename(gamefile);
702 g_object_notify(G_OBJECT(self), "story-name");
704 gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
712 /* Set current format and interpreter if plugin was started successfully */
715 priv->format = format;
716 priv->interpreter = interpreter;
722 * chimara_if_get_format:
723 * @self: A #ChimaraIF widget.
725 * Returns the file format of the currently running game.
727 * Returns: a #ChimaraIFFormat constant.
730 chimara_if_get_format(ChimaraIF *self)
732 g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
733 CHIMARA_IF_USE_PRIVATE(self, priv);
738 * chimara_if_get_interpreter:
739 * @self: A #ChimaraIF widget.
741 * Returns the interpreter plugin currently running.
743 * Returns: a #ChimaraIFInterpreter constant.
746 chimara_if_get_interpreter(ChimaraIF *self)
748 g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
749 CHIMARA_IF_USE_PRIVATE(self, priv);
750 return priv->interpreter;