4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <sys/types.h>
35 #include <polyp/xmalloc.h>
37 #include <polypcore/core-error.h>
38 #include <polypcore/module.h>
39 #include <polypcore/core-util.h>
40 #include <polypcore/modargs.h>
41 #include <polypcore/log.h>
42 #include <polypcore/core-subscribe.h>
43 #include <polypcore/sink-input.h>
44 #include <polypcore/core-util.h>
46 #include "module-match-symdef.h"
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("Playback stream expression matching module")
50 PA_MODULE_USAGE("table=<filename>")
51 PA_MODULE_VERSION(PACKAGE_VERSION
)
53 #define WHITESPACE "\n\r \t"
55 #ifndef DEFAULT_CONFIG_DIR
56 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
59 #define DEFAULT_MATCH_TABLE_FILE DEFAULT_CONFIG_DIR"/match.table"
60 #define DEFAULT_MATCH_TABLE_FILE_USER ".polypaudio/match.table"
62 static const char* const valid_modargs
[] = {
75 pa_subscription
*subscription
;
78 static int load_rules(struct userdata
*u
, const char *filename
) {
82 struct rule
*end
= NULL
;
86 fopen(fn
= pa_xstrdup(filename
), "r") :
87 pa_open_config_file(DEFAULT_MATCH_TABLE_FILE
, DEFAULT_MATCH_TABLE_FILE_USER
, NULL
, &fn
, "r");
90 pa_log(__FILE__
": failed to open file '%s': %s", fn
, pa_cstrerror(errno
));
94 pa_lock_fd(fileno(f
), 1);
104 if (!fgets(ln
, sizeof(ln
), f
))
111 if (ln
[0] == '#' || !*ln
)
114 d
= ln
+strcspn(ln
, WHITESPACE
);
115 v
= d
+strspn(d
, WHITESPACE
);
119 pa_log(__FILE__
": [%s:%u] failed to parse line - too few words", filename
, n
);
124 if (pa_atou(v
, &k
) < 0) {
125 pa_log(__FILE__
": [%s:%u] failed to parse volume", filename
, n
);
129 volume
= (pa_volume_t
) k
;
132 if (regcomp(®ex
, ln
, REG_EXTENDED
|REG_NOSUB
) != 0) {
133 pa_log(__FILE__
": [%s:%u] invalid regular expression", filename
, n
);
137 rule
= pa_xmalloc(sizeof(struct rule
));
139 rule
->volume
= volume
;
155 pa_lock_fd(fileno(f
), 0);
165 static void callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
166 struct userdata
*u
= userdata
;
171 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
))
174 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
180 for (r
= u
->rules
; r
; r
= r
->next
) {
181 if (!regexec(&r
->regex
, si
->name
, 0, NULL
, 0)) {
183 pa_log_debug(__FILE__
": changing volume of sink input '%s' to 0x%03x", si
->name
, r
->volume
);
184 pa_cvolume_set(&cv
, r
->volume
, si
->sample_spec
.channels
);
185 pa_sink_input_set_volume(si
, &cv
);
190 int pa__init(pa_core
*c
, pa_module
*m
) {
191 pa_modargs
*ma
= NULL
;
195 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
196 pa_log(__FILE__
": Failed to parse module arguments");
200 u
= pa_xmalloc(sizeof(struct userdata
));
202 u
->subscription
= NULL
;
205 if (load_rules(u
, pa_modargs_get_value(ma
, "table", NULL
)) < 0)
208 u
->subscription
= pa_subscription_new(c
, PA_SUBSCRIPTION_MASK_SINK_INPUT
, callback
, u
);
221 void pa__done(pa_core
*c
, pa_module
*m
) {
226 if (!(u
= m
->userdata
))
230 pa_subscription_free(u
->subscription
);
232 for (r
= u
->rules
; r
; r
= n
) {