]> code.delx.au - pulseaudio/blob - src/pulsecore/hook-list.c
Huge trailing whitespace cleanup. Let's keep the tree pure from here on,
[pulseaudio] / src / pulsecore / hook-list.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 PulseAudio 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
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #include <pulsecore/hook-list.h>
23
24 void pa_hook_init(pa_hook *hook, void *data) {
25 assert(hook);
26
27 PA_LLIST_HEAD_INIT(pa_hook_slot, hook->slots);
28 hook->last = NULL;
29 hook->n_dead = hook->firing = 0;
30 hook->data = data;
31 }
32
33 static void slot_free(pa_hook *hook, pa_hook_slot *slot) {
34 assert(hook);
35 assert(slot);
36
37 if (hook->last == slot)
38 hook->last = slot->prev;
39
40 PA_LLIST_REMOVE(pa_hook_slot, hook->slots, slot);
41
42 pa_xfree(slot);
43 }
44
45 void pa_hook_free(pa_hook *hook) {
46 assert(hook);
47 assert(!hook->firing);
48
49 while (hook->slots)
50 slot_free(hook, hook->slots);
51
52 pa_hook_init(hook, NULL);
53 }
54
55 pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t cb, void *data) {
56 pa_hook_slot *slot;
57
58 assert(cb);
59
60 slot = pa_xnew(pa_hook_slot, 1);
61 slot->hook = hook;
62 slot->dead = 0;
63 slot->callback = cb;
64 slot->data = data;
65
66 PA_LLIST_INSERT_AFTER(pa_hook_slot, hook->slots, hook->last, slot);
67 hook->last = slot;
68
69 return slot;
70 }
71
72 void pa_hook_slot_free(pa_hook_slot *slot) {
73 assert(slot);
74 assert(!slot->dead);
75
76 if (slot->hook->firing > 0) {
77 slot->dead = 1;
78 slot->hook->n_dead++;
79 } else
80 slot_free(slot->hook, slot);
81 }
82
83 pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data) {
84 pa_hook_slot *slot, *next;
85 pa_hook_result_t result = PA_HOOK_OK;
86
87 assert(hook);
88
89 hook->firing ++;
90
91 for (slot = hook->slots; slot; slot = slot->next) {
92 if (slot->dead)
93 continue;
94
95 if ((result = slot->callback(hook->data, data, slot->data)) != PA_HOOK_OK)
96 break;
97 }
98
99 hook->firing --;
100
101 for (slot = hook->slots; hook->n_dead > 0 && slot; slot = next) {
102 next = slot->next;
103
104 if (slot->dead) {
105 slot_free(hook, slot);
106 hook->n_dead--;
107 }
108 }
109
110 return result;
111 }
112