Commit graph

105 commits

Author SHA1 Message Date
Julian Bouzas
be6f2b2926 m-lua-scripting: Handle both Properties and Lua tables in all Lua APIs 2025-11-24 19:42:07 +02:00
Julian Bouzas
01eb206460 m-lua-scripting: Add get_property() API for pipewire objects
This can be faster if we only want to get one property.
2025-11-24 19:42:07 +02:00
Julian Bouzas
5a4ecceee6 m-lua-scripting: Add get_property() API for session items
This can be faster if we only want to get one property.
2025-11-24 19:42:07 +02:00
Julian Bouzas
a35e40c1d2 m-lua-scripting: Add WpProperties API
Similar to Pod and Json, this API allows handling WpProperties references.
2025-11-24 19:42:07 +02:00
Julian Bouzas
4239055454 m-lua-scripting: Pass returned itrator to the closure when finishing enum_param()
We should not ignore the returned iterator as it allows users to get the exact
returned params after enumerating them, which might be useful in some cases.
2025-10-17 09:05:27 -04:00
Julian Bouzas
f188ddfb34 m-lua-scripting: Add WpIterator API
We skip the fold() API as the same thing can be done just using Lua closures.
2025-10-17 09:05:22 -04:00
Julian Bouzas
8ab6ae5897 m-lua-scripting: Add enum_params Lua API for WpPipewireObject 2025-08-22 12:15:08 -04:00
Demi Marie Obenour
f3625bee61 lua: fix SPA POD array and choice builders
These builders had many bugs:

1. They would longjmp() across the destructor of a g_autoptr() if a Lua
   error was thrown.  This will leak the memory in the g_autoptr()
   unless Lua is compiled with C++ exceptions.
2. They depended on the iteration order of numerical keys in Lua tables.
   Lua explicitly does not specify this order.
3. They would produce nonsensical SPA POD array or choice types with
   strings or bytes as values.  These would cause undefined behavior if
   manipulated by naive C code, or assertion failures if
   spa_pod_is_array() and spa_pod_is_choice() are modified to check that
   the contents of arrays and choices have sensible types.
4. They silently accepted extra arguments, potentially causing confusion
   and making it harder to extend the functions in a
   backwards-compatible way.

Solve the first problem by calling functions that can raise a Lua error
in a protected environment (with lua_pcall).  If there is a Lua error,
rethrow it after the g_autoptr() destructor has run.

Solve the second problem by first obtaining the number of keys in the
table and then iterating over the keys that are expected to be present.
If any of the keys are not contiguious integers starting at 1, the range
[1..number of keys] will include a number that is not a table key.  This
will result in lua_rawgeti pushing a nil onto the Lua stack.  An
explicit check throws a useful error in this case.

Solve the third problem by explicitly checking that the type is
reasonable before building an array or choice.  If it is wrong,
a Lua error is thrown.

Solve the fourth problem by using luaL_checktype (L, 2, LUA_TNONE) to
check that no unwanted values were passed.  The C function called with
lua_pcall is passed every argument passed by Lua, followed by a light
userdata that stores a context pointer.  After the light userdata is
popped from the Lua stack, the Lua stack is identical to what Lua
created when it called the outer C function, so the type-checking
functions in the auxillary library can be used to enforce that only the
correct number and type of arguments were passed.
2025-07-24 12:36:29 +03:00
Julian Bouzas
86cdfaccc4 lib: Add new proc-utils API for process utilities 2025-03-05 16:28:34 +02:00
George Kiagiadakis
b697546476 lua: bind wp_core_update_properties() 2025-02-07 11:05:44 +00:00
George Kiagiadakis
a1bc3d9285 lua: fix wp_lua_log_topic_copy() to copy the topic name correctly
Fixes: #757
2025-01-08 11:35:57 +02:00
Pauli Virtanen
2df5d94697 m-lua-scripting: add WpSpaDevice:set_managed_pending 2024-12-15 20:51:55 +02:00
Ashok Sidipotu
a6328cf2c1 m-lua-scripting: correct typo 2024-05-08 14:20:16 +00:00
Pauli Virtanen
2fb055f43d log: add wp_logt_checked taking WpLogTopic, to decide on debug messages
Make decision on whether to show code location in journal messages based
on whether the WpLogTopic would enable DEBUG level messages.

Add wp_logt_checked API to take WpLogTopic as input to make this
possible, and deprecate wp_log_checked.
2024-05-03 13:46:52 +00:00
Stefan Ursella
709eecb21f lua: json: fix error ouput 2024-04-30 05:32:33 +00:00
Stefan Ursella
fd7a1af7ff lua: json: add method to merge json containers 2024-04-30 05:32:33 +00:00
James Calligeros
d77edf70e9
lua: allow Conf methods to be indexed or called
Given that we are allowing Conf() to instantiate a new WpConf,
the methods on the class need to be callable in reference to Self while
maintaining API compatibility with being indexable from the table as a
static method.

