]> code.delx.au - pulseaudio/blob - src/polypcore/core-subscribe.c
add new module module-volume-restore which saves and restores volume of playback...
[pulseaudio] / src / polypcore / core-subscribe.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include <polypcore/queue.h>
30 #include <polypcore/xmalloc.h>
31 #include <polypcore/log.h>
32
33 #include "core-subscribe.h"
34
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. */
41
42 struct pa_subscription {
43 pa_core *core;
44 int dead;
45 void (*callback)(pa_core *c, pa_subscription_event_type_t t, uint32_t index, void *userdata);
46 void *userdata;
47 pa_subscription_mask_t mask;
48
49 pa_subscription *prev, *next;
50 };
51
52 struct pa_subscription_event {
53 pa_subscription_event_type_t type;
54 uint32_t index;
55 };
56
57 static void sched_event(pa_core *c);
58
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) {
61 pa_subscription *s;
62 assert(c);
63
64 s = pa_xmalloc(sizeof(pa_subscription));
65 s->core = c;
66 s->dead = 0;
67 s->callback = callback;
68 s->userdata = userdata;
69 s->mask = m;
70
71 if ((s->next = c->subscriptions))
72 s->next->prev = s;
73 s->prev = NULL;
74 c->subscriptions = s;
75 return s;
76 }
77
78 /* Free a subscription object, effectively marking it for deletion */
79 void pa_subscription_free(pa_subscription*s) {
80 assert(s && !s->dead);
81 s->dead = 1;
82 sched_event(s->core);
83 }
84
85 static void free_item(pa_subscription *s) {
86 assert(s && s->core);
87
88 if (s->prev)
89 s->prev->next = s->next;
90 else
91 s->core->subscriptions = s->next;
92
93 if (s->next)
94 s->next->prev = s->prev;
95
96 pa_xfree(s);
97 }
98
99 /* Free all subscription objects */
100 void pa_subscription_free_all(pa_core *c) {
101 pa_subscription_event *e;
102 assert(c);
103
104 while (c->subscriptions)
105 free_item(c->subscriptions);
106
107 if (c->subscription_event_queue) {
108 while ((e = pa_queue_pop(c->subscription_event_queue)))
109 pa_xfree(e);
110
111 pa_queue_free(c->subscription_event_queue, NULL, NULL);
112 c->subscription_event_queue = NULL;
113 }
114
115 if (c->subscription_defer_event) {
116 c->mainloop->defer_free(c->subscription_defer_event);
117 c->subscription_defer_event = NULL;
118 }
119 }
120
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");
125 break;
126 case PA_SUBSCRIPTION_EVENT_SOURCE:
127 pa_log(__FILE__": SOURCE_EVENT");
128 break;
129 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
130 pa_log(__FILE__": SINK_INPUT_EVENT");
131 break;
132 case PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT:
133 pa_log(__FILE__": SOURCE_OUTPUT_EVENT");
134 break;
135 case PA_SUBSCRIPTION_EVENT_MODULE:
136 pa_log(__FILE__": MODULE_EVENT");
137 break;
138 case PA_SUBSCRIPTION_EVENT_CLIENT:
139 pa_log(__FILE__": CLIENT_EVENT");
140 break;
141 default:
142 pa_log(__FILE__": OTHER");
143 break;
144 }
145
146 switch (e->type & PA_SUBSCRIPTION_EVENT_TYPE_MASK) {
147 case PA_SUBSCRIPTION_EVENT_NEW:
148 pa_log(__FILE__": NEW");
149 break;
150 case PA_SUBSCRIPTION_EVENT_CHANGE:
151 pa_log(__FILE__": CHANGE");
152 break;
153 case PA_SUBSCRIPTION_EVENT_REMOVE:
154 pa_log(__FILE__": REMOVE");
155 break;
156 default:
157 pa_log(__FILE__": OTHER");
158 break;
159 }
160
161 pa_log(__FILE__": %u", e->index);
162 }*/
163
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;
167 pa_subscription *s;
168 assert(c && c->subscription_defer_event == de && c->mainloop == m);
169
170 c->mainloop->defer_enable(c->subscription_defer_event, 0);
171
172
173 /* Dispatch queued events */
174
175 if (c->subscription_event_queue) {
176 pa_subscription_event *e;
177
178 while ((e = pa_queue_pop(c->subscription_event_queue))) {
179
180 for (s = c->subscriptions; s; s = s->next) {
181
182 if (!s->dead && pa_subscription_match_flags(s->mask, e->type))
183 s->callback(c, e->type, e->index, s->userdata);
184 }
185
186 pa_xfree(e);
187 }
188 }
189
190 /* Remove dead subscriptions */
191
192 s = c->subscriptions;
193 while (s) {
194 pa_subscription *n = s->next;
195 if (s->dead)
196 free_item(s);
197 s = n;
198 }
199 }
200
201 /* Schedule an mainloop event so that a pending subscription event is dispatched */
202 static void sched_event(pa_core *c) {
203 assert(c);
204
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);
208 }
209
210 c->mainloop->defer_enable(c->subscription_defer_event, 1);
211 }
212
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;
216 assert(c);
217
218 e = pa_xmalloc(sizeof(pa_subscription_event));
219 e->type = t;
220 e->index = index;
221
222 if (!c->subscription_event_queue) {
223 c->subscription_event_queue = pa_queue_new();
224 assert(c->subscription_event_queue);
225 }
226
227 pa_queue_push(c->subscription_event_queue, e);
228 sched_event(c);
229 }
230
231