| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include <wplua/wplua.h> |
| 11 |
|
| 12 |
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting |
| 13 |
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting) |
| 14 |
|
| 15 |
struct _WpRequireApiTransition |
| 16 |
{ |
| 17 |
WpTransition parent; |
| 18 |
GPtrArray *apis; |
| 19 |
guint pending_plugins; |
| 20 |
}; |
| 21 |
|
| 22 |
enum { |
| 23 |
STEP_LOAD_PLUGINS = WP_TRANSITION_STEP_CUSTOM_START, |
| 24 |
}; |
| 25 |
|
| 26 |
G_DECLARE_FINAL_TYPE (WpRequireApiTransition, wp_require_api_transition, |
| 27 |
WP, REQUIRE_API_TRANSITION, WpTransition) |
| 28 |
G_DEFINE_TYPE (WpRequireApiTransition, wp_require_api_transition, WP_TYPE_TRANSITION) |
| 29 |
|
| 30 |
static void |
| 31 |
wp_require_api_transition_init (WpRequireApiTransition * self) |
| 32 |
{ |
| 33 |
self->apis = g_ptr_array_new_with_free_func (g_free); |
| 34 |
} |
| 35 |
|
| 36 |
static void |
| 37 |
wp_require_api_transition_finalize (GObject * object) |
| 38 |
{ |
| 39 |
WpRequireApiTransition *self = WP_REQUIRE_API_TRANSITION (object); |
| 40 |
|
| 41 |
g_clear_pointer (&self->apis, g_ptr_array_unref); |
| 42 |
|
| 43 |
G_OBJECT_CLASS (wp_require_api_transition_parent_class)->finalize (object); |
| 44 |
} |
| 45 |
|
| 46 |
static guint |
| 47 |
wp_require_api_transition_get_next_step (WpTransition * transition, guint step) |
| 48 |
{ |
| 49 |
WpRequireApiTransition *self = WP_REQUIRE_API_TRANSITION (transition); |
| 50 |
|
| 51 |
switch (step) { |
| 52 |
case WP_TRANSITION_STEP_NONE: return STEP_LOAD_PLUGINS; |
| 53 |
case STEP_LOAD_PLUGINS: |
| 54 |
return (self->pending_plugins > 0) ? |
| 55 |
STEP_LOAD_PLUGINS : WP_TRANSITION_STEP_NONE; |
| 56 |
default: |
| 57 |
g_return_val_if_reached (WP_TRANSITION_STEP_ERROR); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
on_plugin_loaded (WpCore * core, GAsyncResult * res, |
| 63 |
WpRequireApiTransition *self) |
| 64 |
{ |
| 65 |
GError *error = NULL; |
| 66 |
|
| 67 |
if (!wp_core_load_component_finish (core, res, &error)) { |
| 68 |
wp_transition_return_error (WP_TRANSITION (self), error); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
--self->pending_plugins; |
| 73 |
wp_transition_advance (WP_TRANSITION (self)); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
wp_require_api_transition_execute_step (WpTransition * transition, guint step) |
| 78 |
{ |
| 79 |
WpRequireApiTransition *self = WP_REQUIRE_API_TRANSITION (transition); |
| 80 |
WpCore *core = wp_transition_get_source_object (transition); |
| 81 |
|
| 82 |
switch (step) { |
| 83 |
case STEP_LOAD_PLUGINS: |
| 84 |
wp_debug_object (self, "Loading plugins..."); |
| 85 |
|
| 86 |
for (guint i = 0; i < self->apis->len; i++) { |
| 87 |
const gchar *api_name = g_ptr_array_index (self->apis, i); |
| 88 |
g_autoptr (WpPlugin) plugin = wp_plugin_find (core, api_name); |
| 89 |
if (!plugin) { |
| 90 |
gchar module_name[50]; |
| 91 |
g_snprintf (module_name, sizeof (module_name), |
| 92 |
"libwireplumber-module-%s", api_name); |
| 93 |
|
| 94 |
self->pending_plugins++; |
| 95 |
wp_core_load_component (core, module_name, "module", NULL, NULL, NULL, |
| 96 |
(GAsyncReadyCallback) on_plugin_loaded, self); |
| 97 |
} |
| 98 |
} |
| 99 |
wp_transition_advance (transition); |
| 100 |
break; |
| 101 |
|
| 102 |
case WP_TRANSITION_STEP_ERROR: |
| 103 |
break; |
| 104 |
|
| 105 |
default: |
| 106 |
g_assert_not_reached (); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
wp_require_api_transition_class_init (WpRequireApiTransitionClass * klass) |
| 112 |
{ |
| 113 |
GObjectClass * object_class = (GObjectClass *) klass; |
| 114 |
WpTransitionClass * transition_class = (WpTransitionClass *) klass; |
| 115 |
|
| 116 |
object_class->finalize = wp_require_api_transition_finalize; |
| 117 |
|
| 118 |
transition_class->get_next_step = wp_require_api_transition_get_next_step; |
| 119 |
transition_class->execute_step = wp_require_api_transition_execute_step; |
| 120 |
} |
| 121 |
|
| 122 |
static void |
| 123 |
on_require_api_transition_done (WpCore * core, GAsyncResult * res, gpointer data) |
| 124 |
{ |
| 125 |
g_autoptr (GClosure) closure = data; |
| 126 |
g_autoptr (GError) error = NULL; |
| 127 |
|
| 128 |
if (!wp_transition_finish (res, &error)) { |
| 129 |
wp_warning ("Core.require_api failed: %s", error->message); |
| 130 |
wp_core_idle_add (core, NULL, G_SOURCE_FUNC (core_disconnect), core, NULL); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
WpRequireApiTransition *t = WP_REQUIRE_API_TRANSITION (res); |
| 135 |
g_autoptr (GArray) params = g_array_new (FALSE, TRUE, sizeof (GValue)); |
| 136 |
|
| 137 |
g_array_set_clear_func (params, (GDestroyNotify) g_value_unset); |
| 138 |
g_array_set_size (params, t->apis->len); |
| 139 |
|
| 140 |
for (guint i = 0; i < t->apis->len; i++) { |
| 141 |
const gchar *api_name = g_ptr_array_index (t->apis, i); |
| 142 |
g_autoptr (WpPlugin) plugin = wp_plugin_find (core, api_name); |
| 143 |
g_value_init_from_instance (&g_array_index (params, GValue, i), plugin); |
| 144 |
} |
| 145 |
|
| 146 |
g_closure_invoke (closure, NULL, |
| 147 |
params->len, (const GValue *) params->data, NULL); |
| 148 |
g_closure_invalidate (closure); |
| 149 |
} |
| 150 |
|
| 151 |
static int |
| 152 |
wp_require_api_transition_new_from_lua (lua_State *L, WpCore * core) |
| 153 |
{ |
| 154 |
int n_args = lua_gettop (L); |
| 155 |
|
| 156 |
wp_info("n_args = %d", n_args); |
| 157 |
|
| 158 |
for (int i = 1; i < n_args; i++) |
| 159 |
luaL_checktype (L, i, LUA_TSTRING); |
| 160 |
luaL_checktype (L, n_args, LUA_TFUNCTION); |
| 161 |
|
| 162 |
GClosure *closure = wplua_function_to_closure (L, n_args); |
| 163 |
g_closure_ref (closure); |
| 164 |
g_closure_sink (closure); |
| 165 |
|
| 166 |
WpRequireApiTransition *t = (WpRequireApiTransition *) |
| 167 |
wp_transition_new (wp_require_api_transition_get_type (), core, NULL, |
| 168 |
(GAsyncReadyCallback) on_require_api_transition_done, closure); |
| 169 |
|
| 170 |
for (int i = 1; i < n_args; i++) { |
| 171 |
const char * api_name = lua_tostring (L, i); |
| 172 |
g_ptr_array_add (t->apis, g_strdup_printf ("%s-api", api_name)); |
| 173 |
} |
| 174 |
|
| 175 |
wp_transition_advance (WP_TRANSITION (t)); |
| 176 |
return 0; |
| 177 |
} |