Allow this by checking if the first argument passed in is userdata.

Signed-off-by: James Calligeros <jcalligeros99@gmail.com>
2024-04-17 18:31:09 +10:00
James Calligeros
f769ea8f30
lua: add constructor and file ops for WpConf
This exposes the ability to load a SPA-JSON representation of a WpConf
object from an arbitrary file on disk to the Lua API

Signed-off-by: James Calligeros <jcalligeros99@gmail.com>
2024-04-17 18:31:04 +10:00
George Kiagiadakis
053d2ae69c m-lua-scripting: downgrade notice to debug when printing operation errors
These errors are propagated to the caller, so it's the caller's
responsibility to print them appropriately, if necessary. Printing
a notice also here is only confusing.

A debug mesage is still be useful for developers to understand the flow.
2024-03-25 09:55:59 +02:00
George Kiagiadakis
d0b7dde4e9 log: make the log topic flags an enumeration and publicly documented
Fixes: #591
2024-03-18 16:39:45 +02:00
George Kiagiadakis
655a24acf0 scripts: remove cutils.evaluateRulesApplyProperties()
Cache the rules in a global variable in each script, as JSON,
and use JsonUtils directly to evaluate them. This will allow us to
close the WpConf in the future after loading the scripts.

Also change the order of the return values of the match_rules_apply_properties
function to be able to easily ignore the number of changed values,
which is useless in most cases.
2024-03-04 16:33:14 +00:00
George Kiagiadakis
3fbf1286e6 lua: change the Conf API to have methods for getting sections as specific types
In some cases we need to get a section as JSON, so that we can pass it
down to the rules parser, while in other cases we neeed to get it as a
table to use it natively, and in that case we even need to differentiate
between it being an object, an array or an object with WpProperties.

Make it also possible to optionally pass tables with default values to
the functions so that we can get rid of cutils.get_config_section()
as well.
2024-03-04 16:33:14 +00:00
George Kiagiadakis
c841ec97a8 conf: drop all the _get_value() functions and remove the fallback from _get_section()
We do not use these APIs, so there's no point in keeping them.

Realistically, every component that needs a section just does its
own parsing on it, so the _get_value() functions are not needed.

The fallback in _get_section() is also not needed, as we always
pass NULL and then test for it. In Lua, however, it seems we are
using the fallback to return an empty object, so that getting
a section does not expand to multiple lines of code. For that reason,
I have kept the syntax there and implemented it in the bindings layer.
2024-03-04 07:07:56 +00:00
George Kiagiadakis
60382df63f conf: refactor configuration loading
Changes:

- Configuration files are no longer located by libpipewire,
  which allows us to control the paths that are being looked up.
  This is a requirement for installations where pipewire and
  wireplumber are built using different prefixes, in which case
  the configuration files of wireplumber end up being installed in
  a place that libpipewire doesn't look into...

- The location of conf files is now again $prefix/share/wireplumber,
  /etc/wireplumber and $XDG_CONFIG_HOME/wireplumber, instead of using
  the pipewire directories. Also, since the previous commits, we now
  also support $XDG_CONFIG_DIRS/wireplumber (typically /etc/xdg/wireplumber)
  and $XDG_DATA_DIRS/wireplumber for system-wide configuration.

- Since libpipewire doesn't expose the parser, we now also do the
  parsing of sections ourselves. This has the advantage that we can
  optimize it a bit for our use case.

- The WpConf API has changed to not be a singleton and it is a
  property of WpCore instead. The configuration is now expected
  to be opened before the core is created, which allows the caller
  to identify configuration errors in advance. By not being a singleton,
  we can also reuse the WpConf API to open other SPA-JSON files.

- WpConf also now has a lazy loading mechanism. The configuration
  files are mmap'ed and the various sections are located in advance,
  but not parsed until they are actually requested. Also, the sections
  are not copied in memory, unlike what happens in libpipewire. They
  are only copied when merging is needed.

- WpCore now disables loading of a configuration file in pw_context,
  if a WpConf is provided. This is to have complete control here.
  The 'context.spa-libs' and 'context.modules' sections are still
  loaded, but we load them in WpConf and pass them down to pw_context
  for parsing. If a WpConf is not provided, pw_context is left to load
  the default configuration file (client.conf normally).
