]> code.delx.au - pulseaudio/blob - src/pulsecore/hook-list.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / hook-list.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulsecore/macro.h>
29
30 #include "hook-list.h"
31
32 void pa_hook_init(pa_hook *hook, void *data) {
33 pa_assert(hook);
34
35 PA_LLIST_HEAD_INIT(pa_hook_slot, hook->slots);
36 hook->last = NULL;
37 hook->n_dead = hook->firing = 0;
38 hook->data = data;
39 }
40
41 static void slot_free(pa_hook *hook, pa_hook_slot *slot) {
42 pa_assert(hook);
43 pa_assert(slot);
44
45 if (hook->last == slot)
46 hook->last = slot->prev;
47
48 PA_LLIST_REMOVE(pa_hook_slot, hook->slots, slot);
49
50 pa_xfree(slot);
51 }
52
53 void pa_hook_free(pa_hook *hook) {
54 pa_assert(hook);
55 pa_assert(!hook->firing);
56
57 while (hook->slots)
58 slot_free(hook, hook->slots);
59
60 pa_hook_init(hook, NULL);
61 }
62
63 pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t cb, void *data) {
64 pa_hook_slot *slot;
65
66 pa_assert(cb);
67
68 slot = pa_xnew(pa_hook_slot, 1);
69 slot->hook = hook;
70 slot->dead = 0;
71 slot->callback = cb;
72 slot->data = data;
73
74 PA_LLIST_INSERT_AFTER(pa_hook_slot, hook->slots, hook->last, slot);
75 hook->last = slot;
76
77 return slot;
78 }
79
80 void pa_hook_slot_free(pa_hook_slot *slot) {
81 pa_assert(slot);
82 pa_assert(!slot->dead);
83
84 if (slot->hook->firing > 0) {
85 slot->dead = 1;
86 slot->hook->n_dead++;
87 } else
88 slot_free(slot->hook, slot);
89 }
90
91 pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data) {
92 pa_hook_slot *slot, *next;
93 pa_hook_result_t result = PA_HOOK_OK;
94
95 pa_assert(hook);
96
97 hook->firing ++;
98
99 for (slot = hook->slots; slot; slot = slot->next) {
100 if (slot->dead)
101 continue;
102
103 if ((result = slot->callback(hook->data, data, slot->data)) != PA_HOOK_OK)
104 break;
105 }
106
107 hook->firing --;
108
109 for (slot = hook->slots; hook->n_dead > 0 && slot; slot = next) {
110 next = slot->next;
111
112 if (slot->dead) {
113 slot_free(hook, slot);
114 hook->n_dead--;
115 }
116 }
117
118 return result;
119 }
120