some minor reformatting
[platform/upstream/pulseaudio.git] / src / modules / module-flat-volume.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
5   Copyright 2004-2006, 2008 Lennart Poettering
6
7   Contact: Marc-Andre Lureau <marc-andre.lureau@nokia.com>
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <regex.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/sink-input.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/macro.h>
46
47 #include "module-flat-volume-symdef.h"
48
49 PA_MODULE_AUTHOR("Marc-Andre Lureau");
50 PA_MODULE_DESCRIPTION("Flat volume");
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(TRUE);
53 PA_MODULE_USAGE("");
54
55 struct userdata {
56     pa_subscription *subscription;
57     pa_hook_slot *sink_input_set_volume_hook_slot;
58     pa_hook_slot *sink_input_fixate_hook_slot;
59 };
60
61 static void process_input_volume_change(
62         pa_cvolume *dest_volume,
63         const pa_cvolume *dest_virtual_volume,
64         pa_channel_map *dest_channel_map,
65         pa_sink_input *this,
66         pa_sink *sink) {
67
68     pa_sink_input *i;
69     uint32_t idx;
70     pa_cvolume max_volume, sink_volume;
71
72     pa_assert(dest_volume);
73     pa_assert(dest_virtual_volume);
74     pa_assert(dest_channel_map);
75     pa_assert(sink);
76
77     if (!(sink->flags & PA_SINK_DECIBEL_VOLUME))
78         return;
79
80     pa_log_debug("Sink input volume changed");
81
82     max_volume = *dest_virtual_volume;
83     pa_cvolume_remap(&max_volume, dest_channel_map, &sink->channel_map);
84
85     for (i = PA_SINK_INPUT(pa_idxset_first(sink->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(sink->inputs, &idx))) {
86         /* skip this sink-input if we are processing a volume change request */
87         if (this && this == i)
88             continue;
89
90         if (pa_cvolume_avg(&i->virtual_volume) > pa_cvolume_avg(&max_volume)) {
91             max_volume = i->virtual_volume;
92             pa_cvolume_remap(&max_volume, &i->channel_map, &sink->channel_map);
93         }
94     }
95
96     /* Set the master volume, and normalize inputs */
97     if (!pa_cvolume_equal(&max_volume, &sink->volume)) {
98         /* copied from pa_sink_set_volume(): need to call from here to avoid sink hooks */
99         /* alternatively, could create an extra function pa_sink_set_volume_full()? */
100         sink->volume = max_volume;
101         if (sink->set_volume && sink->set_volume(sink) < 0)
102             sink->set_volume = NULL;
103
104         if (!sink->set_volume)
105             pa_sink_set_soft_volume(sink, &max_volume);
106
107         pa_subscription_post(sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, sink->index);
108
109         pa_log_debug("sink = %.2f (changed)", (double)pa_cvolume_avg(&sink->volume)/PA_VOLUME_NORM);
110
111         /* Now, normalize each of the internal volume (client sink-input volume / sink master volume) */
112         for (i = PA_SINK_INPUT(pa_idxset_first(sink->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(sink->inputs, &idx))) {
113             /* skip this sink-input if we are processing a volume change request */
114             if (this && this == i)
115                 continue;
116
117             sink_volume = max_volume;
118             pa_cvolume_remap(&sink_volume, &sink->channel_map, &i->channel_map);
119             pa_sw_cvolume_divide(&i->volume, &i->virtual_volume, &sink_volume);
120             pa_log_debug("sink input { id = %d, flat = %.2f, true = %.2f }",
121                          i->index,
122                          (double)pa_cvolume_avg(&i->virtual_volume)/PA_VOLUME_NORM,
123                          (double)pa_cvolume_avg(&i->volume)/PA_VOLUME_NORM);
124             pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, &i->volume, 1), 0, NULL, pa_xfree);
125         }
126     } else
127         pa_log_debug("sink = %.2f", (double)pa_cvolume_avg(&sink->volume)/PA_VOLUME_NORM);
128
129     /* and this one */
130
131     sink_volume = max_volume;
132     pa_cvolume_remap(&sink_volume, &sink->channel_map, dest_channel_map);
133     pa_sw_cvolume_divide(dest_volume, dest_virtual_volume, &sink_volume);
134     pa_log_debug("caller sink input: { id = %d, flat = %.2f, true = %.2f }",
135                  this ? (int)this->index : -1,
136                  (double)pa_cvolume_avg(dest_virtual_volume)/PA_VOLUME_NORM,
137                  (double)pa_cvolume_avg(dest_volume)/PA_VOLUME_NORM);
138 }
139
140 static pa_hook_result_t sink_input_set_volume_hook_callback(pa_core *c, pa_sink_input_set_volume_data *this, struct userdata *u) {
141     pa_assert(this);
142     pa_assert(this->sink_input);
143
144     process_input_volume_change(&this->volume, &this->virtual_volume, &this->sink_input->channel_map,
145                                 this->sink_input, this->sink_input->sink);
146
147     return PA_HOOK_OK;
148 }
149
150 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *core, pa_sink_input_new_data *this, struct userdata *u) {
151     pa_assert(this);
152     pa_assert(this->sink);
153
154     process_input_volume_change(&this->volume, &this->virtual_volume, &this->channel_map,
155                                 NULL, this->sink);
156
157     return PA_HOOK_OK;
158 }
159
160 static void subscribe_callback(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
161     struct userdata *u = userdata;
162     pa_sink *sink;
163     pa_sink_input *i;
164     uint32_t iidx;
165     pa_cvolume this_volume;
166
167     pa_assert(core);
168     pa_assert(u);
169
170     if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
171         t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE))
172         return;
173
174     if (!(sink = pa_idxset_get_by_index(core->sinks, idx)))
175         return;
176
177     if (!(sink->flags & PA_SINK_DECIBEL_VOLUME))
178         return;
179
180     pa_log_debug("Sink volume changed");
181     pa_log_debug("sink = %.2f", (double)pa_cvolume_avg(pa_sink_get_volume(sink, FALSE)) / PA_VOLUME_NORM);
182
183     for (i = PA_SINK_INPUT(pa_idxset_first(sink->inputs, &iidx)); i; i = PA_SINK_INPUT(pa_idxset_next(sink->inputs, &iidx))) {
184         this_volume = *pa_sink_get_volume(sink, FALSE);
185         pa_cvolume_remap(&this_volume, &sink->channel_map, &i->channel_map);
186         pa_sw_cvolume_multiply(&i->virtual_volume, &i->volume, &this_volume);
187         pa_log_debug("sink input = { id = %d, flat = %.2f, true = %.2f }",
188                      i->index,
189                      (double)pa_cvolume_avg(&i->virtual_volume)/PA_VOLUME_NORM,
190                      (double)pa_cvolume_avg(&i->volume)/PA_VOLUME_NORM);
191         pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
192     }
193 }
194
195 int pa__init(pa_module*m) {
196     struct userdata *u;
197
198     pa_assert(m);
199
200     u = pa_xnew(struct userdata, 1);
201     m->userdata = u;
202
203     u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
204     u->sink_input_set_volume_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_set_volume_hook_callback, u);
205
206     u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK, subscribe_callback, u);
207
208     return 0;
209 }
210
211 void pa__done(pa_module*m) {
212     struct userdata* u;
213
214     pa_assert(m);
215
216     if (!(u = m->userdata))
217         return;
218
219     if (u->subscription)
220       pa_subscription_free(u->subscription);
221
222     if (u->sink_input_set_volume_hook_slot)
223         pa_hook_slot_free(u->sink_input_set_volume_hook_slot);
224     if (u->sink_input_fixate_hook_slot)
225         pa_hook_slot_free(u->sink_input_fixate_hook_slot);
226
227     pa_xfree(u);
228 }