~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/modules/module-card-restore.c

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2009-07-28 14:00:27 UTC
  • mfrom: (1.5.1 upstream)
  • mto: (1.4.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 65.
  • Revision ID: james.westby@ubuntu.com-20090728140027-wts8ya37tyrh4ww5
Tags: upstream-0.9.16~test2~20090726git59659e1db
ImportĀ upstreamĀ versionĀ 0.9.16~test2~20090726git59659e1db

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <stdio.h>
31
31
#include <stdlib.h>
32
32
#include <ctype.h>
33
 
#include <gdbm.h>
34
33
 
35
34
#include <pulse/xmalloc.h>
36
35
#include <pulse/volume.h>
37
36
#include <pulse/timeval.h>
38
37
#include <pulse/util.h>
 
38
#include <pulse/rtclock.h>
39
39
 
40
40
#include <pulsecore/core-error.h>
41
41
#include <pulsecore/module.h>
45
45
#include <pulsecore/core-subscribe.h>
46
46
#include <pulsecore/card.h>
47
47
#include <pulsecore/namereg.h>
 
48
#include <pulsecore/database.h>
48
49
 
49
50
#include "module-card-restore-symdef.h"
50
51
 
53
54
PA_MODULE_VERSION(PACKAGE_VERSION);
54
55
PA_MODULE_LOAD_ONCE(TRUE);
55
56
 
56
 
#define SAVE_INTERVAL 10
 
57
#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
57
58
 
58
59
static const char* const valid_modargs[] = {
59
60
    NULL
65
66
    pa_subscription *subscription;
66
67
    pa_hook_slot *card_new_hook_slot;
67
68
    pa_time_event *save_time_event;
68
 
    GDBM_FILE gdbm_file;
 
69
    pa_database *database;
69
70
};
70
71
 
71
72
#define ENTRY_VERSION 1
75
76
    char profile[PA_NAME_MAX];
76
77
} PA_GCC_PACKED ;
77
78
 
78
 
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
 
79
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
79
80
    struct userdata *u = userdata;
80
81
 
81
82
    pa_assert(a);
82
83
    pa_assert(e);
83
 
    pa_assert(tv);
84
84
    pa_assert(u);
85
85
 
86
86
    pa_assert(e == u->save_time_event);
87
87
    u->core->mainloop->time_free(u->save_time_event);
88
88
    u->save_time_event = NULL;
89
89
 
90
 
    gdbm_sync(u->gdbm_file);
 
90
    pa_database_sync(u->database);
91
91
    pa_log_info("Synced.");
92
92
}
93
93
 
94
94
static struct entry* read_entry(struct userdata *u, const char *name) {
95
 
    datum key, data;
 
95
    pa_datum key, data;
96
96
    struct entry *e;
97
97
 
98
98
    pa_assert(u);
99
99
    pa_assert(name);
100
100
 
101
 
    key.dptr = (char*) name;
102
 
    key.dsize = (int) strlen(name);
103
 
 
104
 
    data = gdbm_fetch(u->gdbm_file, key);
105
 
 
106
 
    if (!data.dptr)
 
101
    key.data = (char*) name;
 
102
    key.size = strlen(name);
 
103
 
 
104
    pa_zero(data);
 
105
 
 
106
    if (!pa_database_get(u->database, &key, &data))
107
107
        goto fail;
108
108
 
109
 
    if (data.dsize != sizeof(struct entry)) {
110
 
        pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
 
109
    if (data.size != sizeof(struct entry)) {
 
110
        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));
111
111
        goto fail;
112
112
    }
113
113
 
114
 
    e = (struct entry*) data.dptr;
 
114
    e = (struct entry*) data.data;
115
115
 
116
116
    if (e->version != ENTRY_VERSION) {
117
117
        pa_log_debug("Version of database entry for card %s doesn't match our version. Probably due to upgrade, ignoring.", name);
127
127
 
128
128
fail:
129
129
 
130
 
    pa_xfree(data.dptr);
 
130
    pa_datum_free(&data);
131
131
    return NULL;
132
132
}
133
133
 
134
134
static void trigger_save(struct userdata *u) {
135
 
    struct timeval tv;
136
 
 
137
135
    if (u->save_time_event)
138
136
        return;
139
137
 
140
 
    pa_gettimeofday(&tv);
141
 
    tv.tv_sec += SAVE_INTERVAL;
142
 
    u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
 
138
    u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
143
139
}
144
140
 
