scripting: support for zone based routing in application classes
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / zone.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
22 #include <pulsecore/pulsecore-config.h>
23
24 #include <pulse/proplist.h>
25 #include <pulsecore/idxset.h>
26 #include <pulsecore/hashmap.h>
27 #include <pulsecore/module.h>
28
29 #include "zone.h"
30
31
32 struct pa_zoneset {
33     pa_hashmap     *zones;
34 };
35
36
37 static void free_zone_cb(void *);
38
39
40 pa_zoneset *pa_zoneset_init(struct userdata *u)
41 {
42     pa_zoneset *zs;
43
44     pa_assert(u);
45
46     zs = pa_xnew0(pa_zoneset, 1);
47     zs->zones = pa_hashmap_new(pa_idxset_string_hash_func,
48                                pa_idxset_string_compare_func);
49
50     return zs;
51 }
52
53
54 void pa_zoneset_done(struct userdata *u)
55 {
56     pa_zoneset *zs;
57     int i;
58
59     if (u && (zs = u->zoneset)) {
60         pa_hashmap_free(zs->zones, free_zone_cb);
61
62         free(zs);
63     }    
64 }
65
66 int pa_zoneset_add_zone(struct userdata *u, const char *name, uint32_t index)
67 {
68     pa_zoneset *zs;
69     mir_zone *zone;
70
71     pa_assert(u);
72     pa_assert(name);
73     pa_assert_se((zs = u->zoneset));
74
75     zone = pa_xnew0(mir_zone, 1);
76     zone->name = pa_xstrdup(name);
77     zone->index = index;
78
79     return pa_hashmap_put(zs->zones, zone->name, zone);
80 }
81
82 mir_zone *pa_zoneset_get_zone(struct userdata *u, const char *name)
83 {
84     pa_zoneset *zs;
85     mir_zone *zone;
86
87     pa_assert(u);
88     pa_assert_se((zs = u->zoneset));
89
90     if (name && zs->zones)
91         zone = pa_hashmap_get(zs->zones, name);
92     else
93         zone = NULL;
94
95     return zone;
96 }
97
98
99 void pa_zoneset_update_module_property(struct userdata *u)
100 {
101     pa_module *module;
102     pa_zoneset *zs;
103     mir_zone *zone;
104     void *state;
105     char buf[4096];
106     char *p, *e;
107
108     pa_assert(u);
109     pa_assert_se((module = u->module));
110     pa_assert_se((zs = u->zoneset));
111     pa_assert(zs->zones);
112
113     e = (p = buf) + sizeof(buf);
114
115     buf[1] = 0;
116     
117     PA_HASHMAP_FOREACH(zone, zs->zones, state) {
118         if (p >= e) break;
119         p += snprintf(p, e-p, " '%s'", zone->name);
120     }
121
122     pa_proplist_sets(module->proplist, PA_PROP_ZONES, buf+1); /* skip ' '@begining */
123 }
124
125 static void free_zone_cb(void *void_zone)
126 {
127     mir_zone  *zone = (mir_zone *)void_zone;
128
129     pa_xfree((void *)zone->name);
130
131     pa_xfree(zone);
132 }
133
134
135                                   
136
137
138
139 /*
140  * Local Variables:
141  * c-basic-offset: 4
142  * indent-tabs-mode: nil
143  * End:
144  *
145  */