Add "Rear Mic" to alsa mixer paths.
[profile/ivi/pulseaudio.git] / src / modules / module-position-event-sounds.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 <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulse/volume.h>
36 #include <pulse/channelmap.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/sink-input.h>
44
45 #include "module-position-event-sounds-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering");
48 PA_MODULE_DESCRIPTION("Position event sounds between L and R depending on the position on screen of the widget triggering them.");
49 PA_MODULE_VERSION(PACKAGE_VERSION);
50 PA_MODULE_LOAD_ONCE(TRUE);
51
52 static const char* const valid_modargs[] = {
53     NULL
54 };
55
56 struct userdata {
57     pa_hook_slot *sink_input_fixate_hook_slot;
58 };
59
60 static int parse_pos(const char *pos, double *f) {
61
62     if (pa_atod(pos, f) < 0) {
63         pa_log_warn("Failed to parse hpos/vpos property '%s'.", pos);
64         return -1;
65     }
66
67     if (*f < 0.0 || *f > 1.0) {
68         pa_log_debug("Property hpos/vpos out of range %0.2f", *f);
69
70         *f = PA_CLAMP(*f, 0.0, 1.0);
71     }
72
73     return 0;
74 }
75
76 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *core, pa_sink_input_new_data *data, struct userdata *u) {
77     const char *hpos, *vpos, *role, *id;
78     double f;
79     char t[PA_CVOLUME_SNPRINT_MAX];
80     pa_cvolume v;
81
82     pa_assert(data);
83
84     if (!(role = pa_proplist_gets(data->proplist, PA_PROP_MEDIA_ROLE)))
85         return PA_HOOK_OK;
86
87     if (!pa_streq(role, "event"))
88         return PA_HOOK_OK;
89
90     if ((id = pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID))) {
91
92         /* The test sounds should never be positioned in space, since
93          * they might be trigered themselves to configure the speakers
94          * in space, which we don't want to mess up. */
95
96         if (pa_startswith(id, "audio-channel-"))
97             return PA_HOOK_OK;
98
99         if (pa_streq(id, "audio-volume-change"))
100             return PA_HOOK_OK;
101
102         if (pa_streq(id, "audio-test-signal"))
103             return PA_HOOK_OK;
104     }
105
106     if (!(hpos = pa_proplist_gets(data->proplist, PA_PROP_EVENT_MOUSE_HPOS)))
107         hpos = pa_proplist_gets(data->proplist, PA_PROP_WINDOW_HPOS);
108
109     if (!(vpos = pa_proplist_gets(data->proplist, PA_PROP_EVENT_MOUSE_VPOS)))
110         vpos = pa_proplist_gets(data->proplist, PA_PROP_WINDOW_VPOS);
111
112     if (!hpos && !vpos)
113         return PA_HOOK_OK;
114
115     pa_cvolume_reset(&v, data->sink->sample_spec.channels);
116
117     if (hpos) {
118         if (parse_pos(hpos, &f) < 0)
119             return PA_HOOK_OK;
120
121         if (pa_channel_map_can_balance(&data->sink->channel_map)) {
122             pa_log_debug("Positioning event sound '%s' horizontally at %0.2f.", pa_strnull(pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID)), f);
123             pa_cvolume_set_balance(&v, &data->sink->channel_map, f*2.0-1.0);
124         }
125     }
126
127     if (vpos) {
128         if (parse_pos(vpos, &f) < 0)
129             return PA_HOOK_OK;
130
131         if (pa_channel_map_can_fade(&data->sink->channel_map)) {
132             pa_log_debug("Positioning event sound '%s' vertically at %0.2f.", pa_strnull(pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID)), f);
133             pa_cvolume_set_fade(&v, &data->sink->channel_map, f*2.0-1.0);
134         }
135     }
136
137     pa_log_debug("Final volume factor %s.", pa_cvolume_snprint(t, sizeof(t), &v));
138     pa_sink_input_new_data_apply_volume_factor_sink(data, &v);
139
140     return PA_HOOK_OK;
141 }
142
143 int pa__init(pa_module*m) {
144     pa_modargs *ma = NULL;
145     struct userdata *u;
146
147     pa_assert(m);
148
149     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
150         pa_log("Failed to parse module arguments");
151         goto fail;
152     }
153
154     m->userdata = u = pa_xnew(struct userdata, 1);
155     u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
156
157     pa_modargs_free(ma);
158
159     return 0;
160
161 fail:
162     pa__done(m);
163
164     if (ma)
165         pa_modargs_free(ma);
166
167     return  -1;
168 }
169
170 void pa__done(pa_module*m) {
171     struct userdata* u;
172
173     pa_assert(m);
174
175     if (!(u = m->userdata))
176         return;
177
178     if (u->sink_input_fixate_hook_slot)
179         pa_hook_slot_free(u->sink_input_fixate_hook_slot);
180
181     pa_xfree(u);
182 }