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:
George Kiagiadakis
2021-03-02 12:56:28 +02:00
parent 6c2bfea55b
commit 905636dc8d
5 changed files with 85 additions and 85 deletions

View File

@@ -462,17 +462,17 @@ object_interest_new_add_constraint (lua_State *L, GType type,
} }
static int static int
object_interest_new (lua_State *L) object_interest_new_index (lua_State *L, int idx)
{ {
WpObjectInterest *interest = NULL; WpObjectInterest *interest = NULL;
GType type = 0; GType type = 0;
gchar *typestr; gchar *typestr;
luaL_checktype (L, 1, LUA_TTABLE); luaL_checktype (L, idx, LUA_TTABLE);
/* type = "string" -> required */ /* type = "string" -> required */
lua_pushliteral (L, "type"); 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"); luaL_error (L, "Interest: expected 'type' as string");
/* "device" -> "WpDevice" */ /* "device" -> "WpDevice" */
@@ -492,7 +492,7 @@ object_interest_new (lua_State *L)
/* add constraints */ /* add constraints */
lua_pushnil (L); lua_pushnil (L);
while (lua_next (L, 1)) { while (lua_next (L, idx)) {
/* if the key isn't "type" */ /* if the key isn't "type" */
if (!(lua_type (L, -2) == LUA_TSTRING && if (!(lua_type (L, -2) == LUA_TSTRING &&
!g_strcmp0 ("type", lua_tostring (L, -2)))) !g_strcmp0 ("type", lua_tostring (L, -2))))
@@ -503,6 +503,12 @@ object_interest_new (lua_State *L)
return 1; return 1;
} }
static int
object_interest_new (lua_State *L)
{
return object_interest_new_index (L, 1);
}
static int static int
object_interest_matches (lua_State *L) object_interest_matches (lua_State *L)
{ {
@@ -527,6 +533,20 @@ static const luaL_Reg object_interest_methods[] = {
{ NULL, NULL } { 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 */ /* WpObjectManager */
static int static int
@@ -574,17 +594,11 @@ static int
object_manager_iterate (lua_State *L) object_manager_iterate (lua_State *L)
{ {
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER); WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
WpIterator *it = wp_object_manager_new_iterator (om); WpObjectInterest *oi = get_optional_object_interest (L, 2);
return push_wpiterator (L, it); WpIterator *it = oi ?
} wp_object_manager_new_filtered_iterator_full (om,
wp_object_interest_ref (oi)) :
static int wp_object_manager_new_iterator (om);
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));
return push_wpiterator (L, it); return push_wpiterator (L, it);
} }
@@ -592,13 +606,10 @@ static int
object_manager_lookup (lua_State *L) object_manager_lookup (lua_State *L)
{ {
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER); WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
WpObject *o = NULL; WpObjectInterest *oi = get_optional_object_interest (L, 2);
if (lua_isuserdata (L, 2)) { WpObject *o = oi ?
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST); wp_object_manager_lookup_full (om, wp_object_interest_ref (oi)) :
o = wp_object_manager_lookup_full (om, wp_object_interest_ref (oi)); wp_object_manager_lookup (om, G_TYPE_OBJECT, NULL);
} else {
o = wp_object_manager_lookup (om, WP_TYPE_OBJECT, NULL);
}
if (o) { if (o) {
wplua_pushobject (L, o); wplua_pushobject (L, o);
return 1; return 1;
@@ -609,7 +620,6 @@ object_manager_lookup (lua_State *L)
static const luaL_Reg object_manager_methods[] = { static const luaL_Reg object_manager_methods[] = {
{ "activate", object_manager_activate }, { "activate", object_manager_activate },
{ "iterate", object_manager_iterate }, { "iterate", object_manager_iterate },
{ "iterate_filtered", object_manager_iterate_filtered },
{ "lookup", object_manager_lookup }, { "lookup", object_manager_lookup },
{ NULL, NULL } { NULL, NULL }
}; };
@@ -649,14 +659,11 @@ static int
session_iterate_endpoints (lua_State *L) session_iterate_endpoints (lua_State *L)
{ {
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION); WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpIterator *it = NULL; WpObjectInterest *oi = get_optional_object_interest (L, 2);
if (lua_isuserdata (L, 2)) { WpIterator *it = oi ?
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST); wp_session_new_endpoints_filtered_iterator_full (session,
it = wp_session_new_endpoints_filtered_iterator_full (session, wp_object_interest_ref (oi)) :
wp_object_interest_ref (oi)); wp_session_new_endpoints_iterator (session);
} else {
it = wp_session_new_endpoints_iterator (session);
}
return push_wpiterator (L, it); return push_wpiterator (L, it);
} }
@@ -664,31 +671,26 @@ static int
session_lookup_endpoint (lua_State *L) session_lookup_endpoint (lua_State *L)
{ {
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION); WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpEndpoint *ep = NULL; WpObjectInterest *oi = get_optional_object_interest (L, 2);
if (lua_isuserdata (L, 2)) { WpEndpoint *ep = oi ?
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST); wp_session_lookup_endpoint_full (session, wp_object_interest_ref (oi)) :
ep = wp_session_lookup_endpoint_full (session, wp_object_interest_ref (oi)); wp_session_lookup_endpoint (session, NULL);
} else { if (ep) {
ep = wp_session_lookup_endpoint (session, NULL); wplua_pushobject (L, ep);
return 1;
} }
if (!ep) return 0;
return 0;
wplua_pushobject (L, ep);
return 1;
} }
static int static int
session_iterate_links (lua_State *L) session_iterate_links (lua_State *L)
{ {
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION); WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpIterator *it = NULL; WpObjectInterest *oi = get_optional_object_interest (L, 2);
if (lua_isuserdata (L, 2)) { WpIterator *it = oi ?
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST); wp_session_new_links_filtered_iterator_full (session,
it = wp_session_new_links_filtered_iterator_full (session, wp_object_interest_ref (oi)) :
wp_object_interest_ref (oi)); wp_session_new_links_iterator (session);
} else {
it = wp_session_new_links_iterator (session);
}
return push_wpiterator (L, it); return push_wpiterator (L, it);
} }
@@ -696,16 +698,14 @@ static int
session_lookup_link (lua_State *L) session_lookup_link (lua_State *L)
{ {
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION); WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpEndpointLink *l = NULL; WpObjectInterest *oi = get_optional_object_interest (L, 2);
if (lua_isuserdata (L, 2)) { WpEndpointLink *l = oi ?
WpObjectInterest *oi = wplua_checkboxed (L, 2, WP_TYPE_OBJECT_INTEREST); wp_session_lookup_link_full (session, wp_object_interest_ref (oi)) :
l = wp_session_lookup_link_full (session, wp_object_interest_ref (oi)); wp_session_lookup_link (session, NULL);
} else { if (l) {
l = wp_session_lookup_link (session, NULL); wplua_pushobject (L, l);
return 1;
} }
if (!l)
return 0;
wplua_pushobject (L, l);
return 0; return 0;
} }

