]> code.delx.au - pulseaudio/blob - src/pulsecore/hook-list.c
merge Colin Guthrie's module-always-sink module, and add priorization to the hook...
[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->n_dead = hook->n_firing = 0;
37 hook->data = data;
38 }
39
40 static void slot_free(pa_hook *hook, pa_hook_slot *slot) {
41 pa_assert(hook);
42 pa_assert(slot);
43
44 PA_LLIST_REMOVE(pa_hook_slot, hook->slots, slot);
45
46 pa_xfree(slot);
47 }
48
49 void pa_hook_free(pa_hook *hook) {
50 pa_assert(hook);
51 pa_assert(hook->n_firing == 0);
52
53 while (hook->slots)
54 slot_free(hook, hook->slots);
55
56 pa_hook_init(hook, NULL);
57 }
58
59 pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_priority_t prio, pa_hook_cb_t cb, void *data) {
60 pa_hook_slot *slot, *where, *prev;
61
62 pa_assert(cb);
63
64 slot = pa_xnew(pa_hook_slot, 1);
65 slot->hook = hook;
66 slot->dead = FALSE;
67 slot->callback = cb;
68 slot->data = data;
69 slot->priority = prio;
70
71 prev = NULL;
72 for (where = hook->slots; where; where = where->next) {
73 if (prio < where->priority)
74 break;
75 prev = where;
76 }
77
78 PA_LLIST_INSERT_AFTER(pa_hook_slot, hook->slots, prev, slot);
79
80 return slot;
81 }
82
83 void pa_hook_slot_free(pa_hook_slot *slot) {
84 pa_assert(slot);
85 pa_assert(!slot->dead);
86
87 if (slot->hook->n_firing > 0) {
88 slot->dead = TRUE;
89 slot->hook->n_dead++;
90 } else
91 slot_free(slot->hook, slot);
92 }
93
94 pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data) {
95 pa_hook_slot *slot, *next;
96 pa_hook_result_t result = PA_HOOK_OK;
97
98 pa_assert(hook);
99
100 hook->n_firing ++;
101
102 for (slot = hook->slots; slot; slot = slot->next) {
103 if (slot->dead)
104 continue;
105
106 if ((result = slot->callback(hook->data, data, slot->data)) != PA_HOOK_OK)
107 break;
108 }
109
110 hook->n_firing --;
111 pa_assert(hook->n_firing >= 0);
112
113 for (slot = hook->slots; hook->n_dead > 0 && slot; slot = next) {
114 next = slot->next;
115
116 if (slot->dead) {
117 slot_free(hook, slot);
118 hook->n_dead--;
119 }
120 }
121
122 pa_assert(hook->n_dead == 0);
123
124 return result;
125 }