]> code.delx.au - pulseaudio/blobdiff - src/modules/module-device-restore.c
Modify pa_state_path() to take an additional argument for prepending the machine...
[pulseaudio] / src / modules / module-device-restore.c
index 0a41b84a2ce86826af9ba2fac405846a17bae1ae..3d731f122511a8eeb4fd4ad543afa3b3280857cf 100644 (file)
@@ -1,9 +1,7 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
-  Copyright 2006 Lennart Poettering
+  Copyright 2006-2008 Lennart Poettering
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
@@ -59,20 +57,29 @@ 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_module *module;
     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_channel_map channel_map;
     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) {
@@ -91,9 +98,65 @@ 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;
+    }
+
+    if (!(pa_channel_map_valid(&e->channel_map))) {
+        pa_log_warn("Invalid channel map stored in database for device %s", name);
+        goto fail;
+    }
+
+    if (e->volume.channels != e->channel_map.channels) {
+        pa_log_warn("Volume and channel map don't match in database entry for device %s", name);
+        goto fail;
+    }
+
+    return e;
+
+fail:
+
+    pa_xfree(data.dptr);
+    return NULL;
+}
+
+static void trigger_save(struct userdata *u) {
+    struct timeval tv;
+
+    if (u->save_time_event)
+        return;
+
+    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);
+}
+
 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;
 
@@ -106,6 +169,8 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
         t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
         return;
 
+    memset(&entry, 0, sizeof(entry));
+
     if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
         pa_sink *sink;
 
@@ -113,6 +178,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
             return;
 
         name = pa_sprintf_malloc("sink:%s", sink->name);
+        entry.channel_map = sink->channel_map;
         entry.volume = *pa_sink_get_volume(sink);
         entry.muted = pa_sink_get_mute(sink);
 
@@ -125,38 +191,27 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
             return;
 
         name = pa_sprintf_malloc("source:%s", source->name);
+        entry.channel_map = source->channel_map;
         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 ((old = read_entry(u, name))) {
 
-            if (pa_cvolume_valid(&old->volume)) {
+        if (pa_cvolume_equal(pa_cvolume_remap(&old->volume, &old->channel_map, &entry.channel_map), &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);
 
@@ -164,52 +219,11 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
 
     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) {
-    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;
+    trigger_save(u);
 }
 
-
 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;
@@ -220,13 +234,16 @@ 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) {
             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, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
+        }
+
+        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_log_info("Restoring mute state for sink %s.", new_data->name);
-        pa_sink_new_data_set_muted(new_data, e->muted);
         pa_xfree(e);
     }
 
@@ -245,13 +262,16 @@ 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) {
             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, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
+        }
+
+        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_log_info("Restoring mute state for source %s.", new_data->name);
-        pa_source_new_data_set_muted(new_data, e->muted);
         pa_xfree(e);
     }
 
@@ -263,11 +283,11 @@ static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_da
 int pa__init(pa_module*m) {
     pa_modargs *ma = NULL;
     struct userdata *u;
-    char *fname, *runtime_dir;
-    char hn[256];
+    char *fname, *fn;
     pa_sink *sink;
     pa_source *source;
     uint32_t idx;
+    pa_bool_t restore_volume = TRUE, restore_muted = TRUE;
 
     pa_assert(m);
 
@@ -276,26 +296,41 @@ 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->module = m;
     u->save_time_event = NULL;
+    u->restore_volume = restore_volume;
+    u->restore_muted = restore_muted;
+    u->gdbm_file = NULL;
 
     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_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_cb_t) source_fixate_hook_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->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;
+    /* We include the host identifier in the file name because gdbm
+     * files are CPU dependant, and we don't want things to go wrong
+     * if we are on a multiarch system. */
 
-    if (!pa_get_host_name(hn, sizeof(hn)))
-        goto fail;
+    fn = pa_sprintf_malloc("device-volumes."CANONICAL_HOST".gdbm");
+    fname = pa_state_path(fn, TRUE);
+    pa_xfree(fn);
 
-    if (!(runtime_dir = pa_get_runtime_dir()))
+    if (!fname)
         goto fail;
 
-    fname = pa_sprintf_malloc("%s/device-volumes.%s.gdbm", runtime_dir, hn);
-    pa_xfree(runtime_dir);
-
     if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT, 0600, NULL))) {
         pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
         pa_xfree(fname);