示例#1
0
void triggers_save(Trigger triggers[]){
    Serial.println(F("Save trigers"));
    for (int i=0; i < TRIGGERS; i++) {
        trigger_save(triggers, i);
    }
#ifdef DEBUG_TRIGGERS
    Serial.println(F("Saved."));
#endif
}
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
    struct userdata *u = userdata;
    struct entry entry, *old;
    char *name;
    pa_datum key, data;

    pa_assert(c);
    pa_assert(u);

    if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
        t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE) &&
        t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW) &&
        t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
        return;

    pa_zero(entry);
    entry.version = ENTRY_VERSION;

    if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
        pa_sink *sink;

        if (!(sink = pa_idxset_get_by_index(c->sinks, idx)))
            return;

        name = pa_sprintf_malloc("sink:%s", sink->name);

        if ((old = read_entry(u, name)))
            entry = *old;

        if (sink->save_volume) {
            entry.channel_map = sink->channel_map;
            entry.volume = *pa_sink_get_volume(sink, FALSE);
            entry.volume_valid = TRUE;
        }

        if (sink->save_muted) {
            entry.muted = pa_sink_get_mute(sink, FALSE);
            entry.muted_valid = TRUE;
        }

        if (sink->save_port) {
            pa_strlcpy(entry.port, sink->active_port ? sink->active_port->name : "", sizeof(entry.port));
            entry.port_valid = TRUE;
        }

    } else {
        pa_source *source;

        pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);

        if (!(source = pa_idxset_get_by_index(c->sources, idx)))
            return;

        name = pa_sprintf_malloc("source:%s", source->name);

        if ((old = read_entry(u, name)))
            entry = *old;

        if (source->save_volume) {
            entry.channel_map = source->channel_map;
            entry.volume = *pa_source_get_volume(source, FALSE);
            entry.volume_valid = TRUE;
        }

        if (source->save_muted) {
            entry.muted = pa_source_get_mute(source, FALSE);
            entry.muted_valid = TRUE;
        }

        if (source->save_port) {
            pa_strlcpy(entry.port, source->active_port ? source->active_port->name : "", sizeof(entry.port));
            entry.port_valid = TRUE;
        }
    }

    if (old) {

        if (entries_equal(old, &entry)) {
            pa_xfree(old);
            pa_xfree(name);
            return;
        }

        pa_xfree(old);
    }

    key.data = name;
    key.size = strlen(name);

    data.data = &entry;
    data.size = sizeof(entry);

    pa_log_info("Storing volume/mute/port for device %s.", name);

    pa_database_set(u->database, &key, &data, TRUE);

    pa_xfree(name);

    trigger_save(u);
}