From 763f0f1795d0eccb7880e4d73a5ab050687cbbcb Mon Sep 17 00:00:00 2001
From: Julian Bouzas <julian.bouzas@collabora.com>
Date: Wed, 14 Oct 2020 12:07:12 -0400
Subject: [PATCH] m-monitor: add use-acp flag

---
 modules/module-device-activation.c |  6 +++++-
 modules/module-monitor.c           | 17 ++++++++++++++---
 src/config/wireplumber.conf        |  2 +-
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/modules/module-device-activation.c b/modules/module-device-activation.c
index cb9e7662..9020d948 100644
--- a/modules/module-device-activation.c
+++ b/modules/module-device-activation.c
@@ -89,7 +89,11 @@ on_device_added (WpObjectManager *om, WpProxy *proxy, gpointer d)
 
   /* ALSA */
   if (g_str_has_prefix (device_api, "alsa")) {
-    set_device_profile (proxy, 1);
+    const gchar *acp = wp_proxy_get_property (proxy, "device.api.alsa.acp");
+
+    /* Skip ACP devices as they are automatically enabled when created */
+    if (!acp || !atoi (acp))
+      set_device_profile (proxy, 1);
   }
 
   /* Bluez5 */
diff --git a/modules/module-monitor.c b/modules/module-monitor.c
index 41f5fa35..fa6d740c 100644
--- a/modules/module-monitor.c
+++ b/modules/module-monitor.c
@@ -21,6 +21,7 @@ G_DEFINE_QUARK (wp-module-monitor-children, children);
 typedef enum {
   FLAG_LOCAL_NODES = (1 << 0),
   FLAG_USE_ADAPTER = (1 << 1),
+  FLAG_USE_ACP = (1 << 2),
 } MonitorFlags;
 
 static const struct {
@@ -29,6 +30,7 @@ static const struct {
 } flag_names[] = {
   { FLAG_LOCAL_NODES, "local-nodes" },
   { FLAG_USE_ADAPTER, "use-adapter" },
+  { FLAG_USE_ACP, "use-acp" },
 };
 
 enum {
@@ -80,12 +82,17 @@ device_data_free (gpointer data, GClosure *closure)
 }
 
 static void
-setup_device_props (WpProperties *p)
+setup_device_props (WpMonitor * self, WpProperties *p)
 {
   const gchar *s, *d, *api;
 
   api = wp_properties_get (p, SPA_KEY_DEVICE_API);
 
+  /* if alsa and ACP, set acp property to true */
+  if (!g_strcmp0 (api, "alsa") && (self->flags & FLAG_USE_ACP))
+    wp_properties_setf (p, "device.api.alsa.acp", "%d",
+        self->flags & FLAG_USE_ACP ? TRUE : FALSE);
+
   /* set the device name if it's not already set */
   if (!wp_properties_get (p, SPA_KEY_DEVICE_NAME)) {
     if ((s = wp_properties_get (p, SPA_KEY_DEVICE_BUS_ID)) == NULL) {
@@ -339,14 +346,18 @@ create_device (WpMonitor * self, WpSpaDevice * parent, guint id,
   GList *children = NULL;
   GList *link = NULL;
   GObject *child = NULL;
+  const char *factory_name = NULL;
 
   g_return_val_if_fail (parent, NULL);
   g_return_val_if_fail (spa_factory, NULL);
 
   find_child (G_OBJECT (parent), id, &children, &link, &child);
 
+  factory_name = self->flags & FLAG_USE_ACP ?
+      SPA_NAME_API_ALSA_ACP_DEVICE : spa_factory;
+
   /* Create the device */
-  device = wp_spa_device_new_from_spa_factory (self->local_core, spa_factory,
+  device = wp_spa_device_new_from_spa_factory (self->local_core, factory_name,
       props);
   if (!device)
     return NULL;
@@ -414,7 +425,7 @@ maybe_create_device (WpMonitor * self, WpSpaDevice * parent, guint id,
 
   /* Create the properties */
   props = wp_properties_copy (props);
-  setup_device_props (props);
+  setup_device_props (self, props);
 
   /* If dbus reservation API exists, let dbus manage the device, otherwise just
    * create it and never destroy it */
diff --git a/src/config/wireplumber.conf b/src/config/wireplumber.conf
index e068aef8..80b645b7 100644
--- a/src/config/wireplumber.conf
+++ b/src/config/wireplumber.conf
@@ -40,7 +40,7 @@ load-module C libwireplumber-module-si-bluez5-endpoint
 #
 
 load-module C libwireplumber-module-monitor {
-  "alsa":   <{"factory": <"api.alsa.enum.udev">,   "flags": <["use-adapter"]>}>,
+  "alsa":   <{"factory": <"api.alsa.enum.udev">,   "flags": <["use-acp", "use-adapter"]>}>,
   "bluez5": <{"factory": <"api.bluez5.enum.dbus">, "flags": <["local-nodes", "use-adapter"]>}>,
   "v4l2":   <{"factory": <"api.v4l2.enum.udev">}>
 }
-- 
GitLab