| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author Julian Bouzas <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include <wplua/wplua.h> |
| 11 |
|
| 12 |
#include <spa/utils/type.h> |
| 13 |
|
| 14 |
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting |
| 15 |
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting) |
| 16 |
|
| 17 |
#define MAX_LUA_TYPES 9 |
| 18 |
|
| 19 |
/* Builder */ |
| 20 |
|
| 21 |
typedef gboolean (*primitive_lua_add_func) (WpSpaPodBuilder *, WpSpaIdValue, |
| 22 |
lua_State *, int); |
| 23 |
|
| 24 |
struct primitive_lua_type { |
| 25 |
WpSpaType primitive_type; |
| 26 |
primitive_lua_add_func primitive_lua_add_funcs[MAX_LUA_TYPES]; |
| 27 |
}; |
| 28 |
|
| 29 |
static inline gboolean |
| 30 |
builder_add_boolean_lua_boolean (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 31 |
lua_State *L, int idx) |
| 32 |
{ |
| 33 |
wp_spa_pod_builder_add_boolean (b, lua_toboolean (L, idx)); |
| 34 |
return TRUE; |
| 35 |
} |
| 36 |
|
| 37 |
static inline gboolean |
| 38 |
builder_add_boolean_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 39 |
lua_State *L, int idx) |
| 40 |
{ |
| 41 |
if (lua_isinteger (L, idx)) { |
| 42 |
wp_spa_pod_builder_add_boolean (b, lua_tointeger (L, idx) > 0); |
| 43 |
return TRUE; |
| 44 |
} |
| 45 |
return FALSE; |
| 46 |
} |
| 47 |
|
| 48 |
static inline gboolean |
| 49 |
builder_add_boolean_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 50 |
lua_State *L, int idx) |
| 51 |
{ |
| 52 |
const gchar *value = lua_tostring (L, idx); |
| 53 |
wp_spa_pod_builder_add_boolean (b, |
| 54 |
g_strcmp0 (value, "true") == 0 || g_strcmp0 (value, "1") == 0); |
| 55 |
return TRUE; |
| 56 |
} |
| 57 |
|
| 58 |
static inline gboolean |
| 59 |
builder_add_id_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 60 |
lua_State *L, int idx) |
| 61 |
{ |
| 62 |
if (lua_isinteger (L, idx)) { |
| 63 |
wp_spa_pod_builder_add_id (b, lua_tointeger (L, idx)); |
| 64 |
return TRUE; |
| 65 |
} |
| 66 |
return FALSE; |
| 67 |
} |
| 68 |
|
| 69 |
static inline gboolean |
| 70 |
builder_add_id_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 71 |
lua_State *L, int idx) |
| 72 |
{ |
| 73 |
const gchar *value = lua_tostring (L, idx); |
| 74 |
WpSpaIdTable id_table = NULL; |
| 75 |
WpSpaIdValue id_val = NULL; |
| 76 |
if (key_id) { |
| 77 |
wp_spa_id_value_get_value_type (key_id, &id_table); |
| 78 |
if (id_table) { |
| 79 |
id_val = wp_spa_id_table_find_value_from_short_name (id_table, value); |
| 80 |
wp_spa_pod_builder_add_id (b, wp_spa_id_value_number (id_val)); |
| 81 |
return TRUE; |
| 82 |
} |
| 83 |
} |
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
|
| 87 |
static inline gboolean |
| 88 |
builder_add_int_lua_boolean (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 89 |
lua_State *L, int idx) |
| 90 |
{ |
| 91 |
gboolean value = lua_toboolean (L, idx); |
| 92 |
wp_spa_pod_builder_add_int (b, value ? 1 : 0); |
| 93 |
return TRUE; |
| 94 |
} |
| 95 |
|
| 96 |
static inline gboolean |
| 97 |
builder_add_int_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 98 |
lua_State *L, int idx) |
| 99 |
{ |
| 100 |
if (lua_isinteger (L, idx)) { |
| 101 |
wp_spa_pod_builder_add_int (b, lua_tointeger (L, idx)); |
| 102 |
return TRUE; |
| 103 |
} |
| 104 |
return FALSE; |
| 105 |
} |
| 106 |
|
| 107 |
static inline gboolean |
| 108 |
builder_add_int_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 109 |
lua_State *L, int idx) |
| 110 |
{ |
| 111 |
const gchar *value = lua_tostring (L, idx); |
| 112 |
wp_spa_pod_builder_add_int (b, strtol (value, NULL, 10)); |
| 113 |
return TRUE; |
| 114 |
} |
| 115 |
|
| 116 |
static inline gboolean |
| 117 |
builder_add_long_lua_boolean (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 118 |
lua_State *L, int idx) |
| 119 |
{ |
| 120 |
gboolean value = lua_toboolean (L, idx); |
| 121 |
wp_spa_pod_builder_add_long (b, value ? 1 : 0); |
| 122 |
return TRUE; |
| 123 |
} |
| 124 |
|
| 125 |
static inline gboolean |
| 126 |
builder_add_long_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 127 |
lua_State *L, int idx) |
| 128 |
{ |
| 129 |
if (lua_isinteger (L, idx)) { |
| 130 |
wp_spa_pod_builder_add_long (b, lua_tointeger (L, idx)); |
| 131 |
return TRUE; |
| 132 |
} |
| 133 |
return FALSE; |
| 134 |
} |
| 135 |
|
| 136 |
static inline gboolean |
| 137 |
builder_add_long_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 138 |
lua_State *L, int idx) |
| 139 |
{ |
| 140 |
const gchar *value = lua_tostring (L, idx); |
| 141 |
wp_spa_pod_builder_add_long (b, strtol (value, NULL, 10)); |
| 142 |
return TRUE; |
| 143 |
} |
| 144 |
|
| 145 |
static inline gboolean |
| 146 |
builder_add_float_lua_boolean (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 147 |
lua_State *L, int idx) |
| 148 |
{ |
| 149 |
gboolean value = lua_toboolean (L, idx); |
| 150 |
wp_spa_pod_builder_add_float (b, value ? 1.0f : 0.0f); |
| 151 |
return TRUE; |
| 152 |
} |
| 153 |
|
| 154 |
static inline gboolean |
| 155 |
builder_add_float_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 156 |
lua_State *L, int idx) |
| 157 |
{ |
| 158 |
if (lua_isnumber (L, idx) && !lua_isinteger (L, idx)) { |
| 159 |
wp_spa_pod_builder_add_float (b, lua_tonumber (L, idx)); |
| 160 |
return TRUE; |
| 161 |
} |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
|
| 165 |
static inline gboolean |
| 166 |
builder_add_double_lua_boolean (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 167 |
lua_State *L, int idx) |
| 168 |
{ |
| 169 |
gboolean value = lua_toboolean (L, idx); |
| 170 |
wp_spa_pod_builder_add_double (b, value ? 1.0f : 0.0f); |
| 171 |
return TRUE; |
| 172 |
} |
| 173 |
|
| 174 |
static inline gboolean |
| 175 |
builder_add_double_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 176 |
lua_State *L, int idx) |
| 177 |
{ |
| 178 |
if (lua_isnumber (L, idx) && !lua_isinteger (L, idx)) { |
| 179 |
wp_spa_pod_builder_add_double (b, lua_tonumber (L, idx)); |
| 180 |
return TRUE; |
| 181 |
} |
| 182 |
return FALSE; |
| 183 |
} |
| 184 |
|
| 185 |
static inline gboolean |
| 186 |
builder_add_string_lua_boolean (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 187 |
lua_State *L, int idx) |
| 188 |
{ |
| 189 |
gboolean value = lua_toboolean (L, idx); |
| 190 |
wp_spa_pod_builder_add_string (b, value ? "true" : "false"); |
| 191 |
return TRUE; |
| 192 |
} |
| 193 |
|
| 194 |
static inline gboolean |
| 195 |
builder_add_string_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 196 |
lua_State *L, int idx) |
| 197 |
{ |
| 198 |
g_autofree gchar *value = NULL; |
| 199 |
value = lua_isinteger (L, idx) ? |
| 200 |
g_strdup_printf ("%lld", lua_tointeger (L, idx)) : |
| 201 |
g_strdup_printf ("%f", lua_tonumber (L, idx)); |
| 202 |
wp_spa_pod_builder_add_string (b, value); |
| 203 |
return TRUE; |
| 204 |
} |
| 205 |
|
| 206 |
static inline gboolean |
| 207 |
builder_add_string_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 208 |
lua_State *L, int idx) |
| 209 |
{ |
| 210 |
const gchar *value = lua_tostring (L, idx); |
| 211 |
wp_spa_pod_builder_add_string (b, value); |
| 212 |
return TRUE; |
| 213 |
} |
| 214 |
|
| 215 |
static inline gboolean |
| 216 |
builder_add_bytes_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 217 |
lua_State *L, int idx) |
| 218 |
{ |
| 219 |
if (lua_isinteger (L, idx)) { |
| 220 |
gint64 value = lua_tointeger (L, idx); |
| 221 |
wp_spa_pod_builder_add_bytes (b, (gconstpointer)&value, sizeof (value)); |
| 222 |
} else { |
| 223 |
double value = lua_tonumber (L, idx); |
| 224 |
wp_spa_pod_builder_add_bytes (b, (gconstpointer)&value, sizeof (value)); |
| 225 |
} |
| 226 |
return TRUE; |
| 227 |
} |
| 228 |
|
| 229 |
static inline gboolean |
| 230 |
builder_add_bytes_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 231 |
lua_State *L, int idx) |
| 232 |
{ |
| 233 |
const gchar *value = lua_tostring (L, idx); |
| 234 |
wp_spa_pod_builder_add_bytes (b, (gconstpointer)value, strlen (value)); |
| 235 |
return TRUE; |
| 236 |
} |
| 237 |
|
| 238 |
static inline gboolean |
| 239 |
builder_add_fd_lua_number (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 240 |
lua_State *L, int idx) |
| 241 |
{ |
| 242 |
if (lua_isinteger (L, idx)) { |
| 243 |
wp_spa_pod_builder_add_fd (b, lua_tointeger (L, idx)); |
| 244 |
return TRUE; |
| 245 |
} |
| 246 |
return FALSE; |
| 247 |
} |
| 248 |
|
| 249 |
static inline gboolean |
| 250 |
builder_add_fd_lua_string (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 251 |
lua_State *L, int idx) |
| 252 |
{ |
| 253 |
const gchar *value = lua_tostring (L, idx); |
| 254 |
wp_spa_pod_builder_add_fd (b, strtol (value, NULL, 10)); |
| 255 |
return TRUE; |
| 256 |
} |
| 257 |
|
| 258 |
static inline gboolean |
| 259 |
is_pod_type_compatible (WpSpaType type, WpSpaPod *pod) |
| 260 |
{ |
| 261 |
/* Check if the pod type matches */ |
| 262 |
if (type == wp_spa_pod_get_spa_type (pod)) |
| 263 |
return TRUE; |
| 264 |
|
| 265 |
/* Otherwise, check if the child type of Choice pod matches */ |
| 266 |
if (wp_spa_pod_is_choice (pod)) { |
| 267 |
g_autoptr (WpSpaPod) child = wp_spa_pod_get_choice_child (pod); |
| 268 |
WpSpaType child_type = wp_spa_pod_get_spa_type (child); |
| 269 |
if (type == child_type) |
| 270 |
return TRUE; |
| 271 |
} |
| 272 |
|
| 273 |
return FALSE; |
| 274 |
} |
| 275 |
|
| 276 |
static inline gboolean |
| 277 |
builder_add_lua_userdata (WpSpaPodBuilder *b, WpSpaIdValue key_id, |
| 278 |
lua_State *L, int idx) |
| 279 |
{ |
| 280 |
WpSpaPod *pod = wplua_checkboxed (L, idx, WP_TYPE_SPA_POD); |
| 281 |
if (pod) { |
| 282 |
if (key_id) { |
| 283 |
WpSpaType prop_type = wp_spa_id_value_get_value_type (key_id, NULL); |
| 284 |
if (is_pod_type_compatible (prop_type, pod)) { |
| 285 |
wp_spa_pod_builder_add_pod (b, pod); |
| 286 |
return TRUE; |
| 287 |
} |
| 288 |
} else { |
| 289 |
wp_spa_pod_builder_add_pod (b, pod); |
| 290 |
return TRUE; |
| 291 |
} |
| 292 |
} |
| 293 |
return FALSE; |
| 294 |
} |
| 295 |
|
| 296 |
static const struct primitive_lua_type primitive_lua_types[] = { |
| 297 |
{SPA_TYPE_Bool, { |
| 298 |
[LUA_TBOOLEAN] = builder_add_boolean_lua_boolean, |
| 299 |
[LUA_TNUMBER] = builder_add_boolean_lua_number, |
| 300 |
[LUA_TSTRING] = builder_add_boolean_lua_string, |
| 301 |
}}, |
| 302 |
{SPA_TYPE_Id, { |
| 303 |
[LUA_TNUMBER] = builder_add_id_lua_number, |
| 304 |
[LUA_TSTRING] = builder_add_id_lua_string, |
| 305 |
}}, |
| 306 |
{SPA_TYPE_Int, { |
| 307 |
[LUA_TBOOLEAN] = builder_add_int_lua_boolean, |
| 308 |
[LUA_TNUMBER] = builder_add_int_lua_number, |
| 309 |
[LUA_TSTRING] = builder_add_int_lua_string, |
| 310 |
}}, |
| 311 |
{SPA_TYPE_Long, { |
| 312 |
[LUA_TBOOLEAN] = builder_add_long_lua_boolean, |
| 313 |
[LUA_TNUMBER] = builder_add_long_lua_number, |
| 314 |
[LUA_TSTRING] = builder_add_long_lua_string, |
| 315 |
}}, |
| 316 |
{SPA_TYPE_Float, { |
| 317 |
[LUA_TBOOLEAN] = builder_add_float_lua_boolean, |
| 318 |
[LUA_TNUMBER] = builder_add_float_lua_number, |
| 319 |
}}, |
| 320 |
{SPA_TYPE_Double, { |
| 321 |
[LUA_TBOOLEAN] = builder_add_double_lua_boolean, |
| 322 |
[LUA_TNUMBER] = builder_add_double_lua_number, |
| 323 |
}}, |
| 324 |
{SPA_TYPE_String, { |
| 325 |
[LUA_TBOOLEAN] = builder_add_string_lua_boolean, |
| 326 |
[LUA_TNUMBER] = builder_add_string_lua_number, |
| 327 |
[LUA_TSTRING] = builder_add_string_lua_string, |
| 328 |
}}, |
| 329 |
{SPA_TYPE_Bytes, { |
| 330 |
[LUA_TNUMBER] = builder_add_bytes_lua_number, |
| 331 |
[LUA_TSTRING] = builder_add_bytes_lua_string, |
| 332 |
}}, |
| 333 |
{SPA_TYPE_Fd, { |
| 334 |
[LUA_TNUMBER] = builder_add_fd_lua_number, |
| 335 |
[LUA_TSTRING] = builder_add_fd_lua_string, |
| 336 |
}}, |
| 337 |
{0, {}}, |
| 338 |
}; |
| 339 |
|
| 340 |
static gboolean |
| 341 |
builder_add_key (WpSpaPodBuilder *b, WpSpaIdTable table, lua_State *L, int type) |
| 342 |
{ |
| 343 |
/* Number */ |
| 344 |
if (type == LUA_TNUMBER) { |
| 345 |
wp_spa_pod_builder_add_id (b, lua_tonumber (L, -1)); |
| 346 |
return TRUE; |
| 347 |
} |
| 348 |
|
| 349 |
/* String */ |
| 350 |
else if (type == LUA_TSTRING) { |
| 351 |
const gchar *key = lua_tostring (L, -1); |
| 352 |
WpSpaIdValue val = wp_spa_id_table_find_value_from_short_name (table, key); |
| 353 |
if (val) { |
| 354 |
wp_spa_pod_builder_add_id (b, wp_spa_id_value_number (val)); |
| 355 |
return TRUE; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
return FALSE; |
| 360 |
} |
| 361 |
|
| 362 |
static gboolean |
| 363 |
builder_add_value (WpSpaPodBuilder *b, WpSpaType array_type, lua_State *L, |
| 364 |
int ltype) |
| 365 |
{ |
| 366 |
guint i; |
| 367 |
|
| 368 |
if (ltype < 0 || ltype >= MAX_LUA_TYPES) |
| 369 |
return FALSE; |
| 370 |
|
| 371 |
for (i = 0; primitive_lua_types[i].primitive_type; i++) { |
| 372 |
const struct primitive_lua_type *t = primitive_lua_types + i; |
| 373 |
if (t->primitive_type == array_type) { |
| 374 |
primitive_lua_add_func f = t->primitive_lua_add_funcs[ltype]; |
| 375 |
if (f) { |
| 376 |
gboolean v = f (b, NULL, L, -1); |
| 377 |
if (ltype != lua_type (L, -1)) |
| 378 |
g_error ("corrupted Lua stack"); |
| 379 |
return v; |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
return FALSE; |
| 384 |
} |
| 385 |
|
| 386 |
static int |
| 387 |
wp_spa_pod_lua_table_members (lua_State *L, int idx) |
| 388 |
{ |
| 389 |
size_t members = 0; |
| 390 |
|
| 391 |
lua_pushnil (L); |
| 392 |
while (lua_next (L, idx)) { |
| 393 |
lua_pop (L, 1); |
| 394 |
if (!lua_isinteger (L, -1)) |
| 395 |
luaL_error (L, "Tables used to construct POD must have only integer keys"); |
| 396 |
members++; |
| 397 |
} |
| 398 |
if ((lua_Integer)members < 0 || |
| 399 |
(size_t)(lua_Integer)members != members || |
| 400 |
(lua_Integer)members > INT_MAX) |
| 401 |
luaL_error (L, "too many table members"); |
| 402 |
return (int)members; |
| 403 |
} |
| 404 |
|
| 405 |
static int |
| 406 |
builder_add_table_inner (lua_State *L) |
| 407 |
{ |
| 408 |
WpSpaType type = WP_SPA_TYPE_INVALID; |
| 409 |
const gchar *type_name = NULL; |
| 410 |
WpSpaIdTable table = NULL; |
| 411 |
WpSpaPodBuilder *builder; |
| 412 |
lua_Integer table_key; |
| 413 |
int members; |
| 414 |
|
| 415 |
/* stack at entry: args from Lua, light userdata */ |
| 416 |
/* Last argument is the pointer pushed by builder_add_table(). */ |
| 417 |
builder = lua_touserdata (L, -1); |
| 418 |
lua_pop(L, 1); |
| 419 |
/* stack: args from Lua */ |
| 420 |
|
| 421 |
/* Exactly one argument is expected, and it must be a table. */ |
| 422 |
luaL_checktype (L, 1, LUA_TTABLE); |
| 423 |
luaL_checktype (L, 2, LUA_TNONE); |
| 424 |
/* stack: Lua table */ |
| 425 |
members = wp_spa_pod_lua_table_members (L, 1); |
| 426 |
if (members == 0) |
| 427 |
return 0; |
| 428 |
if (lua_rawgeti (L, 1, 1) != LUA_TSTRING) |
| 429 |
luaL_error (L, "must have the item type or table on its first field"); |
| 430 |
type_name = lua_tostring (L, -1); |
| 431 |
type = wp_spa_type_from_name (type_name); |
| 432 |
switch (type) { |
| 433 |
case WP_SPA_TYPE_INVALID: |
| 434 |
table = wp_spa_id_table_from_name (type_name); |
| 435 |
if (!table) |
| 436 |
luaL_error (L, "Unknown type '%s'", type_name); |
| 437 |
break; |
| 438 |
case SPA_TYPE_Bool: |
| 439 |
case SPA_TYPE_Id: |
| 440 |
case SPA_TYPE_Int: |
| 441 |
case SPA_TYPE_Long: |
| 442 |
case SPA_TYPE_Float: |
| 443 |
case SPA_TYPE_Double: |
| 444 |
case SPA_TYPE_Fd: |
| 445 |
/* no string or bytes */ |
| 446 |
break; |
| 447 |
default: |
| 448 |
luaL_error (L, "Unsupported type %" PRIu32 " for array or choice", type); |
| 449 |
} |
| 450 |
lua_pop (L, 1); |
| 451 |
for (table_key = 2; table_key <= (lua_Integer)members; table_key++) { |
| 452 |
int ltype = lua_rawgeti (L, 1, table_key); |
| 453 |
if (ltype == LUA_TNIL) |
| 454 |
luaL_error (L, "table has %d keys but is missing key %d", |
| 455 |
members, (int)table_key); |
| 456 |
if (!(table ? |
| 457 |
builder_add_key (builder, table, L, ltype) : |
| 458 |
builder_add_value (builder, type, L, ltype))) |
| 459 |
luaL_error (L, "key could not be added"); |
| 460 |
lua_pop (L, 1); |
| 461 |
} |
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
static int |
| 466 |
builder_add_table (lua_State *L, WpSpaPodBuilder *builder_) |
| 467 |
{ |
| 468 |
int call_args, status; |
| 469 |
{ |
| 470 |
g_autoptr (WpSpaPodBuilder) builder = builder_; |
| 471 |
lua_pushlightuserdata (L, builder); |
| 472 |
call_args = lua_gettop (L); |
| 473 |
lua_pushcfunction (L, builder_add_table_inner); |
| 474 |
lua_insert (L, 1); |
| 475 |
status = lua_pcall (L, call_args, 1, 0); |
| 476 |
if (status == 0) |
| 477 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_builder_end (builder)); |
| 478 |
} |
| 479 |
if (status != 0) |
| 480 |
lua_error (L); |
| 481 |
return 1; |
| 482 |
} |
| 483 |
|
| 484 |
/* None */ |
| 485 |
|
| 486 |
static int |
| 487 |
spa_pod_none_new (lua_State *L) |
| 488 |
{ |
| 489 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_none ()); |
| 490 |
return 1; |
| 491 |
} |
| 492 |
|
| 493 |
/* Boolean */ |
| 494 |
|
| 495 |
static int |
| 496 |
spa_pod_boolean_new (lua_State *L) |
| 497 |
{ |
| 498 |
gboolean value = lua_toboolean (L, 1); |
| 499 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_boolean (value)); |
| 500 |
return 1; |
| 501 |
} |
| 502 |
|
| 503 |
/* Id */ |
| 504 |
|
| 505 |
static int |
| 506 |
spa_pod_id_new (lua_State *L) |
| 507 |
{ |
| 508 |
/* Id number */ |
| 509 |
if (lua_type (L, 1) == LUA_TNUMBER) { |
| 510 |
gint64 value = lua_tointeger (L, 1); |
| 511 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_id (value)); |
| 512 |
return 1; |
| 513 |
} |
| 514 |
|
| 515 |
/* Table name and key name */ |
| 516 |
else if (lua_type (L, 1) == LUA_TSTRING) { |
| 517 |
const gchar *table_name = lua_tostring (L, 1); |
| 518 |
const gchar *key_name = lua_tostring (L, 2); |
| 519 |
WpSpaIdTable table = NULL; |
| 520 |
WpSpaIdValue value = NULL; |
| 521 |
table = wp_spa_id_table_from_name (table_name); |
| 522 |
if (!table) |
| 523 |
luaL_error (L, "table '%s' does not exist", table_name); |
| 524 |
value = wp_spa_id_table_find_value_from_short_name (table, key_name); |
| 525 |
if (!value) |
| 526 |
luaL_error (L, "key '%s' does not exist in '%s'", key_name, table_name); |
| 527 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, |
| 528 |
wp_spa_pod_new_id (wp_spa_id_value_number (value))); |
| 529 |
return 1; |
| 530 |
} |
| 531 |
|
| 532 |
/* Error */ |
| 533 |
luaL_error (L, "Invalid parameters"); |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
/* Int */ |
| 538 |
|
| 539 |
static int |
| 540 |
spa_pod_int_new (lua_State *L) |
| 541 |
{ |
| 542 |
gint64 value = lua_tointeger (L, 1); |
| 543 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_int (value)); |
| 544 |
return 1; |
| 545 |
} |
| 546 |
|
| 547 |
/* Long */ |
| 548 |
|
| 549 |
static int |
| 550 |
spa_pod_long_new (lua_State *L) |
| 551 |
{ |
| 552 |
gint64 value = lua_tointeger (L, 1); |
| 553 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_long (value)); |
| 554 |
return 1; |
| 555 |
} |
| 556 |
|
| 557 |
/* Float */ |
| 558 |
|
| 559 |
static int |
| 560 |
spa_pod_float_new (lua_State *L) |
| 561 |
{ |
| 562 |
float value = lua_tonumber (L, 1); |
| 563 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_float (value)); |
| 564 |
return 1; |
| 565 |
} |
| 566 |
|
| 567 |
/* Double */ |
| 568 |
|
| 569 |
static int |
| 570 |
spa_pod_double_new (lua_State *L) |
| 571 |
{ |
| 572 |
double value = lua_tonumber (L, 1); |
| 573 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_double (value)); |
| 574 |
return 1; |
| 575 |
} |
| 576 |
|
| 577 |
/* String */ |
| 578 |
|
| 579 |
static int |
| 580 |
spa_pod_string_new (lua_State *L) |
| 581 |
{ |
| 582 |
const gchar *value = lua_tostring (L, 1); |
| 583 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_string (value)); |
| 584 |
return 1; |
| 585 |
} |
| 586 |
|
| 587 |
/* Bytes */ |
| 588 |
|
| 589 |
static int |
| 590 |
spa_pod_bytes_new (lua_State *L) |
| 591 |
{ |
| 592 |
switch (lua_type (L, 1)) { |
| 593 |
case LUA_TNUMBER: { |
| 594 |
if (lua_isinteger (L, 1)) { |
| 595 |
guint64 value = lua_tointeger (L, 1); |
| 596 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, |
| 597 |
wp_spa_pod_new_bytes (&value, sizeof (guint64))); |
| 598 |
} else { |
| 599 |
double value = lua_tonumber (L, 1); |
| 600 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, |
| 601 |
wp_spa_pod_new_bytes (&value, sizeof (double))); |
| 602 |
} |
| 603 |
return 1; |
| 604 |
} |
| 605 |
case LUA_TSTRING: { |
| 606 |
const gchar *str = lua_tostring (L, 1); |
| 607 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, |
| 608 |
wp_spa_pod_new_bytes (str, strlen (str))); |
| 609 |
return 1; |
| 610 |
} |
| 611 |
default: |
| 612 |
luaL_error (L, "Only number and strings are valid for bytes pod"); |
| 613 |
break; |
| 614 |
} |
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
/* Pointer */ |
| 619 |
|
| 620 |
static int |
| 621 |
spa_pod_pointer_new (lua_State *L) |
| 622 |
{ |
| 623 |
const gchar *type = lua_tostring (L, 1); |
| 624 |
gconstpointer value = lua_touserdata (L, 2); |
| 625 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_pointer (type, value)); |
| 626 |
return 1; |
| 627 |
} |
| 628 |
|
| 629 |
/* Fd */ |
| 630 |
|
| 631 |
static int |
| 632 |
spa_pod_fd_new (lua_State *L) |
| 633 |
{ |
| 634 |
gint64 value = lua_tointeger (L, 1); |
| 635 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_fd (value)); |
| 636 |
return 1; |
| 637 |
} |
| 638 |
|
| 639 |
/* Rectangle */ |
| 640 |
|
| 641 |
static int |
| 642 |
spa_pod_rectangle_new (lua_State *L) |
| 643 |
{ |
| 644 |
gint64 width = lua_tointeger (L, 1); |
| 645 |
gint64 height = lua_tointeger (L, 2); |
| 646 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, |
| 647 |
wp_spa_pod_new_rectangle (width, height)); |
| 648 |
return 1; |
| 649 |
} |
| 650 |
|
| 651 |
/* Fraction */ |
| 652 |
|
| 653 |
static int |
| 654 |
spa_pod_fraction_new (lua_State *L) |
| 655 |
{ |
| 656 |
gint64 num = lua_tointeger (L, 1); |
| 657 |
gint64 denom = lua_tointeger (L, 2); |
| 658 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_new_fraction (num, denom)); |
| 659 |
return 1; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
/* Object */ |
| 664 |
|
| 665 |
static gboolean |
| 666 |
object_add_property (WpSpaPodBuilder *b, WpSpaIdTable table, |
| 667 |
const gchar *key, lua_State *L, int idx) |
| 668 |
{ |
| 669 |
guint i; |
| 670 |
WpSpaIdValue prop_id = NULL; |
| 671 |
int ltype = lua_type (L, idx); |
| 672 |
|
| 673 |
if (ltype < 0 || ltype >= MAX_LUA_TYPES) |
| 674 |
return FALSE; |
| 675 |
|
| 676 |
/* Check if we can add primitive property directly from LUA type */ |
| 677 |
prop_id = wp_spa_id_table_find_value_from_short_name (table, key); |
| 678 |
if (prop_id) { |
| 679 |
WpSpaType prop_type = wp_spa_id_value_get_value_type (prop_id, NULL); |
| 680 |
if (prop_type != WP_SPA_TYPE_INVALID) { |
| 681 |
for (i = 0; primitive_lua_types[i].primitive_type; i++) { |
| 682 |
const struct primitive_lua_type *t = primitive_lua_types + i; |
| 683 |
if (t->primitive_type == prop_type) { |
| 684 |
primitive_lua_add_func f = t->primitive_lua_add_funcs[ltype]; |
| 685 |
if (f) { |
| 686 |
wp_spa_pod_builder_add_property (b, key); |
| 687 |
return f (b, prop_id, L, idx); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
/* Otherwise just add pod property */ |
| 695 |
if (lua_type (L, idx) == LUA_TUSERDATA) { |
| 696 |
wp_spa_pod_builder_add_property (b, key); |
| 697 |
return builder_add_lua_userdata (b, prop_id, L, idx); |
| 698 |
} |
| 699 |
|
| 700 |
return FALSE; |
| 701 |
} |
| 702 |
|
| 703 |
static int |
| 704 |
spa_pod_object_new (lua_State *L) |
| 705 |
{ |
| 706 |
g_autoptr (WpSpaPodBuilder) builder = NULL; |
| 707 |
const gchar *fields[2] = { NULL, NULL }; // type_name, name_id |
| 708 |
WpSpaType object_type = WP_SPA_TYPE_INVALID; |
| 709 |
WpSpaIdTable table = NULL; |
| 710 |
|
| 711 |
luaL_checktype (L, 1, LUA_TTABLE); |
| 712 |
|
| 713 |
lua_geti (L, 1, 1); |
| 714 |
fields[0] = lua_tostring (L, -1); |
| 715 |
lua_geti (L, 1, 2); |
| 716 |
fields[1] = lua_tostring (L, -1); |
| 717 |
|
| 718 |
object_type = wp_spa_type_from_name (fields[0]); |
| 719 |
if (object_type == WP_SPA_TYPE_INVALID) |
| 720 |
luaL_error (L, "Invalid object type '%s'", fields[0]); |
| 721 |
|
| 722 |
table = wp_spa_type_get_values_table (object_type); |
| 723 |
if (!table) |
| 724 |
luaL_error (L, "Object type '%s' has incomplete type information", |
| 725 |
fields[0]); |
| 726 |
|
| 727 |
builder = wp_spa_pod_builder_new_object (fields[0], fields[1]); |
| 728 |
if (!builder) |
| 729 |
luaL_error (L, "Could not create pod object"); |
| 730 |
|
| 731 |
lua_pop (L, 2); |
| 732 |
|
| 733 |
lua_pushnil(L); |
| 734 |
while (lua_next (L, -2)) { |
| 735 |
/* Remaining fields with string keys are the object properties */ |
| 736 |
if (lua_type (L, -2) == LUA_TSTRING) { |
| 737 |
const gchar *key = lua_tostring (L, -2); |
| 738 |
if (!object_add_property (builder, table, key, L, -1)) |
| 739 |
luaL_error (L, "Property '%s' could not be added", key); |
| 740 |
} |
| 741 |
|
| 742 |
lua_pop (L, 1); |
| 743 |
} |
| 744 |
|
| 745 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_builder_end (builder)); |
| 746 |
return 1; |
| 747 |
} |
| 748 |
|
| 749 |
/* Struct */ |
| 750 |
|
| 751 |
static int |
| 752 |
spa_pod_struct_new (lua_State *L) |
| 753 |
{ |
| 754 |
g_autoptr (WpSpaPodBuilder) builder = NULL; |
| 755 |
|
| 756 |
luaL_checktype (L, 1, LUA_TTABLE); |
| 757 |
|
| 758 |
builder = wp_spa_pod_builder_new_struct (); |
| 759 |
|
| 760 |
lua_pushnil (L); |
| 761 |
while (lua_next (L, 1)) { |
| 762 |
switch (lua_type (L, -1)) { |
| 763 |
case LUA_TBOOLEAN: |
| 764 |
wp_spa_pod_builder_add_boolean (builder, lua_toboolean (L, -1)); |
| 765 |
break; |
| 766 |
case LUA_TNUMBER: |
| 767 |
if (lua_isinteger (L, -1)) |
| 768 |
wp_spa_pod_builder_add_long (builder, lua_tointeger (L, -1)); |
| 769 |
else |
| 770 |
wp_spa_pod_builder_add_double (builder, lua_tonumber (L, -1)); |
| 771 |
break; |
| 772 |
case LUA_TSTRING: |
| 773 |
wp_spa_pod_builder_add_string (builder, lua_tostring (L, -1)); |
| 774 |
break; |
| 775 |
case LUA_TUSERDATA: { |
| 776 |
WpSpaPod *pod = wplua_checkboxed (L, -1, WP_TYPE_SPA_POD); |
| 777 |
wp_spa_pod_builder_add_pod (builder, pod); |
| 778 |
break; |
| 779 |
} |
| 780 |
default: |
| 781 |
luaL_error (L, "Struct does not support lua type ", |
| 782 |
lua_typename(L, lua_type(L, -1))); |
| 783 |
break; |
| 784 |
} |
| 785 |
lua_pop (L, 1); |
| 786 |
} |
| 787 |
|
| 788 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_builder_end (builder)); |
| 789 |
return 1; |
| 790 |
} |
| 791 |
|
| 792 |
/* Sequence */ |
| 793 |
|
| 794 |
static int |
| 795 |
spa_pod_sequence_new (lua_State *L) |
| 796 |
{ |
| 797 |
g_autoptr (WpSpaPodBuilder) builder = NULL; |
| 798 |
|
| 799 |
luaL_checktype (L, 1, LUA_TTABLE); |
| 800 |
|
| 801 |
builder = wp_spa_pod_builder_new_sequence (0); |
| 802 |
|
| 803 |
lua_pushnil (L); |
| 804 |
while (lua_next (L, -2)) { |
| 805 |
guint32 offset = 0; |
| 806 |
const gchar *type_name = NULL; |
| 807 |
WpSpaPod *value = NULL; |
| 808 |
|
| 809 |
/* Read Control */ |
| 810 |
if (lua_istable(L, -1)) { |
| 811 |
lua_pushnil (L); |
| 812 |
while (lua_next (L, -2)) { |
| 813 |
const gchar *key = lua_tostring (L, -2); |
| 814 |
if (g_strcmp0 (key, "offset") == 0) { |
| 815 |
offset = lua_tointeger (L, -1); |
| 816 |
} else if (!type_name && g_strcmp0 (key, "typename") == 0) { |
| 817 |
type_name = lua_tostring (L, -1); |
| 818 |
} else if (!value && g_strcmp0 (key, "value") == 0) { |
| 819 |
switch (lua_type (L, -1)) { |
| 820 |
case LUA_TBOOLEAN: |
| 821 |
value = wp_spa_pod_new_boolean (lua_toboolean (L, -1)); |
| 822 |
break; |
| 823 |
case LUA_TNUMBER: |
| 824 |
if (lua_isinteger (L, -1)) |
| 825 |
value = wp_spa_pod_new_long (lua_tointeger (L, -1)); |
| 826 |
else |
| 827 |
value = wp_spa_pod_new_double (lua_tonumber (L, -1)); |
| 828 |
break; |
| 829 |
case LUA_TSTRING: |
| 830 |
value = wp_spa_pod_new_string (lua_tostring (L, -1)); |
| 831 |
break; |
| 832 |
case LUA_TUSERDATA: { |
| 833 |
value = wplua_checkboxed (L, -1, WP_TYPE_SPA_POD); |
| 834 |
break; |
| 835 |
} |
| 836 |
default: { |
| 837 |
luaL_error (L, "Control value does not support lua type ", |
| 838 |
lua_typename(L, lua_type(L, -1))); |
| 839 |
value = NULL; |
| 840 |
break; |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
lua_pop(L, 1); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
/* Add control */ |
| 849 |
if (type_name && value) { |
| 850 |
wp_spa_pod_builder_add_control (builder, offset, type_name); |
| 851 |
wp_spa_pod_builder_add_pod (builder, value); |
| 852 |
wp_spa_pod_unref (value); |
| 853 |
} |
| 854 |
|
| 855 |
lua_pop(L, 1); |
| 856 |
} |
| 857 |
|
| 858 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, wp_spa_pod_builder_end (builder)); |
| 859 |
return 1; |
| 860 |
} |
| 861 |
|
| 862 |
/* Array */ |
| 863 |
|
| 864 |
static int |
| 865 |
spa_pod_array_new (lua_State *L) |
| 866 |
{ |
| 867 |
WpSpaPodBuilder *builder = wp_spa_pod_builder_new_array (); |
| 868 |
return builder_add_table (L, builder); |
| 869 |
} |
| 870 |
|
| 871 |
/* Choice */ |
| 872 |
|
| 873 |
static int |
| 874 |
spa_pod_choice_none_new (lua_State *L) |
| 875 |
{ |
| 876 |
WpSpaPodBuilder *builder = wp_spa_pod_builder_new_choice ("None"); |
| 877 |
return builder_add_table (L, builder); |
| 878 |
} |
| 879 |
|
| 880 |
static int |
| 881 |
spa_pod_choice_range_new (lua_State *L) |
| 882 |
{ |
| 883 |
WpSpaPodBuilder *builder = wp_spa_pod_builder_new_choice ("Range"); |
| 884 |
return builder_add_table (L, builder); |
| 885 |
} |
| 886 |
|
| 887 |
static int |
| 888 |
spa_pod_choice_step_new (lua_State *L) |
| 889 |
{ |
| 890 |
WpSpaPodBuilder *builder = wp_spa_pod_builder_new_choice ("Step"); |
| 891 |
return builder_add_table (L, builder); |
| 892 |
} |
| 893 |
|
| 894 |
static int |
| 895 |
spa_pod_choice_enum_new (lua_State *L) |
| 896 |
{ |
| 897 |
WpSpaPodBuilder *builder = wp_spa_pod_builder_new_choice ("Enum"); |
| 898 |
/* TODO: check bool enums */ |
| 899 |
return builder_add_table (L, builder); |
| 900 |
} |
| 901 |
|
| 902 |
static int |
| 903 |
spa_pod_choice_flags_new (lua_State *L) |
| 904 |
{ |
| 905 |
WpSpaPodBuilder *builder = wp_spa_pod_builder_new_choice ("Flags"); |
| 906 |
return builder_add_table (L, builder); |
| 907 |
} |
| 908 |
|
| 909 |
/* API */ |
| 910 |
|
| 911 |
static int |
| 912 |
spa_pod_get_type_name (lua_State *L) |
| 913 |
{ |
| 914 |
WpSpaPod *pod = wplua_checkboxed (L, 1, WP_TYPE_SPA_POD); |
| 915 |
lua_pushstring (L, wp_spa_type_name (wp_spa_pod_get_spa_type (pod))); |
| 916 |
return 1; |
| 917 |
} |
| 918 |
|
| 919 |
static void |
| 920 |
push_primitive_values (lua_State *L, WpSpaPod *pod, WpSpaType type, |
| 921 |
guint start_index, WpSpaIdTable idtable) |
| 922 |
{ |
| 923 |
g_auto (GValue) item = G_VALUE_INIT; |
| 924 |
g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (pod); |
| 925 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 926 |
gpointer p = g_value_get_pointer (&item); |
| 927 |
if (!p) |
| 928 |
continue; |
| 929 |
switch (type) { |
| 930 |
case SPA_TYPE_Bool: |
| 931 |
lua_pushboolean (L, *(gboolean *)p); |
| 932 |
break; |
| 933 |
case SPA_TYPE_Id: { |
| 934 |
WpSpaIdValue idval = NULL; |
| 935 |
if (idtable) |
| 936 |
idval = wp_spa_id_table_find_value (idtable, *(guint32 *)p); |
| 937 |
if (idval) |
| 938 |
lua_pushstring (L, wp_spa_id_value_short_name (idval)); |
| 939 |
else |
| 940 |
lua_pushinteger (L, *(guint32 *)p); |
| 941 |
break; |
| 942 |
} |
| 943 |
case SPA_TYPE_Int: |
| 944 |
lua_pushinteger (L, *(gint *)p); |
| 945 |
break; |
| 946 |
case SPA_TYPE_Long: |
| 947 |
lua_pushinteger (L, *(long *)p); |
| 948 |
break; |
| 949 |
case SPA_TYPE_Float: |
| 950 |
lua_pushnumber (L, *(float *)p); |
| 951 |
break; |
| 952 |
case SPA_TYPE_Double: |
| 953 |
lua_pushnumber (L, *(double *)p); |
| 954 |
break; |
| 955 |
case SPA_TYPE_Fd: |
| 956 |
lua_pushnumber (L, *(gint64 *)p); |
| 957 |
break; |
| 958 |
default: |
| 959 |
continue; |
| 960 |
} |
| 961 |
lua_rawseti (L, -2, start_index++); |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
static void |
| 966 |
push_luapod (lua_State *L, WpSpaPod *pod, WpSpaIdValue field_idval) |
| 967 |
{ |
| 968 |
/* None */ |
| 969 |
if (wp_spa_pod_is_none (pod)) { |
| 970 |
lua_pushnil (L); |
| 971 |
} |
| 972 |
|
| 973 |
/* Boolean */ |
| 974 |
else if (wp_spa_pod_is_boolean (pod)) { |
| 975 |
gboolean value = FALSE; |
| 976 |
g_warn_if_fail (wp_spa_pod_get_boolean (pod, &value)); |
| 977 |
lua_pushboolean (L, value); |
| 978 |
} |
| 979 |
|
| 980 |
/* Id */ |
| 981 |
else if (wp_spa_pod_is_id (pod)) { |
| 982 |
guint32 value = 0; |
| 983 |
WpSpaIdTable idtable = NULL; |
| 984 |
WpSpaIdValue idval = NULL; |
| 985 |
g_warn_if_fail (wp_spa_pod_get_id (pod, &value)); |
| 986 |
if (field_idval && SPA_TYPE_Id == |
| 987 |
wp_spa_id_value_get_value_type (field_idval, &idtable)) { |
| 988 |
idval = wp_spa_id_table_find_value (idtable, value); |
| 989 |
} |
| 990 |
if (idval) |
| 991 |
lua_pushstring (L, wp_spa_id_value_short_name (idval)); |
| 992 |
else |
| 993 |
lua_pushinteger (L, value); |
| 994 |
} |
| 995 |
|
| 996 |
/* Int */ |
| 997 |
else if (wp_spa_pod_is_int (pod)) { |
| 998 |
gint value = 0; |
| 999 |
g_warn_if_fail (wp_spa_pod_get_int (pod, &value)); |
| 1000 |
lua_pushinteger (L, value); |
| 1001 |
} |
| 1002 |
|
| 1003 |
/* Long */ |
| 1004 |
else if (wp_spa_pod_is_long (pod)) { |
| 1005 |
gint64 value = 0; |
| 1006 |
wp_spa_pod_get_long (pod, &value); |
| 1007 |
lua_pushinteger (L, value); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* Float */ |
| 1011 |
else if (wp_spa_pod_is_float (pod)) { |
| 1012 |
float value = 0; |
| 1013 |
g_warn_if_fail (wp_spa_pod_get_float (pod, &value)); |
| 1014 |
lua_pushnumber (L, value); |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* Double */ |
| 1018 |
else if (wp_spa_pod_is_double (pod)) { |
| 1019 |
double value = 0; |
| 1020 |
g_warn_if_fail (wp_spa_pod_get_double (pod, &value)); |
| 1021 |
lua_pushnumber (L, value); |
| 1022 |
} |
| 1023 |
|
| 1024 |
/* String */ |
| 1025 |
else if (wp_spa_pod_is_string (pod)) { |
| 1026 |
const gchar *value = NULL; |
| 1027 |
g_warn_if_fail (wp_spa_pod_get_string (pod, &value)); |
| 1028 |
lua_pushstring (L, value); |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* Bytes */ |
| 1032 |
else if (wp_spa_pod_is_bytes (pod)) { |
| 1033 |
gconstpointer value = NULL; |
| 1034 |
guint32 size = 0; |
| 1035 |
g_warn_if_fail (wp_spa_pod_get_bytes (pod, &value, &size)); |
| 1036 |
char str[size + 1]; |
| 1037 |
for (guint i = 0; i < size; i++) |
| 1038 |
str[i] = ((const gchar *)value)[i]; |
| 1039 |
str[size] = '\0'; |
| 1040 |
lua_pushstring (L, str); |
| 1041 |
} |
| 1042 |
|
| 1043 |
/* Pointer */ |
| 1044 |
else if (wp_spa_pod_is_pointer (pod)) { |
| 1045 |
gconstpointer value = NULL; |
| 1046 |
g_warn_if_fail (wp_spa_pod_get_pointer (pod, &value)); |
| 1047 |
if (!value) |
| 1048 |
lua_pushnil (L); |
| 1049 |
else |
| 1050 |
lua_pushlightuserdata (L, (gpointer)value); |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* Fd */ |
| 1054 |
else if (wp_spa_pod_is_fd (pod)) { |
| 1055 |
gint64 value = 0; |
| 1056 |
g_warn_if_fail (wp_spa_pod_get_fd (pod, &value)); |
| 1057 |
lua_pushinteger (L, value); |
| 1058 |
} |
| 1059 |
|
| 1060 |
/* Rectangle */ |
| 1061 |
else if (wp_spa_pod_is_rectangle (pod)) { |
| 1062 |
guint32 width = 0, height = 0; |
| 1063 |
g_warn_if_fail (wp_spa_pod_get_rectangle (pod, &width, &height)); |
| 1064 |
lua_newtable (L); |
| 1065 |
lua_pushstring (L, "Rectangle"); |
| 1066 |
lua_setfield (L, -2, "pod_type"); |
| 1067 |
lua_pushinteger (L, width); |
| 1068 |
lua_setfield (L, -2, "width"); |
| 1069 |
lua_pushinteger (L, height); |
| 1070 |
lua_setfield (L, -2, "height"); |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* Fraction */ |
| 1074 |
else if (wp_spa_pod_is_fraction (pod)) { |
| 1075 |
guint32 num = 0, denom = 0; |
| 1076 |
g_warn_if_fail (wp_spa_pod_get_fraction (pod, &num, &denom)); |
| 1077 |
lua_newtable (L); |
| 1078 |
lua_pushstring (L, "Fraction"); |
| 1079 |
lua_setfield (L, -2, "pod_type"); |
| 1080 |
lua_pushinteger (L, num); |
| 1081 |
lua_setfield (L, -2, "num"); |
| 1082 |
lua_pushinteger (L, denom); |
| 1083 |
lua_setfield (L, -2, "denom"); |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* Object */ |
| 1087 |
else if (wp_spa_pod_is_object (pod)) { |
| 1088 |
WpSpaType type = wp_spa_pod_get_spa_type (pod); |
| 1089 |
WpSpaIdTable values_table = wp_spa_type_get_values_table (type); |
| 1090 |
const gchar *id_name = NULL; |
| 1091 |
g_auto (GValue) item = G_VALUE_INIT; |
| 1092 |
g_autoptr (WpIterator) it = NULL; |
| 1093 |
g_warn_if_fail (wp_spa_pod_get_object (pod, &id_name, NULL)); |
| 1094 |
lua_newtable (L); |
| 1095 |
lua_pushstring (L, "Object"); |
| 1096 |
lua_setfield (L, -2, "pod_type"); |
| 1097 |
lua_pushstring (L, id_name); |
| 1098 |
lua_setfield (L, -2, "object_id"); |
| 1099 |
it = wp_spa_pod_new_iterator (pod); |
| 1100 |
lua_newtable (L); |
| 1101 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 1102 |
WpSpaPod *prop = g_value_get_boxed (&item); |
| 1103 |
const gchar *key = NULL; |
| 1104 |
g_autoptr (WpSpaPod) val = NULL; |
| 1105 |
//FIXME: this is suboptimal because _get_property() converts |
| 1106 |
// the key to a short name and we convert it back |
| 1107 |
g_warn_if_fail (wp_spa_pod_get_property (prop, &key, &val)); |
| 1108 |
if (key) { |
| 1109 |
push_luapod (L, val, |
| 1110 |
wp_spa_id_table_find_value_from_short_name (values_table, key)); |
| 1111 |
lua_setfield (L, -2, key); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
lua_setfield (L, -2, "properties"); |
| 1115 |
} |
| 1116 |
|
| 1117 |
/* Struct */ |
| 1118 |
else if (wp_spa_pod_is_struct (pod)) { |
| 1119 |
g_auto (GValue) item = G_VALUE_INIT; |
| 1120 |
g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (pod); |
| 1121 |
guint i = 1; |
| 1122 |
lua_newtable (L); |
| 1123 |
lua_pushstring (L, "Struct"); |
| 1124 |
lua_setfield (L, -2, "pod_type"); |
| 1125 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 1126 |
WpSpaPod *val = g_value_get_boxed (&item); |
| 1127 |
push_luapod (L, val, NULL); |
| 1128 |
lua_rawseti (L, -2, i++); |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
/* Sequence */ |
| 1133 |
else if (wp_spa_pod_is_sequence (pod)) { |
| 1134 |
g_auto (GValue) item = G_VALUE_INIT; |
| 1135 |
g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (pod); |
| 1136 |
guint i = 1; |
| 1137 |
lua_newtable (L); |
| 1138 |
lua_pushstring (L, "Sequence"); |
| 1139 |
lua_setfield (L, -2, "pod_type"); |
| 1140 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 1141 |
WpSpaPod *control = g_value_get_boxed (&item); |
| 1142 |
guint32 offset = 0; |
| 1143 |
const char *type_name = NULL; |
| 1144 |
g_autoptr (WpSpaPod) val = NULL; |
| 1145 |
g_warn_if_fail (wp_spa_pod_get_control (control, &offset, &type_name, |
| 1146 |
&val)); |
| 1147 |
lua_newtable (L); |
| 1148 |
lua_pushinteger (L, offset); |
| 1149 |
lua_setfield (L, -2, "offset"); |
| 1150 |
lua_pushstring (L, type_name); |
| 1151 |
lua_setfield (L, -2, "typename"); |
| 1152 |
push_luapod (L, val, NULL); |
| 1153 |
lua_setfield (L, -2, "value"); |
| 1154 |
lua_rawseti (L, -2, i++); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* Array */ |
| 1159 |
else if (wp_spa_pod_is_array (pod)) { |
| 1160 |
g_autoptr (WpSpaPod) child = wp_spa_pod_get_array_child (pod); |
| 1161 |
WpSpaType type = wp_spa_pod_get_spa_type (child); |
| 1162 |
WpSpaIdTable idtable = NULL; |
| 1163 |
if (field_idval && type == SPA_TYPE_Id && SPA_TYPE_Array == |
| 1164 |
wp_spa_id_value_get_value_type (field_idval, &idtable)) |
| 1165 |
wp_spa_id_value_array_get_item_type (field_idval, &idtable); |
| 1166 |
lua_newtable (L); |
| 1167 |
lua_pushstring (L, "Array"); |
| 1168 |
lua_setfield (L, -2, "pod_type"); |
| 1169 |
lua_pushstring (L, wp_spa_type_name (type)); |
| 1170 |
lua_setfield (L, -2, "value_type"); |
| 1171 |
push_primitive_values (L, pod, type, 1, idtable); |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* Choice */ |
| 1175 |
else if (wp_spa_pod_is_choice (pod)) { |
| 1176 |
g_autoptr (WpSpaPod) child = wp_spa_pod_get_choice_child (pod); |
| 1177 |
WpSpaType type = wp_spa_pod_get_spa_type (child); |
| 1178 |
g_autofree const gchar *choice_type = g_strdup_printf ("Choice.%s", |
| 1179 |
wp_spa_id_value_short_name (wp_spa_pod_get_choice_type (pod))); |
| 1180 |
WpSpaIdTable idtable = NULL; |
| 1181 |
if (field_idval && type == SPA_TYPE_Id) |
| 1182 |
wp_spa_id_value_get_value_type (field_idval, &idtable); |
| 1183 |
lua_newtable (L); |
| 1184 |
lua_pushstring (L, choice_type); |
| 1185 |
lua_setfield (L, -2, "pod_type"); |
| 1186 |
lua_pushstring (L, wp_spa_type_name (type)); |
| 1187 |
lua_setfield (L, -2, "value_type"); |
| 1188 |
push_primitive_values (L, pod, type, 1, idtable); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/* Error */ |
| 1192 |
else { |
| 1193 |
luaL_error (L, "Unsupported pod type ", |
| 1194 |
wp_spa_type_name (wp_spa_pod_get_spa_type (pod))); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
static int |
| 1199 |
spa_pod_parse (lua_State *L) |
| 1200 |
{ |
| 1201 |
WpSpaPod *pod = wplua_checkboxed (L, 1, WP_TYPE_SPA_POD); |
| 1202 |
push_luapod (L, pod, NULL); |
| 1203 |
return 1; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static int |
| 1207 |
spa_pod_fixate (lua_State *L) |
| 1208 |
{ |
| 1209 |
WpSpaPod *pod = wplua_checkboxed (L, 1, WP_TYPE_SPA_POD); |
| 1210 |
gboolean res = wp_spa_pod_fixate (pod); |
| 1211 |
lua_pushboolean (L, res); |
| 1212 |
return 1; |
| 1213 |
} |
| 1214 |
|
| 1215 |
static inline WpSpaPod * |
| 1216 |
_checkpod (lua_State *L, int n) |
| 1217 |
{ |
| 1218 |
return wplua_checkboxed (L, n, WP_TYPE_SPA_POD); |
| 1219 |
} |
| 1220 |
|
| 1221 |
static int |
| 1222 |
spa_pod_filter (lua_State *L) |
| 1223 |
{ |
| 1224 |
WpSpaPod *pod = wplua_checkboxed (L, 1, WP_TYPE_SPA_POD); |
| 1225 |
WpSpaPod *filter = luaL_opt (L, _checkpod, 2, NULL); |
| 1226 |
WpSpaPod *result = wp_spa_pod_filter (pod, filter); |
| 1227 |
if (result) { |
| 1228 |
wplua_pushboxed (L, WP_TYPE_SPA_POD, result); |
| 1229 |
return 1; |
| 1230 |
} |
| 1231 |
return 0; |
| 1232 |
} |
| 1233 |
|
| 1234 |
static const luaL_Reg spa_pod_methods[] = { |
| 1235 |
{ "get_type_name", spa_pod_get_type_name }, |
| 1236 |
{ "parse", spa_pod_parse }, |
| 1237 |
{ "fixate", spa_pod_fixate }, |
| 1238 |
{ "filter", spa_pod_filter }, |
| 1239 |
{ NULL, NULL } |
| 1240 |
}; |
| 1241 |
|
| 1242 |
static const luaL_Reg spa_pod_constructors[] = { |
| 1243 |
{ "None", spa_pod_none_new }, |
| 1244 |
{ "Boolean", spa_pod_boolean_new }, |
| 1245 |
{ "Id", spa_pod_id_new }, |
| 1246 |
{ "Int", spa_pod_int_new }, |
| 1247 |
{ "Long", spa_pod_long_new }, |
| 1248 |
{ "Float", spa_pod_float_new }, |
| 1249 |
{ "Double", spa_pod_double_new }, |
| 1250 |
{ "String", spa_pod_string_new }, |
| 1251 |
{ "Bytes", spa_pod_bytes_new }, |
| 1252 |
{ "Pointer", spa_pod_pointer_new }, |
| 1253 |
{ "Fd", spa_pod_fd_new }, |
| 1254 |
{ "Rectangle", spa_pod_rectangle_new }, |
| 1255 |
{ "Fraction", spa_pod_fraction_new }, |
| 1256 |
{ "Object", spa_pod_object_new }, |
| 1257 |
{ "Struct", spa_pod_struct_new }, |
| 1258 |
{ "Sequence", spa_pod_sequence_new }, |
| 1259 |
{ "Array", spa_pod_array_new }, |
| 1260 |
{ NULL, NULL } |
| 1261 |
}; |
| 1262 |
|
| 1263 |
static const luaL_Reg spa_pod_choice_constructors[] = { |
| 1264 |
{ "None", spa_pod_choice_none_new }, |
| 1265 |
{ "Range", spa_pod_choice_range_new }, |
| 1266 |
{ "Step", spa_pod_choice_step_new }, |
| 1267 |
{ "Enum", spa_pod_choice_enum_new }, |
| 1268 |
{ "Flags", spa_pod_choice_flags_new }, |
| 1269 |
{ NULL, NULL } |
| 1270 |
}; |
| 1271 |
|
| 1272 |
/* Init */ |
| 1273 |
|
| 1274 |
void |
| 1275 |
wp_lua_scripting_pod_init (lua_State *L) |
| 1276 |
{ |
| 1277 |
luaL_newlib (L, spa_pod_constructors); |
| 1278 |
luaL_newlib (L, spa_pod_choice_constructors); |
| 1279 |
lua_setfield (L, -2, "Choice"); |
| 1280 |
lua_setglobal (L, "WpSpaPod"); |
| 1281 |
|
| 1282 |
wplua_register_type_methods (L, WP_TYPE_SPA_POD, NULL, spa_pod_methods); |
| 1283 |
} |