Debian Codegraph @

modules/module-settings.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022 Collabora Ltd.
4 * @author Ashok Sidipotu <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <wp/wp.h>
10 #include <pipewire/pipewire.h>
11 #include <spa/utils/json.h>
12 #include <spa/utils/defs.h>
13
14 WP_DEFINE_LOCAL_LOG_TOPIC ("m-settings")
15
16 /*
17 * This module parses the "wireplumber.settings" section from the .conf file.
18 *
19 * Creates "sm-settings"(default) metadata and pushes the settings to it.
20 * Looks out for changes done in the metadata via the pw-metadata interface.
21 *
22 * If persistent settings is enabled stores the settings in a state file
23 * and retains the settings from there on subsequent reboots ignoring the
24 * contents of .conf file.
25 */
26
27 struct _WpSettingsPlugin
28 {
29 WpPlugin parent;
30
31 /* Props */
32 gchar *metadata_name;
33 gchar *metadata_schema_name;
34 gchar *metadata_persistent_name;
35
36 WpImplMetadata *impl_metadata;
37 WpImplMetadata *schema_impl_metadata;
38 WpImplMetadata *persistent_impl_metadata;
39 WpState *state;
40 WpProperties *persistent_settings;
41 };
42
43 enum {
44 PROP_0,
45 PROP_METADATA_NAME,
46 PROP_PROPERTIES,
47 };
48
49 G_DECLARE_FINAL_TYPE (WpSettingsPlugin, wp_settings_plugin,
50 WP, SETTINGS_PLUGIN, WpPlugin)
51 G_DEFINE_TYPE (WpSettingsPlugin, wp_settings_plugin, WP_TYPE_PLUGIN)
52
53 #define NAME "sm-settings"
54
55 static void
56 wp_settings_plugin_init (WpSettingsPlugin * self)
57 {
58 }
59
60 static void
61 on_persistent_metadata_changed (WpMetadata *m, guint32 subject,
62 const gchar *key, const gchar *type, const gchar *value, gpointer d)
63 {
64 WpSettingsPlugin *self = WP_SETTINGS_PLUGIN (d);
65 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
66
67 /* Update persistent settings with new value. If key is null it means all
68 * settings need to be removed */
69 if (key) {
70 wp_properties_set (self->persistent_settings, key, value);
71 if (value)
72 wp_info_object (self, "persistent setting updated: %s = %s", key, value);
73 else
74 wp_info_object (self, "persistent setting removed: %s", key);
75 } else {
76 g_clear_pointer (&self->persistent_settings, wp_properties_unref);
77 self->persistent_settings = wp_properties_new_empty ();
78 wp_info_object (self, "all persistent settings removed");
79 }
80
81 /* Save changes */
82 wp_state_save_after_timeout (self->state, core, self->persistent_settings);
83
84 /* Also update current settings with new value */
85 if (value)
86 wp_metadata_set (WP_METADATA (self->impl_metadata), 0, key, type, value);
87 }
88
89 WpProperties *
90 load_configuration_settings (WpSettingsPlugin *self)
91 {
92 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
93 g_autoptr (WpConf) conf = NULL;
94 g_autoptr (WpSpaJson) json = NULL;
95 g_autoptr (WpProperties) res = wp_properties_new_empty ();
96
97 g_return_val_if_fail (core, NULL);
98 conf = wp_core_get_conf (core);
99 g_return_val_if_fail (conf, NULL);
100
101 json = wp_conf_get_section (conf, "wireplumber.settings");
102 if (!json)
103 return g_steal_pointer (&res);
104
105 if (!wp_spa_json_is_object (json)) {
106 wp_warning_object (self,
107 "ignoring wireplumber.settings from conf as it isn't a JSON object");
108 return g_steal_pointer (&res);
109 }
110
111 {
112 g_autoptr (WpIterator) iter = wp_spa_json_new_iterator (json);
113 g_auto (GValue) item = G_VALUE_INIT;
114 while (wp_iterator_next (iter, &item)) {
115 WpSpaJson *j = g_value_get_boxed (&item);
116 g_autofree gchar *name = wp_spa_json_parse_string (j);
117 g_autofree gchar *value = NULL;
118
119 g_value_unset (&item);
120 if (!wp_iterator_next (iter, &item)) {
121 wp_warning_object (self, "malformed wireplumber.settings from conf");
122 return res;
123 }
124 j = g_value_get_boxed (&item);
125
126 value = wp_spa_json_to_string (j);
127 g_value_unset (&item);
128
129 if (name && value)
130 wp_properties_set (res, name, value);
131 }
132 }
133
134 return g_steal_pointer (&res);
135 }
136
137 static void
138 on_metadata_activated (WpMetadata * m, GAsyncResult * res,
139 gpointer user_data)
140 {
141 WpTransition *transition = WP_TRANSITION (user_data);
142 WpSettingsPlugin *self = wp_transition_get_source_object (transition);
143 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
144 g_autoptr (WpProperties) config_settings = NULL;
145 g_autoptr (GError) error = NULL;
146 g_autoptr (WpIterator) it = NULL;
147 g_auto (GValue) item = G_VALUE_INIT;
148
149 if (!wp_object_activate_finish (WP_OBJECT (m), res, &error)) {
150 g_prefix_error (&error, "Failed to activate \"%s\": "
151 "Metadata object ", self->metadata_name);
152 wp_transition_return_error (transition, g_steal_pointer (&error));
153 return;
154 }
155
156 /* Load settings from configuration */
157 config_settings = load_configuration_settings (self);
158 if (!config_settings) {
159 wp_transition_return_error (transition, g_error_new (
160 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
161 "failed to parse settings"));
162 return;
163 }
164
165 /* Update the configuration properties with persistent settings */
166 wp_properties_update (config_settings, self->persistent_settings);
167
168 /* Populate settings metadata from schema using values from configuration if
169 * they are present, otherwise use default values */
170 it = wp_metadata_new_iterator (WP_METADATA (self->schema_impl_metadata), 0);
171 for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
172 WpMetadataItem *mi = g_value_get_boxed (&item);
173 const gchar *key = wp_metadata_item_get_key (mi);
174 const gchar *spec_str = wp_metadata_item_get_value (mi);
175 const gchar *value;
176 g_autoptr (WpSpaJson) spec_json = NULL;
177 g_autoptr (WpSpaJson) def_value = NULL;
178
179 /* Use configuration value if found, otherwise use default value */
180 value = wp_properties_get (config_settings, key);
181 if (!value) {
182 spec_json = wp_spa_json_new_from_string (spec_str);
183
184 if (!wp_spa_json_is_object (spec_json)) {
185 wp_warning_object (self,
186 "settings schema spec for %s is not an object: %s", key, spec_str);
187 continue;
188 }
189
190 if (!wp_spa_json_object_get (spec_json, "default", "J", &def_value,
191 NULL)) {
192 wp_warning_object (self,
193 "settings schema spec for %s does not have default value: %s", key,
194 spec_str);
195 continue;
196 }
197
198 value = wp_spa_json_get_data (def_value);
199 }
200
201 /* Add setting in the metadata */
202 wp_debug_object (self, "adding setting to %s metadata: %s = %s",
203 self->metadata_name, key, value);
204 wp_metadata_set (m, 0, key, "Spa:String:JSON", value);
205 }
206
207 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
208 }
209
210 static void
211 on_persistent_metadata_activated (WpMetadata * m, GAsyncResult * res,
212 gpointer user_data)
213 {
214 WpTransition *transition = WP_TRANSITION (user_data);
215 WpSettingsPlugin *self = wp_transition_get_source_object (transition);
216 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
217 g_autoptr (GError) error = NULL;
218 g_autoptr (WpIterator) it = NULL;
219 g_auto (GValue) item = G_VALUE_INIT;
220
221 if (!wp_object_activate_finish (WP_OBJECT (m), res, &error)) {
222 g_prefix_error (&error, "Failed to activate \"%s\": "
223 "Metadata object ", self->metadata_persistent_name);
224 wp_transition_return_error (transition, g_steal_pointer (&error));
225 return;
226 }
227
228 /* Load the persistent settings */
229 self->state = wp_state_new (NAME);
230 self->persistent_settings = wp_state_load (self->state);
231
232 /* Set persistent settings in persistent metadata */
233 for (it = wp_properties_new_iterator (self->persistent_settings);
234 wp_iterator_next (it, &item);
235 g_value_unset (&item)) {
236 WpPropertiesItem *pi = g_value_get_boxed (&item);
237 const gchar *key = wp_properties_item_get_key (pi);
238 const gchar *value = wp_properties_item_get_value (pi);
239
240 wp_debug_object (self, "adding persistent setting to %s metadata: %s = %s",
241 self->metadata_persistent_name, key, value);
242 wp_metadata_set (m, 0, key, "Spa:String:JSON", value);
243 }
244
245 /* monitor changes in persistent metadata */
246 g_signal_connect_object (m, "changed",
247 G_CALLBACK (on_persistent_metadata_changed), self, 0);
248
249 /* create metadata object */
250 self->impl_metadata = wp_impl_metadata_new_full (core, self->metadata_name,
251 NULL);
252 wp_object_activate (WP_OBJECT (self->impl_metadata),
253 WP_OBJECT_FEATURES_ALL,
254 NULL,
255 (GAsyncReadyCallback)on_metadata_activated,
256 transition);
257 }
258
259 static void
260 on_schema_metadata_activated (WpMetadata * m, GAsyncResult * res,
261 gpointer user_data)
262 {
263 WpTransition *transition = WP_TRANSITION (user_data);
264 WpSettingsPlugin *self = wp_transition_get_source_object (transition);
265 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
266 g_autoptr (WpConf) conf = wp_core_get_conf (core);
267 g_autoptr (GError) error = NULL;
268 g_autoptr (WpSpaJson) schema_json = NULL;
269
270 if (!wp_object_activate_finish (WP_OBJECT (m), res, &error)) {
271 wp_transition_return_error (transition, g_error_new (
272 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
273 "Failed to activate metadata object %s", self->metadata_schema_name));
274 return;
275 }
276
277 /* Load the schema into metadata if any */
278 schema_json = wp_conf_get_section (conf, "wireplumber.settings.schema");
279 if (schema_json) {
280 g_autoptr (WpIterator) it = NULL;
281 g_auto (GValue) item = G_VALUE_INIT;
282
283 if (!wp_spa_json_is_object (schema_json)) {
284 wp_transition_return_error (transition, g_error_new (
285 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
286 "Settings schema is not a JSON object: %s",
287 wp_spa_json_get_data (schema_json)));
288 return;
289 }
290
291 it = wp_spa_json_new_iterator (schema_json);
292 while (wp_iterator_next (it, &item)) {
293 WpSpaJson *j = g_value_get_boxed (&item);
294 g_autofree gchar *key = wp_spa_json_parse_string (j);
295 g_autofree gchar *value = NULL;
296
297 g_value_unset (&item);
298 if (!wp_iterator_next (it, &item)) {
299 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
300 WP_LIBRARY_ERROR_INVARIANT, "Malformed settings schema"));
301 return;
302 }
303 j = g_value_get_boxed (&item);
304 value = wp_spa_json_to_string (j);
305 g_value_unset (&item);
306
307 wp_debug_object (self, "adding schema setting to %s metadata: %s = %s",
308 self->metadata_schema_name, key, value);
309 wp_metadata_set (m, 0, key, "Spa:String:JSON", value);
310 }
311 } else {
312 wp_warning_object (self, "settings schema not found in configuration");
313 }
314
315 /* create persistent metadata object */
316 self->persistent_impl_metadata = wp_impl_metadata_new_full (core,
317 self->metadata_persistent_name, NULL);
318 wp_object_activate (WP_OBJECT (self->persistent_impl_metadata),
319 WP_OBJECT_FEATURES_ALL,
320 NULL,
321 (GAsyncReadyCallback)on_persistent_metadata_activated,
322 transition);
323 }
324
325 static void
326 wp_settings_plugin_enable (WpPlugin * plugin, WpTransition * transition)
327 {
328 WpSettingsPlugin * self = WP_SETTINGS_PLUGIN (plugin);
329 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (plugin));
330
331 /* create schema metadata object */
332 self->schema_impl_metadata = wp_impl_metadata_new_full (core,
333 self->metadata_schema_name, NULL);
334 wp_object_activate (WP_OBJECT (self->schema_impl_metadata),
335 WP_OBJECT_FEATURES_ALL,
336 NULL,
337 (GAsyncReadyCallback)on_schema_metadata_activated,
338 transition);
339 }
340
341 static void
342 wp_settings_plugin_disable (WpPlugin * plugin)
343 {
344 WpSettingsPlugin * self = WP_SETTINGS_PLUGIN (plugin);
345
346 g_clear_object (&self->impl_metadata);
347 g_clear_object (&self->schema_impl_metadata);
348 g_clear_object (&self->persistent_impl_metadata);
349 g_clear_pointer (&self->persistent_settings, wp_properties_unref);
350 g_clear_object (&self->state);
351
352 g_clear_pointer (&self->metadata_name, g_free);
353 g_clear_pointer (&self->metadata_schema_name, g_free);
354 g_clear_pointer (&self->metadata_persistent_name, g_free);
355 }
356
357 static void
358 wp_settings_plugin_set_property (GObject * object, guint property_id,
359 const GValue * value, GParamSpec * pspec)
360 {
361 WpSettingsPlugin *self = WP_SETTINGS_PLUGIN (object);
362
363 switch (property_id) {
364 case PROP_METADATA_NAME:
365 self->metadata_name = g_value_dup_string (value);
366 self->metadata_schema_name = g_strdup_printf (
367 WP_SETTINGS_SCHEMA_METADATA_NAME_PREFIX "%s", self->metadata_name);
368 self->metadata_persistent_name = g_strdup_printf (
369 WP_SETTINGS_PERSISTENT_METADATA_NAME_PREFIX "%s", self->metadata_name);
370 break;
371 default:
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
373 break;
374 }
375 }
376
377 static void
378 wp_settings_plugin_get_property (GObject * object, guint property_id,
379 GValue * value, GParamSpec * pspec)
380 {
381 WpSettingsPlugin *self = WP_SETTINGS_PLUGIN (object);
382
383 switch (property_id) {
384 case PROP_METADATA_NAME:
385 g_value_set_string (value, self->metadata_name);
386 break;
387 default:
388 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
389 break;
390 }
391 }
392
393 static void
394 wp_settings_plugin_class_init (WpSettingsPluginClass * klass)
395 {
396 WpPluginClass *plugin_class = (WpPluginClass *) klass;
397 GObjectClass *object_class = (GObjectClass *) klass;
398
399 plugin_class->enable = wp_settings_plugin_enable;
400 plugin_class->disable = wp_settings_plugin_disable;
401
402 object_class->set_property = wp_settings_plugin_set_property;
403 object_class->get_property = wp_settings_plugin_get_property;
404
405 g_object_class_install_property (object_class, PROP_METADATA_NAME,
406 g_param_spec_string ("metadata-name", "metadata-name",
407 "The metadata object to look after", NULL,
408 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
409 }
410
411 WP_PLUGIN_EXPORT GObject *
412 wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error)
413 {
414 g_autofree gchar *metadata_name = NULL;
415 if (args)
416 wp_spa_json_object_get (args, "metadata.name", "s", &metadata_name, NULL);
417
418 return G_OBJECT (g_object_new (wp_settings_plugin_get_type (),
419 "name", "settings",
420 "core", core,
421 "metadata-name", metadata_name ? metadata_name : "sm-settings",
422 NULL));
423 }