2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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
29 #include <sys/types.h>
33 #if defined(HAVE_REGEX_H)
35 #elif defined(HAVE_PCREPOSIX_H)
36 #include <pcreposix.h>
39 #include <pulse/xmalloc.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/sink-input.h>
47 #include <pulsecore/core-util.h>
49 #include "module-match-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering");
52 PA_MODULE_DESCRIPTION("Playback stream expression matching module");
53 PA_MODULE_VERSION(PACKAGE_VERSION
);
54 PA_MODULE_LOAD_ONCE(true);
55 PA_MODULE_USAGE("table=<filename> "
56 "key=<property_key>");
58 #define WHITESPACE "\n\r \t"
60 #define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
61 #define DEFAULT_MATCH_TABLE_FILE_USER "match.table"
63 #define UPDATE_REPLACE "replace"
64 #define UPDATE_MERGE "merge"
66 static const char* const valid_modargs
[] = {
75 pa_proplist
*proplist
;
76 pa_update_mode_t mode
;
83 pa_hook_slot
*sink_input_fixate_hook_slot
;
86 static int load_rules(struct userdata
*u
, const char *filename
) {
90 struct rule
*end
= NULL
;
96 f
= pa_fopen_cloexec(fn
= pa_xstrdup(filename
), "r");
98 f
= pa_open_config_file(DEFAULT_MATCH_TABLE_FILE
, DEFAULT_MATCH_TABLE_FILE_USER
, NULL
, &fn
);
101 pa_log("Failed to open file config file: %s", pa_cstrerror(errno
));
105 pa_lock_fd(fileno(f
), 1);
108 char *token_end
, *value_str
;
109 pa_volume_t volume
= PA_VOLUME_NORM
;
114 pa_proplist
*proplist
= NULL
;
115 pa_update_mode_t mode
= (pa_update_mode_t
) -1;
117 if (!fgets(ln
, sizeof(ln
), f
))
124 if (ln
[0] == '#' || !*ln
)
127 token_end
= ln
+ strcspn(ln
, WHITESPACE
);
128 value_str
= token_end
+ strspn(token_end
, WHITESPACE
);
132 pa_log("[%s:%u] failed to parse line - missing regexp", fn
, n
);
137 pa_log("[%s:%u] failed to parse line - too few words", fn
, n
);
141 if (pa_atou(value_str
, &k
) >= 0)
142 volume
= (pa_volume_t
) PA_CLAMP_VOLUME(k
);
146 token_end
= value_str
+ strcspn(value_str
, WHITESPACE
);
148 len
= token_end
- value_str
;
149 if (len
== (sizeof(UPDATE_REPLACE
) - 1) && !strncmp(value_str
, UPDATE_REPLACE
, len
))
150 mode
= PA_UPDATE_REPLACE
;
151 else if (len
== (sizeof(UPDATE_MERGE
) - 1) && !strncmp(value_str
, UPDATE_MERGE
, len
))
152 mode
= PA_UPDATE_MERGE
;
154 if (mode
!= (pa_update_mode_t
) -1) {
155 value_str
= token_end
+ strspn(token_end
, WHITESPACE
);
158 pa_log("[%s:%u] failed to parse line - too few words", fn
, n
);
162 mode
= PA_UPDATE_MERGE
;
164 if (*value_str
== '"') {
167 token_end
= strchr(value_str
, '"');
169 pa_log("[%s:%u] failed to parse line - missing role closing quote", fn
, n
);
173 token_end
= value_str
+ strcspn(value_str
, WHITESPACE
);
177 value_str
= pa_sprintf_malloc("media.role=\"%s\"", value_str
);
178 proplist
= pa_proplist_from_string(value_str
);
182 if (regcomp(®ex
, ln
, REG_EXTENDED
|REG_NOSUB
) != 0) {
183 pa_log("[%s:%u] invalid regular expression", fn
, n
);
185 pa_proplist_free(proplist
);
189 rule
= pa_xnew(struct rule
, 1);
191 rule
->proplist
= proplist
;
193 rule
->volume
= volume
;
207 pa_lock_fd(fileno(f
), 0);
216 static pa_hook_result_t
sink_input_fixate_hook_callback(pa_core
*c
, pa_sink_input_new_data
*si
, struct userdata
*u
) {
223 if (!(n
= pa_proplist_gets(si
->proplist
, u
->property_key
)))
226 pa_log_debug("Matching with %s", n
);
228 for (r
= u
->rules
; r
; r
= r
->next
) {
229 if (!regexec(&r
->regex
, n
, 0, NULL
, 0)) {
231 pa_log_debug("updating proplist of sink input '%s'", n
);
232 pa_proplist_update(si
->proplist
, r
->mode
, r
->proplist
);
233 } else if (si
->volume_writable
) {
235 pa_log_debug("changing volume of sink input '%s' to 0x%03x", n
, r
->volume
);
236 pa_cvolume_set(&cv
, si
->sample_spec
.channels
, r
->volume
);
237 pa_sink_input_new_data_set_volume(si
, &cv
);
239 pa_log_debug("the volume of sink input '%s' is not writable, can't change it", n
);
246 int pa__init(pa_module
*m
) {
247 pa_modargs
*ma
= NULL
;
252 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
253 pa_log("Failed to parse module arguments");
257 u
= pa_xnew0(struct userdata
, 1);
261 u
->property_key
= pa_xstrdup(pa_modargs_get_value(ma
, "key", PA_PROP_MEDIA_NAME
));
263 if (load_rules(u
, pa_modargs_get_value(ma
, "table", NULL
)) < 0)
266 /* hook EARLY - 1, to match before stream-restore */
267 u
->sink_input_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_FIXATE
], PA_HOOK_EARLY
- 1, (pa_hook_cb_t
) sink_input_fixate_hook_callback
, u
);
280 void pa__done(pa_module
*m
) {
286 if (!(u
= m
->userdata
))
289 if (u
->sink_input_fixate_hook_slot
)
290 pa_hook_slot_free(u
->sink_input_fixate_hook_slot
);
293 pa_xfree(u
->property_key
);
295 for (r
= u
->rules
; r
; r
= n
) {
300 pa_proplist_free(r
->proplist
);