write/alsa: Add btr support.
[paraslash.git] / buffer_tree.c
index 1ca4a15..2a52f09 100644 (file)
@@ -5,6 +5,7 @@
 #include "list.h"
 #include "string.h"
 #include "buffer_tree.h"
+#include "error.h"
 
 
 struct btr_buffer {
@@ -34,6 +35,7 @@ struct btr_node {
         * used by this btr node.
         */
        struct list_head input_queue;
+       btr_command_handler execute;
 };
 
 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
@@ -44,12 +46,14 @@ struct btr_node {
 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
        list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
 
-struct btr_node *btr_new_node(char *name, struct btr_node *parent)
+struct btr_node *btr_new_node(char *name, struct btr_node *parent,
+               btr_command_handler handler)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
 
        btrn->name = para_strdup(name);
        btrn->parent = parent;
+       btrn->execute = handler;
        if (parent)
                list_add_tail(&btrn->node, &parent->children);
        INIT_LIST_HEAD(&btrn->children);
@@ -79,6 +83,7 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
 {
        struct btr_buffer *btrb = br->btrb;
 
+       //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
        list_del(&br->node);
        free(br);
        btrb->refcount--;
@@ -204,8 +209,10 @@ size_t btr_get_input_queue_size(struct btr_node *btrn)
        struct btr_buffer_reference *br;
        size_t size = 0;
 
-       FOR_EACH_BUFFER_REF(br, btrn)
+       FOR_EACH_BUFFER_REF(br, btrn) {
+               //PARA_CRIT_LOG("size: %zu\n", size);
                size += br_available_bytes(br);
+       }
        return size;
 }
 
@@ -225,3 +232,32 @@ size_t btr_bytes_pending(struct btr_node *btrn)
        }
        return max_size;
 }
+
+int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
+{
+       if (!btrn)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       if (!btrn->execute)
+               return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+       return btrn->execute(command, value_result);
+}
+
+int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
+{
+       int ret;
+
+       for (; btrn; btrn = btrn->parent) {
+               struct btr_node *parent = btrn->parent;
+               PARA_CRIT_LOG("parent: %p\n", parent);
+               if (!parent)
+                       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+               if (!parent->execute)
+                       continue;
+               ret = parent->execute(command, value_result);
+               if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
+                       continue;
+               if (ret < 0)
+                       return ret;
+       }
+       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+}