View File

@@ -34,11 +34,12 @@ function setPermissions (client, nodes_om, allow_client, allow_nodes)
client:update_permissions { [client_id] = allow_client and "all" or "-" } client:update_permissions { [client_id] = allow_client and "all" or "-" }
-- Update permissions on client's nodes -- 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 { "client.id", "=", client_id },
Constraint { "media.role", "=", "Camera" }, Constraint { "media.role", "=", "Camera" },
Constraint { "media.class", "=", "Video/Source" }, Constraint { "media.class", "=", "Video/Source" },
}) do } do
local node_id = node["bound-id"] local node_id = node["bound-id"]
client:update_permissions { [node_id] = allow_nodes and "all" or "-" } client:update_permissions { [node_id] = allow_nodes and "all" or "-" }
end end
@@ -112,9 +113,10 @@ if pps_plugin then
pps_plugin:connect("changed", function (p, table, id, deleted, permissions) pps_plugin:connect("changed", function (p, table, id, deleted, permissions)
if table == "devices" or id == "camera" then if table == "devices" or id == "camera" then
for app_id, _ in pairs(permissions) do 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 } Constraint { "pipewire.access.portal.app_id", "=", app_id }
}) do } do
updateClientPermissions (client, nodes_om, permissions) updateClientPermissions (client, nodes_om, permissions)
end end
end end

View File

@@ -17,19 +17,17 @@ function addEndpoint (node, session_name, endpoint_type, priority)
local name = nil local name = nil
-- find the session -- find the session
session = sessions_om:lookup(Interest { type = "session", session = sessions_om:lookup {
Constraint { "session.name", "=", session_name, type = "pw-global" } type = "session",
}) Constraint { "session.name", "=", session_name }
}
if session == nil then if session == nil then
Log.warning(node, "could not find session"); Log.warning(node, "could not find session");
return return
end end
-- get the endpoint name -- get the endpoint name
name = node.properties['node.name'] name = node.properties['node.name'] or "endpoint.node." .. id
if name == nil then
name = "endpoint.node." .. id
end
-- create endpoint -- create endpoint
session_items.endpoints[id] = SessionItem ( endpoint_type ) session_items.endpoints[id] = SessionItem ( endpoint_type )

View File

