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 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 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>
39 #include "subscribe.h"
41 #include "sink-input.h"
42 #include "module-match-symdef.h"
44 PA_MODULE_AUTHOR("Lennart Poettering")
45 PA_MODULE_DESCRIPTION("Sink input matching module")
46 PA_MODULE_USAGE("table=<filename>")
47 PA_MODULE_VERSION(PACKAGE_VERSION
)
49 #define WHITESPACE "\n\r \t"
51 #ifndef DEFAULT_CONFIG_DIR
52 #define DEFAULT_CONFIG_DIR "/etc/polypaudio"
55 #define DEFAULT_MATCH_TABLE_FILE DEFAULT_CONFIG_DIR"/match.table"
56 #define DEFAULT_MATCH_TABLE_FILE_USER ".polypaudio/match.table"
58 static const char* const valid_modargs
[] = {
71 struct pa_subscription
*subscription
;
74 static int load_rules(struct userdata
*u
, const char *filename
) {
78 struct rule
*end
= NULL
;
82 fopen(fn
= pa_xstrdup(filename
), "r") :
83 pa_open_config_file(DEFAULT_MATCH_TABLE_FILE
, DEFAULT_MATCH_TABLE_FILE_USER
, NULL
, &fn
);
86 pa_log(__FILE__
": failed to open file '%s': %s\n", fn
, strerror(errno
));
91 char *d
, *v
, *e
= NULL
;
97 if (!fgets(ln
, sizeof(ln
), f
))
104 if (ln
[0] == '#' || !*ln
)
107 d
= ln
+strcspn(ln
, WHITESPACE
);
108 v
= d
+strspn(d
, WHITESPACE
);
112 pa_log(__FILE__
": [%s:%u] failed to parse line - too few words\n", filename
, n
);
118 volume
= (pa_volume_t
) strtol(v
, &e
, 0);
121 pa_log(__FILE__
": [%s:%u] failed to parse volume\n", filename
, n
);
125 if (regcomp(®ex
, ln
, REG_EXTENDED
|REG_NOSUB
) != 0) {
126 pa_log(__FILE__
": [%s:%u] invalid regular expression\n", filename
, n
);
130 rule
= pa_xmalloc(sizeof(struct rule
));
132 rule
->volume
= volume
;
156 static void callback(struct pa_core
*c
, enum pa_subscription_event_type t
, uint32_t index
, void *userdata
) {
157 struct userdata
*u
= userdata
;
158 struct pa_sink_input
*si
;
162 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
))
165 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, index
))) {
166 pa_log(__FILE__
": WARNING: failed to get sink input\n");
173 for (r
= u
->rules
; r
; r
= r
->next
) {
174 if (!regexec(&r
->regex
, si
->name
, 0, NULL
, 0)) {
175 pa_log(__FILE__
": changing volume of sink input '%s' to 0x%03x\n", si
->name
, r
->volume
);
176 pa_sink_input_set_volume(si
, r
->volume
);
181 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
182 struct pa_modargs
*ma
= NULL
;
183 const char *table_file
;
187 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
)) ||
188 !(table_file
= pa_modargs_get_value(ma
, "table", NULL
))) {
189 pa_log(__FILE__
": Failed to parse module arguments\n");
193 u
= pa_xmalloc(sizeof(struct userdata
));
195 u
->subscription
= NULL
;
198 if (load_rules(u
, table_file
) < 0)
201 u
->subscription
= pa_subscription_new(c
, PA_SUBSCRIPTION_MASK_SINK_INPUT
, callback
, u
);
214 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
219 if (!(u
= m
->userdata
))
223 pa_subscription_free(u
->subscription
);
225 for (r
= u
->rules
; r
; r
= n
) {