scripts: add suspend-node.lua to replace m-node-suspension

... plus the necessary API for it to work
This commit is contained in:
George Kiagiadakis
2021-02-05 12:02:42 +02:00
parent 7b37b31af9
commit d94b0a2f0c
3 changed files with 83 additions and 5 deletions

View File

@@ -31,6 +31,21 @@ get_wp_export_core (lua_State *L)
return lua_touserdata (L, -1); return lua_touserdata (L, -1);
} }
/* GSource */
static int
source_destroy (lua_State *L)
{
GSource *source = wplua_checkboxed (L, 1, G_TYPE_SOURCE);
g_source_destroy (source);
return 0;
}
static const luaL_Reg source_methods[] = {
{ "destroy", source_destroy },
{ NULL, NULL }
};
/* WpCore */ /* WpCore */
static int static int
@@ -58,20 +73,24 @@ core_get_info (lua_State *L)
static int static int
core_idle_add (lua_State *L) core_idle_add (lua_State *L)
{ {
GSource *source = NULL;
luaL_checktype (L, 1, LUA_TFUNCTION); luaL_checktype (L, 1, LUA_TFUNCTION);
wp_core_idle_add_closure (get_wp_core (L), NULL, wp_core_idle_add_closure (get_wp_core (L), &source,
wplua_function_to_closure (L, 1)); wplua_function_to_closure (L, 1));
return 0; wplua_pushboxed (L, G_TYPE_SOURCE, source);
return 1;
} }
static int static int
core_timeout_add (lua_State *L) core_timeout_add (lua_State *L)
{ {
GSource *source = NULL;
lua_Integer timeout_ms = luaL_checkinteger (L, 1); lua_Integer timeout_ms = luaL_checkinteger (L, 1);
luaL_checktype (L, 2, LUA_TFUNCTION); luaL_checktype (L, 2, LUA_TFUNCTION);
wp_core_timeout_add_closure (get_wp_core (L), NULL, timeout_ms, wp_core_timeout_add_closure (get_wp_core (L), &source, timeout_ms,
wplua_function_to_closure (L, 2)); wplua_function_to_closure (L, 2));
return 0; wplua_pushboxed (L, G_TYPE_SOURCE, source);
return 1;
} }
static void static void
@@ -800,7 +819,17 @@ node_new (lua_State *L)
return 1; return 1;
} }
static int
node_send_command (lua_State *L)
{
WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
const char *command = luaL_checkstring (L, 2);
wp_node_send_command (node, command);
return 0;
}
static const luaL_Reg node_methods[] = { static const luaL_Reg node_methods[] = {
{ "send_command", node_send_command },
{ NULL, NULL } { NULL, NULL }
}; };
@@ -968,6 +997,8 @@ wp_lua_scripting_api_init (lua_State *L)
wp_lua_scripting_pod_init (L); wp_lua_scripting_pod_init (L);
wplua_register_type_methods (L, G_TYPE_SOURCE,
NULL, source_methods);
wplua_register_type_methods (L, WP_TYPE_OBJECT, wplua_register_type_methods (L, WP_TYPE_OBJECT,
NULL, object_methods); NULL, object_methods);
wplua_register_type_methods (L, WP_TYPE_PROXY, wplua_register_type_methods (L, WP_TYPE_PROXY,

View File

@@ -57,7 +57,7 @@ load_module("si-standard-link")
load_monitor("v4l2") load_monitor("v4l2")
-- Automatically suspends idle nodes after 3 seconds -- Automatically suspends idle nodes after 3 seconds
load_module("node-suspension") load_script("suspend-node.lua")
-- Automatically sets device profiles to 'On' -- Automatically sets device profiles to 'On'
load_module("device-activation") load_module("device-activation")

View File

@@ -0,0 +1,47 @@
-- WirePlumber
--
-- Copyright © 2021 Collabora Ltd.
-- @author George Kiagiadakis <george.kiagiadakis@collabora.com>
--
-- SPDX-License-Identifier: MIT
om = ObjectManager {
Interest { type = "node",
Constraint { "media.class", "matches", "Audio/*" }
},
Interest { type = "node",
Constraint { "media.class", "matches", "Video/*" }
},
}
sources = {}
om:connect("object-added", function (om, node)
node:connect("state-changed", function (node, old_state, cur_state)
-- Always clear the current source if any
local id = node["bound-id"]
if sources[id] then
sources[id]:destroy()
sources[id] = nil
end
-- Add a timeout source if idle for at least 3 seconds
if cur_state == "idle" then
sources[id] = Core.timeout_add(3000, function()
-- Suspend the node
Log.info(node, "was idle for a while; suspending ...")
node:send_command("Suspend")
-- Unref the source
sources[id] = nil
-- false (== G_SOURCE_REMOVE) destroys the source so that this
-- function does not get fired again after 3 seconds
return false
end)
end
end)
end)
om:activate()