@@ -114,15 +114,15 @@ end
function moveEndpointFromNodeId (ep_node_id, target_node_id) function moveEndpointFromNodeId (ep_node_id, target_node_id)
for session in om_session:iterate() do for session in om_session:iterate() do
local ep = session:lookup_endpoint (Interest { local ep = session:lookup_endpoint {
type = "endpoint", type = "endpoint",
Constraint { "node.id", "=", tostring(ep_node_id), type = "pw" } Constraint { "node.id", "=", tostring(ep_node_id), type = "pw" }
}) }
if ep then if ep then
local target = session:lookup_endpoint (Interest { local target = session:lookup_endpoint {
type = "endpoint", type = "endpoint",
Constraint { "node.id", "=", tostring(target_node_id), type = "pw" } Constraint { "node.id", "=", tostring(target_node_id), type = "pw" }
}) }
if target then if target then
moveEndpoint (session, ep, target) moveEndpoint (session, ep, target)
break break
@@ -139,16 +139,16 @@ function reevaluateAutoLinkedEndpoints (ep_media_class, target_id)
default_endpoint_target[ep_media_class] = target_id default_endpoint_target[ep_media_class] = target_id
-- move auto linked endpoints to the new target -- move auto linked endpoints to the new target
for session in om_session:iterate_filtered (Interest { type = "session" } ) do for session in om_session:iterate { type = "session" } do
local target = session:lookup_endpoint (Interest { local target = session:lookup_endpoint {
type = "endpoint", type = "endpoint",
Constraint { "bound-id", "=", target_id, type = "gobject" } Constraint { "bound-id", "=", target_id, type = "gobject" }
}) }
if target then if target then
for ep in session:iterate_endpoints (Interest{ for ep in session:iterate_endpoints {
type = "endpoint", type = "endpoint",
Constraint { "media-class", "=", ep_media_class, type = "gobject" }, Constraint { "media-class", "=", ep_media_class, type = "gobject" },
} ) do } do
if auto_linked_endpoints[ep["bound-id"]] == true then if auto_linked_endpoints[ep["bound-id"]] == true then
moveEndpoint (session, ep, target) moveEndpoint (session, ep, target)
end end
@@ -271,10 +271,10 @@ om_metadata:connect("object-added", function (om, metadata)
if config.move and key == "target.node" then if config.move and key == "target.node" then
moveEndpointFromNodeId (subject, tonumber (value)) moveEndpointFromNodeId (subject, tonumber (value))
elseif config.follow and string.find(key, "default.session.endpoint") then elseif config.follow and string.find(key, "default.session.endpoint") then
local session = om_session:lookup (Interest { local session = om_session:lookup {
type = "session", type = "session",
Constraint { "bound-id", "=", subject, type = "gobject" } Constraint { "bound-id", "=", subject, type = "gobject" }
}) }
if session then if session then
local target_class = local target_class =
metadata_key_target_class_assoc[key][session.properties["session.name"]] metadata_key_target_class_assoc[key][session.properties["session.name"]]

View File

@@ -65,7 +65,7 @@ obj_mgr:connect("installed", function (om)
-- Print connected clients -- Print connected clients
-- --
print ("\nClients:") 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 -- 'bound-id' and 'global-properties' are GObject
@@ -82,7 +82,7 @@ obj_mgr:connect("installed", function (om)
-- Print devices -- Print devices
-- --
print ("\nDevices:") 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 id = obj["bound-id"]
local global_props = obj["global-properties"] local global_props = obj["global-properties"]
@@ -116,7 +116,7 @@ obj_mgr:connect("installed", function (om)
local interest = Interest { type = "node", local interest = Interest { type = "node",
Constraint { "media.class", "matches", "*/Sink" } Constraint { "media.class", "matches", "*/Sink" }
} }
for obj in om:iterate_filtered(interest) do for obj in om:iterate(interest) do
printNode(obj) printNode(obj)
end end
@@ -127,7 +127,7 @@ obj_mgr:connect("installed", function (om)
local interest = Interest { type = "node", local interest = Interest { type = "node",
Constraint { "media.class", "matches", "*/Source" } Constraint { "media.class", "matches", "*/Source" }
} }
for obj in om:iterate_filtered(interest) do for obj in om:iterate(interest) do
printNode(obj) printNode(obj)
end end
@@ -138,7 +138,7 @@ obj_mgr:connect("installed", function (om)
local interest = Interest { type = "node", local interest = Interest { type = "node",
Constraint { "media.class", "matches", "Stream/*" } Constraint { "media.class", "matches", "Stream/*" }
} }
for obj in om:iterate_filtered(interest) do for obj in om:iterate(interest) do
printNode(obj) printNode(obj)
end end