]> code.delx.au - pulseaudio/blob - src/modules/module-suspend-on-idle.c
* drop redundant pa_core argument from module initialization functions
[pulseaudio] / src / modules / module-suspend-on-idle.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 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 <pulse/xmalloc.h>
29 #include <pulse/timeval.h>
30
31 #include <pulsecore/core.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/source-output.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/namereg.h>
37
38 #include "module-suspend-on-idle-symdef.h"
39
40 PA_MODULE_AUTHOR("Lennart Poettering")
41 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it")
42 PA_MODULE_VERSION(PACKAGE_VERSION)
43
44 static const char* const valid_modargs[] = {
45 "timeout",
46 NULL,
47 };
48
49 struct userdata {
50 pa_core *core;
51 pa_usec_t timeout;
52 pa_hashmap *device_infos;
53 pa_hook_slot *sink_new_slot, *source_new_slot, *sink_disconnect_slot, *source_disconnect_slot;
54 pa_hook_slot *sink_input_new_slot, *source_output_new_slot, *sink_input_disconnect_slot, *source_output_disconnect_slot;
55 };
56
57 struct device_info {
58 struct userdata *userdata;
59 pa_sink *sink;
60 pa_source *source;
61 struct timeval last_use;
62 pa_time_event *time_event;
63 };
64
65 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
66 struct device_info *d = userdata;
67
68 pa_assert(d);
69
70 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
71
72 if (d->sink && pa_sink_used_by(d->sink) <= 0) {
73 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
74 pa_sink_suspend(d->sink, 1);
75 pa_source_suspend(d->sink->monitor_source, 1);
76 }
77
78 if (d->source && pa_source_used_by(d->source) <= 0) {
79 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
80 pa_source_suspend(d->source, 1);
81 }
82 }
83
84 static void restart(struct device_info *d) {
85 struct timeval tv;
86 pa_assert(d);
87
88 pa_gettimeofday(&tv);
89 d->last_use = tv;
90 pa_timeval_add(&tv, d->userdata->timeout*1000000);
91 d->userdata->core->mainloop->time_restart(d->time_event, &tv);
92
93 if (d->source)
94 pa_log_debug("Source %s becomes idle.", d->source->name);
95 if (d->sink)
96 pa_log_debug("Sink %s becomes idle.", d->sink->name);
97 }
98
99 static pa_hook_result_t sink_input_new_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
100 struct device_info *d;
101
102 pa_assert(c);
103 pa_sink_input_assert_ref(s);
104 pa_assert(u);
105
106 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->sink)));
107 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
108
109 pa_sink_suspend(s->sink, 0);
110 pa_source_suspend(s->sink->monitor_source, 0);
111
112 pa_log_debug("Sink %s becomes busy.", s->sink->name);
113
114 return PA_HOOK_OK;
115 }
116
117 static pa_hook_result_t source_output_new_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
118 struct device_info *d;
119
120 pa_assert(c);
121 pa_source_output_assert_ref(s);
122 pa_assert(u);
123
124 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->source)));
125 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
126
127 pa_source_suspend(s->source, 0);
128 if (s->source->monitor_of)
129 pa_sink_suspend(s->source->monitor_of, 0);
130
131 pa_log_debug("Source %s becomes busy.", s->source->name);
132
133 return PA_HOOK_OK;
134 }
135
136 static pa_hook_result_t sink_input_disconnect_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
137 pa_assert(c);
138 pa_sink_input_assert_ref(s);
139 pa_assert(u);
140
141 if (pa_sink_used_by(s->sink) <= 0) {
142 struct device_info *d;
143 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->sink)));
144 restart(d);
145 }
146
147 return PA_HOOK_OK;
148 }
149
150 static pa_hook_result_t source_output_disconnect_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
151 pa_assert(c);
152 pa_source_output_assert_ref(s);
153 pa_assert(u);
154
155 if (pa_source_used_by(s->source) <= 0) {
156 struct device_info *d;
157 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->source)));
158 restart(d);
159 }
160
161 return PA_HOOK_OK;
162 }
163
164 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
165 struct device_info *d;
166
167 pa_assert(c);
168 pa_object_assert_ref(o);
169 pa_assert(u);
170
171 d = pa_xnew(struct device_info, 1);
172 d->userdata = u;
173 d->source = pa_source_isinstance(o) ? pa_source_ref(PA_SOURCE(o)) : NULL;
174 d->sink = pa_sink_isinstance(o) ? pa_sink_ref(PA_SINK(o)) : NULL;
175 pa_assert(d->source || d->sink);
176 d->time_event = c->mainloop->time_new(c->mainloop, NULL, timeout_cb, d);
177 pa_hashmap_put(u->device_infos, o, d);
178
179 if ((d->sink && pa_sink_used_by(d->sink) <= 0) ||
180 (d->source && pa_source_used_by(d->source) <= 0))
181 restart(d);
182
183 return PA_HOOK_OK;
184 }
185
186 static void device_info_free(struct device_info *d) {
187 pa_assert(d);
188
189 if (d->source)
190 pa_source_unref(d->source);
191 if (d->sink)
192 pa_sink_unref(d->sink);
193
194 d->userdata->core->mainloop->time_free(d->time_event);
195
196 pa_xfree(d);
197 }
198
199 static pa_hook_result_t device_disconnect_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
200 struct device_info *d;
201
202 pa_assert(c);
203 pa_object_assert_ref(o);
204 pa_assert(u);
205
206 pa_assert_se((d = pa_hashmap_remove(u->device_infos, o)));
207 device_info_free(d);
208
209 return PA_HOOK_OK;
210 }
211
212 int pa__init(pa_module*m) {
213 pa_modargs *ma = NULL;
214 struct userdata *u;
215 uint32_t timeout = 5;
216 uint32_t idx;
217 pa_sink *sink;
218 pa_source *source;
219
220 pa_assert(m);
221
222 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
223 pa_log("Failed to parse module arguments.");
224 goto fail;
225 }
226
227 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
228 pa_log("Failed to parse timeout value.");
229 goto fail;
230 }
231
232 m->userdata = u = pa_xnew(struct userdata, 1);
233 u->core = m->core;
234 u->timeout = timeout;
235 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
236
237 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
238 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
239
240 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
241 device_new_hook_cb(m->core, PA_OBJECT(source), u);
242
243 u->sink_new_slot = pa_hook_connect(&m->core->hook_sink_new_post, (pa_hook_cb_t) device_new_hook_cb, u);
244 u->source_new_slot = pa_hook_connect(&m->core->hook_source_new_post, (pa_hook_cb_t) device_new_hook_cb, u);
245 u->sink_disconnect_slot = pa_hook_connect(&m->core->hook_sink_disconnect_post, (pa_hook_cb_t) device_disconnect_hook_cb, u);
246 u->source_disconnect_slot = pa_hook_connect(&m->core->hook_source_disconnect_post, (pa_hook_cb_t) device_disconnect_hook_cb, u);
247
248 u->sink_input_new_slot = pa_hook_connect(&m->core->hook_sink_input_new_post, (pa_hook_cb_t) sink_input_new_hook_cb, u);
249 u->source_output_new_slot = pa_hook_connect(&m->core->hook_source_output_new_post, (pa_hook_cb_t) source_output_new_hook_cb, u);
250 u->sink_input_disconnect_slot = pa_hook_connect(&m->core->hook_sink_input_disconnect_post, (pa_hook_cb_t) sink_input_disconnect_hook_cb, u);
251 u->source_output_disconnect_slot = pa_hook_connect(&m->core->hook_source_output_disconnect_post, (pa_hook_cb_t) source_output_disconnect_hook_cb, u);
252
253 pa_modargs_free(ma);
254 return 0;
255
256 fail:
257
258 if (ma)
259 pa_modargs_free(ma);
260
261 return -1;
262 }
263
264 void pa__done(pa_module*m) {
265 struct userdata *u;
266 struct device_info *d;
267
268 pa_assert(m);
269
270 if (!m->userdata)
271 return;
272
273 u = m->userdata;
274
275 if (u->sink_new_slot)
276 pa_hook_slot_free(u->sink_new_slot);
277 if (u->sink_disconnect_slot)
278 pa_hook_slot_free(u->sink_disconnect_slot);
279
280 if (u->source_new_slot)
281 pa_hook_slot_free(u->source_new_slot);
282 if (u->source_disconnect_slot)
283 pa_hook_slot_free(u->source_disconnect_slot);
284
285 if (u->sink_input_new_slot)
286 pa_hook_slot_free(u->sink_input_new_slot);
287 if (u->sink_input_disconnect_slot)
288 pa_hook_slot_free(u->sink_input_disconnect_slot);
289
290 if (u->source_output_new_slot)
291 pa_hook_slot_free(u->source_output_new_slot);
292 if (u->source_output_disconnect_slot)
293 pa_hook_slot_free(u->source_output_disconnect_slot);
294
295 while ((d = pa_hashmap_steal_first(u->device_infos)))
296 device_info_free(d);
297
298 pa_hashmap_free(u->device_infos, NULL, NULL);
299
300 pa_xfree(u);
301 }