2007-10-28 19:13:50 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
Copyright 2006-2008 Lennart Poettering
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
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
|
|
|
|
|
along with PulseAudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include <pulse/timeval.h>
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/module.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/namereg.h>
|
2008-05-15 23:34:41 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
#include "module-default-device-restore-symdef.h"
|
|
|
|
|
|
2007-11-09 18:25:40 +00:00
|
|
|
PA_MODULE_AUTHOR("Lennart Poettering");
|
|
|
|
|
PA_MODULE_DESCRIPTION("Automatically restore the default sink and source");
|
|
|
|
|
PA_MODULE_VERSION(PACKAGE_VERSION);
|
|
|
|
|
PA_MODULE_LOAD_ONCE(TRUE);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
#define DEFAULT_SINK_FILE "default-sink"
|
|
|
|
|
#define DEFAULT_SOURCE_FILE "default-source"
|
2008-05-15 23:34:41 +00:00
|
|
|
#define DEFAULT_SAVE_INTERVAL 5
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
struct userdata {
|
|
|
|
|
pa_core *core;
|
|
|
|
|
pa_subscription *subscription;
|
|
|
|
|
pa_time_event *time_event;
|
|
|
|
|
char *sink_filename, *source_filename;
|
|
|
|
|
pa_bool_t modified;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void load(struct userdata *u) {
|
2007-10-28 19:13:50 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
/* We never overwrite manually configured settings */
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (u->core->default_sink_name)
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_info("Manually configured default sink, not overwriting.");
|
2008-05-15 23:34:41 +00:00
|
|
|
else if ((f = fopen(u->sink_filename, "r"))) {
|
2007-10-28 19:13:50 +00:00
|
|
|
char ln[256] = "";
|
|
|
|
|
|
|
|
|
|
fgets(ln, sizeof(ln)-1, f);
|
|
|
|
|
pa_strip_nl(ln);
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
if (!ln[0])
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_log_info("No previous default sink setting, ignoring.");
|
|
|
|
|
else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SINK, TRUE)) {
|
|
|
|
|
pa_namereg_set_default(u->core, ln, PA_NAMEREG_SINK);
|
|
|
|
|
pa_log_info("Restored default sink '%s'.", ln);
|
2007-10-28 19:13:50 +00:00
|
|
|
} else
|
|
|
|
|
pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
} else if (errno != ENOENT)
|
|
|
|
|
pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
|
|
|
|
|
|
|
|
|
|
if (u->core->default_source_name)
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_info("Manually configured default source, not overwriting.");
|
2008-05-15 23:34:41 +00:00
|
|
|
else if ((f = fopen(u->source_filename, "r"))) {
|
2007-10-28 19:13:50 +00:00
|
|
|
char ln[256] = "";
|
|
|
|
|
|
|
|
|
|
fgets(ln, sizeof(ln)-1, f);
|
|
|
|
|
pa_strip_nl(ln);
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
if (!ln[0])
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_log_info("No previous default source setting, ignoring.");
|
|
|
|
|
else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE, TRUE)) {
|
|
|
|
|
pa_namereg_set_default(u->core, ln, PA_NAMEREG_SOURCE);
|
|
|
|
|
pa_log_info("Restored default source '%s'.", ln);
|
2007-10-28 19:13:50 +00:00
|
|
|
} else
|
|
|
|
|
pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
} else if (errno != ENOENT)
|
|
|
|
|
pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
static void save(struct userdata *u) {
|
2007-10-28 19:13:50 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (!u->modified)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (u->sink_filename) {
|
|
|
|
|
if ((f = fopen(u->sink_filename, "w"))) {
|
|
|
|
|
const char *n = pa_namereg_get_default_sink_name(u->core);
|
|
|
|
|
fprintf(f, "%s\n", pa_strempty(n));
|
|
|
|
|
fclose(f);
|
|
|
|
|
} else
|
|
|
|
|
pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (u->source_filename) {
|
|
|
|
|
if ((f = fopen(u->source_filename, "w"))) {
|
|
|
|
|
const char *n = pa_namereg_get_default_source_name(u->core);
|
|
|
|
|
fprintf(f, "%s\n", pa_strempty(n));
|
|
|
|
|
fclose(f);
|
|
|
|
|
} else
|
|
|
|
|
pa_log("Failed to save default source: %s", pa_cstrerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u->modified = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
|
|
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
save(u);
|
|
|
|
|
|
|
|
|
|
if (u->time_event) {
|
|
|
|
|
u->core->mainloop->time_free(u->time_event);
|
|
|
|
|
u->time_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
|
|
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
|
|
|
|
|
u->modified = TRUE;
|
|
|
|
|
|
|
|
|
|
if (!u->time_event) {
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
pa_gettimeofday(&tv);
|
|
|
|
|
pa_timeval_add(&tv, DEFAULT_SAVE_INTERVAL*PA_USEC_PER_SEC);
|
|
|
|
|
u->time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, time_cb, u);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
int pa__init(pa_module *m) {
|
|
|
|
|
struct userdata *u;
|
|
|
|
|
|
2008-05-18 19:06:31 +00:00
|
|
|
pa_assert(m);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
u = pa_xnew0(struct userdata, 1);
|
|
|
|
|
u->core = m->core;
|
|
|
|
|
|
|
|
|
|
if (!(u->sink_filename = pa_runtime_path(DEFAULT_SINK_FILE)))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
if (!(u->source_filename = pa_runtime_path(DEFAULT_SOURCE_FILE)))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
load(u);
|
|
|
|
|
|
|
|
|
|
u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa__done(m);
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa__done(pa_module*m) {
|
|
|
|
|
struct userdata *u;
|
|
|
|
|
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
|
|
|
|
|
if (!(u = m->userdata))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
save(u);
|
|
|
|
|
|
|
|
|
|
if (u->subscription)
|
|
|
|
|
pa_subscription_free(u->subscription);
|
|
|
|
|
|
|
|
|
|
if (u->time_event)
|
|
|
|
|
m->core->mainloop->time_free(u->time_event);
|
|
|
|
|
|
|
|
|
|
pa_xfree(u->sink_filename);
|
|
|
|
|
pa_xfree(u->source_filename);
|
|
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|