2009-01-21 02:49:42 +01:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
|
|
|
|
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
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2009-01-21 02:49:42 +01:00
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2014-11-26 14:14:51 +01:00
|
|
|
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
2009-01-21 02:49:42 +01:00
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2011-06-13 15:04:33 +02:00
|
|
|
#include <pulse/gccmacro.h>
|
2009-01-21 02:49:42 +01:00
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
#include <pulse/timeval.h>
|
2009-04-05 02:13:43 +03:00
|
|
|
#include <pulse/rtclock.h>
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/module.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/modargs.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/core-subscribe.h>
|
|
|
|
|
#include <pulsecore/card.h>
|
|
|
|
|
#include <pulsecore/namereg.h>
|
2009-05-14 01:24:26 +02:00
|
|
|
#include <pulsecore/database.h>
|
2011-06-07 23:21:04 +01:00
|
|
|
#include <pulsecore/tagstruct.h>
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
#include "module-card-restore-symdef.h"
|
|
|
|
|
|
|
|
|
|
PA_MODULE_AUTHOR("Lennart Poettering");
|
|
|
|
|
PA_MODULE_DESCRIPTION("Automatically restore profile of cards");
|
|
|
|
|
PA_MODULE_VERSION(PACKAGE_VERSION);
|
2013-06-27 19:28:09 +02:00
|
|
|
PA_MODULE_LOAD_ONCE(true);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2009-04-05 02:13:43 +03:00
|
|
|
#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
static const char* const valid_modargs[] = {
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct userdata {
|
|
|
|
|
pa_core *core;
|
|
|
|
|
pa_module *module;
|
|
|
|
|
pa_time_event *save_time_event;
|
2009-05-14 01:24:26 +02:00
|
|
|
pa_database *database;
|
2009-01-21 02:49:42 +01:00
|
|
|
};
|
|
|
|
|
|
2012-06-27 22:55:35 +02:00
|
|
|
#define ENTRY_VERSION 2
|
|
|
|
|
|
|
|
|
|
struct port_info {
|
|
|
|
|
char *name;
|
|
|
|
|
int64_t offset;
|
|
|
|
|
};
|
2009-02-04 18:31:24 +01:00
|
|
|
|
2009-02-13 18:02:47 +01:00
|
|
|
struct entry {
|
2009-02-04 18:31:24 +01:00
|
|
|
uint8_t version;
|
2011-06-07 23:21:04 +01:00
|
|
|
char *profile;
|
2012-06-27 22:55:35 +02:00
|
|
|
pa_hashmap *ports; /* Port name -> struct port_info */
|
2011-06-07 23:21:04 +01:00
|
|
|
};
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2009-04-05 02:13:43 +03:00
|
|
|
static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
|
2009-01-21 02:49:42 +01:00
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(e);
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
|
|
|
|
|
pa_assert(e == u->save_time_event);
|
|
|
|
|
u->core->mainloop->time_free(u->save_time_event);
|
|
|
|
|
u->save_time_event = NULL;
|
|
|
|
|
|
2009-05-14 01:24:26 +02:00
|
|
|
pa_database_sync(u->database);
|
2009-01-21 02:49:42 +01:00
|
|
|
pa_log_info("Synced.");
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-08 00:47:19 +01:00
|
|
|
static void trigger_save(struct userdata *u) {
|
|
|
|
|
if (u->save_time_event)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 11:50:10 +05:30
|
|
|
static void port_info_free(struct port_info *p_info) {
|
|
|
|
|
pa_assert(p_info);
|
|
|
|
|
|
|
|
|
|
pa_xfree(p_info->name);
|
|
|
|
|
pa_xfree(p_info);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
static struct entry* entry_new(void) {
|
|
|
|
|
struct entry *r = pa_xnew0(struct entry, 1);
|
|
|
|
|
r->version = ENTRY_VERSION;
|
2013-09-14 11:50:10 +05:30
|
|
|
r->ports = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) port_info_free);
|
2011-06-07 23:21:04 +01:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
static struct port_info *port_info_new(pa_device_port *port) {
|
|
|
|
|
struct port_info *p_info;
|
|
|
|
|
|
|
|
|
|
if (port) {
|
|
|
|
|
p_info = pa_xnew(struct port_info, 1);
|
|
|
|
|
p_info->name = pa_xstrdup(port->name);
|
|
|
|
|
p_info->offset = port->latency_offset;
|
|
|
|
|
} else
|
|
|
|
|
p_info = pa_xnew0(struct port_info, 1);
|
|
|
|
|
|
|
|
|
|
return p_info;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
static void entry_free(struct entry* e) {
|
|
|
|
|
pa_assert(e);
|
|
|
|
|
|
|
|
|
|
pa_xfree(e->profile);
|
2013-09-14 11:50:10 +05:30
|
|
|
pa_hashmap_free(e->ports);
|
2012-06-27 22:55:35 +02:00
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
pa_xfree(e);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
static struct entry *entry_from_card(pa_card *card) {
|
|
|
|
|
struct port_info *p_info;
|
|
|
|
|
struct entry *entry;
|
|
|
|
|
pa_device_port *port;
|
|
|
|
|
void *state;
|
|
|
|
|
|
|
|
|
|
pa_assert(card);
|
|
|
|
|
|
|
|
|
|
entry = entry_new();
|
|
|
|
|
if (card->save_profile)
|
|
|
|
|
entry->profile = pa_xstrdup(card->active_profile->name);
|
|
|
|
|
|
|
|
|
|
PA_HASHMAP_FOREACH(port, card->ports, state) {
|
|
|
|
|
p_info = port_info_new(port);
|
|
|
|
|
pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool entrys_equal(struct entry *a, struct entry *b) {
|
|
|
|
|
struct port_info *Ap_info, *Bp_info;
|
|
|
|
|
void *state;
|
|
|
|
|
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
if (!pa_streq(a->profile, b->profile) ||
|
|
|
|
|
pa_hashmap_size(a->ports) != pa_hashmap_size(b->ports))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
PA_HASHMAP_FOREACH(Ap_info, a->ports, state) {
|
|
|
|
|
if ((Bp_info = pa_hashmap_get(b->ports, Ap_info->name))) {
|
|
|
|
|
if (Ap_info->offset != Bp_info->offset)
|
|
|
|
|
return false;
|
|
|
|
|
} else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
static bool entry_write(struct userdata *u, const char *name, const struct entry *e) {
|
2011-06-08 00:47:19 +01:00
|
|
|
pa_tagstruct *t;
|
|
|
|
|
pa_datum key, data;
|
2013-06-27 19:28:09 +02:00
|
|
|
bool r;
|
2012-06-27 22:55:35 +02:00
|
|
|
void *state;
|
|
|
|
|
struct port_info *p_info;
|
2011-06-08 00:47:19 +01:00
|
|
|
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
pa_assert(name);
|
|
|
|
|
pa_assert(e);
|
|
|
|
|
|
2014-10-23 16:09:45 +02:00
|
|
|
t = pa_tagstruct_new();
|
2011-06-08 00:47:19 +01:00
|
|
|
pa_tagstruct_putu8(t, e->version);
|
|
|
|
|
pa_tagstruct_puts(t, e->profile);
|
2012-06-27 22:55:35 +02:00
|
|
|
pa_tagstruct_putu32(t, pa_hashmap_size(e->ports));
|
|
|
|
|
|
|
|
|
|
PA_HASHMAP_FOREACH(p_info, e->ports, state) {
|
|
|
|
|
pa_tagstruct_puts(t, p_info->name);
|
|
|
|
|
pa_tagstruct_puts64(t, p_info->offset);
|
|
|
|
|
}
|
2011-06-08 00:47:19 +01:00
|
|
|
|
|
|
|
|
key.data = (char *) name;
|
|
|
|
|
key.size = strlen(name);
|
|
|
|
|
|
|
|
|
|
data.data = (void*)pa_tagstruct_data(t, &data.size);
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
r = (pa_database_set(u->database, &key, &data, true) == 0);
|
2011-06-08 00:47:19 +01:00
|
|
|
|
|
|
|
|
pa_tagstruct_free(t);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
|
|
|
|
|
|
|
|
|
|
#define LEGACY_ENTRY_VERSION 1
|
|
|
|
|
static struct entry* legacy_entry_read(struct userdata *u, pa_datum *data) {
|
|
|
|
|
struct legacy_entry {
|
|
|
|
|
uint8_t version;
|
|
|
|
|
char profile[PA_NAME_MAX];
|
|
|
|
|
} PA_GCC_PACKED ;
|
|
|
|
|
struct legacy_entry *le;
|
|
|
|
|
struct entry *e;
|
|
|
|
|
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
if (data->size != sizeof(struct legacy_entry)) {
|
|
|
|
|
pa_log_debug("Size does not match.");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
le = (struct legacy_entry*)data->data;
|
|
|
|
|
|
|
|
|
|
if (le->version != LEGACY_ENTRY_VERSION) {
|
|
|
|
|
pa_log_debug("Version mismatch.");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!memchr(le->profile, 0, sizeof(le->profile))) {
|
|
|
|
|
pa_log_warn("Profile has missing NUL byte.");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e = entry_new();
|
|
|
|
|
e->profile = pa_xstrdup(le->profile);
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
static struct entry* entry_read(struct userdata *u, const char *name) {
|
2009-05-14 01:24:26 +02:00
|
|
|
pa_datum key, data;
|
2011-06-07 23:21:04 +01:00
|
|
|
struct entry *e = NULL;
|
|
|
|
|
pa_tagstruct *t = NULL;
|
|
|
|
|
const char* profile;
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
pa_assert(name);
|
|
|
|
|
|
2009-05-14 01:24:26 +02:00
|
|
|
key.data = (char*) name;
|
|
|
|
|
key.size = strlen(name);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2009-05-14 01:24:26 +02:00
|
|
|
pa_zero(data);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2014-10-20 16:19:36 +02:00
|
|
|
if (!pa_database_get(u->database, &key, &data)) {
|
|
|
|
|
pa_log_debug("Database contains no data for key: %s", name);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2014-10-23 16:09:45 +02:00
|
|
|
t = pa_tagstruct_new_fixed(data.data, data.size);
|
2011-06-07 23:21:04 +01:00
|
|
|
e = entry_new();
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
if (pa_tagstruct_getu8(t, &e->version) < 0 ||
|
|
|
|
|
e->version > ENTRY_VERSION ||
|
|
|
|
|
pa_tagstruct_gets(t, &profile) < 0) {
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2009-02-04 18:31:24 +01:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-30 13:00:25 +03:00
|
|
|
if (!profile)
|
|
|
|
|
profile = "";
|
|
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
e->profile = pa_xstrdup(profile);
|
|
|
|
|
|
2012-06-27 22:55:35 +02:00
|
|
|
if (e->version >= 2) {
|
|
|
|
|
uint32_t port_count = 0;
|
|
|
|
|
const char *port_name = NULL;
|
|
|
|
|
int64_t port_offset = 0;
|
|
|
|
|
struct port_info *p_info;
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
if (pa_tagstruct_getu32(t, &port_count) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < port_count; i++) {
|
|
|
|
|
if (pa_tagstruct_gets(t, &port_name) < 0 ||
|
|
|
|
|
!port_name ||
|
|
|
|
|
pa_hashmap_get(e->ports, port_name) ||
|
|
|
|
|
pa_tagstruct_gets64(t, &port_offset) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
p_info = port_info_new(NULL);
|
2012-06-27 22:55:35 +02:00
|
|
|
p_info->name = pa_xstrdup(port_name);
|
|
|
|
|
p_info->offset = port_offset;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(pa_hashmap_put(e->ports, p_info->name, p_info) >= 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
if (!pa_tagstruct_eof(t))
|
2009-01-21 02:49:42 +01:00
|
|
|
goto fail;
|
2011-06-07 23:21:04 +01:00
|
|
|
|
|
|
|
|
pa_tagstruct_free(t);
|
|
|
|
|
pa_datum_free(&data);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
return e;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
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);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2011-06-08 00:47:19 +01:00
|
|
|
#ifdef ENABLE_LEGACY_DATABASE_ENTRY_FORMAT
|
|
|
|
|
pa_log_debug("Attempting to load legacy (pre-v1.0) data for key: %s", name);
|
|
|
|
|
if ((e = legacy_entry_read(u, &data))) {
|
|
|
|
|
pa_log_debug("Success. Saving new format for key: %s", name);
|
|
|
|
|
if (entry_write(u, name, e))
|
|
|
|
|
trigger_save(u);
|
|
|
|
|
pa_datum_free(&data);
|
|
|
|
|
return e;
|
|
|
|
|
} else
|
|
|
|
|
pa_log_debug("Unable to load legacy (pre-v1.0) data for key: %s. Ignoring.", name);
|
|
|
|
|
#endif
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2011-06-08 00:47:19 +01:00
|
|
|
pa_datum_free(&data);
|
|
|
|
|
return NULL;
|
2009-01-21 02:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
static void show_full_info(pa_card *card) {
|
|
|
|
|
pa_assert(card);
|
|
|
|
|
|
|
|
|
|
if (card->save_profile)
|
|
|
|
|
pa_log_info("Storing profile and port latency offsets for card %s.", card->name);
|
|
|
|
|
else
|
|
|
|
|
pa_log_info("Storing port latency offsets for card %s.", card->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static pa_hook_result_t card_put_hook_callback(pa_core *c, pa_card *card, struct userdata *u) {
|
2011-06-07 23:21:04 +01:00
|
|
|
struct entry *entry, *old;
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
pa_assert(card);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
entry = entry_from_card(card);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
if ((old = entry_read(u, card->name))) {
|
|
|
|
|
if (!card->save_profile)
|
|
|
|
|
entry->profile = pa_xstrdup(old->profile);
|
|
|
|
|
if (entrys_equal(entry, old))
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
show_full_info(card);
|
2012-06-27 22:55:35 +02:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
if (entry_write(u, card->name, entry))
|
|
|
|
|
trigger_save(u);
|
2012-06-27 22:55:35 +02:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
finish:
|
|
|
|
|
entry_free(entry);
|
|
|
|
|
if (old)
|
|
|
|
|
entry_free(old);
|
2012-06-27 22:55:35 +02:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 16:39:01 +02:00
|
|
|
static pa_hook_result_t card_profile_changed_callback(pa_core *c, pa_card *card, struct userdata *u) {
|
2013-01-15 23:51:30 +01:00
|
|
|
struct entry *entry;
|
|
|
|
|
|
|
|
|
|
pa_assert(card);
|
|
|
|
|
|
|
|
|
|
if (!card->save_profile)
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
|
|
|
|
|
if ((entry = entry_read(u, card->name))) {
|
2014-01-29 20:54:39 +02:00
|
|
|
pa_xfree(entry->profile);
|
2013-01-15 23:51:30 +01:00
|
|
|
entry->profile = pa_xstrdup(card->active_profile->name);
|
|
|
|
|
pa_log_info("Storing card profile for card %s.", card->name);
|
|
|
|
|
} else {
|
|
|
|
|
entry = entry_from_card(card);
|
|
|
|
|
show_full_info(card);
|
2012-06-27 22:55:35 +02:00
|
|
|
}
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
if (entry_write(u, card->name, entry))
|
|
|
|
|
trigger_save(u);
|
2012-06-27 22:55:35 +02:00
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
entry_free(entry);
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 16:39:01 +02:00
|
|
|
static pa_hook_result_t card_profile_added_callback(pa_core *c, pa_card_profile *profile, struct userdata *u) {
|
|
|
|
|
struct entry *entry;
|
|
|
|
|
|
|
|
|
|
pa_assert(profile);
|
|
|
|
|
|
card-restore: Fix profile restoring with bluetooth
The bluetooth card is created when the first profile becomes
available, which means that the card may have profiles that are not
available when the card is initialized. If module-card-restore tries
to restore such profile, that will fail, and the card will be
initialized with the "off" profile active.
This patch modifies module-card-restore so that if follows the profile
availability status, and when the saved profile becomes available, it
is activated. Additionally, module-card-restore is modified so that it
doesn't even try to restore unavailable profiles, when the necessary
information is available. In practice there are two existing places
where the profile is restored, and only one of those contexts has the
necessary information available. Unfortunately, it's the more
important context (card creation) where the information is not
available. This means that module-card-restore will set the initial
profile of a new card even if the profile is unavailable, and this
will cause an ugly warning in the log, even though there's nothing
abnormal happening.
BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=87081
2014-12-16 13:14:41 +02:00
|
|
|
if (profile->available == PA_AVAILABLE_NO)
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
|
2013-07-25 16:39:01 +02:00
|
|
|
if (!(entry = entry_read(u, profile->card->name)))
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
|
|
|
|
|
if (pa_safe_streq(entry->profile, profile->name)) {
|
2013-11-20 15:42:26 +02:00
|
|
|
if (pa_card_set_profile(profile->card, profile, true) >= 0)
|
2013-07-25 16:39:01 +02:00
|
|
|
pa_log_info("Restored profile '%s' for card %s.", profile->name, profile->card->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry_free(entry);
|
|
|
|
|
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
}
|
|
|
|
|
|
card-restore: Fix profile restoring with bluetooth
The bluetooth card is created when the first profile becomes
available, which means that the card may have profiles that are not
available when the card is initialized. If module-card-restore tries
to restore such profile, that will fail, and the card will be
initialized with the "off" profile active.
This patch modifies module-card-restore so that if follows the profile
availability status, and when the saved profile becomes available, it
is activated. Additionally, module-card-restore is modified so that it
doesn't even try to restore unavailable profiles, when the necessary
information is available. In practice there are two existing places
where the profile is restored, and only one of those contexts has the
necessary information available. Unfortunately, it's the more
important context (card creation) where the information is not
available. This means that module-card-restore will set the initial
profile of a new card even if the profile is unavailable, and this
will cause an ugly warning in the log, even though there's nothing
abnormal happening.
BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=87081
2014-12-16 13:14:41 +02:00
|
|
|
static pa_hook_result_t profile_available_changed_callback(void *hook_data, void *call_data, void *userdata) {
|
|
|
|
|
pa_card_profile *profile = call_data;
|
|
|
|
|
pa_card *card;
|
|
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
struct entry *entry;
|
|
|
|
|
|
|
|
|
|
pa_assert(profile);
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
|
|
|
|
|
card = profile->card;
|
|
|
|
|
|
|
|
|
|
if (profile->available == PA_AVAILABLE_NO)
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
|
|
|
|
|
entry = entry_read(u, card->name);
|
|
|
|
|
if (!entry)
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
|
|
|
|
|
if (!pa_streq(profile->name, entry->profile))
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
|
|
|
|
|
pa_log_info("Card %s profile %s became available, activating.", card->name, profile->name);
|
|
|
|
|
pa_card_set_profile(profile->card, profile, true);
|
|
|
|
|
|
|
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 23:51:30 +01:00
|
|
|
static pa_hook_result_t port_offset_change_callback(pa_core *c, pa_device_port *port, struct userdata *u) {
|
|
|
|
|
struct entry *entry;
|
|
|
|
|
pa_card *card;
|
|
|
|
|
|
|
|
|
|
pa_assert(port);
|
|
|
|
|
card = port->card;
|
|
|
|
|
|
|
|
|
|
if ((entry = entry_read(u, card->name))) {
|
|
|
|
|
struct port_info *p_info;
|
|
|
|
|
|
|
|
|
|
if ((p_info = pa_hashmap_get(entry->ports, port->name)))
|
|
|
|
|
p_info->offset = port->latency_offset;
|
|
|
|
|
else {
|
|
|
|
|
p_info = port_info_new(port);
|
|
|
|
|
pa_assert_se(pa_hashmap_put(entry->ports, p_info->name, p_info) >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_log_info("Storing latency offset for port %s on card %s.", port->name, card->name);
|
|
|
|
|
|
|
|
|
|
} else {
|
2013-12-16 17:00:24 +01:00
|
|
|
entry = entry_from_card(card);
|
2013-01-15 23:51:30 +01:00
|
|
|
show_full_info(card);
|
|
|
|
|
}
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
if (entry_write(u, card->name, entry))
|
|
|
|
|
trigger_save(u);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2011-06-07 23:21:04 +01:00
|
|
|
entry_free(entry);
|
2013-01-15 23:51:30 +01:00
|
|
|
return PA_HOOK_OK;
|
2009-01-21 02:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, struct userdata *u) {
|
|
|
|
|
struct entry *e;
|
2012-06-27 22:55:35 +02:00
|
|
|
void *state;
|
|
|
|
|
pa_device_port *p;
|
|
|
|
|
struct port_info *p_info;
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
pa_assert(new_data);
|
|
|
|
|
|
2012-06-27 22:55:35 +02:00
|
|
|
if (!(e = entry_read(u, new_data->name)))
|
|
|
|
|
return PA_HOOK_OK;
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2012-06-27 22:55:35 +02:00
|
|
|
if (e->profile[0]) {
|
2009-01-21 02:49:42 +01:00
|
|
|
if (!new_data->active_profile) {
|
2009-06-18 00:58:19 +02:00
|
|
|
pa_card_new_data_set_profile(new_data, e->profile);
|
2012-11-19 12:59:31 +02:00
|
|
|
pa_log_info("Restored profile '%s' for card %s.", new_data->active_profile, new_data->name);
|
2013-06-27 19:28:09 +02:00
|
|
|
new_data->save_profile = true;
|
2012-06-27 22:55:35 +02:00
|
|
|
|
2009-01-21 02:49:42 +01:00
|
|
|
} else
|
|
|
|
|
pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-27 22:55:35 +02:00
|
|
|
/* Always restore the latency offsets because their
|
|
|
|
|
* initial value is always 0 */
|
|
|
|
|
|
|
|
|
|
pa_log_info("Restoring port latency offsets for card %s.", new_data->name);
|
|
|
|
|
|
|
|
|
|
PA_HASHMAP_FOREACH(p_info, e->ports, state)
|
|
|
|
|
if ((p = pa_hashmap_get(new_data->ports, p_info->name)))
|
|
|
|
|
p->latency_offset = p_info->offset;
|
|
|
|
|
|
|
|
|
|
entry_free(e);
|
|
|
|
|
|
2009-01-21 02:49:42 +01:00
|
|
|
return PA_HOOK_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa__init(pa_module*m) {
|
|
|
|
|
pa_modargs *ma = NULL;
|
|
|
|
|
struct userdata *u;
|
2009-05-14 01:24:26 +02:00
|
|
|
char *fname;
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
|
|
|
|
|
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
|
|
|
|
|
pa_log("Failed to parse module arguments");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-18 00:59:33 +02:00
|
|
|
m->userdata = u = pa_xnew0(struct userdata, 1);
|
2009-01-21 02:49:42 +01:00
|
|
|
u->core = m->core;
|
|
|
|
|
u->module = m;
|
|
|
|
|
|
2015-03-27 11:20:12 +01:00
|
|
|
pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u);
|
|
|
|
|
pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) card_put_hook_callback, u);
|
|
|
|
|
pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) card_profile_changed_callback, u);
|
|
|
|
|
pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], PA_HOOK_NORMAL, (pa_hook_cb_t) card_profile_added_callback, u);
|
|
|
|
|
pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED],
|
card-restore: Fix profile restoring with bluetooth
The bluetooth card is created when the first profile becomes
available, which means that the card may have profiles that are not
available when the card is initialized. If module-card-restore tries
to restore such profile, that will fail, and the card will be
initialized with the "off" profile active.
This patch modifies module-card-restore so that if follows the profile
availability status, and when the saved profile becomes available, it
is activated. Additionally, module-card-restore is modified so that it
doesn't even try to restore unavailable profiles, when the necessary
information is available. In practice there are two existing places
where the profile is restored, and only one of those contexts has the
necessary information available. Unfortunately, it's the more
important context (card creation) where the information is not
available. This means that module-card-restore will set the initial
profile of a new card even if the profile is unavailable, and this
will cause an ugly warning in the log, even though there's nothing
abnormal happening.
BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=87081
2014-12-16 13:14:41 +02:00
|
|
|
PA_HOOK_NORMAL, profile_available_changed_callback, u);
|
2015-03-27 11:20:12 +01:00
|
|
|
pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) port_offset_change_callback, u);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
if (!(fname = pa_state_path("card-database", true)))
|
2009-01-21 02:49:42 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
if (!(u->database = pa_database_open(fname, true))) {
|
2009-05-14 01:24:26 +02:00
|
|
|
pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno));
|
2009-01-21 02:49:42 +01:00
|
|
|
pa_xfree(fname);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-20 17:20:57 +08:00
|
|
|
pa_log_info("Successfully opened database file '%s'.", fname);
|
2009-01-21 02:49:42 +01:00
|
|
|
pa_xfree(fname);
|
|
|
|
|
|
|
|
|
|
pa_modargs_free(ma);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa__done(m);
|
|
|
|
|
|
|
|
|
|
if (ma)
|
|
|
|
|
pa_modargs_free(ma);
|
|
|
|
|
|
2011-03-12 19:45:02 +01:00
|
|
|
return -1;
|
2009-01-21 02:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa__done(pa_module*m) {
|
|
|
|
|
struct userdata* u;
|
|
|
|
|
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
|
|
|
|
|
if (!(u = m->userdata))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (u->save_time_event)
|
|
|
|
|
u->core->mainloop->time_free(u->save_time_event);
|
|
|
|
|
|
2009-05-14 01:24:26 +02:00
|
|
|
if (u->database)
|
|
|
|
|
pa_database_close(u->database);
|
2009-01-21 02:49:42 +01:00
|
|
|
|
|
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|