scripts: add suspend-node.lua to replace m-node-suspension
... plus the necessary API for it to work
This commit is contained in:
@@ -31,6 +31,21 @@ get_wp_export_core (lua_State *L)
|
||||
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 */
|
||||
|
||||
static int
|
||||
@@ -58,20 +73,24 @@ core_get_info (lua_State *L)
|
||||
static int
|
||||
core_idle_add (lua_State *L)
|
||||
{
|
||||
GSource *source = NULL;
|
||||
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));
|
||||
return 0;
|
||||
wplua_pushboxed (L, G_TYPE_SOURCE, source);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
core_timeout_add (lua_State *L)
|
||||
{
|
||||
GSource *source = NULL;
|
||||
lua_Integer timeout_ms = luaL_checkinteger (L, 1);
|
||||
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));
|
||||
return 0;
|
||||
wplua_pushboxed (L, G_TYPE_SOURCE, source);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -800,7 +819,17 @@ node_new (lua_State *L)
|
||||
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[] = {
|
||||
{ "send_command", node_send_command },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
@@ -968,6 +997,8 @@ wp_lua_scripting_api_init (lua_State *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,
|
||||
NULL, object_methods);
|
||||
wplua_register_type_methods (L, WP_TYPE_PROXY,
|
||||
|
@@ -57,7 +57,7 @@ load_module("si-standard-link")
|
||||
load_monitor("v4l2")
|
||||
|
||||
-- Automatically suspends idle nodes after 3 seconds
|
||||
load_module("node-suspension")
|
||||
load_script("suspend-node.lua")
|
||||
|
||||
-- Automatically sets device profiles to 'On'
|
||||
load_module("device-activation")
|
||||
|
47
src/scripts/suspend-node.lua
Normal file
47
src/scripts/suspend-node.lua
Normal 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()
|
Reference in New Issue
Block a user