Fix indent
[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
212 #ifdef USE_PM_LOCK
213     int ret = -1;
214 #endif
215
216     pa_assert(d);
217
218     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
219
220     if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
221         pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
222         pa_sink_suspend(d->sink, true, PA_SUSPEND_IDLE);
223         pa_core_maybe_vacuum(d->userdata->core);
224 #ifdef USE_PM_LOCK
225         UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SINK);
226         if(!(d->userdata->pm_state)) {
227             ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
228             if(ret != -1)
229                 pa_log_info("sink pm_unlock_state success [%d]", ret);
230             else
231                 pa_log_error("sink pm_unlock_state failed [%d]", ret);
232         }
233 #endif /* USE_PM_LOCK */
234     }
235
236     if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
237         pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
238         pa_source_suspend(d->source, true, PA_SUSPEND_IDLE);
239         pa_core_maybe_vacuum(d->userdata->core);
240 #ifdef USE_PM_LOCK
241
242         UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
243         if(!(d->userdata->pm_state)) {
244             ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
245             if(ret != -1)
246                 pa_log_info("source pm_unlock_state success [%d]", ret);
247             else
248                 pa_log_error("source pm_unlock_state failed [%d]", ret);
249         }
250 #endif /* USE_PM_LOCK */
251     }
252 }
253
254 static void restart(struct device_info *d) {
255     pa_usec_t now;
256
257     pa_assert(d);
258     pa_assert(d->sink || d->source);
259
260     d->last_use = now = pa_rtclock_now();
261     pa_core_rttime_restart(d->userdata->core, d->time_event, now + d->timeout);
262
263     if (d->sink)
264         pa_log_debug("Sink %s becomes idle, timeout in %" PRIu64 " seconds.", d->sink->name, d->timeout / PA_USEC_PER_SEC);
265     if (d->source)
266         pa_log_debug("Source %s becomes idle, timeout in %" PRIu64 " seconds.", d->source->name, d->timeout / PA_USEC_PER_SEC);
267 }
268
269 static void resume(struct device_info *d) {
270 #ifdef USE_PM_LOCK
271     int ret = -1;
272 #endif
273
274     pa_assert(d);
275
276     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
277
278     if (d->sink) {
279 #ifdef USE_PM_LOCK
280         UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SINK);
281         ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
282         if(ret != -1) {
283             pa_log_info("sink pm_lock_state success [%d]", ret);
284         } else {
285             pa_log_error("sink pm_lock_state failed [%d]", ret);
286         }
287 #endif /* USE_PM_LOCK */
288         pa_log_debug("Sink %s becomes busy.", d->sink->name);
289         pa_sink_suspend(d->sink, false, PA_SUSPEND_IDLE);
290     }
291
292     if (d->source) {
293 #ifdef USE_PM_LOCK
294         UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
295         ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
296         if(ret != -1) {
297             pa_log_info("source pm_lock_state success [%d]", ret);
298         } else {
299             pa_log_error("source pm_lock_state failed [%d]", ret);
300         }
301 #endif /* USE_PM_LOCK */
302         pa_log_debug("Source %s becomes busy.", d->source->name);
303         pa_source_suspend(d->source, false, PA_SUSPEND_IDLE);
304     }
305 }
306
307 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
308     struct device_info *d;
309
310     pa_assert(c);
311     pa_assert(data);
312     pa_assert(u);
313
314     /* We need to resume the audio device here even for
315      * PA_SINK_INPUT_START_CORKED, since we need the device parameters
316      * to be fully available while the stream is set up. In that case,
317      * make sure we close the sink again after the timeout interval. */
318
319     if ((d = pa_hashmap_get(u->device_infos, data->sink))) {
320         resume(d);
321         if (pa_sink_check_suspend(d->sink) <= 0)
322             restart(d);
323     }
324
325     return PA_HOOK_OK;
326 }
327
328 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
329     struct device_info *d;
330
331     pa_assert(c);
332     pa_assert(data);
333     pa_assert(u);
334
335     if (data->source->monitor_of)
336         d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
337     else
338         d = pa_hashmap_get(u->device_infos, data->source);
339
340     if (d) {
341         resume(d);
342         if (d->source) {
343             if (pa_source_check_suspend(d->source) <= 0)
344                 restart(d);
345         } else {
346             /* The source output is connected to a monitor source. */
347             pa_assert(d->sink);
348             if (pa_sink_check_suspend(d->sink) <= 0)
349                 restart(d);
350         }
351     }
352
353     return PA_HOOK_OK;
354 }
355
356 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
357     pa_assert(c);
358     pa_sink_input_assert_ref(s);
359     pa_assert(u);
360
361     if (!s->sink)
362         return PA_HOOK_OK;
363
364     if (pa_sink_check_suspend(s->sink) <= 0) {
365         struct device_info *d;
366         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
367             restart(d);
368     }
369
370     return PA_HOOK_OK;
371 }
372
373 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
374     struct device_info *d = NULL;
375
376     pa_assert(c);
377     pa_source_output_assert_ref(s);
378     pa_assert(u);
379
380     if (!s->source)
381         return PA_HOOK_OK;
382
383     if (s->source->monitor_of) {
384         if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
385             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
386     } else {
387         if (pa_source_check_suspend(s->source) <= 0)
388             d = pa_hashmap_get(u->device_infos, s->source);
389     }
390
391     if (d)
392         restart(d);
393
394     return PA_HOOK_OK;
395 }
396
397 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
398     struct device_info *d;
399
400     pa_assert(c);
401     pa_sink_input_assert_ref(s);
402     pa_assert(u);
403
404     if (pa_sink_check_suspend(s->sink) <= 1)
405         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
406             restart(d);
407
408     return PA_HOOK_OK;
409 }
410
411 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
412     struct device_info *d;
413     pa_sink_input_state_t state;
414
415     pa_assert(c);
416     pa_sink_input_assert_ref(s);
417     pa_assert(u);
418
419     state = pa_sink_input_get_state(s);
420     if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
421         return PA_HOOK_OK;
422
423     if ((d = pa_hashmap_get(u->device_infos, s->sink)))
424         resume(d);
425
426     return PA_HOOK_OK;
427 }
428
429 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
430     struct device_info *d = NULL;
431
432     pa_assert(c);
433     pa_source_output_assert_ref(s);
434     pa_assert(u);
435
436     if (s->source->monitor_of) {
437         if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
438             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
439     } else {
440         if (pa_source_check_suspend(s->source) <= 1)
441             d = pa_hashmap_get(u->device_infos, s->source);
442     }
443
444     if (d)
445         restart(d);
446
447     return PA_HOOK_OK;
448 }
449
450 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
451     struct device_info *d;
452
453     pa_assert(c);
454     pa_source_output_assert_ref(s);
455     pa_assert(u);
456
457     if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
458         return PA_HOOK_OK;
459
460     if (s->source->monitor_of)
461         d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
462     else
463         d = pa_hashmap_get(u->device_infos, s->source);
464
465     if (d)
466         resume(d);
467
468     return PA_HOOK_OK;
469 }
470
471 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
472     struct device_info *d;
473     pa_sink_input_state_t state;
474
475     pa_assert(c);
476     pa_sink_input_assert_ref(s);
477     pa_assert(u);
478
479     state = pa_sink_input_get_state(s);
480     if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
481         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
482             resume(d);
483
484     return PA_HOOK_OK;
485 }
486
487 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
488     pa_assert(c);
489     pa_source_output_assert_ref(s);
490     pa_assert(u);
491
492     if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
493         struct device_info *d;
494
495         if (s->source->monitor_of)
496             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
497         else
498             d = pa_hashmap_get(u->device_infos, s->source);
499
500         if (d)
501             resume(d);
502     }
503
504     return PA_HOOK_OK;
505 }
506
507 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
508     struct device_info *d;
509     pa_source *source;
510     pa_sink *sink;
511     const char *timeout_str;
512     int32_t timeout;
513     bool timeout_valid;
514
515     pa_assert(c);
516     pa_object_assert_ref(o);
517     pa_assert(u);
518
519     source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
520     sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
521
522     /* Never suspend monitors */
523     if (source && source->monitor_of)
524         return PA_HOOK_OK;
525
526     pa_assert(source || sink);
527
528     timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout");
529     if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0)
530         timeout_valid = true;
531     else
532         timeout_valid = false;
533
534     if (timeout_valid && timeout < 0)
535         return PA_HOOK_OK;
536
537     d = pa_xnew(struct device_info, 1);
538     d->userdata = u;
539     d->source = source ? pa_source_ref(source) : NULL;
540     d->sink = sink ? pa_sink_ref(sink) : NULL;
541     d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
542
543     if (timeout_valid)
544         d->timeout = timeout * PA_USEC_PER_SEC;
545     else
546         d->timeout = d->userdata->timeout;
547
548     pa_hashmap_put(u->device_infos, o, d);
549
550     if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
551         (d->source && pa_source_check_suspend(d->source) <= 0))
552         restart(d);
553
554     return PA_HOOK_OK;
555 }
556
557 static void device_info_free(struct device_info *d) {
558     pa_assert(d);
559
560     if (d->source)
561         pa_source_unref(d->source);
562     if (d->sink)
563         pa_sink_unref(d->sink);
564
565     d->userdata->core->mainloop->time_free(d->time_event);
566
567     pa_xfree(d);
568 }
569
570 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
571     struct device_info *d;
572
573     pa_assert(c);
574     pa_object_assert_ref(o);
575     pa_assert(u);
576
577     if ((d = pa_hashmap_remove(u->device_infos, o)))
578         device_info_free(d);
579
580     return PA_HOOK_OK;
581 }
582
583 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
584     struct device_info *d;
585
586     pa_assert(c);
587     pa_object_assert_ref(o);
588     pa_assert(u);
589
590     if (!(d = pa_hashmap_get(u->device_infos, o)))
591         return PA_HOOK_OK;
592
593     if (pa_sink_isinstance(o)) {
594         pa_sink *s = PA_SINK(o);
595         pa_sink_state_t state = pa_sink_get_state(s);
596
597         if (pa_sink_check_suspend(s) <= 0)
598             if (PA_SINK_IS_OPENED(state))
599                 restart(d);
600
601     } else if (pa_source_isinstance(o)) {
602         pa_source *s = PA_SOURCE(o);
603         pa_source_state_t state = pa_source_get_state(s);
604
605         if (pa_source_check_suspend(s) <= 0)
606             if (PA_SOURCE_IS_OPENED(state))
607                 restart(d);
608     }
609
610     return PA_HOOK_OK;
611 }
612
613 int pa__init(pa_module*m) {
614     pa_modargs *ma = NULL;
615     struct userdata *u;
616     uint32_t timeout = 5;
617     uint32_t idx;
618     pa_sink *sink;
619     pa_source *source;
620
621     pa_assert(m);
622
623     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
624         pa_log("Failed to parse module arguments.");
625         goto fail;
626     }
627
628     if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
629         pa_log("Failed to parse timeout value.");
630         goto fail;
631     }
632
633     m->userdata = u = pa_xnew(struct userdata, 1);
634     u->core = m->core;
635     u->timeout = timeout * PA_USEC_PER_SEC;
636     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);
637 #ifdef USE_PM_LOCK
638     u->pm_state = 0x00;
639 #endif /* USE_PM_LOCK */
640
641     PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
642         device_new_hook_cb(m->core, PA_OBJECT(sink), u);
643
644     PA_IDXSET_FOREACH(source, m->core->sources, idx)
645         device_new_hook_cb(m->core, PA_OBJECT(source), u);
646
647     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);
648     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);
649     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);
650     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);
651     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);
652     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);
653
654     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);
655     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);
656     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);
657     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);
658     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);
659     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);
660     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);
661     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);
662     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);
663     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);
664
665     pa_modargs_free(ma);
666     return 0;
667
668 fail:
669
670     if (ma)
671         pa_modargs_free(ma);
672
673     return -1;
674 }
675
676 void pa__done(pa_module*m) {
677     struct userdata *u;
678
679     pa_assert(m);
680
681     if (!m->userdata)
682         return;
683
684     u = m->userdata;
685
686     if (u->sink_new_slot)
687         pa_hook_slot_free(u->sink_new_slot);
688     if (u->sink_unlink_slot)
689         pa_hook_slot_free(u->sink_unlink_slot);
690     if (u->sink_state_changed_slot)
691         pa_hook_slot_free(u->sink_state_changed_slot);
692
693     if (u->source_new_slot)
694         pa_hook_slot_free(u->source_new_slot);
695     if (u->source_unlink_slot)
696         pa_hook_slot_free(u->source_unlink_slot);
697     if (u->source_state_changed_slot)
698         pa_hook_slot_free(u->source_state_changed_slot);
699
700     if (u->sink_input_new_slot)
701         pa_hook_slot_free(u->sink_input_new_slot);
702     if (u->sink_input_unlink_slot)
703         pa_hook_slot_free(u->sink_input_unlink_slot);
704     if (u->sink_input_move_start_slot)
705         pa_hook_slot_free(u->sink_input_move_start_slot);
706     if (u->sink_input_move_finish_slot)
707         pa_hook_slot_free(u->sink_input_move_finish_slot);
708     if (u->sink_input_state_changed_slot)
709         pa_hook_slot_free(u->sink_input_state_changed_slot);
710
711     if (u->source_output_new_slot)
712         pa_hook_slot_free(u->source_output_new_slot);
713     if (u->source_output_unlink_slot)
714         pa_hook_slot_free(u->source_output_unlink_slot);
715     if (u->source_output_move_start_slot)
716         pa_hook_slot_free(u->source_output_move_start_slot);
717     if (u->source_output_move_finish_slot)
718         pa_hook_slot_free(u->source_output_move_finish_slot);
719     if (u->source_output_state_changed_slot)
720         pa_hook_slot_free(u->source_output_state_changed_slot);
721
722     pa_hashmap_free(u->device_infos);
723
724     pa_xfree(u);
725 }