m-lua-scripting/pod: retrieve Ids in Array & Choice as strings, if possible

When an Array or Choice that contains Spa:Id appears as a value for an object
field, it is possible to retrieve the Id type from that object field type.
This commit is contained in:
George Kiagiadakis
2021-02-23 13:56:38 +02:00
parent 5302700c6d
commit 51e24c58b5
2 changed files with 27 additions and 6 deletions

View File

@@ -861,7 +861,7 @@ spa_pod_get_type_name (lua_State *L)
static void static void
push_primitive_values (lua_State *L, WpSpaPod *pod, WpSpaType type, push_primitive_values (lua_State *L, WpSpaPod *pod, WpSpaType type,
guint start_index) guint start_index, WpSpaIdTable idtable)
{ {
g_auto (GValue) item = G_VALUE_INIT; g_auto (GValue) item = G_VALUE_INIT;
g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (pod); g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (pod);
@@ -873,9 +873,16 @@ push_primitive_values (lua_State *L, WpSpaPod *pod, WpSpaType type,
case SPA_TYPE_Bool: case SPA_TYPE_Bool:
lua_pushboolean (L, *(gboolean *)p); lua_pushboolean (L, *(gboolean *)p);
break; break;
case SPA_TYPE_Id: case SPA_TYPE_Id: {
lua_pushinteger (L, *(guint32 *)p); WpSpaIdValue idval = NULL;
if (idtable)
idval = wp_spa_id_table_find_value (idtable, *(guint32 *)p);
if (idval)
lua_pushstring (L, wp_spa_id_value_short_name (idval));
else
lua_pushinteger (L, *(guint32 *)p);
break; break;
}
case SPA_TYPE_Int: case SPA_TYPE_Int:
lua_pushinteger (L, *(gint *)p); lua_pushinteger (L, *(gint *)p);
break; break;
@@ -1092,12 +1099,16 @@ push_luapod (lua_State *L, WpSpaPod *pod, WpSpaIdValue field_idval)
else if (wp_spa_pod_is_array (pod)) { else if (wp_spa_pod_is_array (pod)) {
g_autoptr (WpSpaPod) child = wp_spa_pod_get_array_child (pod); g_autoptr (WpSpaPod) child = wp_spa_pod_get_array_child (pod);
WpSpaType type = wp_spa_pod_get_spa_type (child); WpSpaType type = wp_spa_pod_get_spa_type (child);
WpSpaIdTable idtable = NULL;
if (field_idval && type == SPA_TYPE_Id && SPA_TYPE_Array ==
wp_spa_id_value_get_value_type (field_idval, &idtable))
wp_spa_id_value_array_get_item_type (field_idval, &idtable);
lua_newtable (L); lua_newtable (L);
lua_pushstring (L, "Array"); lua_pushstring (L, "Array");
lua_setfield (L, -2, "pod_type"); lua_setfield (L, -2, "pod_type");
lua_pushstring (L, wp_spa_type_name (type)); lua_pushstring (L, wp_spa_type_name (type));
lua_setfield (L, -2, "value_type"); lua_setfield (L, -2, "value_type");
push_primitive_values (L, pod, type, 1); push_primitive_values (L, pod, type, 1, idtable);
} }
/* Choice */ /* Choice */
@@ -1106,12 +1117,15 @@ push_luapod (lua_State *L, WpSpaPod *pod, WpSpaIdValue field_idval)
WpSpaType type = wp_spa_pod_get_spa_type (child); WpSpaType type = wp_spa_pod_get_spa_type (child);
g_autofree const gchar *choice_type = g_strdup_printf ("Choice.%s", g_autofree const gchar *choice_type = g_strdup_printf ("Choice.%s",
wp_spa_id_value_short_name (wp_spa_pod_get_choice_type (pod))); wp_spa_id_value_short_name (wp_spa_pod_get_choice_type (pod)));
WpSpaIdTable idtable = NULL;
if (field_idval && type == SPA_TYPE_Id)
wp_spa_id_value_get_value_type (field_idval, &idtable);
lua_newtable (L); lua_newtable (L);
lua_pushstring (L, choice_type); lua_pushstring (L, choice_type);
lua_setfield (L, -2, "pod_type"); lua_setfield (L, -2, "pod_type");
lua_pushstring (L, wp_spa_type_name (type)); lua_pushstring (L, wp_spa_type_name (type));
lua_setfield (L, -2, "value_type"); lua_setfield (L, -2, "value_type");
push_primitive_values (L, pod, type, 1); push_primitive_values (L, pod, type, 1, idtable);
} }
/* Error */ /* Error */

View File

@@ -175,6 +175,7 @@ pod = Pod.Object {
mediaType = "audio", mediaType = "audio",
mediaSubtype = "raw", mediaSubtype = "raw",
rate = 48000, rate = 48000,
format = Pod.Choice.Enum { "Spa:Enum:AudioFormat", "S16LE", "S16LE", "F32LE" },
channels = Pod.Choice.Range { "Spa:Int", 2, 1, 32 }, channels = Pod.Choice.Range { "Spa:Int", 2, 1, 32 },
position = Pod.Array { "Spa:Enum:AudioChannel", "FL", "FR" } position = Pod.Array { "Spa:Enum:AudioChannel", "FL", "FR" }
} }
@@ -190,6 +191,11 @@ assert (val.properties.format.object_id == "Format")
assert (val.properties.format.properties.mediaType == "audio") assert (val.properties.format.properties.mediaType == "audio")
assert (val.properties.format.properties.mediaSubtype == "raw") assert (val.properties.format.properties.mediaSubtype == "raw")
assert (val.properties.format.properties.rate == 48000) assert (val.properties.format.properties.rate == 48000)
assert (val.properties.format.properties.format.pod_type == "Choice.Enum")
assert (val.properties.format.properties.format.value_type == "Spa:Id")
assert (val.properties.format.properties.format[1] == "S16LE")
assert (val.properties.format.properties.format[2] == "S16LE")
assert (val.properties.format.properties.format[3] == "F32LE")
assert (val.properties.format.properties.channels.pod_type == "Choice.Range") assert (val.properties.format.properties.channels.pod_type == "Choice.Range")
assert (val.properties.format.properties.channels.value_type == "Spa:Int") assert (val.properties.format.properties.channels.value_type == "Spa:Int")
assert (val.properties.format.properties.channels[1] == 2) assert (val.properties.format.properties.channels[1] == 2)
@@ -197,5 +203,6 @@ assert (val.properties.format.properties.channels[2] == 1)
assert (val.properties.format.properties.channels[3] == 32) assert (val.properties.format.properties.channels[3] == 32)
assert (val.properties.format.properties.position.pod_type == "Array") assert (val.properties.format.properties.position.pod_type == "Array")
assert (val.properties.format.properties.position.value_type == "Spa:Id") assert (val.properties.format.properties.position.value_type == "Spa:Id")
assert (val.properties.format.properties.position[1] == 3 and val.properties.format.properties.position[2] == 4) assert (val.properties.format.properties.position[1] == "FL")
assert (val.properties.format.properties.position[2] == "FR")
assert (pod:get_type_name() == "Spa:Pod:Object:Param:PortConfig") assert (pod:get_type_name() == "Spa:Pod:Object:Param:PortConfig")