Use LGPL 2.1 on all files previously using LGPL 2
[platform/upstream/pulseaudio.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.1 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 #include <pulse/util.h>
31
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/namereg.h>
36 #include <pulsecore/core-error.h>
37
38 #include "module-default-device-restore-symdef.h"
39
40 PA_MODULE_AUTHOR("Lennart Poettering");
41 PA_MODULE_DESCRIPTION("Automatically restore the default sink and source");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_LOAD_ONCE(TRUE);
44
45 #define DEFAULT_SAVE_INTERVAL 5
46
47 struct userdata {
48     pa_core *core;
49     pa_subscription *subscription;
50     pa_time_event *time_event;
51     char *sink_filename, *source_filename;
52     pa_bool_t modified;
53 };
54
55 static void load(struct userdata *u) {
56     FILE *f;
57
58     /* We never overwrite manually configured settings */
59
60     if (u->core->default_sink)
61         pa_log_info("Manually configured default sink, not overwriting.");
62     else if ((f = fopen(u->sink_filename, "r"))) {
63         char ln[256] = "";
64         pa_sink *s;
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 ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SINK))) {
73             pa_namereg_set_default_sink(u->core, s);
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)
82         pa_log_info("Manually configured default source, not overwriting.");
83     else if ((f = fopen(u->source_filename, "r"))) {
84         char ln[256] = "";
85         pa_source *s;
86
87         fgets(ln, sizeof(ln)-1, f);
88         pa_strip_nl(ln);
89         fclose(f);
90
91         if (!ln[0])
92             pa_log_info("No previous default source setting, ignoring.");
93         else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE))) {
94             pa_namereg_set_default_source(u->core, s);
95             pa_log_info("Restored default source '%s'.", ln);
96         } else
97             pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
98
99     } else if (errno != ENOENT)
100             pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
101 }
102
103 static void save(struct userdata *u) {
104     FILE *f;
105
106     if (!u->modified)
107         return;
108
109     if (u->sink_filename) {
110         if ((f = fopen(u->sink_filename, "w"))) {
111             pa_sink *s = pa_namereg_get_default_sink(u->core);
112             fprintf(f, "%s\n", s ? s->name : "");
113             fclose(f);
114         } else
115             pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
116     }
117
118     if (u->source_filename) {
119         if ((f = fopen(u->source_filename, "w"))) {
120             pa_source *s = pa_namereg_get_default_source(u->core);
121             fprintf(f, "%s\n", s ? s->name : "");
122             fclose(f);
123         } else
124             pa_log("Failed to save default source: %s", pa_cstrerror(errno));
125     }
126
127     u->modified = FALSE;
128 }
129
130 static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
131     struct userdata *u = userdata;
132
133     pa_assert(u);
134     save(u);
135
136     if (u->time_event) {
137         u->core->mainloop->time_free(u->time_event);
138         u->time_event = NULL;
139     }
140 }
141
142 static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
143     struct userdata *u = userdata;
144
145     pa_assert(u);
146
147     u->modified = TRUE;
148
149     if (!u->time_event) {
150         struct timeval tv;
151         pa_gettimeofday(&tv);
152         pa_timeval_add(&tv, DEFAULT_SAVE_INTERVAL*PA_USEC_PER_SEC);
153         u->time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, time_cb, u);
154     }
155 }
156
157 int pa__init(pa_module *m) {
158     struct userdata *u;
159
160     pa_assert(m);
161
162     m->userdata = u = pa_xnew0(struct userdata, 1);
163     u->core = m->core;
164
165     if (!(u->sink_filename = pa_state_path("default-sink", TRUE)))
166         goto fail;
167
168     if (!(u->source_filename = pa_state_path("default-source", TRUE)))
169         goto fail;
170
171     load(u);
172
173     u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
174
175     return 0;
176
177 fail:
178     pa__done(m);
179
180     return -1;
181 }
182
183 void pa__done(pa_module*m) {
184     struct userdata *u;
185
186     pa_assert(m);
187
188     if (!(u = m->userdata))
189         return;
190
191     save(u);
192
193     if (u->subscription)
194         pa_subscription_free(u->subscription);
195
196     if (u->time_event)
197         m->core->mainloop->time_free(u->time_event);
198
199     pa_xfree(u->sink_filename);
200     pa_xfree(u->source_filename);
201     pa_xfree(u);
202 }