Merge dead branch 'glitch-free'
[profile/ivi/pulseaudio-panda.git] / src / modules / module-default-device-restore.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2006-2008 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28
29 #include <pulse/timeval.h>
30
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/module.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/namereg.h>
35 #include <pulsecore/core-error.h>
36
37 #include "module-default-device-restore-symdef.h"
38
39 PA_MODULE_AUTHOR("Lennart Poettering");
40 PA_MODULE_DESCRIPTION("Automatically restore the default sink and source");
41 PA_MODULE_VERSION(PACKAGE_VERSION);
42 PA_MODULE_LOAD_ONCE(TRUE);
43
44 #define DEFAULT_SINK_FILE "default-sink"
45 #define DEFAULT_SOURCE_FILE "default-source"
46 #define DEFAULT_SAVE_INTERVAL 5
47
48 struct userdata {
49     pa_core *core;
50     pa_subscription *subscription;
51     pa_time_event *time_event;
52     char *sink_filename, *source_filename;
53     pa_bool_t modified;
54 };
55
56 static void load(struct userdata *u) {
57     FILE *f;
58
59     /* We never overwrite manually configured settings */
60
61     if (u->core->default_sink_name)
62         pa_log_info("Manually configured default sink, not overwriting.");
63     else if ((f = fopen(u->sink_filename, "r"))) {
64         char ln[256] = "";
65
66         fgets(ln, sizeof(ln)-1, f);
67         pa_strip_nl(ln);
68         fclose(f);
69
70         if (!ln[0])
71             pa_log_info("No previous default sink setting, ignoring.");
72         else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SINK, TRUE)) {
73             pa_namereg_set_default(u->core, ln, PA_NAMEREG_SINK);
74             pa_log_info("Restored default sink '%s'.", ln);
75         } else
76             pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
77
78     } else if (errno != ENOENT)
79         pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
80
81     if (u->core->default_source_name)
82         pa_log_info("Manually configured default source, not overwriting.");
83     else if ((f = fopen(u->source_filename, "r"))) {
84         char ln[256] = "";
85
86         fgets(ln, sizeof(ln)-1, f);
87         pa_strip_nl(ln);
88         fclose(f);
89
90         if (!ln[0])
91             pa_log_info("No previous default source setting, ignoring.");
92         else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE, TRUE)) {
93             pa_namereg_set_default(u->core, ln, PA_NAMEREG_SOURCE);
94             pa_log_info("Restored default source '%s'.", ln);
95         } else
96             pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
97
98     } else if (errno != ENOENT)
99             pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
100 }
101
102 static void save(struct userdata *u) {
103     FILE *f;
104
105     if (!u->modified)
106         return;
107
108     if (u->sink_filename) {
109         if ((f = fopen(u->sink_filename, "w"))) {
110             const char *n = pa_namereg_get_default_sink_name(u->core);
111             fprintf(f, "%s\n", pa_strempty(n));
112             fclose(f);
113         } else
114             pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
115     }
116
117     if (u->source_filename) {
118         if ((f = fopen(u->source_filename, "w"))) {
119             const char *n = pa_namereg_get_default_source_name(u->core);
120             fprintf(f, "%s\n", pa_strempty(n));
121             fclose(f);
122         } else
123             pa_log("Failed to save default source: %s", pa_cstrerror(errno));
124     }
125
126     u->modified = FALSE;
127 }
128
129 static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
130     struct userdata *u = userdata;
131
132     pa_assert(u);
133     save(u);
134
135     if (u->time_event) {
136         u->core->mainloop->time_free(u->time_event);
137         u->time_event = NULL;
138     }
139 }
140
141 static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
142     struct userdata *u = userdata;
143
144     pa_assert(u);
145
146     u->modified = TRUE;
147
148     if (!u->time_event) {
149         struct timeval tv;
150         pa_gettimeofday(&tv);
151         pa_timeval_add(&tv, DEFAULT_SAVE_INTERVAL*PA_USEC_PER_SEC);
152         u->time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, time_cb, u);
153     }
154 }
155
156 int pa__init(pa_module *m) {
157     struct userdata *u;
158
159     pa_assert(m);
160
161     u = pa_xnew0(struct userdata, 1);
162     u->core = m->core;
163
164     if (!(u->sink_filename = pa_state_path(DEFAULT_SINK_FILE)))
165         goto fail;
166
167     if (!(u->source_filename = pa_state_path(DEFAULT_SOURCE_FILE)))
168         goto fail;
169
170     load(u);
171
172     u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
173
174     return 0;
175
176 fail:
177     pa__done(m);
178
179     return -1;
180 }
181
182 void pa__done(pa_module*m) {
183     struct userdata *u;
184
185     pa_assert(m);
186
187     if (!(u = m->userdata))
188         return;
189
190     save(u);
191
192     if (u->subscription)
193         pa_subscription_free(u->subscription);
194
195     if (u->time_event)
196         m->core->mainloop->time_free(u->time_event);
197
198     pa_xfree(u->sink_filename);
199     pa_xfree(u->source_filename);
200     pa_xfree(u);
201 }