diff options
-rw-r--r-- | src/modules/module-match.c | 66 | ||||
-rw-r--r-- | src/pulsecore/sink.c | 22 | ||||
-rw-r--r-- | src/pulsecore/sink.h | 2 |
3 files changed, 69 insertions, 21 deletions
diff --git a/src/modules/module-match.c b/src/modules/module-match.c index 769a6b59..8c825c4f 100644 --- a/src/modules/module-match.c +++ b/src/modules/module-match.c @@ -48,7 +48,8 @@ PA_MODULE_AUTHOR("Lennart Poettering"); PA_MODULE_DESCRIPTION("Playback stream expression matching module"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); -PA_MODULE_USAGE("table=<filename>"); +PA_MODULE_USAGE("table=<filename> " + "key=<property_key>"); #define WHITESPACE "\n\r \t" @@ -57,17 +58,20 @@ PA_MODULE_USAGE("table=<filename>"); static const char* const valid_modargs[] = { "table", + "key", NULL, }; struct rule { regex_t regex; pa_volume_t volume; + pa_proplist *proplist; struct rule *next; }; struct userdata { struct rule *rules; + char *property_key; pa_subscription *subscription; }; @@ -95,11 +99,12 @@ static int load_rules(struct userdata *u, const char *filename) { while (!feof(f)) { char *d, *v; - pa_volume_t volume; + pa_volume_t volume = PA_VOLUME_NORM; uint32_t k; regex_t regex; char ln[256]; struct rule *rule; + pa_proplist *proplist = NULL; if (!fgets(ln, sizeof(ln), f)) break; @@ -121,14 +126,33 @@ static int load_rules(struct userdata *u, const char *filename) { } *d = 0; - if (pa_atou(v, &k) < 0) { - pa_log("[%s:%u] failed to parse volume", filename, n); - goto finish; + if (pa_atou(v, &k) >= 0) { + volume = (pa_volume_t) k; + } else if (*v == '"') { + char *e; + + e = strchr(v+1, '"'); + if (!e) { + pa_log(__FILE__ ": [%s:%u] failed to parse line - missing role closing quote", filename, n); + goto finish; + } + + *e = '\0'; + e = pa_sprintf_malloc("media.role=\"%s\"", v+1); + proplist = pa_proplist_from_string(e); + pa_xfree(e); + } else { + char *e; + + e = v+strspn(v, WHITESPACE); + if (!*e) { + pa_log(__FILE__ ": [%s:%u] failed to parse line - missing end of property list", filename, n); + goto finish; + } + *e = '\0'; + proplist = pa_proplist_from_string(v); } - volume = (pa_volume_t) k; - - if (regcomp(®ex, ln, REG_EXTENDED|REG_NOSUB) != 0) { pa_log("[%s:%u] invalid regular expression", filename, n); goto finish; @@ -136,6 +160,7 @@ static int load_rules(struct userdata *u, const char *filename) { rule = pa_xnew(struct rule, 1); rule->regex = regex; + rule->proplist = proplist; rule->volume = volume; rule->next = NULL; @@ -177,15 +202,22 @@ static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, v if (!(si = pa_idxset_get_by_index(c->sink_inputs, idx))) return; - if (!(n = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_NAME))) + if (!(n = pa_proplist_gets(si->proplist, u->property_key))) return; + pa_log_debug("Matching with %s", n); + for (r = u->rules; r; r = r->next) { if (!regexec(&r->regex, n, 0, NULL, 0)) { - pa_cvolume cv; - pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume); - pa_cvolume_set(&cv, si->sample_spec.channels, r->volume); - pa_sink_input_set_volume(si, &cv); + if (r->proplist) { + pa_log_debug("updating proplist of sink input '%s'", n); + pa_proplist_update(si->proplist, PA_UPDATE_MERGE, r->proplist); + } else { + pa_cvolume cv; + pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume); + pa_cvolume_set(&cv, si->sample_spec.channels, r->volume); + pa_sink_input_set_volume(si, &cv); + } } } } @@ -201,11 +233,14 @@ int pa__init(pa_module*m) { goto fail; } + u = pa_xnew(struct userdata, 1); u->rules = NULL; u->subscription = NULL; m->userdata = u; + u->property_key = pa_xstrdup(pa_modargs_get_value(ma, "key", PA_PROP_MEDIA_NAME)); + if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0) goto fail; @@ -234,10 +269,15 @@ void pa__done(pa_module*m) { if (u->subscription) pa_subscription_free(u->subscription); + if (u->property_key) + pa_xfree(u->property_key); + for (r = u->rules; r; r = n) { n = r->next; regfree(&r->regex); + if (r->proplist) + pa_proplist_free(r->proplist); pa_xfree(r); } diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c index dbc72fb4..d1d68cdd 100644 --- a/src/pulsecore/sink.c +++ b/src/pulsecore/sink.c @@ -193,6 +193,8 @@ pa_sink* pa_sink_new( s->volume = data->volume; s->base_volume = PA_VOLUME_NORM; + s->virtual_volume = s->volume; + s->muted = data->muted; s->refresh_volume = s->refresh_muted = FALSE; @@ -862,24 +864,26 @@ void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume) { pa_assert(pa_cvolume_compatible(volume, &s->sample_spec)); data.sink = s; - data.volume = *volume; + data.virtual_volume = data.volume = *volume; - changed = !pa_cvolume_equal(&data.volume, &s->volume); + changed = !pa_cvolume_equal(&data.virtual_volume, &s->virtual_volume) || + !pa_cvolume_equal(&data.volume, &s->volume); if (changed) { if (pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_SET_VOLUME], &data) < 0) return; - changed = !pa_cvolume_equal(&data.volume, &s->volume); + changed = !pa_cvolume_equal(&data.virtual_volume, &s->virtual_volume); /* from client-side view */ } s->volume = data.volume; + s->virtual_volume = data.virtual_volume; if (s->set_volume && s->set_volume(s) < 0) s->set_volume = NULL; if (!s->set_volume) - pa_sink_set_soft_volume(s, volume); + pa_sink_set_soft_volume(s, &s->volume); if (changed) pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index); @@ -902,19 +906,21 @@ const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh) { pa_assert(PA_SINK_IS_LINKED(s->state)); if (s->refresh_volume || force_refresh) { - struct pa_cvolume old_volume = s->volume; + struct pa_cvolume old_volume = s->virtual_volume; if (s->get_volume && s->get_volume(s) < 0) s->get_volume = NULL; - if (!s->get_volume) + if (!s->get_volume) { pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, &s->volume, 0, NULL); + s->virtual_volume = s->volume; + } - if (!pa_cvolume_equal(&old_volume, &s->volume)) + if (!pa_cvolume_equal(&old_volume, &s->virtual_volume)) pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index); } - return &s->volume; + return &s->virtual_volume; } /* Called from main thread */ diff --git a/src/pulsecore/sink.h b/src/pulsecore/sink.h index 2bd83b63..507c1603 100644 --- a/src/pulsecore/sink.h +++ b/src/pulsecore/sink.h @@ -81,6 +81,7 @@ struct pa_sink { pa_source *monitor_source; pa_cvolume volume; + pa_cvolume virtual_volume; pa_bool_t muted; pa_volume_t base_volume; /* shall be constant */ @@ -214,6 +215,7 @@ void pa_sink_new_data_done(pa_sink_new_data *data); typedef struct pa_sink_set_volume_data { pa_sink *sink; pa_cvolume volume; + pa_cvolume virtual_volume; } pa_sink_set_volume_data; /* To be called exclusively by the sink driver, from main context */ |