murphyif: Free the connect timer when unloading
[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 <murphy/resource/data-types.h>
30
31 #include "zone.h"
32
33
34 struct pa_zoneset {
35     struct {
36         pa_hashmap     *hash;
37         mir_zone       *index[MRP_ZONE_MAX];
38     } zones;
39 };
40
41
42 static void free_zone_cb(void *);
43
44
45 pa_zoneset *pa_zoneset_init(struct userdata *u)
46 {
47     pa_zoneset *zs;
48
49     pa_assert(u);
50
51     zs = pa_xnew0(pa_zoneset, 1);
52     zs->zones.hash = pa_hashmap_new(pa_idxset_string_hash_func,
53                                     pa_idxset_string_compare_func);
54
55     return zs;
56 }
57
58 void pa_zoneset_done(struct userdata *u)
59 {
60     pa_zoneset *zs;
61     void       *state;
62     mir_zone   *zone;
63
64     if (u && (zs = u->zoneset)) {
65
66         PA_HASHMAP_FOREACH(zone, zs->zones.hash, state) {
67             pa_xfree((void *)zone->name);
68         }
69
70         pa_hashmap_free(zs->zones.hash);
71
72         free(zs);
73     }    
74 }
75
76 int pa_zoneset_add_zone(struct userdata *u, const char *name, uint32_t index)
77 {
78     pa_zoneset *zs;
79     mir_zone *zone;
80
81     pa_assert(u);
82     pa_assert(name);
83     pa_assert(index < MRP_ZONE_MAX);
84     pa_assert_se((zs = u->zoneset));
85
86     zone = pa_xnew0(mir_zone, 1);
87     zone->name = pa_xstrdup(name);
88     zone->index = index;
89
90     if (pa_hashmap_put(zs->zones.hash, (void *)zone->name, zone))
91         return -1;
92
93     zs->zones.index[index] = zone;
94
95     return 0;
96 }
97
98 mir_zone *pa_zoneset_get_zone_by_name(struct userdata *u, const char *name)
99 {
100     pa_zoneset *zs;
101     mir_zone *zone;
102
103     pa_assert(u);
104     pa_assert_se((zs = u->zoneset));
105
106     if (name && zs->zones.hash)
107         zone = pa_hashmap_get(zs->zones.hash, name);
108     else
109         zone = NULL;
110
111     return zone;
112 }
113
114
115 mir_zone *pa_zoneset_get_zone_by_index(struct userdata *u, uint32_t index)
116 {
117     pa_zoneset *zs;
118     mir_zone *zone;
119
120     pa_assert(u);
121     pa_assert_se((zs = u->zoneset));
122
123     if (index < MRP_ZONE_MAX)
124         zone = zs->zones.index[index];
125     else
126         zone = NULL;
127
128     return zone;
129 }
130
131 void pa_zoneset_update_module_property(struct userdata *u)
132 {
133     pa_module *module;
134     pa_zoneset *zs;
135     mir_zone *zone;
136     void *state;
137     char buf[4096];
138     char *p, *e;
139
140     pa_assert(u);
141     pa_assert_se((module = u->module));
142     pa_assert_se((zs = u->zoneset));
143     pa_assert(zs->zones.hash);
144
145     e = (p = buf) + sizeof(buf);
146
147     buf[1] = 0;
148     
149     PA_HASHMAP_FOREACH(zone, zs->zones.hash, state) {
150         if (p >= e) break;
151         p += snprintf(p, e-p, " '%s'", zone->name);
152     }
153
154     pa_proplist_sets(module->proplist, PA_PROP_ZONES, buf+1);
155 }
156
157 static void free_zone_cb(void *void_zone)
158 {
159     mir_zone  *zone = (mir_zone *)void_zone;
160
161     pa_xfree((void *)zone->name);
162
163     pa_xfree(zone);
164 }
165
166
167                                   
168
169
170
171 /*
172  * Local Variables:
173  * c-basic-offset: 4
174  * indent-tabs-mode: nil
175  * End:
176  *
177  */