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"
33 struct pa_subscription
{
36 void (*callback
)(struct pa_core
*c
, enum pa_subscription_event_type t
, uint32_t index
, void *userdata
);
38 enum pa_subscription_mask mask
;
40 struct pa_subscription
*prev
, *next
;
43 struct pa_subscription_event
{
44 enum pa_subscription_event_type type
;
48 static void sched_event(struct pa_core
*c
);
50 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
) {
51 struct pa_subscription
*s
;
54 s
= pa_xmalloc(sizeof(struct pa_subscription
));
57 s
->callback
= callback
;
58 s
->userdata
= userdata
;
61 if ((s
->next
= c
->subscriptions
))
68 void pa_subscription_free(struct pa_subscription
*s
) {
69 assert(s
&& !s
->dead
);
74 static void free_item(struct pa_subscription
*s
) {
78 s
->prev
->next
= s
->next
;
80 s
->core
->subscriptions
= s
->next
;
83 s
->next
->prev
= s
->prev
;
88 void pa_subscription_free_all(struct pa_core
*c
) {
89 struct pa_subscription_event
*e
;
92 while (c
->subscriptions
)
93 free_item(c
->subscriptions
);
95 if (c
->subscription_event_queue
) {
96 while ((e
= pa_queue_pop(c
->subscription_event_queue
)))
99 pa_queue_free(c
->subscription_event_queue
, NULL
, NULL
);
100 c
->subscription_event_queue
= NULL
;
103 if (c
->subscription_defer_event
) {
104 c
->mainloop
->defer_free(c
->subscription_defer_event
);
105 c
->subscription_defer_event
= NULL
;
109 /*static void dump_event(struct pa_subscription_event*e) {
110 switch (e->type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) {
111 case PA_SUBSCRIPTION_EVENT_SINK:
112 fprintf(stderr, "SINK_EVENT");
114 case PA_SUBSCRIPTION_EVENT_SOURCE:
115 fprintf(stderr, "SOURCE_EVENT");
117 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
118 fprintf(stderr, "SINK_INPUT_EVENT");
120 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
121 fprintf(stderr, "SOURCE_OUTPUT_EVENT");
123 case PA_SUBSCRIPTION_EVENT_MODULE:
124 fprintf(stderr, "MODULE_EVENT");
126 case PA_SUBSCRIPTION_EVENT_CLIENT:
127 fprintf(stderr, "CLIENT_EVENT");
130 fprintf(stderr, "OTHER");
134 switch (e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
135 case PA_SUBSCRIPTION_EVENT_NEW:
136 fprintf(stderr, " NEW");
138 case PA_SUBSCRIPTION_EVENT_CHANGE:
139 fprintf(stderr, " CHANGE");
141 case PA_SUBSCRIPTION_EVENT_REMOVE:
142 fprintf(stderr, " REMOVE");
145 fprintf(stderr, " OTHER");
149 fprintf(stderr, " %u\n", e->index);
152 static void defer_cb(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void *userdata
) {
153 struct pa_core
*c
= userdata
;
154 struct pa_subscription
*s
;
155 assert(c
&& c
->subscription_defer_event
== e
&& c
->mainloop
== m
);
157 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 0);
160 /* Dispatch queued events */
162 if (c
->subscription_event_queue
) {
163 struct pa_subscription_event
*e
;
165 while ((e
= pa_queue_pop(c
->subscription_event_queue
))) {
166 struct pa_subscription
*s
;
168 for (s
= c
->subscriptions
; s
; s
= s
->next
) {
170 if (!s
->dead
&& pa_subscription_match_flags(s
->mask
, e
->type
))
171 s
->callback(c
, e
->type
, e
->index
, s
->userdata
);
178 /* Remove dead subscriptions */
180 s
= c
->subscriptions
;
182 struct pa_subscription
*n
= s
->next
;
189 static void sched_event(struct pa_core
*c
) {
192 if (!c
->subscription_defer_event
) {
193 c
->subscription_defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_cb
, c
);
194 assert(c
->subscription_defer_event
);
197 c
->mainloop
->defer_enable(c
->subscription_defer_event
, 1);
201 void pa_subscription_post(struct pa_core
*c
, enum pa_subscription_event_type t
, uint32_t index
) {
202 struct pa_subscription_event
*e
;
205 e
= pa_xmalloc(sizeof(struct pa_subscription_event
));
209 if (!c
->subscription_event_queue
) {
210 c
->subscription_event_queue
= pa_queue_new();
211 assert(c
->subscription_event_queue
);
214 pa_queue_push(c
->subscription_event_queue
, e
);