discover: fix card bus detection
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / utils.c
1 /*
2  * module-murphy-ivi -- PulseAudio module for providing audio routing support
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU Lesser General Public License,
7  * version 2.1, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
17  * MA 02110-1301 USA.
18  *
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <limits.h>
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32
33 #include <pulsecore/pulsecore-config.h>
34
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/card.h>
37 #include <pulsecore/sink.h>
38 #include <pulsecore/source.h>
39 #include <pulsecore/sink-input.h>
40 #include <pulsecore/source-output.h>
41
42 #include "userdata.h"
43 #include "utils.h"
44 #include "node.h"
45
46
47 #define DEFAULT_NULL_SINK_NAME "null.mir"
48
49 struct pa_null_sink {
50     char      *name;
51     uint32_t   module_index;
52     uint32_t   sink_index;
53 };
54
55
56 static uint32_t stamp;
57
58 static char *stream_name(pa_proplist *);
59
60
61 pa_null_sink *pa_utils_create_null_sink(struct userdata *u, const char *name)
62 {
63     pa_core      *core;
64     pa_module    *module;
65     pa_null_sink *ns;
66     pa_sink      *sink;
67     pa_sink      *s;
68     uint32_t      idx;
69     char          args[256];
70
71     pa_assert(u);
72     pa_assert_se((core = u->core));
73
74
75     if (!name)
76         name = DEFAULT_NULL_SINK_NAME;
77
78
79     snprintf(args, sizeof(args), "sink_name=\"%s\" channels=2", name);
80     module = pa_module_load(core, "module-null-sink", args);
81     sink = NULL;
82
83     if (!module)
84         pa_log("failed to load null sink '%s'", name);
85     else {
86         PA_IDXSET_FOREACH(s, core->sinks, idx) {
87             if (s->module && s->module == module) {
88                 sink = s;
89                 pa_log_info("mir null sink is '%s'", name);
90                 break;
91             }
92         }
93     }
94
95     ns = pa_xnew0(pa_null_sink, 1);
96     ns->name = pa_xstrdup(name);
97     ns->module_index = module ? module->index : PA_IDXSET_INVALID;
98     ns->sink_index = sink ? sink->index : PA_IDXSET_INVALID;
99
100     return ns;
101 }
102
103 void pa_utils_destroy_null_sink(struct userdata *u)
104 {
105     pa_core      *core;
106     pa_module    *module;
107     pa_null_sink *ns;
108
109     if (u && (ns = u->nullsink) && (core = u->core)) {
110         if ((module = pa_idxset_get_by_index(core->modules,ns->module_index))){
111             pa_log_info("unloading null sink '%s'", ns->name);
112             pa_module_unload(core, module, FALSE);
113         }
114
115         pa_xfree(ns->name);
116         pa_xfree(ns);
117     }
118 }
119
120 pa_sink *pa_utils_get_null_sink(struct userdata *u)
121 {
122     pa_core *core;
123     pa_null_sink *ns;
124
125     pa_assert(u);
126     pa_assert_se((core = u->core));
127     pa_assert_se((ns = u->nullsink));
128
129     return pa_idxset_get_by_index(core->sinks, ns->sink_index);
130 }
131
132 pa_source *pa_utils_get_null_source(struct userdata *u)
133 {
134     pa_sink *ns = pa_utils_get_null_sink(u);
135
136     return ns ? ns->monitor_source : NULL;
137 }
138
139
140
141 char *pa_utils_get_card_name(pa_card *card)
142 {
143     return (card && card->name) ? card->name : "<unknown>";
144 }
145
146 char *pa_utils_get_card_bus(pa_card *card)
147 {
148     const char *bus = NULL;
149
150     if (card && !(bus = pa_proplist_gets(card->proplist,PA_PROP_DEVICE_BUS))) {
151         if (!strncmp(card->name, "alsa_card.", 10)) {
152             if (!strncmp(card->name + 10, "pci-", 4))
153                 bus = "pci";
154             else if (!strncmp(card->name + 10, "usb-", 4))
155                 bus = "usb";
156         }
157     }
158
159     return (char *)bus;
160 }
161
162 char *pa_utils_get_sink_name(pa_sink *sink)
163 {
164     return (sink && sink->name) ? sink->name : "<unknown>";
165 }
166
167 char *pa_utils_get_source_name(pa_source *source)
168 {
169     return (source && source->name) ? source->name : "<unknown>";
170 }
171
172 char *pa_utils_get_sink_input_name(pa_sink_input *sinp)
173 {
174     char *name;
175
176     if (sinp && (name = stream_name(sinp->proplist)))
177         return name;
178
179     return "<unknown>";
180 }
181
182 char *pa_utils_get_sink_input_name_from_data(pa_sink_input_new_data *data)
183 {
184     char *name;
185
186     if (data && (name = stream_name(data->proplist)))
187         return name;
188
189     return "<unknown>";
190 }
191
192
193 char *pa_utils_get_source_output_name(pa_source_output *sout)
194 {
195     char *name;
196
197     if (sout && (name = stream_name(sout->proplist)))
198         return name;
199
200     return "<unknown>";
201 }
202
203 char *pa_utils_get_source_output_name_from_data(pa_source_output_new_data*data)
204 {
205     char *name;
206
207     if (data && (name = stream_name(data->proplist)))
208         return name;
209
210     return "<unknown>";
211 }
212
213
214 void pa_utils_set_stream_routing_properties(pa_proplist *pl,
215                                             int          styp,
216                                             void        *target)
217 {
218     const char    *clnam;
219     const char    *method;
220     char           clid[32];
221
222     pa_assert(pl);
223     pa_assert(styp >= 0);
224
225     snprintf(clid, sizeof(clid), "%d", styp);
226     clnam  = mir_node_type_str(styp);
227     method = target ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
228
229     if (pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_NAME, clnam ) < 0 ||
230         pa_proplist_sets(pl, PA_PROP_ROUTING_CLASS_ID  , clid  ) < 0 ||
231         pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD    , method) < 0  )
232     {
233         pa_log("failed to set some stream property");
234     }
235 }
236
237 void pa_utils_set_stream_routing_method_property(pa_proplist *pl,
238                                                  pa_bool_t explicit)
239 {
240     const char *method = explicit ? PA_ROUTING_EXPLICIT : PA_ROUTING_DEFAULT;
241
242     pa_assert(pl);
243
244     if (pa_proplist_sets(pl, PA_PROP_ROUTING_METHOD, method) < 0) {
245         pa_log("failed to set routing method property on sink-input");
246     }
247 }
248
249 pa_bool_t pa_utils_stream_has_default_route(pa_proplist *pl)
250 {
251     const char *method;
252
253     pa_assert(pl);
254
255     method = pa_proplist_gets(pl, PA_PROP_ROUTING_METHOD);
256
257     if (method && pa_streq(method, PA_ROUTING_DEFAULT))
258         return TRUE;
259
260     return FALSE;
261 }
262
263 int pa_utils_get_stream_class(pa_proplist *pl)
264 {
265     const char *clid_str;
266     char *e;
267     unsigned long int clid = 0;
268
269     pa_assert(pl);
270
271     if ((clid_str = pa_proplist_gets(pl, PA_PROP_ROUTING_CLASS_ID))) {
272         clid = strtoul(clid_str, &e, 10);
273
274         if (*e)
275             clid = 0;
276     }
277
278     return (int)clid;
279 }
280
281 void pa_utils_set_port_properties(pa_device_port *port, mir_node *node)
282 {
283     char nodeidx[256];
284
285     pa_assert(port);
286     pa_assert(port->proplist);
287     pa_assert(node);
288
289     snprintf(nodeidx, sizeof(nodeidx), "%u", node->index);
290
291     pa_proplist_sets(port->proplist, PA_PROP_NODE_INDEX, nodeidx);
292 }
293
294 mir_node *pa_utils_get_node_from_port(struct userdata *u,
295                                       pa_device_port *port)
296 {
297     const char *value;
298     char *e;
299     uint32_t index = PA_IDXSET_INVALID;
300     mir_node *node = NULL;
301
302     pa_assert(u);
303     pa_assert(port);
304     pa_assert(port->proplist);
305
306     if ((value = pa_proplist_gets(port->proplist, PA_PROP_NODE_INDEX))) {
307         index = strtoul(value, &e, 10);
308
309         if (value[0] && !e[0])
310             node = mir_node_find_by_index(u, index);
311     }
312
313     return node;
314 }
315
316 mir_node *pa_utils_get_node_from_stream(struct userdata *u,
317                                         mir_direction    type,
318                                         void            *ptr)
319 {
320     pa_sink_input    *sinp;
321     pa_source_output *sout;
322     pa_proplist      *pl;
323     mir_node         *node;
324     const char       *index_str;
325     uint32_t          index = PA_IDXSET_INVALID;
326     char             *e;
327     char              name[256];
328
329     pa_assert(u);
330     pa_assert(ptr);
331     pa_assert(type == mir_input || type == mir_output);
332
333     if (type == mir_input) {
334         sinp = (pa_sink_input *)ptr;
335         pl = sinp->proplist;
336         snprintf(name, sizeof(name), "sink-input.%u", sinp->index);
337     }
338     else {
339         sout = (pa_source_output *)ptr;
340         pl = sout->proplist;
341         snprintf(name, sizeof(name), "source-output.%u", sout->index);
342     }
343
344
345     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
346         index = strtoul(index_str, &e, 10);
347         if (e != index_str && *e == '\0') {
348             if ((node = mir_node_find_by_index(u, index)))
349                 return node;
350
351             pa_log_debug("can't find find node for %s", name);
352         }
353     }
354
355     return NULL;
356 }
357
358 mir_node *pa_utils_get_node_from_data(struct userdata *u,
359                                       mir_direction    type,
360                                       void            *ptr)
361 {
362     pa_sink_input_new_data *sinp;
363     pa_source_output_new_data *sout;
364     pa_proplist  *pl;
365     mir_node     *node;
366     const char   *index_str;
367     uint32_t      index = PA_IDXSET_INVALID;
368     char         *e;
369     char          name[256];
370
371     pa_assert(u);
372     pa_assert(ptr);
373     pa_assert(type == mir_input || type == mir_output);
374
375     if (type == mir_input) {
376         sinp = (pa_sink_input_new_data *)ptr;
377         pl = sinp->proplist;
378         snprintf(name, sizeof(name), "sink-input");
379     }
380     else {
381         sout = (pa_source_output_new_data *)ptr;
382         pl = sout->proplist;
383         snprintf(name, sizeof(name), "source-output");
384     }
385
386
387     if ((index_str = pa_proplist_gets(pl, PA_PROP_NODE_INDEX))) {
388         index = strtoul(index_str, &e, 10);
389         if (e != index_str && *e == '\0') {
390             if ((node = mir_node_find_by_index(u, index)))
391                 return node;
392
393             pa_log_debug("can't find find node for %s", name);
394         }
395     }
396
397     return NULL;
398 }
399
400 static char *stream_name(pa_proplist *pl)
401 {
402     const char  *appnam;
403     const char  *binnam;
404
405     if ((appnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_NAME)))
406         return (char *)appnam;
407
408     if ((binnam = pa_proplist_gets(pl, PA_PROP_APPLICATION_PROCESS_BINARY)))
409         return (char *)binnam;
410
411     return NULL;
412 }
413
414
415 const char *pa_utils_file_path(const char *dir, const char *file,
416                                char *buf, size_t len)
417 {
418     pa_assert(file);
419     pa_assert(buf);
420     pa_assert(len > 0);
421
422     snprintf(buf, len, "%s/%s", dir, file);
423
424     return buf;
425 }
426
427
428 uint32_t pa_utils_new_stamp(void)
429 {
430     return ++stamp;
431 }
432
433 uint32_t pa_utils_get_stamp(void)
434 {
435     return stamp;
436 }
437
438
439
440
441 /*
442  * Local Variables:
443  * c-basic-offset: 4
444  * indent-tabs-mode: nil
445  * End:
446  *
447  */
448
449