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 <polypcore/queue.h>
30 #include <polypcore/xmalloc.h>
31 #include <polypcore/log.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
{
45 void (*callback
)(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t index
, void *userdata
);
47 pa_subscription_mask_t mask
;
49 pa_subscription
*prev
, *next
;
52 struct pa_subscription_event
{
53 pa_subscription_event_type_t type
;
57 static void sched_event(pa_core
*c
);
59 /* Allocate a new subscription object for the given subscription mask. Use the specified callback function and user data */
60 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
) {
64 s
= pa_xmalloc(sizeof(pa_subscription
));
67 s
->callback
= callback
;
68 s
->userdata
= userdata
;
71 if ((s
->next
= c
->subscriptions
))
78 /* Free a subscription object, effectively marking it for deletion */
79 void pa_subscription_free(pa_subscription
*s
) {
80 assert(s
&& !s
->dead
);
85 static void free_item(pa_subscription
*s
) {
89 s
->prev
->next
= s
->next
;
91 s
->core
->subscriptions
= s
->next
;
94 s
->next
->prev
= s
->prev
;
99 /* Free all subscription objects */
100 void pa_subscription_free_all(pa_core
*c
) {
101 pa_subscription_event
*e
;
104 while (c
->subscriptions
)
105 free_item(c
->subscriptions
);
107 if (c
->subscription_event_queue
) {
108 while ((e
= pa_queue_pop(c
->subscription_event_queue
)))
111 pa_queue_free(c
->subscription_event_queue
, NULL
, NULL
);
112 c
->subscription_event_queue
= NULL
;
115 if (c
->subscription_defer_event
) {
116 c
->mainloop
->defer_free(c
->subscription_defer_event
);
117 c
->subscription_defer_event
= NULL
;
121 /*static void dump_event(pa_subscription_event*e) {
122 switch (e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
123 case PA_SUBSCRIPTION_EVENT_SINK:
124 pa_log(__FILE__": SINK_EVENT");
126 case PA_SUBSCRIPTION_EVENT_SOURCE:
127 pa_log(__FILE__": SOURCE_EVENT");
129 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
130 pa_log(__FILE__": SINK_INPUT_EVENT");
132 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
133 pa_log(__FILE__": SOURCE_OUTPUT_EVENT");
135 case PA_SUBSCRIPTION_EVENT_MODULE:
136 pa_log(__FILE__": MODULE_EVENT");
138 case PA_SUBSCRIPTION_EVENT_CLIENT:
139 pa_log(__FILE__": CLIENT_EVENT");
142 pa_log(__FILE__": OTHER");
146 switch (e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
147 case PA_SUBSCRIPTION_EVENT_NEW:
148 pa_log(__FILE__": NEW");
150 case PA_SUBSCRIPTION_EVENT_CHANGE:
151 pa_log(__FILE__": CHANGE");
153 case PA_SUBSCRIPTION_EVENT_REMOVE:
154 pa_log(__FILE__": REMOVE");
157 pa_log(__FILE__": OTHER");
161 pa_log(__FILE__": %u\n", e->index);
164 /* Deferred callback for dispatching subscirption events */
165 static void defer_cb(pa_mainloop_api
*m
, pa_defer_event
*de
, void *userdata
) {
166 pa_core
*c
= userdata
;
168 assert(c
&& c
->subscription_defer_event
== de
&& c
->mainloop
== m
);
170 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 0);
173 /* Dispatch queued events */
175 if (c
->subscription_event_queue
) {
176 pa_subscription_event
*e
;
178 while ((e
= pa_queue_pop(c
->subscription_event_queue
))) {
180 for (s
= c
->subscriptions
; s
; s
= s
->next
) {
182 if (!s
->dead
&& pa_subscription_match_flags(s
->mask
, e
->type
))
183 s
->callback(c
, e
->type
, e
->index
, s
->userdata
);
190 /* Remove dead subscriptions */
192 s
= c
->subscriptions
;
194 pa_subscription
*n
= s
->next
;
201 /* Schedule an mainloop event so that a pending subscription event is dispatched */
202 static void sched_event(pa_core
*c
) {
205 if (!c
->subscription_defer_event
) {
206 c
->subscription_defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_cb
, c
);
207 assert(c
->subscription_defer_event
);
210 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 1);
213 /* Append a new subscription event to the subscription event queue and schedule a main loop event */
214 void pa_subscription_post(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t index
) {
215 pa_subscription_event
*e
;
218 e
= pa_xmalloc(sizeof(pa_subscription_event
));
222 if (!c
->subscription_event_queue
) {
223 c
->subscription_event_queue
= pa_queue_new();
224 assert(c
->subscription_event_queue
);
227 pa_queue_push(c
->subscription_event_queue
, e
);