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/log.h>
31 #include <pulsecore/macro.h>
33 #include "core-subscribe.h"
35 /* The subscription subsystem may be used to be notified whenever an
36 * entity (sink, source, ...) is created or deleted. Modules may
37 * register a callback function that is called whenever an event
38 * matching a subscription mask happens. The execution of the callback
39 * function is postponed to the next main loop iteration, i.e. is not
40 * called from within the stack frame the entity was created in. */
42 struct pa_subscription
{
46 pa_subscription_cb_t callback
;
48 pa_subscription_mask_t mask
;
50 PA_LLIST_FIELDS(pa_subscription
);
53 struct pa_subscription_event
{
56 pa_subscription_event_type_t type
;
59 PA_LLIST_FIELDS(pa_subscription_event
);
62 static void sched_event(pa_core
*c
);
64 /* Allocate a new subscription object for the given subscription mask. Use the specified callback function and user data */
65 pa_subscription
* pa_subscription_new(pa_core
*c
, pa_subscription_mask_t m
, pa_subscription_cb_t callback
, void *userdata
) {
72 s
= pa_xnew(pa_subscription
, 1);
75 s
->callback
= callback
;
76 s
->userdata
= userdata
;
79 PA_LLIST_PREPEND(pa_subscription
, c
->subscriptions
, s
);
83 /* Free a subscription object, effectively marking it for deletion */
84 void pa_subscription_free(pa_subscription
*s
) {
92 static void free_subscription(pa_subscription
*s
) {
96 PA_LLIST_REMOVE(pa_subscription
, s
->core
->subscriptions
, s
);
100 static void free_event(pa_subscription_event
*s
) {
105 s
->core
->subscription_event_last
= s
->prev
;
107 PA_LLIST_REMOVE(pa_subscription_event
, s
->core
->subscription_event_queue
, s
);
111 /* Free all subscription objects */
112 void pa_subscription_free_all(pa_core
*c
) {
115 while (c
->subscriptions
)
116 free_subscription(c
->subscriptions
);
118 while (c
->subscription_event_queue
)
119 free_event(c
->subscription_event_queue
);
121 if (c
->subscription_defer_event
) {
122 c
->mainloop
->defer_free(c
->subscription_defer_event
);
123 c
->subscription_defer_event
= NULL
;
128 static void dump_event(const char * prefix
, pa_subscription_event
*e
) {
129 const char * const fac_table
[] = {
130 [PA_SUBSCRIPTION_EVENT_SINK
] = "SINK",
131 [PA_SUBSCRIPTION_EVENT_SOURCE
] = "SOURCE",
132 [PA_SUBSCRIPTION_EVENT_SINK_INPUT
] = "SINK_INPUT",
133 [PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
] = "SOURCE_OUTPUT",
134 [PA_SUBSCRIPTION_EVENT_MODULE
] = "MODULE",
135 [PA_SUBSCRIPTION_EVENT_CLIENT
] = "CLIENT",
136 [PA_SUBSCRIPTION_EVENT_SAMPLE_CACHE
] = "SAMPLE_CACHE",
137 [PA_SUBSCRIPTION_EVENT_SERVER
] = "SERVER",
138 [PA_SUBSCRIPTION_EVENT_AUTOLOAD
] = "AUTOLOAD"
141 const char * const type_table
[] = {
142 [PA_SUBSCRIPTION_EVENT_NEW
] = "NEW",
143 [PA_SUBSCRIPTION_EVENT_CHANGE
] = "CHANGE",
144 [PA_SUBSCRIPTION_EVENT_REMOVE
] = "REMOVE"
147 pa_log_debug("%s event (%s|%s|%u)",
149 fac_table
[e
->type
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
],
150 type_table
[e
->type
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
],
155 /* Deferred callback for dispatching subscirption events */
156 static void defer_cb(pa_mainloop_api
*m
, pa_defer_event
*de
, void *userdata
) {
157 pa_core
*c
= userdata
;
160 pa_assert(c
->mainloop
== m
);
162 pa_assert(c
->subscription_defer_event
== de
);
164 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 0);
166 /* Dispatch queued events */
168 while (c
->subscription_event_queue
) {
169 pa_subscription_event
*e
= c
->subscription_event_queue
;
171 for (s
= c
->subscriptions
; s
; s
= s
->next
) {
173 if (!s
->dead
&& pa_subscription_match_flags(s
->mask
, e
->type
))
174 s
->callback(c
, e
->type
, e
->index
, s
->userdata
);
178 dump_event("Dispatched", e
);
183 /* Remove dead subscriptions */
185 s
= c
->subscriptions
;
187 pa_subscription
*n
= s
->next
;
189 free_subscription(s
);
194 /* Schedule an mainloop event so that a pending subscription event is dispatched */
195 static void sched_event(pa_core
*c
) {
198 if (!c
->subscription_defer_event
) {
199 c
->subscription_defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_cb
, c
);
200 pa_assert(c
->subscription_defer_event
);
203 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 1);
206 /* Append a new subscription event to the subscription event queue and schedule a main loop event */
207 void pa_subscription_post(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
) {
208 pa_subscription_event
*e
;
211 /* No need for queuing subscriptions of noone is listening */
212 if (!c
->subscriptions
)
215 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) != PA_SUBSCRIPTION_EVENT_NEW
) {
216 pa_subscription_event
*i
, *n
;
218 /* Check for duplicates */
219 for (i
= c
->subscription_event_last
; i
; i
= n
) {
222 /* not the same object type */
223 if (((t
^ i
->type
) & PA_SUBSCRIPTION_EVENT_FACILITY_MASK
))
226 /* not the same object */
230 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
231 /* This object is being removed, hence there is no
232 * point in keeping the old events regarding this
233 * entry in the queue. */
236 pa_log_debug("Dropped redundant event due to remove event.");
240 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_CHANGE
) {
241 /* This object has changed. If a "new" or "change" event for
242 * this object is still in the queue we can exit. */
244 pa_log_debug("Dropped redundant event due to change event.");
250 e
= pa_xnew(pa_subscription_event
, 1);
255 PA_LLIST_INSERT_AFTER(pa_subscription_event
, c
->subscription_event_queue
, c
->subscription_event_last
, e
);
256 c
->subscription_event_last
= e
;
259 dump_event("Queued", e
);