4accaa97 |
1 | #include "hook.h" |
eccb5352 |
2 | |
3 | |
4 | typedef struct _lttv_hook_closure { |
5 | lttv_hook hook; |
6 | void *hook_data; |
7 | } lttv_hook_closure; |
8 | |
9 | |
10 | inline lttv_hooks *lttv_hooks_new() { |
11 | return g_array_new(FALSE, FALSE, sizeof(lttv_hook_closure)); |
12 | } |
13 | |
14 | /* MD: delete elements of the array also, but don't free pointed addresses |
15 | * (functions). |
16 | */ |
17 | inline void lttv_hooks_destroy(lttv_hooks *h) { |
18 | g_array_free(h, TRUE); |
19 | } |
20 | |
21 | inline void lttv_hooks_add(lttv_hooks *h, lttv_hook f, void *hook_data) { |
22 | lttv_hook_closure c; |
23 | |
24 | /* if h is null, do nothing, or popup a warning message */ |
25 | if(h == NULL)return; |
26 | |
27 | c.hook = f; |
28 | c.hook_data = hook_data; |
29 | g_array_append_val(h,c); |
30 | } |
31 | |
32 | |
33 | void lttv_hooks_call(lttv_hooks *h, void *call_data) |
34 | { |
35 | int i; |
36 | lttv_hook_closure * c; |
37 | |
38 | for(i = 0 ; i < h->len ; i++) { |
39 | c = ((lttv_hook_closure *)h->data) + i; |
40 | if(c != NULL) |
41 | c->hook(c->hook_data,call_data); |
42 | else |
43 | g_critical("NULL hook called\n"); |
44 | } |
45 | } |
46 | |
47 | |
48 | /* Sometimes different hooks need to be called based on the case. The |
49 | case is represented by an unsigned integer id and may represent different |
50 | event types, for instance. */ |
51 | |
52 | inline lttv_hooks_by_id *lttv_hooks_by_id_new() { |
53 | return g_ptr_array_new(); |
54 | } |
55 | |
56 | |
57 | inline void lttv_hooks_by_id_destroy(lttv_hooks_by_id *h) { |
58 | /* MD: delete elements of the array also */ |
59 | g_ptr_array_free(h, TRUE); |
60 | } |
61 | |
62 | |
63 | void lttv_hooks_by_id_add(lttv_hooks_by_id *h, lttv_hook f, void *hook_data, |
64 | unsigned int id) |
65 | { |
66 | if(h->len < id) g_ptr_array_set_size(h, id); |
67 | if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new(); |
68 | lttv_hooks_add(h->pdata[id], f, hook_data); |
69 | } |
70 | |
71 | void lttv_hooks_by_id_call(lttv_hooks_by_id *h, void *call_data, unsigned int id) |
72 | { |
73 | if(h->len <= id || h->pdata[id] == NULL) return; |
74 | lttv_hooks_call(h->pdata[id],call_data); |
75 | } |
76 | |