From: Philip Chimento Date: Sun, 1 Jan 2012 17:32:28 +0000 (+0100) Subject: Add API to start plugins using GFile X-Git-Tag: v0.9~48 X-Git-Url: https://git.stderr.nl/gitweb?p=projects%2Fchimara%2Fchimara.git;a=commitdiff_plain;h=28c6b69cb8e971a9959066fe896edcc4ae412935 Add API to start plugins using GFile To complete the API, we should have functions that start a plugin using GIO's GFile API. It's easier for navigating the filesystem in language bindings such as Python. --- diff --git a/libchimara/chimara-glk.c b/libchimara/chimara-glk.c index 949f951..b7c0a7d 100644 --- a/libchimara/chimara-glk.c +++ b/libchimara/chimara-glk.c @@ -1299,6 +1299,35 @@ chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GE return !(priv->thread == NULL); } +/** + * chimara_glk_run_file: + * @self: a #ChimaraGlk widget + * @plugin_file: a #GFile pointing to a plugin module compiled with glk.h + * @argc: Number of command line arguments in @argv + * @argv: Array of command line arguments to pass to the plugin + * @error: location to store a GError, or %NULL + * + * Opens a Glk program compiled as a plugin, from a #GFile. See + * chimara_glk_run() for details. + * + * Return value: %TRUE if the Glk program was started successfully. + */ +gboolean +chimara_glk_run_file(ChimaraGlk *self, GFile *plugin_file, int argc, char *argv[], GError **error) +{ + g_return_val_if_fail(self || CHIMARA_IS_GLK(self), FALSE); + g_return_val_if_fail(plugin_file || G_IS_FILE(plugin_file), FALSE); + g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + + char *path = g_file_get_path(plugin_file); + gboolean retval = chimara_glk_run(self, path, argc, argv, error); + g_free(path); + + return retval; +} + /** * chimara_glk_stop: * @glk: a #ChimaraGlk widget diff --git a/libchimara/chimara-glk.h b/libchimara/chimara-glk.h index 644de4d..b3ea8b8 100644 --- a/libchimara/chimara-glk.h +++ b/libchimara/chimara-glk.h @@ -122,6 +122,7 @@ void chimara_glk_set_css_from_string(ChimaraGlk *glk, const gchar *css); void chimara_glk_set_spacing(ChimaraGlk *glk, guint spacing); guint chimara_glk_get_spacing(ChimaraGlk *glk); gboolean chimara_glk_run(ChimaraGlk *glk, const gchar *plugin, int argc, char *argv[], GError **error); +gboolean chimara_glk_run_file(ChimaraGlk *self, GFile *plugin_file, int argc, char *argv[], GError **error); void chimara_glk_stop(ChimaraGlk *glk); void chimara_glk_wait(ChimaraGlk *glk); void chimara_glk_unload_plugin(ChimaraGlk *glk); diff --git a/libchimara/chimara-if.c b/libchimara/chimara-if.c index a459ac0..ed132bb 100644 --- a/libchimara/chimara-if.c +++ b/libchimara/chimara-if.c @@ -562,7 +562,7 @@ chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format) /** * chimara_if_run_game: * @self: A #ChimaraIF widget. - * @gamefile: Path to an interactive fiction game file. + * @game_path: Path to an interactive fiction game file. * @error: Return location for an error, or %NULL. * * Autodetects the type of a game file and runs it using an appropriate @@ -574,27 +574,28 @@ chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format) * case @error is set. */ gboolean -chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error) +chimara_if_run_game(ChimaraIF *self, const char *game_path, GError **error) { g_return_val_if_fail(self && CHIMARA_IS_IF(self), FALSE); - g_return_val_if_fail(gamefile, FALSE); + g_return_val_if_fail(game_path, FALSE); + g_return_val_if_fail(error == NULL || *error == NULL, FALSE); CHIMARA_IF_USE_PRIVATE(self, priv); /* Find out what format the game is */ /* TODO: Look inside the file instead of just looking at the extension */ ChimaraIFFormat format = CHIMARA_IF_FORMAT_Z5; - if(g_str_has_suffix(gamefile, ".z5")) + if(g_str_has_suffix(game_path, ".z5")) format = CHIMARA_IF_FORMAT_Z5; - else if(g_str_has_suffix(gamefile, ".z6")) + else if(g_str_has_suffix(game_path, ".z6")) format = CHIMARA_IF_FORMAT_Z6; - else if(g_str_has_suffix(gamefile, ".z8")) + else if(g_str_has_suffix(game_path, ".z8")) format = CHIMARA_IF_FORMAT_Z8; - else if(g_str_has_suffix(gamefile, ".zlb") || g_str_has_suffix(gamefile, ".zblorb")) + else if(g_str_has_suffix(game_path, ".zlb") || g_str_has_suffix(game_path, ".zblorb")) format = CHIMARA_IF_FORMAT_Z_BLORB; - else if(g_str_has_suffix(gamefile, ".ulx")) + else if(g_str_has_suffix(game_path, ".ulx")) format = CHIMARA_IF_FORMAT_GLULX; - 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")) + 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")) format = CHIMARA_IF_FORMAT_GLULX_BLORB; /* Now decide what interpreter to use */ @@ -679,7 +680,7 @@ chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error) } /* Game file and external blorb file */ - args = g_slist_prepend(args, (gpointer)gamefile); + args = g_slist_prepend(args, (gpointer)game_path); if(priv->graphics_file && (interpreter == CHIMARA_IF_INTERPRETER_FROTZ || interpreter == CHIMARA_IF_INTERPRETER_NITFOL) && g_file_test(priv->graphics_file, G_FILE_TEST_EXISTS)) { @@ -702,7 +703,7 @@ chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error) /* We peek into ChimaraGlk's private data here, because GObject has no equivalent to "protected" */ CHIMARA_GLK_USE_PRIVATE(self, glk_priv); - glk_priv->story_name = g_path_get_basename(gamefile); + glk_priv->story_name = g_path_get_basename(game_path); g_object_notify(G_OBJECT(self), "story-name"); gboolean retval = chimara_glk_run(CHIMARA_GLK(self), pluginpath, argc, argv, error); @@ -722,6 +723,31 @@ chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error) return retval; } +/** + * chimara_if_run_game_file: + * @self: A #ChimaraIF widget. + * @game_file: a #GFile pointing to an interactive fiction game file. + * @error: Return location for an error, or %NULL. + * + * Autodetects the type of a game file and runs it using an appropriate + * interpreter plugin. See chimara_if_run_game() for more information. + * + * Returns: %TRUE if the game was started successfully, %FALSE if not, in which + * case @error is set. + */ +gboolean +chimara_if_run_game_file(ChimaraIF *self, GFile *game_file, GError **error) +{ + g_return_val_if_fail(self || CHIMARA_IS_IF(self), FALSE); + g_return_val_if_fail(game_file || G_IS_FILE(game_file), FALSE); + g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + + char *path = g_file_get_path(game_file); + gboolean retval = chimara_if_run_game(self, path, error); + g_free(path); + return retval; +} + /** * chimara_if_get_format: * @self: A #ChimaraIF widget. diff --git a/libchimara/chimara-if.h b/libchimara/chimara-if.h index c0763ee..3bbe06a 100644 --- a/libchimara/chimara-if.h +++ b/libchimara/chimara-if.h @@ -116,7 +116,8 @@ GType chimara_if_get_type(void) G_GNUC_CONST; GtkWidget *chimara_if_new(void); void chimara_if_set_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format, ChimaraIFInterpreter interpreter); ChimaraIFInterpreter chimara_if_get_preferred_interpreter(ChimaraIF *self, ChimaraIFFormat format); -gboolean chimara_if_run_game(ChimaraIF *self, const char *gamefile, GError **error); +gboolean chimara_if_run_game(ChimaraIF *self, const char *game_path, GError **error); +gboolean chimara_if_run_game_file(ChimaraIF *self, GFile *game_file, GError **error); ChimaraIFFormat chimara_if_get_format(ChimaraIF *self); ChimaraIFInterpreter chimara_if_get_interpreter(ChimaraIF *self);