6c0935e0f0c64259d232f6659f275f4f8af41c95
[platform/upstream/pulseaudio.git] / src / modules / module-suspend-on-idle.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2006 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 <pulse/xmalloc.h>
27 #include <pulse/timeval.h>
28 #include <pulse/rtclock.h>
29
30 #include <pulsecore/core.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/source-output.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/log.h>
36
37 #include <pulsecore/namereg.h>
38 #include "module-suspend-on-idle-symdef.h"
39 //move to configure.ac
40 //#define USE_PM_LOCK /* Enable as default */
41 #ifdef USE_PM_LOCK
42
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <linux/limits.h>
50
51 #define SOCK_PATH                       "/tmp/pm_sock"
52 #define SHIFT_UNLOCK                    4
53 #define SHIFT_UNLOCK_PARAMETER          12
54 #define SHIFT_CHANGE_STATE              8
55 #define SHIFT_HOLD_KEY_BLOCK            16
56 #define SHIFT_CHANGE_TIMEOUT            20
57 #define TIMEOUT_RESET_BIT               0x80
58
59 #define LCD_NORMAL      0x1   /**< NORMAL state */
60 #define LCD_DIM         0x2  /**< LCD dimming state */
61 #define LCD_OFF         0x4  /**< LCD off state */
62 #define SUSPEND         0x8  /**< Sleep state */
63 #define POWER_OFF       0x16  /**< Sleep state */
64 #define SETALL (LCD_DIM | LCD_OFF | LCD_NORMAL) /*< select all state - not supported yet */
65
66 /* parameters for pm_lock_state() */
67 #define STAY_CUR_STATE  0x0
68 #define GOTO_STATE_NOW  0x1
69 #define HOLD_KEY_BLOCK  0x2
70
71 /* paramters for pm_unlcok_state() - details are described at 162 line */
72 #define PM_SLEEP_MARGIN 0x0     /**< keep guard time for unlock */
73 #define PM_RESET_TIMER  0x1     /**< reset timer for unlock */
74 #define PM_KEEP_TIMER   0x2     /**< keep timer for unlock */
75
76 struct pwr_msg {
77         pid_t pid;
78         unsigned int cond;
79         unsigned int timeout;
80         unsigned int timeout2;
81 };
82
83 #endif /* USE_PM_LOCK */
84
85 PA_MODULE_AUTHOR("Lennart Poettering");
86 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
87 PA_MODULE_VERSION(PACKAGE_VERSION);
88 PA_MODULE_LOAD_ONCE(true);
89 PA_MODULE_USAGE("timeout=<timeout>");
90
91 static const char* const valid_modargs[] = {
92     "timeout",
93     NULL,
94 };
95
96 #ifdef USE_PM_LOCK
97 #define PM_TYPE_SINK    0x01
98 #define PM_TYPE_SOURCE  0x02
99
100 #define UPDATE_PM_LOCK(current,type)    (current |= type)
101 #define UPDATE_PM_UNLOCK(current,type)  (current &= ~type)
102 #endif /* USE_PM_LOCK */
103 struct userdata {
104     pa_core *core;
105     pa_usec_t timeout;
106     pa_hashmap *device_infos;
107     pa_hook_slot
108         *sink_new_slot,
109         *source_new_slot,
110         *sink_unlink_slot,
111         *source_unlink_slot,
112         *sink_state_changed_slot,
113         *source_state_changed_slot;
114
115     pa_hook_slot
116         *sink_input_new_slot,
117         *source_output_new_slot,
118         *sink_input_unlink_slot,
119         *source_output_unlink_slot,
120         *sink_input_move_start_slot,
121         *source_output_move_start_slot,
122         *sink_input_move_finish_slot,
123         *source_output_move_finish_slot,
124         *sink_input_state_changed_slot,
125         *source_output_state_changed_slot;
126 #ifdef USE_PM_LOCK
127     uint32_t pm_state;
128 #endif /* USE_PM_LOCK */
129 };
130
131 struct device_info {
132     struct userdata *userdata;
133     pa_sink *sink;
134     pa_source *source;
135     pa_usec_t last_use;
136     pa_time_event *time_event;
137     pa_usec_t timeout;
138 };
139 #ifdef USE_PM_LOCK
140
141 static int send_msg(unsigned int s_bits, unsigned int timeout, unsigned int timeout2)
142 {
143         int rc = 0;
144         int sock;
145         struct pwr_msg p;
146         struct sockaddr_un remote;
147
148         p.pid = getpid();
149         p.cond = s_bits;
150         p.timeout = timeout;
151         p.timeout2 = timeout2;
152
153         sock = socket(AF_UNIX, SOCK_DGRAM, 0);
154         if (sock == -1) {
155                 return -1;
156         }
157
158         remote.sun_family = AF_UNIX;
159         if(strlen(SOCK_PATH) >= sizeof(remote.sun_path)) {
160                 return -1;
161         }
162         strncpy(remote.sun_path, SOCK_PATH, sizeof(remote.sun_path));
163
164         rc = sendto(sock, (void *)&p, sizeof(p), 0, (struct sockaddr *)&remote,
165                     sizeof(struct sockaddr_un));
166
167         close(sock);
168         return rc;
169 }
170
171 static int pm_lock_state(unsigned int s_bits, unsigned int flag,
172                       unsigned int timeout)
173 {
174         switch (s_bits) {
175         case LCD_NORMAL:
176         case LCD_DIM:
177         case LCD_OFF:
178                 break;
179         default:
180                 return -1;
181         }
182         if (flag & GOTO_STATE_NOW)
183                 /* if the flag is true, go to the locking state directly */
184                 s_bits = s_bits | (s_bits << SHIFT_CHANGE_STATE);
185         if (flag & HOLD_KEY_BLOCK)
186                 s_bits = s_bits | (1 << SHIFT_HOLD_KEY_BLOCK);
187
188         return send_msg(s_bits, timeout, 0);
189 }
190
191 static int pm_unlock_state(unsigned int s_bits, unsigned int flag)
192 {
193         switch (s_bits) {
194         case LCD_NORMAL:
195         case LCD_DIM:
196         case LCD_OFF:
197                 break;
198         default:
199                 return -1;
200         }
201
202         s_bits = (s_bits << SHIFT_UNLOCK);
203         s_bits = (s_bits | (flag << SHIFT_UNLOCK_PARAMETER));
204         return send_msg(s_bits, 0, 0);
205 }
206
207 #endif
208
209 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
210     struct device_info *d = userdata;
211     int ret = -1;
212
213     pa_assert(d);
214
215     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
216
217     if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
218         pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
219         pa_sink_suspend(d->sink, true, PA_SUSPEND_IDLE);
220         pa_core_maybe_vacuum(d->userdata->core);
221 #ifdef USE_PM_LOCK
222                 UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SINK);
223                 if(!(d->userdata->pm_state)) {
224                         ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
225                         if(ret != -1)
226                                 pa_log_info("sink pm_unlock_state success [%d]", ret);
227                         else
228                                 pa_log_error("sink pm_unlock_state failed [%d]", ret);
229                 }
230 #endif /* USE_PM_LOCK */
231     }
232
233     if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
234         pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
235         pa_source_suspend(d->source, true, PA_SUSPEND_IDLE);
236         pa_core_maybe_vacuum(d->userdata->core);
237 #ifdef USE_PM_LOCK
238
239         UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
240         if(!(d->userdata->pm_state)) {
241                         ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
242                         if(ret != -1)
243                                 pa_log_info("source pm_unlock_state success [%d]", ret);
244                         else
245                                 pa_log_error("source pm_unlock_state failed [%d]", ret);
246                 }
247 #endif /* USE_PM_LOCK */
248     }
249 }
250
251 static void restart(struct device_info *d) {
252     pa_usec_t now;
253
254     pa_assert(d);
255     pa_assert(d->sink || d->source);
256
257     d->last_use = now = pa_rtclock_now();
258     pa_core_rttime_restart(d->userdata->core, d->time_event, now + d->timeout);
259
260     if (d->sink)
261         pa_log_debug("Sink %s becomes idle, timeout in %" PRIu64 " seconds.", d->sink->name, d->timeout / PA_USEC_PER_SEC);
262     if (d->source)
263         pa_log_debug("Source %s becomes idle, timeout in %" PRIu64 " seconds.", d->source->name, d->timeout / PA_USEC_PER_SEC);
264 }
265
266 static void resume(struct device_info *d) {
267     int ret = -1;
268
269     pa_assert(d);
270
271     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
272
273     if (d->sink) {
274 #ifdef USE_PM_LOCK
275                 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SINK);
276                 ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
277                 if(ret != -1) {
278                         pa_log_info("sink pm_lock_state success [%d]", ret);
279                 } else {
280                         pa_log_error("sink pm_lock_state failed [%d]", ret);
281                 }
282 #endif /* USE_PM_LOCK */
283         pa_log_debug("Sink %s becomes busy.", d->sink->name);
284         pa_sink_suspend(d->sink, false, PA_SUSPEND_IDLE);
285     }
286
287     if (d->source) {
288 #ifdef USE_PM_LOCK
289                 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
290                 ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
291                 if(ret != -1) {
292                         pa_log_info("source pm_lock_state success [%d]", ret);
293                 } else {
294                         pa_log_error("source pm_lock_state failed [%d]", ret);
295                 }
296 #endif /* USE_PM_LOCK */
297         pa_log_debug("Source %s becomes busy.", d->source->name);
298         pa_source_suspend(d->source, false, PA_SUSPEND_IDLE);
299     }
300 }
301
302 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
303     struct device_info *d;
304
305     pa_assert(c);
306     pa_assert(data);
307     pa_assert(u);
308
309     /* We need to resume the audio device here even for
310      * PA_SINK_INPUT_START_CORKED, since we need the device parameters
311      * to be fully available while the stream is set up. */
312
313     if ((d = pa_hashmap_get(u->device_infos, data->sink)))
314         resume(d);
315
316     return PA_HOOK_OK;
317 }
318
319 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
320     struct device_info *d;
321
322     pa_assert(c);
323     pa_assert(data);
324     pa_assert(u);
325
326     if (data->source->monitor_of)
327         d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
328     else
329         d = pa_hashmap_get(u->device_infos, data->source);
330
331     if (d)
332         resume(d);
333
334     return PA_HOOK_OK;
335 }
336
337 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
338     pa_assert(c);
339     pa_sink_input_assert_ref(s);
340     pa_assert(u);
341
342     if (!s->sink)
343         return PA_HOOK_OK;
344
345     if (pa_sink_check_suspend(s->sink) <= 0) {
346         struct device_info *d;
347         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
348             restart(d);
349     }
350
351     return PA_HOOK_OK;
352 }
353
354 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
355     struct device_info *d = NULL;
356
357     pa_assert(c);
358     pa_source_output_assert_ref(s);
359     pa_assert(u);
360
361     if (!s->source)
362         return PA_HOOK_OK;
363
364     if (s->source->monitor_of) {
365         if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
366             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
367     } else {
368         if (pa_source_check_suspend(s->source) <= 0)
369             d = pa_hashmap_get(u->device_infos, s->source);
370     }
371
372     if (d)
373         restart(d);
374
375     return PA_HOOK_OK;
376 }
377
378 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
379     struct device_info *d;
380
381     pa_assert(c);
382     pa_sink_input_assert_ref(s);
383     pa_assert(u);
384
385     if (pa_sink_check_suspend(s->sink) <= 1)
386         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
387             restart(d);
388
389     return PA_HOOK_OK;
390 }
391
392 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
393     struct device_info *d;
394     pa_sink_input_state_t state;
395
396     pa_assert(c);
397     pa_sink_input_assert_ref(s);
398     pa_assert(u);
399
400     state = pa_sink_input_get_state(s);
401     if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
402         return PA_HOOK_OK;
403
404     if ((d = pa_hashmap_get(u->device_infos, s->sink)))
405         resume(d);
406
407     return PA_HOOK_OK;
408 }
409
410 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
411     struct device_info *d = NULL;
412
413     pa_assert(c);
414     pa_source_output_assert_ref(s);
415     pa_assert(u);
416
417     if (s->source->monitor_of) {
418         if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
419             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
420     } else {
421         if (pa_source_check_suspend(s->source) <= 1)
422             d = pa_hashmap_get(u->device_infos, s->source);
423     }
424
425     if (d)
426         restart(d);
427
428     return PA_HOOK_OK;
429 }
430
431 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
432     struct device_info *d;
433
434     pa_assert(c);
435     pa_source_output_assert_ref(s);
436     pa_assert(u);
437
438     if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
439         return PA_HOOK_OK;
440
441     if (s->source->monitor_of)
442         d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
443     else
444         d = pa_hashmap_get(u->device_infos, s->source);
445
446     if (d)
447         resume(d);
448
449     return PA_HOOK_OK;
450 }
451
452 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
453     struct device_info *d;
454     pa_sink_input_state_t state;
455
456     pa_assert(c);
457     pa_sink_input_assert_ref(s);
458     pa_assert(u);
459
460     state = pa_sink_input_get_state(s);
461     if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
462         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
463             resume(d);
464
465     return PA_HOOK_OK;
466 }
467
468 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
469     pa_assert(c);
470     pa_source_output_assert_ref(s);
471     pa_assert(u);
472
473     if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
474         struct device_info *d;
475
476         if (s->source->monitor_of)
477             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
478         else
479             d = pa_hashmap_get(u->device_infos, s->source);
480
481         if (d)
482             resume(d);
483     }
484
485     return PA_HOOK_OK;
486 }
487
488 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
489     struct device_info *d;
490     pa_source *source;
491     pa_sink *sink;
492     const char *timeout_str;
493     int32_t timeout;
494     bool timeout_valid;
495
496     pa_assert(c);
497     pa_object_assert_ref(o);
498     pa_assert(u);
499
500     source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
501     sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
502
503     /* Never suspend monitors */
504     if (source && source->monitor_of)
505         return PA_HOOK_OK;
506
507     pa_assert(source || sink);
508
509     timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout");
510     if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0)
511         timeout_valid = true;
512     else
513         timeout_valid = false;
514
515     if (timeout_valid && timeout < 0)
516         return PA_HOOK_OK;
517
518     d = pa_xnew(struct device_info, 1);
519     d->userdata = u;
520     d->source = source ? pa_source_ref(source) : NULL;
521     d->sink = sink ? pa_sink_ref(sink) : NULL;
522     d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
523
524     if (timeout_valid)
525         d->timeout = timeout * PA_USEC_PER_SEC;
526     else
527         d->timeout = d->userdata->timeout;
528
529     pa_hashmap_put(u->device_infos, o, d);
530
531     if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
532         (d->source && pa_source_check_suspend(d->source) <= 0))
533         restart(d);
534
535     return PA_HOOK_OK;
536 }
537
538 static void device_info_free(struct device_info *d) {
539     pa_assert(d);
540
541     if (d->source)
542         pa_source_unref(d->source);
543     if (d->sink)
544         pa_sink_unref(d->sink);
545
546     d->userdata->core->mainloop->time_free(d->time_event);
547
548     pa_xfree(d);
549 }
550
551 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
552     struct device_info *d;
553
554     pa_assert(c);
555     pa_object_assert_ref(o);
556     pa_assert(u);
557
558     if ((d = pa_hashmap_remove(u->device_infos, o)))
559         device_info_free(d);
560
561     return PA_HOOK_OK;
562 }
563
564 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
565     struct device_info *d;
566
567     pa_assert(c);
568     pa_object_assert_ref(o);
569     pa_assert(u);
570
571     if (!(d = pa_hashmap_get(u->device_infos, o)))
572         return PA_HOOK_OK;
573
574     if (pa_sink_isinstance(o)) {
575         pa_sink *s = PA_SINK(o);
576         pa_sink_state_t state = pa_sink_get_state(s);
577
578         if (pa_sink_check_suspend(s) <= 0)
579             if (PA_SINK_IS_OPENED(state))
580                 restart(d);
581
582     } else if (pa_source_isinstance(o)) {
583         pa_source *s = PA_SOURCE(o);
584         pa_source_state_t state = pa_source_get_state(s);
585
586         if (pa_source_check_suspend(s) <= 0)
587             if (PA_SOURCE_IS_OPENED(state))
588                 restart(d);
589     }
590
591     return PA_HOOK_OK;
592 }
593
594 int pa__init(pa_module*m) {
595     pa_modargs *ma = NULL;
596     struct userdata *u;
597     uint32_t timeout = 5;
598     uint32_t idx;
599     pa_sink *sink;
600     pa_source *source;
601
602     pa_assert(m);
603
604     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
605         pa_log("Failed to parse module arguments.");
606         goto fail;
607     }
608
609     if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
610         pa_log("Failed to parse timeout value.");
611         goto fail;
612     }
613
614     m->userdata = u = pa_xnew(struct userdata, 1);
615     u->core = m->core;
616     u->timeout = timeout * PA_USEC_PER_SEC;
617     u->device_infos = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL, (pa_free_cb_t) device_info_free);
618 #ifdef USE_PM_LOCK
619     u->pm_state = 0x00;
620 #endif /* USE_PM_LOCK */
621
622     PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
623         device_new_hook_cb(m->core, PA_OBJECT(sink), u);
624
625     PA_IDXSET_FOREACH(source, m->core->sources, idx)
626         device_new_hook_cb(m->core, PA_OBJECT(source), u);
627
628     u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
629     u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
630     u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
631     u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
632     u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
633     u->source_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
634
635     u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_fixate_hook_cb, u);
636     u->source_output_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_fixate_hook_cb, u);
637     u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_unlink_hook_cb, u);
638     u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_unlink_hook_cb, u);
639     u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_start_hook_cb, u);
640     u->source_output_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_start_hook_cb, u);
641     u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_finish_hook_cb, u);
642     u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_finish_hook_cb, u);
643     u->sink_input_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_state_changed_hook_cb, u);
644     u->source_output_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_state_changed_hook_cb, u);
645
646     pa_modargs_free(ma);
647     return 0;
648
649 fail:
650
651     if (ma)
652         pa_modargs_free(ma);
653
654     return -1;
655 }
656
657 void pa__done(pa_module*m) {
658     struct userdata *u;
659
660     pa_assert(m);
661
662     if (!m->userdata)
663         return;
664
665     u = m->userdata;
666
667     if (u->sink_new_slot)
668         pa_hook_slot_free(u->sink_new_slot);
669     if (u->sink_unlink_slot)
670         pa_hook_slot_free(u->sink_unlink_slot);
671     if (u->sink_state_changed_slot)
672         pa_hook_slot_free(u->sink_state_changed_slot);
673
674     if (u->source_new_slot)
675         pa_hook_slot_free(u->source_new_slot);
676     if (u->source_unlink_slot)
677         pa_hook_slot_free(u->source_unlink_slot);
678     if (u->source_state_changed_slot)
679         pa_hook_slot_free(u->source_state_changed_slot);
680
681     if (u->sink_input_new_slot)
682         pa_hook_slot_free(u->sink_input_new_slot);
683     if (u->sink_input_unlink_slot)
684         pa_hook_slot_free(u->sink_input_unlink_slot);
685     if (u->sink_input_move_start_slot)
686         pa_hook_slot_free(u->sink_input_move_start_slot);
687     if (u->sink_input_move_finish_slot)
688         pa_hook_slot_free(u->sink_input_move_finish_slot);
689     if (u->sink_input_state_changed_slot)
690         pa_hook_slot_free(u->sink_input_state_changed_slot);
691
692     if (u->source_output_new_slot)
693         pa_hook_slot_free(u->source_output_new_slot);
694     if (u->source_output_unlink_slot)
695         pa_hook_slot_free(u->source_output_unlink_slot);
696     if (u->source_output_move_start_slot)
697         pa_hook_slot_free(u->source_output_move_start_slot);
698     if (u->source_output_move_finish_slot)
699         pa_hook_slot_free(u->source_output_move_finish_slot);
700     if (u->source_output_state_changed_slot)
701         pa_hook_slot_free(u->source_output_state_changed_slot);
702
703     pa_hashmap_free(u->device_infos);
704
705     pa_xfree(u);
706 }