]>
Commit | Line | Data |
---|---|---|
37e08c82 MB |
1 | /* eventhooks.c - Event hooks |
2 | * Copyright 2000-2024 Evilnet Development | |
3 | * | |
4 | * This file is part of x3. | |
5 | * | |
6 | * x3 is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #include <assert.h> | |
22 | #include <stdlib.h> | |
23 | #include "eventhooks.h" | |
24 | ||
25 | struct eh_func_list *init_hook_func_list(struct eh_func_list *list, int defpos) { | |
26 | if ((defpos != EH_ADD_TAIL) && (defpos != EH_ADD_HEAD)) | |
27 | list->add_default = EH_ADD_TAIL; | |
28 | else | |
29 | list->add_default = defpos; | |
30 | list->head = NULL; | |
31 | list->tail = NULL; | |
32 | list->count = 0; | |
33 | list->clean = NULL; | |
34 | ||
35 | return list; | |
36 | } | |
37 | ||
38 | void reg_hook_func_pos(struct eh_func_list *list, eh_func_t func, void *extra, int pos) { | |
39 | struct eh_func *newehf = malloc(sizeof(struct eh_func)); | |
40 | int addpos = pos; | |
41 | ||
42 | if ((addpos != EH_ADD_HEAD) && (addpos != EH_ADD_TAIL)) | |
43 | addpos = list->add_default; | |
44 | ||
45 | if (newehf == NULL) | |
46 | return; | |
47 | ||
48 | newehf->next = NULL; | |
49 | newehf->func = func; | |
50 | newehf->extra = extra; | |
51 | ||
52 | if (list->head == NULL) { | |
53 | list->head = newehf; | |
54 | list->tail = newehf; | |
55 | } else if (addpos > 0) { | |
56 | newehf->next = list->head; | |
57 | list->head = newehf; | |
58 | } else { | |
59 | list->tail->next = newehf; | |
60 | list->tail = newehf; | |
61 | } | |
62 | list->count++; | |
63 | } | |
64 | ||
65 | void reg_hook_func(struct eh_func_list *list, eh_func_t func, void *extra) { | |
66 | reg_hook_func_pos(list, func, extra, EH_ADD_DEFAULT); | |
67 | } | |
68 | ||
69 | void unreg_hook_func(struct eh_func_list *list, eh_func_t func, void *extra) { | |
70 | struct eh_func *ehfi = list->head; | |
71 | struct eh_func *ehfr = NULL; | |
72 | ||
73 | if (ehfi != NULL) { | |
74 | if ((ehfi->func == func) && (ehfi->extra == extra)) { | |
75 | list->head = ehfi->next; | |
76 | if (list->tail == ehfi) | |
77 | list->tail = NULL; | |
78 | if (list->clean != NULL) | |
79 | list->clean(ehfi); | |
80 | free(ehfi); | |
81 | list->count--; | |
82 | } else { | |
83 | for (ehfi=list->head; ehfi!=NULL; ehfi=ehfi->next) { | |
84 | if (ehfi->next != NULL) { | |
85 | if ((ehfi->next->func == func) && (ehfi->next->extra == extra)) { | |
86 | ehfr = ehfi->next; | |
87 | ehfi->next = ehfi->next->next; | |
88 | if (list->tail == ehfr) | |
89 | list->tail = ehfi; | |
90 | if (list->clean != NULL) | |
91 | list->clean(ehfr); | |
92 | free(ehfr); | |
93 | list->count--; | |
94 | break; | |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | void free_hook_func_list(struct eh_func_list *list) { | |
103 | struct eh_func *ehfi = NULL; | |
104 | struct eh_func *ehfn = NULL; | |
105 | int i = 0; | |
106 | ||
107 | if (list->head == NULL) | |
108 | return; | |
109 | ||
110 | for (ehfi=list->head; ehfi!=NULL; ehfi=ehfn) { | |
111 | ehfn = ehfi->next; | |
112 | ||
113 | if (list->clean != NULL) | |
114 | list->clean(ehfi); | |
115 | ||
116 | free(ehfi); | |
117 | i++; | |
118 | } | |
119 | ||
120 | assert(i==list->count); | |
121 | ||
122 | list->head = NULL; | |
123 | list->tail = NULL; | |
124 | list->count = 0; | |
125 | } | |
126 | ||
127 | void call_hook_func_args(struct eh_func_list *list, void *callextra) { | |
128 | struct eh_func *ehfi = NULL; | |
129 | ||
130 | for (ehfi=list->head; ehfi!=NULL; ehfi=ehfi->next) { | |
131 | if (ehfi->func(ehfi->extra, callextra) != EH_CONT) | |
132 | break; | |
133 | } | |
134 | } | |
135 | ||
136 | void call_hook_func_noargs(struct eh_func_list *list) { | |
137 | call_hook_func_args(list, NULL); | |
138 | } |