2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
26 #include <pulse/xmalloc.h>
28 #include <pulsecore/log.h>
29 #include <pulsecore/macro.h>
31 #include "core-subscribe.h"
33 /* The subscription subsystem may be used to be notified whenever an
34 * entity (sink, source, ...) is created or deleted. Modules may
35 * register a callback function that is called whenever an event
36 * matching a subscription mask happens. The execution of the callback
37 * function is postponed to the next main loop iteration, i.e. is not
38 * called from within the stack frame the entity was created in. */
40 struct pa_subscription {
44 pa_subscription_cb_t callback;
46 pa_subscription_mask_t mask;
48 PA_LLIST_FIELDS(pa_subscription);
51 struct pa_subscription_event {
54 pa_subscription_event_type_t type;
57 PA_LLIST_FIELDS(pa_subscription_event);
60 static void sched_event(pa_core *c);
62 /* Allocate a new subscription object for the given subscription mask. Use the specified callback function and user data */
63 pa_subscription* pa_subscription_new(pa_core *c, pa_subscription_mask_t m, pa_subscription_cb_t callback, void *userdata) {
70 s = pa_xnew(pa_subscription, 1);
73 s->callback = callback;
74 s->userdata = userdata;
77 PA_LLIST_PREPEND(pa_subscription, c->subscriptions, s);
81 /* Free a subscription object, effectively marking it for deletion */
82 void pa_subscription_free(pa_subscription*s) {
90 static void free_subscription(pa_subscription *s) {
94 PA_LLIST_REMOVE(pa_subscription, s->core->subscriptions, s);
98 static void free_event(pa_subscription_event *s) {
103 s->core->subscription_event_last = s->prev;
105 PA_LLIST_REMOVE(pa_subscription_event, s->core->subscription_event_queue, s);
109 /* Free all subscription objects */
110 void pa_subscription_free_all(pa_core *c) {
113 while (c->subscriptions)
114 free_subscription(c->subscriptions);
116 while (c->subscription_event_queue)
117 free_event(c->subscription_event_queue);
119 if (c->subscription_defer_event) {
120 c->mainloop->defer_free(c->subscription_defer_event);
121 c->subscription_defer_event = NULL;
126 static void dump_event(const char * prefix, pa_subscription_event*e) {
127 const char * const fac_table[] = {
128 [PA_SUBSCRIPTION_EVENT_SINK] = "SINK",
129 [PA_SUBSCRIPTION_EVENT_SOURCE] = "SOURCE",
130 [PA_SUBSCRIPTION_EVENT_SINK_INPUT] = "SINK_INPUT",
131 [PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT] = "SOURCE_OUTPUT",
132 [PA_SUBSCRIPTION_EVENT_MODULE] = "MODULE",
133 [PA_SUBSCRIPTION_EVENT_CLIENT] = "CLIENT",
134 [PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE] = "SAMPLE_CACHE",
135 [PA_SUBSCRIPTION_EVENT_SERVER] = "SERVER",
136 [PA_SUBSCRIPTION_EVENT_AUTOLOAD] = "AUTOLOAD",
137 [PA_SUBSCRIPTION_EVENT_CARD] = "CARD"
140 const char * const type_table[] = {
141 [PA_SUBSCRIPTION_EVENT_NEW] = "NEW",
142 [PA_SUBSCRIPTION_EVENT_CHANGE] = "CHANGE",
143 [PA_SUBSCRIPTION_EVENT_REMOVE] = "REMOVE"
146 pa_log_debug("%s event (%s|%s|%u)",
148 fac_table[e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK],
149 type_table[e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK],
154 /* Deferred callback for dispatching subscription events */
155 static void defer_cb(pa_mainloop_api *m, pa_defer_event *de, void *userdata) {
156 pa_core *c = userdata;
159 pa_assert(c->mainloop == m);
161 pa_assert(c->subscription_defer_event == de);
163 c->mainloop->defer_enable(c->subscription_defer_event, 0);
165 /* Dispatch queued events */
167 while (c->subscription_event_queue) {
168 pa_subscription_event *e = c->subscription_event_queue;
170 for (s = c->subscriptions; s; s = s->next) {
172 if (!s->dead && pa_subscription_match_flags(s->mask, e->type))
173 s->callback(c, e->type, e->index, s->userdata);
177 dump_event("Dispatched", e);
182 /* Remove dead subscriptions */
184 s = c->subscriptions;
186 pa_subscription *n = s->next;
188 free_subscription(s);
193 /* Schedule an mainloop event so that a pending subscription event is dispatched */
194 static void sched_event(pa_core *c) {
197 if (!c->subscription_defer_event) {
198 c->subscription_defer_event = c->mainloop->defer_new(c->mainloop, defer_cb, c);
199 pa_assert(c->subscription_defer_event);
202 c->mainloop->defer_enable(c->subscription_defer_event, 1);
205 /* Append a new subscription event to the subscription event queue and schedule a main loop event */
206 void pa_subscription_post(pa_core *c, pa_subscription_event_type_t t, uint32_t idx) {
207 pa_subscription_event *e;
210 /* No need for queuing subscriptions of no one is listening */
211 if (!c->subscriptions)
214 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_NEW) {
215 pa_subscription_event *i, *n;
217 /* Check for duplicates */
218 for (i = c->subscription_event_last; i; i = n) {
221 /* not the same object type */
222 if (((t ^ i->type) & PA_SUBSCRIPTION_EVENT_FACILITY_MASK))
225 /* not the same object */
229 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
230 /* This object is being removed, hence there is no
231 * point in keeping the old events regarding this
232 * entry in the queue. */
235 pa_log_debug("Dropped redundant event due to remove event.");
239 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE) {
240 /* This object has changed. If a "new" or "change" event for
241 * this object is still in the queue we can exit. */
243 pa_log_debug("Dropped redundant event due to change event.");
249 e = pa_xnew(pa_subscription_event, 1);
254 PA_LLIST_INSERT_AFTER(pa_subscription_event, c->subscription_event_queue, c->subscription_event_last, e);
255 c->subscription_event_last = e;
258 dump_event("Queued", e);