2024-03-04 07:07:56 +00:00
Julian Bouzas
f2e7a41175 m-lua-scripting: complete Lua Settings API
This patch adds the new WpSettings API in Lua.
2024-02-28 10:20:26 -05:00
Julian Bouzas
a23248847a metadata: remove wp_metadata_iterator_item_extract() API
Similar to WpPropertiesItem, this implements a new WpMetadataItem type that is
returned when iterating metadata
2024-02-28 08:15:17 -05:00
George Kiagiadakis
475ec4944d lib/settings: make the WpSettings object a non-singleton
This doesn't need to be a singleton, since we have the core registration
API available publicly nowadays. Makes things more clean for the API,
following the pattern of WpPlugin and WpSiFactory and simplifies the
built-in settings component in the internal component loader :)
2024-02-10 17:48:23 +02:00
Pauli Virtanen
82df32b0b0 m-lua-scripting: register/unregister log topics 2024-01-13 16:33:05 +00:00
George Kiagiadakis
7bd4032a28 lua: hooks: record the entire 'after' array
Previously we were ignoring the last element in the 'after' arrays
because of a wrong loop limit
2024-01-08 12:28:21 +02:00
George Kiagiadakis
eb2d6efcd4 state: add save_after_timeout() method to replace all custom timeout code
This was a common pattern that we had in many places, so it makes sense
to consolidate it.
2024-01-04 16:38:33 +02:00
George Kiagiadakis
c2d125b0da Merge branch 'master' into next 2023-12-23 18:34:00 +02:00
George Kiagiadakis
4feebfc3a3 m-lua-scripting: add Core.get_properties() method 2023-12-08 12:28:24 +02:00
George Kiagiadakis
b1f891e149 lua api: add support for PW_PERM_L 2023-11-13 17:18:40 +02:00
George Kiagiadakis
cf4fb87b35 lua api: allow nil to be passed on all constructors that take optional properties 2023-11-10 13:23:50 +02:00
George Kiagiadakis
a6bea40172 core: add wp_core_get_own_bound_id() method
This allows retrieving the bound-id of our own client
2023-11-10 11:27:13 +02:00
George Kiagiadakis
e88fa840f2 lua: json: add optional argument in the json parse() method to limit the number of recursions
This allows partially parsing a json object, allowing some parts to be
passed on as strings to another component that does its own parsing
(ex. a pipewire module)
2023-11-10 11:27:13 +02:00
George Kiagiadakis
0bc6ca6a2d lua: json: allow keys inside objects to be without quotes 2023-11-10 11:27:13 +02:00
George Kiagiadakis
a6dd60ff83 conf: remove wp_conf_apply_rules() 2023-11-07 16:36:49 +02:00
George Kiagiadakis
fbecc50319 lua: api: bind json-utils functions 2023-11-07 16:36:49 +02:00
George Kiagiadakis
dbf8204cf9 Revert "api: module: support loading arguments from file"
This reverts commit 2ae1b3cbd9.

This is not a good API. It was only allowed temporarily in 0.4.15
to get things done. We should approach this properly in 0.5
2023-10-24 11:55:52 +03:00
George Kiagiadakis
eb6f569be8 Merge branch 'master' into next 2023-10-24 11:09:52 +03:00
Pauli Virtanen
7f495b63eb lua: add new Client.update_properties() API
This allows Lua scripts to update properties of other clients.
2023-10-15 21:27:14 +00:00
Pauli Virtanen
b65f8884a5 m-lua-scripting: check argument type is table to avoid crashing
In conf_apply_rules, check input argument is a table before assuming it
is and potentially crashing.

Add check also in wplua_table_to_properties to avoid similar bugs.
2023-10-13 22:47:57 +03:00
Dmitry Sharshakov
2ae1b3cbd9 api: module: support loading arguments from file 2023-09-22 19:35:10 +03:00
George Kiagiadakis
67f9f63520 Merge branch '0.4' into next 2023-09-01 19:12:20 +03:00
Ashok Sidipotu
3fe930f15f api.c: add wp_core_test_feature API 2023-08-15 07:14:59 +00:00
Julian Bouzas
227dd97036 m-lua-scripting: always parse JSON to String if its type is not recognized
This allows parsing JSON strings without quotes.
2023-08-11 14:48:13 -04:00
George Kiagiadakis
840a2304b4 core: load components internally and refactor main to just activate it
+ move the export-core creation and media-session check to be built-in components
+ add WpCore API to find the export core
2023-06-23 18:01:26 +03:00
George Kiagiadakis
c7cb193588 core: introduce the notion of provided features on components
Each component can optionally "provide" a feature, which is basically
a string that describes the feature (ex. "support.dbus"). If the
component loads successfully, the feature is marked as provided and
can be tested for its presence with wp_core_test_feature()
2023-06-20 12:39:29 +03:00
George Kiagiadakis
55fc845098 component-loader: make wp_core_load_component_finish() return a boolean
There is no reason to return the component object... all components
are supposed to be long-lived objects that are referenced by the
registry and there is API to find them. The caller is only interested
in the success or failure of the operation.
2023-05-28 21:07:15 +03:00