| 1 | #ifndef HOOK_H |
| 2 | #define HOOK_H |
| 3 | |
| 4 | #include <glib.h> |
| 5 | |
| 6 | /* A hook is a function to call with the supplied hook data, and with |
| 7 | call site specific data (e.g., hooks for events are called with a |
| 8 | pointer to the current event). */ |
| 9 | |
| 10 | typedef gboolean (*LttvHook)(void *hook_data, void *call_data); |
| 11 | |
| 12 | |
| 13 | /* A list of hooks allows registering hooks to be called later. */ |
| 14 | |
| 15 | typedef GArray LttvHooks; |
| 16 | |
| 17 | |
| 18 | /* Create and destroy a list of hooks */ |
| 19 | |
| 20 | LttvHooks *lttv_hooks_new(); |
| 21 | |
| 22 | void lttv_hooks_destroy(LttvHooks *h); |
| 23 | |
| 24 | |
| 25 | /* Add a hook and its hook data to the list */ |
| 26 | |
| 27 | void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data); |
| 28 | |
| 29 | |
| 30 | /* Add a list of hooks to the list h */ |
| 31 | |
| 32 | void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list); |
| 33 | |
| 34 | |
| 35 | /* Remove a hook from the list. Return the hook data. */ |
| 36 | |
| 37 | void *lttv_hooks_remove(LttvHooks *h, LttvHook f); |
| 38 | |
| 39 | |
| 40 | /* Remove a hook from the list checking that the hook data match. */ |
| 41 | |
| 42 | void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data); |
| 43 | |
| 44 | |
| 45 | /* Remove a list of hooks from the hooks list in h. */ |
| 46 | |
| 47 | void lttv_hooks_remove_data_list(LttvHooks *h, LttvHook *list); |
| 48 | |
| 49 | |
| 50 | /* Return the number of hooks in the list */ |
| 51 | |
| 52 | unsigned lttv_hooks_number(LttvHooks *h); |
| 53 | |
| 54 | |
| 55 | /* Return the hook at the specified position in the list */ |
| 56 | |
| 57 | void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data); |
| 58 | |
| 59 | |
| 60 | /* Remove the specified hook. The position of the following hooks may change */ |
| 61 | |
| 62 | void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i); |
| 63 | |
| 64 | |
| 65 | /* Call all the hooks in the list, each with its hook data, |
| 66 | with the specified call data. Return TRUE if one hook returned TRUE. */ |
| 67 | |
| 68 | gboolean lttv_hooks_call(LttvHooks *h, void *call_data); |
| 69 | |
| 70 | |
| 71 | /* Call the hooks in the list until one returns true, in which case TRUE is |
| 72 | returned. */ |
| 73 | |
| 74 | gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data); |
| 75 | |
| 76 | |
| 77 | /* Sometimes different hooks need to be called based on the case. The |
| 78 | case is represented by an unsigned integer id */ |
| 79 | |
| 80 | typedef GPtrArray LttvHooksById; |
| 81 | |
| 82 | |
| 83 | /* Create and destroy a hooks by id list */ |
| 84 | |
| 85 | LttvHooksById *lttv_hooks_by_id_new(); |
| 86 | |
| 87 | void lttv_hooks_by_id_destroy(LttvHooksById *h); |
| 88 | |
| 89 | |
| 90 | /* Obtain the hooks for a given id, creating a list if needed */ |
| 91 | |
| 92 | LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id); |
| 93 | |
| 94 | |
| 95 | /* Return an id larger than any for which a list exists. */ |
| 96 | |
| 97 | unsigned lttv_hooks_by_id_max_id(LttvHooksById *h); |
| 98 | |
| 99 | |
| 100 | /* Get the list of hooks for an id, NULL if none exists */ |
| 101 | |
| 102 | LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id); |
| 103 | |
| 104 | |
| 105 | /* Remove the list of hooks associated with an id */ |
| 106 | |
| 107 | void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id); |
| 108 | |
| 109 | #endif // HOOK_H |