1 /* GLIB - Library of useful routines for C programming 2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 /* 19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS 20 * file for a list of people on the GLib Team. See the ChangeLog 21 * files for a list of changes. These files are distributed with 22 * GLib at ftp://ftp.gtk.org/pub/gtk/. 23 */ 24 25 #ifndef __G_HOOK_H__ 26 #define __G_HOOK_H__ 27 28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) 29 #error "Only <glib.h> can be included directly." 30 #endif 31 32 #include <glib/gmem.h> 33 34 G_BEGIN_DECLS 35 36 37 /* --- typedefs --- */ 38 typedef struct _GHook GHook; 39 typedef struct _GHookList GHookList; 40 41 typedef gint (*GHookCompareFunc) (GHook *new_hook, 42 GHook *sibling); 43 typedef gboolean (*GHookFindFunc) (GHook *hook, 44 gpointer data); 45 typedef void (*GHookMarshaller) (GHook *hook, 46 gpointer marshal_data); 47 typedef gboolean (*GHookCheckMarshaller) (GHook *hook, 48 gpointer marshal_data); 49 typedef void (*GHookFunc) (gpointer data); 50 typedef gboolean (*GHookCheckFunc) (gpointer data); 51 typedef void (*GHookFinalizeFunc) (GHookList *hook_list, 52 GHook *hook); 53 typedef enum 54 { 55 G_HOOK_FLAG_ACTIVE = 1 << 0, 56 G_HOOK_FLAG_IN_CALL = 1 << 1, 57 G_HOOK_FLAG_MASK = 0x0f 58 } GHookFlagMask; 59 #define G_HOOK_FLAG_USER_SHIFT (4) 60 61 62 /* --- structures --- */ 63 struct _GHookList 64 { 65 gulong seq_id; 66 guint hook_size : 16; 67 guint is_setup : 1; 68 GHook *hooks; 69 gpointer dummy3; 70 GHookFinalizeFunc finalize_hook; 71 gpointer dummy[2]; 72 }; 73 struct _GHook 74 { 75 gpointer data; 76 GHook *next; 77 GHook *prev; 78 guint ref_count; 79 gulong hook_id; 80 guint flags; 81 gpointer func; 82 GDestroyNotify destroy; 83 }; 84 85 86 /* --- macros --- */ 87 #define G_HOOK(hook) ((GHook*) (hook)) 88 #define G_HOOK_FLAGS(hook) (G_HOOK (hook)->flags) 89 #define G_HOOK_ACTIVE(hook) ((G_HOOK_FLAGS (hook) & \ 90 G_HOOK_FLAG_ACTIVE) != 0) 91 #define G_HOOK_IN_CALL(hook) ((G_HOOK_FLAGS (hook) & \ 92 G_HOOK_FLAG_IN_CALL) != 0) 93 #define G_HOOK_IS_VALID(hook) (G_HOOK (hook)->hook_id != 0 && \ 94 (G_HOOK_FLAGS (hook) & \ 95 G_HOOK_FLAG_ACTIVE)) 96 #define G_HOOK_IS_UNLINKED(hook) (G_HOOK (hook)->next == NULL && \ 97 G_HOOK (hook)->prev == NULL && \ 98 G_HOOK (hook)->hook_id == 0 && \ 99 G_HOOK (hook)->ref_count == 0) 100 101 102 /* --- prototypes --- */ 103 /* callback maintenance functions */ 104 GLIB_AVAILABLE_IN_ALL 105 void g_hook_list_init (GHookList *hook_list, 106 guint hook_size); 107 GLIB_AVAILABLE_IN_ALL 108 void g_hook_list_clear (GHookList *hook_list); 109 GLIB_AVAILABLE_IN_ALL 110 GHook* g_hook_alloc (GHookList *hook_list); 111 GLIB_AVAILABLE_IN_ALL 112 void g_hook_free (GHookList *hook_list, 113 GHook *hook); 114 GLIB_AVAILABLE_IN_ALL 115 GHook * g_hook_ref (GHookList *hook_list, 116 GHook *hook); 117 GLIB_AVAILABLE_IN_ALL 118 void g_hook_unref (GHookList *hook_list, 119 GHook *hook); 120 GLIB_AVAILABLE_IN_ALL 121 gboolean g_hook_destroy (GHookList *hook_list, 122 gulong hook_id); 123 GLIB_AVAILABLE_IN_ALL 124 void g_hook_destroy_link (GHookList *hook_list, 125 GHook *hook); 126 GLIB_AVAILABLE_IN_ALL 127 void g_hook_prepend (GHookList *hook_list, 128 GHook *hook); 129 GLIB_AVAILABLE_IN_ALL 130 void g_hook_insert_before (GHookList *hook_list, 131 GHook *sibling, 132 GHook *hook); 133 GLIB_AVAILABLE_IN_ALL 134 void g_hook_insert_sorted (GHookList *hook_list, 135 GHook *hook, 136 GHookCompareFunc func); 137 GLIB_AVAILABLE_IN_ALL 138 GHook* g_hook_get (GHookList *hook_list, 139 gulong hook_id); 140 GLIB_AVAILABLE_IN_ALL 141 GHook* g_hook_find (GHookList *hook_list, 142 gboolean need_valids, 143 GHookFindFunc func, 144 gpointer data); 145 GLIB_AVAILABLE_IN_ALL 146 GHook* g_hook_find_data (GHookList *hook_list, 147 gboolean need_valids, 148 gpointer data); 149 GLIB_AVAILABLE_IN_ALL 150 GHook* g_hook_find_func (GHookList *hook_list, 151 gboolean need_valids, 152 gpointer func); 153 GLIB_AVAILABLE_IN_ALL 154 GHook* g_hook_find_func_data (GHookList *hook_list, 155 gboolean need_valids, 156 gpointer func, 157 gpointer data); 158 /* return the first valid hook, and increment its reference count */ 159 GLIB_AVAILABLE_IN_ALL 160 GHook* g_hook_first_valid (GHookList *hook_list, 161 gboolean may_be_in_call); 162 /* return the next valid hook with incremented reference count, and 163 * decrement the reference count of the original hook 164 */ 165 GLIB_AVAILABLE_IN_ALL 166 GHook* g_hook_next_valid (GHookList *hook_list, 167 GHook *hook, 168 gboolean may_be_in_call); 169 /* GHookCompareFunc implementation to insert hooks sorted by their id */ 170 GLIB_AVAILABLE_IN_ALL 171 gint g_hook_compare_ids (GHook *new_hook, 172 GHook *sibling); 173 /* convenience macros */ 174 #define g_hook_append( hook_list, hook ) \ 175 g_hook_insert_before ((hook_list), NULL, (hook)) 176 /* invoke all valid hooks with the (*GHookFunc) signature. 177 */ 178 GLIB_AVAILABLE_IN_ALL 179 void g_hook_list_invoke (GHookList *hook_list, 180 gboolean may_recurse); 181 /* invoke all valid hooks with the (*GHookCheckFunc) signature, 182 * and destroy the hook if FALSE is returned. 183 */ 184 GLIB_AVAILABLE_IN_ALL 185 void g_hook_list_invoke_check (GHookList *hook_list, 186 gboolean may_recurse); 187 /* invoke a marshaller on all valid hooks. 188 */ 189 GLIB_AVAILABLE_IN_ALL 190 void g_hook_list_marshal (GHookList *hook_list, 191 gboolean may_recurse, 192 GHookMarshaller marshaller, 193 gpointer marshal_data); 194 GLIB_AVAILABLE_IN_ALL 195 void g_hook_list_marshal_check (GHookList *hook_list, 196 gboolean may_recurse, 197 GHookCheckMarshaller marshaller, 198 gpointer marshal_data); 199 200 G_END_DECLS 201 202 #endif /* __G_HOOK_H__ */ 203