c6c59e2a28f6c8660d788301cf9233c2532e11bc
[staging/agl-audio-plugin.git] / node.c
1 /*
2  * module-agl-audio -- PulseAudio module for providing audio routing support
3  * (forked from "module-murphy-ivi" - https://github.com/otcshare )
4  * Copyright (c) 2012, Intel Corporation.
5  * Copyright (c) 2016, IoT.bzh
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU Lesser General Public License,
9  * version 2.1, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
19  * MA 02110-1301 USA.
20  *
21  */
22 #include "node.h"
23
24 #include <pulsecore/idxset.h>
25
26 pa_nodeset *pa_nodeset_init (struct userdata *u)
27 {
28         pa_nodeset *ns;
29
30         pa_assert (u);
31
32         ns = pa_xnew0 (pa_nodeset, 1);
33         ns->nodes = pa_idxset_new (pa_idxset_trivial_hash_func,
34                                    pa_idxset_trivial_compare_func);
35         ns->roles = pa_hashmap_new (pa_idxset_string_hash_func,
36                                     pa_idxset_string_compare_func);
37         ns->binaries = pa_hashmap_new (pa_idxset_string_hash_func,
38                                        pa_idxset_string_compare_func);
39         return ns;
40 }
41
42 void pa_nodeset_done(struct userdata *u)
43 {
44         pa_nodeset *ns;
45         pa_nodeset_map *role, *binary;
46         void *state;
47         int i;
48
49         if (u && (ns = u->nodeset)) {
50                 pa_idxset_free (ns->nodes, NULL);
51
52                 PA_HASHMAP_FOREACH(role, ns->roles, state) {
53                         pa_xfree ((void *)role->name);
54                         pa_xfree ((void *)role->resdef);
55                 }
56                 pa_hashmap_free (ns->roles);
57
58                 PA_HASHMAP_FOREACH(binary, ns->binaries, state) {
59                         pa_xfree ((void *)binary->name);
60                         pa_xfree ((void *)binary->resdef);
61                 }
62                 pa_hashmap_free (ns->binaries);
63
64                 for (i = 0;  i < APCLASS_DIM;  i++)
65                         pa_xfree((void *)ns->class_name[i]);
66
67                 free(ns);
68         }
69 }
70
71 agl_node *agl_node_create (struct userdata *u, agl_node *data)
72 {
73         pa_nodeset *ns;
74         agl_node *node;
75
76         pa_assert (u);
77         pa_assert_se (ns = u->nodeset);
78
79         node = pa_xnew0 (agl_node, 1);
80
81         pa_idxset_put (ns->nodes, node, &node->index);
82
83         if (data) {
84                 node->key = pa_xstrdup (data->key);
85                 node->direction = data->direction;
86                 node->implement = data->implement;
87                 node->channels = data->channels;
88                 node->location = data->location;
89                 node->privacy = data->privacy;
90                 node->type = data->type;
91                 node->zone = pa_xstrdup (data->zone);
92                 node->visible = data->visible;
93                 node->available = data->available;
94                 node->amname = pa_xstrdup (data->amname ? data->amname : data->paname);
95                 node->amdescr = pa_xstrdup(data->amdescr ? data->amdescr : "");
96                 node->amid = data->amid;
97                 node->paname = pa_xstrdup (data->paname);
98                 node->stamp = data->stamp;
99                 node->rset.id = data->rset.id ? pa_xstrdup(data->rset.id) : NULL;
100                 node->rset.grant = data->rset.grant;
101
102                 if (node->implement == agl_device) {
103                         node->pacard.index = data->pacard.index;
104                         if (data->pacard.profile)
105                                 node->pacard.profile = pa_xstrdup (data->pacard.profile);
106                         if (data->paport)
107                                 node->paport = data->paport;
108                 }
109         }
110
111          /* TODO : register the node to the router */
112         /* agl_router_register_node (u, node); */
113
114         return node;
115 }
116
117 const char *agl_node_type_str (agl_node_type type)
118 {
119         switch (type) {
120                 case agl_node_type_unknown: return "Unknown";
121                 case agl_radio:             return "Radio";
122                 case agl_player:            return "Player";
123                 case agl_navigator:         return "Navigator";
124                 case agl_game:              return "Game";
125                 case agl_browser:           return "Browser";
126                 case agl_camera:            return "Camera";
127                 case agl_phone:             return "Phone";
128                 case agl_alert:             return "Alert";
129                 case agl_event:             return "Event";
130                 case agl_system:            return "System";
131                 default:                    return "<user defined>";
132         }
133 }
134
135 agl_node *agl_node_get_from_data (struct userdata *u, agl_direction type, void *data)
136 {
137         pa_sink_input_new_data *sinp_data;
138         pa_source_output_new_data *sout_data;
139         pa_nodeset *nodeset;
140         agl_node *node;
141         uint32_t index;
142
143         pa_assert (u);
144         pa_assert (data);
145         pa_assert (nodeset = u->nodeset);
146
147         pa_assert (type == agl_input || type == agl_output);
148
149         /* input (= sink_input) */
150         if (type == agl_input) {
151                 sinp_data = (pa_sink_input_new_data *) data;
152                 PA_IDXSET_FOREACH(node, nodeset->nodes, index) {
153                         if (node->client == sinp_data->client)
154                                 return node;
155                 }
156         /* output (= source_output) TODO */
157         /*} else {*/
158         }
159
160         return NULL;
161 }
162
163 agl_node *agl_node_get_from_client (struct userdata *u, pa_client *client)
164 {
165         pa_nodeset *nodeset;
166         agl_node *node;
167         uint32_t index;
168
169         pa_assert (u);
170         pa_assert (client);
171         pa_assert (nodeset = u->nodeset);
172
173         PA_IDXSET_FOREACH(node, nodeset->nodes, index) {
174                 if (node->client == client)
175                         return node;
176         }
177
178         return NULL;
179 }