#endif
};
-#define ENTRY_VERSION 2
+#define ENTRY_VERSION 3
struct entry {
uint8_t version;
- pa_bool_t muted_valid:1, volume_valid:1, device_valid:1;
+ pa_bool_t muted_valid:1, volume_valid:1, device_valid:1, card_valid:1;
pa_bool_t muted:1;
pa_channel_map channel_map;
pa_cvolume volume;
char device[PA_NAME_MAX];
+ char card[PA_NAME_MAX];
} PA_GCC_PACKED;
enum {
static void handle_entry_get_index(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_entry_get_name(DBusConnection *conn, DBusMessage *msg, void *userdata);
static void handle_entry_get_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
-static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
static void handle_entry_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
-static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
static void handle_entry_get_is_muted(DBusConnection *conn, DBusMessage *msg, void *userdata);
-static void handle_entry_set_is_muted(DBusConnection *conn, DBusMessage *msg, void *userdata);
+static void handle_entry_set_is_muted(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata);
static void handle_entry_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata);
/* Reads an array [(UInt32, UInt32)] from the iterator. The struct items are
* are a channel position and a volume value, respectively. The result is
- * stored in the map and vol arguments. If the volume can't be read from the
- * iterator, an error reply is sent and a negative number is returned. In case
- * of a failure we make no guarantees about the state of map and vol. In case
- * of an empty array the channels field of both map and vol are set to 0. */
+ * stored in the map and vol arguments. The iterator must point to a "a(uu)"
+ * element. If the data is invalid, an error reply is sent and a negative
+ * number is returned. In case of a failure we make no guarantees about the
+ * state of map and vol. In case of an empty array the channels field of both
+ * map and vol are set to 0. This function calls dbus_message_iter_next(iter)
+ * before returning. */
static int get_volume_arg(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, pa_channel_map *map, pa_cvolume *vol) {
DBusMessageIter array_iter;
DBusMessageIter struct_iter;
- int arg_type;
pa_assert(conn);
pa_assert(msg);
pa_assert(iter);
+ pa_assert(pa_streq(dbus_message_iter_get_signature(iter), "a(uu)"));
pa_assert(map);
pa_assert(vol);
map->channels = 0;
vol->channels = 0;
- arg_type = dbus_message_iter_get_arg_type(iter);
- if (arg_type != DBUS_TYPE_ARRAY) {
- if (arg_type == DBUS_TYPE_INVALID)
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Too few arguments. An array was expected.");
- else
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Wrong argument type: '%c'. An array was expected.", (char) arg_type);
- return -1;
- }
-
- arg_type = dbus_message_iter_get_element_type(iter);
- if (arg_type != DBUS_TYPE_STRUCT) {
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Wrong array element type: '%c'. A struct was expected.", (char) arg_type);
- return -1;
- }
-
dbus_message_iter_recurse(iter, &array_iter);
while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) {
dbus_message_iter_recurse(&array_iter, &struct_iter);
- arg_type = dbus_message_iter_get_arg_type(&struct_iter);
- if (arg_type != DBUS_TYPE_UINT32) {
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Wrong channel position type: '%c'. An unsigned 32-bit integer was expected.", (char) arg_type);
- return -1;
- }
-
dbus_message_iter_get_basic(&struct_iter, &chan_pos);
- dbus_message_iter_next(&struct_iter);
if (chan_pos >= PA_CHANNEL_POSITION_MAX) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid channel position: %u", chan_pos);
return -1;
}
- arg_type = dbus_message_iter_get_arg_type(&struct_iter);
- if (arg_type != DBUS_TYPE_UINT32) {
- if (arg_type == DBUS_TYPE_INVALID)
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Channel volume missing.");
- else
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Wrong volume value type: '%c'. An unsigned 32-bit integer was expected.", (char) arg_type);
- return -1;
- }
-
+ pa_assert_se(dbus_message_iter_next(&struct_iter));
dbus_message_iter_get_basic(&struct_iter, &chan_vol);
if (chan_vol > PA_VOLUME_MAX) {
static void handle_add_entry(DBusConnection *conn, DBusMessage *msg, void *userdata) {
struct userdata *u = userdata;
DBusMessageIter msg_iter;
- const char *name;
- const char *device;
+ const char *name = NULL;
+ const char *device = NULL;
pa_channel_map map;
pa_cvolume vol;
- dbus_bool_t muted;
- dbus_bool_t apply_immediately;
+ dbus_bool_t muted = FALSE;
+ dbus_bool_t apply_immediately = FALSE;
pa_datum key;
pa_datum value;
- struct dbus_entry *dbus_entry;
- struct entry *e;
+ struct dbus_entry *dbus_entry = NULL;
+ struct entry *e = NULL;
pa_assert(conn);
pa_assert(msg);
pa_assert(u);
- if (!dbus_message_iter_init(msg, &msg_iter)) {
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Too few arguments.");
- return;
- }
-
- if (pa_dbus_get_basic_arg(conn, msg, &msg_iter, DBUS_TYPE_STRING, &name) < 0)
- return;
+ pa_assert_se(dbus_message_iter_init(msg, &msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &name);
- if (pa_dbus_get_basic_arg(conn, msg, &msg_iter, DBUS_TYPE_STRING, &device) < 0)
- return;
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &device);
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
if (get_volume_arg(conn, msg, &msg_iter, &map, &vol) < 0)
return;
- if (pa_dbus_get_basic_arg(conn, msg, &msg_iter, DBUS_TYPE_BOOLEAN, &muted) < 0)
- return;
+ dbus_message_iter_get_basic(&msg_iter, &muted);
- if (pa_dbus_get_basic_arg(conn, msg, &msg_iter, DBUS_TYPE_BOOLEAN, &apply_immediately) < 0)
- return;
+ pa_assert_se(dbus_message_iter_next(&msg_iter));
+ dbus_message_iter_get_basic(&msg_iter, &apply_immediately);
if (!*name) {
pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "An empty string was given as the entry name.");
struct userdata *u = userdata;
const char *name;
struct dbus_entry *de;
- DBusError error;
pa_assert(conn);
pa_assert(msg);
pa_assert(u);
- dbus_error_init(&error);
-
- if (!dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
- pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "%s", error.message);
- dbus_error_free(&error);
- return;
- }
+ pa_assert_se(dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
if (!(de = pa_hashmap_get(u->dbus_entries, name))) {
pa_dbus_send_error(conn, msg, PA_DBUS_ERROR_NOT_FOUND, "No such stream restore entry.");
pa_xfree(e);
}
-static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
struct dbus_entry *de = userdata;
const char *device;
struct entry *e;
pa_assert(conn);
pa_assert(msg);
+ pa_assert(iter);
pa_assert(de);
- if (pa_dbus_get_basic_set_property_arg(conn, msg, DBUS_TYPE_STRING, &device) < 0)
- return;
+ dbus_message_iter_get_basic(iter, &device);
pa_assert_se(e = read_entry(de->userdata, de->entry_name));
pa_xfree(e);
}
-static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
struct dbus_entry *de = userdata;
- DBusMessageIter msg_iter;
pa_channel_map map;
pa_cvolume vol;
- struct entry *e;
- pa_bool_t updated;
+ struct entry *e = NULL;
+ pa_bool_t updated = FALSE;
pa_assert(conn);
pa_assert(msg);
+ pa_assert(iter);
pa_assert(de);
- /* 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(conn, msg, DBUS_ERROR_INVALID_ARGS, "Too few arguments.");
- return;
- }
-
- if (get_volume_arg(conn, msg, &msg_iter, &map, &vol) < 0)
+ if (get_volume_arg(conn, msg, iter, &map, &vol) < 0)
return;
pa_assert_se(e = read_entry(de->userdata, de->entry_name));
pa_xfree(e);
}
-static void handle_entry_set_is_muted(DBusConnection *conn, DBusMessage *msg, void *userdata) {
+static void handle_entry_set_is_muted(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
struct dbus_entry *de = userdata;
pa_bool_t muted;
struct entry *e;
pa_assert(conn);
pa_assert(msg);
+ pa_assert(iter);
pa_assert(de);
- if (pa_dbus_get_basic_set_property_arg(conn, msg, DBUS_TYPE_BOOLEAN, &muted) < 0)
- return;
+ dbus_message_iter_get_basic(iter, &muted);
pa_assert_se(e = read_entry(de->userdata, de->entry_name));
goto fail;
}
+ if (!memchr(e->card, 0, sizeof(e->card))) {
+ pa_log_warn("Database contains entry for stream %s with missing NUL byte in card name", name);
+ goto fail;
+ }
+
if (e->device_valid && !pa_namereg_is_valid_name(e->device)) {
pa_log_warn("Invalid device name stored in database for stream %s", name);
goto fail;
}
+ if (e->card_valid && !pa_namereg_is_valid_name(e->card)) {
+ pa_log_warn("Invalid card name stored in database for stream %s", name);
+ goto fail;
+ }
+
if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
pa_log_warn("Invalid channel map stored in database for stream %s", name);
goto fail;
(a->device_valid && strncmp(a->device, b->device, sizeof(a->device))))
return FALSE;
+ if (a->card_valid != b->card_valid ||
+ (a->card_valid && strncmp(a->card, b->card, sizeof(a->card))))
+ return FALSE;
+
if (a->muted_valid != b->muted_valid ||
(a->muted_valid && (a->muted != b->muted)))
return FALSE;
entry.device_valid = TRUE;
device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry.device, old->device));
+ if (sink_input->sink->card) {
+ pa_strlcpy(entry.card, sink_input->sink->card->name, sizeof(entry.card));
+ entry.card_valid = TRUE;
+ }
}
} else {
entry.device_valid = source_output->save_source;
device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry.device, old->device));
+
+ if (source_output->source->card) {
+ pa_strlcpy(entry.card, source_output->source->card->name, sizeof(entry.card));
+ entry.card_valid = TRUE;
+ }
}
}
if (!(name = get_name(new_data->proplist, "sink-input")))
return PA_HOOK_OK;
- if ((e = read_entry(u, name))) {
+ if (new_data->sink)
+ pa_log_debug("Not restoring device for stream %s, because already set.", name);
+ else if ((e = read_entry(u, name))) {
+ pa_sink *s = NULL;
- if (e->device_valid) {
- pa_sink *s;
+ if (e->device_valid)
+ s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK);
- if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK))) {
- if (!new_data->sink) {
- pa_log_info("Restoring device for stream %s.", name);
- new_data->sink = s;
- new_data->save_sink = TRUE;
- } else
- pa_log_debug("Not restoring device for stream %s, because already set.", name);
- }
+ if (!s && e->card_valid) {
+ pa_card *card;
+
+ if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
+ s = pa_idxset_first(card->sinks, NULL);
+ }
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (s && PA_SINK_IS_LINKED(pa_sink_get_state(s))) {
+ pa_log_info("Restoring device for stream %s.", name);
+ new_data->sink = s;
+ new_data->save_sink = TRUE;
}
pa_xfree(e);
if (!(name = get_name(new_data->proplist, "source-output")))
return PA_HOOK_OK;
- if ((e = read_entry(u, name))) {
- pa_source *s;
+ if (new_data->source)
+ pa_log_debug("Not restoring device for stream %s, because already set", name);
+ else if ((e = read_entry(u, name))) {
+ pa_source *s = NULL;
- if (e->device_valid) {
- if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE))) {
- if (!new_data->source) {
- pa_log_info("Restoring device for stream %s.", name);
- new_data->source = s;
- new_data->save_source = TRUE;
- } else
- pa_log_debug("Not restoring device for stream %s, because already set", name);
- }
+ if (e->device_valid)
+ s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE);
+
+ if (!s && e->card_valid) {
+ pa_card *card;
+
+ if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
+ s = pa_idxset_first(card->sources, NULL);
+ }
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (s && PA_SOURCE_IS_LINKED(pa_source_get_state(s))) {
+ pa_log_info("Restoring device for stream %s.", name);
+ new_data->source = s;
+ new_data->save_source = TRUE;
}
pa_xfree(e);
if (si->save_sink)
continue;
+ /* Skip this if it is already in the process of being moved
+ * anyway */
+ if (!si->sink)
+ continue;
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+ continue;
+
if (!(name = get_name(si->proplist, "sink-input")))
continue;
if (so->direct_on_input)
continue;
+ /* Skip this if it is already in the process of being moved anyway */
+ if (!so->source)
+ continue;
+
+ /* It might happen that a stream and a sink are set up at the
+ same time, in which case we want to make sure we don't
+ interfere with that */
+ if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+ continue;
+
if (!(name = get_name(so->proplist, "source-input")))
continue;
char *name;
struct entry *e;
+ if (!si->sink)
+ continue;
+
if (!(name = get_name(si->proplist, "sink-input")))
continue;
if (e->device_valid) {
pa_sink *d;
- if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) && d != sink)
+ if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) &&
+ d != sink &&
+ PA_SINK_IS_LINKED(pa_sink_get_state(d)))
pa_sink_input_move_to(si, d, TRUE);
}
char *name;
struct entry *e;
+ if (so->direct_on_input)
+ continue;
+
+ if (!so->source)
+ continue;
+
if (!(name = get_name(so->proplist, "source-output")))
continue;
if (e->device_valid) {
pa_source *d;
- if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) && d != source)
+ if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) &&
+ d != source &&
+ PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
pa_source_output_move_to(so, d, TRUE);
}
pa_assert(name);
pa_assert(e);
- for (si = pa_idxset_first(u->core->sink_inputs, &idx); si; si = pa_idxset_next(u->core->sink_inputs, &idx)) {
+ PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) {
char *n;
pa_sink *s;
v = e->volume;
pa_log_info("Restoring volume for sink input %s.", name);
- pa_sink_input_set_volume(si, pa_cvolume_remap(&v, &e->channel_map, &si->channel_map), FALSE, FALSE);
+ pa_cvolume_remap(&v, &e->channel_map, &si->channel_map);
+ pa_sink_input_set_volume(si, &v, TRUE, FALSE);
}
if (u->restore_muted && e->muted_valid) {
pa_log_info("Restoring mute state for sink input %s.", name);
- pa_sink_input_set_mute(si, e->muted, FALSE);
+ pa_sink_input_set_mute(si, e->muted, TRUE);
}
if (u->restore_device &&
(s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SINK))) {
pa_log_info("Restoring device for stream %s.", name);
- pa_sink_input_move_to(si, s, FALSE);
+ pa_sink_input_move_to(si, s, TRUE);
}
}
- for (so = pa_idxset_first(u->core->source_outputs, &idx); so; so = pa_idxset_next(u->core->source_outputs, &idx)) {
+ PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) {
char *n;
pa_source *s;
(s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SOURCE))) {
pa_log_info("Restoring device for stream %s.", name);
- pa_source_output_move_to(so, s, FALSE);
+ pa_source_output_move_to(so, s, TRUE);
}
}
}
data.data = &entry;
data.size = sizeof(entry);
+ pa_log_debug("Client %s changes entry %s.",
+ pa_strnull(pa_proplist_gets(pa_native_connection_get_client(c)->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)),
+ name);
+
if (pa_database_set(u->database, &key, &data, mode == PA_UPDATE_REPLACE) == 0) {
#ifdef HAVE_DBUS
struct dbus_entry *de;
send_device_updated_signal(de, &entry);
if ((old->volume_valid != entry.volume_valid)
- || (entry.volume_valid
- && (!pa_cvolume_equal(&entry.volume, &old->volume) || !pa_channel_map_equal(&entry.channel_map, &old->channel_map))))
+ || (entry.volume_valid && (!pa_cvolume_equal(&entry.volume, &old->volume)
+ || !pa_channel_map_equal(&entry.channel_map, &old->channel_map))))
send_volume_updated_signal(de, &entry);
if (!old->muted_valid || (entry.muted != old->muted))