lirc: fix logic behind mute buttons
[profile/ivi/pulseaudio-panda.git] / src / modules / module-cork-music-on-phone.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 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 <pulsecore/macro.h>
27 #include <pulsecore/hashmap.h>
28 #include <pulsecore/hook-list.h>
29 #include <pulsecore/core.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/modargs.h>
33
34 #include "module-cork-music-on-phone-symdef.h"
35
36 PA_MODULE_AUTHOR("Lennart Poettering");
37 PA_MODULE_DESCRIPTION("Mute or cork music while a phone stream exists");
38 PA_MODULE_VERSION(PACKAGE_VERSION);
39 PA_MODULE_LOAD_ONCE(TRUE);
40
41 static const char* const valid_modargs[] = {
42     NULL
43 };
44
45 struct userdata {
46     pa_core *core;
47     pa_hashmap *cork_state;
48     pa_hook_slot
49         *sink_input_put_slot,
50         *sink_input_unlink_slot,
51         *sink_input_move_start_slot,
52         *sink_input_move_finish_slot;
53 };
54
55 static pa_bool_t shall_cork(pa_sink *s, pa_sink_input *ignore) {
56     pa_sink_input *j;
57     uint32_t idx;
58     pa_sink_assert_ref(s);
59
60     for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
61         const char *role;
62
63         if (j == ignore)
64             continue;
65
66         if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
67             continue;
68
69         if (pa_streq(role, "phone"))
70             return TRUE;
71     }
72
73     return FALSE;
74 }
75
76 static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, pa_bool_t cork) {
77     pa_sink_input *j;
78     uint32_t idx;
79
80     pa_assert(u);
81     pa_sink_assert_ref(s);
82
83     for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
84         pa_bool_t corked;
85         const char *role;
86
87         if (j == ignore)
88             continue;
89
90         if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
91             continue;
92
93         if (!pa_streq(role, "video") &&
94             !pa_streq(role, "music"))
95             continue;
96
97         corked = !!pa_hashmap_get(u->cork_state, j);
98
99         if (cork && !corked) {
100             pa_hashmap_put(u->cork_state, j, PA_INT_TO_PTR(1));
101             pa_sink_input_set_mute(j, TRUE, FALSE);
102             pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_CORK, NULL);
103         } else if (!cork) {
104             pa_hashmap_remove(u->cork_state, j);
105
106             if (corked) {
107                 pa_sink_input_set_mute(j, FALSE, FALSE);
108                 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_UNCORK, NULL);
109             }
110         }
111     }
112 }
113
114 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, pa_bool_t create) {
115     pa_bool_t cork = FALSE;
116     const char *role;
117
118     pa_assert(u);
119     pa_sink_input_assert_ref(i);
120
121     if (!create)
122         pa_hashmap_remove(u->cork_state, i);
123
124     if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
125         return PA_HOOK_OK;
126
127     if (!pa_streq(role, "phone") &&
128         !pa_streq(role, "music") &&
129         !pa_streq(role, "video"))
130         return PA_HOOK_OK;
131
132     cork = shall_cork(i->sink, create ? NULL : i);
133     apply_cork(u, i->sink, create ? NULL : i, cork);
134
135     return PA_HOOK_OK;
136 }
137
138 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
139     pa_core_assert_ref(core);
140     pa_sink_input_assert_ref(i);
141
142     return process(u, i, TRUE);
143 }
144
145 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
146     pa_core_assert_ref(core);
147     pa_sink_input_assert_ref(i);
148
149     return process(u, i, FALSE);
150 }
151
152 static pa_hook_result_t sink_input_move_start_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
153     pa_core_assert_ref(core);
154     pa_sink_input_assert_ref(i);
155
156     return process(u, i, FALSE);
157 }
158
159 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
160     pa_core_assert_ref(core);
161     pa_sink_input_assert_ref(i);
162
163     return process(u, i, TRUE);
164 }
165
166 int pa__init(pa_module *m) {
167     pa_modargs *ma = NULL;
168     struct userdata *u;
169
170     pa_assert(m);
171
172     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
173         pa_log("Failed to parse module arguments");
174         goto fail;
175     }
176
177     m->userdata = u = pa_xnew(struct userdata, 1);
178
179     u->core = m->core;
180     u->cork_state = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
181
182     u->sink_input_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_put_cb, u);
183     u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_unlink_cb, u);
184     u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_start_cb, u);
185     u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_finish_cb, u);
186
187     pa_modargs_free(ma);
188
189     return 0;
190
191 fail:
192     pa__done(m);
193
194     if (ma)
195         pa_modargs_free(ma);
196
197     return  -1;
198
199
200 }
201
202 void pa__done(pa_module *m) {
203     struct userdata* u;
204
205     pa_assert(m);
206
207     if (!(u = m->userdata))
208         return;
209
210     if (u->sink_input_put_slot)
211         pa_hook_slot_free(u->sink_input_put_slot);
212     if (u->sink_input_unlink_slot)
213         pa_hook_slot_free(u->sink_input_unlink_slot);
214     if (u->sink_input_move_start_slot)
215         pa_hook_slot_free(u->sink_input_move_start_slot);
216     if (u->sink_input_move_finish_slot)
217         pa_hook_slot_free(u->sink_input_move_finish_slot);
218
219     if (u->cork_state)
220         pa_hashmap_free(u->cork_state, NULL, NULL);
221
222     pa_xfree(u);
223
224 }