| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* hook.c: Hooks. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* Copyright (c) 2007 Giacomo Lozito <james -at- develia.org> |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 9 |
* purpose with or without fee is hereby granted, provided that the above |
| 10 |
* copyright notice and this permission notice is present in all copies. |
| 11 |
* |
| 12 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 13 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 16 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 17 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 20 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 21 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 |
* POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "mowgli.h" |
| 26 |
#include "core/bootstrap_internal.h" |
| 27 |
|
| 28 |
static mowgli_patricia_t *mowgli_hooks = NULL; |
| 29 |
static mowgli_heap_t *mowgli_hook_item_heap; |
| 30 |
|
| 31 |
static void |
| 32 |
_hook_key_canon(char *str) |
| 33 |
{ |
| 34 |
while (*str) |
| 35 |
{ |
| 36 |
*str = toupper((unsigned char)*str); |
| 37 |
str++; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void |
| 42 |
mowgli_hook_bootstrap(void) |
| 43 |
{ |
| 44 |
mowgli_hooks = mowgli_patricia_create(_hook_key_canon); |
| 45 |
mowgli_hook_item_heap = mowgli_heap_create(sizeof(mowgli_hook_item_t), 64, BH_NOW); |
| 46 |
} |
| 47 |
|
| 48 |
static mowgli_hook_t * |
| 49 |
mowgli_hook_find(const char *name) |
| 50 |
{ |
| 51 |
return mowgli_patricia_retrieve(mowgli_hooks, name); |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
mowgli_hook_register(const char *name) |
| 56 |
{ |
| 57 |
mowgli_hook_t *hook; |
| 58 |
|
| 59 |
return_if_fail(name != NULL); |
| 60 |
return_if_fail((hook = mowgli_hook_find(name)) == NULL); |
| 61 |
|
| 62 |
hook = mowgli_alloc(sizeof *hook); |
| 63 |
hook->name = mowgli_strdup(name); |
| 64 |
|
| 65 |
mowgli_patricia_add(mowgli_hooks, hook->name, hook); |
| 66 |
} |
| 67 |
|
| 68 |
int |
| 69 |
mowgli_hook_associate(const char *name, mowgli_hook_function_t func, void *user_data) |
| 70 |
{ |
| 71 |
mowgli_hook_t *hook; |
| 72 |
mowgli_hook_item_t *hookitem; |
| 73 |
|
| 74 |
return_val_if_fail(name != NULL, -1); |
| 75 |
return_val_if_fail(func != NULL, -1); |
| 76 |
|
| 77 |
hook = mowgli_hook_find(name); |
| 78 |
|
| 79 |
if (hook == NULL) |
| 80 |
{ |
| 81 |
mowgli_hook_register(name); |
| 82 |
hook = mowgli_hook_find(name); |
| 83 |
} |
| 84 |
|
| 85 |
/* this *cant* happen */ |
| 86 |
return_val_if_fail(hook != NULL, -1); |
| 87 |
|
| 88 |
hookitem = mowgli_heap_alloc(mowgli_hook_item_heap); |
| 89 |
hookitem->func = func; |
| 90 |
hookitem->user_data = user_data; |
| 91 |
|
| 92 |
mowgli_node_add(hookitem, &hookitem->node, &hook->items); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
int |
| 98 |
mowgli_hook_dissociate(const char *name, mowgli_hook_function_t func) |
| 99 |
{ |
| 100 |
mowgli_hook_t *hook; |
| 101 |
mowgli_node_t *n, *tn; |
| 102 |
|
| 103 |
return_val_if_fail(name != NULL, -1); |
| 104 |
return_val_if_fail(func != NULL, -1); |
| 105 |
|
| 106 |
hook = mowgli_hook_find(name); |
| 107 |
|
| 108 |
if (hook == NULL) |
| 109 |
return -1; |
| 110 |
|
| 111 |
MOWGLI_LIST_FOREACH_SAFE(n, tn, hook->items.head) |
| 112 |
{ |
| 113 |
mowgli_hook_item_t *hookitem = n->data; |
| 114 |
|
| 115 |
if (hookitem->func == func) |
| 116 |
{ |
| 117 |
mowgli_node_delete(&hookitem->node, &hook->items); |
| 118 |
mowgli_heap_free(mowgli_hook_item_heap, hookitem); |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
mowgli_hook_call(const char *name, void *hook_data) |
| 129 |
{ |
| 130 |
mowgli_hook_t *hook; |
| 131 |
mowgli_node_t *n; |
| 132 |
|
| 133 |
return_if_fail(name != NULL); |
| 134 |
|
| 135 |
hook = mowgli_hook_find(name); |
| 136 |
|
| 137 |
if (hook == NULL) |
| 138 |
return; |
| 139 |
|
| 140 |
MOWGLI_LIST_FOREACH(n, hook->items.head) |
| 141 |
{ |
| 142 |
mowgli_hook_item_t *hookitem = n->data; |
| 143 |
|
| 144 |
return_if_fail(hookitem->func != NULL); |
| 145 |
|
| 146 |
hookitem->func(hook_data, hookitem->user_data); |
| 147 |
} |
| 148 |
} |