]> code.delx.au - pulseaudio/blob - src/pulsecore/core-subscribe.c
54fb7ec2326943cebc4fea52221a480a092fa62e
[pulseaudio] / src / pulsecore / core-subscribe.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/queue.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33
34 #include "core-subscribe.h"
35
36 /* The subscription subsystem may be used to be notified whenever an
37 * entity (sink, source, ...) is created or deleted. Modules may
38 * register a callback function that is called whenever an event
39 * matching a subscription mask happens. The execution of the callback
40 * function is postponed to the next main loop iteration, i.e. is not
41 * called from within the stack frame the entity was created in. */
42
43 struct pa_subscription {
44 pa_core *core;
45 pa_bool_t dead;
46
47 pa_subscription_cb_t callback;
48 void *userdata;
49 pa_subscription_mask_t mask;
50
51 PA_LLIST_FIELDS(pa_subscription);
52 };
53
54 struct pa_subscription_event {
55 pa_core *core;
56
57 pa_subscription_event_type_t type;
58 uint32_t index;
59
60 PA_LLIST_FIELDS(pa_subscription_event);
61 };
62
63 static void sched_event(pa_core *c);
64
65 /* Allocate a new subscription object for the given subscription mask. Use the specified callback function and user data */
66 pa_subscription* pa_subscription_new(pa_core *c, pa_subscription_mask_t m, pa_subscription_cb_t callback, void *userdata) {
67 pa_subscription *s;
68
69 pa_assert(c);
70 pa_assert(m);
71 pa_assert(callback);
72
73 s = pa_xnew(pa_subscription, 1);
74 s->core = c;
75 s->dead = FALSE;
76 s->callback = callback;
77 s->userdata = userdata;
78 s->mask = m;
79
80 PA_LLIST_PREPEND(pa_subscription, c->subscriptions, s);
81 return s;
82 }
83
84 /* Free a subscription object, effectively marking it for deletion */
85 void pa_subscription_free(pa_subscription*s) {
86 pa_assert(s);
87 pa_assert(!s->dead);
88
89 s->dead = TRUE;
90 sched_event(s->core);
91 }
92
93 static void free_subscription(pa_subscription *s) {
94 pa_assert(s);
95 pa_assert(s->core);
96
97 PA_LLIST_REMOVE(pa_subscription, s->core->subscriptions, s);
98 pa_xfree(s);
99 }
100
101 static void free_event(pa_subscription_event *s) {
102 pa_assert(s);
103 pa_assert(s->core);
104
105 if (!s->next)
106 s->core->subscription_event_last = s->prev;
107
108 PA_LLIST_REMOVE(pa_subscription_event, s->core->subscription_event_queue, s);
109 pa_xfree(s);
110 }
111
112 /* Free all subscription objects */
113 void pa_subscription_free_all(pa_core *c) {
114 pa_assert(c);
115
116 while (c->subscriptions)
117 free_subscription(c->subscriptions);
118
119 while (c->subscription_event_queue)
120 free_event(c->subscription_event_queue);
121
122 if (c->subscription_defer_event) {
123 c->mainloop->defer_free(c->subscription_defer_event);
124 c->subscription_defer_event = NULL;
125 }
126 }
127
128 #ifdef DEBUG
129 static void dump_event(const char * prefix, pa_subscription_event*e) {
130 const char * const fac_table[] = {
131 [PA_SUBSCRIPTION_EVENT_SINK] = "SINK",
132 [PA_SUBSCRIPTION_EVENT_SOURCE] = "SOURCE",
133 [PA_SUBSCRIPTION_EVENT_SINK_INPUT] = "SINK_INPUT",
134 [PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT] = "SOURCE_OUTPUT",
135 [PA_SUBSCRIPTION_EVENT_MODULE] = "MODULE",
136 [PA_SUBSCRIPTION_EVENT_CLIENT] = "CLIENT",
137 [PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE] = "SAMPLE_CACHE",
138 [PA_SUBSCRIPTION_EVENT_SERVER] = "SERVER",
139 [PA_SUBSCRIPTION_EVENT_AUTOLOAD] = "AUTOLOAD"
140 };
141
142 const char * const type_table[] = {
143 [PA_SUBSCRIPTION_EVENT_NEW] = "NEW",
144 [PA_SUBSCRIPTION_EVENT_CHANGE] = "CHANGE",
145 [PA_SUBSCRIPTION_EVENT_REMOVE] = "REMOVE"
146 };
147
148 pa_log_debug("%s event (%s|%s|%u)",
149 prefix,
150 fac_table[e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK],
151 type_table[e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK],
152 e->index);
153 }
154 #endif
155
156 /* Deferred callback for dispatching subscirption events */
157 static void defer_cb(pa_mainloop_api *m, pa_defer_event *de, void *userdata) {
158 pa_core *c = userdata;
159 pa_subscription *s;
160
161 pa_assert(c->mainloop == m);
162 pa_assert(c);
163 pa_assert(c->subscription_defer_event == de);
164
165 c->mainloop->defer_enable(c->subscription_defer_event, 0);
166
167 /* Dispatch queued events */
168
169 while (c->subscription_event_queue) {
170 pa_subscription_event *e = c->subscription_event_queue;
171
172 for (s = c->subscriptions; s; s = s->next) {
173
174 if (!s->dead && pa_subscription_match_flags(s->mask, e->type))
175 s->callback(c, e->type, e->index, s->userdata);
176 }
177
178 #ifdef DEBUG
179 dump_event("Dispatched", e);
180 #endif
181 free_event(e);
182 }
183
184 /* Remove dead subscriptions */
185
186 s = c->subscriptions;
187 while (s) {
188 pa_subscription *n = s->next;
189 if (s->dead)
190 free_subscription(s);
191 s = n;
192 }
193 }
194
195 /* Schedule an mainloop event so that a pending subscription event is dispatched */
196 static void sched_event(pa_core *c) {
197 pa_assert(c);
198
199 if (!c->subscription_defer_event) {
200 c->subscription_defer_event = c->mainloop->defer_new(c->mainloop, defer_cb, c);
201 pa_assert(c->subscription_defer_event);
202 }
203
204 c->mainloop->defer_enable(c->subscription_defer_event, 1);
205 }
206
207 /* Append a new subscription event to the subscription event queue and schedule a main loop event */
208 void pa_subscription_post(pa_core *c, pa_subscription_event_type_t t, uint32_t idx) {
209 pa_subscription_event *e;
210 pa_assert(c);
211
212 /* No need for queuing subscriptions of noone is listening */
213 if (!c->subscriptions)
214 return;
215
216 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_NEW) {
217 pa_subscription_event *i, *n;
218
219 /* Check for duplicates */
220 for (i = c->subscription_event_last; i; i = n) {
221 n = i->prev;
222
223 /* not the same object type */
224 if (((t ^ i->type) & PA_SUBSCRIPTION_EVENT_FACILITY_MASK))
225 continue;
226
227 /* not the same object */
228 if (i->index != idx)
229 continue;
230
231 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
232 /* This object is being removed, hence there is no
233 * point in keeping the old events regarding this
234 * entry in the queue. */
235
236 free_event(i);
237 pa_log_debug("Dropped redundant event due to remove event.");
238 continue;
239 }
240
241 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE) {
242 /* This object has changed. If a "new" or "change" event for
243 * this object is still in the queue we can exit. */
244
245 pa_log_debug("Dropped redundant event due to change event.");
246 return;
247 }
248 }
249 }
250
251 e = pa_xnew(pa_subscription_event, 1);
252 e->core = c;
253 e->type = t;
254 e->index = idx;
255
256 PA_LLIST_INSERT_AFTER(pa_subscription_event, c->subscription_event_queue, c->subscription_event_last, e);
257 c->subscription_event_last = e;
258
259 #ifdef DEBUG
260 dump_event("Queued", e);
261 #endif
262
263 sched_event(c);
264 }