build-sys: Use ax_check_flag macros from autoconf archive
[pulseaudio-mirror.git] / src / modules / module-default-device-restore.c
blobf28bddb7d80b3b0786af5b7013315385e6274721
1 /***
2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <errno.h>
27 #include <stdio.h>
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/module.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-error.h>
39 #include "module-default-device-restore-symdef.h"
41 PA_MODULE_AUTHOR("Lennart Poettering");
42 PA_MODULE_DESCRIPTION("Automatically restore the default sink and source");
43 PA_MODULE_VERSION(PACKAGE_VERSION);
44 PA_MODULE_LOAD_ONCE(TRUE);
46 #define SAVE_INTERVAL (5 * PA_USEC_PER_SEC)
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;
56 static void load(struct userdata *u) {
57 FILE *f;
59 /* We never overwrite manually configured settings */
61 if (u->core->default_sink)
62 pa_log_info("Manually configured default sink, not overwriting.");
63 else if ((f = pa_fopen_cloexec(u->sink_filename, "r"))) {
64 char ln[256] = "";
65 pa_sink *s;
67 if (fgets(ln, sizeof(ln)-1, f))
68 pa_strip_nl(ln);
69 fclose(f);
71 if (!ln[0])
72 pa_log_info("No previous default sink setting, ignoring.");
73 else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SINK))) {
74 pa_namereg_set_default_sink(u->core, s);
75 pa_log_info("Restored default sink '%s'.", ln);
76 } else
77 pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
79 } else if (errno != ENOENT)
80 pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
82 if (u->core->default_source)
83 pa_log_info("Manually configured default source, not overwriting.");
84 else if ((f = pa_fopen_cloexec(u->source_filename, "r"))) {
85 char ln[256] = "";
86 pa_source *s;
88 if (fgets(ln, sizeof(ln)-1, f))
89 pa_strip_nl(ln);
90 fclose(f);
92 if (!ln[0])
93 pa_log_info("No previous default source setting, ignoring.");
94 else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE))) {
95 pa_namereg_set_default_source(u->core, s);
96 pa_log_info("Restored default source '%s'.", ln);
97 } else
98 pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
100 } else if (errno != ENOENT)
101 pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
104 static void save(struct userdata *u) {
105 FILE *f;
107 if (!u->modified)
108 return;
110 if (u->sink_filename) {
111 if ((f = pa_fopen_cloexec(u->sink_filename, "w"))) {
112 pa_sink *s = pa_namereg_get_default_sink(u->core);
113 fprintf(f, "%s\n", s ? s->name : "");
114 fclose(f);
115 } else
116 pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
119 if (u->source_filename) {
120 if ((f = pa_fopen_cloexec(u->source_filename, "w"))) {
121 pa_source *s = pa_namereg_get_default_source(u->core);
122 fprintf(f, "%s\n", s ? s->name : "");
123 fclose(f);
124 } else
125 pa_log("Failed to save default source: %s", pa_cstrerror(errno));
128 u->modified = FALSE;
131 static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
132 struct userdata *u = userdata;
134 pa_assert(u);
135 save(u);
137 if (u->time_event) {
138 u->core->mainloop->time_free(u->time_event);
139 u->time_event = NULL;
143 static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
144 struct userdata *u = userdata;
146 pa_assert(u);
148 u->modified = TRUE;
150 if (!u->time_event)
151 u->time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, time_cb, u);
154 int pa__init(pa_module *m) {
155 struct userdata *u;
157 pa_assert(m);
159 m->userdata = u = pa_xnew0(struct userdata, 1);
160 u->core = m->core;
162 if (!(u->sink_filename = pa_state_path("default-sink", TRUE)))
163 goto fail;
165 if (!(u->source_filename = pa_state_path("default-source", TRUE)))
166 goto fail;
168 load(u);
170 u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
172 return 0;
174 fail:
175 pa__done(m);
177 return -1;
180 void pa__done(pa_module*m) {
181 struct userdata *u;
183 pa_assert(m);
185 if (!(u = m->userdata))
186 return;
188 save(u);
190 if (u->subscription)
191 pa_subscription_free(u->subscription);
193 if (u->time_event)
194 m->core->mainloop->time_free(u->time_event);
196 pa_xfree(u->sink_filename);
197 pa_xfree(u->source_filename);
198 pa_xfree(u);