lua/api: improve getting optional Interest arguments
Add the ability to construct an Interest argument without using the Interest keyword, so instead of: om:lookup(Interest { type = "session" }) we can now write: om:lookup { type = "session" } ... and also remove lookup_filtered from ObjectManager
This commit is contained in:
@@ -462,17 +462,17 @@ object_interest_new_add_constraint (lua_State *L, GType type,
|
||||
}
|
||||
|
||||
static int
|
||||
object_interest_new (lua_State *L)
|
||||
object_interest_new_index (lua_State *L, int idx)
|
||||
{
|
||||
WpObjectInterest *interest = NULL;
|
||||
GType type = 0;
|
||||
gchar *typestr;
|
||||
|
||||
luaL_checktype (L, 1, LUA_TTABLE);
|
||||
luaL_checktype (L, idx, LUA_TTABLE);
|
||||
|
||||
/* type = "string" -> required */
|
||||
lua_pushliteral (L, "type");
|
||||
if (lua_gettable (L, -2) != LUA_TSTRING)
|
||||
if (lua_gettable (L, idx) != LUA_TSTRING)
|
||||
luaL_error (L, "Interest: expected 'type' as string");
|
||||
|
||||
/* "device" -> "WpDevice" */
|
||||
@@ -492,7 +492,7 @@ object_interest_new (lua_State *L)
|
||||
|
||||
/* add constraints */
|
||||
lua_pushnil (L);
|
||||
while (lua_next (L, 1)) {
|
||||
while (lua_next (L, idx)) {
|
||||
/* if the key isn't "type" */
|
||||
if (!(lua_type (L, -2) == LUA_TSTRING &&
|
||||
!g_strcmp0 ("type", lua_tostring (L, -2))))
|
||||
@@ -503,6 +503,12 @@ object_interest_new (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
object_interest_new (lua_State *L)
|
||||
{
|
||||
return object_interest_new_index (L, 1);
|
||||
}
|
||||
|
||||
static int
|
||||
object_interest_matches (lua_State *L)
|
||||
{
|
||||
@@ -527,6 +533,20 @@ static const luaL_Reg object_interest_methods[] = {
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static WpObjectInterest *
|
||||
get_optional_object_interest (lua_State *L, int idx)
|
||||
{
|
||||
if (lua_isnil (L, idx))
|
||||
return NULL;
|
||||
else if (lua_isuserdata (L, idx))
|
||||
return wplua_checkboxed (L, idx, WP_TYPE_OBJECT_INTEREST);
|
||||
else if (lua_istable (L, idx)) {
|
||||
object_interest_new_index (L, idx);
|
||||
return wplua_toboxed (L, -1);
|
||||
} else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* WpObjectManager */
|
||||
|
||||
static int
|
||||
@@ -574,17 +594,11 @@ static int
|
||||
object_manager_iterate (lua_State *L)
|
||||
{
|
||||
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
|
||||
WpIterator *it = wp_object_manager_new_iterator (om);
|
||||
return push_wpiterator (L, it);
|
||||
}
|
||||
|
||||
static int
|
||||
object_manager_iterate_filtered (lua_State *L)
|
||||
{
|
||||
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
|
||||
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST);
|
||||
WpIterator *it = wp_object_manager_new_filtered_iterator_full (om,
|
||||
wp_object_interest_ref (oi));
|
||||
WpObjectInterest *oi = get_optional_object_interest (L, 2);
|
||||
WpIterator *it = oi ?
|
||||
wp_object_manager_new_filtered_iterator_full (om,
|
||||
wp_object_interest_ref (oi)) :
|
||||
wp_object_manager_new_iterator (om);
|
||||
return push_wpiterator (L, it);
|
||||
}
|
||||
|
||||
@@ -592,13 +606,10 @@ static int
|
||||
object_manager_lookup (lua_State *L)
|
||||
{
|
||||
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
|
||||
WpObject *o = NULL;
|
||||
if (lua_isuserdata (L, 2)) {
|
||||
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST);
|
||||
o = wp_object_manager_lookup_full (om, wp_object_interest_ref (oi));
|
||||
} else {
|
||||
o = wp_object_manager_lookup (om, WP_TYPE_OBJECT, NULL);
|
||||
}
|
||||
WpObjectInterest *oi = get_optional_object_interest (L, 2);
|
||||
WpObject *o = oi ?
|
||||
wp_object_manager_lookup_full (om, wp_object_interest_ref (oi)) :
|
||||
wp_object_manager_lookup (om, G_TYPE_OBJECT, NULL);
|
||||
if (o) {
|
||||
wplua_pushobject (L, o);
|
||||
return 1;
|
||||
@@ -609,7 +620,6 @@ object_manager_lookup (lua_State *L)
|
||||
static const luaL_Reg object_manager_methods[] = {
|
||||
{ "activate", object_manager_activate },
|
||||
{ "iterate", object_manager_iterate },
|
||||
{ "iterate_filtered", object_manager_iterate_filtered },
|
||||
{ "lookup", object_manager_lookup },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
@@ -649,14 +659,11 @@ static int
|
||||
session_iterate_endpoints (lua_State *L)
|
||||
{
|
||||
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
|
||||
WpIterator *it = NULL;
|
||||
if (lua_isuserdata (L, 2)) {
|
||||
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST);
|
||||
it = wp_session_new_endpoints_filtered_iterator_full (session,
|
||||
wp_object_interest_ref (oi));
|
||||
} else {
|
||||
it = wp_session_new_endpoints_iterator (session);
|
||||
}
|
||||
WpObjectInterest *oi = get_optional_object_interest (L, 2);
|
||||
WpIterator *it = oi ?
|
||||
wp_session_new_endpoints_filtered_iterator_full (session,
|
||||
wp_object_interest_ref (oi)) :
|
||||
wp_session_new_endpoints_iterator (session);
|
||||
return push_wpiterator (L, it);
|
||||
}
|
||||
|
||||
@@ -664,31 +671,26 @@ static int
|
||||
session_lookup_endpoint (lua_State *L)
|
||||
{
|
||||
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
|
||||
WpEndpoint *ep = NULL;
|
||||
if (lua_isuserdata (L, 2)) {
|
||||
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST);
|
||||
ep = wp_session_lookup_endpoint_full (session, wp_object_interest_ref (oi));
|
||||
} else {
|
||||
ep = wp_session_lookup_endpoint (session, NULL);
|
||||
}
|
||||
if (!ep)
|
||||
return 0;
|
||||
WpObjectInterest *oi = get_optional_object_interest (L, 2);
|
||||
WpEndpoint *ep = oi ?
|
||||
wp_session_lookup_endpoint_full (session, wp_object_interest_ref (oi)) :
|
||||
wp_session_lookup_endpoint (session, NULL);
|
||||
if (ep) {
|
||||
wplua_pushobject (L, ep);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
session_iterate_links (lua_State *L)
|
||||
{
|
||||
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
|
||||
WpIterator *it = NULL;
|
||||
if (lua_isuserdata (L, 2)) {
|
||||
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST);
|
||||
it = wp_session_new_links_filtered_iterator_full (session,
|
||||
wp_object_interest_ref (oi));
|
||||
} else {
|
||||
it = wp_session_new_links_iterator (session);
|
||||
}
|
||||
WpObjectInterest *oi = get_optional_object_interest (L, 2);
|
||||
WpIterator *it = oi ?
|
||||
wp_session_new_links_filtered_iterator_full (session,
|
||||
wp_object_interest_ref (oi)) :
|
||||
wp_session_new_links_iterator (session);
|
||||
return push_wpiterator (L, it);
|
||||
}
|
||||
|
||||
@@ -696,16 +698,14 @@ static int
|
||||
session_lookup_link (lua_State *L)
|
||||
{
|
||||
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
|
||||
WpEndpointLink *l = NULL;
|
||||
if (lua_isuserdata (L, 2)) {
|
||||
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST);
|
||||
l = wp_session_lookup_link_full (session, wp_object_interest_ref (oi));
|
||||
} else {
|
||||
l = wp_session_lookup_link (session, NULL);
|
||||
}
|
||||
if (!l)
|
||||
return 0;
|
||||
WpObjectInterest *oi = get_optional_object_interest (L, 2);
|
||||
WpEndpointLink *l = oi ?
|
||||
wp_session_lookup_link_full (session, wp_object_interest_ref (oi)) :
|
||||
wp_session_lookup_link (session, NULL);
|
||||
if (l) {
|
||||
wplua_pushobject (L, l);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -34,11 +34,12 @@ function setPermissions (client, nodes_om, allow_client, allow_nodes)
|
||||
client:update_permissions { [client_id] = allow_client and "all" or "-" }
|
||||
|
||||
-- Update permissions on client's nodes
|
||||
for node in nodes_om:iterate_filtered( Interest { type = "node",
|
||||
for node in nodes_om:iterate {
|
||||
type = "node",
|
||||
Constraint { "client.id", "=", client_id },
|
||||
Constraint { "media.role", "=", "Camera" },
|
||||
Constraint { "media.class", "=", "Video/Source" },
|
||||
}) do
|
||||
} do
|
||||
local node_id = node["bound-id"]
|
||||
client:update_permissions { [node_id] = allow_nodes and "all" or "-" }
|
||||
end
|
||||
@@ -112,9 +113,10 @@ if pps_plugin then
|
||||
pps_plugin:connect("changed", function (p, table, id, deleted, permissions)
|
||||
if table == "devices" or id == "camera" then
|
||||
for app_id, _ in pairs(permissions) do
|
||||
for client in clients_om:iterate_filtered( Interest { type = "client",
|
||||
for client in clients_om:iterate {
|
||||
type = "client",
|
||||
Constraint { "pipewire.access.portal.app_id", "=", app_id }
|
||||
}) do
|
||||
} do
|
||||
updateClientPermissions (client, nodes_om, permissions)
|
||||
end
|
||||
end
|
||||
|
@@ -17,19 +17,17 @@ function addEndpoint (node, session_name, endpoint_type, priority)
|
||||
local name = nil
|
||||
|
||||
-- find the session
|
||||
session = sessions_om:lookup(Interest { type = "session",
|
||||
Constraint { "session.name", "=", session_name, type = "pw-global" }
|
||||
})
|
||||
session = sessions_om:lookup {
|
||||
type = "session",
|
||||
Constraint { "session.name", "=", session_name }
|
||||
}
|
||||
if session == nil then
|
||||
Log.warning(node, "could not find session");
|
||||
return
|
||||
end
|
||||
|
||||
-- get the endpoint name
|
||||
name = node.properties['node.name']
|
||||
if name == nil then
|
||||
name = "endpoint.node." .. id
|
||||
end
|
||||
name = node.properties['node.name'] or "endpoint.node." .. id
|
||||
|
||||
-- create endpoint
|
||||
session_items.endpoints[id] = SessionItem ( endpoint_type )
|
||||
|
@@ -114,15 +114,15 @@ end
|
||||
|
||||
function moveEndpointFromNodeId (ep_node_id, target_node_id)
|
||||
for session in om_session:iterate() do
|
||||
local ep = session:lookup_endpoint (Interest {
|
||||
local ep = session:lookup_endpoint {
|
||||
type = "endpoint",
|
||||
Constraint { "node.id", "=", tostring(ep_node_id), type = "pw" }
|
||||
})
|
||||
}
|
||||
if ep then
|
||||
local target = session:lookup_endpoint (Interest {
|
||||
local target = session:lookup_endpoint {
|
||||
type = "endpoint",
|
||||
Constraint { "node.id", "=", tostring(target_node_id), type = "pw" }
|
||||
})
|
||||
}
|
||||
if target then
|
||||
moveEndpoint (session, ep, target)
|
||||
break
|
||||
@@ -139,16 +139,16 @@ function reevaluateAutoLinkedEndpoints (ep_media_class, target_id)
|
||||
default_endpoint_target[ep_media_class] = target_id
|
||||
|
||||
-- move auto linked endpoints to the new target
|
||||
for session in om_session:iterate_filtered (Interest { type = "session" } ) do
|
||||
local target = session:lookup_endpoint (Interest {
|
||||
for session in om_session:iterate { type = "session" } do
|
||||
local target = session:lookup_endpoint {
|
||||
type = "endpoint",
|
||||
Constraint { "bound-id", "=", target_id, type = "gobject" }
|
||||
})
|
||||
}
|
||||
if target then
|
||||
for ep in session:iterate_endpoints (Interest{
|
||||
for ep in session:iterate_endpoints {
|
||||
type = "endpoint",
|
||||
Constraint { "media-class", "=", ep_media_class, type = "gobject" },
|
||||
} ) do
|
||||
} do
|
||||
if auto_linked_endpoints[ep["bound-id"]] == true then
|
||||
moveEndpoint (session, ep, target)
|
||||
end
|
||||
@@ -271,10 +271,10 @@ om_metadata:connect("object-added", function (om, metadata)
|
||||
if config.move and key == "target.node" then
|
||||
moveEndpointFromNodeId (subject, tonumber (value))
|
||||
elseif config.follow and string.find(key, "default.session.endpoint") then
|
||||
local session = om_session:lookup (Interest {
|
||||
local session = om_session:lookup {
|
||||
type = "session",
|
||||
Constraint { "bound-id", "=", subject, type = "gobject" }
|
||||
})
|
||||
}
|
||||
if session then
|
||||
local target_class =
|
||||
metadata_key_target_class_assoc[key][session.properties["session.name"]]
|
||||
|
@@ -65,7 +65,7 @@ obj_mgr:connect("installed", function (om)
|
||||
-- Print connected clients
|
||||
--
|
||||
print ("\nClients:")
|
||||
for obj in om:iterate_filtered(Interest { type = "client" }) do
|
||||
for obj in om:iterate { type = "client" } do
|
||||
|
||||
--
|
||||
-- 'bound-id' and 'global-properties' are GObject
|
||||
@@ -82,7 +82,7 @@ obj_mgr:connect("installed", function (om)
|
||||
-- Print devices
|
||||
--
|
||||
print ("\nDevices:")
|
||||
for obj in om:iterate_filtered(Interest { type = "device" }) do
|
||||
for obj in om:iterate { type = "device" } do
|
||||
local id = obj["bound-id"]
|
||||
local global_props = obj["global-properties"]
|
||||
|
||||
@@ -116,7 +116,7 @@ obj_mgr:connect("installed", function (om)
|
||||
local interest = Interest { type = "node",
|
||||
Constraint { "media.class", "matches", "*/Sink" }
|
||||
}
|
||||
for obj in om:iterate_filtered(interest) do
|
||||
for obj in om:iterate(interest) do
|
||||
printNode(obj)
|
||||
end
|
||||
|
||||
@@ -127,7 +127,7 @@ obj_mgr:connect("installed", function (om)
|
||||
local interest = Interest { type = "node",
|
||||
Constraint { "media.class", "matches", "*/Source" }
|
||||
}
|
||||
for obj in om:iterate_filtered(interest) do
|
||||
for obj in om:iterate(interest) do
|
||||
printNode(obj)
|
||||
end
|
||||
|
||||
@@ -138,7 +138,7 @@ obj_mgr:connect("installed", function (om)
|
||||
local interest = Interest { type = "node",
|
||||
Constraint { "media.class", "matches", "Stream/*" }
|
||||
}
|
||||
for obj in om:iterate_filtered(interest) do
|
||||
for obj in om:iterate(interest) do
|
||||
printNode(obj)
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user