]>
Commit | Line | Data |
---|---|---|
2c5db955 CP |
1 | /* hooks.c */ |
2 | ||
3 | #include "hooks.h" | |
5c86c36f | 4 | #include "error.h" |
2c5db955 | 5 | #include "../lib/array.h" |
5c86c36f | 6 | #include <assert.h> |
8dabf9c9 | 7 | #include <stdlib.h> |
2c5db955 CP |
8 | |
9 | array hooks[HOOKMAX]; | |
a451a113 | 10 | unsigned int hookqueuelength; |
2c5db955 CP |
11 | |
12 | void inithooks() { | |
13 | int i; | |
14 | ||
526e7c1d | 15 | hookqueuelength=0; |
2c5db955 CP |
16 | for (i=0;i<HOOKMAX;i++) { |
17 | array_init(&(hooks[i]),sizeof(HookCallback)); | |
18 | array_setlim1(&(hooks[i]),2); | |
19 | array_setlim2(&(hooks[i]),2); | |
20 | } | |
21 | } | |
22 | ||
23 | int registerhook(int hooknum, HookCallback callback) { | |
24 | int i; | |
25 | HookCallback *hcbs; | |
26 | ||
27 | if (hooknum>HOOKMAX) | |
28 | return 1; | |
29 | ||
30 | hcbs=(HookCallback *)(hooks[hooknum].content); | |
31 | for(i=0;i<hooks[hooknum].cursi;i++) | |
32 | if(hcbs[i]==callback) | |
33 | return 1; | |
34 | ||
8dabf9c9 | 35 | /* If there is a previously blanked slot, go in there */ |
36 | for(i=0;i<hooks[hooknum].cursi;i++) { | |
37 | if(hcbs[i]==NULL) { | |
38 | hcbs[i]=callback; | |
39 | return 0; | |
40 | } | |
41 | } | |
42 | ||
2c5db955 CP |
43 | i=array_getfreeslot(&hooks[hooknum]); |
44 | hcbs=(HookCallback *)(hooks[hooknum].content); | |
45 | hcbs[i]=callback; | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
5c86c36f CP |
50 | int registerpriorityhook(int hooknum, HookCallback callback, long priority) { |
51 | Error("core", ERR_WARNING, "Priority hook system not loaded."); | |
52 | ||
53 | return registerhook(hooknum, callback); | |
54 | } | |
55 | ||
2c5db955 CP |
56 | int deregisterhook(int hooknum, HookCallback callback) { |
57 | int i; | |
58 | HookCallback *hcbs; | |
59 | ||
60 | if (hooknum>HOOKMAX) | |
61 | return 1; | |
62 | ||
63 | hcbs=(HookCallback *)(hooks[hooknum].content); | |
64 | ||
65 | for(i=0;i<hooks[hooknum].cursi;i++) | |
66 | if(hcbs[i]==callback) { | |
8dabf9c9 | 67 | /* If there is an ongoing callback, don't actually delete from the |
68 | * array in case THIS hook is active */ | |
69 | if (hookqueuelength) { | |
70 | hcbs[i]=NULL; | |
71 | } else { | |
72 | array_delslot(&(hooks[hooknum]),i); | |
73 | } | |
2c5db955 CP |
74 | return 0; |
75 | } | |
76 | ||
77 | return 1; | |
78 | } | |
79 | ||
80 | void triggerhook(int hooknum, void *arg) { | |
81 | int i; | |
82 | HookCallback *hcbs; | |
83 | ||
84 | if (hooknum>HOOKMAX) | |
85 | return; | |
86 | ||
526e7c1d | 87 | hookqueuelength++; |
2c5db955 CP |
88 | hcbs=(HookCallback *)(hooks[hooknum].content); |
89 | for (i=0;i<hooks[hooknum].cursi;i++) { | |
8dabf9c9 | 90 | if (hcbs[i]) |
91 | (hcbs[i])(hooknum, arg); | |
2c5db955 | 92 | } |
526e7c1d P |
93 | hookqueuelength--; |
94 | ||
95 | if (!hookqueuelength && hooknum!=HOOK_CORE_ENDOFHOOKSQUEUE) | |
96 | triggerhook(HOOK_CORE_ENDOFHOOKSQUEUE, 0); | |
2c5db955 | 97 | } |