diff --git a/core/cog-platform.c b/core/cog-platform.c index ba651e54..bd3cdb87 100644 --- a/core/cog-platform.c +++ b/core/cog-platform.c @@ -148,29 +148,23 @@ cog_platform_create_im_context(CogPlatform *platform) } /** - * cog_platform_configure: (constructor) - * @name: (nullable): Name of the platform implementation to use. + * cog_platform_configure: * @params: (nullable): Parameters passed to the platform implementation. * @env_prefix: (nullable): Prefix for environment variables to check. * @shell: The shell that the platform will be configured for. * @error: Location where to store errors, if any. * - * Creates and configures a platform implementation. + * Configures a platform implementation. * - * Search and create a platform implementation with the given @name, + * Search a platform implementation with the given @name, * configuring it with the passed @params for use with a @shell. * * If the @env_prefix is non-%NULL, then the environment variable - * `_PLATFORM_NAME` can be used to set the platform name - * when @name is %NULL, and the variable `_PLATFORM_PARAMS` + * `_PLATFORM_PARAMS` * can be used to set the configuration parameters when @params is %NULL. * Environment variables will *not* be used if %NULL is passed as the * prefix. * - * If both @name is %NULL and the `_PLATFORM_NAME` variable - * not defined, then the platform implementation will be chosen automatically - * among the available ones. - * * Note that [id@cog_modules_add_directory] may be used beforehand to * configure where to search for available platform plug-ins. * @@ -179,28 +173,26 @@ cog_platform_create_im_context(CogPlatform *platform) * Since: 0.18 */ CogPlatform * -cog_platform_configure(const char *name, const char *params, const char *env_prefix, CogShell *shell, GError **error) +cog_platform_configure(const char *params, const char *env_prefix, CogShell *shell, GError **error) { - g_return_val_if_fail(!default_platform, default_platform); + CogPlatform *platform = cog_platform_get_default(); + if (!platform) { + g_set_error_literal(error, + COG_PLATFORM_WPE_ERROR, + COG_PLATFORM_WPE_ERROR_INIT, + "Failed to configure missing platform"); + return NULL; + } - g_autofree char *platform_name = g_strdup(name); g_autofree char *platform_params = g_strdup(params); if (env_prefix) { - if (!platform_name) { - g_autofree char *name_var = g_strconcat(env_prefix, "_PLATFORM_NAME", NULL); - platform_name = g_strdup(g_getenv(name_var)); - } if (!params) { g_autofree char *params_var = g_strconcat(env_prefix, "_PLATFORM_PARAMS", NULL); platform_params = g_strdup(g_getenv(params_var)); } } - g_debug("%s: name '%s', params '%s'", G_STRFUNC, platform_name, platform_params); - - CogPlatform *platform = cog_platform_new(platform_name, error); - if (!platform) - return NULL; + g_debug("%s: params '%s'", G_STRFUNC, platform_params); if (!cog_platform_setup(platform, shell, platform_params ?: "", error)) { g_object_unref(platform); @@ -212,3 +204,53 @@ cog_platform_configure(const char *name, const char *params, const char *env_pre g_assert(platform == default_platform); return platform; } + +/** + * cog_platform_create: (constructor) + * @name: (nullable): Name of the platform implementation to use. + * @env_prefix: (nullable): Prefix for environment variables to check. + * @error: Location where to store errors, if any. + * + * Creates a platform implementation. + * + * Create a platform implementation with the given @name, + * + * If the @env_prefix is non-%NULL, then the environment variable + * `_PLATFORM_NAME` can be used to set the platform name + * when @name is %NULL. + * Environment variables will *not* be used if %NULL is passed as the + * prefix. + * + * If both @name is %NULL and the `_PLATFORM_NAME` variable + * not defined, then the platform implementation will be chosen automatically + * among the available ones. + * + * Note that [id@cog_modules_add_directory] may be used beforehand to + * configure where to search for available platform plug-ins. + * + * Returns: (transfer full): The created platform. + * + * Since: 0.20 + */ +CogPlatform * +cog_platform_create(const char *name, const char *env_prefix, GError **error) +{ + g_autofree char *platform_name = g_strdup(name); + + if (env_prefix) { + if (!platform_name) { + g_autofree char *name_var = g_strconcat(env_prefix, "_PLATFORM_NAME", NULL); + platform_name = g_strdup(g_getenv(name_var)); + } + } + g_debug("%s: name '%s'", G_STRFUNC, platform_name); + + CogPlatform *platform = cog_platform_new(platform_name, error); + if (!platform) { + g_set_error_literal(error, COG_PLATFORM_WPE_ERROR, COG_PLATFORM_WPE_ERROR_INIT, "Failed creating platform"); + return NULL; + } + + g_assert(platform == default_platform); + return platform; +} diff --git a/core/cog-platform.h b/core/cog-platform.h index 544de246..9e730c0d 100644 --- a/core/cog-platform.h +++ b/core/cog-platform.h @@ -71,7 +71,8 @@ COG_API WebKitInputMethodContext *cog_platform_create_im_context (CogPlatform *platform); COG_API CogPlatform * -cog_platform_configure(const char *name, const char *params, const char *env_prefix, CogShell *shell, GError **error); +cog_platform_configure(const char *params, const char *env_prefix, CogShell *shell, GError **error); +COG_API CogPlatform *cog_platform_create(const char *name, const char *env_prefix, GError **error); G_END_DECLS diff --git a/examples/viewport.c b/examples/viewport.c index 69053769..de986971 100644 --- a/examples/viewport.c +++ b/examples/viewport.c @@ -37,10 +37,11 @@ main(int argc, char *argv[]) cog_modules_add_directory(g_getenv("COG_MODULEDIR") ?: COG_MODULEDIR); g_autoptr(GError) error = NULL; - g_autoptr(CogShell) shell = cog_shell_new(g_get_prgname(), FALSE); - g_autoptr(CogPlatform) platform = cog_platform_configure(NULL, NULL, "COG", shell, &error); + g_autoptr(CogPlatform) platform = cog_platform_create(NULL, "COG", &error); if (!platform) g_error("Cannot configure platform: %s", error->message); + g_autoptr(CogShell) shell = cog_shell_new(g_get_prgname(), FALSE); + cog_platform_configure(NULL, "COG", shell, &error); g_autoptr(GMainLoop) loop = g_main_loop_new(NULL, FALSE); diff --git a/launcher/cog-launcher.c b/launcher/cog-launcher.c index 02375aab..6fa7473d 100644 --- a/launcher/cog-launcher.c +++ b/launcher/cog-launcher.c @@ -237,11 +237,9 @@ cog_launcher_create_view(CogLauncher *self, CogShell *shell) webkit_web_context_set_cache_model(web_context, WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER); } - g_autoptr(GError) error = NULL; - CogPlatform *platform = - cog_platform_configure(s_options.platform_name, s_options.platform_params, "COG", shell, &error); + CogPlatform *platform = cog_platform_get_default(); if (!platform) - g_error("Cannot instantiate a platform: %s", error->message); + g_error("Cannot find a platform available"); #if HAVE_WEBKIT_AUTOPLAY WebKitWebsitePolicies *website_policies = @@ -331,6 +329,10 @@ cog_launcher_startup(GApplication *application) g_application_hold(application); CogLauncher *self = COG_LAUNCHER(application); + + g_autoptr(GError) error = NULL; + CogPlatform *platform = cog_platform_create(s_options.platform_name, "COG", &error); + self->shell = g_object_new( COG_TYPE_SHELL, "name", g_get_prgname(), "automated", self->automated, "web-settings", self->web_settings, #if !COG_USE_WPE2 @@ -340,6 +342,10 @@ cog_launcher_startup(GApplication *application) "web-memory-settings", self->web_mem_settings, "network-memory-settings", self->net_mem_settings, #endif /* COG_HAVE_MEM_PRESSURE */ NULL); + + if (platform != cog_platform_configure(s_options.platform_params, "COG", self->shell, &error)) + g_error("Cannot configure the platform: %s", error->message); + g_signal_connect_swapped(self->shell, "create-view", G_CALLBACK(cog_launcher_create_view), self); g_signal_connect(self->shell, "notify::web-view", G_CALLBACK(on_notify_web_view), self);