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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/queue.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
34 #include "core-subscribe.h"
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. */
43 struct pa_subscription
{
47 pa_subscription_cb_t callback
;
49 pa_subscription_mask_t mask
;
51 PA_LLIST_FIELDS(pa_subscription
);
54 struct pa_subscription_event
{
57 pa_subscription_event_type_t type
;
60 PA_LLIST_FIELDS(pa_subscription_event
);
63 static void sched_event(pa_core
*c
);
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
) {
73 s
= pa_xnew(pa_subscription
, 1);
76 s
->callback
= callback
;
77 s
->userdata
= userdata
;
80 PA_LLIST_PREPEND(pa_subscription
, c
->subscriptions
, s
);
84 /* Free a subscription object, effectively marking it for deletion */
85 void pa_subscription_free(pa_subscription
*s
) {
93 static void free_subscription(pa_subscription
*s
) {
97 PA_LLIST_REMOVE(pa_subscription
, s
->core
->subscriptions
, s
);
101 static void free_event(pa_subscription_event
*s
) {
106 s
->core
->subscription_event_last
= s
->prev
;
108 PA_LLIST_REMOVE(pa_subscription_event
, s
->core
->subscription_event_queue
, s
);
112 /* Free all subscription objects */
113 void pa_subscription_free_all(pa_core
*c
) {
116 while (c
->subscriptions
)
117 free_subscription(c
->subscriptions
);
119 while (c
->subscription_event_queue
)
120 free_event(c
->subscription_event_queue
);
122 if (c
->subscription_defer_event
) {
123 c
->mainloop
->defer_free(c
->subscription_defer_event
);
124 c
->subscription_defer_event
= NULL
;
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"
142 const char * const type_table
[] = {
143 [PA_SUBSCRIPTION_EVENT_NEW
] = "NEW",
144 [PA_SUBSCRIPTION_EVENT_CHANGE
] = "CHANGE",
145 [PA_SUBSCRIPTION_EVENT_REMOVE
] = "REMOVE"
148 pa_log_debug("%s event (%s|%s|%u)",
150 fac_table
[e
->type
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
],
151 type_table
[e
->type
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
],
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
;
161 pa_assert(c
->mainloop
== m
);
163 pa_assert(c
->subscription_defer_event
== de
);
165 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 0);
167 /* Dispatch queued events */
169 while (c
->subscription_event_queue
) {
170 pa_subscription_event
*e
= c
->subscription_event_queue
;
172 for (s
= c
->subscriptions
; s
; s
= s
->next
) {
174 if (!s
->dead
&& pa_subscription_match_flags(s
->mask
, e
->type
))
175 s
->callback(c
, e
->type
, e
->index
, s
->userdata
);
179 dump_event("Dispatched", e
);
184 /* Remove dead subscriptions */
186 s
= c
->subscriptions
;
188 pa_subscription
*n
= s
->next
;
190 free_subscription(s
);
195 /* Schedule an mainloop event so that a pending subscription event is dispatched */
196 static void sched_event(pa_core
*c
) {
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
);
204 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 1);
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
;
212 /* No need for queuing subscriptions of noone is listening */
213 if (!c
->subscriptions
)
216 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_NEW
) {
217 pa_subscription_event
*i
, *n
;
219 /* Check for duplicates */
220 for (i
= c
->subscription_event_last
; i
; i
= n
) {
223 /* not the same object type */
224 if (((t
^ i
->type
) & PA_SUBSCRIPTION_EVENT_FACILITY_MASK
))
227 /* not the same object */
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. */
237 pa_log_debug("Dropped redundant event due to remove event.");
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. */
245 pa_log_debug("Dropped redundant event due to change event.");
251 e
= pa_xnew(pa_subscription_event
, 1);
256 PA_LLIST_INSERT_AFTER(pa_subscription_event
, c
->subscription_event_queue
, c
->subscription_event_last
, e
);
257 c
->subscription_event_last
= e
;
260 dump_event("Queued", e
);