mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-06 13:29:56 -05:00
Finish the Core dbus interface.
This commit is contained in:
parent
5c7952e4fa
commit
9347e90fed
14 changed files with 1526 additions and 1861 deletions
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
#include <pulse/rtclock.h>
|
||||
#include <pulse/timeval.h>
|
||||
#include <pulse/utf8.h>
|
||||
#include <pulse/xmalloc.h>
|
||||
|
||||
#include <pulsecore/core-rtclock.h>
|
||||
|
|
@ -444,3 +445,335 @@ void pa_dbus_free_pending_list(pa_dbus_pending **p) {
|
|||
pa_dbus_pending_free(i);
|
||||
}
|
||||
}
|
||||
|
||||
void pa_dbus_send_error(DBusConnection *c, DBusMessage *in_reply_to, const char *name, const char *message) {
|
||||
DBusMessage *reply = NULL;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(in_reply_to);
|
||||
pa_assert(name);
|
||||
pa_assert(message);
|
||||
|
||||
pa_assert_se((reply = dbus_message_new_error(in_reply_to, name, message)));
|
||||
pa_assert_se(dbus_connection_send(c, reply, NULL));
|
||||
|
||||
dbus_message_unref(reply);
|
||||
}
|
||||
|
||||
void pa_dbus_send_empty_reply(DBusConnection *c, DBusMessage *in_reply_to) {
|
||||
DBusMessage *reply = NULL;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(in_reply_to);
|
||||
|
||||
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
|
||||
pa_assert_se(dbus_connection_send(c, reply, NULL));
|
||||
dbus_message_unref(reply);
|
||||
}
|
||||
|
||||
void pa_dbus_send_basic_value_reply(DBusConnection *c, DBusMessage *in_reply_to, int type, void *data) {
|
||||
DBusMessage *reply = NULL;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(in_reply_to);
|
||||
pa_assert(dbus_type_is_basic(type));
|
||||
pa_assert(data);
|
||||
|
||||
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
|
||||
pa_assert_se(dbus_message_append_args(reply, type, data, DBUS_TYPE_INVALID));
|
||||
pa_assert_se(dbus_connection_send(c, reply, NULL));
|
||||
dbus_message_unref(reply);
|
||||
}
|
||||
|
||||
static const char *signature_from_basic_type(int type) {
|
||||
switch (type) {
|
||||
case DBUS_TYPE_BOOLEAN: return DBUS_TYPE_BOOLEAN_AS_STRING;
|
||||
case DBUS_TYPE_BYTE: return DBUS_TYPE_BYTE_AS_STRING;
|
||||
case DBUS_TYPE_INT16: return DBUS_TYPE_INT16_AS_STRING;
|
||||
case DBUS_TYPE_UINT16: return DBUS_TYPE_UINT16_AS_STRING;
|
||||
case DBUS_TYPE_INT32: return DBUS_TYPE_INT32_AS_STRING;
|
||||
case DBUS_TYPE_UINT32: return DBUS_TYPE_UINT32_AS_STRING;
|
||||
case DBUS_TYPE_INT64: return DBUS_TYPE_INT64_AS_STRING;
|
||||
case DBUS_TYPE_UINT64: return DBUS_TYPE_UINT64_AS_STRING;
|
||||
case DBUS_TYPE_DOUBLE: return DBUS_TYPE_DOUBLE_AS_STRING;
|
||||
case DBUS_TYPE_STRING: return DBUS_TYPE_STRING_AS_STRING;
|
||||
case DBUS_TYPE_OBJECT_PATH: return DBUS_TYPE_OBJECT_PATH_AS_STRING;
|
||||
case DBUS_TYPE_SIGNATURE: return DBUS_TYPE_SIGNATURE_AS_STRING;
|
||||
default: pa_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void pa_dbus_send_basic_variant_reply(DBusConnection *c, DBusMessage *in_reply_to, int type, void *data) {
|
||||
DBusMessage *reply = NULL;
|
||||
DBusMessageIter msg_iter;
|
||||
DBusMessageIter variant_iter;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(in_reply_to);
|
||||
pa_assert(dbus_type_is_basic(type));
|
||||
pa_assert(data);
|
||||
|
||||
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
|
||||
dbus_message_iter_init_append(reply, &msg_iter);
|
||||
pa_assert_se(dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_VARIANT, signature_from_basic_type(type), &variant_iter));
|
||||
pa_assert_se(dbus_message_iter_append_basic(&variant_iter, type, data));
|
||||
pa_assert_se(dbus_message_iter_close_container(&msg_iter, &variant_iter));
|
||||
pa_assert_se(dbus_connection_send(c, reply, NULL));
|
||||
dbus_message_unref(reply);
|
||||
}
|
||||
|
||||
/* Note: returns sizeof(char*) for strings, object paths and signatures. */
|
||||
static unsigned basic_type_size(int type) {
|
||||
switch (type) {
|
||||
case DBUS_TYPE_BOOLEAN: return sizeof(dbus_bool_t);
|
||||
case DBUS_TYPE_BYTE: return 1;
|
||||
case DBUS_TYPE_INT16: return sizeof(dbus_int16_t);
|
||||
case DBUS_TYPE_UINT16: return sizeof(dbus_uint16_t);
|
||||
case DBUS_TYPE_INT32: return sizeof(dbus_int32_t);
|
||||
case DBUS_TYPE_UINT32: return sizeof(dbus_uint32_t);
|
||||
case DBUS_TYPE_INT64: return sizeof(dbus_int64_t);
|
||||
case DBUS_TYPE_UINT64: return sizeof(dbus_uint64_t);
|
||||
case DBUS_TYPE_DOUBLE: return sizeof(double);
|
||||
case DBUS_TYPE_STRING:
|
||||
case DBUS_TYPE_OBJECT_PATH:
|
||||
case DBUS_TYPE_SIGNATURE: return sizeof(char*);
|
||||
default: pa_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void pa_dbus_send_basic_array_variant_reply(DBusConnection *c, DBusMessage *in_reply_to, int item_type, void *array, unsigned n) {
|
||||
DBusMessage *reply = NULL;
|
||||
DBusMessageIter msg_iter;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(in_reply_to);
|
||||
pa_assert(dbus_type_is_basic(item_type));
|
||||
pa_assert(array || n == 0);
|
||||
|
||||
pa_assert_se((reply = dbus_message_new_method_return(in_reply_to)));
|
||||
dbus_message_iter_init_append(reply, &msg_iter);
|
||||
pa_dbus_append_basic_array_variant(&msg_iter, item_type, array, n);
|
||||
pa_assert_se(dbus_connection_send(c, reply, NULL));
|
||||
dbus_message_unref(reply);
|
||||
}
|
||||
|
||||
void pa_dbus_append_basic_array(DBusMessageIter *iter, int item_type, const void *array, unsigned n) {
|
||||
DBusMessageIter array_iter;
|
||||
unsigned i;
|
||||
unsigned item_size;
|
||||
|
||||
pa_assert(iter);
|
||||
pa_assert(dbus_type_is_basic(item_type));
|
||||
pa_assert(array || n == 0);
|
||||
|
||||
item_size = basic_type_size(item_type);
|
||||
|
||||
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, signature_from_basic_type(item_type), &array_iter));
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
pa_assert_se(dbus_message_iter_append_basic(&array_iter, item_type, &((uint8_t*) array)[i * item_size]));
|
||||
|
||||
pa_assert_se(dbus_message_iter_close_container(iter, &array_iter));
|
||||
};
|
||||
|
||||
void pa_dbus_append_basic_variant(DBusMessageIter *iter, int type, void *data) {
|
||||
DBusMessageIter variant_iter;
|
||||
|
||||
pa_assert(iter);
|
||||
pa_assert(dbus_type_is_basic(type));
|
||||
pa_assert(data);
|
||||
|
||||
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, signature_from_basic_type(type), &variant_iter));
|
||||
pa_assert_se(dbus_message_iter_append_basic(&variant_iter, type, data));
|
||||
pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
|
||||
}
|
||||
|
||||
void pa_dbus_append_basic_array_variant(DBusMessageIter *iter, int item_type, const void *array, unsigned n) {
|
||||
DBusMessageIter variant_iter;
|
||||
char *array_signature;
|
||||
|
||||
pa_assert(iter);
|
||||
pa_assert(dbus_type_is_basic(item_type));
|
||||
pa_assert(array || n == 0);
|
||||
|
||||
array_signature = pa_sprintf_malloc("a%c", *signature_from_basic_type(item_type));
|
||||
|
||||
pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, array_signature, &variant_iter));
|
||||
pa_dbus_append_basic_array(&variant_iter, item_type, array, n);
|
||||
pa_assert_se(dbus_message_iter_close_container(iter, &variant_iter));
|
||||
|
||||
pa_xfree(array_signature);
|
||||
}
|
||||
|
||||
void pa_dbus_append_basic_variant_dict_entry(DBusMessageIter *dict_iter, const char *key, int type, void *data) {
|
||||
DBusMessageIter dict_entry_iter;
|
||||
|
||||
pa_assert(dict_iter);
|
||||
pa_assert(key);
|
||||
pa_assert(dbus_type_is_basic(type));
|
||||
pa_assert(data);
|
||||
|
||||
pa_assert_se(dbus_message_iter_open_container(dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
|
||||
pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
|
||||
pa_dbus_append_basic_variant(&dict_entry_iter, type, data);
|
||||
pa_assert_se(dbus_message_iter_close_container(dict_iter, &dict_entry_iter));
|
||||
}
|
||||
|
||||
void pa_dbus_append_basic_array_variant_dict_entry(DBusMessageIter *dict_iter, const char *key, int item_type, const void *array, unsigned n) {
|
||||
DBusMessageIter dict_entry_iter;
|
||||
|
||||
pa_assert(dict_iter);
|
||||
pa_assert(key);
|
||||
pa_assert(dbus_type_is_basic(item_type));
|
||||
pa_assert(array || n == 0);
|
||||
|
||||
pa_assert_se(dbus_message_iter_open_container(dict_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry_iter));
|
||||
pa_assert_se(dbus_message_iter_append_basic(&dict_entry_iter, DBUS_TYPE_STRING, &key));
|
||||
pa_dbus_append_basic_array_variant(&dict_entry_iter, item_type, array, n);
|
||||
pa_assert_se(dbus_message_iter_close_container(dict_iter, &dict_entry_iter));
|
||||
}
|
||||
|
||||
int pa_dbus_get_basic_set_property_arg(DBusConnection *c, DBusMessage *msg, int type, void *data) {
|
||||
DBusMessageIter msg_iter;
|
||||
DBusMessageIter variant_iter;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(msg);
|
||||
pa_assert(dbus_type_is_basic(type));
|
||||
pa_assert(data);
|
||||
|
||||
/* Skip the interface and property name arguments. */
|
||||
if (!dbus_message_iter_init(msg, &msg_iter) || !dbus_message_iter_next(&msg_iter) || !dbus_message_iter_next(&msg_iter)) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Too few arguments.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&msg_iter) != DBUS_TYPE_VARIANT) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Message argument isn't a variant.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dbus_message_iter_recurse(&msg_iter, &variant_iter);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Variant has wrong contained type.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_basic(&variant_iter, data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pa_dbus_get_basic_arg(DBusConnection *c, DBusMessage *msg, DBusMessageIter *iter, int type, void *data) {
|
||||
pa_assert(c);
|
||||
pa_assert(msg);
|
||||
pa_assert(iter);
|
||||
pa_assert(dbus_type_is_basic(type));
|
||||
pa_assert(data);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(iter) != type) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong argument type or too few arguments.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_basic(iter, data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pa_dbus_get_fixed_array_arg(DBusConnection *c, DBusMessage *msg, DBusMessageIter *iter, int item_type, void *array, unsigned *n) {
|
||||
DBusMessageIter array_iter;
|
||||
int signed_n;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(msg);
|
||||
pa_assert(iter);
|
||||
pa_assert(dbus_type_is_fixed(item_type));
|
||||
pa_assert(array);
|
||||
pa_assert(n);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong argument type or too few arguments. An array was expected.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dbus_message_iter_get_element_type(iter) != item_type) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong array element type.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dbus_message_iter_recurse(iter, &array_iter);
|
||||
|
||||
dbus_message_iter_get_fixed_array(&array_iter, array, &signed_n);
|
||||
|
||||
pa_assert(signed_n >= 0);
|
||||
|
||||
*n = signed_n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
pa_proplist *pa_dbus_get_proplist_arg(DBusConnection *c, DBusMessage *msg, DBusMessageIter *iter) {
|
||||
DBusMessageIter dict_iter;
|
||||
pa_proplist *proplist = NULL;
|
||||
const char *key;
|
||||
const uint8_t *value;
|
||||
int value_length;
|
||||
|
||||
pa_assert(c);
|
||||
pa_assert(msg);
|
||||
pa_assert(iter);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong argument type or too few arguments. An array was expected.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (dbus_message_iter_get_element_type(iter) != DBUS_TYPE_DICT_ENTRY) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong array element type. A dict entry was expected.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
proplist = pa_proplist_new();
|
||||
|
||||
dbus_message_iter_recurse(iter, &dict_iter);
|
||||
|
||||
while (dbus_message_iter_has_next(&dict_iter)) {
|
||||
if (dbus_message_iter_get_arg_type(&dict_iter) != DBUS_TYPE_STRING) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong dict key type. A string was expected.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_basic(&dict_iter, &key);
|
||||
|
||||
if (strlen(key) <= 0 || !pa_ascii_valid(key)) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Invalid property list key.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&dict_iter) != DBUS_TYPE_ARRAY) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong dict value type. An array was expected.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (dbus_message_iter_get_element_type(&dict_iter) != DBUS_TYPE_BYTE) {
|
||||
pa_dbus_send_error(c, msg, DBUS_ERROR_INVALID_ARGS, "Wrong dict value item type. A byte was expected.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_fixed_array(&dict_iter, &value, &value_length);
|
||||
|
||||
pa_assert(value_length >= 0);
|
||||
|
||||
pa_assert_se(pa_proplist_set(proplist, key, value, value_length) >= 0);
|
||||
}
|
||||
|
||||
return proplist;
|
||||
|
||||
fail:
|
||||
if (proplist)
|
||||
pa_proplist_free(proplist);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue