a bit of refactoring

This commit is contained in:
Lennart Poettering 2008-08-03 23:22:38 +02:00
parent d7b138daa2
commit ec19f2bbf8

View file

@ -57,20 +57,27 @@ PA_MODULE_LOAD_ONCE(TRUE);
#define SAVE_INTERVAL 10 #define SAVE_INTERVAL 10
static const char* const valid_modargs[] = { static const char* const valid_modargs[] = {
NULL, "restore_volume",
"restore_muted",
NULL
}; };
struct userdata { struct userdata {
pa_core *core; pa_core *core;
pa_subscription *subscription; pa_subscription *subscription;
pa_hook_slot *sink_fixate_hook_slot, *source_fixate_hook_slot; pa_hook_slot
*sink_fixate_hook_slot,
*source_fixate_hook_slot;
pa_time_event *save_time_event; pa_time_event *save_time_event;
GDBM_FILE gdbm_file; GDBM_FILE gdbm_file;
pa_bool_t restore_volume:1;
pa_bool_t restore_muted:1;
}; };
struct entry { struct entry {
pa_cvolume volume; pa_cvolume volume;
int muted; pa_bool_t muted:1;
}; };
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
@ -89,89 +96,6 @@ static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct
pa_log_info("Synced."); pa_log_info("Synced.");
} }
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;
char *name;
datum key, data;
pa_assert(c);
pa_assert(u);
if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
pa_sink *sink;
if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
return;
name = pa_sprintf_malloc("sink:%s", sink->name);
entry.volume = *pa_sink_get_volume(sink);
entry.muted = pa_sink_get_mute(sink);
} else {
pa_source *source;
pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
if (!(source = pa_idxset_get_by_index(c->sources, idx)))
return;
name = pa_sprintf_malloc("source:%s", source->name);
entry.volume = *pa_source_get_volume(source);
entry.muted = pa_source_get_mute(source);
}
key.dptr = name;
key.dsize = strlen(name);
data = gdbm_fetch(u->gdbm_file, key);
if (data.dptr) {
if (data.dsize == sizeof(struct entry)) {
struct entry *old = (struct entry*) data.dptr;
if (pa_cvolume_valid(&old->volume)) {
if (pa_cvolume_equal(&old->volume, &entry.volume) &&
!old->muted == !entry.muted) {
pa_xfree(data.dptr);
pa_xfree(name);
return;
}
} else
pa_log_warn("Invalid volume stored in database for device %s", name);
} else
pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
pa_xfree(data.dptr);
}
data.dptr = (void*) &entry;
data.dsize = sizeof(entry);
pa_log_info("Storing volume/mute for device %s.", name);
gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
if (!u->save_time_event) {
struct timeval tv;
pa_gettimeofday(&tv);
tv.tv_sec += SAVE_INTERVAL;
u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
}
pa_xfree(name);
}
static struct entry* read_entry(struct userdata *u, char *name) { static struct entry* read_entry(struct userdata *u, char *name) {
datum key, data; datum key, data;
struct entry *e; struct entry *e;
@ -207,6 +131,76 @@ fail:
return NULL; return NULL;
} }
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;
char *name;
datum key, data;
pa_assert(c);
pa_assert(u);
if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
return;
if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
pa_sink *sink;
if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
return;
name = pa_sprintf_malloc("sink:%s", sink->name);
entry.volume = *pa_sink_get_volume(sink);
entry.muted = pa_sink_get_mute(sink);
} else {
pa_source *source;
pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
if (!(source = pa_idxset_get_by_index(c->sources, idx)))
return;
name = pa_sprintf_malloc("source:%s", source->name);
entry.volume = *pa_source_get_volume(source);
entry.muted = pa_source_get_mute(source);
}
if ((old = read_entry(u, name))) {
if (pa_cvolume_equal(&old->volume, &entry.volume) &&
!old->muted == !entry.muted) {
pa_xfree(old);
pa_xfree(name);
return;
}
pa_xfree(old);
}
key.dptr = name;
key.dsize = strlen(name);
data.dptr = (void*) &entry;
data.dsize = sizeof(entry);
pa_log_info("Storing volume/mute for device %s.", name);
gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
if (!u->save_time_event) {
struct timeval tv;
pa_gettimeofday(&tv);
tv.tv_sec += SAVE_INTERVAL;
u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
}
pa_xfree(name);
}
static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) { static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) {
char *name; char *name;
@ -218,13 +212,18 @@ static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *
if ((e = read_entry(u, name))) { if ((e = read_entry(u, name))) {
if (e->volume.channels == new_data->sample_spec.channels) { if (u->restore_volume &&
e->volume.channels == new_data->sample_spec.channels) {
pa_log_info("Restoring volume for sink %s.", new_data->name); pa_log_info("Restoring volume for sink %s.", new_data->name);
pa_sink_new_data_set_volume(new_data, &e->volume); pa_sink_new_data_set_volume(new_data, &e->volume);
} }
if (u->restore_muted) {
pa_log_info("Restoring mute state for sink %s.", new_data->name); pa_log_info("Restoring mute state for sink %s.", new_data->name);
pa_sink_new_data_set_muted(new_data, e->muted); pa_sink_new_data_set_muted(new_data, e->muted);
}
pa_xfree(e); pa_xfree(e);
} }
@ -243,13 +242,18 @@ static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_da
if ((e = read_entry(u, name))) { if ((e = read_entry(u, name))) {
if (e->volume.channels == new_data->sample_spec.channels) { if (u->restore_volume &&
e->volume.channels == new_data->sample_spec.channels) {
pa_log_info("Restoring volume for source %s.", new_data->name); pa_log_info("Restoring volume for source %s.", new_data->name);
pa_source_new_data_set_volume(new_data, &e->volume); pa_source_new_data_set_volume(new_data, &e->volume);
} }
if (u->restore_muted) {
pa_log_info("Restoring mute state for source %s.", new_data->name); pa_log_info("Restoring mute state for source %s.", new_data->name);
pa_source_new_data_set_muted(new_data, e->muted); pa_source_new_data_set_muted(new_data, e->muted);
}
pa_xfree(e); pa_xfree(e);
} }
@ -266,6 +270,7 @@ int pa__init(pa_module*m) {
pa_sink *sink; pa_sink *sink;
pa_source *source; pa_source *source;
uint32_t idx; uint32_t idx;
pa_bool_t restore_volume = TRUE, restore_muted = TRUE;
pa_assert(m); pa_assert(m);
@ -274,21 +279,32 @@ int pa__init(pa_module*m) {
goto fail; goto fail;
} }
u = pa_xnew(struct userdata, 1); if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
pa_log("restore_volume= and restore_muted= expect boolean arguments");
goto fail;
}
if (!restore_muted && !restore_volume)
pa_log_warn("Neither restoring volume nor restoring muted enabled!");
m->userdata = u = pa_xnew(struct userdata, 1);
u->core = m->core; u->core = m->core;
u->save_time_event = NULL; u->save_time_event = NULL;
u->restore_volume = restore_volume;
u->restore_muted = restore_muted;
u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u); u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u);
if (restore_muted || restore_volume) {
u->sink_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_fixate_hook_callback, u); u->sink_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_fixate_hook_callback, u);
u->source_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) source_fixate_hook_callback, u); u->source_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) source_fixate_hook_callback, u);
}
m->userdata = u;
if (!pa_get_host_name(hn, sizeof(hn))) if (!pa_get_host_name(hn, sizeof(hn)))
goto fail; goto fail;
fn = pa_sprintf_malloc("device-volumes.%s.gdbm", hn); fn = pa_sprintf_malloc("device-volumes.%s."CANONICAL_HOST".gdbm", hn);
fname = pa_state_path(fn); fname = pa_state_path(fn);
pa_xfree(fn); pa_xfree(fn);