summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor V. Kovalenko <igor.v.kovalenko@gmail.com>2021-10-18 20:20:15 +0300
committerIgor V. Kovalenko <igor.v.kovalenko@gmail.com>2021-10-19 09:45:27 +0300
commit8262815e94aa6716c20ba9a3f2737e5dd5e4adad (patch)
treef23cf4850bc4df7aec160f318a36b15b8b158b76
parent5a03d84c3226428acd2e71338003cb88aaa52316 (diff)
message-params: If message paremeters is not JSON, wrap into JSON string
Requiring user to invoke send-message with correctly quoted parameters string is not good for usability. Wrap parameters string into JSON string and try again. If that works, log a warning use wrapped JSON string with parameters. As an example these two commands will now invoke the same action: pactl send-message /card/bluez_card... switch-codec '"CODECNAME"' pactl send-message /card/bluez_card... switch-codec CODECNAME Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/648>
-rw-r--r--src/pulsecore/message-handler.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/pulsecore/message-handler.c b/src/pulsecore/message-handler.c
index 89c0b5110..3d2d71609 100644
--- a/src/pulsecore/message-handler.c
+++ b/src/pulsecore/message-handler.c
@@ -131,8 +131,26 @@ int pa_message_handler_send_message(pa_core *c, const char *object_path, const c
if (message_parameters) {
parameters = pa_json_parse(message_parameters);
- if (!parameters)
- return -PA_ERR_INVALID;
+ if (!parameters) {
+ char *wrapped_message_parameters;
+
+ /* Message parameters is not a valid JSON
+ *
+ * Wrap message parameters into JSON string and try again.
+ * User might have missed double-quotes and passed ARGSTRING instead of proper JSON "ARGSTRING"
+ */
+ pa_log_warn(_("Message parameters is not a valid JSON, wrapping into JSON string '\"%s\"'"), message_parameters);
+
+ wrapped_message_parameters = pa_sprintf_malloc("\"%s\"", message_parameters);
+ parameters = pa_json_parse(wrapped_message_parameters);
+ pa_xfree(wrapped_message_parameters);
+
+ if (!parameters) {
+ pa_log_error(_("Message parameters is not a valid JSON object. Tried both '%s' and '\"%s\"'"),
+ message_parameters, message_parameters);
+ return -PA_ERR_INVALID;
+ }
+ }
}
/* The handler is expected to return an error code and may also