rework hook list stuff once again: change the callback prototype to recieve three data pointers: one to the data for the hook, once for the slot and once for the call

git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1235 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2006-08-13 16:13:36 +00:00
parent 281125c727
commit db3f561ec4
3 changed files with 27 additions and 18 deletions

View file

@ -21,12 +21,13 @@
#include <pulsecore/hook-list.h>
void pa_hook_init(pa_hook *hook) {
void pa_hook_init(pa_hook *hook, void *data) {
assert(hook);
PA_LLIST_HEAD_INIT(pa_hook_slots, hook->slots);
hook->last = NULL;
hook->n_dead = hook->firing = 0;
hook->data = data;
}
static void slot_free(pa_hook *hook, pa_hook_slot *slot) {
@ -48,10 +49,10 @@ void pa_hook_free(pa_hook *hook) {
while (hook->slots)
slot_free(hook, hook->slots);
pa_hook_init(hook);
pa_hook_init(hook, NULL);
}
pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t cb, void *userdata) {
pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t cb, void *data) {
pa_hook_slot *slot;
assert(cb);
@ -60,7 +61,7 @@ pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t cb, void *userdata) {
slot->hook = hook;
slot->dead = 0;
slot->callback = cb;
slot->userdata = userdata;
slot->data = data;
PA_LLIST_INSERT_AFTER(pa_hook_slot, hook->slots, hook->last, slot);
hook->last = slot;
@ -91,7 +92,7 @@ pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data) {
if (slot->dead)
continue;
if ((result = slot->callback(data, slot->userdata)) != PA_HOOK_OK)
if ((result = slot->callback(hook->data, data, slot->data)) != PA_HOOK_OK)
break;
}

View file

@ -35,13 +35,16 @@ typedef enum pa_hook_result {
PA_HOOK_CANCEL = -1
} pa_hook_result_t;
typedef pa_hook_result_t (*pa_hook_cb_t)(void *data, void *userdata);
typedef pa_hook_result_t (*pa_hook_cb_t)(
void *hook_data,
void *call_data,
void *slot_data);
struct pa_hook_slot {
int dead;
pa_hook *hook;
pa_hook_cb_t callback;
void *userdata;
void *data;
PA_LLIST_FIELDS(pa_hook_slot);
};
@ -49,12 +52,14 @@ struct pa_hook {
PA_LLIST_HEAD(pa_hook_slot, slots);
pa_hook_slot *last;
int firing, n_dead;
void *data;
};
void pa_hook_init(pa_hook *hook);
void pa_hook_init(pa_hook *hook, void *data);
void pa_hook_free(pa_hook *hook);
pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t, void *userdata);
pa_hook_slot* pa_hook_connect(pa_hook *hook, pa_hook_cb_t, void *data);
void pa_hook_slot_free(pa_hook_slot *slot);
pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data);

View file

@ -3,13 +3,13 @@
#include <pulsecore/hook-list.h>
#include <pulsecore/log.h>
static pa_hook_result_t func1(const char*a, void *userdata) {
pa_log("#1 arg=%s userdata=%s", a, (char*) userdata);
static pa_hook_result_t func1(const char*hook_data, const char*call_data, const char*slot_data) {
pa_log("(func1) hook=%s call=%s slot=%s", hook_data, call_data, slot_data);
return PA_HOOK_OK;
}
static pa_hook_result_t func2(const char*a, void *userdata) {
pa_log("#2 arg=%s userdata=%s", a, (char*) userdata);
static pa_hook_result_t func2(const char*hook_data, const char*call_data, const char*slot_data) {
pa_log("(func2) hook=%s call=%s slot=%s", hook_data, call_data, slot_data);
return PA_HOOK_OK;
}
@ -17,15 +17,18 @@ int main(int argc, char *argv[]) {
pa_hook hook;
pa_hook_slot *slot;
pa_hook_init(&hook);
pa_hook_init(&hook, (void*) "hook");
pa_hook_connect(&hook, (pa_hook_cb_t) func1, (void*) "1-1");
slot = pa_hook_connect(&hook, (pa_hook_cb_t) func2, (void*) "2-1");
pa_hook_connect(&hook, (pa_hook_cb_t) func1, (void*) "1-2");
pa_hook_connect(&hook, (pa_hook_cb_t) func1, (void*) "slot1");
slot = pa_hook_connect(&hook, (pa_hook_cb_t) func2, (void*) "slot2");
pa_hook_connect(&hook, (pa_hook_cb_t) func1, (void*) "slot3");
pa_hook_fire(&hook, (void*) "arg2");
pa_hook_fire(&hook, (void*) "call1");
pa_hook_slot_free(slot);
pa_hook_fire(&hook, (void*) "call2");
pa_hook_free(&hook);
return 0;