diff options
Diffstat (limited to 'src/modules/module-device-restore.c')
-rw-r--r-- | src/modules/module-device-restore.c | 166 |
1 files changed, 91 insertions, 75 deletions
diff --git a/src/modules/module-device-restore.c b/src/modules/module-device-restore.c index b7a1e1b5..c0d2ddb7 100644 --- a/src/modules/module-device-restore.c +++ b/src/modules/module-device-restore.c @@ -57,20 +57,27 @@ PA_MODULE_LOAD_ONCE(TRUE); #define SAVE_INTERVAL 10 static const char* const valid_modargs[] = { - NULL, + "restore_volume", + "restore_muted", + NULL }; struct userdata { pa_core *core; 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; GDBM_FILE gdbm_file; + + pa_bool_t restore_volume:1; + pa_bool_t restore_muted:1; }; struct entry { 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) { @@ -89,9 +96,44 @@ static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct pa_log_info("Synced."); } +static struct entry* read_entry(struct userdata *u, char *name) { + datum key, data; + struct entry *e; + + pa_assert(u); + pa_assert(name); + + key.dptr = name; + key.dsize = strlen(name); + + data = gdbm_fetch(u->gdbm_file, key); + + if (!data.dptr) + goto fail; + + if (data.dsize != sizeof(struct entry)) { + pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry)); + goto fail; + } + + e = (struct entry*) data.dptr; + + if (!(pa_cvolume_valid(&e->volume))) { + pa_log_warn("Invalid volume stored in database for device %s", name); + goto fail; + } + + return e; + +fail: + + pa_xfree(data.dptr); + 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; + struct entry entry, *old; char *name; datum key, data; @@ -127,34 +169,22 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 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 ((old = read_entry(u, name))) { - if (pa_cvolume_equal(&old->volume, &entry.volume) && - !old->muted == !entry.muted) { + 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(old); + pa_xfree(name); + return; + } - pa_xfree(data.dptr); + pa_xfree(old); } + key.dptr = name; + key.dsize = strlen(name); + data.dptr = (void*) &entry; data.dsize = sizeof(entry); @@ -172,42 +202,6 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 pa_xfree(name); } -static struct entry* read_entry(struct userdata *u, char *name) { - datum key, data; - struct entry *e; - - pa_assert(u); - pa_assert(name); - - key.dptr = name; - key.dsize = strlen(name); - - data = gdbm_fetch(u->gdbm_file, key); - - if (!data.dptr) - goto fail; - - if (data.dsize != sizeof(struct entry)) { - pa_log_warn("Database contains entry for device %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry)); - goto fail; - } - - e = (struct entry*) data.dptr; - - if (!(pa_cvolume_valid(&e->volume))) { - pa_log_warn("Invalid volume stored in database for device %s", name); - goto fail; - } - - return e; - -fail: - - pa_xfree(data.dptr); - return NULL; -} - - static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) { char *name; struct entry *e; @@ -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->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_sink_new_data_set_volume(new_data, &e->volume); } - pa_log_info("Restoring mute state for sink %s.", new_data->name); - pa_sink_new_data_set_muted(new_data, e->muted); + if (u->restore_muted) { + pa_log_info("Restoring mute state for sink %s.", new_data->name); + pa_sink_new_data_set_muted(new_data, e->muted); + } + 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->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_source_new_data_set_volume(new_data, &e->volume); } - pa_log_info("Restoring mute state for source %s.", new_data->name); - pa_source_new_data_set_muted(new_data, e->muted); + if (u->restore_muted) { + pa_log_info("Restoring mute state for source %s.", new_data->name); + pa_source_new_data_set_muted(new_data, e->muted); + } + pa_xfree(e); } @@ -266,6 +270,7 @@ int pa__init(pa_module*m) { pa_sink *sink; pa_source *source; uint32_t idx; + pa_bool_t restore_volume = TRUE, restore_muted = TRUE; pa_assert(m); @@ -274,21 +279,32 @@ int pa__init(pa_module*m) { 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->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->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); - - m->userdata = 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->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); + } if (!pa_get_host_name(hn, sizeof(hn))) 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); pa_xfree(fn); |