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 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 */
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"), N_("Bocfel")
53 static gchar *plugin_names[CHIMARA_IF_NUM_INTERPRETERS] = {
54 "frotz", "nitfol", "glulxe", "git", "bocfel"
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 inputs and responses */
75 GHashTable *active_inputs;
76 GSList *window_librock_list;
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)
92 PROP_EXPAND_ABBREVIATIONS,
95 PROP_INTERPRETER_NUMBER,
106 static guint chimara_if_signals[LAST_SIGNAL] = { 0 };
108 G_DEFINE_TYPE(ChimaraIF, chimara_if, CHIMARA_TYPE_GLK);
110 static InputResponse *
111 input_response_new(void)
113 InputResponse *retval = g_slice_new0(InputResponse);
114 retval->response = g_string_new("");
119 input_response_destroy(InputResponse *inp)
122 g_string_free(inp->response, TRUE);
123 g_slice_free(InputResponse, inp);
126 static InputResponse *
127 ensure_input_response_object(ChimaraIFPrivate *priv, char *string_id)
129 InputResponse *retval = g_hash_table_lookup(priv->active_inputs, string_id);
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);
141 emit_command_signal_on_active_inputs(char *window_librock, ChimaraGlk *glk)
143 CHIMARA_IF_USE_PRIVATE(glk, priv);
144 InputResponse *inp = g_hash_table_lookup(priv->active_inputs, window_librock);
148 char *response = g_strdup(inp->response->str);
149 g_string_truncate(inp->response, 0);
152 g_signal_emit_by_name(glk, "command", inp->input, response);
162 chimara_if_waiting(ChimaraGlk *glk)
164 CHIMARA_IF_USE_PRIVATE(glk, priv);
165 g_slist_foreach(priv->window_librock_list, (GFunc)emit_command_signal_on_active_inputs, glk);
169 chimara_if_stopped(ChimaraGlk *glk)
171 CHIMARA_IF_USE_PRIVATE(glk, priv);
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);
176 priv->format = CHIMARA_IF_FORMAT_NONE;
177 priv->interpreter = CHIMARA_IF_INTERPRETER_NONE;
181 chimara_if_char_input(ChimaraGlk *glk, guint32 win_rock, char *string_id, unsigned keysym)
183 CHIMARA_IF_USE_PRIVATE(glk, priv);
184 InputResponse *inp = ensure_input_response_object(priv, string_id);
185 g_assert(!inp->active);
188 gint outbuflen = g_unichar_to_utf8(gdk_keyval_to_unicode(keysym), outbuf);
189 inp->input = g_strndup(outbuf, outbuflen);
194 chimara_if_line_input(ChimaraGlk *glk, guint32 win_rock, char *string_id, char *input)
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);
204 chimara_if_text_buffer_output(ChimaraGlk *glk, guint32 win_rock, char *string_id, char *output)
206 CHIMARA_IF_USE_PRIVATE(glk, priv);
207 InputResponse *inp = ensure_input_response_object(priv, string_id);
208 g_string_append(inp->response, output);
213 chimara_if_init(ChimaraIF *self)
215 chimara_init(); /* This is a library entry point */
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);
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);
239 #define PROCESS_FLAG(flags, flag, val) (flags) = (val)? (flags) | (flag) : (flags) & ~(flag)
242 chimara_if_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
244 CHIMARA_IF_USE_PRIVATE(object, priv);
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");
252 PROCESS_FLAG(priv->flags, CHIMARA_IF_TANDY_BIT, g_value_get_boolean(value));
253 g_object_notify(object, "tandy-bit");
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");
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");
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");
267 case PROP_INTERPRETER_NUMBER:
268 priv->interpreter_number = g_value_get_uint(value);
269 g_object_notify(object, "interpreter-number");
271 case PROP_RANDOM_SEED:
272 priv->random_seed = g_value_get_int(value);
273 g_object_notify(object, "random-seed");
275 case PROP_RANDOM_SEED_SET:
276 priv->random_seed_set = g_value_get_boolean(value);
277 g_object_notify(object, "random-seed-set");
279 case PROP_GRAPHICS_FILE:
280 priv->graphics_file = g_strdup(g_value_get_string(value));
281 g_object_notify(object, "graphics-file");
284 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
289 chimara_if_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
291 CHIMARA_IF_USE_PRIVATE(object, priv);
294 case PROP_PIRACY_MODE:
295 g_value_set_boolean(value, priv->flags & CHIMARA_IF_PIRACY_MODE);
298 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TANDY_BIT);
300 case PROP_EXPAND_ABBREVIATIONS:
301 g_value_set_boolean(value, priv->flags & CHIMARA_IF_EXPAND_ABBREVIATIONS);
303 case PROP_IGNORE_ERRORS:
304 g_value_set_boolean(value, priv->flags & CHIMARA_IF_IGNORE_ERRORS);
306 case PROP_TYPO_CORRECTION:
307 g_value_set_boolean(value, priv->flags & CHIMARA_IF_TYPO_CORRECTION);
309 case PROP_INTERPRETER_NUMBER:
310 g_value_set_uint(value, priv->interpreter_number);
312 case PROP_RANDOM_SEED:
313 g_value_set_int(value, priv->random_seed);
315 case PROP_RANDOM_SEED_SET:
316 g_value_set_boolean(value, priv->random_seed_set);
318 case PROP_GRAPHICS_FILE:
319 g_value_set_string(value, priv->graphics_file);
322 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
327 chimara_if_finalize(GObject *object)
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);
337 chimara_if_class_init(ChimaraIFClass *klass)
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;
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
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.
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.
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);
368 * ChimaraIF:piracy-mode:
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.
377 * Only affects Z-machine interpreters.
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));
384 * ChimaraIF:tandy-bit:
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>.
392 * Only affects Z-machine interpreters.
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));
399 * ChimaraIF:expand-abbreviations:
401 * Most Z-machine games, in particular ones compiled with the Inform
402 * library, support the following one-letter abbreviations:
404 * <member>D — Down</member>
405 * <member>E — East</member>
406 * <member>G — aGain</member>
407 * <member>I — Inventory</member>
408 * <member>L — Look</member>
409 * <member>N — North</member>
410 * <member>O — Oops</member>
411 * <member>Q — Quit</member>
412 * <member>S — South</member>
413 * <member>U — Up</member>
414 * <member>W — West</member>
415 * <member>X — eXamine</member>
416 * <member>Y — Yes</member>
417 * <member>Z — wait (ZZZZ...)</member>
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:
425 * <member>C — Close</member>
426 * <member>K — attacK</member>
427 * <member>P — oPen</member>
428 * <member>R — dRop</member>
429 * <member>T — Take</member>
432 * Only affects Z-machine interpreters. Behaves differently on Frotz and
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));
440 * ChimaraIF:ignore-errors:
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.
447 * Only affects Z-machine interpreters. Behaves differently on Frotz and
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));
455 * ChimaraIF:typo-correction:
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.
461 * Only affects Nitfol.
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));
468 * ChimaraIF:interpreter-number:
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
476 * Note that Nitfol pretends to be an Apple IIe by default.
478 * Only affects Z-machine interpreters.
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));
486 * ChimaraIF:random-seed:
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
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.
497 * Only affects Z-machine interpreters. Behaves slightly differently on
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));
505 * ChimaraIF:random-seed-set:
507 * Whether to use or ignore the #ChimaraIF:random-seed property.
509 * Only affects Z-machine interpreters.
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));
516 * ChimaraIF:graphics-file:
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.
524 * Only affects Frotz and Nitfol.
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));
531 g_type_class_add_private(klass, sizeof(ChimaraIFPrivate));
534 /* PUBLIC FUNCTIONS */
539 * Creates and initializes a new #ChimaraIF widget.
541 * Return value: a #ChimaraIF widget, with a floating reference.
546 /* This is a library entry point; initialize the library */
548 return GTK_WIDGET(g_object_new(CHIMARA_TYPE_IF, NULL));
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.
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.
564 chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter)
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);
570 CHIMARA_IF_USE_PRIVATE(self, priv);
572 if(supported_formats[format][interpreter])
573 priv->preferred_interpreter[format] = interpreter;
575 g_warning("Format '%s' is not supported by interpreter '%s'", format_names[format], interpreter_names[interpreter]);
579 * chimara_if_get_preferred_interpreter:
580 * @self: A #ChimaraIF widget.
581 * @format: The game format to query the preferred interpreter plugin for.
583 * Looks up the preferred interpreter for the game file format @format. See
584 * chimara_if_set_preferred_interpreter().
586 * Returns: a #ChimaraIFInterpreter value representing the preferred interpreter
587 * plugin for @format.
590 chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format)
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];
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.
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().
609 * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
610 * case @error is set.
613 chimara_if_run_game(ChimaraIF *self, const char *game_path, GError **error)
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);
619 CHIMARA_IF_USE_PRIVATE(self, priv);
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;
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);
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) )
652 pluginpath = g_build_filename(PLUGINDIR, pluginfile, NULL);
653 if( !g_file_test(pluginpath, G_FILE_TEST_EXISTS) )
657 g_set_error(error, CHIMARA_ERROR, CHIMARA_PLUGIN_NOT_FOUND, _("No appropriate %s interpreter plugin was found"), interpreter_names[interpreter]);
665 /* Decide what arguments to pass to the interpreters; currently only the
666 Z-machine interpreters accept command line arguments other than the game */
668 gchar *terpnumstr = NULL, *randomstr = NULL;
669 args = g_slist_prepend(args, pluginpath);
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)
683 terpnumstr = g_strdup_printf("-I%u", priv->interpreter_number);
684 args = g_slist_prepend(args, terpnumstr);
686 if(priv->random_seed_set)
688 randomstr = g_strdup_printf("-s%d", priv->random_seed);
689 args = g_slist_prepend(args, randomstr);
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)
705 terpnumstr = g_strdup_printf("-terpnum%u", priv->interpreter_number);
706 args = g_slist_prepend(args, terpnumstr);
708 if(priv->random_seed_set)
710 randomstr = g_strdup_printf("-random%d", priv->random_seed);
711 args = g_slist_prepend(args, randomstr);
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);
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);
732 args = g_slist_reverse(args);
735 for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
736 argv[count] = g_strdup(ptr->data);
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");
745 gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error);
753 /* Set current format and interpreter if plugin was started successfully */
756 priv->format = format;
757 priv->interpreter = interpreter;
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.
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.
771 * Returns: %TRUE if the game was started successfully, %FALSE if not, in which
772 * case @error is set.
775 chimara_if_run_game_file(ChimaraIF *self, GFile *game_file, GError **error)
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);
781 char *path = g_file_get_path(game_file);
782 gboolean retval = chimara_if_run_game(self, path, error);
788 * chimara_if_get_format:
789 * @self: A #ChimaraIF widget.
791 * Returns the file format of the currently running game.
793 * Returns: a #ChimaraIFFormat constant.
796 chimara_if_get_format(ChimaraIF *self)
798 g_return_val_if_fail(self && CHIMARA_IS_IF(self), CHIMARA_IF_FORMAT_NONE);
799 CHIMARA_IF_USE_PRIVATE(self, priv);
804 * chimara_if_get_interpreter:
805 * @self: A #ChimaraIF widget.
807 * Returns the interpreter plugin currently running.
809 * Returns: a #ChimaraIFInterpreter constant.
812 chimara_if_get_interpreter(ChimaraIF *self)
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;