]>
jfr.im git - irc/evilnet/x3.git/blob - src/eventhooks.c
1 /* eventhooks.c - Event hooks
2 * Copyright 2000-2024 Evilnet Development
4 * This file is part of x3.
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.
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.
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.
23 #include "eventhooks.h"
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
;
29 list
->add_default
= defpos
;
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
));
42 if ((addpos
!= EH_ADD_HEAD
) && (addpos
!= EH_ADD_TAIL
))
43 addpos
= list
->add_default
;
50 newehf
->extra
= extra
;
52 if (list
->head
== NULL
) {
55 } else if (addpos
> 0) {
56 newehf
->next
= list
->head
;
59 list
->tail
->next
= newehf
;
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
);
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
;
74 if ((ehfi
->func
== func
) && (ehfi
->extra
== extra
)) {
75 list
->head
= ehfi
->next
;
76 if (list
->tail
== ehfi
)
78 if (list
->clean
!= NULL
)
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
)) {
87 ehfi
->next
= ehfi
->next
->next
;
88 if (list
->tail
== ehfr
)
90 if (list
->clean
!= NULL
)
102 void free_hook_func_list(struct eh_func_list
*list
) {
103 struct eh_func
*ehfi
= NULL
;
104 struct eh_func
*ehfn
= NULL
;
107 if (list
->head
== NULL
)
110 for (ehfi
=list
->head
; ehfi
!=NULL
; ehfi
=ehfn
) {
113 if (list
->clean
!= NULL
)
120 assert(i
==list
->count
);
127 void call_hook_func_args(struct eh_func_list
*list
, void *callextra
) {
128 struct eh_func
*ehfi
= NULL
;
130 for (ehfi
=list
->head
; ehfi
!=NULL
; ehfi
=ehfi
->next
) {
131 if (ehfi
->func(ehfi
->extra
, callextra
) != EH_CONT
)
136 void call_hook_func_noargs(struct eh_func_list
*list
) {
137 call_hook_func_args(list
, NULL
);