2021-01-31 23:29:55 +02:00
|
|
|
/* WirePlumber
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2021 Collabora Ltd.
|
|
|
|
|
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "component-loader.h"
|
2023-05-26 13:49:40 +03:00
|
|
|
#include "log.h"
|
|
|
|
|
#include "error.h"
|
2021-01-31 23:29:55 +02:00
|
|
|
#include "private/registry.h"
|
|
|
|
|
|
log: implement a log topics system, like pipewire
The intention is to make checks for enabled log topics faster.
Every topic has its own structure that is statically defined in the file
where the logs are printed from. The structure is initialized transparently
when it is first used and it contains all the log level flags for the levels
that this topic should print messages. It is then checked on the wp_log()
macro before printing the message.
Topics from SPA/PipeWire are also handled natively, so messages are printed
directly without checking if the topic is enabled, since the PipeWire and SPA
macros do the checking themselves.
Messages coming from GLib are checked inside the handler.
An internal WpLogFields object is used to manage the state of each log
message, populating all the fields appropriately from the place they
are coming from (wp_log, spa_log, glib log), formatting the message and
then printing it. For printing to the journald, we still use the glib
message handler, converting all the needed fields to GLogField on demand.
That message handler does not do any checks for the topic or the level, so
we can just call it to send the message.
2023-05-16 11:51:29 +03:00
|
|
|
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-comp-loader")
|
|
|
|
|
|
2021-05-21 18:40:43 +03:00
|
|
|
/*! \defgroup wpcomponentloader WpComponentLoader */
|
|
|
|
|
/*!
|
|
|
|
|
* \struct WpComponentLoader
|
|
|
|
|
*
|
2023-05-25 19:20:40 +03:00
|
|
|
* An interface that provides the ability to load components.
|
2021-05-21 18:40:43 +03:00
|
|
|
*
|
|
|
|
|
* Components can be:
|
|
|
|
|
* - WirePlumber modules (libraries that provide WpPlugin and WpSiFactory objects)
|
|
|
|
|
* - Scripts (ex. lua scripts)
|
|
|
|
|
*
|
|
|
|
|
* The WirePlumber library provides built-in support for loading WirePlumber
|
2023-04-25 16:06:32 +03:00
|
|
|
* modules, without a component loader. For other kinds of components,
|
|
|
|
|
* a component loader is meant to be provided in by some WirePlumber module.
|
|
|
|
|
* For Lua scripts specifically, a component loader is provided by the lua
|
|
|
|
|
* scripting module.
|
2021-05-21 18:40:43 +03:00
|
|
|
*/
|
|
|
|
|
|
2023-05-25 19:20:40 +03:00
|
|
|
G_DEFINE_INTERFACE (WpComponentLoader, wp_component_loader, G_TYPE_OBJECT)
|
2021-01-31 23:29:55 +02:00
|
|
|
|
|
|
|
|
static void
|
2023-05-25 19:20:40 +03:00
|
|
|
wp_component_loader_default_init (WpComponentLoaderInterface * iface)
|
2021-01-31 23:29:55 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
find_component_loader_func (gpointer cl, gpointer type)
|
|
|
|
|
{
|
|
|
|
|
if (WP_IS_COMPONENT_LOADER (cl) &&
|
2023-05-25 19:20:40 +03:00
|
|
|
(WP_COMPONENT_LOADER_GET_IFACE (cl)->supports_type (
|
2021-01-31 23:29:55 +02:00
|
|
|
WP_COMPONENT_LOADER (cl), (const gchar *) type)))
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static WpComponentLoader *
|
|
|
|
|
wp_component_loader_find (WpCore * core, const gchar * type)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (WP_IS_CORE (core), NULL);
|
|
|
|
|
GObject *c = wp_registry_find_object (wp_core_get_registry (core),
|
|
|
|
|
(GEqualFunc) find_component_loader_func, type);
|
|
|
|
|
return c ? WP_COMPONENT_LOADER (c) : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 13:11:14 -05:00
|
|
|
static void
|
2023-05-26 13:04:10 +03:00
|
|
|
wp_component_loader_load (WpComponentLoader * self, WpCore * core,
|
|
|
|
|
const gchar * component, const gchar * type, WpSpaJson * args,
|
|
|
|
|
GCancellable * cancellable, GAsyncReadyCallback callback, gpointer data)
|
2023-02-09 13:11:14 -05:00
|
|
|
{
|
2023-05-26 13:04:10 +03:00
|
|
|
WP_COMPONENT_LOADER_GET_IFACE (self)->load (self, core, component, type,
|
|
|
|
|
args, cancellable, callback, data);
|
2023-02-09 13:11:14 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-26 19:21:51 +03:00
|
|
|
static GObject *
|
|
|
|
|
wp_component_loader_load_finish (WpComponentLoader * self, GAsyncResult * res,
|
|
|
|
|
GError ** error)
|
|
|
|
|
{
|
|
|
|
|
return WP_COMPONENT_LOADER_GET_IFACE (self)->load_finish (self, res, error);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 21:22:48 +03:00
|
|
|
static void
|
|
|
|
|
on_object_activated (WpObject * object, GAsyncResult * res, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (GTask) task = G_TASK (data);
|
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
|
|
|
|
|
|
if (!wp_object_activate_finish (object, res, &error)) {
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 21:07:15 +03:00
|
|
|
g_task_return_boolean (task, TRUE);
|
2023-05-26 21:22:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
on_component_loader_load_done (WpComponentLoader * cl, GAsyncResult * res,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (GTask) task = G_TASK (data);
|
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
|
g_autoptr (GObject) o = NULL;
|
|
|
|
|
WpCore *core = g_task_get_source_object (task);
|
|
|
|
|
|
|
|
|
|
o = wp_component_loader_load_finish (cl, res, &error);
|
|
|
|
|
if (!o) {
|
|
|
|
|
g_task_return_error (task, g_steal_pointer (&error));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wp_trace_object (cl, "loaded object " WP_OBJECT_FORMAT, WP_OBJECT_ARGS (o));
|
|
|
|
|
|
|
|
|
|
/* store object in the registry */
|
|
|
|
|
wp_registry_register_object (wp_core_get_registry (core), g_object_ref (o));
|
|
|
|
|
|
|
|
|
|
if (WP_IS_OBJECT (o)) {
|
|
|
|
|
/* WpObject needs to be activated */
|
|
|
|
|
wp_object_activate (WP_OBJECT (o), WP_OBJECT_FEATURES_ALL, NULL,
|
|
|
|
|
(GAsyncReadyCallback) on_object_activated, g_steal_pointer (&task));
|
|
|
|
|
} else {
|
2023-05-28 21:07:15 +03:00
|
|
|
g_task_return_boolean (task, TRUE);
|
2023-05-26 21:22:48 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 17:54:58 +03:00
|
|
|
/*!
|
2021-05-21 18:40:43 +03:00
|
|
|
* \brief Loads the specified \a component on \a self
|
|
|
|
|
*
|
|
|
|
|
* The \a type will determine which component loader to use. The following types
|
|
|
|
|
* are built-in and will always work without a component loader:
|
|
|
|
|
* - "module" - Loads a WirePlumber module
|
2021-01-31 23:29:55 +02:00
|
|
|
*
|
2021-05-21 18:40:43 +03:00
|
|
|
* \ingroup wpcomponentloader
|
|
|
|
|
* \param self the core
|
|
|
|
|
* \param component the module name or file name
|
|
|
|
|
* \param type the type of the component
|
2023-05-25 18:29:58 +03:00
|
|
|
* \param args (transfer none)(nullable): additional arguments for the component,
|
|
|
|
|
* expected to be a JSON object
|
2023-05-26 13:04:10 +03:00
|
|
|
* \param cancellable (nullable): optional GCancellable
|
2023-02-09 13:11:14 -05:00
|
|
|
* \param callback (scope async): the callback to call when the operation is done
|
|
|
|
|
* \param data (closure): data to pass to \a callback
|
2021-01-31 23:29:55 +02:00
|
|
|
*/
|
2023-02-09 13:11:14 -05:00
|
|
|
void
|
2021-01-31 23:29:55 +02:00
|
|
|
wp_core_load_component (WpCore * self, const gchar * component,
|
2023-05-26 13:04:10 +03:00
|
|
|
const gchar * type, WpSpaJson * args, GCancellable * cancellable,
|
|
|
|
|
GAsyncReadyCallback callback, gpointer data)
|
2021-01-31 23:29:55 +02:00
|
|
|
{
|
2023-02-09 13:11:14 -05:00
|
|
|
g_autoptr (GTask) task = NULL;
|
2023-05-26 13:04:10 +03:00
|
|
|
g_autoptr (WpComponentLoader) cl = NULL;
|
2023-02-09 13:11:14 -05:00
|
|
|
|
2023-05-26 21:22:48 +03:00
|
|
|
task = g_task_new (self, cancellable, callback, data);
|
|
|
|
|
g_task_set_source_tag (task, wp_core_load_component);
|
|
|
|
|
|
2023-05-26 13:49:40 +03:00
|
|
|
/* find a component loader for that type and load the component */
|
2023-05-26 13:04:10 +03:00
|
|
|
cl = wp_component_loader_find (self, type);
|
|
|
|
|
if (!cl) {
|
2023-02-09 13:11:14 -05:00
|
|
|
g_task_return_new_error (task, WP_DOMAIN_LIBRARY,
|
|
|
|
|
WP_LIBRARY_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
"No component loader was found for components of type '%s'", type);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 21:22:48 +03:00
|
|
|
wp_debug_object (self, "load '%s', type '%s', loader " WP_OBJECT_FORMAT,
|
|
|
|
|
component, type, WP_OBJECT_ARGS (cl));
|
|
|
|
|
|
2023-05-26 13:04:10 +03:00
|
|
|
wp_component_loader_load (cl, self, component, type, args, cancellable,
|
2023-05-26 21:22:48 +03:00
|
|
|
(GAsyncReadyCallback) on_component_loader_load_done, g_object_ref (task));
|
2023-02-09 13:11:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Finishes the operation started by wp_core_load_component().
|
|
|
|
|
* This is meant to be called in the callback that was passed to that method.
|
|
|
|
|
*
|
|
|
|
|
* \ingroup wpcomponentloader
|
|
|
|
|
* \param self the component loader object
|
|
|
|
|
* \param res the async result
|
|
|
|
|
* \param error (out) (optional): the operation's error, if it occurred
|
2023-05-28 21:07:15 +03:00
|
|
|
* \returns TRUE if the requested component was loaded, FALSE otherwise
|
2023-02-09 13:11:14 -05:00
|
|
|
*/
|
2023-05-28 21:07:15 +03:00
|
|
|
gboolean
|
2023-02-09 13:11:14 -05:00
|
|
|
wp_core_load_component_finish (WpCore * self, GAsyncResult * res,
|
|
|
|
|
GError ** error)
|
|
|
|
|
{
|
2023-05-26 21:22:48 +03:00
|
|
|
g_return_val_if_fail (
|
2023-05-28 21:07:15 +03:00
|
|
|
g_async_result_is_tagged (res, wp_core_load_component), FALSE);
|
2023-05-26 19:21:51 +03:00
|
|
|
|
2023-05-28 21:07:15 +03:00
|
|
|
return g_task_propagate_boolean (G_TASK (res), error);
|
2021-01-31 23:29:55 +02:00
|
|
|
}
|