2004-09-27 21:05:55 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-09-27 21:05:55 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-09-27 21:05:55 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-09-27 21:05:55 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-09-27 21:05:55 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <regex.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
|
|
|
|
#include <pulsecore/module.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/modargs.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/core-subscribe.h>
|
|
|
|
|
#include <pulsecore/sink-input.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
2006-02-16 19:19:58 +00:00
|
|
|
|
2004-10-30 01:55:16 +00:00
|
|
|
#include "module-match-symdef.h"
|
2004-09-27 21:05:55 +00:00
|
|
|
|
|
|
|
|
PA_MODULE_AUTHOR("Lennart Poettering")
|
2006-05-14 00:41:18 +00:00
|
|
|
PA_MODULE_DESCRIPTION("Playback stream expression matching module")
|
2004-09-27 21:05:55 +00:00
|
|
|
PA_MODULE_USAGE("table=<filename>")
|
|
|
|
|
PA_MODULE_VERSION(PACKAGE_VERSION)
|
|
|
|
|
|
|
|
|
|
#define WHITESPACE "\n\r \t"
|
|
|
|
|
|
2006-07-19 17:44:19 +00:00
|
|
|
#define DEFAULT_MATCH_TABLE_FILE PA_DEFAULT_CONFIG_DIR"/match.table"
|
2006-06-19 23:51:58 +00:00
|
|
|
#define DEFAULT_MATCH_TABLE_FILE_USER ".pulse/match.table"
|
2004-11-04 21:27:12 +00:00
|
|
|
|
2004-09-27 21:05:55 +00:00
|
|
|
static const char* const valid_modargs[] = {
|
|
|
|
|
"table",
|
|
|
|
|
NULL,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct rule {
|
|
|
|
|
regex_t regex;
|
|
|
|
|
pa_volume_t volume;
|
|
|
|
|
struct rule *next;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct userdata {
|
|
|
|
|
struct rule *rules;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_subscription *subscription;
|
2004-09-27 21:05:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int load_rules(struct userdata *u, const char *filename) {
|
|
|
|
|
FILE *f;
|
|
|
|
|
int n = 0;
|
|
|
|
|
int ret = -1;
|
|
|
|
|
struct rule *end = NULL;
|
2004-11-04 21:27:12 +00:00
|
|
|
char *fn = NULL;
|
2004-09-27 21:05:55 +00:00
|
|
|
|
2004-11-04 21:27:12 +00:00
|
|
|
f = filename ?
|
|
|
|
|
fopen(fn = pa_xstrdup(filename), "r") :
|
2006-05-14 00:41:18 +00:00
|
|
|
pa_open_config_file(DEFAULT_MATCH_TABLE_FILE, DEFAULT_MATCH_TABLE_FILE_USER, NULL, &fn, "r");
|
2004-11-04 21:27:12 +00:00
|
|
|
|
|
|
|
|
if (!f) {
|
2006-05-22 15:20:46 +00:00
|
|
|
pa_log(__FILE__": failed to open file '%s': %s", fn, pa_cstrerror(errno));
|
2004-09-27 21:05:55 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-17 15:21:34 +00:00
|
|
|
pa_lock_fd(fileno(f), 1);
|
|
|
|
|
|
2004-09-27 21:05:55 +00:00
|
|
|
while (!feof(f)) {
|
2004-12-11 00:10:41 +00:00
|
|
|
char *d, *v;
|
2004-09-27 21:05:55 +00:00
|
|
|
pa_volume_t volume;
|
2004-12-11 00:10:41 +00:00
|
|
|
uint32_t k;
|
2004-09-27 21:05:55 +00:00
|
|
|
regex_t regex;
|
|
|
|
|
char ln[256];
|
|
|
|
|
struct rule *rule;
|
|
|
|
|
|
|
|
|
|
if (!fgets(ln, sizeof(ln), f))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
|
|
pa_strip_nl(ln);
|
|
|
|
|
|
|
|
|
|
if (ln[0] == '#' || !*ln )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
d = ln+strcspn(ln, WHITESPACE);
|
|
|
|
|
v = d+strspn(d, WHITESPACE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!*v) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__ ": [%s:%u] failed to parse line - too few words", filename, n);
|
2004-09-27 21:05:55 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*d = 0;
|
2004-12-11 00:10:41 +00:00
|
|
|
if (pa_atou(v, &k) < 0) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": [%s:%u] failed to parse volume", filename, n);
|
2004-09-27 21:05:55 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-11 00:10:41 +00:00
|
|
|
volume = (pa_volume_t) k;
|
|
|
|
|
|
|
|
|
|
|
2004-09-27 21:05:55 +00:00
|
|
|
if (regcomp(®ex, ln, REG_EXTENDED|REG_NOSUB) != 0) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": [%s:%u] invalid regular expression", filename, n);
|
2004-09-27 21:05:55 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule = pa_xmalloc(sizeof(struct rule));
|
|
|
|
|
rule->regex = regex;
|
|
|
|
|
rule->volume = volume;
|
|
|
|
|
rule->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
|
end->next = rule;
|
|
|
|
|
else
|
|
|
|
|
u->rules = rule;
|
|
|
|
|
end = rule;
|
|
|
|
|
|
|
|
|
|
*d = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
2006-05-17 15:21:34 +00:00
|
|
|
if (f) {
|
|
|
|
|
pa_lock_fd(fileno(f), 0);
|
2004-09-27 21:05:55 +00:00
|
|
|
fclose(f);
|
2006-05-17 15:21:34 +00:00
|
|
|
}
|
2004-09-27 21:05:55 +00:00
|
|
|
|
2004-11-04 21:27:12 +00:00
|
|
|
if (fn)
|
|
|
|
|
pa_xfree(fn);
|
|
|
|
|
|
2004-09-27 21:05:55 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
|
2004-09-27 21:05:55 +00:00
|
|
|
struct userdata *u = userdata;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink_input *si;
|
2004-09-27 21:05:55 +00:00
|
|
|
struct rule *r;
|
|
|
|
|
assert(c && u);
|
|
|
|
|
|
|
|
|
|
if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW))
|
|
|
|
|
return;
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx)))
|
2004-09-27 21:05:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!si->name)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (r = u->rules; r; r = r->next) {
|
|
|
|
|
if (!regexec(&r->regex, si->name, 0, NULL, 0)) {
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_cvolume cv;
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log_debug(__FILE__": changing volume of sink input '%s' to 0x%03x", si->name, r->volume);
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_cvolume_set(&cv, r->volume, si->sample_spec.channels);
|
|
|
|
|
pa_sink_input_set_volume(si, &cv);
|
2004-09-27 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa__init(pa_core *c, pa_module*m) {
|
|
|
|
|
pa_modargs *ma = NULL;
|
2004-09-27 21:05:55 +00:00
|
|
|
struct userdata *u;
|
|
|
|
|
assert(c && m);
|
|
|
|
|
|
2005-01-08 21:36:53 +00:00
|
|
|
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
|
2006-02-23 02:27:19 +00:00
|
|
|
pa_log(__FILE__": Failed to parse module arguments");
|
2004-09-28 22:47:48 +00:00
|
|
|
goto fail;
|
2004-09-27 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u = pa_xmalloc(sizeof(struct userdata));
|
|
|
|
|
u->rules = NULL;
|
|
|
|
|
u->subscription = NULL;
|
2004-09-28 22:47:48 +00:00
|
|
|
m->userdata = u;
|
2004-09-27 21:05:55 +00:00
|
|
|
|
2005-01-08 21:36:53 +00:00
|
|
|
if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
|
2004-09-28 22:47:48 +00:00
|
|
|
goto fail;
|
2004-09-27 21:05:55 +00:00
|
|
|
|
|
|
|
|
u->subscription = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
|
|
|
|
|
|
2004-09-28 22:47:48 +00:00
|
|
|
pa_modargs_free(ma);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa__done(c, m);
|
|
|
|
|
|
2004-09-27 21:05:55 +00:00
|
|
|
if (ma)
|
|
|
|
|
pa_modargs_free(ma);
|
2004-09-28 22:47:48 +00:00
|
|
|
return -1;
|
2004-09-27 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa__done(pa_core *c, pa_module*m) {
|
2004-09-27 21:05:55 +00:00
|
|
|
struct userdata* u;
|
|
|
|
|
struct rule *r, *n;
|
|
|
|
|
assert(c && m);
|
|
|
|
|
|
|
|
|
|
if (!(u = m->userdata))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (u->subscription)
|
|
|
|
|
pa_subscription_free(u->subscription);
|
|
|
|
|
|
|
|
|
|
for (r = u->rules; r; r = n) {
|
|
|
|
|
n = r->next;
|
|
|
|
|
|
|
|
|
|
regfree(&r->regex);
|
|
|
|
|
pa_xfree(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|