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