]> code.delx.au - pulseaudio/blob - src/modules/module-default-device-restore.c
Yes, yet another evil all-in-one commit of intervowen changes. I suck.
[pulseaudio] / src / modules / module-default-device-restore.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006-2008 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 <errno.h>
29 #include <stdio.h>
30
31 #include <pulse/timeval.h>
32
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>
38
39 #include "module-default-device-restore-symdef.h"
40
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);
45
46 #define DEFAULT_SINK_FILE "default-sink"
47 #define DEFAULT_SOURCE_FILE "default-source"
48 #define DEFAULT_SAVE_INTERVAL 5
49
50 struct userdata {
51 pa_core *core;
52 pa_subscription *subscription;
53 pa_time_event *time_event;
54 char *sink_filename, *source_filename;
55 pa_bool_t modified;
56 };
57
58 static void load(struct userdata *u) {
59 FILE *f;
60
61 /* We never overwrite manually configured settings */
62
63 if (u->core->default_sink_name)
64 pa_log_info("Manually configured default sink, not overwriting.");
65 else if ((f = fopen(u->sink_filename, "r"))) {
66 char ln[256] = "";
67
68 fgets(ln, sizeof(ln)-1, f);
69 pa_strip_nl(ln);
70 fclose(f);
71
72 if (!ln[0])
73 pa_log_info("No previous default sink setting, ignoring.");
74 else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SINK, TRUE)) {
75 pa_namereg_set_default(u->core, ln, PA_NAMEREG_SINK);
76 pa_log_info("Restored default sink '%s'.", ln);
77 } else
78 pa_log_info("Saved default sink '%s' not existant, not restoring default sink setting.", ln);
79
80 } else if (errno != ENOENT)
81 pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
82
83 if (u->core->default_source_name)
84 pa_log_info("Manually configured default source, not overwriting.");
85 else if ((f = fopen(u->source_filename, "r"))) {
86 char ln[256] = "";
87
88 fgets(ln, sizeof(ln)-1, f);
89 pa_strip_nl(ln);
90 fclose(f);
91
92 if (!ln[0])
93 pa_log_info("No previous default source setting, ignoring.");
94 else if (pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE, TRUE)) {
95 pa_namereg_set_default(u->core, ln, PA_NAMEREG_SOURCE);
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);
99
100 } else if (errno != ENOENT)
101 pa_log("Failed to load default sink: %s", pa_cstrerror(errno));
102 }
103
104 static void save(struct userdata *u) {
105 FILE *f;
106
107 if (!u->modified)
108 return;
109
110 if (u->sink_filename) {
111 if ((f = fopen(u->sink_filename, "w"))) {
112 const char *n = pa_namereg_get_default_sink_name(u->core);
113 fprintf(f, "%s\n", pa_strempty(n));
114 fclose(f);
115 } else
116 pa_log("Failed to save default sink: %s", pa_cstrerror(errno));
117 }
118
119 if (u->source_filename) {
120 if ((f = fopen(u->source_filename, "w"))) {
121 const char *n = pa_namereg_get_default_source_name(u->core);
122 fprintf(f, "%s\n", pa_strempty(n));
123 fclose(f);
124 } else
125 pa_log("Failed to save default source: %s", pa_cstrerror(errno));
126 }
127
128 u->modified = FALSE;
129 }
130
131 static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) {
132 struct userdata *u = userdata;
133
134 pa_assert(u);
135 save(u);
136
137 if (u->time_event) {
138 u->core->mainloop->time_free(u->time_event);
139 u->time_event = NULL;
140 }
141 }
142
143 static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
144 struct userdata *u = userdata;
145
146 pa_assert(u);
147
148 u->modified = TRUE;
149
150 if (!u->time_event) {
151 struct timeval tv;
152 pa_gettimeofday(&tv);
153 pa_timeval_add(&tv, DEFAULT_SAVE_INTERVAL*PA_USEC_PER_SEC);
154 u->time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, time_cb, u);
155 }
156 }
157
158 int pa__init(pa_module *m) {
159 struct userdata *u;
160
161 pa_assert(u);
162
163 u = pa_xnew0(struct userdata, 1);
164 u->core = m->core;
165
166 if (!(u->sink_filename = pa_runtime_path(DEFAULT_SINK_FILE)))
167 goto fail;
168
169 if (!(u->source_filename = pa_runtime_path(DEFAULT_SOURCE_FILE)))
170 goto fail;
171
172 load(u);
173
174 u->subscription = pa_subscription_new(u->core, PA_SUBSCRIPTION_MASK_SERVER, subscribe_cb, u);
175
176 return 0;
177
178 fail:
179 pa__done(m);
180
181 return -1;
182 }
183
184 void pa__done(pa_module*m) {
185 struct userdata *u;
186
187 pa_assert(m);
188
189 if (!(u = m->userdata))
190 return;
191
192 save(u);
193
194 if (u->subscription)
195 pa_subscription_free(u->subscription);
196
197 if (u->time_event)
198 m->core->mainloop->time_free(u->time_event);
199
200 pa_xfree(u->sink_filename);
201 pa_xfree(u->source_filename);
202 pa_xfree(u);
203 }