lua/api: simplify & improve session_item_configure

This commit is contained in:
George Kiagiadakis
2021-03-02 14:39:00 +02:00
parent a4ec7538bc
commit 9399f3db66
2 changed files with 25 additions and 51 deletions

View File

@@ -1040,8 +1040,6 @@ session_item_configure (lua_State *L)
g_auto (GVariantBuilder) b = g_auto (GVariantBuilder) b =
G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT); G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
GVariant *config = NULL; GVariant *config = NULL;
gboolean is_key = TRUE;
const gchar *key = NULL;
/* validate arguments */ /* validate arguments */
luaL_checktype (L, 2, LUA_TTABLE); luaL_checktype (L, 2, LUA_TTABLE);
@@ -1049,48 +1047,24 @@ session_item_configure (lua_State *L)
/* build the configuration */ /* build the configuration */
lua_pushnil (L); lua_pushnil (L);
while (lua_next (L, 2)) { while (lua_next (L, 2)) {
if (is_key) { const gchar *key = NULL;
is_key = FALSE; GVariant *var = NULL;
key = lua_tostring (L, -1);
} else {
is_key = TRUE;
switch (lua_type (L, -1)) { switch (lua_type (L, -1)) {
case LUA_TBOOLEAN:
g_variant_builder_add (&b, "{sv}", key,
g_variant_new_boolean (lua_toboolean (L, -1)));
break;
case LUA_TNUMBER:
g_variant_builder_add (&b, "{sv}", key,
g_variant_new_int64 (lua_tointeger (L, -1)));
break;
case LUA_TSTRING:
g_variant_builder_add (&b, "{sv}", key,
g_variant_new_string (lua_tostring (L, -1)));
break;
case LUA_TUSERDATA: { case LUA_TUSERDATA: {
GValue *v = lua_touserdata (L, -1); GValue *v = lua_touserdata (L, -1);
gpointer p = NULL; gpointer p = g_value_peek_pointer (v);
if (G_VALUE_HOLDS_OBJECT (v)) { var = g_variant_new_uint64 ((guint64) p);
p = g_value_get_object (v);
} else if (G_VALUE_HOLDS_BOXED (v)) {
p = g_value_get_boxed (v);
} else if (G_VALUE_HOLDS_POINTER (v)) {
p = g_value_get_pointer (v);
} else {
luaL_error (L, "Key '%s' does not hold a valid pointer", key);
break;
}
g_variant_builder_add (&b, "{sv}", key,
g_variant_new_uint64 ((guint64)p));
break; break;
} }
default: default:
luaL_error (L, "Key '%s' with value type '%s' is not supported", key, var = wplua_lua_to_gvariant (L, -1);
lua_typename(L, lua_type(L, -1)));
break; break;
} }
}
lua_pop (L, 1); key = luaL_tolstring (L, -2, NULL);
g_variant_builder_add (&b, "{sv}", key, var);
lua_pop (L, 2);
} }
config = g_variant_builder_end (&b); config = g_variant_builder_end (&b);

View File

@@ -32,12 +32,12 @@ function addEndpoint (node, session_name, endpoint_type, priority)
session_items.endpoints[id] = SessionItem ( endpoint_type ) session_items.endpoints[id] = SessionItem ( endpoint_type )
-- configure endpoint -- configure endpoint
if not session_items.endpoints[id]:configure ({ if not session_items.endpoints[id]:configure {
"node", node, ["node"] = node,
"name", name, ["name"] = name,
"media-class", media_class, ["media-class"] = media_class,
"priority", priority, ["priority"] = priority,
}) then } then
Log.warning(node, "failed to configure endpoint"); Log.warning(node, "failed to configure endpoint");
return return
end end
@@ -56,9 +56,9 @@ function addEndpoint (node, session_name, endpoint_type, priority)
local monitor = SessionItem ( "si-monitor-endpoint" ) local monitor = SessionItem ( "si-monitor-endpoint" )
-- configure monitor -- configure monitor
if not monitor:configure ({ if not monitor:configure {
"adapter", session_items.endpoints[id] ["adapter"] = session_items.endpoints[id]
}) then } then
Log.warning(monitor, "failed to configure monitor " .. name); Log.warning(monitor, "failed to configure monitor " .. name);
return return
end end