+#define EXT_VERSION 1
+
+static void read_sink_format_reply(struct userdata *u, pa_tagstruct *reply, pa_sink *sink) {
+ struct perportentry *e;
+ char *name;
+
+ pa_assert(u);
+ pa_assert(reply);
+ pa_assert(sink);
+
+ pa_tagstruct_putu32(reply, PA_DEVICE_TYPE_SINK);
+ pa_tagstruct_putu32(reply, sink->index);
+
+ /* Read or create an entry */
+ name = pa_sprintf_malloc("sink:%s", sink->name);
+ if (!(e = perportentry_read(u, name, (sink->active_port ? sink->active_port->name : NULL)))) {
+ /* Fake a reply with PCM encoding supported */
+ pa_format_info *f = pa_format_info_new();
+
+ pa_tagstruct_putu8(reply, 1);
+ f->encoding = PA_ENCODING_PCM;
+ pa_tagstruct_put_format_info(reply, f);
+
+ pa_format_info_free(f);
+ } else {
+ uint32_t idx;
+ pa_format_info *f;
+
+ /* Write all the formats from the entry to the reply */
+ pa_tagstruct_putu8(reply, pa_idxset_size(e->formats));
+ PA_IDXSET_FOREACH(f, e->formats, idx) {
+ pa_tagstruct_put_format_info(reply, f);
+ }
+ }
+ pa_xfree(name);
+}
+
+static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connection *c, uint32_t tag, pa_tagstruct *t) {
+ struct userdata *u;
+ uint32_t command;
+ pa_tagstruct *reply = NULL;
+
+ pa_assert(p);
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(t);
+
+ u = m->userdata;
+
+ if (pa_tagstruct_getu32(t, &command) < 0)
+ goto fail;
+
+ reply = pa_tagstruct_new(NULL, 0);
+ pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
+ pa_tagstruct_putu32(reply, tag);
+
+ switch (command) {
+ case SUBCOMMAND_TEST: {
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ pa_tagstruct_putu32(reply, EXT_VERSION);
+ break;
+ }
+
+ case SUBCOMMAND_SUBSCRIBE: {
+
+ bool enabled;
+
+ if (pa_tagstruct_get_boolean(t, &enabled) < 0 ||
+ !pa_tagstruct_eof(t))
+ goto fail;
+
+ if (enabled)
+ pa_idxset_put(u->subscribed, c, NULL);
+ else
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+
+ break;
+ }
+
+ case SUBCOMMAND_READ_FORMATS_ALL: {
+ pa_sink *sink;
+ uint32_t idx;
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
+ read_sink_format_reply(u, reply, sink);
+ }
+
+ break;
+ }
+ case SUBCOMMAND_READ_FORMATS: {
+ pa_device_type_t type;
+ uint32_t sink_index;
+ pa_sink *sink;
+
+ pa_assert(reply);
+
+ /* Get the sink index and the number of formats from the tagstruct */
+ if (pa_tagstruct_getu32(t, &type) < 0 ||
+ pa_tagstruct_getu32(t, &sink_index) < 0)
+ goto fail;
+
+ if (type != PA_DEVICE_TYPE_SINK) {
+ pa_log("Device format reading is only supported on sinks");
+ goto fail;
+ }
+
+ if (!pa_tagstruct_eof(t))
+ goto fail;
+
+ /* Now find our sink */
+ if (!(sink = pa_idxset_get_by_index(u->core->sinks, sink_index)))
+ goto fail;
+
+ read_sink_format_reply(u, reply, sink);
+
+ break;
+ }
+
+ case SUBCOMMAND_SAVE_FORMATS: {
+
+ struct perportentry *e;
+ pa_device_type_t type;
+ uint32_t sink_index;
+ char *name;
+ pa_sink *sink;
+ uint8_t i, n_formats;
+
+ /* Get the sink index and the number of formats from the tagstruct */
+ if (pa_tagstruct_getu32(t, &type) < 0 ||
+ pa_tagstruct_getu32(t, &sink_index) < 0 ||
+ pa_tagstruct_getu8(t, &n_formats) < 0 || n_formats < 1) {
+
+ goto fail;
+ }
+
+ if (type != PA_DEVICE_TYPE_SINK) {
+ pa_log("Device format saving is only supported on sinks");
+ goto fail;
+ }
+
+ /* Now find our sink */
+ if (!(sink = pa_idxset_get_by_index(u->core->sinks, sink_index))) {
+ pa_log("Could not find sink #%d", sink_index);
+ goto fail;
+ }
+
+ /* Read or create an entry */
+ name = pa_sprintf_malloc("sink:%s", sink->name);
+ if (!(e = perportentry_read(u, name, (sink->active_port ? sink->active_port->name : NULL))))
+ e = perportentry_new(false);
+ else {
+ /* Clean out any saved formats */
+ pa_idxset_free(e->formats, (pa_free_cb_t) pa_format_info_free);
+ e->formats = pa_idxset_new(NULL, NULL);
+ }
+
+ /* Read all the formats from our tagstruct */
+ for (i = 0; i < n_formats; ++i) {
+ pa_format_info *f = pa_format_info_new();
+ if (pa_tagstruct_get_format_info(t, f) < 0) {
+ pa_format_info_free(f);
+ pa_xfree(name);
+ goto fail;
+ }
+ pa_idxset_put(e->formats, f, NULL);
+ }
+
+ if (!pa_tagstruct_eof(t)) {
+ perportentry_free(e);
+ pa_xfree(name);
+ goto fail;
+ }
+
+ if (pa_sink_set_formats(sink, e->formats) && perportentry_write(u, name, (sink->active_port ? sink->active_port->name : NULL), e))
+ trigger_save(u, type, sink_index);
+ else
+ pa_log_warn("Could not save format info for sink %s", sink->name);
+
+ pa_xfree(name);
+ perportentry_free(e);
+
+ break;
+ }
+
+ default:
+ goto fail;
+ }
+
+ pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
+ return 0;
+
+fail:
+
+ if (reply)
+ pa_tagstruct_free(reply);
+
+ return -1;
+}
+
+static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_native_connection *c, struct userdata *u) {
+ pa_assert(p);
+ pa_assert(c);
+ pa_assert(u);
+
+ pa_idxset_remove_by_data(u->subscribed, c, NULL);
+ return PA_HOOK_OK;
+}
+