| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 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 |
#include <pipewire/keys.h> |
| 12 |
|
| 13 |
#include "script.h" |
| 14 |
|
| 15 |
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting |
| 16 |
WP_LOG_TOPIC (log_topic_lua_scripting, "m-lua-scripting") |
| 17 |
|
| 18 |
void wp_lua_scripting_api_init (lua_State *L); |
| 19 |
|
| 20 |
struct _WpLuaScriptingPlugin |
| 21 |
{ |
| 22 |
WpPlugin parent; |
| 23 |
lua_State *L; |
| 24 |
}; |
| 25 |
|
| 26 |
static int |
| 27 |
wp_lua_scripting_package_loader (lua_State *L) |
| 28 |
{ |
| 29 |
luaL_checktype (L, 2, LUA_TFUNCTION); |
| 30 |
wplua_push_sandbox (L); |
| 31 |
lua_pushvalue (L, 2); |
| 32 |
lua_call (L, 1, 1); |
| 33 |
return 1; |
| 34 |
} |
| 35 |
|
| 36 |
static int |
| 37 |
wp_lua_scripting_package_searcher (lua_State *L) |
| 38 |
{ |
| 39 |
const gchar *name = luaL_checkstring (L, 1); |
| 40 |
g_autoptr (GError) error = NULL; |
| 41 |
g_autofree gchar *filename = g_strdup_printf ("%s.lua", name); |
| 42 |
g_autofree gchar *script = |
| 43 |
wp_base_dirs_find_file (WP_BASE_DIRS_DATA, "scripts/lib", filename); |
| 44 |
|
| 45 |
if (!script) { |
| 46 |
lua_pushliteral (L, "script not found"); |
| 47 |
return 1; |
| 48 |
} |
| 49 |
|
| 50 |
/* 1. loader (function) */ |
| 51 |
lua_pushcfunction (L, wp_lua_scripting_package_loader); |
| 52 |
|
| 53 |
/* 2. loader data (param to 1) */ |
| 54 |
wp_debug ("Executing script %s", script); |
| 55 |
if (!wplua_load_path (L, script, &error)) { |
| 56 |
lua_pop (L, 1); |
| 57 |
lua_pushstring (L, error->message); |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
/* 3. script path */ |
| 62 |
lua_pushstring (L, script); |
| 63 |
return 3; |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
wp_lua_scripting_enable_package_searcher (lua_State *L) |
| 68 |
{ |
| 69 |
/* table.insert(package.searchers, 2, wp_lua_scripting_package_searcher) */ |
| 70 |
lua_getglobal (L, "table"); |
| 71 |
lua_getfield (L, -1, "insert"); |
| 72 |
lua_remove (L, -2); |
| 73 |
lua_getglobal (L, "package"); |
| 74 |
lua_getfield (L, -1, "searchers"); |
| 75 |
lua_remove (L, -2); |
| 76 |
lua_pushinteger (L, 2); |
| 77 |
lua_pushcfunction (L, wp_lua_scripting_package_searcher); |
| 78 |
lua_call (L, 3, 0); |
| 79 |
} |
| 80 |
|
| 81 |
static void wp_lua_scripting_component_loader_init (WpComponentLoaderInterface * iface); |
| 82 |
|
| 83 |
G_DECLARE_FINAL_TYPE (WpLuaScriptingPlugin, wp_lua_scripting_plugin, |
| 84 |
WP, LUA_SCRIPTING_PLUGIN, WpPlugin) |
| 85 |
G_DEFINE_TYPE_WITH_CODE (WpLuaScriptingPlugin, wp_lua_scripting_plugin, |
| 86 |
WP_TYPE_PLUGIN, G_IMPLEMENT_INTERFACE ( |
| 87 |
WP_TYPE_COMPONENT_LOADER, |
| 88 |
wp_lua_scripting_component_loader_init)) |
| 89 |
|
| 90 |
static void |
| 91 |
wp_lua_scripting_plugin_init (WpLuaScriptingPlugin * self) |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
wp_lua_scripting_plugin_enable (WpPlugin * plugin, WpTransition * transition) |
| 97 |
{ |
| 98 |
WpLuaScriptingPlugin * self = WP_LUA_SCRIPTING_PLUGIN (plugin); |
| 99 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (plugin)); |
| 100 |
|
| 101 |
/* init lua engine */ |
| 102 |
self->L = wplua_new (); |
| 103 |
|
| 104 |
lua_pushliteral (self->L, "wireplumber_core"); |
| 105 |
lua_pushlightuserdata (self->L, core); |
| 106 |
lua_settable (self->L, LUA_REGISTRYINDEX); |
| 107 |
|
| 108 |
wp_lua_scripting_api_init (self->L); |
| 109 |
wp_lua_scripting_enable_package_searcher (self->L); |
| 110 |
wplua_enable_sandbox (self->L, WP_LUA_SANDBOX_ISOLATE_ENV); |
| 111 |
|
| 112 |
wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0); |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
wp_lua_scripting_plugin_disable (WpPlugin * plugin) |
| 117 |
{ |
| 118 |
WpLuaScriptingPlugin * self = WP_LUA_SCRIPTING_PLUGIN (plugin); |
| 119 |
|
| 120 |
g_clear_pointer (&self->L, wplua_unref); |
| 121 |
} |
| 122 |
|
| 123 |
static gboolean |
| 124 |
wp_lua_scripting_plugin_supports_type (WpComponentLoader * cl, |
| 125 |
const gchar * type) |
| 126 |
{ |
| 127 |
return g_str_equal (type, "script/lua"); |
| 128 |
} |
| 129 |
|
| 130 |
static gchar * |
| 131 |
find_script (const gchar * script, WpCore *core) |
| 132 |
{ |
| 133 |
g_autoptr (WpProperties) p = wp_core_get_properties (core); |
| 134 |
const gchar *str = wp_properties_get (p, "wireplumber.daemon"); |
| 135 |
gboolean daemon = !g_strcmp0 (str, "true"); |
| 136 |
|
| 137 |
if ((!daemon || g_path_is_absolute (script)) && |
| 138 |
g_file_test (script, G_FILE_TEST_IS_REGULAR)) |
| 139 |
return g_strdup (script); |
| 140 |
|
| 141 |
return wp_base_dirs_find_file (WP_BASE_DIRS_DATA, "scripts", script); |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
wp_lua_scripting_plugin_load (WpComponentLoader * cl, WpCore * core, |
| 146 |
const gchar * component, const gchar * type, WpSpaJson * args, |
| 147 |
GCancellable * cancellable, GAsyncReadyCallback callback, gpointer data) |
| 148 |
{ |
| 149 |
WpLuaScriptingPlugin * self = WP_LUA_SCRIPTING_PLUGIN (cl); |
| 150 |
g_autoptr (GTask) task = task = g_task_new (self, cancellable, callback, data); |
| 151 |
g_autofree gchar *filepath = NULL; |
| 152 |
g_autofree gchar *pluginname = NULL; |
| 153 |
g_autoptr (WpPlugin) script = NULL; |
| 154 |
|
| 155 |
g_task_set_source_tag (task, wp_lua_scripting_plugin_load); |
| 156 |
|
| 157 |
/* make sure the component loader is activated */ |
| 158 |
if (!self->L) { |
| 159 |
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
| 160 |
"Lua script component loader cannot load Lua scripts if not enabled"); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
/* make sure the type is supported */ |
| 165 |
if (!g_str_equal (type, "script/lua")) { |
| 166 |
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
| 167 |
"Could not load script '%s' as its type is not 'script/lua'", |
| 168 |
component); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
/* find the script */ |
| 173 |
filepath = find_script (component, core); |
| 174 |
if (!filepath) { |
| 175 |
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
| 176 |
"Could not locate script '%s'", component); |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
pluginname = g_strdup_printf ("script:%s", component); |
| 181 |
|
| 182 |
script = g_object_new (WP_TYPE_LUA_SCRIPT, |
| 183 |
"core", core, |
| 184 |
"name", pluginname, |
| 185 |
"lua-engine", self->L, |
| 186 |
"filename", filepath, |
| 187 |
"arguments", args, |
| 188 |
NULL); |
| 189 |
|
| 190 |
g_task_return_pointer (task, g_steal_pointer (&script), g_object_unref); |
| 191 |
} |
| 192 |
|
| 193 |
static GObject * |
| 194 |
wp_lua_scripting_plugin_load_finish (WpComponentLoader * self, |
| 195 |
GAsyncResult * res, GError ** error) |
| 196 |
{ |
| 197 |
g_return_val_if_fail ( |
| 198 |
g_async_result_is_tagged (res, wp_lua_scripting_plugin_load), NULL); |
| 199 |
|
| 200 |
return g_task_propagate_pointer (G_TASK (res), error); |
| 201 |
} |
| 202 |
|
| 203 |
static void |
| 204 |
wp_lua_scripting_plugin_class_init (WpLuaScriptingPluginClass * klass) |
| 205 |
{ |
| 206 |
WpPluginClass *plugin_class = (WpPluginClass *) klass; |
| 207 |
|
| 208 |
plugin_class->enable = wp_lua_scripting_plugin_enable; |
| 209 |
plugin_class->disable = wp_lua_scripting_plugin_disable; |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
wp_lua_scripting_component_loader_init (WpComponentLoaderInterface * iface) |
| 214 |
{ |
| 215 |
iface->supports_type = wp_lua_scripting_plugin_supports_type; |
| 216 |
iface->load = wp_lua_scripting_plugin_load; |
| 217 |
iface->load_finish = wp_lua_scripting_plugin_load_finish; |
| 218 |
} |
| 219 |
|
| 220 |
WP_PLUGIN_EXPORT GObject * |
| 221 |
wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error) |
| 222 |
{ |
| 223 |
return G_OBJECT (g_object_new (wp_lua_scripting_plugin_get_type (), |
| 224 |
"name", "lua-scripting", |
| 225 |
"core", core, |
| 226 |
NULL)); |
| 227 |
} |