]> code.delx.au - pulseaudio/blob - src/modules/module-position-event-sounds.c
8e4f4c3260d6a6b7862a40072591563f4bf4eb39
[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 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_core *core;
58 pa_hook_slot *sink_input_fixate_hook_slot;
59 };
60
61 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *core, pa_sink_input_new_data *data, struct userdata *u) {
62 const char *hpos;
63 double f;
64 char t[PA_CVOLUME_SNPRINT_MAX];
65
66 pa_assert(data);
67
68 if (!(hpos = pa_proplist_gets(data->proplist, PA_PROP_EVENT_MOUSE_HPOS)))
69 return PA_HOOK_OK;
70
71 if (pa_atod(hpos, &f) < 0) {
72 pa_log_warn("Failed to parse "PA_PROP_EVENT_MOUSE_HPOS" property '%s'.", hpos);
73 return PA_HOOK_OK;
74 }
75
76 if (f < 0.0 || f > 1.0) {
77 pa_log_warn("Property "PA_PROP_EVENT_MOUSE_HPOS" out of range %0.2f", f);
78 return PA_HOOK_OK;
79 }
80
81 pa_log_debug("Positioning event sound '%s' at %0.2f.", pa_strnull(pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID)), f);
82
83 if (!data->virtual_volume_is_set) {
84 pa_cvolume_reset(&data->virtual_volume, data->sample_spec.channels);
85 data->virtual_volume_is_set = TRUE;
86 data->virtual_volume_is_absolute = FALSE;
87 }
88
89 pa_cvolume_set_balance(&data->virtual_volume, &data->channel_map, f*2.0-1.0);
90 data->save_volume = FALSE;
91
92 pa_log_debug("Final volume %s.", pa_cvolume_snprint(t, sizeof(t), &data->virtual_volume));
93
94 return PA_HOOK_OK;
95 }
96
97 int pa__init(pa_module*m) {
98 pa_modargs *ma = NULL;
99 struct userdata *u;
100
101 pa_assert(m);
102
103 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
104 pa_log("Failed to parse module arguments");
105 goto fail;
106 }
107
108 m->userdata = u = pa_xnew(struct userdata, 1);
109 u->core = m->core;
110 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);
111
112 pa_modargs_free(ma);
113
114 return 0;
115
116 fail:
117 pa__done(m);
118
119 if (ma)
120 pa_modargs_free(ma);
121
122 return -1;
123 }
124
125 void pa__done(pa_module*m) {
126 struct userdata* u;
127
128 pa_assert(m);
129
130 if (!(u = m->userdata))
131 return;
132
133 if (u->sink_input_fixate_hook_slot)
134 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
135
136 pa_xfree(u);
137 }