Debian Codegraph @

modules/module-lua-scripting/api/api.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <glib/gstdio.h>
10 #include <wp/wp.h>
11 #include <pipewire/pipewire.h>
12 #include <wplua/wplua.h>
13 #include <libintl.h>
14
15 #define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
16 WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
17
18 #define URI_API "resource:///org/freedesktop/pipewire/wireplumber/m-lua-scripting/api.lua"
19
20 void wp_lua_scripting_pod_init (lua_State *L);
21 void wp_lua_scripting_json_init (lua_State *L);
22 void push_luajson (lua_State *L, WpSpaJson *json, gint n_recursions);
23
24 /* helpers */
25
26 static WpCore *
27 get_wp_core (lua_State *L)
28 {
29 WpCore *core = NULL;
30 lua_pushliteral (L, "wireplumber_core");
31 lua_gettable (L, LUA_REGISTRYINDEX);
32 core = lua_touserdata (L, -1);
33 lua_pop (L, 1);
34 return core;
35 }
36
37 static WpCore *
38 get_wp_export_core (lua_State *L)
39 {
40 WpCore *core = get_wp_core (L);
41 g_autoptr (WpCore) export_core = wp_core_get_export_core (core);
42 return export_core ? export_core : core;
43 }
44
45 /* GLib */
46
47 static int
48 glib_get_monotonic_time (lua_State *L)
49 {
50 lua_pushinteger (L, g_get_monotonic_time ());
51 return 1;
52 }
53
54 static int
55 glib_get_real_time (lua_State *L)
56 {
57 lua_pushinteger (L, g_get_real_time ());
58 return 1;
59 }
60
61 static gboolean
62 access_parse_mode (const gchar * mode_str, gint *mode)
63 {
64 *mode = 0;
65
66 if (!mode_str)
67 return FALSE;
68 else {
69 for (guint i = 0; i < strlen (mode_str); i++) {
70 switch (mode_str[i]) {
71 case 'r': *mode |= R_OK; break;
72 case 'w': *mode |= W_OK; break;
73 case 'x': *mode |= X_OK; break;
74 case 'f': *mode |= F_OK; break;
75 case '-': break;
76 default:
77 return FALSE;
78 }
79 }
80 }
81 return TRUE;
82 }
83
84 static int
85 glib_access (lua_State *L)
86 {
87 const gchar *filename = luaL_checkstring (L, 1);
88 int mode = 0;
89 if (!access_parse_mode (luaL_checkstring (L, 2), &mode))
90 luaL_error (L, "invalid mode string: '%s'", lua_tostring (L, 2));
91 lua_pushboolean (L, g_access (filename, mode) >= 0);
92 return 1;
93 }
94
95 static const luaL_Reg glib_methods[] = {
96 { "get_monotonic_time", glib_get_monotonic_time },
97 { "get_real_time", glib_get_real_time },
98 { "access", glib_access },
99 { NULL, NULL }
100 };
101
102 /* GSource */
103
104 static int
105 source_destroy (lua_State *L)
106 {
107 GSource *source = wplua_checkboxed (L, 1, G_TYPE_SOURCE);
108 g_source_destroy (source);
109 return 0;
110 }
111
112 static const luaL_Reg source_methods[] = {
113 { "destroy", source_destroy },
114 { NULL, NULL }
115 };
116
117 /* i18n */
118
119 static int
120 i18n_gettext (lua_State *L)
121 {
122 const gchar * msgid = luaL_checkstring (L, 1);
123 lua_pushstring (L, dgettext (GETTEXT_PACKAGE, msgid));
124 return 1;
125 }
126
127 static int
128 i18n_ngettext (lua_State *L)
129 {
130 const gchar * msgid = luaL_checkstring (L, 1);
131 const gchar *msgid_plural = luaL_checkstring (L, 2);
132 gulong n = luaL_checkinteger (L, 3);
133 lua_pushstring (L, dngettext (GETTEXT_PACKAGE, msgid, msgid_plural, n));
134 return 1;
135 }
136
137 static const luaL_Reg i18n_funcs[] = {
138 { "gettext", i18n_gettext },
139 { "ngettext", i18n_ngettext },
140 { NULL, NULL }
141 };
142
143 /* WpCore */
144
145 static int
146 core_get_properties (lua_State *L)
147 {
148 WpCore * core = get_wp_core (L);
149 WpProperties *p = wp_core_get_properties (core);
150 wplua_pushboxed (L, WP_TYPE_PROPERTIES, p);
151 return 1;
152 }
153
154 static int
155 core_get_info (lua_State *L)
156 {
157 WpCore * core = get_wp_core (L);
158 WpProperties *p = wp_core_get_remote_properties (core);
159
160 lua_newtable (L);
161 lua_pushinteger (L, wp_core_get_remote_cookie (core));
162 lua_setfield (L, -2, "cookie");
163 lua_pushstring (L, wp_core_get_remote_name (core));
164 lua_setfield (L, -2, "name");
165 lua_pushstring (L, wp_core_get_remote_user_name (core));
166 lua_setfield (L, -2, "user_name");
167 lua_pushstring (L, wp_core_get_remote_host_name (core));
168 lua_setfield (L, -2, "host_name");
169 lua_pushstring (L, wp_core_get_remote_version (core));
170 lua_setfield (L, -2, "version");
171 wplua_pushboxed (L, WP_TYPE_PROPERTIES, p);
172 lua_setfield (L, -2, "properties");
173 return 1;
174 }
175
176 static int
177 core_get_vm_type (lua_State *L)
178 {
179 WpCore * core = get_wp_core (L);
180 g_autofree gchar *vm = wp_core_get_vm_type (core);
181 lua_pushstring (L, vm);
182 return 1;
183 }
184
185 static int
186 core_get_own_bound_id (lua_State *L)
187 {
188 WpCore * core = get_wp_core (L);
189 guint32 id = wp_core_get_own_bound_id (core);
190 lua_pushinteger (L, id);
191 return 1;
192 }
193
194 static int
195 core_idle_add (lua_State *L)
196 {
197 GSource *source = NULL;
198 luaL_checktype (L, 1, LUA_TFUNCTION);
199 wp_core_idle_add_closure (get_wp_core (L), &source,
200 wplua_function_to_closure (L, 1));
201 wplua_pushboxed (L, G_TYPE_SOURCE, source);
202 return 1;
203 }
204
205 static int
206 core_timeout_add (lua_State *L)
207 {
208 GSource *source = NULL;
209 lua_Integer timeout_ms = luaL_checkinteger (L, 1);
210 luaL_checktype (L, 2, LUA_TFUNCTION);
211 wp_core_timeout_add_closure (get_wp_core (L), &source, timeout_ms,
212 wplua_function_to_closure (L, 2));
213 wplua_pushboxed (L, G_TYPE_SOURCE, source);
214 return 1;
215 }
216
217 static void
218 on_core_done (WpCore * core, GAsyncResult * res, GClosure * closure)
219 {
220 g_autoptr (GError) error = NULL;
221 GValue val = G_VALUE_INIT;
222 int n_vals = 0;
223
224 if (!wp_core_sync_finish (core, res, &error)) {
225 g_value_init (&val, G_TYPE_STRING);
226 g_value_set_string (&val, error->message);
227 n_vals = 1;
228 }
229 g_closure_invoke (closure, NULL, n_vals, &val, NULL);
230 g_value_unset (&val);
231 g_closure_invalidate (closure);
232 g_closure_unref (closure);
233 }
234
235 static int
236 core_sync (lua_State *L)
237 {
238 luaL_checktype (L, 1, LUA_TFUNCTION);
239 GClosure * closure = wplua_function_to_closure (L, 1);
240 g_closure_sink (g_closure_ref (closure));
241 wp_core_sync (get_wp_core (L), NULL, (GAsyncReadyCallback) on_core_done,
242 closure);
243 return 0;
244 }
245
246 static gboolean
247 core_disconnect (WpCore * core)
248 {
249 wp_core_disconnect (core);
250 return G_SOURCE_REMOVE;
251 }
252
253 static int
254 core_quit (lua_State *L)
255 {
256 WpCore * core = get_wp_core (L);
257 g_autoptr (WpProperties) p = wp_core_get_properties (core);
258 const gchar *daemon = wp_properties_get (p, "wireplumber.daemon");
259 if (!g_strcmp0 (daemon, "true")) {
260 wp_warning ("script attempted to quit, but the engine is "
261 "running in the wireplumber daemon; ignoring");
262 return 0;
263 }
264
265 /* wp_core_disconnect() will immediately destroy the lua plugin
266 and the lua engine, so we cannot call it directly */
267 wp_core_idle_add (core, NULL, G_SOURCE_FUNC (core_disconnect), core, NULL);
268 return 0;
269 }
270
271 #include "require.c"
272
273 static int
274 core_require_api (lua_State *L)
275 {
276 WpCore * core = get_wp_core (L);
277 g_autoptr (WpProperties) p = wp_core_get_properties (core);
278 const gchar *daemon = wp_properties_get (p, "wireplumber.daemon");
279 if (!g_strcmp0 (daemon, "true")) {
280 wp_warning ("script attempted to load an API module, but the engine is "
281 "running in the wireplumber daemon; ignoring");
282 return 0;
283 }
284 return wp_require_api_transition_new_from_lua (L, core);
285 }
286
287 static int
288 core_test_feature (lua_State *L)
289 {
290 WpCore *core = get_wp_core(L);
291 const char *f = luaL_checkstring (L, 1);
292 lua_pushboolean (L, wp_core_test_feature (core, f));
293 return 1;
294 }
295
296 static int
297 core_update_properties (lua_State *L)
298 {
299 WpCore *core = get_wp_core(L);
300 WpProperties *props = NULL;
301 if (lua_istable (L, 1))
302 props = wplua_table_to_properties (L, 1);
303 else
304 props = wp_properties_ref (wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES));
305
306 wp_core_update_properties (core, props);
307 return 0;
308 }
309
310 static const luaL_Reg core_funcs[] = {
311 { "get_properties", core_get_properties },
312 { "get_info", core_get_info },
313 { "get_vm_type", core_get_vm_type },
314 { "get_own_bound_id", core_get_own_bound_id },
315 { "idle_add", core_idle_add },
316 { "timeout_add", core_timeout_add },
317 { "sync", core_sync },
318 { "quit", core_quit },
319 { "require_api", core_require_api },
320 { "test_feature", core_test_feature },
321 { "update_properties", core_update_properties },
322 { NULL, NULL }
323 };
324
325 /* WpLog */
326
327 typedef WpLogTopic WpLuaLogTopic;
328
329 static WpLuaLogTopic *
330 wp_lua_log_topic_new (const char *name)
331 {
332 WpLuaLogTopic *topic = g_new0 (WpLuaLogTopic, 1);
333 topic->topic_name = g_ref_string_new (name);
334 wp_log_topic_register (topic);
335 return topic;
336 }
337
338 static WpLuaLogTopic *
339 wp_lua_log_topic_copy (WpLuaLogTopic *topic)
340 {
341 WpLuaLogTopic *copy = g_new0 (WpLuaLogTopic, 1);
342 copy->topic_name = g_ref_string_acquire ((char *) topic->topic_name);
343 wp_log_topic_register (copy);
344 return copy;
345 }
346
347 static void
348 wp_lua_log_topic_free (WpLuaLogTopic *topic)
349 {
350 wp_log_topic_unregister (topic);
351 g_ref_string_release ((char *) topic->topic_name);
352 g_free (topic);
353 }
354
355 G_DEFINE_BOXED_TYPE (WpLuaLogTopic, wp_lua_log_topic, wp_lua_log_topic_copy,
356 wp_lua_log_topic_free)
357
358 static int
359 log_log (lua_State *L, GLogLevelFlags lvl)
360 {
361 lua_Debug ar = {0};
362 const gchar *message;
363 gchar line_str[11];
364 gconstpointer instance = NULL;
365 GType type = G_TYPE_INVALID;
366 int index = 1;
367 WpLogTopic *topic = log_topic_lua_scripting;
368
369 /* if called with log topic object */
370 if (lua_istable (L, index)) {
371 if (lua_getmetatable (L, index)) {
372 lua_getfield (L, -1, "__topic");
373 if (wplua_isboxed (L, -1, wp_lua_log_topic_get_type ())) {
374 topic = wplua_toboxed (L, -1);
375 }
376 lua_pop (L, 2);
377 }
378 index++;
379 }
380
381 if (!wp_log_topic_is_enabled (topic, lvl))
382 return 0;
383
384 g_warn_if_fail (lua_getstack (L, 1, &ar) == 1);
385 g_warn_if_fail (lua_getinfo (L, "nSl", &ar) == 1);
386
387 if (wplua_isobject (L, index, G_TYPE_OBJECT)) {
388 instance = wplua_toobject (L, index);
389 type = G_TYPE_FROM_INSTANCE (instance);
390 index++;
391 }
392 else if (wplua_isboxed (L, index, G_TYPE_BOXED)) {
393 instance = wplua_toboxed (L, index);
394 type = wplua_gvalue_userdata_type (L, index);
395 index++;
396 }
397
398 message = luaL_checkstring (L, index);
399 snprintf (line_str, 11, "%d", ar.currentline);
400 ar.name = ar.name ? ar.name : "chunk";
401
402 wp_logt_checked (topic, lvl,
403 ar.source, line_str, ar.name, type, instance, "%s", message);
404 return 0;
405 }
406
407 static int
408 log_warning (lua_State *L) { return log_log (L, G_LOG_LEVEL_WARNING); }
409
410 static int
411 log_notice (lua_State *L) { return log_log (L, G_LOG_LEVEL_MESSAGE); }
412
413 static int
414 log_info (lua_State *L) { return log_log (L, G_LOG_LEVEL_INFO); }
415
416 static int
417 log_debug (lua_State *L) { return log_log (L, G_LOG_LEVEL_DEBUG); }
418
419 static int
420 log_trace (lua_State *L) { return log_log (L, WP_LOG_LEVEL_TRACE); }
421
422 static const luaL_Reg log_obj_funcs[] = {
423 { "warning", log_warning },
424 { "notice", log_notice },
425 { "info", log_info },
426 { "debug", log_debug },
427 { "trace", log_trace },
428 { NULL, NULL }
429 };
430
431 static int
432 log_open_topic (lua_State *L)
433 {
434 const char *name = luaL_checkstring (L, 1);
435 WpLuaLogTopic *topic = wp_lua_log_topic_new (name);
436
437 lua_newtable (L); // empty table
438 lua_newtable (L); // metatable
439 luaL_newlib (L, log_obj_funcs);
440 lua_setfield (L, -2, "__index");
441 wplua_pushboxed (L, wp_lua_log_topic_get_type (), topic);
442 lua_setfield (L, -2, "__topic");
443 lua_setmetatable (L, -2);
444 return 1;
445 }
446
447 static const luaL_Reg log_funcs[] = {
448 { "open_topic", log_open_topic },
449 { "warning", log_warning },
450 { "notice", log_notice },
451 { "info", log_info },
452 { "debug", log_debug },
453 { "trace", log_trace },
454 { NULL, NULL }
455 };
456
457 /* WpPlugin */
458
459 static int
460 plugin_find (lua_State *L)
461 {
462 const char *name = luaL_checkstring (L, 1);
463 WpPlugin *plugin = wp_plugin_find (get_wp_core (L), name);
464 if (plugin)
465 wplua_pushobject (L, plugin);
466 else
467 lua_pushnil (L);
468 return 1;
469 }
470
471 static const luaL_Reg plugin_funcs[] = {
472 { "find", plugin_find },
473 { NULL, NULL }
474 };
475
476 /* WpObject */
477
478 static void
479 object_activate_done (WpObject *o, GAsyncResult * res, GClosure * closure)
480 {
481 g_autoptr (GError) error = NULL;
482 GValue val[2] = { G_VALUE_INIT, G_VALUE_INIT };
483 int n_vals = 1;
484
485 if (!wp_object_activate_finish (o, res, &error)) {
486 wp_debug_object (o, "%s", error->message);
487 if (closure) {
488 g_value_init (&val[1], G_TYPE_STRING);
489 g_value_set_string (&val[1], error->message);
490 n_vals = 2;
491 }
492 }
493 if (closure) {
494 g_value_init_from_instance (&val[0], o);
495 g_closure_invoke (closure, NULL, n_vals, val, NULL);
496 g_value_unset (&val[0]);
497 g_value_unset (&val[1]);
498 g_closure_invalidate (closure);
499 g_closure_unref (closure);
500 }
501 }
502
503 static int
504 object_activate (lua_State *L)
505 {
506 WpObject *o = wplua_checkobject (L, 1, WP_TYPE_OBJECT);
507 WpObjectFeatures features = luaL_checkinteger (L, 2);
508 GClosure * closure = luaL_opt (L, wplua_checkclosure, 3, NULL);
509 if (closure)
510 g_closure_sink (g_closure_ref (closure));
511 wp_object_activate (o, features, NULL,
512 (GAsyncReadyCallback) object_activate_done, closure);
513 return 0;
514 }
515
516 static int
517 object_deactivate (lua_State *L)
518 {
519 WpObject *o = wplua_checkobject (L, 1, WP_TYPE_OBJECT);
520 WpObjectFeatures features = luaL_checkinteger (L, 2);
521 wp_object_deactivate (o, features);
522 return 0;
523 }
524
525 static int
526 object_get_active_features (lua_State *L)
527 {
528 WpObject *o = wplua_checkobject (L, 1, WP_TYPE_OBJECT);
529 WpObjectFeatures features = wp_object_get_active_features (o);
530 lua_pushinteger (L, features);
531 return 1;
532 }
533
534 static int
535 object_get_supported_features (lua_State *L)
536 {
537 WpObject *o = wplua_checkobject (L, 1, WP_TYPE_OBJECT);
538 WpObjectFeatures features = wp_object_get_supported_features (o);
539 lua_pushinteger (L, features);
540 return 1;
541 }
542
543 static const luaL_Reg object_methods[] = {
544 { "activate", object_activate },
545 { "deactivate", object_deactivate },
546 { "get_active_features", object_get_active_features },
547 { "get_supported_features", object_get_supported_features },
548 { NULL, NULL }
549 };
550
551 /* WpProxy */
552
553 static int
554 proxy_get_interface_type (lua_State *L)
555 {
556 WpProxy * p = wplua_checkobject (L, 1, WP_TYPE_PROXY);
557 guint32 version = 0;
558 const gchar *type = wp_proxy_get_interface_type (p, &version);
559 lua_pushstring (L, type);
560 lua_pushinteger (L, version);
561 return 2;
562 }
563
564 static const luaL_Reg proxy_methods[] = {
565 { "get_interface_type", proxy_get_interface_type },
566 { NULL, NULL }
567 };
568
569 /* WpGlobalProxy */
570
571 static int
572 global_proxy_request_destroy (lua_State *L)
573 {
574 WpGlobalProxy * p = wplua_checkobject (L, 1, WP_TYPE_GLOBAL_PROXY);
575 wp_global_proxy_request_destroy (p);
576 return 0;
577 }
578
579 static const luaL_Reg global_proxy_methods[] = {
580 { "request_destroy", global_proxy_request_destroy },
581 { NULL, NULL }
582 };
583
584 /* WpIterator */
585
586 static int
587 iterator_next (lua_State *L)
588 {
589 WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR);
590 g_auto (GValue) v = G_VALUE_INIT;
591 if (it && wp_iterator_next (it, &v)) {
592 return wplua_gvalue_to_lua (L, &v);
593 } else {
594 lua_pushnil (L);
595 return 1;
596 }
597 }
598
599 static int
600 push_wpiterator (lua_State *L, WpIterator *it)
601 {
602 lua_pushcfunction (L, iterator_next);
603 wplua_pushboxed (L, WP_TYPE_ITERATOR, it);
604 return 2;
605 }
606
607 static int
608 iterator_reset (lua_State *L)
609 {
610 WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR);
611 wp_iterator_reset (it);
612 return 0;
613 }
614
615 static int
616 iterator_iterate (lua_State *L)
617 {
618 WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR);
619 return push_wpiterator (L, wp_iterator_ref (it));
620 }
621
622 static const luaL_Reg iterator_funcs[] = {
623 { "next", iterator_next },
624 { "reset", iterator_reset },
625 { "iterate", iterator_iterate },
626 { NULL, NULL }
627 };
628
629 /* Settings WpIterator */
630
631 static int
632 settings_iterator_next (lua_State *L)
633 {
634 WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR);
635 g_auto (GValue) item = G_VALUE_INIT;
636 if (wp_iterator_next (it, &item)) {
637 WpSettingsItem *si = g_value_get_boxed (&item);
638 const gchar *k = wp_settings_item_get_key (si);
639 WpSpaJson *v = wp_settings_item_get_value (si);
640 lua_pushstring (L, k);
641 wplua_pushboxed (L, WP_TYPE_SPA_JSON, v);
642 return 2;
643 } else {
644 lua_pushnil (L);
645 return 1;
646 }
647 }
648
649 static int
650 push_settings_wpiterator (lua_State *L, WpIterator *it)
651 {
652 lua_pushcfunction (L, settings_iterator_next);
653 wplua_pushboxed (L, WP_TYPE_ITERATOR, it);
654 return 2;
655 }
656
657 /* Metadata WpIterator */
658
659 static int
660 metadata_iterator_next (lua_State *L)
661 {
662 WpIterator *it = wplua_checkboxed (L, 1, WP_TYPE_ITERATOR);
663 g_auto (GValue) item = G_VALUE_INIT;
664 if (wp_iterator_next (it, &item)) {
665 WpMetadataItem *mi = g_value_get_boxed (&item);
666 guint32 s = wp_metadata_item_get_subject (mi);
667 const gchar *k = wp_metadata_item_get_key (mi);
668 const gchar *t = wp_metadata_item_get_value_type (mi);
669 const gchar *v = wp_metadata_item_get_value (mi);
670 lua_pushinteger (L, s);
671 lua_pushstring (L, k);
672 lua_pushstring (L, t);
673 lua_pushstring (L, v);
674 return 4;
675 } else {
676 lua_pushnil (L);
677 return 1;
678 }
679 }
680
681 static int
682 push_metadata_wpiterator (lua_State *L, WpIterator *it)
683 {
684 lua_pushcfunction (L, metadata_iterator_next);
685 wplua_pushboxed (L, WP_TYPE_ITERATOR, it);
686 return 2;
687 }
688
689 /* WpObjectInterest */
690
691 static GVariant *
692 constraint_value_to_variant (lua_State *L, int idx)
693 {
694 switch (lua_type (L, idx)) {
695 case LUA_TBOOLEAN:
696 return g_variant_new_boolean (lua_toboolean (L, idx));
697 case LUA_TSTRING:
698 return g_variant_new_string (lua_tostring (L, idx));
699 case LUA_TNUMBER:
700 if (lua_isinteger (L, idx))
701 return g_variant_new_int64 (lua_tointeger (L, idx));
702 else
703 return g_variant_new_double (lua_tonumber (L, idx));
704 default:
705 return NULL;
706 }
707 }
708
709 static void
710 object_interest_new_add_constraint (lua_State *L, GType type,
711 WpObjectInterest *interest)
712 {
713 int constraint_idx;
714 WpConstraintType ctype;
715 const gchar *subject;
716 WpConstraintVerb verb;
717 GVariant *value = NULL;
718
719 constraint_idx = lua_absindex (L, -1);
720
721 /* verify this is a Constraint{} */
722 if (lua_type (L, constraint_idx) != LUA_TTABLE) {
723 luaL_error (L, "Interest: expected Constraint at index %d",
724 lua_tointeger (L, -2));
725 }
726
727 if (luaL_getmetafield (L, constraint_idx, "__name") == LUA_TNIL ||
728 g_strcmp0 (lua_tostring (L, -1), "Constraint") != 0) {
729 luaL_error (L, "Interest: expected Constraint at index %d",
730 lua_tointeger (L, -2));
731 }
732 lua_pop (L, 1);
733
734 /* get the constraint type */
735 lua_pushliteral (L, "type");
736 if (lua_gettable (L, constraint_idx) == LUA_TNUMBER)
737 ctype = lua_tointeger (L, -1);
738 else
739 ctype = WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY;
740 lua_pop (L, 1);
741
742 /* get t[1] (the subject) and t[2] (the verb) */
743 lua_geti (L, constraint_idx, 1);
744 subject = lua_tostring (L, -1);
745
746 lua_geti (L, constraint_idx, 2);
747 verb = lua_tostring (L, -1)[0];
748
749 switch (verb) {
750 case WP_CONSTRAINT_VERB_EQUALS:
751 case WP_CONSTRAINT_VERB_NOT_EQUALS:
752 case WP_CONSTRAINT_VERB_MATCHES: {
753 lua_geti (L, constraint_idx, 3);
754 value = constraint_value_to_variant (L, -1);
755 if (G_UNLIKELY (!value))
756 luaL_error (L, "Constraint: bad value type");
757 break;
758 }
759 case WP_CONSTRAINT_VERB_IN_RANGE: {
760 GVariant *values[2];
761 lua_geti (L, constraint_idx, 3);
762 lua_geti (L, constraint_idx, 4);
763 values[0] = constraint_value_to_variant (L, -2);
764 values[1] = constraint_value_to_variant (L, -1);
765 if (G_UNLIKELY (!values[0] || !values[1])) {
766 g_clear_pointer (&values[0], g_variant_unref);
767 g_clear_pointer (&values[1], g_variant_unref);
768 luaL_error (L, "Constraint: bad value type");
769 }
770 value = g_variant_new_tuple (values, 2);
771 break;
772 }
773 case WP_CONSTRAINT_VERB_IN_LIST: {
774 GPtrArray *values =
775 g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_unref);
776 int i = 3;
777 while (lua_geti (L, constraint_idx, i++) != LUA_TNIL) {
778 GVariant *tmp = constraint_value_to_variant (L, -1);
779 if (G_UNLIKELY (!tmp)) {
780 g_ptr_array_unref (values);
781 luaL_error (L, "Constraint: bad value type");
782 }
783 g_ptr_array_add (values, g_variant_ref_sink (tmp));
784 lua_pop (L, 1);
785 }
786 value = g_variant_new_tuple ((GVariant **) values->pdata, values->len);
787 g_ptr_array_unref (values);
788 break;
789 }
790 default:
791 break;
792 }
793
794 wp_object_interest_add_constraint (interest, ctype, subject, verb, value);
795 lua_settop (L, constraint_idx);
796 }
797
798 static GType
799 parse_gtype (const gchar *str)
800 {
801 g_autofree gchar *typestr = NULL;
802 GType res = G_TYPE_INVALID;
803
804 g_return_val_if_fail (str, res);
805
806 /* "device" -> "WpDevice" */
807 typestr = g_strdup_printf ("Wp%s", str);
808 if (typestr[2] != 0) {
809 typestr[2] = g_ascii_toupper (typestr[2]);
810 res = g_type_from_name (typestr);
811 }
812
813 return res;
814 }
815
816 static int
817 object_interest_new_index (lua_State *L, int idx, GType def_type)
818 {
819 WpObjectInterest *interest = NULL;
820 GType type = def_type;
821
822 luaL_checktype (L, idx, LUA_TTABLE);
823
824 /* type = "string" */
825 lua_pushliteral (L, "type");
826 if (lua_gettable (L, idx) == LUA_TSTRING) {
827 type = parse_gtype (lua_tostring (L, -1));
828 if (type == G_TYPE_INVALID)
829 luaL_error (L, "Interest: unknown type '%s'", lua_tostring (L, -1));
830 }
831 else if (def_type == G_TYPE_INVALID)
832 luaL_error (L, "Interest: expected 'type' as string");
833 lua_pop (L, 1);
834
835 interest = wp_object_interest_new_type (type);
836 wplua_pushboxed (L, WP_TYPE_OBJECT_INTEREST, interest);
837
838 /* add constraints */
839 lua_pushnil (L);
840 while (lua_next (L, idx)) {
841 /* if the key isn't "type" */
842 if (!(lua_type (L, -2) == LUA_TSTRING &&
843 !g_strcmp0 ("type", lua_tostring (L, -2))))
844 object_interest_new_add_constraint (L, type, interest);
845 lua_pop (L, 1);
846 }
847
848 return 1;
849 }
850
851 static int
852 object_interest_new (lua_State *L)
853 {
854 return object_interest_new_index (L, 1, G_TYPE_INVALID);
855 }
856
857 static int
858 object_interest_matches (lua_State *L)
859 {
860 WpObjectInterest *interest = wplua_checkboxed (L, 1, WP_TYPE_OBJECT_INTEREST);
861 gboolean matches = FALSE;
862
863 if (wplua_isobject (L, 2, G_TYPE_OBJECT)) {
864 matches = wp_object_interest_matches (interest, wplua_toobject (L, 2));
865 }
866 else if (lua_istable (L, 2)) {
867 g_autoptr (WpProperties) props = NULL;
868 if (lua_istable (L, 2))
869 props = wplua_table_to_properties (L, 2);
870 else
871 props = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
872 matches = wp_object_interest_matches (interest, props);
873 } else
874 luaL_argerror (L, 2, "expected GObject or table");
875
876 lua_pushboolean (L, matches);
877 return 1;
878 }
879
880 static const luaL_Reg object_interest_methods[] = {
881 { "matches", object_interest_matches },
882 { NULL, NULL }
883 };
884
885 static WpObjectInterest *
886 get_optional_object_interest (lua_State *L, int idx, GType def_type)
887 {
888 if (lua_isnoneornil (L, idx))
889 return NULL;
890 else if (lua_isuserdata (L, idx))
891 return wplua_checkboxed (L, idx, WP_TYPE_OBJECT_INTEREST);
892 else if (lua_istable (L, idx)) {
893 object_interest_new_index (L, idx, def_type);
894 return wplua_toboxed (L, -1);
895 } else {
896 luaL_error (L, "expected Interest or none/nil");
897 return NULL;
898 }
899 }
900
901 /* WpObjectManager */
902
903 static int
904 object_manager_new (lua_State *L)
905 {
906 WpObjectManager *om;
907
908 /* validate arguments */
909 luaL_checktype (L, 1, LUA_TTABLE);
910
911 /* push to Lua asap to have a way to unref in case of error */
912 om = wp_object_manager_new ();
913 wplua_pushobject (L, om);
914
915 lua_pushnil (L);
916 while (lua_next (L, 1)) {
917 WpObjectInterest *interest =
918 wplua_checkboxed (L, -1, WP_TYPE_OBJECT_INTEREST);
919 wp_object_manager_add_interest_full (om, wp_object_interest_ref (interest));
920 lua_pop (L, 1);
921 }
922
923 /* request all the features for Lua scripts to make their job easier */
924 wp_object_manager_request_object_features (om,
925 WP_TYPE_OBJECT, WP_OBJECT_FEATURES_ALL);
926
927 return 1;
928 }
929
930 static int
931 object_manager_activate (lua_State *L)
932 {
933 WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
934 wp_core_install_object_manager (get_wp_core (L), om);
935 return 0;
936 }
937
938 static int
939 object_manager_get_n_objects (lua_State *L)
940 {
941 WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
942 lua_pushinteger (L, wp_object_manager_get_n_objects (om));
943 return 1;
944 }
945
946 static int
947 object_manager_iterate (lua_State *L)
948 {
949 WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
950 WpObjectInterest *oi = get_optional_object_interest (L, 2, G_TYPE_OBJECT);
951 WpIterator *it = oi ?
952 wp_object_manager_new_filtered_iterator_full (om,
953 wp_object_interest_ref (oi)) :
954 wp_object_manager_new_iterator (om);
955 return push_wpiterator (L, it);
956 }
957
958 static int
959 object_manager_lookup (lua_State *L)
960 {
961 WpObjectManager *om = wplua_checkobject (L, 1, WP_TYPE_OBJECT_MANAGER);
962 WpObjectInterest *oi = get_optional_object_interest (L, 2, G_TYPE_OBJECT);
963 WpObject *o = oi ?
964 wp_object_manager_lookup_full (om, wp_object_interest_ref (oi)) :
965 wp_object_manager_lookup (om, G_TYPE_OBJECT, NULL);
966 if (o) {
967 wplua_pushobject (L, o);
968 return 1;
969 }
970 return 0;
971 }
972
973 static const luaL_Reg object_manager_methods[] = {
974 { "activate", object_manager_activate },
975 { "get_n_objects", object_manager_get_n_objects },
976 { "iterate", object_manager_iterate },
977 { "lookup", object_manager_lookup },
978 { NULL, NULL }
979 };
980
981 /* WpMetadata */
982
983 static int
984 metadata_iterate (lua_State *L)
985 {
986 WpMetadata *metadata = wplua_checkobject (L, 1, WP_TYPE_METADATA);
987 lua_Integer subject = luaL_checkinteger (L, 2);
988 WpIterator *it = wp_metadata_new_iterator (metadata, subject);
989 return push_metadata_wpiterator (L, it);
990 }
991
992 static int
993 metadata_find (lua_State *L)
994 {
995 WpMetadata *metadata = wplua_checkobject (L, 1, WP_TYPE_METADATA);
996 lua_Integer subject = luaL_checkinteger (L, 2);
997 const char *key = luaL_checkstring (L, 3), *v = NULL, *t = NULL;
998 v = wp_metadata_find (metadata, subject, key, &t);
999 lua_pushstring (L, v);
1000 lua_pushstring (L, t);
1001 return 2;
1002 }
1003
1004 static int
1005 metadata_set (lua_State *L)
1006 {
1007 WpMetadata *metadata = wplua_checkobject (L, 1, WP_TYPE_METADATA);
1008 lua_Integer subject = luaL_checkinteger (L, 2);
1009 const char *key = luaL_opt (L, luaL_checkstring, 3, NULL);
1010 const char *type = luaL_opt (L, luaL_checkstring, 4, NULL);
1011 const char *value = luaL_opt (L, luaL_checkstring, 5, NULL);
1012 wp_metadata_set (metadata, subject, key, type, value);
1013 return 0;
1014 }
1015
1016 static const luaL_Reg metadata_methods[] = {
1017 { "iterate", metadata_iterate },
1018 { "find", metadata_find },
1019 { "set", metadata_set },
1020 { NULL, NULL }
1021 };
1022
1023 /* WpImplMetadata */
1024
1025 static int
1026 impl_metadata_new (lua_State *L)
1027 {
1028 const char *name = luaL_checkstring (L, 1);
1029 WpProperties *properties = NULL;
1030
1031 if (lua_istable (L, 2))
1032 properties = wplua_table_to_properties (L, 2);
1033 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1034 properties = wp_properties_ref (wplua_checkboxed (L, 2,
1035 WP_TYPE_PROPERTIES));
1036
1037 WpImplMetadata *m = wp_impl_metadata_new_full (get_wp_core (L),
1038 name, properties);
1039 if (m)
1040 wplua_pushobject (L, m);
1041 return m ? 1 : 0;
1042 }
1043
1044 /* Device */
1045
1046 static int
1047 device_new (lua_State *L)
1048 {
1049 const char *factory = luaL_checkstring (L, 1);
1050 WpProperties *properties = NULL;
1051
1052 if (lua_istable (L, 2))
1053 properties = wplua_table_to_properties (L, 2);
1054 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1055 properties = wp_properties_ref (wplua_checkboxed (L, 2,
1056 WP_TYPE_PROPERTIES));
1057
1058 WpDevice *d = wp_device_new_from_factory (get_wp_export_core (L),
1059 factory, properties);
1060 if (d)
1061 wplua_pushobject (L, d);
1062 return d ? 1 : 0;
1063 }
1064
1065 /* WpSpaDevice */
1066
1067 static int
1068 spa_device_new (lua_State *L)
1069 {
1070 const char *factory = luaL_checkstring (L, 1);
1071 WpProperties *properties = NULL;
1072
1073 if (lua_istable (L, 2))
1074 properties = wplua_table_to_properties (L, 2);
1075 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1076 properties = wp_properties_ref (wplua_checkboxed (L, 2,
1077 WP_TYPE_PROPERTIES));
1078
1079 WpSpaDevice *d = wp_spa_device_new_from_spa_factory (get_wp_export_core (L),
1080 factory, properties);
1081 if (d)
1082 wplua_pushobject (L, d);
1083 return d ? 1 : 0;
1084 }
1085
1086 static int
1087 spa_device_iterate_managed_objects (lua_State *L)
1088 {
1089 WpSpaDevice *device = wplua_checkobject (L, 1, WP_TYPE_SPA_DEVICE);
1090 WpIterator *it = wp_spa_device_new_managed_object_iterator (device);
1091 return push_wpiterator (L, it);
1092 }
1093
1094 static int
1095 spa_device_get_managed_object (lua_State *L)
1096 {
1097 WpSpaDevice *device = wplua_checkobject (L, 1, WP_TYPE_SPA_DEVICE);
1098 guint id = luaL_checkinteger (L, 2);
1099 GObject *obj = wp_spa_device_get_managed_object (device, id);
1100 if (obj)
1101 wplua_pushobject (L, obj);
1102 return obj ? 1 : 0;
1103 }
1104
1105 static int
1106 spa_device_store_managed_object (lua_State *L)
1107 {
1108 WpSpaDevice *device = wplua_checkobject (L, 1, WP_TYPE_SPA_DEVICE);
1109 guint id = luaL_checkinteger (L, 2);
1110 GObject *obj = (lua_type (L, 3) != LUA_TNIL) ?
1111 g_object_ref (wplua_checkobject (L, 3, G_TYPE_OBJECT)) : NULL;
1112
1113 wp_spa_device_store_managed_object (device, id, obj);
1114 return 0;
1115 }
1116
1117 static int
1118 spa_device_set_managed_pending (lua_State *L)
1119 {
1120 WpSpaDevice *device = wplua_checkobject (L, 1, WP_TYPE_SPA_DEVICE);
1121 guint id = luaL_checkinteger (L, 2);
1122 wp_spa_device_set_managed_pending (device, id);
1123 return 0;
1124 }
1125
1126 static const luaL_Reg spa_device_methods[] = {
1127 { "iterate_managed_objects", spa_device_iterate_managed_objects },
1128 { "get_managed_object", spa_device_get_managed_object },
1129 { "store_managed_object", spa_device_store_managed_object },
1130 { "set_managed_pending", spa_device_set_managed_pending },
1131 { NULL, NULL }
1132 };
1133
1134 /* Node */
1135
1136 static int
1137 node_new (lua_State *L)
1138 {
1139 const char *factory = luaL_checkstring (L, 1);
1140 WpProperties *properties = NULL;
1141
1142 if (lua_istable (L, 2))
1143 properties = wplua_table_to_properties (L, 2);
1144 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1145 properties = wp_properties_ref (
1146 wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1147
1148 WpNode *d = wp_node_new_from_factory (get_wp_export_core (L),
1149 factory, properties);
1150 if (d)
1151 wplua_pushobject (L, d);
1152 return d ? 1 : 0;
1153 }
1154
1155 static int
1156 node_get_state (lua_State *L)
1157 {
1158 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1159 const gchar *error = NULL;
1160 WpNodeState state = wp_node_get_state (node, &error);
1161 wplua_enum_to_lua (L, state, WP_TYPE_NODE_STATE);
1162 lua_pushstring (L, error ? error : "");
1163 return 2;
1164 }
1165
1166 static int
1167 node_get_n_input_ports (lua_State *L)
1168 {
1169 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1170 guint max = 0;
1171 guint ports = wp_node_get_n_input_ports (node, &max);
1172 lua_pushinteger (L, ports);
1173 lua_pushinteger (L, max);
1174 return 2;
1175 }
1176
1177 static int
1178 node_get_n_output_ports (lua_State *L)
1179 {
1180 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1181 guint max = 0;
1182 guint ports = wp_node_get_n_output_ports (node, &max);
1183 lua_pushinteger (L, ports);
1184 lua_pushinteger (L, max);
1185 return 2;
1186 }
1187
1188 static int
1189 node_get_n_ports (lua_State *L)
1190 {
1191 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1192 guint ports = wp_node_get_n_ports (node);
1193 lua_pushinteger (L, ports);
1194 return 1;
1195 }
1196
1197 static int
1198 node_iterate_ports (lua_State *L)
1199 {
1200 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1201 WpObjectInterest *oi = get_optional_object_interest (L, 2, WP_TYPE_PORT);
1202 WpIterator *it = oi ?
1203 wp_node_new_ports_filtered_iterator_full (node,
1204 wp_object_interest_ref (oi)) :
1205 wp_node_new_ports_iterator (node);
1206 return push_wpiterator (L, it);
1207 }
1208
1209 static int
1210 node_lookup_port (lua_State *L)
1211 {
1212 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1213 WpObjectInterest *oi = get_optional_object_interest (L, 2, WP_TYPE_PORT);
1214 WpPort *port = oi ?
1215 wp_node_lookup_port_full (node, wp_object_interest_ref (oi)) :
1216 wp_node_lookup_port (node, G_TYPE_OBJECT, NULL);
1217 if (port) {
1218 wplua_pushobject (L, port);
1219 return 1;
1220 }
1221 return 0;
1222 }
1223
1224 static int
1225 node_send_command (lua_State *L)
1226 {
1227 WpNode *node = wplua_checkobject (L, 1, WP_TYPE_NODE);
1228 const char *command = luaL_checkstring (L, 2);
1229 wp_node_send_command (node, command);
1230 return 0;
1231 }
1232
1233 static const luaL_Reg node_methods[] = {
1234 { "get_state", node_get_state },
1235 { "get_n_input_ports", node_get_n_input_ports },
1236 { "get_n_output_ports", node_get_n_output_ports },
1237 { "get_n_ports", node_get_n_ports },
1238 { "iterate_ports", node_iterate_ports },
1239 { "lookup_port", node_lookup_port },
1240 { "send_command", node_send_command },
1241 { NULL, NULL }
1242 };
1243
1244 /* ImplNode */
1245
1246 static int
1247 impl_node_new (lua_State *L)
1248 {
1249 const char *factory = luaL_checkstring (L, 1);
1250 WpProperties *properties = NULL;
1251
1252 if (lua_istable (L, 2))
1253 properties = wplua_table_to_properties (L, 2);
1254 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1255 properties = wp_properties_ref (wplua_checkboxed (L, 2,
1256 WP_TYPE_PROPERTIES));
1257
1258 WpImplNode *d = wp_impl_node_new_from_pw_factory (get_wp_export_core (L),
1259 factory, properties);
1260 if (d)
1261 wplua_pushobject (L, d);
1262 return d ? 1 : 0;
1263 }
1264
1265 /* Port */
1266
1267 static int
1268 port_get_direction (lua_State *L)
1269 {
1270 WpPort *port = wplua_checkobject (L, 1, WP_TYPE_PORT);
1271 WpDirection direction = wp_port_get_direction (port);
1272 wplua_enum_to_lua (L, direction, WP_TYPE_DIRECTION);
1273 return 1;
1274 }
1275
1276 static const luaL_Reg port_methods[] = {
1277 { "get_direction", port_get_direction },
1278 { NULL, NULL }
1279 };
1280
1281 /* Link */
1282
1283 static int
1284 link_new (lua_State *L)
1285 {
1286 const char *factory = luaL_checkstring (L, 1);
1287 WpProperties *properties = NULL;
1288
1289 if (lua_istable (L, 2))
1290 properties = wplua_table_to_properties (L, 2);
1291 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1292 properties = wp_properties_ref (wplua_checkboxed (L, 2,
1293 WP_TYPE_PROPERTIES));
1294
1295 WpLink *l = wp_link_new_from_factory (get_wp_core (L), factory, properties);
1296 if (l)
1297 wplua_pushobject (L, l);
1298 return l ? 1 : 0;
1299 }
1300
1301 /* Client */
1302
1303 static gboolean
1304 client_parse_permissions (const gchar * perms_str, guint32 *perms)
1305 {
1306 *perms = 0;
1307
1308 if (!perms_str)
1309 return FALSE;
1310 else if (g_strcmp0 (perms_str, "all") == 0)
1311 *perms = PW_PERM_ALL;
1312 else {
1313 for (guint i = 0; i < strlen (perms_str); i++) {
1314 switch (perms_str[i]) {
1315 case 'r': *perms |= PW_PERM_R; break;
1316 case 'w': *perms |= PW_PERM_W; break;
1317 case 'x': *perms |= PW_PERM_X; break;
1318 case 'm': *perms |= PW_PERM_M; break;
1319 case 'l': *perms |= PW_PERM_L; break;
1320 case '-': break;
1321 default:
1322 return FALSE;
1323 }
1324 }
1325 }
1326 return TRUE;
1327 }
1328
1329 static int
1330 client_update_permissions (lua_State *L)
1331 {
1332 WpClient *client = wplua_checkobject (L, 1, WP_TYPE_CLIENT);
1333 g_autoptr (GArray) arr = NULL;
1334
1335 luaL_checktype (L, 2, LUA_TTABLE);
1336
1337 lua_pushnil(L);
1338 while (lua_next (L, -2)) {
1339 struct pw_permission perm = {0};
1340
1341 if (lua_type (L, -2) == LUA_TSTRING &&
1342 (!g_ascii_strcasecmp (lua_tostring(L, -2), "any") ||
1343 !g_ascii_strcasecmp (lua_tostring(L, -2), "all")))
1344 perm.id = PW_ID_ANY;
1345 else if (lua_isinteger (L, -2))
1346 perm.id = lua_tointeger (L, -2);
1347 else
1348 luaL_error (L, "invalid key for permissions array");
1349
1350 if (!client_parse_permissions (lua_tostring (L, -1), &perm.permissions))
1351 luaL_error (L, "invalid permission string: '%s'", lua_tostring (L, -1));
1352
1353 if (!arr)
1354 arr = g_array_new (FALSE, FALSE, sizeof (struct pw_permission));
1355
1356 g_array_append_val (arr, perm);
1357 lua_pop (L, 1);
1358 }
1359
1360 wp_client_update_permissions_array (client, arr->len,
1361 (const struct pw_permission *) arr->data);
1362 return 0;
1363 }
1364
1365 static int
1366 client_update_properties (lua_State *L)
1367 {
1368 WpClient *client = wplua_checkobject (L, 1, WP_TYPE_CLIENT);
1369 WpProperties *properties = NULL;
1370
1371 if (lua_istable (L, 2))
1372 properties = wplua_table_to_properties (L, 2);
1373 else
1374 properties = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1375
1376 wp_client_update_properties (client, properties);
1377 return 0;
1378 }
1379
1380 static int
1381 client_send_error (lua_State *L)
1382 {
1383 WpClient *client = wplua_checkobject (L, 1, WP_TYPE_CLIENT);
1384 guint id = luaL_checkinteger (L, 2);
1385 int res = luaL_checkinteger (L, 3);
1386 const char *message = luaL_checkstring (L, 4);
1387 wp_client_send_error (client, id, res, message);
1388 return 0;
1389 }
1390
1391 static const luaL_Reg client_methods[] = {
1392 { "update_permissions", client_update_permissions },
1393 { "update_properties", client_update_properties },
1394 { "send_error", client_send_error },
1395 { NULL, NULL }
1396 };
1397
1398 /* WpSessionItem */
1399
1400 static int
1401 session_item_new (lua_State *L)
1402 {
1403 const char *type = luaL_checkstring (L, 1);
1404 WpSessionItem *si = wp_session_item_make (get_wp_core (L), type);
1405 if (si)
1406 wplua_pushobject (L, si);
1407 return si ? 1 : 0;
1408 }
1409
1410 static int
1411 session_item_get_associated_proxy (lua_State *L)
1412 {
1413 WpSessionItem *si = wplua_checkobject (L, 1, WP_TYPE_SESSION_ITEM);
1414 const char *typestr = luaL_checkstring (L, 2);
1415 WpProxy *proxy = wp_session_item_get_associated_proxy (si,
1416 parse_gtype (typestr));
1417 if (proxy)
1418 wplua_pushobject (L, proxy);
1419 return proxy ? 1 : 0;
1420 }
1421
1422 static int
1423 session_item_reset (lua_State *L)
1424 {
1425 WpSessionItem *si = wplua_checkobject (L, 1, WP_TYPE_SESSION_ITEM);
1426 wp_session_item_reset (si);
1427 return 0;
1428 }
1429
1430 static int
1431 session_item_configure (lua_State *L)
1432 {
1433 WpSessionItem *si = wplua_checkobject (L, 1, WP_TYPE_SESSION_ITEM);
1434 WpProperties *props;
1435
1436 if (lua_istable (L, 2))
1437 props = wplua_table_to_properties (L, 2);
1438 else
1439 props = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1440
1441 lua_pushboolean (L, wp_session_item_configure (si, props));
1442 return 1;
1443 }
1444
1445 static int
1446 session_item_register (lua_State *L)
1447 {
1448 WpSessionItem *si = wplua_checkobject (L, 1, WP_TYPE_SESSION_ITEM);
1449 wp_session_item_register (g_object_ref (si));
1450 return 0;
1451 }
1452
1453 static int
1454 session_item_remove (lua_State *L)
1455 {
1456 WpSessionItem *si = wplua_checkobject (L, 1, WP_TYPE_SESSION_ITEM);
1457 wp_session_item_remove (si);
1458 return 0;
1459 }
1460
1461 static int
1462 session_item_get_property (lua_State *L)
1463 {
1464 WpSessionItem *si = wplua_checkobject (L, 1, WP_TYPE_SESSION_ITEM);
1465 const char *key = luaL_checkstring (L, 2);
1466 const char *val = wp_session_item_get_property (si, key);
1467 lua_pushstring (L, val);
1468 return 1;
1469 }
1470
1471 static const luaL_Reg session_item_methods[] = {
1472 { "get_associated_proxy", session_item_get_associated_proxy },
1473 { "reset", session_item_reset },
1474 { "configure", session_item_configure },
1475 { "register", session_item_register },
1476 { "remove", session_item_remove },
1477 { "get_property", session_item_get_property },
1478 { NULL, NULL }
1479 };
1480
1481 /* WpSiAdapter */
1482
1483 static int
1484 si_adapter_get_ports_format (lua_State *L)
1485 {
1486 WpSiAdapter *adapter = wplua_checkobject (L, 1, WP_TYPE_SI_ADAPTER);
1487 const gchar *mode = NULL;
1488 WpSpaPod *format = wp_si_adapter_get_ports_format (adapter, &mode);
1489 wplua_pushboxed (L, WP_TYPE_SPA_POD, format);
1490 lua_pushstring (L, mode);
1491 return 2;
1492 }
1493
1494 static void
1495 si_adapter_set_ports_format_done (WpObject *o, GAsyncResult * res,
1496 GClosure * closure)
1497 {
1498 g_autoptr (GError) error = NULL;
1499 GValue val[2] = { G_VALUE_INIT, G_VALUE_INIT };
1500 int n_vals = 1;
1501
1502 if (!wp_si_adapter_set_ports_format_finish (WP_SI_ADAPTER (o), res, &error)) {
1503 wp_debug_object (o, "%s", error->message);
1504 if (closure) {
1505 g_value_init (&val[1], G_TYPE_STRING);
1506 g_value_set_string (&val[1], error->message);
1507 n_vals = 2;
1508 }
1509 }
1510 if (closure) {
1511 g_value_init_from_instance (&val[0], o);
1512 g_closure_invoke (closure, NULL, n_vals, val, NULL);
1513 g_value_unset (&val[0]);
1514 g_value_unset (&val[1]);
1515 g_closure_invalidate (closure);
1516 g_closure_unref (closure);
1517 }
1518 }
1519
1520 static int
1521 si_adapter_set_ports_format (lua_State *L)
1522 {
1523 WpSiAdapter *adapter = wplua_checkobject (L, 1, WP_TYPE_SI_ADAPTER);
1524 WpSpaPod *format = wplua_checkboxed (L, 2, WP_TYPE_SPA_POD);
1525 const gchar *mode = luaL_checkstring (L, 3);
1526 GClosure * closure = luaL_opt (L, wplua_checkclosure, 4, NULL);
1527 if (closure)
1528 g_closure_sink (g_closure_ref (closure));
1529 wp_si_adapter_set_ports_format (adapter, wp_spa_pod_ref (format), mode,
1530 (GAsyncReadyCallback) si_adapter_set_ports_format_done, closure);
1531 return 0;
1532 }
1533
1534 static const luaL_Reg si_adapter_methods[] = {
1535 { "get_ports_format", si_adapter_get_ports_format },
1536 { "set_ports_format", si_adapter_set_ports_format },
1537 { NULL, NULL }
1538 };
1539
1540 /* WpPipewireObject */
1541
1542 static void
1543 on_enum_params_done (WpPipewireObject * pwobj, GAsyncResult * res,
1544 GClosure * closure)
1545 {
1546 g_autoptr (GError) error = NULL;
1547 GValue vals[2] = { G_VALUE_INIT, G_VALUE_INIT };
1548 int n_vals = 1;
1549 WpIterator *it;
1550
1551 it = wp_pipewire_object_enum_params_finish (pwobj, res, &error);
1552 g_value_init (&vals[0], WP_TYPE_ITERATOR);
1553 g_value_set_boxed (&vals[0], it);
1554 if (!it) {
1555 g_value_init (&vals[1], G_TYPE_STRING);
1556 g_value_set_string (&vals[1], error->message);
1557 n_vals = 2;
1558 }
1559
1560 g_clear_pointer (&it, wp_iterator_unref);
1561 g_closure_invoke (closure, NULL, n_vals, vals, NULL);
1562
1563 g_value_unset (&vals[0]);
1564 g_value_unset (&vals[1]);
1565 g_closure_invalidate (closure);
1566 g_closure_unref (closure);
1567 }
1568
1569 static int
1570 pipewire_object_enum_params (lua_State *L)
1571 {
1572 WpPipewireObject *pwobj = wplua_checkobject (L, 1, WP_TYPE_PIPEWIRE_OBJECT);
1573 const gchar *id = luaL_checkstring (L, 2);
1574 GClosure * closure = wplua_checkclosure (L, 3);
1575 g_closure_sink (g_closure_ref (closure));
1576 wp_pipewire_object_enum_params (pwobj, id, NULL, NULL,
1577 (GAsyncReadyCallback) on_enum_params_done, closure);
1578 return 0;
1579 }
1580
1581 static int
1582 pipewire_object_iterate_params (lua_State *L)
1583 {
1584 WpPipewireObject *pwobj = wplua_checkobject (L, 1, WP_TYPE_PIPEWIRE_OBJECT);
1585 const gchar *id = luaL_checkstring (L, 2);
1586 WpIterator *it = wp_pipewire_object_enum_params_sync (pwobj, id, NULL);
1587 return push_wpiterator (L, it);
1588 }
1589
1590 static int
1591 pipewire_object_set_param (lua_State *L)
1592 {
1593 WpPipewireObject *pwobj = wplua_checkobject (L, 1, WP_TYPE_PIPEWIRE_OBJECT);
1594 const gchar *id = luaL_checkstring (L, 2);
1595 WpSpaPod *pod = wplua_checkboxed (L, 3, WP_TYPE_SPA_POD);
1596 wp_pipewire_object_set_param (pwobj, id, 0, wp_spa_pod_ref (pod));
1597 return 0;
1598 }
1599
1600 static int
1601 pipewire_object_get_property (lua_State *L)
1602 {
1603 WpPipewireObject *pwobj = wplua_checkobject (L, 1, WP_TYPE_PIPEWIRE_OBJECT);
1604 const char *key = luaL_checkstring (L, 2);
1605 const char *val = wp_pipewire_object_get_property (pwobj, key);
1606 lua_pushstring (L, val);
1607 return 1;
1608 }
1609
1610 static const luaL_Reg pipewire_object_methods[] = {
1611 { "enum_params", pipewire_object_enum_params },
1612 { "iterate_params", pipewire_object_iterate_params },
1613 { "set_param" , pipewire_object_set_param },
1614 { "set_params" , pipewire_object_set_param }, /* deprecated, compat only */
1615 { "get_property", pipewire_object_get_property },
1616 { NULL, NULL }
1617 };
1618
1619 /* WpState */
1620
1621 static int
1622 state_new (lua_State *L)
1623 {
1624 const gchar *name = luaL_checkstring (L, 1);
1625 WpState *state = wp_state_new (name);
1626 wplua_pushobject (L, state);
1627 return 1;
1628 }
1629
1630 static int
1631 state_clear (lua_State *L)
1632 {
1633 WpState *state = wplua_checkobject (L, 1, WP_TYPE_STATE);
1634 wp_state_clear (state);
1635 return 0;
1636 }
1637
1638 static int
1639 state_save (lua_State *L)
1640 {
1641 WpState *state = wplua_checkobject (L, 1, WP_TYPE_STATE);
1642 g_autoptr (WpProperties) props = NULL;
1643 g_autoptr (GError) error = NULL;
1644
1645 if (lua_istable (L, 2))
1646 props = wplua_table_to_properties (L, 2);
1647 else
1648 props = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1649
1650 gboolean saved = wp_state_save (state, props, &error);
1651 lua_pushboolean (L, saved);
1652 lua_pushstring (L, error ? error->message : "");
1653 return 2;
1654 }
1655
1656 static int
1657 state_save_after_timeout (lua_State *L)
1658 {
1659 WpState *state = wplua_checkobject (L, 1, WP_TYPE_STATE);
1660 g_autoptr (WpProperties) props = NULL;
1661
1662 if (lua_istable (L, 2))
1663 props = wplua_table_to_properties (L, 2);
1664 else
1665 props = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1666
1667 wp_state_save_after_timeout (state, get_wp_core (L), props);
1668 return 0;
1669 }
1670
1671 static int
1672 state_load (lua_State *L)
1673 {
1674 WpState *state = wplua_checkobject (L, 1, WP_TYPE_STATE);
1675 WpProperties *props = wp_state_load (state);
1676 wplua_pushboxed (L, WP_TYPE_PROPERTIES, props);
1677 return 1;
1678 }
1679
1680 static const luaL_Reg state_methods[] = {
1681 { "clear", state_clear },
1682 { "save" , state_save },
1683 { "save_after_timeout", state_save_after_timeout },
1684 { "load" , state_load },
1685 { NULL, NULL }
1686 };
1687
1688 /* ImplModule */
1689
1690 static int
1691 impl_module_new (lua_State *L)
1692 {
1693 const char *name, *args = NULL;
1694 g_autoptr (WpProperties) properties = NULL;
1695
1696 name = luaL_checkstring (L, 1);
1697
1698 if (lua_type (L, 2) != LUA_TNONE && lua_type (L, 2) != LUA_TNIL)
1699 args = luaL_checkstring (L, 2);
1700
1701 if (lua_istable (L, 3))
1702 properties = wplua_table_to_properties (L, 3);
1703 else if (!lua_isnone (L, 3) && !lua_isnil (L, 3))
1704 properties = wp_properties_ref (wplua_checkboxed (L, 3,
1705 WP_TYPE_PROPERTIES));
1706
1707 WpImplModule *m = wp_impl_module_load (get_wp_export_core (L),
1708 name, args, properties);
1709
1710 if (m) {
1711 wplua_pushobject (L, m);
1712 return 1;
1713 } else {
1714 return 0;
1715 }
1716 }
1717
1718 /* WpConf */
1719
1720
1721 static int
1722 conf_new (lua_State *L)
1723 {
1724 const char *path = luaL_checkstring (L, 1);
1725 WpProperties *p = NULL;
1726 WpConf *conf = NULL;
1727
1728 if (lua_istable (L, 2))
1729 p = wplua_table_to_properties (L, 2);
1730 else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
1731 p = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1732
1733 conf = wp_conf_new (path, p);
1734 if (conf) {
1735 wplua_pushobject (L, conf);
1736 } else
1737 lua_pushnil (L);
1738 return 1;
1739 }
1740
1741 static int
1742 conf_open (lua_State *L)
1743 {
1744 WpConf *conf = wplua_checkobject (L, 1, WP_TYPE_CONF);
1745 g_autoptr (GError) err = NULL;
1746
1747 if (wp_conf_open (conf, &err)) {
1748 lua_pushnil (L);
1749 } else
1750 lua_pushstring (L, err->message);
1751 return 1;
1752 }
1753
1754 static int
1755 conf_close (lua_State *L)
1756 {
1757 WpConf *conf = wplua_checkobject (L, 1, WP_TYPE_CONF);
1758
1759 wp_conf_close (conf);
1760 return 0;
1761 }
1762
1763 static int
1764 conf_get_section_as_properties (lua_State *L)
1765 {
1766 const char *section = NULL;
1767 g_autoptr (WpConf) conf = NULL;
1768 g_autoptr (WpSpaJson) s = NULL;
1769 WpProperties *props = NULL;
1770 int argi = 1;
1771
1772 /* check if called as method on object */
1773 if (lua_isuserdata (L, argi)) {
1774 conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
1775 argi++;
1776 } else
1777 conf = wp_core_get_conf (get_wp_core (L));
1778
1779 section = luaL_checkstring (L, argi);
1780 argi++;
1781
1782 if (lua_istable (L, argi))
1783 props = wplua_table_to_properties (L, argi);
1784 else if (!lua_isnone (L, argi) && !lua_isnil (L, argi))
1785 props = wp_properties_ref (wplua_checkboxed (L, argi, WP_TYPE_PROPERTIES));
1786 else
1787 props = wp_properties_new_empty ();
1788
1789 if (conf) {
1790 s = wp_conf_get_section (conf, section);
1791 if (s && wp_spa_json_is_object (s))
1792 wp_properties_update_from_json (props, s);
1793 }
1794 wplua_pushboxed (L, WP_TYPE_PROPERTIES, props);
1795 return 1;
1796 }
1797
1798 static int
1799 conf_get_section_as_object (lua_State *L)
1800 {
1801 const char *section = NULL;
1802 g_autoptr (WpConf) conf = NULL;
1803 g_autoptr (WpSpaJson) s = NULL;
1804 int argi = 1;
1805
1806 /* check if called as method on object */
1807 if (lua_isuserdata (L, argi)) {
1808 conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
1809 argi++;
1810 } else
1811 conf = wp_core_get_conf (get_wp_core (L));
1812
1813 section = luaL_checkstring (L, argi);
1814 argi++;
1815
1816 if (conf) {
1817 s = wp_conf_get_section (conf, section);
1818 if (s && wp_spa_json_is_object (s)) {
1819 push_luajson (L, s, INT_MAX);
1820 return 1;
1821 }
1822 }
1823
1824 if (lua_istable (L, argi))
1825 lua_pushvalue (L, argi);
1826 else
1827 lua_newtable (L);
1828 return 1;
1829 }
1830
1831 static int
1832 conf_get_section_as_array (lua_State *L)
1833 {
1834 const char *section = NULL;
1835 g_autoptr (WpConf) conf = NULL;
1836 g_autoptr (WpSpaJson) s = NULL;
1837 int argi = 1;
1838
1839 /* check if called as method on object */
1840 if (lua_isuserdata (L, argi)) {
1841 conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
1842 argi++;
1843 } else
1844 conf = wp_core_get_conf (get_wp_core (L));
1845
1846 section = luaL_checkstring (L, argi);
1847 argi++;
1848
1849 if (conf) {
1850 s = wp_conf_get_section (conf, section);
1851 if (s && wp_spa_json_is_array (s)) {
1852 push_luajson (L, s, INT_MAX);
1853 return 1;
1854 }
1855 }
1856
1857 if (lua_istable (L, argi))
1858 lua_pushvalue (L, argi);
1859 else
1860 lua_newtable (L);
1861 return 1;
1862 }
1863
1864 static int
1865 conf_get_section_as_json (lua_State *L)
1866 {
1867 const char *section = NULL;
1868 g_autoptr (WpConf) conf = NULL;
1869 g_autoptr (WpSpaJson) s = NULL;
1870 WpSpaJson *fb = NULL;
1871 int argi = 1;
1872
1873 /* check if called as method on object */
1874 if (lua_isuserdata (L, argi)) {
1875 conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
1876 argi++;
1877 } else
1878 conf = wp_core_get_conf (get_wp_core (L));
1879
1880 section = luaL_checkstring (L, argi);
1881 argi++;
1882
1883 if (lua_isuserdata (L, argi))
1884 fb = wplua_checkboxed (L, argi, WP_TYPE_SPA_JSON);
1885
1886 if (conf) {
1887 s = wp_conf_get_section (conf, section);
1888 if (!s && fb)
1889 s = wp_spa_json_ref (fb);
1890 if (s) {
1891 wplua_pushboxed (L, WP_TYPE_SPA_JSON,
1892 wp_spa_json_ensure_unique_owner (g_steal_pointer (&s)));
1893 return 1;
1894 }
1895 }
1896
1897 lua_pushnil (L);
1898 return 1;
1899 }
1900
1901 static const luaL_Reg conf_methods[] = {
1902 { "open", conf_open },
1903 { "close", conf_close },
1904 { "get_section_as_properties", conf_get_section_as_properties },
1905 { "get_section_as_object", conf_get_section_as_object },
1906 { "get_section_as_array", conf_get_section_as_array },
1907 { "get_section_as_json", conf_get_section_as_json },
1908 { NULL, NULL }
1909 };
1910
1911 /* JsonUtils */
1912
1913 static gboolean
1914 json_utils_match_rules_cb (gpointer data, const gchar * action,
1915 WpSpaJson * value, GError ** error)
1916 {
1917 lua_State *L = data;
1918 g_autoptr (GError) pcall_err = NULL;
1919 gboolean ret = TRUE;
1920 int top = lua_gettop (L);
1921
1922 /* this callback is called within the context of json_utils_match_rules()
1923 * and the Lua callback function is at the top of the stack at this point */
1924
1925 /* re-push the function because lua_call pops it; then, push arguments */
1926 lua_pushvalue (L, -1);
1927 lua_pushstring (L, action);
1928 wplua_pushboxed (L, WP_TYPE_SPA_JSON, wp_spa_json_ref (value));
1929
1930 lua_call (L, 2, 2);
1931
1932 ret = lua_toboolean (L, -2);
1933 if (!ret) {
1934 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
1935 "%s", lua_tostring (L, -1));
1936 }
1937
1938 lua_settop (L, top);
1939 return ret;
1940 }
1941
1942 static int
1943 json_utils_match_rules (lua_State *L)
1944 {
1945 g_autoptr (WpProperties) properties = NULL;
1946 g_autoptr (GError) error = NULL;
1947 WpSpaJson *json;
1948 gboolean res;
1949
1950 json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON);
1951 luaL_checktype (L, 3, LUA_TFUNCTION);
1952
1953 if (lua_istable (L, 2))
1954 properties = wplua_table_to_properties (L, 2);
1955 else
1956 properties = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
1957
1958 res = wp_json_utils_match_rules (json, properties, json_utils_match_rules_cb,
1959 L, &error);
1960
1961 lua_pushboolean (L, res);
1962 if (error)
1963 lua_pushstring (L, error->message);
1964 else
1965 lua_pushnil (L);
1966 return 2;
1967 }
1968
1969 static int
1970 json_utils_match_rules_update_properties (lua_State *L)
1971 {
1972 WpProperties *properties = NULL;
1973 WpSpaJson *json;
1974 int count;
1975
1976 json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON);
1977
1978 if (lua_istable (L, 2))
1979 properties = wplua_table_to_properties (L, 2);
1980 else
1981 properties = wp_properties_ref (wplua_checkboxed (L, 2,
1982 WP_TYPE_PROPERTIES));
1983
1984 count = wp_json_utils_match_rules_update_properties (json, properties);
1985
1986 wplua_pushboxed (L, WP_TYPE_PROPERTIES, properties);
1987 lua_pushinteger (L, count);
1988 return 2;
1989 }
1990
1991 static const luaL_Reg json_utils_funcs[] = {
1992 { "match_rules", json_utils_match_rules },
1993 { "match_rules_update_properties", json_utils_match_rules_update_properties },
1994 { NULL, NULL }
1995 };
1996
1997 /* ProcInfo */
1998
1999 static int
2000 proc_info_get_pid (lua_State *L)
2001 {
2002 WpProcInfo *pi = wplua_checkboxed (L, 1, WP_TYPE_PROC_INFO);
2003 lua_pushinteger (L, wp_proc_info_get_pid (pi));
2004 return 1;
2005 }
2006
2007 static int
2008 proc_info_get_parent_pid (lua_State *L)
2009 {
2010 WpProcInfo *pi = wplua_checkboxed (L, 1, WP_TYPE_PROC_INFO);
2011 lua_pushinteger (L, wp_proc_info_get_parent_pid (pi));
2012 return 1;
2013 }
2014
2015 static int
2016 proc_info_get_n_args (lua_State *L)
2017 {
2018 WpProcInfo *pi = wplua_checkboxed (L, 1, WP_TYPE_PROC_INFO);
2019 lua_pushinteger (L, wp_proc_info_get_n_args (pi));
2020 return 1;
2021 }
2022
2023 static int
2024 proc_info_get_arg (lua_State *L)
2025 {
2026 WpProcInfo *pi = wplua_checkboxed (L, 1, WP_TYPE_PROC_INFO);
2027 guint index = luaL_checkinteger (L, 2);
2028 lua_pushstring (L, wp_proc_info_get_arg (pi, index));
2029 return 1;
2030 }
2031
2032 static int
2033 proc_info_get_cgroup (lua_State *L)
2034 {
2035 WpProcInfo *pi = wplua_checkboxed (L, 1, WP_TYPE_PROC_INFO);
2036 lua_pushstring (L, wp_proc_info_get_cgroup (pi));
2037 return 1;
2038 }
2039
2040 static const luaL_Reg proc_info_funcs[] = {
2041 { "get_pid", proc_info_get_pid },
2042 { "get_parent_pid", proc_info_get_parent_pid },
2043 { "get_cgroup", proc_info_get_cgroup },
2044 { "get_n_args", proc_info_get_n_args },
2045 { "get_arg", proc_info_get_arg },
2046 { NULL, NULL }
2047 };
2048
2049 /* ProcUtils */
2050
2051 static int
2052 proc_utils_get_proc_info (lua_State *L)
2053 {
2054 guint pid = luaL_checkinteger (L, 1);
2055 WpProcInfo *pi = wp_proc_utils_get_proc_info (pid);
2056 if (pi)
2057 wplua_pushboxed (L, WP_TYPE_PROC_INFO, pi);
2058 else
2059 lua_pushnil (L);
2060 return 1;
2061 }
2062
2063 static const luaL_Reg proc_utils_funcs[] = {
2064 { "get_proc_info", proc_utils_get_proc_info },
2065 { NULL, NULL }
2066 };
2067
2068 /* Properties */
2069
2070 static int
2071 properties_new (lua_State *L)
2072 {
2073 WpProperties *props;
2074
2075 if (lua_istable (L, 1))
2076 props = wplua_table_to_properties (L, 1);
2077 else if (!lua_isnone (L, 1) && !lua_isnil (L, 1))
2078 props = wp_properties_ref (wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES));
2079 else
2080 props = wp_properties_new_empty ();
2081
2082 wplua_pushboxed (L, WP_TYPE_PROPERTIES, props);
2083 return 1;
2084 }
2085
2086 static int
2087 properties_get_boolean (lua_State *L)
2088 {
2089 WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES);
2090 const char *key = luaL_checkstring (L, 2);
2091 const char *val = wp_properties_get (props, key);
2092 if (val)
2093 lua_pushboolean (L, spa_atob (val));
2094 else
2095 lua_pushnil (L);
2096 return 1;
2097 }
2098
2099 static int
2100 properties_get_int (lua_State *L)
2101 {
2102 WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES);
2103 const char *key = luaL_checkstring (L, 2);
2104 const char *val = wp_properties_get (props, key);
2105 if (val) {
2106 gint64 int_val = 0;
2107 if (spa_atoi64 (val, &int_val, 10))
2108 lua_pushinteger (L, int_val);
2109 else
2110 lua_pushnil (L);
2111 } else {
2112 lua_pushnil (L);
2113 }
2114 return 1;
2115 }
2116
2117 static int
2118 properties_get_float (lua_State *L)
2119 {
2120 WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES);
2121 const char *key = luaL_checkstring (L, 2);
2122 const char *val = wp_properties_get (props, key);
2123 if (val) {
2124 double d_val = 0;
2125 if (spa_atod (val, &d_val))
2126 lua_pushnumber (L, d_val);
2127 else
2128 lua_pushnil (L);
2129 } else {
2130 lua_pushnil (L);
2131 }
2132 return 1;
2133 }
2134
2135 static int
2136 properties_get_count (lua_State *L)
2137 {
2138 WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES);
2139 lua_pushinteger (L, wp_properties_get_count (props));
2140 return 1;
2141 }
2142
2143 static int
2144 properties_copy (lua_State *L)
2145 {
2146 WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES);
2147 WpProperties *copy = wp_properties_copy (props);
2148 wplua_pushboxed (L, WP_TYPE_PROPERTIES, copy);
2149 return 1;
2150 }
2151
2152 static int
2153 properties_parse (lua_State *L)
2154 {
2155 WpProperties *props = wplua_checkboxed (L, 1, WP_TYPE_PROPERTIES);
2156 wplua_properties_to_table (L, props);
2157 return 1;
2158 }
2159
2160 static const luaL_Reg properties_funcs[] = {
2161 { "get_boolean", properties_get_boolean },
2162 { "get_int", properties_get_int },
2163 { "get_float", properties_get_float },
2164 { "get_count", properties_get_count },
2165 { "copy", properties_copy },
2166 { "parse", properties_parse },
2167 { NULL, NULL }
2168 };
2169
2170 /* WpSettings */
2171
2172 static int
2173 settings_get (lua_State *L)
2174 {
2175 const char *setting = luaL_checkstring (L, 1);
2176 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2177 if (s) {
2178 WpSpaJson *j = wp_settings_get (s, setting);
2179 if (j)
2180 wplua_pushboxed (L, WP_TYPE_SPA_JSON, j);
2181 else
2182 lua_pushnil (L);
2183 } else
2184 lua_pushnil (L);
2185 return 1;
2186 }
2187
2188 static int
2189 settings_get_boolean (lua_State *L)
2190 {
2191 const char *setting = luaL_checkstring (L, 1);
2192 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2193 gboolean val = FALSE;
2194
2195 if (s) {
2196 g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
2197 if (j)
2198 wp_spa_json_parse_boolean (j, &val);
2199 }
2200
2201 lua_pushboolean (L, val);
2202 return 1;
2203 }
2204
2205 static int
2206 settings_get_int (lua_State *L)
2207 {
2208 const char *setting = luaL_checkstring (L, 1);
2209 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2210 gint val = 0;
2211
2212 if (s) {
2213 g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
2214 if (j)
2215 wp_spa_json_parse_int (j, &val);
2216 }
2217
2218 lua_pushinteger (L, val);
2219 return 1;
2220 }
2221
2222 static int
2223 settings_get_float (lua_State *L)
2224 {
2225 const char *setting = luaL_checkstring (L, 1);
2226 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2227 float val = 0.0;
2228
2229 if (s) {
2230 g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
2231 if (j)
2232 wp_spa_json_parse_float (j, &val);
2233 }
2234
2235 lua_pushnumber (L, val);
2236 return 1;
2237 }
2238
2239 static int
2240 settings_get_string (lua_State *L)
2241 {
2242 const char *setting = luaL_checkstring (L, 1);
2243 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2244
2245 if (s) {
2246 g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
2247 if (j) {
2248 g_autofree gchar *val = wp_spa_json_parse_string (j);
2249 if (val) {
2250 lua_pushstring (L, val);
2251 return 1;
2252 }
2253 }
2254 }
2255
2256 lua_pushstring (L, "");
2257 return 1;
2258 }
2259
2260 static int
2261 settings_get_array (lua_State *L)
2262 {
2263 const char *setting = luaL_checkstring (L, 1);
2264 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2265 g_autoptr (WpSpaJson) val = NULL;
2266
2267 if (s) {
2268 g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
2269 if (j && wp_spa_json_is_array (j)) {
2270 push_luajson (L, j, INT_MAX);
2271 return 1;
2272 }
2273 }
2274
2275 val = wp_spa_json_new_array (NULL, NULL);
2276 push_luajson (L, val, INT_MAX);
2277 return 1;
2278 }
2279
2280 static int
2281 settings_get_object (lua_State *L)
2282 {
2283 const char *setting = luaL_checkstring (L, 1);
2284 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2285 g_autoptr (WpSpaJson) val = NULL;
2286
2287 if (s) {
2288 g_autoptr (WpSpaJson) j = wp_settings_get (s, setting);
2289 if (j && wp_spa_json_is_object (j)) {
2290 push_luajson (L, j, INT_MAX);
2291 return 1;
2292 }
2293 }
2294
2295 val = wp_spa_json_new_object (NULL, NULL, NULL);
2296 push_luajson (L, val, INT_MAX);
2297 return 1;
2298 }
2299
2300 static int
2301 settings_get_saved (lua_State *L)
2302 {
2303 const char *setting = luaL_checkstring (L, 1);
2304 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2305 if (s) {
2306 WpSpaJson *j = wp_settings_get_saved (s, setting);
2307 if (j)
2308 wplua_pushboxed (L, WP_TYPE_SPA_JSON, j);
2309 else
2310 lua_pushnil (L);
2311 } else
2312 lua_pushnil (L);
2313 return 1;
2314 }
2315
2316 static int
2317 settings_set (lua_State *L)
2318 {
2319 const char *key = luaL_checkstring (L, 1);
2320 WpSpaJson *val = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON);
2321 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2322 if (s) {
2323 lua_pushboolean (L, wp_settings_set (s, key, val));
2324 } else {
2325 lua_pushboolean (L, FALSE);
2326 }
2327 return 1;
2328 }
2329
2330 static int
2331 settings_reset (lua_State *L)
2332 {
2333 const char *key = luaL_checkstring (L, 1);
2334 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2335 if (s) {
2336 lua_pushboolean (L, wp_settings_reset (s, key));
2337 } else {
2338 lua_pushboolean (L, FALSE);
2339 }
2340 return 1;
2341 }
2342
2343 static int
2344 settings_save (lua_State *L)
2345 {
2346 const char *key = luaL_checkstring (L, 1);
2347 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2348 if (s) {
2349 lua_pushboolean (L, wp_settings_save (s, key));
2350 } else {
2351 lua_pushboolean (L, FALSE);
2352 }
2353 return 1;
2354 }
2355
2356 static int
2357 settings_delete (lua_State *L)
2358 {
2359 const char *key = luaL_checkstring (L, 1);
2360 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2361 if (s) {
2362 lua_pushboolean (L, wp_settings_delete (s, key));
2363 } else {
2364 lua_pushboolean (L, FALSE);
2365 }
2366 return 1;
2367 }
2368
2369 static int
2370 settings_reset_all (lua_State *L)
2371 {
2372 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2373 if (s)
2374 wp_settings_reset_all (s);
2375 return 0;
2376 }
2377
2378 static int
2379 settings_save_all (lua_State *L)
2380 {
2381 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2382 if (s)
2383 wp_settings_save_all (s);
2384 return 0;
2385 }
2386
2387 static int
2388 settings_delete_all (lua_State *L)
2389 {
2390 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2391 if (s)
2392 wp_settings_delete_all (s);
2393 return 0;
2394 }
2395
2396 static int
2397 settings_iterate (lua_State *L)
2398 {
2399 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2400 WpIterator *it = wp_settings_new_iterator (s);
2401 return push_settings_wpiterator (L, it);
2402 }
2403
2404 static int
2405 settings_subscribe (lua_State *L)
2406 {
2407 const gchar *pattern = luaL_checkstring (L, 1);
2408 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2409
2410 guintptr sub_id = 0;
2411
2412 GClosure * closure = wplua_function_to_closure (L, -1);
2413
2414 if (s)
2415 sub_id = wp_settings_subscribe_closure (s, pattern, closure);
2416
2417 lua_pushinteger (L, sub_id);
2418 return 1;
2419 }
2420
2421 static int
2422 settings_unsubscribe (lua_State *L)
2423 {
2424 guintptr sub_id = luaL_checkinteger (L, 1);
2425 gboolean ret = FALSE;
2426 g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
2427
2428 if (s)
2429 ret = wp_settings_unsubscribe (s, sub_id);
2430
2431 lua_pushboolean (L, ret);
2432 return 1;
2433 }
2434
2435 static const luaL_Reg settings_methods[] = {
2436 { "get", settings_get },
2437 { "get_boolean", settings_get_boolean },
2438 { "get_int", settings_get_int },
2439 { "get_float", settings_get_float },
2440 { "get_string", settings_get_string },
2441 { "get_array", settings_get_array },
2442 { "get_object", settings_get_object },
2443 { "get_saved", settings_get_saved },
2444 { "set", settings_set },
2445 { "reset", settings_reset },
2446 { "save", settings_save },
2447 { "delete", settings_delete },
2448 { "reset_all", settings_reset_all },
2449 { "save_all", settings_save_all },
2450 { "delete_all", settings_delete_all },
2451 { "iterate", settings_iterate },
2452 { "subscribe", settings_subscribe },
2453 { "unsubscribe", settings_unsubscribe },
2454 { NULL, NULL }
2455 };
2456
2457 /* WpEvent */
2458
2459 static int
2460 event_get_properties (lua_State *L)
2461 {
2462 WpEvent *event = wplua_checkboxed (L, 1, WP_TYPE_EVENT);
2463 WpProperties *props = wp_event_get_properties (event);
2464 wplua_pushboxed (L, WP_TYPE_PROPERTIES, props);
2465 return 1;
2466 }
2467
2468 static int
2469 event_get_source (lua_State *L)
2470 {
2471 WpEvent *event = wplua_checkboxed (L, 1, WP_TYPE_EVENT);
2472 wplua_pushobject (L, wp_event_get_source (event));
2473 return 1;
2474 }
2475
2476 static int
2477 event_get_subject (lua_State *L)
2478 {
2479 WpEvent *event = wplua_checkboxed (L, 1, WP_TYPE_EVENT);
2480 wplua_pushobject (L, wp_event_get_subject (event));
2481 return 1;
2482 }
2483
2484 static int
2485 event_stop_processing (lua_State *L)
2486 {
2487 WpEvent *event = wplua_checkboxed (L, 1, WP_TYPE_EVENT);
2488 wp_event_stop_processing (event);
2489 return 0;
2490 }
2491
2492 static int
2493 event_set_data (lua_State *L)
2494 {
2495 WpEvent *event = wplua_checkboxed (L, 1, WP_TYPE_EVENT);
2496 const gchar *key = luaL_checkstring (L, 2);
2497 GType type = G_TYPE_INVALID;
2498 g_auto (GValue) value = G_VALUE_INIT;
2499 const GValue *data = NULL;
2500
2501 switch (lua_type (L, 3)) {
2502 case LUA_TNONE:
2503 case LUA_TNIL:
2504 break;
2505 case LUA_TUSERDATA:
2506 type = wplua_gvalue_userdata_type (L, 3);
2507 if (G_UNLIKELY (type == G_TYPE_INVALID))
2508 wp_warning ("cannot set userdata on event data (not GValue userdata)");
2509 break;
2510 case LUA_TBOOLEAN:
2511 type = G_TYPE_BOOLEAN;
2512 break;
2513 case LUA_TNUMBER:
2514 type = lua_isinteger (L, 3) ? G_TYPE_INT64 : G_TYPE_DOUBLE;
2515 break;
2516 case LUA_TSTRING:
2517 type = G_TYPE_STRING;
2518 break;
2519 case LUA_TTABLE:
2520 type = WP_TYPE_PROPERTIES;
2521 break;
2522 default:
2523 wp_warning ("cannot set value on event data (value type not supported)");
2524 break;
2525 }
2526
2527 if (type != G_TYPE_INVALID) {
2528 g_value_init (&value, type);
2529 wplua_lua_to_gvalue (L, 3, &value);
2530 data = &value;
2531 }
2532
2533 wp_event_set_data (event, key, data);
2534 return 0;
2535 }
2536
2537 static int
2538 event_get_data (lua_State *L)
2539 {
2540 WpEvent *event = wplua_checkboxed (L, 1, WP_TYPE_EVENT);
2541 const gchar *key = luaL_checkstring (L, 2);
2542 const GValue *data = wp_event_get_data (event, key);
2543 if (data)
2544 wplua_gvalue_to_lua (L, data);
2545 else
2546 lua_pushnil (L);
2547 return 1;
2548 }
2549
2550 static const luaL_Reg event_methods[] = {
2551 { "get_properties", event_get_properties },
2552 { "get_source", event_get_source },
2553 { "get_subject", event_get_subject },
2554 { "stop_processing", event_stop_processing },
2555 { "set_data", event_set_data },
2556 { "get_data", event_get_data },
2557 { NULL, NULL }
2558 };
2559
2560 /* WpEventDispatcher */
2561
2562 static int
2563 event_dispatcher_push_event (lua_State *L)
2564 {
2565 const gchar *type = NULL;
2566 gint priority = 0;
2567 WpProperties *properties = NULL;
2568 GObject *source = NULL;
2569 GObject *subject = NULL;
2570 WpEvent *event = NULL;
2571
2572 if (lua_type (L, 1) == LUA_TTABLE) {
2573 lua_pushliteral (L, "type");
2574 if (lua_gettable (L, 1) != LUA_TSTRING)
2575 luaL_error (L, "EventDispatcher.push_event: expected 'type' as string");
2576 type = lua_tostring (L, -1);
2577 // not popping to keep the string allocated
2578
2579 lua_pushliteral (L, "priority");
2580 if (lua_gettable (L, 1) != LUA_TNUMBER)
2581 luaL_error (L, "EventDispatcher.push_event: expected 'priority' as number");
2582 priority = lua_tointeger (L, -1);
2583 lua_pop (L, 1);
2584
2585 lua_pushliteral (L, "properties");
2586 if (lua_istable (L, -1))
2587 properties = wplua_table_to_properties (L, -1);
2588 else if (!lua_isnil (L, -1) && !lua_isnone (L, -1) && !lua_isstring (L, -1))
2589 properties = wp_properties_ref (
2590 wplua_checkboxed (L, -1, WP_TYPE_PROPERTIES));
2591 lua_pop (L, 1);
2592
2593 lua_pushliteral (L, "source");
2594 if (lua_gettable (L, 1) != LUA_TNIL)
2595 source = wplua_checkobject (L, -1, G_TYPE_OBJECT);
2596 lua_pop (L, 1);
2597
2598 lua_pushliteral (L, "subject");
2599 if (lua_gettable (L, 1) != LUA_TNIL)
2600 subject = wplua_checkobject (L, -1, G_TYPE_OBJECT);
2601 lua_pop (L, 1);
2602
2603 event = wp_event_new (type, priority, properties, source, subject);
2604 } else {
2605 event = wp_event_ref (wplua_checkboxed (L, 1, WP_TYPE_EVENT));
2606 }
2607
2608 g_autoptr (WpEventDispatcher) dispatcher =
2609 wp_event_dispatcher_get_instance (get_wp_core (L));
2610 wp_event_dispatcher_push_event (dispatcher, wp_event_ref (event));
2611 wplua_pushboxed (L, WP_TYPE_EVENT, event);
2612 return 1;
2613 }
2614
2615 static const luaL_Reg event_dispatcher_funcs[] = {
2616 { "push_event", event_dispatcher_push_event },
2617 { NULL, NULL }
2618 };
2619
2620 /* WpEventHook */
2621
2622 static int
2623 event_hook_register (lua_State *L)
2624 {
2625 WpEventHook *hook = wplua_checkobject (L, 1, WP_TYPE_EVENT_HOOK);
2626 g_autoptr (WpEventDispatcher) dispatcher =
2627 wp_event_dispatcher_get_instance (get_wp_core (L));
2628 wp_event_dispatcher_register_hook (dispatcher, hook);
2629 return 0;
2630 }
2631
2632 static int
2633 event_hook_remove (lua_State *L)
2634 {
2635 WpEventHook *hook = wplua_checkobject (L, 1, WP_TYPE_EVENT_HOOK);
2636 g_autoptr (WpEventDispatcher) dispatcher =
2637 wp_event_dispatcher_get_instance (get_wp_core (L));
2638 wp_event_dispatcher_unregister_hook (dispatcher, hook);
2639 return 0;
2640 }
2641
2642 static const luaL_Reg event_hook_methods[] = {
2643 { "register", event_hook_register },
2644 { "remove", event_hook_remove },
2645 { NULL, NULL }
2646 };
2647
2648 /* WpSimpleEventHook */
2649
2650 static int
2651 simple_event_hook_new (lua_State *L)
2652 {
2653 WpEventHook *hook = NULL;
2654 int before_size = 0, after_size = 0, i = 0;
2655 const gchar **before, **after;
2656 const gchar *name;
2657 GClosure *closure = NULL;
2658
2659 /* discard any possible arguments after the first one to avoid
2660 any surprises when working with absolute stack indices below */
2661 lua_settop (L, 1);
2662
2663 /* validate arguments */
2664 luaL_checktype (L, 1, LUA_TTABLE);
2665
2666 if (lua_getfield (L, 1, "name") != LUA_TSTRING)
2667 luaL_error(L, "SimpleEventHook: expected 'name' as string");
2668
2669 if (lua_getfield (L, 1, "execute") != LUA_TFUNCTION)
2670 luaL_error (L, "SimpleEventHook: expected 'execute' as function");
2671
2672 switch (lua_getfield (L, 1, "before")) {
2673 case LUA_TTABLE:
2674 lua_len (L, -1);
2675 before_size = lua_tointeger (L, -1);
2676 lua_pop (L, 1);
2677 break;
2678 case LUA_TSTRING:
2679 before_size = 1;
2680 break;
2681 case LUA_TNIL:
2682 before_size = 0;
2683 break;
2684 default:
2685 luaL_error(L, "SimpleEventHook: unexpected value type for 'before'; "
2686 "should be table or string");
2687 }
2688
2689 switch (lua_getfield (L, 1, "after")) {
2690 case LUA_TTABLE:
2691 lua_len (L, -1);
2692 after_size = lua_tointeger (L, -1);
2693 lua_pop (L, 1);
2694 break;
2695 case LUA_TSTRING:
2696 after_size = 1;
2697 break;
2698 case LUA_TNIL:
2699 after_size = 0;
2700 break;
2701 default:
2702 luaL_error(L, "SimpleEventHook: unexpected value type for 'after'; "
2703 "should be table or string");
2704 }
2705
2706 /* allocate C stack space for before & after arrays */
2707 before = before_size > 0 ?
2708 (const gchar **) g_newa (gpointer, before_size + 1) : NULL;
2709 after = after_size > 0 ?
2710 (const gchar **) g_newa (gpointer, after_size + 1) : NULL;
2711
2712 /* parse before */
2713 if (lua_type (L, 4) == LUA_TTABLE && before_size > 0) {
2714 i = 0;
2715 lua_pushnil (L);
2716 while (lua_next (L, 4) && i < before_size) {
2717 before[i++] = luaL_checkstring (L, -1);
2718 /* bring the key on top without popping the string value */
2719 lua_rotate (L, lua_gettop (L) - 1, 1);
2720 }
2721 before[i] = NULL;
2722 } else if (lua_type (L, 4) == LUA_TSTRING) {
2723 before[0] = lua_tostring (L, 4);
2724 before[1] = NULL;
2725 }
2726
2727 /* parse after */
2728 if (lua_type (L, 5) == LUA_TTABLE && after_size > 0) {
2729 i = 0;
2730 lua_pushnil (L);
2731 while (lua_next (L, 5) && i < after_size) {
2732 after[i++] = luaL_checkstring (L, -1);
2733 /* bring the key on top without popping the string value */
2734 lua_rotate (L, lua_gettop (L) - 1, 1);
2735 }
2736 after[i] = NULL;
2737 } else if (lua_type (L, 5) == LUA_TSTRING) {
2738 after[0] = lua_tostring (L, 5);
2739 after[1] = NULL;
2740 }
2741
2742 name = lua_tostring (L, 2);
2743 closure = wplua_function_to_closure (L, 3);
2744
2745 hook = wp_simple_event_hook_new (name, before, after, closure);
2746
2747 /* clear the lua stack now to make some space */
2748 lua_settop (L, 1);
2749
2750 wplua_pushobject (L, hook);
2751
2752 if (lua_getfield (L, 1, "interests") == LUA_TTABLE) {
2753 lua_pushnil (L);
2754 while (lua_next (L, -2)) {
2755 WpObjectInterest *interest =
2756 wplua_checkboxed (L, -1, WP_TYPE_OBJECT_INTEREST);
2757 wp_interest_event_hook_add_interest_full (WP_INTEREST_EVENT_HOOK (hook),
2758 wp_object_interest_ref (interest));
2759 lua_pop (L, 1);
2760 }
2761 }
2762 lua_pop (L, 1);
2763
2764 return 1;
2765 }
2766
2767 /* WpAsyncEventHook */
2768
2769 static int
2770 async_event_hook_get_next_step (lua_State *L)
2771 {
2772 WpTransition *transition = wplua_checkobject (L, 1, WP_TYPE_TRANSITION);
2773 guint step = luaL_checkinteger (L, 2);
2774
2775 wp_trace_object (transition, "prev step: %u", step);
2776
2777 if (step == WP_TRANSITION_STEP_NONE) {
2778 lua_pushinteger (L, WP_TRANSITION_STEP_CUSTOM_START);
2779 return 1;
2780 }
2781
2782 /* step number is the value on the stack at this point */
2783 if (G_UNLIKELY (lua_gettable (L, lua_upvalueindex (1)) != LUA_TSTRING)) {
2784 wp_critical_object (transition, "unknown step number");
2785 lua_pushinteger (L, WP_TRANSITION_STEP_ERROR);
2786 return 1;
2787 }
2788 /* step string is now on the stack */
2789 if (G_UNLIKELY (lua_gettable (L, lua_upvalueindex (1)) != LUA_TTABLE)) {
2790 wp_critical_object (transition, "unknown step string");
2791 lua_pushinteger (L, WP_TRANSITION_STEP_ERROR);
2792 return 1;
2793 }
2794 lua_pushliteral (L, "next_idx");
2795 if (G_UNLIKELY (lua_gettable (L, -2) != LUA_TNUMBER)) {
2796 wp_critical_object (transition, "next_idx not found");
2797 lua_pushinteger (L, WP_TRANSITION_STEP_ERROR);
2798 return 1;
2799 }
2800 return 1;
2801 }
2802
2803 static int
2804 async_event_hook_execute_step (lua_State *L)
2805 {
2806 WpTransition *transition = wplua_checkobject (L, 1, WP_TYPE_TRANSITION);
2807 WpEvent *event = wp_transition_get_data (transition);
2808 guint step = luaL_checkinteger (L, 2);
2809 const char *step_str = NULL;
2810
2811 wp_trace_object (transition, "execute step: %u", step);
2812
2813 if (G_LIKELY (step != WP_TRANSITION_STEP_ERROR)) {
2814 /* step_str = steps_table[step_number] */
2815 /* step number is the value on the stack at this point */
2816 if (G_UNLIKELY (lua_gettable (L, lua_upvalueindex (1)) != LUA_TSTRING)) {
2817 wp_critical_object (transition, "unknown step number %u", step);
2818 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
2819 WP_LIBRARY_ERROR_INVARIANT, "unknown step number %u", step));
2820 return 0;
2821 }
2822 } else {
2823 /* try to execute a step called "error", if it exists */
2824 lua_pushliteral (L, "error");
2825 }
2826 step_str = lua_tostring (L, -1);
2827
2828 /* step string is now on the stack */
2829 if (G_UNLIKELY (lua_gettable (L, lua_upvalueindex (1)) != LUA_TTABLE)) {
2830 /* it's ok if the "error" step is missing */
2831 if (step != WP_TRANSITION_STEP_ERROR) {
2832 wp_critical_object (transition, "unknown step string '%s'", step_str);
2833 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
2834 WP_LIBRARY_ERROR_INVARIANT, "unknown step string '%s", step_str));
2835 }
2836 return 0;
2837 }
2838
2839 lua_pushliteral (L, "execute");
2840 if (G_UNLIKELY (lua_gettable (L, -2) != LUA_TFUNCTION)) {
2841 wp_critical_object (transition, "no execute function defined for '%s'",
2842 step_str);
2843 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
2844 WP_LIBRARY_ERROR_INVARIANT, "no execute function defined for '%s'",
2845 step_str));
2846 return 0;
2847 }
2848
2849 wplua_pushboxed (L, WP_TYPE_EVENT, wp_event_ref (event));
2850 wplua_pushobject (L, g_object_ref (transition));
2851 lua_call (L, 2, 0);
2852 return 0;
2853 }
2854
2855 static void
2856 async_event_hook_prepare_steps_table (lua_State *L, int steps_tbl)
2857 {
2858 const char *step_str = NULL;
2859 int step_str_index = 0;
2860 int step = WP_TRANSITION_STEP_CUSTOM_START;
2861
2862 steps_tbl = lua_absindex (L, steps_tbl);
2863
2864 lua_pushliteral (L, "start");
2865 step_str_index = lua_absindex (L, -1);
2866 step_str = lua_tostring (L, -1);
2867
2868 while (step != WP_TRANSITION_STEP_NONE) {
2869 /* steps[step number] = step string */
2870 lua_pushvalue (L, -1);
2871 lua_seti (L, steps_tbl, step);
2872
2873 lua_pushvalue (L, -1);
2874 if (lua_gettable (L, steps_tbl) != LUA_TTABLE)
2875 luaL_error (L, "AsyncEventHook: expected '%s' in 'steps'", step_str);
2876
2877 lua_pushinteger (L, step++);
2878 lua_setfield (L, -2, "idx");
2879
2880 lua_pushliteral (L, "next");
2881 if (lua_gettable (L, -2) != LUA_TSTRING)
2882 luaL_error (L, "AsyncEventHook: expected 'next' in step '%s'", step_str);
2883 lua_replace (L, step_str_index);
2884 step_str = lua_tostring (L, step_str_index);
2885
2886 if (!g_strcmp0 (step_str, "none"))
2887 step = WP_TRANSITION_STEP_NONE;
2888
2889 lua_pushinteger (L, step);
2890 lua_setfield (L, -2, "next_idx");
2891
2892 lua_settop (L, step_str_index);
2893 }
2894
2895 lua_pop (L, 1);
2896 }
2897
2898 static int
2899 async_event_hook_new (lua_State *L)
2900 {
2901 WpEventHook *hook = NULL;
2902 int before_size = 0, after_size = 0, i = 0;
2903 const gchar **before, **after;
2904 const gchar *name;
2905 GClosure *get_next_step = NULL;
2906 GClosure *execute_step = NULL;
2907
2908 /* discard any possible arguments after the first one to avoid
2909 any surprises when working with absolute stack indices below */
2910 lua_settop (L, 1);
2911
2912 /* validate arguments */
2913 luaL_checktype (L, 1, LUA_TTABLE);
2914
2915 if (lua_getfield (L, 1, "name") != LUA_TSTRING)
2916 luaL_error(L, "AsyncEventHook: expected 'name' as string");
2917
2918 if (lua_getfield (L, 1, "steps") != LUA_TTABLE)
2919 luaL_error (L, "AsyncEventHook: expected 'steps' as table");
2920
2921 switch (lua_getfield (L, 1, "before")) {
2922 case LUA_TTABLE:
2923 lua_len (L, -1);
2924 before_size = lua_tointeger (L, -1);
2925 lua_pop (L, 1);
2926 break;
2927 case LUA_TSTRING:
2928 before_size = 1;
2929 break;
2930 case LUA_TNIL:
2931 before_size = 0;
2932 break;
2933 default:
2934 luaL_error(L, "AsyncEventHook: unexpected value type for 'before'; "
2935 "should be table or string");
2936 }
2937
2938 switch (lua_getfield (L, 1, "after")) {
2939 case LUA_TTABLE:
2940 lua_len (L, -1);
2941 after_size = lua_tointeger (L, -1);
2942 lua_pop (L, 1);
2943 break;
2944 case LUA_TSTRING:
2945 after_size = 1;
2946 break;
2947 case LUA_TNIL:
2948 after_size = 0;
2949 break;
2950 default:
2951 luaL_error(L, "AsyncEventHook: unexpected value type for 'after'; "
2952 "should be table or string");
2953 }
2954
2955 /* allocate C stack space for before & after arrays */
2956 before = before_size > 0 ?
2957 (const gchar **) g_newa (gpointer, before_size + 1) : NULL;
2958 after = after_size > 0 ?
2959 (const gchar **) g_newa (gpointer, after_size + 1) : NULL;
2960
2961 /* parse before */
2962 if (lua_type (L, 4) == LUA_TTABLE && before_size > 0) {
2963 i = 0;
2964 lua_pushnil (L);
2965 while (lua_next (L, 4) && i < before_size) {
2966 before[i++] = luaL_checkstring (L, -1);
2967 /* bring the key on top without popping the string value */
2968 lua_rotate (L, lua_gettop (L) - 1, 1);
2969 }
2970 before[i] = NULL;
2971 } else if (lua_type (L, 4) == LUA_TSTRING) {
2972 before[0] = lua_tostring (L, 4);
2973 before[1] = NULL;
2974 }
2975
2976 /* parse after */
2977 if (lua_type (L, 5) == LUA_TTABLE && after_size > 0) {
2978 i = 0;
2979 lua_pushnil (L);
2980 while (lua_next (L, 5) && i < after_size) {
2981 after[i++] = luaL_checkstring (L, -1);
2982 /* bring the key on top without popping the string value */
2983 lua_rotate (L, lua_gettop (L) - 1, 1);
2984 }
2985 after[i] = NULL;
2986 } else if (lua_type (L, 5) == LUA_TSTRING) {
2987 after[0] = lua_tostring (L, 5);
2988 after[1] = NULL;
2989 }
2990
2991 name = lua_tostring (L, 2);
2992 async_event_hook_prepare_steps_table (L, 3);
2993
2994 lua_pushvalue (L, 3); /* pass 'steps' table as upvalue */
2995 lua_pushcclosure (L, async_event_hook_get_next_step, 1);
2996 get_next_step = wplua_function_to_closure (L, -1);
2997 lua_pop (L, 1);
2998
2999 lua_pushvalue (L, 3); /* pass 'steps' table as upvalue */
3000 lua_pushcclosure (L, async_event_hook_execute_step, 1);
3001 execute_step = wplua_function_to_closure (L, -1);
3002 lua_pop (L, 1);
3003
3004 hook = wp_async_event_hook_new (name, before, after, get_next_step,
3005 execute_step);
3006
3007 /* clear the lua stack now to make some space */
3008 lua_settop (L, 1);
3009
3010 wplua_pushobject (L, hook);
3011
3012 if (lua_getfield (L, 1, "interests") == LUA_TTABLE) {
3013 lua_pushnil (L);
3014 while (lua_next (L, -2)) {
3015 WpObjectInterest *interest =
3016 wplua_checkboxed (L, -1, WP_TYPE_OBJECT_INTEREST);
3017 wp_interest_event_hook_add_interest_full (WP_INTEREST_EVENT_HOOK (hook),
3018 wp_object_interest_ref (interest));
3019 lua_pop (L, 1);
3020 }
3021 }
3022 lua_pop (L, 1);
3023
3024 return 1;
3025 }
3026
3027 static int
3028 transition_advance (lua_State *L)
3029 {
3030 WpTransition *t = wplua_checkobject (L, 1, WP_TYPE_TRANSITION);
3031 wp_transition_advance (t);
3032 return 0;
3033 }
3034
3035 static int
3036 transition_return_error (lua_State *L)
3037 {
3038 WpTransition *t = wplua_checkobject (L, 1, WP_TYPE_TRANSITION);
3039 const char *err = luaL_checkstring (L, 2);
3040 wp_transition_return_error (t, g_error_new (WP_DOMAIN_LIBRARY,
3041 WP_LIBRARY_ERROR_OPERATION_FAILED, "%s", err));
3042 return 0;
3043 }
3044
3045 static const luaL_Reg transition_methods[] = {
3046 { "advance", transition_advance },
3047 { "return_error", transition_return_error },
3048 { NULL, NULL }
3049 };
3050
3051 void
3052 wp_lua_scripting_api_init (lua_State *L)
3053 {
3054 g_autoptr (GError) error = NULL;
3055
3056 luaL_newlib (L, glib_methods);
3057 lua_setglobal (L, "GLib");
3058
3059 luaL_newlib (L, i18n_funcs);
3060 lua_setglobal (L, "I18n");
3061
3062 luaL_newlib (L, log_funcs);
3063 lua_setglobal (L, "WpLog");
3064
3065 luaL_newlib (L, core_funcs);
3066 lua_setglobal (L, "WpCore");
3067
3068 luaL_newlib (L, plugin_funcs);
3069 lua_setglobal (L, "WpPlugin");
3070
3071 luaL_newlib (L, conf_methods);
3072 lua_setglobal (L, "WpConf");
3073
3074 luaL_newlib (L, json_utils_funcs);
3075 lua_setglobal (L, "JsonUtils");
3076
3077 luaL_newlib (L, proc_utils_funcs);
3078 lua_setglobal (L, "ProcUtils");
3079
3080 luaL_newlib (L, settings_methods);
3081 lua_setglobal (L, "WpSettings");
3082
3083 luaL_newlib (L, event_dispatcher_funcs);
3084 lua_setglobal (L, "WpEventDispatcher");
3085
3086 wp_lua_scripting_pod_init (L);
3087 wp_lua_scripting_json_init (L);
3088
3089 wplua_register_type_methods (L, G_TYPE_SOURCE,
3090 NULL, source_methods);
3091 wplua_register_type_methods (L, WP_TYPE_OBJECT,
3092 NULL, object_methods);
3093 wplua_register_type_methods (L, WP_TYPE_PROXY,
3094 NULL, proxy_methods);
3095 wplua_register_type_methods (L, WP_TYPE_GLOBAL_PROXY,
3096 NULL, global_proxy_methods);
3097 wplua_register_type_methods (L, WP_TYPE_OBJECT_INTEREST,
3098 object_interest_new, object_interest_methods);
3099 wplua_register_type_methods (L, WP_TYPE_OBJECT_MANAGER,
3100 object_manager_new, object_manager_methods);
3101 wplua_register_type_methods (L, WP_TYPE_METADATA,
3102 NULL, metadata_methods);
3103 wplua_register_type_methods (L, WP_TYPE_IMPL_METADATA,
3104 impl_metadata_new, NULL);
3105 wplua_register_type_methods (L, WP_TYPE_DEVICE,
3106 device_new, NULL);
3107 wplua_register_type_methods (L, WP_TYPE_SPA_DEVICE,
3108 spa_device_new, spa_device_methods);
3109 wplua_register_type_methods (L, WP_TYPE_NODE,
3110 node_new, node_methods);
3111 wplua_register_type_methods (L, WP_TYPE_IMPL_NODE,
3112 impl_node_new, NULL);
3113 wplua_register_type_methods (L, WP_TYPE_PORT,
3114 NULL, port_methods);
3115 wplua_register_type_methods (L, WP_TYPE_LINK,
3116 link_new, NULL);
3117 wplua_register_type_methods (L, WP_TYPE_CLIENT,
3118 NULL, client_methods);
3119 wplua_register_type_methods (L, WP_TYPE_SESSION_ITEM,
3120 session_item_new, session_item_methods);
3121 wplua_register_type_methods (L, WP_TYPE_SI_ADAPTER,
3122 NULL, si_adapter_methods);
3123 wplua_register_type_methods (L, WP_TYPE_PIPEWIRE_OBJECT,
3124 NULL, pipewire_object_methods);
3125 wplua_register_type_methods (L, WP_TYPE_STATE,
3126 state_new, state_methods);
3127 wplua_register_type_methods (L, WP_TYPE_IMPL_MODULE,
3128 impl_module_new, NULL);
3129 wplua_register_type_methods (L, WP_TYPE_EVENT,
3130 NULL, event_methods);
3131 wplua_register_type_methods (L, WP_TYPE_EVENT_HOOK,
3132 NULL, event_hook_methods);
3133 wplua_register_type_methods (L, WP_TYPE_SIMPLE_EVENT_HOOK,
3134 simple_event_hook_new, NULL);
3135 wplua_register_type_methods (L, WP_TYPE_ASYNC_EVENT_HOOK,
3136 async_event_hook_new, NULL);
3137 wplua_register_type_methods (L, WP_TYPE_TRANSITION,
3138 NULL, transition_methods);
3139 wplua_register_type_methods (L, WP_TYPE_CONF,
3140 conf_new, conf_methods);
3141 wplua_register_type_methods (L, WP_TYPE_PROC_INFO,
3142 NULL, proc_info_funcs);
3143 wplua_register_type_methods (L, WP_TYPE_ITERATOR,
3144 NULL, iterator_funcs);
3145 wplua_register_type_methods (L, WP_TYPE_PROPERTIES,
3146 properties_new, properties_funcs);
3147
3148 if (!wplua_load_uri (L, URI_API, &error) ||
3149 !wplua_pcall (L, 0, 0, &error)) {
3150 wp_critical ("Failed to load api: %s", error->message);
3151 }
3152 }