4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/types.h>
36 #include <pulse/xmalloc.h>
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/core-subscribe.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/core-util.h>
47 #include "module-match-symdef.h"
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("Playback stream expression matching module")
51 PA_MODULE_USAGE("table=<filename>")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 #define WHITESPACE "\n\r \t"
56 #define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
57 #define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
59 static const char* const valid_modargs
[] = {
72 pa_subscription
*subscription
;
75 static int load_rules(struct userdata
*u
, const char *filename
) {
79 struct rule
*end
= NULL
;
85 fopen(fn
= pa_xstrdup(filename
), "r") :
86 pa_open_config_file(DEFAULT_MATCH_TABLE_FILE
, DEFAULT_MATCH_TABLE_FILE_USER
, NULL
, &fn
, "r");
89 pa_log("failed to open file '%s': %s", fn
, pa_cstrerror(errno
));
93 pa_lock_fd(fileno(f
), 1);
103 if (!fgets(ln
, sizeof(ln
), f
))
110 if (ln
[0] == '#' || !*ln
)
113 d
= ln
+strcspn(ln
, WHITESPACE
);
114 v
= d
+strspn(d
, WHITESPACE
);
118 pa_log(__FILE__
": [%s:%u] failed to parse line - too few words", filename
, n
);
123 if (pa_atou(v
, &k
) < 0) {
124 pa_log("[%s:%u] failed to parse volume", filename
, n
);
128 volume
= (pa_volume_t
) k
;
131 if (regcomp(®ex
, ln
, REG_EXTENDED
|REG_NOSUB
) != 0) {
132 pa_log("[%s:%u] invalid regular expression", filename
, n
);
136 rule
= pa_xnew(struct rule
, 1);
138 rule
->volume
= volume
;
154 pa_lock_fd(fileno(f
), 0);
164 static void callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
165 struct userdata
*u
= userdata
;
172 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
))
175 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
181 for (r
= u
->rules
; r
; r
= r
->next
) {
182 if (!regexec(&r
->regex
, si
->name
, 0, NULL
, 0)) {
184 pa_log_debug("changing volume of sink input '%s' to 0x%03x", si
->name
, r
->volume
);
185 pa_cvolume_set(&cv
, si
->sample_spec
.channels
, r
->volume
);
186 pa_sink_input_set_volume(si
, &cv
);
191 int pa__init(pa_module
*m
) {
192 pa_modargs
*ma
= NULL
;
197 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
198 pa_log("Failed to parse module arguments");
202 u
= pa_xnew(struct userdata
, 1);
204 u
->subscription
= NULL
;
207 if (load_rules(u
, pa_modargs_get_value(ma
, "table", NULL
)) < 0)
210 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK_INPUT
, callback
, u
);
223 void pa__done(pa_module
*m
) {
229 if (!(u
= m
->userdata
))
233 pa_subscription_free(u
->subscription
);
235 for (r
= u
->rules
; r
; r
= n
) {