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 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 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
30 #include "subscribe.h"
34 struct pa_subscription
{
37 void (*callback
)(struct pa_core
*c
, enum pa_subscription_event_type t
, uint32_t index
, void *userdata
);
39 enum pa_subscription_mask mask
;
41 struct pa_subscription
*prev
, *next
;
44 struct pa_subscription_event
{
45 enum pa_subscription_event_type type
;
49 static void sched_event(struct pa_core
*c
);
51 struct pa_subscription
* pa_subscription_new(struct pa_core
*c
, enum pa_subscription_mask m
, void (*callback
)(struct pa_core
*c
, enum pa_subscription_event_type t
, uint32_t index
, void *userdata
), void *userdata
) {
52 struct pa_subscription
*s
;
55 s
= pa_xmalloc(sizeof(struct pa_subscription
));
58 s
->callback
= callback
;
59 s
->userdata
= userdata
;
62 if ((s
->next
= c
->subscriptions
))
69 void pa_subscription_free(struct pa_subscription
*s
) {
70 assert(s
&& !s
->dead
);
75 static void free_item(struct pa_subscription
*s
) {
79 s
->prev
->next
= s
->next
;
81 s
->core
->subscriptions
= s
->next
;
84 s
->next
->prev
= s
->prev
;
89 void pa_subscription_free_all(struct pa_core
*c
) {
90 struct pa_subscription_event
*e
;
93 while (c
->subscriptions
)
94 free_item(c
->subscriptions
);
96 if (c
->subscription_event_queue
) {
97 while ((e
= pa_queue_pop(c
->subscription_event_queue
)))
100 pa_queue_free(c
->subscription_event_queue
, NULL
, NULL
);
101 c
->subscription_event_queue
= NULL
;
104 if (c
->subscription_defer_event
) {
105 c
->mainloop
->defer_free(c
->subscription_defer_event
);
106 c
->subscription_defer_event
= NULL
;
110 /*static void dump_event(struct pa_subscription_event*e) {
111 switch (e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
112 case PA_SUBSCRIPTION_EVENT_SINK:
113 pa_log(__FILE__": SINK_EVENT");
115 case PA_SUBSCRIPTION_EVENT_SOURCE:
116 pa_log(__FILE__": SOURCE_EVENT");
118 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
119 pa_log(__FILE__": SINK_INPUT_EVENT");
121 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
122 pa_log(__FILE__": SOURCE_OUTPUT_EVENT");
124 case PA_SUBSCRIPTION_EVENT_MODULE:
125 pa_log(__FILE__": MODULE_EVENT");
127 case PA_SUBSCRIPTION_EVENT_CLIENT:
128 pa_log(__FILE__": CLIENT_EVENT");
131 pa_log(__FILE__": OTHER");
135 switch (e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
136 case PA_SUBSCRIPTION_EVENT_NEW:
137 pa_log(__FILE__": NEW");
139 case PA_SUBSCRIPTION_EVENT_CHANGE:
140 pa_log(__FILE__": CHANGE");
142 case PA_SUBSCRIPTION_EVENT_REMOVE:
143 pa_log(__FILE__": REMOVE");
146 pa_log(__FILE__": OTHER");
150 pa_log(__FILE__": %u\n", e->index);
153 static void defer_cb(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void *userdata
) {
154 struct pa_core
*c
= userdata
;
155 struct pa_subscription
*s
;
156 assert(c
&& c
->subscription_defer_event
== e
&& c
->mainloop
== m
);
158 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 0);
161 /* Dispatch queued events */
163 if (c
->subscription_event_queue
) {
164 struct pa_subscription_event
*e
;
166 while ((e
= pa_queue_pop(c
->subscription_event_queue
))) {
167 struct pa_subscription
*s
;
169 for (s
= c
->subscriptions
; s
; s
= s
->next
) {
171 if (!s
->dead
&& pa_subscription_match_flags(s
->mask
, e
->type
))
172 s
->callback(c
, e
->type
, e
->index
, s
->userdata
);
179 /* Remove dead subscriptions */
181 s
= c
->subscriptions
;
183 struct pa_subscription
*n
= s
->next
;
190 static void sched_event(struct pa_core
*c
) {
193 if (!c
->subscription_defer_event
) {
194 c
->subscription_defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_cb
, c
);
195 assert(c
->subscription_defer_event
);
198 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 1);
202 void pa_subscription_post(struct pa_core
*c
, enum pa_subscription_event_type t
, uint32_t index
) {
203 struct pa_subscription_event
*e
;
206 e
= pa_xmalloc(sizeof(struct pa_subscription_event
));
210 if (!c
->subscription_event_queue
) {
211 c
->subscription_event_queue
= pa_queue_new();
212 assert(c
->subscription_event_queue
);
215 pa_queue_push(c
->subscription_event_queue
, e
);