lua/api: make the type optional when declaring Interest as a function argument

it makes no sense to specify the type when calling, for instance,
session:lookup_endpoint() or similar functions

it also makes it easier to use iterate or lookup in the object
manager, when we already know the type of the objects that the om
contains
This commit is contained in:
George Kiagiadakis 2021-03-02 14:08:43 +02:00
parent 905636dc8d
commit a4ec7538bc
4 changed files with 25 additions and 32 deletions

View file

@ -462,31 +462,32 @@ object_interest_new_add_constraint (lua_State *L, GType type,
}
static int
object_interest_new_index (lua_State *L, int idx)
object_interest_new_index (lua_State *L, int idx, GType def_type)
{
WpObjectInterest *interest = NULL;
GType type = 0;
GType type = def_type;
gchar *typestr;
luaL_checktype (L, idx, LUA_TTABLE);
/* type = "string" -> required */
/* type = "string" */
lua_pushliteral (L, "type");
if (lua_gettable (L, idx) != LUA_TSTRING)
luaL_error (L, "Interest: expected 'type' as string");
if (lua_gettable (L, idx) == LUA_TSTRING) {
/* "device" -> "WpDevice" */
typestr = g_strdup_printf ("Wp%s", lua_tostring (L, -1));
if (typestr[2] != 0) {
typestr[2] = g_ascii_toupper (typestr[2]);
type = g_type_from_name (typestr);
}
g_free (typestr);
/* "device" -> "WpDevice" */
typestr = g_strdup_printf ("Wp%s", lua_tostring (L, -1));
if (typestr[2] != 0) {
typestr[2] = g_ascii_toupper (typestr[2]);
type = g_type_from_name (typestr);
if (type == G_TYPE_INVALID)
luaL_error (L, "Interest: unknown type '%s'", lua_tostring (L, -1));
}
g_free (typestr);
else if (def_type == G_TYPE_INVALID)
luaL_error (L, "Interest: expected 'type' as string");
lua_pop (L, 1);
if (!type)
luaL_error (L, "Interest: unknown type '%s'", lua_tostring (L, -1));
interest = wp_object_interest_new_type (type);
wplua_pushboxed (L, WP_TYPE_OBJECT_INTEREST, interest);
@ -506,7 +507,7 @@ object_interest_new_index (lua_State *L, int idx)
static int
object_interest_new (lua_State *L)
{
return object_interest_new_index (L, 1);
return object_interest_new_index (L, 1, G_TYPE_INVALID);
}
static int
@ -534,14 +535,14 @@ static const luaL_Reg object_interest_methods[] = {
};
static WpObjectInterest *
get_optional_object_interest (lua_State *L, int idx)
get_optional_object_interest (lua_State *L, int idx, GType def_type)
{
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);
object_interest_new_index (L, idx, def_type);
return wplua_toboxed (L, -1);
} else
return NULL;
@ -594,7 +595,7 @@ static int
object_manager_iterate (lua_State *L)
{
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
WpObjectInterest *oi = get_optional_object_interest (L, 2);
WpObjectInterest *oi = get_optional_object_interest (L, 2, G_TYPE_OBJECT);
WpIterator *it = oi ?
wp_object_manager_new_filtered_iterator_full (om,
wp_object_interest_ref (oi)) :
@ -606,7 +607,7 @@ static int
object_manager_lookup (lua_State *L)
{
WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
WpObjectInterest *oi = get_optional_object_interest (L, 2);
WpObjectInterest *oi = get_optional_object_interest (L, 2, G_TYPE_OBJECT);
WpObject *o = oi ?
wp_object_manager_lookup_full (om, wp_object_interest_ref (oi)) :
wp_object_manager_lookup (om, G_TYPE_OBJECT, NULL);
@ -659,7 +660,7 @@ static int
session_iterate_endpoints (lua_State *L)
{
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpObjectInterest *oi = get_optional_object_interest (L, 2);
WpObjectInterest *oi = get_optional_object_interest (L, 2, WP_TYPE_ENDPOINT);
WpIterator *it = oi ?
wp_session_new_endpoints_filtered_iterator_full (session,
wp_object_interest_ref (oi)) :
@ -671,7 +672,7 @@ static int
session_lookup_endpoint (lua_State *L)
{
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpObjectInterest *oi = get_optional_object_interest (L, 2);
WpObjectInterest *oi = get_optional_object_interest (L, 2, WP_TYPE_ENDPOINT);
WpEndpoint *ep = oi ?
wp_session_lookup_endpoint_full (session, wp_object_interest_ref (oi)) :
wp_session_lookup_endpoint (session, NULL);
@ -686,7 +687,7 @@ static int
session_iterate_links (lua_State *L)
{
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpObjectInterest *oi = get_optional_object_interest (L, 2);
WpObjectInterest *oi = get_optional_object_interest (L, 2, WP_TYPE_ENDPOINT_LINK);
WpIterator *it = oi ?
wp_session_new_links_filtered_iterator_full (session,
wp_object_interest_ref (oi)) :
@ -698,7 +699,7 @@ static int
session_lookup_link (lua_State *L)
{
WpSession *session = wplua_checkobject (L, 1, WP_TYPE_SESSION);
WpObjectInterest *oi = get_optional_object_interest (L, 2);
WpObjectInterest *oi = get_optional_object_interest (L, 2, WP_TYPE_ENDPOINT_LINK);
WpEndpointLink *l = oi ?
wp_session_lookup_link_full (session, wp_object_interest_ref (oi)) :
wp_session_lookup_link (session, NULL);

View file

@ -35,7 +35,6 @@ function setPermissions (client, nodes_om, allow_client, allow_nodes)
-- Update permissions on client's nodes
for node in nodes_om:iterate {
type = "node",
Constraint { "client.id", "=", client_id },
Constraint { "media.role", "=", "Camera" },
Constraint { "media.class", "=", "Video/Source" },
@ -114,7 +113,6 @@ if pps_plugin then
if table == "devices" or id == "camera" then
for app_id, _ in pairs(permissions) do
for client in clients_om:iterate {
type = "client",
Constraint { "pipewire.access.portal.app_id", "=", app_id }
} do
updateClientPermissions (client, nodes_om, permissions)

View file

@ -18,7 +18,6 @@ function addEndpoint (node, session_name, endpoint_type, priority)
-- find the session
session = sessions_om:lookup {
type = "session",
Constraint { "session.name", "=", session_name }
}
if session == nil then

View file

@ -115,12 +115,10 @@ end
function moveEndpointFromNodeId (ep_node_id, target_node_id)
for session in om_session:iterate() do
local ep = session:lookup_endpoint {
type = "endpoint",
Constraint { "node.id", "=", tostring(ep_node_id), type = "pw" }
}
if ep then
local target = session:lookup_endpoint {
type = "endpoint",
Constraint { "node.id", "=", tostring(target_node_id), type = "pw" }
}
if target then
@ -139,14 +137,12 @@ 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 { type = "session" } do
for session in om_session:iterate() do
local target = session:lookup_endpoint {
type = "endpoint",
Constraint { "bound-id", "=", target_id, type = "gobject" }
}
if target then
for ep in session:iterate_endpoints {
type = "endpoint",
Constraint { "media-class", "=", ep_media_class, type = "gobject" },
} do
if auto_linked_endpoints[ep["bound-id"]] == true then
@ -272,7 +268,6 @@ om_metadata:connect("object-added", function (om, metadata)
moveEndpointFromNodeId (subject, tonumber (value))
elseif config.follow and string.find(key, "default.session.endpoint") then
local session = om_session:lookup {
type = "session",
Constraint { "bound-id", "=", subject, type = "gobject" }
}
if session then