actually add source code of module-default-device-restore
[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
39 #define DEFAULT_SINK_FILE "default-sink"
40 #define DEFAULT_SOURCE_FILE "default-source"
41
42 int pa__init(pa_module *m) {
43     FILE *f;
44
45     /* We never overwrite manually configured settings */
46     
47     if (m->core->default_sink_name)
48         pa_log_info("Manually configured default sink, not overwriting.");
49     else if ((f = pa_open_config_file(NULL, DEFAULT_SINK_FILE, NULL, NULL, "r"))) {
50         char ln[256] = "";
51         
52         fgets(ln, sizeof(ln)-1, f);
53         pa_strip_nl(ln);
54         fclose(f);
55         
56         if (!ln[0])
57             pa_log_debug("No previous default sink setting, ignoring.");
58         else if (pa_namereg_get(m->core, ln, PA_NAMEREG_SINK, 1)) {
59             pa_namereg_set_default(m->core, ln, PA_NAMEREG_SINK);
60             pa_log_debug("Restored default sink '%s'.", ln);
61         } else
62             pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
63     }
64     
65     if (m->core->default_source_name)
66         pa_log_info("Manually configured default source, not overwriting.");
67     else if ((f = pa_open_config_file(NULL, DEFAULT_SOURCE_FILE, NULL, NULL, "r"))) {
68         char ln[256] = "";
69         
70         fgets(ln, sizeof(ln)-1, f);
71         pa_strip_nl(ln);
72         fclose(f);
73
74         if (!ln[0])
75             pa_log_debug("No previous default source setting, ignoring.");
76         else if (pa_namereg_get(m->core, ln, PA_NAMEREG_SOURCE, 1)) {
77             pa_namereg_set_default(m->core, ln, PA_NAMEREG_SOURCE);
78             pa_log_debug("Restored default source '%s'.", ln);
79         } else
80             pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
81     }
82
83     return 0;
84 }
85
86 void pa__done(pa_module*m) {
87     FILE *f;
88
89     if ((f = pa_open_config_file(NULL, DEFAULT_SINK_FILE, NULL, NULL, "w"))) {
90         const char *n = pa_namereg_get_default_sink_name(m->core);
91         fprintf(f, "%s\n", n ? n : "");
92         fclose(f);
93     }    
94
95     if ((f = pa_open_config_file(NULL, DEFAULT_SOURCE_FILE, NULL, NULL, "w"))) {
96         const char *n = pa_namereg_get_default_source_name(m->core);
97         fprintf(f, "%s\n", n ? n : "");
98         fclose(f);
99     }    
100 }
101
102
103