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