tag modules that may only be loaded once at most especially, and enforce that in...
[platform/upstream/pulseaudio.git] / src / modules / module-default-device-restore.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/module.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/namereg.h>
32
33 #include "module-default-device-restore-symdef.h"
34
35 PA_MODULE_AUTHOR("Lennart Poettering");
36 PA_MODULE_DESCRIPTION("Automatically restore the default sink and source");
37 PA_MODULE_VERSION(PACKAGE_VERSION);
38 PA_MODULE_LOAD_ONCE(TRUE);
39
40 #define DEFAULT_SINK_FILE "default-sink"
41 #define DEFAULT_SOURCE_FILE "default-source"
42
43 int pa__init(pa_module *m) {
44     FILE *f;
45
46     /* We never overwrite manually configured settings */
47
48     if (m->core->default_sink_name)
49         pa_log_info("Manually configured default sink, not overwriting.");
50     else if ((f = pa_open_config_file(NULL, DEFAULT_SINK_FILE, NULL, NULL, "r"))) {
51         char ln[256] = "";
52
53         fgets(ln, sizeof(ln)-1, f);
54         pa_strip_nl(ln);
55         fclose(f);
56
57         if (!ln[0])
58             pa_log_debug("No previous default sink setting, ignoring.");
59         else if (pa_namereg_get(m->core, ln, PA_NAMEREG_SINK, 1)) {
60             pa_namereg_set_default(m->core, ln, PA_NAMEREG_SINK);
61             pa_log_debug("Restored default sink '%s'.", ln);
62         } else
63             pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
64     }
65
66     if (m->core->default_source_name)
67         pa_log_info("Manually configured default source, not overwriting.");
68     else if ((f = pa_open_config_file(NULL, DEFAULT_SOURCE_FILE, NULL, NULL, "r"))) {
69         char ln[256] = "";
70
71         fgets(ln, sizeof(ln)-1, f);
72         pa_strip_nl(ln);
73         fclose(f);
74
75         if (!ln[0])
76             pa_log_debug("No previous default source setting, ignoring.");
77         else if (pa_namereg_get(m->core, ln, PA_NAMEREG_SOURCE, 1)) {
78             pa_namereg_set_default(m->core, ln, PA_NAMEREG_SOURCE);
79             pa_log_debug("Restored default source '%s'.", ln);
80         } else
81             pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
82     }
83
84     return 0;
85 }
86
87 void pa__done(pa_module*m) {
88     FILE *f;
89
90     if ((f = pa_open_config_file(NULL, DEFAULT_SINK_FILE, NULL, NULL, "w"))) {
91         const char *n = pa_namereg_get_default_sink_name(m->core);
92         fprintf(f, "%s\n", n ? n : "");
93         fclose(f);
94     }
95
96     if ((f = pa_open_config_file(NULL, DEFAULT_SOURCE_FILE, NULL, NULL, "w"))) {
97         const char *n = pa_namereg_get_default_source_name(m->core);
98         fprintf(f, "%s\n", n ? n : "");
99         fclose(f);
100     }
101 }