scripting: support for zone based routing in application classes
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / utils.c
index 117ffb5..e1c6751 100644 (file)
@@ -33,8 +33,8 @@
 #include <pulsecore/pulsecore-config.h>
 
 #include <pulsecore/core-util.h>
-#include <pulsecore/card.h>
 #include <pulsecore/sink.h>
+#include <pulsecore/card.h>
 #include <pulsecore/source.h>
 #include <pulsecore/sink-input.h>
 #include <pulsecore/source-output.h>
@@ -56,6 +56,7 @@ struct pa_null_sink {
 static uint32_t stamp;
 
 static char *stream_name(pa_proplist *);
+static pa_bool_t get_unsigned_property(pa_proplist *, const char *,uint32_t *);
 
 
 pa_null_sink *pa_utils_create_null_sink(struct userdata *u, const char *name)
@@ -146,12 +147,14 @@ char *pa_utils_get_card_name(pa_card *card)
 char *pa_utils_get_card_bus(pa_card *card)
 {
     const char *bus = NULL;
+    char       *name;
 
     if (card && !(bus = pa_proplist_gets(card->proplist,PA_PROP_DEVICE_BUS))) {
-        if (!strncmp(card->name, "alsa_card.", 10)) {
-            if (!strncmp(card->name + 10, "pci-", 4))
+        name = pa_utils_get_card_name(card);
+        if (!strncmp(name, "alsa_card.", 10)) {
+            if (!strncmp(name + 10, "pci-", 4))
                 bus = "pci";
-            else if (!strncmp(card->name + 10, "usb-", 4))
+            else if (!strncmp(name + 10, "usb-", 4))
                 bus = "usb";
         }
     }
@@ -210,10 +213,21 @@ char *pa_utils_get_source_output_name_from_data(pa_source_output_new_data*data)
     return "<unknown>";
 }
 
+char *pa_utils_get_zone(pa_proplist *pl)
+{
+    const char *zone;
+
+    pa_assert(pl);
+
+    if (!(zone = pa_proplist_gets(pl, PA_PROP_ZONE_NAME)))
+        zone = PA_ZONE_NAME_DEFAULT;
+
+    return (char *)zone;
+}
 
-void pa_utils_set_stream_routing_properties(pa_proplist *pl,
-                                            int          styp,
-                                            void        *target)
+pa_bool_t pa_utils_set_stream_routing_properties(pa_proplist *pl,
+                                                 int          styp,
+                                                 void        *target)
 {
     const char    *clnam;
     const char    *method;
@@ -231,7 +245,25 @@ void pa_utils_set_stream_routing_properties(pa_proplist *pl,
         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
     {
         pa_log("failed to set some stream property");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+pa_bool_t pa_utils_unset_stream_routing_properties(pa_proplist *pl)
+{
+    pa_assert(pl);
+
+    if (pa_proplist_unset(pl, PA_PROP_ROUTING_CLASS_NAME) < 0 ||
+        pa_proplist_unset(pl, PA_PROP_ROUTING_CLASS_ID  ) < 0 ||
+        pa_proplist_unset(pl, PA_PROP_ROUTING_METHOD    ) < 0  )
+    {
+        pa_log("failed to unset some stream property");
+        return FALSE;
     }
+
+    return TRUE;
 }
 
 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
@@ -278,22 +310,119 @@ int pa_utils_get_stream_class(pa_proplist *pl)
     return (int)clid;
 }
 
+
+pa_bool_t pa_utils_set_resource_properties(pa_proplist *pl,
+                                           pa_nodeset_resdef *resdef)
+{
+    char priority[32];
+    char rsetflags[32];
+    char audioflags[32];
+
+    pa_assert(pl);
+
+    if (!resdef)
+        return FALSE;
+
+    snprintf(priority  , sizeof(priority)  , "%d", resdef->priority   );
+    snprintf(rsetflags , sizeof(rsetflags) , "%d", resdef->flags.rset );
+    snprintf(audioflags, sizeof(audioflags), "%d", resdef->flags.audio);
+
+    if (pa_proplist_sets(pl, PA_PROP_RESOURCE_PRIORITY   , priority  ) < 0 ||
+        pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_FLAGS  , rsetflags ) < 0 ||
+        pa_proplist_sets(pl, PA_PROP_RESOURCE_AUDIO_FLAGS, audioflags) < 0  )
+    {
+        pa_log("failed to set some resource property");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+pa_bool_t pa_utils_unset_resource_properties(pa_proplist *pl)
+{
+    pa_assert(pl);
+
+    if (pa_proplist_unset(pl, PA_PROP_RESOURCE_PRIORITY   ) < 0 ||
+        pa_proplist_unset(pl, PA_PROP_RESOURCE_SET_FLAGS  ) < 0 ||
+        pa_proplist_unset(pl, PA_PROP_RESOURCE_AUDIO_FLAGS) < 0  )
+    {
+        pa_log("failed to unset some resource property");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+pa_nodeset_resdef *pa_utils_get_resource_properties(pa_proplist *pl,
+                                                    pa_nodeset_resdef *rd)
+{
+    pa_assert(pl);
+    pa_assert(rd);
+
+    int success;
+
+    memset(rd, 0, sizeof(pa_nodeset_resdef));
+
+    success  = get_unsigned_property(pl, PA_PROP_RESOURCE_PRIORITY,
+                                     &rd->priority);
+    success |= get_unsigned_property(pl, PA_PROP_RESOURCE_SET_FLAGS,
+                                     &rd->flags.rset);
+    success |= get_unsigned_property(pl, PA_PROP_RESOURCE_AUDIO_FLAGS,
+                                     &rd->flags.audio);
+    
+    return success ? rd : NULL;
+}
+
+
+static pa_bool_t get_unsigned_property(pa_proplist *pl,
+                                       const char *name,
+                                       uint32_t *value)
+{
+    const char *str;
+    char *e;
+
+    pa_assert(pl);
+    pa_assert(name);
+    pa_assert(value);
+
+    if (!(str = pa_proplist_gets(pl, name))) {
+        *value = 0;
+        return FALSE;
+    }
+
+    *value = strtoul(str, &e, 10);
+
+    if (e == str || *e) {
+        *value = 0;
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+
 void pa_utils_set_port_properties(pa_device_port *port, mir_node *node)
 {
+    const char *profile;
+    char propnam[512];
     char nodeidx[256];
 
     pa_assert(port);
     pa_assert(port->proplist);
     pa_assert(node);
+    pa_assert_se((profile = node->pacard.profile));
 
+    snprintf(propnam, sizeof(propnam), "%s.%s", PA_PROP_NODE_INDEX, profile);
     snprintf(nodeidx, sizeof(nodeidx), "%u", node->index);
 
-    pa_proplist_sets(port->proplist, PA_PROP_NODE_INDEX, nodeidx);
+    pa_proplist_sets(port->proplist, propnam, nodeidx);
 }
 
 mir_node *pa_utils_get_node_from_port(struct userdata *u,
-                                      pa_device_port *port)
+                                      pa_device_port *port,
+                                      void **state)
 {
+    const char *name;
     const char *value;
     char *e;
     uint32_t index = PA_IDXSET_INVALID;
@@ -303,14 +432,24 @@ mir_node *pa_utils_get_node_from_port(struct userdata *u,
     pa_assert(port);
     pa_assert(port->proplist);
 
-    if ((value = pa_proplist_gets(port->proplist, PA_PROP_NODE_INDEX))) {
-        index = strtoul(value, &e, 10);
+    while ((name = pa_proplist_iterate(port->proplist, state))) {
+        if (!strncmp(name, PA_PROP_NODE_INDEX, sizeof(PA_PROP_NODE_INDEX)-1)) {
+            if ((value = pa_proplist_gets(port->proplist, name))) {
+                index = strtoul(value, &e, 10);
+                node = NULL;
 
-        if (value[0] && !e[0])
-            node = mir_node_find_by_index(u, index);
+                if (value[0] && !e[0])
+                    node = mir_node_find_by_index(u, index);
+                
+                if (node)
+                    return node;
+
+                pa_log("Can't find node %u for port %s", index, port->name);
+            }
+        }
     }
 
-    return node;
+    return NULL;
 }
 
 mir_node *pa_utils_get_node_from_stream(struct userdata *u,
@@ -437,7 +576,6 @@ uint32_t pa_utils_get_stamp(void)
 
 
 
-
 /*
  * Local Variables:
  * c-basic-offset: 4