145
141
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
146
142
    struct userdata *u = userdata;
147
143
    struct entry entry, *old;
148
 
    datum key, data;
 
144
    pa_datum key, data;
149
145
    pa_card *card;
150
146
 
151
147
    pa_assert(c);
155
151
        t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE))
156
152
        return;
157
153
 
158
 
    memset(&entry, 0, sizeof(entry));
 
154
    pa_zero(entry);
159
155
    entry.version = ENTRY_VERSION;
160
156
 
161
157
    if (!(card = pa_idxset_get_by_index(c->cards, idx)))
162
158
        return;
163
159
 
 
160
    if (!card->save_profile)
 
161
        return;
 
162
 
164
163
    pa_strlcpy(entry.profile, card->active_profile ? card->active_profile->name : "", sizeof(entry.profile));
165
164
 
166
165
    if ((old = read_entry(u, card->name))) {
173
172
        pa_xfree(old);
174
173
    }
175
174
 
176
 
    key.dptr = card->name;
177
 
    key.dsize = (int) strlen(card->name);
 
175
    key.data = card->name;
 
176
    key.size = strlen(card->name);
178
177
 
179
 
    data.dptr = (void*) &entry;
180
 
    data.dsize = sizeof(entry);
 
178
    data.data = &entry;
 
179
    data.size = sizeof(entry);
181
180
 
182
181
    pa_log_info("Storing profile for card %s.", card->name);
183
182
 
184
 
    gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
 
183
    pa_database_set(u->database, &key, &data, TRUE);
185
184
 
186
185
    trigger_save(u);
187
186
}
194
193
    if ((e = read_entry(u, new_data->name)) && e->profile[0]) {
195
194
 
196
195
        if (!new_data->active_profile) {
 
196
            pa_log_info("Restoring profile for card %s.", new_data->name);
197
197
            pa_card_new_data_set_profile(new_data, e->profile);
198
 
            pa_log_info("Restoring profile for card %s.", new_data->name);
 
198
            new_data->save_profile = TRUE;
199
199
        } else
200
200
            pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
201
201
 
208
208
int pa__init(pa_module*m) {
209
209
    pa_modargs *ma = NULL;
210
210
    struct userdata *u;
211
 
    char *fname, *fn;
 
211
    char *fname;
212
212
    pa_card *card;
213
213
    uint32_t idx;
214
 
    int gdbm_cache_size;
215
214
 
216
215
    pa_assert(m);
217
216
 
220
219
        goto fail;
221
220
    }
222
221
 
223
 
    m->userdata = u = pa_xnew(struct userdata, 1);
 
222
    m->userdata = u = pa_xnew0(struct userdata, 1);
224
223
    u->core = m->core;
225
224
    u->module = m;
226
 
    u->save_time_event = NULL;
227
 
    u->gdbm_file = NULL;
228
225
 
229
226
    u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u);
230
227
 
231
228
    u->card_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u);
232
229
 
233
 
    /* We include the host identifier in the file name because gdbm
234
 
     * files are CPU dependant, and we don't want things to go wrong
235
 
     * if we are on a multiarch system. */
236
 
 
237
 
    fn = pa_sprintf_malloc("card-database."CANONICAL_HOST".gdbm");
238
 
    fname = pa_state_path(fn, TRUE);
239
 
    pa_xfree(fn);
240
 
 
241
 
    if (!fname)
 
230
    if (!(fname = pa_state_path("card-database", TRUE)))
242
231
        goto fail;
243
232
 
244
 
    if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) {
245
 
        pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
 
233
    if (!(u->database = pa_database_open(fname, TRUE))) {
 
234
        pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
246
235
        pa_xfree(fname);
247
236
        goto fail;
248
237
    }
249
238
 
250
 
    /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */
251
 
    gdbm_cache_size = 10;
252
 
    gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size));
253
 
 
254
239
    pa_log_info("Sucessfully opened database file '%s'.", fname);
255
240
    pa_xfree(fname);
256
241
 
286
271
    if (u->save_time_event)
287
272
        u->core->mainloop->time_free(u->save_time_event);
288
273
 
289
 
    if (u->gdbm_file)
290
 
        gdbm_close(u->gdbm_file);
 
274
    if (u->database)
 
275
        pa_database_close(u->database);
291
276
 
292
277
    pa_xfree(u);
293
278
}