4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/queue.h>
34 #include <pulsecore/log.h>
36 #include "core-subscribe.h"
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. */
45 struct pa_subscription {
49 pa_subscription_cb_t callback;
51 pa_subscription_mask_t mask;
53 PA_LLIST_FIELDS(pa_subscription);
56 struct pa_subscription_event {
59 pa_subscription_event_type_t type;
62 PA_LLIST_FIELDS(pa_subscription_event);
65 static void sched_event(pa_core *c);
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) {
75 s = pa_xnew(pa_subscription, 1);
78 s->callback = callback;
79 s->userdata = userdata;
82 PA_LLIST_PREPEND(pa_subscription, c->subscriptions, s);
86 /* Free a subscription object, effectively marking it for deletion */
87 void pa_subscription_free(pa_subscription*s) {
95 static void free_subscription(pa_subscription *s) {
99 PA_LLIST_REMOVE(pa_subscription, s->core->subscriptions, s);
103 static void free_event(pa_subscription_event *s) {
108 s->core->subscription_event_last = s->prev;
110 PA_LLIST_REMOVE(pa_subscription_event, s->core->subscription_event_queue, s);
114 /* Free all subscription objects */
115 void pa_subscription_free_all(pa_core *c) {
118 while (c->subscriptions)
119 free_subscription(c->subscriptions);
121 while (c->subscription_event_queue)
122 free_event(c->subscription_event_queue);
124 if (c->subscription_defer_event) {
125 c->mainloop->defer_free(c->subscription_defer_event);
126 c->subscription_defer_event = NULL;
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"
144 const char * const type_table[] = {
145 [PA_SUBSCRIPTION_EVENT_NEW] = "NEW",
146 [PA_SUBSCRIPTION_EVENT_CHANGE] = "CHANGE",
147 [PA_SUBSCRIPTION_EVENT_REMOVE] = "REMOVE"
150 pa_log("%s event (%s|%s|%u)",
152 fac_table[e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK],
153 type_table[e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK],
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;
163 assert(c->mainloop == m);
165 assert(c->subscription_defer_event == de);
167 c->mainloop->defer_enable(c->subscription_defer_event, 0);
169 /* Dispatch queued events */
171 while (c->subscription_event_queue) {
172 pa_subscription_event *e = c->subscription_event_queue;
174 for (s = c->subscriptions; s; s = s->next) {
176 if (!s->dead && pa_subscription_match_flags(s->mask, e->type))
177 s->callback(c, e->type, e->index, s->userdata);
181 dump_event("Dispatched", e);
186 /* Remove dead subscriptions */
188 s = c->subscriptions;
190 pa_subscription *n = s->next;
192 free_subscription(s);
197 /* Schedule an mainloop event so that a pending subscription event is dispatched */
198 static void sched_event(pa_core *c) {
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);
206 c->mainloop->defer_enable(c->subscription_defer_event, 1);
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;
214 /* No need for queuing subscriptions of noone is listening */
215 if (!c->subscriptions)
218 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_NEW) {
219 pa_subscription_event *i, *n;
221 /* Check for duplicates */
222 for (i = c->subscription_event_last; i; i = n) {
225 /* not the same object type */
226 if (((t ^ i->type) & PA_SUBSCRIPTION_EVENT_FACILITY_MASK))
229 /* not the same object */
230 if (i->index != index)
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. */
239 pa_log_debug("dropped redundant event.");
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. */
247 pa_log_debug("dropped redundant event.");
253 e = pa_xnew(pa_subscription_event, 1);
258 PA_LLIST_INSERT_AFTER(pa_subscription_event, c->subscription_event_queue, c->subscription_event_last, e);
259 c->subscription_event_last = e;
262 dump_event("Queued", e);