fix warnings in samsung pm lock
[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. */
317
318     if ((d = pa_hashmap_get(u->device_infos, data->sink)))
319         resume(d);
320
321     return PA_HOOK_OK;
322 }
323
324 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
325     struct device_info *d;
326
327     pa_assert(c);
328     pa_assert(data);
329     pa_assert(u);
330
331     if (data->source->monitor_of)
332         d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
333     else
334         d = pa_hashmap_get(u->device_infos, data->source);
335
336     if (d)
337         resume(d);
338
339     return PA_HOOK_OK;
340 }
341
342 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
343     pa_assert(c);
344     pa_sink_input_assert_ref(s);
345     pa_assert(u);
346
347     if (!s->sink)
348         return PA_HOOK_OK;
349
350     if (pa_sink_check_suspend(s->sink) <= 0) {
351         struct device_info *d;
352         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
353             restart(d);
354     }
355
356     return PA_HOOK_OK;
357 }
358
359 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
360     struct device_info *d = NULL;
361
362     pa_assert(c);
363     pa_source_output_assert_ref(s);
364     pa_assert(u);
365
366     if (!s->source)
367         return PA_HOOK_OK;
368
369     if (s->source->monitor_of) {
370         if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
371             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
372     } else {
373         if (pa_source_check_suspend(s->source) <= 0)
374             d = pa_hashmap_get(u->device_infos, s->source);
375     }
376
377     if (d)
378         restart(d);
379
380     return PA_HOOK_OK;
381 }
382
383 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
384     struct device_info *d;
385
386     pa_assert(c);
387     pa_sink_input_assert_ref(s);
388     pa_assert(u);
389
390     if (pa_sink_check_suspend(s->sink) <= 1)
391         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
392             restart(d);
393
394     return PA_HOOK_OK;
395 }
396
397 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
398     struct device_info *d;
399     pa_sink_input_state_t state;
400
401     pa_assert(c);
402     pa_sink_input_assert_ref(s);
403     pa_assert(u);
404
405     state = pa_sink_input_get_state(s);
406     if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
407         return PA_HOOK_OK;
408
409     if ((d = pa_hashmap_get(u->device_infos, s->sink)))
410         resume(d);
411
412     return PA_HOOK_OK;
413 }
414
415 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
416     struct device_info *d = NULL;
417
418     pa_assert(c);
419     pa_source_output_assert_ref(s);
420     pa_assert(u);
421
422     if (s->source->monitor_of) {
423         if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
424             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
425     } else {
426         if (pa_source_check_suspend(s->source) <= 1)
427             d = pa_hashmap_get(u->device_infos, s->source);
428     }
429
430     if (d)
431         restart(d);
432
433     return PA_HOOK_OK;
434 }
435
436 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
437     struct device_info *d;
438
439     pa_assert(c);
440     pa_source_output_assert_ref(s);
441     pa_assert(u);
442
443     if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
444         return PA_HOOK_OK;
445
446     if (s->source->monitor_of)
447         d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
448     else
449         d = pa_hashmap_get(u->device_infos, s->source);
450
451     if (d)
452         resume(d);
453
454     return PA_HOOK_OK;
455 }
456
457 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
458     struct device_info *d;
459     pa_sink_input_state_t state;
460
461     pa_assert(c);
462     pa_sink_input_assert_ref(s);
463     pa_assert(u);
464
465     state = pa_sink_input_get_state(s);
466     if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
467         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
468             resume(d);
469
470     return PA_HOOK_OK;
471 }
472
473 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
474     pa_assert(c);
475     pa_source_output_assert_ref(s);
476     pa_assert(u);
477
478     if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
479         struct device_info *d;
480
481         if (s->source->monitor_of)
482             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
483         else
484             d = pa_hashmap_get(u->device_infos, s->source);
485
486         if (d)
487             resume(d);
488     }
489
490     return PA_HOOK_OK;
491 }
492
493 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
494     struct device_info *d;
495     pa_source *source;
496     pa_sink *sink;
497     const char *timeout_str;
498     int32_t timeout;
499     bool timeout_valid;
500
501     pa_assert(c);
502     pa_object_assert_ref(o);
503     pa_assert(u);
504
505     source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
506     sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
507
508     /* Never suspend monitors */
509     if (source && source->monitor_of)
510         return PA_HOOK_OK;
511
512     pa_assert(source || sink);
513
514     timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout");
515     if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0)
516         timeout_valid = true;
517     else
518         timeout_valid = false;
519
520     if (timeout_valid && timeout < 0)
521         return PA_HOOK_OK;
522
523     d = pa_xnew(struct device_info, 1);
524     d->userdata = u;
525     d->source = source ? pa_source_ref(source) : NULL;
526     d->sink = sink ? pa_sink_ref(sink) : NULL;
527     d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
528
529     if (timeout_valid)
530         d->timeout = timeout * PA_USEC_PER_SEC;
531     else
532         d->timeout = d->userdata->timeout;
533
534     pa_hashmap_put(u->device_infos, o, d);
535
536     if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
537         (d->source && pa_source_check_suspend(d->source) <= 0))
538         restart(d);
539
540     return PA_HOOK_OK;
541 }
542
543 static void device_info_free(struct device_info *d) {
544     pa_assert(d);
545
546     if (d->source)
547         pa_source_unref(d->source);
548     if (d->sink)
549         pa_sink_unref(d->sink);
550
551     d->userdata->core->mainloop->time_free(d->time_event);
552
553     pa_xfree(d);
554 }
555
556 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
557     struct device_info *d;
558
559     pa_assert(c);
560     pa_object_assert_ref(o);
561     pa_assert(u);
562
563     if ((d = pa_hashmap_remove(u->device_infos, o)))
564         device_info_free(d);
565
566     return PA_HOOK_OK;
567 }
568
569 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
570     struct device_info *d;
571
572     pa_assert(c);
573     pa_object_assert_ref(o);
574     pa_assert(u);
575
576     if (!(d = pa_hashmap_get(u->device_infos, o)))
577         return PA_HOOK_OK;
578
579     if (pa_sink_isinstance(o)) {
580         pa_sink *s = PA_SINK(o);
581         pa_sink_state_t state = pa_sink_get_state(s);
582
583         if (pa_sink_check_suspend(s) <= 0)
584             if (PA_SINK_IS_OPENED(state))
585                 restart(d);
586
587     } else if (pa_source_isinstance(o)) {
588         pa_source *s = PA_SOURCE(o);
589         pa_source_state_t state = pa_source_get_state(s);
590
591         if (pa_source_check_suspend(s) <= 0)
592             if (PA_SOURCE_IS_OPENED(state))
593                 restart(d);
594     }
595
596     return PA_HOOK_OK;
597 }
598
599 int pa__init(pa_module*m) {
600     pa_modargs *ma = NULL;
601     struct userdata *u;
602     uint32_t timeout = 5;
603     uint32_t idx;
604     pa_sink *sink;
605     pa_source *source;
606
607     pa_assert(m);
608
609     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
610         pa_log("Failed to parse module arguments.");
611         goto fail;
612     }
613
614     if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
615         pa_log("Failed to parse timeout value.");
616         goto fail;
617     }
618
619     m->userdata = u = pa_xnew(struct userdata, 1);
620     u->core = m->core;
621     u->timeout = timeout * PA_USEC_PER_SEC;
622     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);
623 #ifdef USE_PM_LOCK
624     u->pm_state = 0x00;
625 #endif /* USE_PM_LOCK */
626
627     PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
628         device_new_hook_cb(m->core, PA_OBJECT(sink), u);
629
630     PA_IDXSET_FOREACH(source, m->core->sources, idx)
631         device_new_hook_cb(m->core, PA_OBJECT(source), u);
632
633     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);
634     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);
635     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);
636     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);
637     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);
638     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);
639
640     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);
641     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);
642     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);
643     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);
644     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);
645     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);
646     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);
647     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);
648     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);
649     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);
650
651     pa_modargs_free(ma);
652     return 0;
653
654 fail:
655
656     if (ma)
657         pa_modargs_free(ma);
658
659     return -1;
660 }
661
662 void pa__done(pa_module*m) {
663     struct userdata *u;
664
665     pa_assert(m);
666
667     if (!m->userdata)
668         return;
669
670     u = m->userdata;
671
672     if (u->sink_new_slot)
673         pa_hook_slot_free(u->sink_new_slot);
674     if (u->sink_unlink_slot)
675         pa_hook_slot_free(u->sink_unlink_slot);
676     if (u->sink_state_changed_slot)
677         pa_hook_slot_free(u->sink_state_changed_slot);
678
679     if (u->source_new_slot)
680         pa_hook_slot_free(u->source_new_slot);
681     if (u->source_unlink_slot)
682         pa_hook_slot_free(u->source_unlink_slot);
683     if (u->source_state_changed_slot)
684         pa_hook_slot_free(u->source_state_changed_slot);
685
686     if (u->sink_input_new_slot)
687         pa_hook_slot_free(u->sink_input_new_slot);
688     if (u->sink_input_unlink_slot)
689         pa_hook_slot_free(u->sink_input_unlink_slot);
690     if (u->sink_input_move_start_slot)
691         pa_hook_slot_free(u->sink_input_move_start_slot);
692     if (u->sink_input_move_finish_slot)
693         pa_hook_slot_free(u->sink_input_move_finish_slot);
694     if (u->sink_input_state_changed_slot)
695         pa_hook_slot_free(u->sink_input_state_changed_slot);
696
697     if (u->source_output_new_slot)
698         pa_hook_slot_free(u->source_output_new_slot);
699     if (u->source_output_unlink_slot)
700         pa_hook_slot_free(u->source_output_unlink_slot);
701     if (u->source_output_move_start_slot)
702         pa_hook_slot_free(u->source_output_move_start_slot);
703     if (u->source_output_move_finish_slot)
704         pa_hook_slot_free(u->source_output_move_finish_slot);
705     if (u->source_output_state_changed_slot)
706         pa_hook_slot_free(u->source_output_state_changed_slot);
707
708     pa_hashmap_free(u->device_infos);
709
710     pa_xfree(u);
711 }