4 This file is part of polypaudio.
6 polypaudio 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 of the License,
9 or (at your option) any later version.
11 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <polyp/xmalloc.h>
31 #include <polypcore/queue.h>
32 #include <polypcore/log.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
{
46 void (*callback
)(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t index
, void *userdata
);
48 pa_subscription_mask_t mask
;
50 pa_subscription
*prev
, *next
;
53 struct pa_subscription_event
{
54 pa_subscription_event_type_t type
;
58 static void sched_event(pa_core
*c
);
60 /* Allocate a new subscription object for the given subscription mask. Use the specified callback function and user data */
61 pa_subscription
* pa_subscription_new(pa_core
*c
, pa_subscription_mask_t m
, void (*callback
)(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t index
, void *userdata
), void *userdata
) {
65 s
= pa_xmalloc(sizeof(pa_subscription
));
68 s
->callback
= callback
;
69 s
->userdata
= userdata
;
72 if ((s
->next
= c
->subscriptions
))
79 /* Free a subscription object, effectively marking it for deletion */
80 void pa_subscription_free(pa_subscription
*s
) {
81 assert(s
&& !s
->dead
);
86 static void free_item(pa_subscription
*s
) {
90 s
->prev
->next
= s
->next
;
92 s
->core
->subscriptions
= s
->next
;
95 s
->next
->prev
= s
->prev
;
100 /* Free all subscription objects */
101 void pa_subscription_free_all(pa_core
*c
) {
102 pa_subscription_event
*e
;
105 while (c
->subscriptions
)
106 free_item(c
->subscriptions
);
108 if (c
->subscription_event_queue
) {
109 while ((e
= pa_queue_pop(c
->subscription_event_queue
)))
112 pa_queue_free(c
->subscription_event_queue
, NULL
, NULL
);
113 c
->subscription_event_queue
= NULL
;
116 if (c
->subscription_defer_event
) {
117 c
->mainloop
->defer_free(c
->subscription_defer_event
);
118 c
->subscription_defer_event
= NULL
;
123 static void dump_event(pa_subscription_event
*e
) {
124 switch (e
->type
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) {
125 case PA_SUBSCRIPTION_EVENT_SINK
:
126 pa_log(__FILE__
": SINK_EVENT");
128 case PA_SUBSCRIPTION_EVENT_SOURCE
:
129 pa_log(__FILE__
": SOURCE_EVENT");
131 case PA_SUBSCRIPTION_EVENT_SINK_INPUT
:
132 pa_log(__FILE__
": SINK_INPUT_EVENT");
134 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
:
135 pa_log(__FILE__
": SOURCE_OUTPUT_EVENT");
137 case PA_SUBSCRIPTION_EVENT_MODULE
:
138 pa_log(__FILE__
": MODULE_EVENT");
140 case PA_SUBSCRIPTION_EVENT_CLIENT
:
141 pa_log(__FILE__
": CLIENT_EVENT");
144 pa_log(__FILE__
": OTHER");
148 switch (e
->type
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) {
149 case PA_SUBSCRIPTION_EVENT_NEW
:
150 pa_log(__FILE__
": NEW");
152 case PA_SUBSCRIPTION_EVENT_CHANGE
:
153 pa_log(__FILE__
": CHANGE");
155 case PA_SUBSCRIPTION_EVENT_REMOVE
:
156 pa_log(__FILE__
": REMOVE");
159 pa_log(__FILE__
": OTHER");
163 pa_log(__FILE__
": %u", e
->index
);
167 /* Deferred callback for dispatching subscirption events */
168 static void defer_cb(pa_mainloop_api
*m
, pa_defer_event
*de
, void *userdata
) {
169 pa_core
*c
= userdata
;
171 assert(c
&& c
->subscription_defer_event
== de
&& c
->mainloop
== m
);
173 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 0);
175 /* Dispatch queued events */
177 if (c
->subscription_event_queue
) {
178 pa_subscription_event
*e
;
180 while ((e
= pa_queue_pop(c
->subscription_event_queue
))) {
182 for (s
= c
->subscriptions
; s
; s
= s
->next
) {
184 if (!s
->dead
&& pa_subscription_match_flags(s
->mask
, e
->type
))
185 s
->callback(c
, e
->type
, e
->index
, s
->userdata
);
192 /* Remove dead subscriptions */
194 s
= c
->subscriptions
;
196 pa_subscription
*n
= s
->next
;
203 /* Schedule an mainloop event so that a pending subscription event is dispatched */
204 static void sched_event(pa_core
*c
) {
207 if (!c
->subscription_defer_event
) {
208 c
->subscription_defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_cb
, c
);
209 assert(c
->subscription_defer_event
);
212 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 1);
215 /* Append a new subscription event to the subscription event queue and schedule a main loop event */
216 void pa_subscription_post(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t index
) {
217 pa_subscription_event
*e
;
220 e
= pa_xmalloc(sizeof(pa_subscription_event
));
224 if (!c
->subscription_event_queue
) {
225 c
->subscription_event_queue
= pa_queue_new();
226 assert(c
->subscription_event_queue
);
229 pa_queue_push(c
->subscription_event_queue
, e
);