#include <pulsecore/card.h>
#include <pulsecore/namereg.h>
#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
#include "module-card-restore-symdef.h"
pa_database *database;
};
-#define ENTRY_VERSION 1
+#define ENTRY_VERSION 2
struct entry {
uint8_t version;
- char profile[PA_NAME_MAX];
-} PA_GCC_PACKED ;
+ char *profile;
+};
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
struct userdata *u = userdata;
pa_log_info("Synced.");
}
-static struct entry* read_entry(struct userdata *u, const char *name) {
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->profile);
+ pa_xfree(e);
+}
+
+static struct entry* entry_read(struct userdata *u, const char *name) {
pa_datum key, data;
- struct entry *e;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char* profile;
pa_assert(u);
pa_assert(name);
if (!pa_database_get(u->database, &key, &data))
goto fail;
- if (data.size != sizeof(struct entry)) {
- pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
- goto fail;
- }
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
- e = (struct entry*) data.data;
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_gets(t, &profile) < 0) {
- if (e->version != ENTRY_VERSION) {
- pa_log_debug("Version of database entry for card %s doesn't match our version. Probably due to upgrade, ignoring.", name);
goto fail;
}
- if (!memchr(e->profile, 0, sizeof(e->profile))) {
- pa_log_warn("Database contains entry for card %s with missing NUL byte in profile name", name);
+ e->profile = pa_xstrdup(profile);
+
+ if (!pa_tagstruct_eof(t))
goto fail;
- }
+
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
return e;
fail:
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
pa_datum_free(&data);
return NULL;
}
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_puts(t, e->profile);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
static void trigger_save(struct userdata *u) {
if (u->save_time_event)
return;
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
struct userdata *u = userdata;
- struct entry entry, *old;
- pa_datum key, data;
+ struct entry *entry, *old;
pa_card *card;
pa_assert(c);
t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
- pa_zero(entry);
- entry.version = ENTRY_VERSION;
+ entry = entry_new();
if (!(card = pa_idxset_get_by_index(c->cards, idx)))
return;
if (!card->save_profile)
return;
- pa_strlcpy(entry.profile, card->active_profile ? card->active_profile->name : "", sizeof(entry.profile));
+ entry->profile = pa_xstrdup(card->active_profile ? card->active_profile->name : "");
- if ((old = read_entry(u, card->name))) {
+ if ((old = entry_read(u, card->name))) {
- if (strncmp(old->profile, entry.profile, sizeof(entry.profile)) == 0) {
- pa_xfree(old);
+ if (pa_streq(old->profile, entry->profile)) {
+ entry_free(old);
+ entry_free(entry);
return;
}
- pa_xfree(old);
+ entry_free(old);
}
- key.data = card->name;
- key.size = strlen(card->name);
-
- data.data = &entry;
- data.size = sizeof(entry);
-
pa_log_info("Storing profile for card %s.", card->name);
- pa_database_set(u->database, &key, &data, TRUE);
+ if (entry_write(u, card->name, entry))
+ trigger_save(u);
- trigger_save(u);
+ entry_free(entry);
}
static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
pa_assert(new_data);
- if ((e = read_entry(u, new_data->name)) && e->profile[0]) {
+ if ((e = entry_read(u, new_data->name)) && e->profile[0]) {
if (!new_data->active_profile) {
pa_log_info("Restoring profile for card %s.", new_data->name);
} else
pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
- pa_xfree(e);
+ entry_free(e);
}
return PA_HOOK_OK;
#include <pulsecore/pstream.h>
#include <pulsecore/pstream-util.h>
#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
#include "module-device-manager-symdef.h"
ROLE_ANIMATION,
ROLE_PRODUCTION,
ROLE_A11Y,
+ ROLE_MAX
};
typedef uint32_t role_indexes_t[NUM_ROLES];
role_indexes_t preferred_sources;
};
-#define ENTRY_VERSION 1
+#define ENTRY_VERSION 2
struct entry {
uint8_t version;
- char description[PA_NAME_MAX];
+ char *description;
pa_bool_t user_set_description;
- char icon[PA_NAME_MAX];
+ char *icon;
role_indexes_t priority;
-} PA_GCC_PACKED;
+};
enum {
SUBCOMMAND_TEST,
};
-static struct entry* read_entry(struct userdata *u, const char *name) {
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->description);
+ pa_xfree(e->icon);
+}
+
+static struct entry* entry_read(struct userdata *u, const char *name) {
pa_datum key, data;
- struct entry *e;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char *description, *icon;
pa_assert(u);
pa_assert(name);
if (!pa_database_get(u->database, &key, &data))
goto fail;
- if (data.size != sizeof(struct entry)) {
- pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
- goto fail;
- }
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
- e = (struct entry*) data.data;
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_gets(t, &description) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->user_set_description) < 0 ||
+ pa_tagstruct_gets(t, &icon) < 0) {
- if (e->version != ENTRY_VERSION) {
- pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name);
goto fail;
}
- if (!memchr(e->description, 0, sizeof(e->description))) {
- pa_log_warn("Database contains entry for device %s with missing NUL byte in description", name);
- goto fail;
+ e->description = pa_xstrdup(description);
+ e->icon = pa_xstrdup(icon);
+
+ for (uint8_t i=0; i<ROLE_MAX; ++i) {
+ if (pa_tagstruct_getu32(t, &e->priority[i]) < 0)
+ goto fail;
}
- if (!memchr(e->icon, 0, sizeof(e->icon))) {
- pa_log_warn("Database contains entry for device %s with missing NUL byte in icon", name);
+ if (!pa_tagstruct_eof(t))
goto fail;
- }
+
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
return e;
fail:
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
pa_datum_free(&data);
return NULL;
}
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_puts(t, e->description);
+ pa_tagstruct_put_boolean(t, e->user_set_description);
+ pa_tagstruct_puts(t, e->icon);
+ for (uint8_t i=0; i<ROLE_MAX; ++i)
+ pa_tagstruct_putu32(t, e->priority[i]);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
#ifdef DUMP_DATABASE
static void dump_database_helper(struct userdata *u, uint32_t role_index, const char* human, pa_bool_t sink_mode) {
pa_assert(u);
name = pa_xstrndup(key.data, key.size);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
pa_log_debug(" Got entry: %s", name);
pa_log_debug(" Description: %s", e->description);
pa_log_debug(" Priorities: None: %3u, Video: %3u, Music: %3u, Game: %3u, Event: %3u",
e->priority[ROLE_NONE], e->priority[ROLE_VIDEO], e->priority[ROLE_MUSIC], e->priority[ROLE_GAME], e->priority[ROLE_EVENT]);
pa_log_debug(" Phone: %3u, Anim: %3u, Prodtn: %3u, A11y: %3u",
e->priority[ROLE_PHONE], e->priority[ROLE_ANIMATION], e->priority[ROLE_PRODUCTION], e->priority[ROLE_A11Y]);
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
pa_assert(a);
pa_assert(b);
- if (strncmp(a->description, b->description, sizeof(a->description))
+ if (!pa_streq(a->description, b->description)
|| a->user_set_description != b->user_set_description
- || strncmp(a->icon, b->icon, sizeof(a->icon)))
+ || !pa_streq(a->icon, b->icon))
return FALSE;
for (int i=0; i < NUM_ROLES; ++i)
pa_assert(name);
pa_assert(prefix);
- if ((old = read_entry(u, name)))
+ if ((old = entry_read(u, name))) {
*entry = *old;
- else {
+ entry->description = pa_xstrdup(old->description);
+ entry->icon = pa_xstrdup(old->icon);
+ } else {
/* This is a new device, so make sure we write it's priority list correctly */
role_indexes_t max_priority;
pa_datum key;
name2 = pa_xstrndup(key.data, key.size);
- if ((e = read_entry(u, name2))) {
+ if ((e = entry_read(u, name2))) {
for (uint32_t i = 0; i < NUM_ROLES; ++i) {
max_priority[i] = PA_MAX(max_priority[i], e->priority[i]);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name2);
name = pa_xstrndup(key.data, key.size);
device_name = get_name(name, prefix);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
for (uint32_t i = 0; i < NUM_ROLES; ++i) {
if (!highest_priority_available[i] || e->priority[i] < highest_priority_available[i]) {
/* We've found a device with a higher priority than that we've currently got,
}
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
struct userdata *u = userdata;
- struct entry entry, *old = NULL;
+ struct entry *entry, *old = NULL;
char *name = NULL;
- pa_datum key, data;
pa_assert(c);
pa_assert(u);
t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
- pa_zero(entry);
- entry.version = ENTRY_VERSION;
+ entry = entry_new();
if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
pa_sink_input *si;
name = pa_sprintf_malloc("sink:%s", sink->name);
- old = load_or_initialize_entry(u, &entry, name, "sink:");
+ old = load_or_initialize_entry(u, entry, name, "sink:");
- if (!entry.user_set_description)
- pa_strlcpy(entry.description, pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)), sizeof(entry.description));
- else if (strncmp(entry.description, pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)), sizeof(entry.description)) != 0) {
+ if (!entry->user_set_description) {
+ pa_xfree(entry->description);
+ entry->description = pa_xstrdup(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ } else if (!pa_streq(entry->description, pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION))) {
/* Warning: If two modules fight over the description, this could cause an infinite loop.
by changing the description here, we retrigger this subscription callback. The only thing stopping us from
looping is the fact that the string comparison will fail on the second iteration. If another module tries to manage
the description, this will fail... */
- pa_sink_set_description(sink, entry.description);
+ pa_sink_set_description(sink, entry->description);
}
- pa_strlcpy(entry.icon, pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME)), sizeof(entry.icon));
+ pa_xfree(entry->icon);
+ entry->icon = pa_xstrdup(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME));
} else if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE) {
pa_source *source;
name = pa_sprintf_malloc("source:%s", source->name);
- old = load_or_initialize_entry(u, &entry, name, "source:");
+ old = load_or_initialize_entry(u, entry, name, "source:");
- if (!entry.user_set_description)
- pa_strlcpy(entry.description, pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)), sizeof(entry.description));
- else if (strncmp(entry.description, pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)), sizeof(entry.description)) != 0) {
+ if (!entry->user_set_description) {
+ pa_xfree(entry->description);
+ entry->description = pa_xstrdup(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION));
+ } else if (!pa_streq(entry->description, pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION))) {
/* Warning: If two modules fight over the description, this could cause an infinite loop.
by changing the description here, we retrigger this subscription callback. The only thing stopping us from
looping is the fact that the string comparison will fail on the second iteration. If another module tries to manage
the description, this will fail... */
- pa_source_set_description(source, entry.description);
+ pa_source_set_description(source, entry->description);
}
- pa_strlcpy(entry.icon, pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_ICON_NAME)), sizeof(entry.icon));
+ pa_xfree(entry->icon);
+ entry->icon = pa_xstrdup(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_ICON_NAME));
}
pa_assert(name);
if (old) {
- if (entries_equal(old, &entry)) {
- pa_xfree(old);
+ if (entries_equal(old, entry)) {
+ entry_free(old);
+ entry_free(entry);
pa_xfree(name);
return;
}
- pa_xfree(old);
+ entry_free(old);
}
- key.data = name;
- key.size = strlen(name);
-
- data.data = &entry;
- data.size = sizeof(entry);
-
pa_log_info("Storing device %s.", name);
- if (pa_database_set(u->database, &key, &data, TRUE) == 0)
+ if (entry_write(u, name, entry))
trigger_save(u);
else
pa_log_warn("Could not save device");;
+ entry_free(entry);
pa_xfree(name);
}
name = pa_sprintf_malloc("sink:%s", new_data->name);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->user_set_description && strncmp(e->description, pa_proplist_gets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION), sizeof(e->description)) != 0) {
pa_log_info("Restoring description for sink %s.", new_data->name);
pa_proplist_sets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION, e->description);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
name = pa_sprintf_malloc("source:%s", new_data->name);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->user_set_description && strncmp(e->description, pa_proplist_gets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION), sizeof(e->description)) != 0) {
/* NB, We cannot detect if we are a monitor here... this could mess things up a bit... */
pa_log_info("Restoring description for source %s.", new_data->name);
pa_proplist_sets(new_data->proplist, PA_PROP_DEVICE_DESCRIPTION, e->description);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
name = pa_xstrndup(key.data, key.size);
pa_datum_free(&key);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
uint32_t idx;
char *device_name;
uint32_t found_index = PA_INVALID_INDEX;
pa_tagstruct_putu32(reply, e->priority[i]);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (!device || !*device || !description || !*description)
goto fail;
- if ((e = read_entry(u, device))) {
- pa_datum key, data;
-
- pa_strlcpy(e->description, description, sizeof(e->description));
+ if ((e = entry_read(u, device))) {
+ pa_xfree(e->description);
+ e->description = pa_xstrdup(description);
e->user_set_description = TRUE;
- key.data = (char *) device;
- key.size = strlen(device);
-
- data.data = e;
- data.size = sizeof(*e);
-
- if (pa_database_set(u->database, &key, &data, TRUE) == 0) {
+ if (entry_write(u, (char *)device, e)) {
apply_entry(u, device, e);
trigger_save(u);
else
pa_log_warn("Could not save device");
- pa_xfree(e);
+ entry_free(e);
}
else
pa_log_warn("Could not rename device %s, no entry in database", device);
const char *role;
struct entry *e;
uint32_t role_index, n_devices;
- pa_datum key, data;
+ pa_datum key;
pa_bool_t done, sink_mode = TRUE;
struct device_t { uint32_t prio; char *device; };
struct device_t *device;
}
/* Ensure this is a valid entry */
- if (!(e = read_entry(u, s))) {
+ if (!(e = entry_read(u, s))) {
while ((device = pa_hashmap_steal_first(h))) {
pa_xfree(device->device);
pa_xfree(device);
pa_log_error("Client specified an unknown device in it's reorder list.");
goto fail;
}
- pa_xfree(e);
+ entry_free(e);
if (first) {
first = FALSE;
/* Add the device to our hashmap. If it's alredy in it, free it now and carry on */
if (pa_hashmap_put(h, device->device, device) == 0
- && (e = read_entry(u, device->device))) {
+ && (e = entry_read(u, device->device))) {
/* We add offset on to the existing priorirty so that when we order, the
existing entries are always lower priority than the new ones. */
device->prio = (offset + e->priority[role_index]);
idx = 1;
first = TRUE;
for (i = 0; i < n_devices; ++i) {
- if ((e = read_entry(u, devices[i]->device))) {
+ if ((e = entry_read(u, devices[i]->device))) {
if (e->priority[role_index] == idx)
idx++;
else {
e->priority[role_index] = idx;
- key.data = (char *) devices[i]->device;
- key.size = strlen(devices[i]->device);
-
- data.data = e;
- data.size = sizeof(*e);
-
- if (pa_database_set(u->database, &key, &data, TRUE) == 0) {
+ if (entry_write(u, (char *) devices[i]->device, e)) {
first = FALSE;
idx++;
}
#include <pulsecore/source-output.h>
#include <pulsecore/namereg.h>
#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
#include "module-device-restore-symdef.h"
pa_bool_t restore_port:1;
};
-#define ENTRY_VERSION 2
+#define ENTRY_VERSION 3
struct entry {
uint8_t version;
- pa_bool_t muted_valid:1, volume_valid:1, port_valid:1;
- pa_bool_t muted:1;
+ pa_bool_t muted_valid, volume_valid, port_valid;
+ pa_bool_t muted;
pa_channel_map channel_map;
pa_cvolume volume;
- char port[PA_NAME_MAX];
-} PA_GCC_PACKED;
+ char *port;
+};
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
struct userdata *u = userdata;
pa_log_info("Synced.");
}
-static struct entry* read_entry(struct userdata *u, const char *name) {
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->port);
+ pa_xfree(e);
+}
+
+static struct entry* entry_read(struct userdata *u, const char *name) {
pa_datum key, data;
- struct entry *e;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char* port;
pa_assert(u);
pa_assert(name);
if (!pa_database_get(u->database, &key, &data))
goto fail;
- if (data.size != sizeof(struct entry)) {
- pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
- goto fail;
- }
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
- e = (struct entry*) data.data;
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_get_boolean(t, &e->volume_valid) < 0 ||
+ pa_tagstruct_get_channel_map(t, &e->channel_map) < 0 ||
+ pa_tagstruct_get_cvolume(t, &e->volume) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted_valid) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->port_valid) < 0 ||
+ pa_tagstruct_gets(t, &port) < 0) {
- if (e->version != ENTRY_VERSION) {
- pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name);
goto fail;
}
- if (!memchr(e->port, 0, sizeof(e->port))) {
- pa_log_warn("Database contains entry for device %s with missing NUL byte in port name", name);
+ e->port = pa_xstrdup(port);
+
+ if (!pa_tagstruct_eof(t))
goto fail;
- }
if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) {
pa_log_warn("Invalid channel map stored in database for device %s", name);
goto fail;
}
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
+
return e;
fail:
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
pa_datum_free(&data);
return NULL;
}
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_put_boolean(t, e->volume_valid);
+ pa_tagstruct_put_channel_map(t, &e->channel_map);
+ pa_tagstruct_put_cvolume(t, &e->volume);
+ pa_tagstruct_put_boolean(t, e->muted_valid);
+ pa_tagstruct_put_boolean(t, e->muted);
+ pa_tagstruct_put_boolean(t, e->port_valid);
+ pa_tagstruct_puts(t, e->port);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, TRUE) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
+static struct entry* entry_copy(const struct entry *e) {
+ struct entry* r;
+
+ pa_assert(e);
+ r = entry_new();
+ *r = *e;
+ r->port = pa_xstrdup(e->port);
+ return r;
+}
+
static void trigger_save(struct userdata *u) {
if (u->save_time_event)
return;
pa_cvolume t;
if (a->port_valid != b->port_valid ||
- (a->port_valid && strncmp(a->port, b->port, sizeof(a->port))))
+ (a->port_valid && !pa_streq(a->port, b->port)))
return FALSE;
if (a->muted_valid != b->muted_valid ||
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
struct userdata *u = userdata;
- struct entry entry, *old;
+ struct entry *entry, *old;
char *name;
- pa_datum key, data;
pa_assert(c);
pa_assert(u);
t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
- pa_zero(entry);
- entry.version = ENTRY_VERSION;
-
if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
pa_sink *sink;
name = pa_sprintf_malloc("sink:%s", sink->name);
- if ((old = read_entry(u, name)))
- entry = *old;
+ if ((old = entry_read(u, name)))
+ entry = entry_copy(old);
+ else
+ entry = entry_new();
if (sink->save_volume) {
- entry.channel_map = sink->channel_map;
- entry.volume = *pa_sink_get_volume(sink, FALSE);
- entry.volume_valid = TRUE;
+ entry->channel_map = sink->channel_map;
+ entry->volume = *pa_sink_get_volume(sink, FALSE);
+ entry->volume_valid = TRUE;
}
if (sink->save_muted) {
- entry.muted = pa_sink_get_mute(sink, FALSE);
- entry.muted_valid = TRUE;
+ entry->muted = pa_sink_get_mute(sink, FALSE);
+ entry->muted_valid = TRUE;
}
if (sink->save_port) {
- pa_strlcpy(entry.port, sink->active_port ? sink->active_port->name : "", sizeof(entry.port));
- entry.port_valid = TRUE;
+ pa_xfree(entry->port);
+ entry->port = pa_xstrdup(sink->active_port ? sink->active_port->name : "");
+ entry->port_valid = TRUE;
}
} else {
name = pa_sprintf_malloc("source:%s", source->name);
- if ((old = read_entry(u, name)))
- entry = *old;
+ if ((old = entry_read(u, name)))
+ entry = entry_copy(old);
+ else
+ entry = entry_new();
if (source->save_volume) {
- entry.channel_map = source->channel_map;
- entry.volume = *pa_source_get_volume(source, FALSE);
- entry.volume_valid = TRUE;
+ entry->channel_map = source->channel_map;
+ entry->volume = *pa_source_get_volume(source, FALSE);
+ entry->volume_valid = TRUE;
}
if (source->save_muted) {
- entry.muted = pa_source_get_mute(source, FALSE);
- entry.muted_valid = TRUE;
+ entry->muted = pa_source_get_mute(source, FALSE);
+ entry->muted_valid = TRUE;
}
if (source->save_port) {
- pa_strlcpy(entry.port, source->active_port ? source->active_port->name : "", sizeof(entry.port));
- entry.port_valid = TRUE;
+ pa_xfree(entry->port);
+ entry->port = pa_xstrdup(source->active_port ? source->active_port->name : "");
+ entry->port_valid = TRUE;
}
}
+ pa_assert(entry);
+
if (old) {
- if (entries_equal(old, &entry)) {
- pa_xfree(old);
+ if (entries_equal(old, entry)) {
+ entry_free(old);
+ entry_free(entry);
pa_xfree(name);
return;
}
- pa_xfree(old);
+ entry_free(old);
}
- key.data = name;
- key.size = strlen(name);
-
- data.data = &entry;
- data.size = sizeof(entry);
-
pa_log_info("Storing volume/mute/port for device %s.", name);
- pa_database_set(u->database, &key, &data, TRUE);
+ if (entry_write(u, name, entry))
+ trigger_save(u);
+ entry_free(entry);
pa_xfree(name);
-
- trigger_save(u);
}
static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
name = pa_sprintf_malloc("sink:%s", new_data->name);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->port_valid) {
if (!new_data->active_port) {
pa_log_debug("Not restoring port for sink %s, because already set.", name);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
name = pa_sprintf_malloc("sink:%s", new_data->name);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (u->restore_volume && e->volume_valid) {
pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
name = pa_sprintf_malloc("source:%s", new_data->name);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->port_valid) {
if (!new_data->active_port) {
pa_log_debug("Not restoring port for source %s, because already set.", name);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
name = pa_sprintf_malloc("source:%s", new_data->name);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (u->restore_volume && e->volume_valid) {
pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
#include <pulsecore/pstream.h>
#include <pulsecore/pstream-util.h>
#include <pulsecore/database.h>
+#include <pulsecore/tagstruct.h>
#ifdef HAVE_DBUS
#include <pulsecore/dbus-util.h>
#endif
};
-#define ENTRY_VERSION 3
+#define ENTRY_VERSION 4
struct entry {
uint8_t version;
- pa_bool_t muted_valid:1, volume_valid:1, device_valid:1, card_valid:1;
- pa_bool_t muted:1;
+ pa_bool_t muted_valid, volume_valid, device_valid, card_valid;
+ pa_bool_t muted;
pa_channel_map channel_map;
pa_cvolume volume;
- char device[PA_NAME_MAX];
- char card[PA_NAME_MAX];
-} PA_GCC_PACKED;
+ char* device;
+ char* card;
+};
enum {
SUBCOMMAND_TEST,
SUBCOMMAND_EVENT
};
-static struct entry *read_entry(struct userdata *u, const char *name);
-static void apply_entry(struct userdata *u, const char *name, struct entry *e);
+
+static struct entry* entry_new(void);
+static void entry_free(struct entry *e);
+static struct entry *entry_read(struct userdata *u, const char *name);
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e, pa_bool_t replace);
+static struct entry* entry_copy(const struct entry *e);
+static void entry_apply(struct userdata *u, const char *name, struct entry *e);
static void trigger_save(struct userdata *u);
#ifdef HAVE_DBUS
pa_cvolume vol;
dbus_bool_t muted = FALSE;
dbus_bool_t apply_immediately = FALSE;
- pa_datum key;
- pa_datum value;
struct dbus_entry *dbus_entry = NULL;
struct entry *e = NULL;
pa_bool_t volume_updated = FALSE;
pa_bool_t device_updated = FALSE;
- pa_assert_se(e = read_entry(u, name));
+ pa_assert_se(e = entry_read(u, name));
mute_updated = e->muted != muted;
e->muted = muted;
e->muted_valid = TRUE;
e->volume_valid = !!map.channels;
device_updated = (e->device_valid != !!device[0]) || !pa_streq(e->device, device);
- pa_strlcpy(e->device, device, sizeof(e->device));
+ pa_xfree(e->device);
+ e->device = pa_xstrdup(device);
e->device_valid = !!device[0];
if (mute_updated)
dbus_entry = dbus_entry_new(u, name);
pa_assert_se(pa_hashmap_put(u->dbus_entries, dbus_entry->entry_name, dbus_entry) == 0);
- e = pa_xnew0(struct entry, 1);
- e->version = ENTRY_VERSION;
+ e = entry_new();
e->muted_valid = TRUE;
e->volume_valid = !!map.channels;
e->device_valid = !!device[0];
e->muted = muted;
e->volume = vol;
e->channel_map = map;
- pa_strlcpy(e->device, device, sizeof(e->device));
+ e->device = pa_xstrdup(device);
send_new_entry_signal(dbus_entry);
}
- key.data = (char *) name;
- key.size = strlen(name);
+ pa_assert_se(entry_write(u, name, e, TRUE));
- value.data = e;
- value.size = sizeof(struct entry);
-
- pa_assert_se(pa_database_set(u->database, &key, &value, TRUE) == 0);
if (apply_immediately)
- apply_entry(u, name, e);
+ entry_apply(u, name, e);
trigger_save(u);
pa_dbus_send_empty_reply(conn, msg);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_get_entry_by_name(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_assert(msg);
pa_assert(de);
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
device = e->device_valid ? e->device : "";
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_STRING, &device);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_set_device(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
dbus_message_iter_get_basic(iter, &device);
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
updated = (e->device_valid != !!device[0]) || !pa_streq(e->device, device);
if (updated) {
- pa_datum key;
- pa_datum value;
-
- pa_strlcpy(e->device, device, sizeof(e->device));
+ pa_xfree(e->device);
+ e->device = pa_xstrdup(device);
e->device_valid = !!device[0];
- key.data = de->entry_name;
- key.size = strlen(de->entry_name);
- value.data = e;
- value.size = sizeof(struct entry);
- pa_assert_se(pa_database_set(de->userdata->database, &key, &value, TRUE) == 0);
+ pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
- apply_entry(de->userdata, de->entry_name, e);
+ entry_apply(de->userdata, de->entry_name, e);
send_device_updated_signal(de, e);
trigger_save(de->userdata);
}
pa_dbus_send_empty_reply(conn, msg);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_get_volume(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_assert(msg);
pa_assert(de);
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
pa_assert_se(reply = dbus_message_new_method_return(msg));
pa_assert_se(dbus_connection_send(conn, reply, NULL));
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_set_volume(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
if (get_volume_arg(conn, msg, iter, &map, &vol) < 0)
return;
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
updated = (e->volume_valid != !!map.channels) || !pa_cvolume_equal(&e->volume, &vol);
if (updated) {
- pa_datum key;
- pa_datum value;
-
e->volume = vol;
e->channel_map = map;
e->volume_valid = !!map.channels;
- key.data = de->entry_name;
- key.size = strlen(de->entry_name);
- value.data = e;
- value.size = sizeof(struct entry);
- pa_assert_se(pa_database_set(de->userdata->database, &key, &value, TRUE) == 0);
+ pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
- apply_entry(de->userdata, de->entry_name, e);
+ entry_apply(de->userdata, de->entry_name, e);
send_volume_updated_signal(de, e);
trigger_save(de->userdata);
}
pa_dbus_send_empty_reply(conn, msg);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_get_mute(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_assert(msg);
pa_assert(de);
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
mute = e->muted_valid ? e->muted : FALSE;
pa_dbus_send_basic_variant_reply(conn, msg, DBUS_TYPE_BOOLEAN, &mute);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_set_mute(DBusConnection *conn, DBusMessage *msg, DBusMessageIter *iter, void *userdata) {
dbus_message_iter_get_basic(iter, &mute);
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
updated = !e->muted_valid || e->muted != mute;
if (updated) {
- pa_datum key;
- pa_datum value;
-
e->muted = mute;
e->muted_valid = TRUE;
- key.data = de->entry_name;
- key.size = strlen(de->entry_name);
- value.data = e;
- value.size = sizeof(struct entry);
- pa_assert_se(pa_database_set(de->userdata->database, &key, &value, TRUE) == 0);
+ pa_assert_se(entry_write(de->userdata, de->entry_name, e, TRUE));
- apply_entry(de->userdata, de->entry_name, e);
+ entry_apply(de->userdata, de->entry_name, e);
send_mute_updated_signal(de, e);
trigger_save(de->userdata);
}
pa_dbus_send_empty_reply(conn, msg);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_get_all(DBusConnection *conn, DBusMessage *msg, void *userdata) {
pa_assert(msg);
pa_assert(de);
- pa_assert_se(e = read_entry(de->userdata, de->entry_name));
+ pa_assert_se(e = entry_read(de->userdata, de->entry_name));
device = e->device_valid ? e->device : "";
mute = e->muted_valid ? e->muted : FALSE;
dbus_message_unref(reply);
- pa_xfree(e);
+ entry_free(e);
}
static void handle_entry_remove(DBusConnection *conn, DBusMessage *msg, void *userdata) {
return t;
}
-static struct entry *read_entry(struct userdata *u, const char *name) {
+static struct entry* entry_new(void) {
+ struct entry *r = pa_xnew0(struct entry, 1);
+ r->version = ENTRY_VERSION;
+ return r;
+}
+
+static void entry_free(struct entry* e) {
+ pa_assert(e);
+
+ pa_xfree(e->device);
+ pa_xfree(e->card);
+ pa_xfree(e);
+}
+
+static struct entry *entry_read(struct userdata *u, const char *name) {
pa_datum key, data;
- struct entry *e;
+ struct entry *e = NULL;
+ pa_tagstruct *t = NULL;
+ const char *device, *card;
pa_assert(u);
pa_assert(name);
if (!pa_database_get(u->database, &key, &data))
goto fail;
- if (data.size != sizeof(struct entry)) {
- /* This is probably just a database upgrade, hence let's not
- * consider this more than a debug message */
- pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry));
- goto fail;
- }
-
- e = (struct entry*) data.data;
+ t = pa_tagstruct_new(data.data, data.size);
+ e = entry_new();
+
+ if (pa_tagstruct_getu8(t, &e->version) < 0 ||
+ e->version > ENTRY_VERSION ||
+ pa_tagstruct_get_boolean(t, &e->volume_valid) < 0 ||
+ pa_tagstruct_get_channel_map(t, &e->channel_map) < 0 ||
+ pa_tagstruct_get_cvolume(t, &e->volume) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted_valid) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->muted) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->device_valid) < 0 ||
+ pa_tagstruct_gets(t, &device) < 0 ||
+ pa_tagstruct_get_boolean(t, &e->card_valid) < 0 ||
+ pa_tagstruct_gets(t, &card) < 0) {
- if (e->version != ENTRY_VERSION) {
- pa_log_debug("Version of database entry for stream %s doesn't match our version. Probably due to upgrade, ignoring.", name);
goto fail;
}
- if (!memchr(e->device, 0, sizeof(e->device))) {
- pa_log_warn("Database contains entry for stream %s with missing NUL byte in device name", name);
- goto fail;
- }
+ e->device = pa_xstrdup(device);
+ e->card = pa_xstrdup(card);
- 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);
+ if (!pa_tagstruct_eof(t))
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;
}
+ pa_tagstruct_free(t);
+ pa_datum_free(&data);
+
return e;
fail:
+ pa_log_debug("Database contains invalid data for key: %s (probably pre-v1.0 data)", name);
+
+ if (e)
+ entry_free(e);
+ if (t)
+ pa_tagstruct_free(t);
pa_datum_free(&data);
return NULL;
}
+static pa_bool_t entry_write(struct userdata *u, const char *name, const struct entry *e, pa_bool_t replace) {
+ pa_tagstruct *t;
+ pa_datum key, data;
+ pa_bool_t r;
+
+ pa_assert(u);
+ pa_assert(name);
+ pa_assert(e);
+
+ t = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu8(t, e->version);
+ pa_tagstruct_put_boolean(t, e->volume_valid);
+ pa_tagstruct_put_channel_map(t, &e->channel_map);
+ pa_tagstruct_put_cvolume(t, &e->volume);
+ pa_tagstruct_put_boolean(t, e->muted_valid);
+ pa_tagstruct_put_boolean(t, e->muted);
+ pa_tagstruct_put_boolean(t, e->device_valid);
+ pa_tagstruct_puts(t, e->device);
+ pa_tagstruct_put_boolean(t, e->card_valid);
+ pa_tagstruct_puts(t, e->card);
+
+ key.data = (char *) name;
+ key.size = strlen(name);
+
+ data.data = (void*)pa_tagstruct_data(t, &data.size);
+
+ r = (pa_database_set(u->database, &key, &data, replace) == 0);
+
+ pa_tagstruct_free(t);
+
+ return r;
+}
+
+static struct entry* entry_copy(const struct entry *e) {
+ struct entry* r;
+
+ pa_assert(e);
+ r = entry_new();
+ *r = *e;
+ r->device = pa_xstrdup(e->device);
+ r->card = pa_xstrdup(e->card);
+ return r;
+}
+
static void trigger_save(struct userdata *u) {
pa_native_connection *c;
uint32_t idx;
pa_assert(b);
if (a->device_valid != b->device_valid ||
- (a->device_valid && strncmp(a->device, b->device, sizeof(a->device))))
+ (a->device_valid && !pa_streq(a->device, b->device)))
return FALSE;
if (a->card_valid != b->card_valid ||
- (a->card_valid && strncmp(a->card, b->card, sizeof(a->card))))
+ (a->card_valid && !pa_streq(a->card, b->card)))
return FALSE;
if (a->muted_valid != b->muted_valid ||
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
struct userdata *u = userdata;
- struct entry entry, *old = NULL;
+ struct entry *entry, *old = NULL;
char *name = NULL;
- pa_datum key, data;
/* These are only used when D-Bus is enabled, but in order to reduce ifdef
* clutter these are defined here unconditionally. */
t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
- pa_zero(entry);
- entry.version = ENTRY_VERSION;
-
if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
pa_sink_input *sink_input;
if (!(name = get_name(sink_input->proplist, "sink-input")))
return;
- if ((old = read_entry(u, name))) {
- entry = *old;
+ if ((old = entry_read(u, name))) {
+ entry = entry_copy(old);
created_new_entry = FALSE;
- }
+ } else
+ entry = entry_new();
if (sink_input->save_volume && pa_sink_input_is_volume_readable(sink_input)) {
pa_assert(sink_input->volume_writable);
- entry.channel_map = sink_input->channel_map;
- pa_sink_input_get_volume(sink_input, &entry.volume, FALSE);
- entry.volume_valid = TRUE;
+ entry->channel_map = sink_input->channel_map;
+ pa_sink_input_get_volume(sink_input, &entry->volume, FALSE);
+ entry->volume_valid = TRUE;
volume_updated = !created_new_entry
&& (!old->volume_valid
- || !pa_channel_map_equal(&entry.channel_map, &old->channel_map)
- || !pa_cvolume_equal(&entry.volume, &old->volume));
+ || !pa_channel_map_equal(&entry->channel_map, &old->channel_map)
+ || !pa_cvolume_equal(&entry->volume, &old->volume));
}
if (sink_input->save_muted) {
- entry.muted = pa_sink_input_get_mute(sink_input);
- entry.muted_valid = TRUE;
+ entry->muted = pa_sink_input_get_mute(sink_input);
+ entry->muted_valid = TRUE;
- mute_updated = !created_new_entry && (!old->muted_valid || entry.muted != old->muted);
+ mute_updated = !created_new_entry && (!old->muted_valid || entry->muted != old->muted);
}
if (sink_input->save_sink) {
- pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device));
- entry.device_valid = TRUE;
+ pa_xfree(entry->device);
+ entry->device = pa_xstrdup(sink_input->sink->name);
+ entry->device_valid = TRUE;
- device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry.device, old->device));
+ 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;
+ pa_xfree(entry->card);
+ entry->card = pa_xstrdup(sink_input->sink->card->name);
+ entry->card_valid = TRUE;
}
}
if (!(name = get_name(source_output->proplist, "source-output")))
return;
- if ((old = read_entry(u, name))) {
- entry = *old;
+ if ((old = entry_read(u, name))) {
+ entry = entry_copy(old);
created_new_entry = FALSE;
- }
+ } else
+ entry = entry_new();
if (source_output->save_source) {
- pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device));
- entry.device_valid = TRUE;
+ pa_xfree(entry->device);
+ entry->device = pa_xstrdup(source_output->source->name);
+ entry->device_valid = TRUE;
- device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry.device, old->device));
+ 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;
+ pa_xfree(entry->card);
+ entry->card = pa_xstrdup(source_output->source->card->name);
+ entry->card_valid = TRUE;
}
}
}
+ pa_assert(entry);
+
if (old) {
- if (entries_equal(old, &entry)) {
- pa_xfree(old);
+ if (entries_equal(old, entry)) {
+ entry_free(old);
+ entry_free(entry);
pa_xfree(name);
return;
}
- pa_xfree(old);
+ entry_free(old);
}
- key.data = name;
- key.size = strlen(name);
-
- data.data = &entry;
- data.size = sizeof(entry);
-
pa_log_info("Storing volume/mute/device for stream %s.", name);
- pa_database_set(u->database, &key, &data, TRUE);
+ if (entry_write(u, name, entry, TRUE))
+ trigger_save(u);
#ifdef HAVE_DBUS
if (created_new_entry) {
pa_assert_se(de = pa_hashmap_get(u->dbus_entries, name));
if (device_updated)
- send_device_updated_signal(de, &entry);
+ send_device_updated_signal(de, entry);
if (volume_updated)
- send_volume_updated_signal(de, &entry);
+ send_volume_updated_signal(de, entry);
if (mute_updated)
- send_mute_updated_signal(de, &entry);
+ send_mute_updated_signal(de, entry);
}
#endif
+ entry_free(entry);
pa_xfree(name);
-
- trigger_save(u);
}
static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
if (new_data->sink)
pa_log_debug("Not restoring device for stream %s, because already set to '%s'.", name, new_data->sink->name);
- else if ((e = read_entry(u, name))) {
+ else if ((e = entry_read(u, name))) {
pa_sink *s = NULL;
if (e->device_valid)
if (pa_sink_input_new_data_set_sink(new_data, s, TRUE))
pa_log_info("Restoring device for stream %s.", name);
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (!(name = get_name(new_data->proplist, "sink-input")))
return PA_HOOK_OK;
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (u->restore_volume && e->volume_valid) {
if (!new_data->volume_writable)
pa_log_debug("Not restoring mute state for sink input %s, because already set.", name);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (new_data->source)
pa_log_debug("Not restoring device for stream %s, because already set", name);
- else if ((e = read_entry(u, name))) {
+ else if ((e = entry_read(u, name))) {
pa_source *s = NULL;
if (e->device_valid)
pa_source_output_new_data_set_source(new_data, s, TRUE);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (!(name = get_name(si->proplist, "sink-input")))
continue;
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->device_valid && pa_streq(e->device, sink->name))
pa_sink_input_move_to(si, sink, TRUE);
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (!(name = get_name(so->proplist, "source-output")))
continue;
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->device_valid && pa_streq(e->device, source->name))
pa_source_output_move_to(so, source, TRUE);
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (!(name = get_name(si->proplist, "sink-input")))
continue;
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->device_valid) {
pa_sink *d;
pa_sink_input_move_to(si, d, TRUE);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
if (!(name = get_name(so->proplist, "source-output")))
continue;
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
if (e->device_valid) {
pa_source *d;
pa_source_output_move_to(so, d, TRUE);
}
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
#define EXT_VERSION 1
-static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
+static void entry_apply(struct userdata *u, const char *name, struct entry *e) {
pa_sink_input *si;
pa_source_output *so;
uint32_t idx;
name = pa_xstrndup(key.data, key.size);
pa_datum_free(&key);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
char t[256];
pa_log("name=%s", name);
pa_log("device=%s %s", e->device, pa_yes_no(e->device_valid));
pa_log("channel_map=%s", pa_channel_map_snprint(t, sizeof(t), &e->channel_map));
pa_log("volume=%s %s", pa_cvolume_snprint(t, sizeof(t), &e->volume), pa_yes_no(e->volume_valid));
pa_log("mute=%s %s", pa_yes_no(e->muted), pa_yes_no(e->volume_valid));
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
name = pa_xstrndup(key.data, key.size);
pa_datum_free(&key);
- if ((e = read_entry(u, name))) {
+ if ((e = entry_read(u, name))) {
pa_cvolume r;
pa_channel_map cm;
pa_tagstruct_puts(reply, e->device_valid ? e->device : NULL);
pa_tagstruct_put_boolean(reply, e->muted_valid ? e->muted : FALSE);
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);
while (!pa_tagstruct_eof(t)) {
const char *name, *device;
pa_bool_t muted;
- struct entry entry;
- pa_datum key, data;
+ struct entry *entry;
#ifdef HAVE_DBUS
struct entry *old;
#endif
- pa_zero(entry);
- entry.version = ENTRY_VERSION;
+ entry = entry_new();
if (pa_tagstruct_gets(t, &name) < 0 ||
- pa_tagstruct_get_channel_map(t, &entry.channel_map) ||
- pa_tagstruct_get_cvolume(t, &entry.volume) < 0 ||
+ pa_tagstruct_get_channel_map(t, &entry->channel_map) ||
+ pa_tagstruct_get_cvolume(t, &entry->volume) < 0 ||
pa_tagstruct_gets(t, &device) < 0 ||
pa_tagstruct_get_boolean(t, &muted) < 0)
goto fail;
- if (!name || !*name)
+ if (!name || !*name) {
+ entry_free(entry);
goto fail;
+ }
- entry.volume_valid = entry.volume.channels > 0;
+ entry->volume_valid = entry->volume.channels > 0;
- if (entry.volume_valid)
- if (!pa_cvolume_compatible_with_channel_map(&entry.volume, &entry.channel_map))
+ if (entry->volume_valid)
+ if (!pa_cvolume_compatible_with_channel_map(&entry->volume, &entry->channel_map)) {
+ entry_free(entry);
goto fail;
+ }
- entry.muted = muted;
- entry.muted_valid = TRUE;
+ entry->muted = muted;
+ entry->muted_valid = TRUE;
- if (device)
- pa_strlcpy(entry.device, device, sizeof(entry.device));
- entry.device_valid = !!entry.device[0];
+ entry->device = pa_xstrdup(device);
+ entry->device_valid = device && !!entry->device[0];
- if (entry.device_valid &&
- !pa_namereg_is_valid_name(entry.device))
+ if (entry->device_valid && !pa_namereg_is_valid_name(entry->device)) {
+ entry_free(entry);
goto fail;
+ }
#ifdef HAVE_DBUS
- old = read_entry(u, name);
+ old = entry_read(u, name);
#endif
- key.data = (char*) name;
- key.size = strlen(name);
-
- 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) {
+ if (entry_write(u, name, entry, mode == PA_UPDATE_REPLACE)) {
#ifdef HAVE_DBUS
struct dbus_entry *de;
if (old) {
pa_assert_se((de = pa_hashmap_get(u->dbus_entries, name)));
- if ((old->device_valid != entry.device_valid)
- || (entry.device_valid && !pa_streq(entry.device, old->device)))
- send_device_updated_signal(de, &entry);
+ if ((old->device_valid != entry->device_valid)
+ || (entry->device_valid && !pa_streq(entry->device, old->device)))
+ 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))))
- send_volume_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))))
+ send_volume_updated_signal(de, entry);
- if (!old->muted_valid || (entry.muted != old->muted))
- send_mute_updated_signal(de, &entry);
+ if (!old->muted_valid || (entry->muted != old->muted))
+ send_mute_updated_signal(de, entry);
} else {
de = dbus_entry_new(u, name);
#endif
if (apply_immediately)
- apply_entry(u, name, &entry);
+ entry_apply(u, name, entry);
}
#ifdef HAVE_DBUS
if (old)
- pa_xfree(old);
+ entry_free(old);
#endif
+ entry_free(entry);
}
trigger_save(u);
name = pa_xstrndup(key.data, key.size);
pa_datum_free(&key);
- /* Use read_entry() for checking that the entry is valid. */
- if ((e = read_entry(u, name))) {
+ /* Use entry_read() for checking that the entry is valid. */
+ if ((e = entry_read(u, name))) {
de = dbus_entry_new(u, name);
pa_assert_se(pa_hashmap_put(u->dbus_entries, de->entry_name, de) == 0);
- pa_xfree(e);
+ entry_free(e);
}
pa_xfree(name);