]> code.delx.au - pulseaudio/blob - src/modules/module-default-device-restore.c
include host name in default sink/default source file
[pulseaudio] / 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 #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_name)
61 pa_log_info("Manually configured default sink, not overwriting.");
62 else if ((f = fopen(u->sink_filename, "r"))) {
63 char ln[256] = "";
64
65 fgets(ln, sizeof(ln)-1, f);
66 pa_strip_nl(ln);
67 fclose(f);
68
69 if (!ln[0])
70 pa_log_info("No previous default sink setting, ignoring.");
71 else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SINK, TRUE)) {
72 pa_namereg_set_default(u->core, ln, PA_NAMEREG_SINK);
73 pa_log_info("Restored default sink '%s'.", ln);
74 } else
75 pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
76
77 } else if (errno != ENOENT)
78 pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
79
80 if (u->core->default_source_name)
81 pa_log_info("Manually configured default source, not overwriting.");
82 else if ((f = fopen(u->source_filename, "r"))) {
83 char ln[256] = "";
84
85 fgets(ln, sizeof(ln)-1, f);
86 pa_strip_nl(ln);
87 fclose(f);
88
89 if (!ln[0])
90 pa_log_info("No previous default source setting, ignoring.");
91 else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE, TRUE)) {
92 pa_namereg_set_default(u->core, ln, PA_NAMEREG_SOURCE);
93 pa_log_info("Restored default source '%s'.", ln);
94 } else
95 pa_log_info("Saved default source '%s' not existant, not restoring default source setting.", ln);
96
97 } else if (errno != ENOENT)
98 pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
99 }
100
101 static void save(struct userdata *u) {
102 FILE *f;
103
104 if (!u->modified)
105 return;
106
107 if (u->sink_filename) {
108 if ((f = fopen(u->sink_filename, "w"))) {
109 const char *n = pa_namereg_get_default_sink_name(u->core);
110 fprintf(f, "%s\n", pa_strempty(n));
111 fclose(f);
112 } else
113 pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
114 }
115
116 if (u->source_filename) {
117 if ((f = fopen(u->source_filename, "w"))) {
118 const char *n = pa_namereg_get_default_source_name(u->core);
119 fprintf(f, "%s\n", pa_strempty(n));
120 fclose(f);
121 } else
122 pa_log("Failed to save default source: %s", pa_cstrerror(errno));
123 }
124
125 u->modified = FALSE;
126 }
127
128 static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
129 struct userdata *u = userdata;
130
131 pa_assert(u);
132 save(u);
133
134 if (u->time_event) {
135 u->core->mainloop->time_free(u->time_event);
136 u->time_event = NULL;
137 }
138 }
139
140 static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
141 struct userdata *u = userdata;
142
143 pa_assert(u);
144
145 u->modified = TRUE;
146
147 if (!u->time_event) {
148 struct timeval tv;
149 pa_gettimeofday(&tv);
150 pa_timeval_add(&tv, DEFAULT_SAVE_INTERVAL*PA_USEC_PER_SEC);
151 u->time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, time_cb, u);
152 }
153 }
154
155 int pa__init(pa_module *m) {
156 struct userdata *u;
157 char hn[256], *fn;
158
159 pa_assert(m);
160
161 m->userdata = u = pa_xnew0(struct userdata, 1);
162 u->core = m->core;
163
164 if (!pa_get_host_name(hn, sizeof(hn)))
165 goto fail;
166
167 fn = pa_sprintf_malloc("default-sink.%s", hn);
168 u->sink_filename = pa_state_path(fn);
169 pa_xfree(fn);
170
171 if (!u->sink_filename)
172 goto fail;
173
174 fn = pa_sprintf_malloc("default-source.%s", hn);
175 u->source_filename = pa_state_path(fn);
176 pa_xfree(fn);
177
178 if (!u->source_filename)
179 goto fail;
180
181 load(u);
182
183 u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
184
185 return 0;
186
187 fail:
188 pa__done(m);
189
190 return -1;
191 }
192
193 void pa__done(pa_module*m) {
194 struct userdata *u;
195
196 pa_assert(m);
197
198 if (!(u = m->userdata))
199 return;
200
201 save(u);
202
203 if (u->subscription)
204 pa_subscription_free(u->subscription);
205
206 if (u->time_event)
207 m->core->mainloop->time_free(u->time_event);
208
209 pa_xfree(u->sink_filename);
210 pa_xfree(u->source_filename);
211 pa_xfree(u);
212 }