diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2010-10-23 08:58:18 +0200 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2010-10-23 08:58:18 +0200 |
commit | 0cf04dca7ce94ce28f3774233bc80826b18c08b0 (patch) | |
tree | 3126694d58281da3fb48836e1e95bff2bfae0840 /src/core | |
parent | 6e126937bc2fd5ee7333f557887e9d422bb84ca5 (diff) | |
download | weechat-0cf04dca7ce94ce28f3774233bc80826b18c08b0.zip |
Add new functions in plugin API: hook_hsignal and hook_hsignal_send
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-hook.c | 96 | ||||
-rw-r--r-- | src/core/wee-hook.h | 20 |
2 files changed, 114 insertions, 2 deletions
diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index d49732ff4..dc0726231 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -53,8 +53,8 @@ char *hook_type_string[HOOK_NUM_TYPES] = { "command", "command_run", "timer", "fd", "process", "connect", "print", - "signal", "config", "completion", "modifier", "info", "info_hashtable", - "infolist" }; + "signal", "hsignal", "config", "completion", "modifier", + "info", "info_hashtable", "infolist" }; struct t_hook *weechat_hooks[HOOK_NUM_TYPES]; /* list of hooks */ struct t_hook *last_weechat_hook[HOOK_NUM_TYPES]; /* last hook */ int hook_exec_recursion = 0; /* 1 when a hook is executed */ @@ -1876,6 +1876,77 @@ hook_signal_send (const char *signal, const char *type_data, void *signal_data) } /* + * hook_hsignal: hook a hsignal (signal with hashtable) + */ + +struct t_hook * +hook_hsignal (struct t_weechat_plugin *plugin, const char *signal, + t_hook_callback_hsignal *callback, void *callback_data) +{ + struct t_hook *new_hook; + struct t_hook_hsignal *new_hook_hsignal; + int priority; + const char *ptr_signal; + + if (!signal || !signal[0] || !callback) + return NULL; + + new_hook = malloc (sizeof (*new_hook)); + if (!new_hook) + return NULL; + new_hook_hsignal = malloc (sizeof (*new_hook_hsignal)); + if (!new_hook_hsignal) + { + free (new_hook); + return NULL; + } + + hook_get_priority_and_name (signal, &priority, &ptr_signal); + hook_init_data (new_hook, plugin, HOOK_TYPE_HSIGNAL, priority, + callback_data); + + new_hook->hook_data = new_hook_hsignal; + new_hook_hsignal->callback = callback; + new_hook_hsignal->signal = strdup ((ptr_signal) ? ptr_signal : signal); + + hook_add_to_list (new_hook); + + return new_hook; +} + +/* + * hook_hsignal_send: send a hsignal (signal with hashtable) + */ + +void +hook_hsignal_send (const char *signal, struct t_hashtable *hashtable) +{ + struct t_hook *ptr_hook, *next_hook; + + hook_exec_start (); + + ptr_hook = weechat_hooks[HOOK_TYPE_HSIGNAL]; + while (ptr_hook) + { + next_hook = ptr_hook->next_hook; + + if (!ptr_hook->deleted + && !ptr_hook->running + && (string_match (signal, HOOK_HSIGNAL(ptr_hook, signal), 0))) + { + ptr_hook->running = 1; + (void) (HOOK_HSIGNAL(ptr_hook, callback)) + (ptr_hook->callback_data, signal, hashtable); + ptr_hook->running = 0; + } + + ptr_hook = next_hook; + } + + hook_exec_end (); +} + +/* * hook_config: hook a config option */ @@ -2553,6 +2624,10 @@ unhook (struct t_hook *hook) if (HOOK_SIGNAL(hook, signal)) free (HOOK_SIGNAL(hook, signal)); break; + case HOOK_TYPE_HSIGNAL: + if (HOOK_HSIGNAL(hook, signal)) + free (HOOK_HSIGNAL(hook, signal)); + break; case HOOK_TYPE_CONFIG: if (HOOK_CONFIG(hook, option)) free (HOOK_CONFIG(hook, option)); @@ -2865,6 +2940,15 @@ hook_add_to_infolist_type (struct t_infolist *infolist, return 0; } break; + case HOOK_TYPE_HSIGNAL: + if (!ptr_hook->deleted) + { + if (!infolist_new_var_pointer (ptr_item, "callback", HOOK_HSIGNAL(ptr_hook, callback))) + return 0; + if (!infolist_new_var_string (ptr_item, "signal", HOOK_HSIGNAL(ptr_hook, signal))) + return 0; + } + break; case HOOK_TYPE_CONFIG: if (!ptr_hook->deleted) { @@ -3181,6 +3265,14 @@ hook_print_log () log_printf (" signal. . . . . . . . : '%s'", HOOK_SIGNAL(ptr_hook, signal)); } break; + case HOOK_TYPE_HSIGNAL: + if (!ptr_hook->deleted) + { + log_printf (" signal data:"); + log_printf (" callback. . . . . . . : 0x%lx", HOOK_HSIGNAL(ptr_hook, callback)); + log_printf (" signal. . . . . . . . : '%s'", HOOK_HSIGNAL(ptr_hook, signal)); + } + break; case HOOK_TYPE_CONFIG: if (!ptr_hook->deleted) { diff --git a/src/core/wee-hook.h b/src/core/wee-hook.h index 2e2b735d6..c849b36af 100644 --- a/src/core/wee-hook.h +++ b/src/core/wee-hook.h @@ -43,6 +43,7 @@ enum t_hook_type HOOK_TYPE_CONNECT, /* connect to peer with fork */ HOOK_TYPE_PRINT, /* printed message */ HOOK_TYPE_SIGNAL, /* signal */ + HOOK_TYPE_HSIGNAL, /* signal (using hashtable) */ HOOK_TYPE_CONFIG, /* config option */ HOOK_TYPE_COMPLETION, /* custom completions */ HOOK_TYPE_MODIFIER, /* string modifier */ @@ -76,6 +77,7 @@ enum t_hook_type #define HOOK_CONNECT(hook, var) (((struct t_hook_connect *)hook->hook_data)->var) #define HOOK_PRINT(hook, var) (((struct t_hook_print *)hook->hook_data)->var) #define HOOK_SIGNAL(hook, var) (((struct t_hook_signal *)hook->hook_data)->var) +#define HOOK_HSIGNAL(hook, var) (((struct t_hook_hsignal *)hook->hook_data)->var) #define HOOK_CONFIG(hook, var) (((struct t_hook_config *)hook->hook_data)->var) #define HOOK_COMPLETION(hook, var) (((struct t_hook_completion *)hook->hook_data)->var) #define HOOK_MODIFIER(hook, var) (((struct t_hook_modifier *)hook->hook_data)->var) @@ -252,6 +254,18 @@ struct t_hook_signal /* with "*", "*" == any signal) */ }; +/* hook hsignal */ + +typedef int (t_hook_callback_hsignal)(void *data, const char *signal, + struct t_hashtable *hashtable); + +struct t_hook_hsignal +{ + t_hook_callback_hsignal *callback; /* signal callback */ + char *signal; /* signal selected (may begin or end */ + /* with "*", "*" == any signal) */ +}; + /* hook config */ typedef int (t_hook_callback_config)(void *data, const char *option, @@ -409,6 +423,12 @@ extern struct t_hook *hook_signal (struct t_weechat_plugin *plugin, void *callback_data); extern void hook_signal_send (const char *signal, const char *type_data, void *signal_data); +extern struct t_hook *hook_hsignal (struct t_weechat_plugin *plugin, + const char *signal, + t_hook_callback_hsignal *callback, + void *callback_data); +extern void hook_hsignal_send (const char *signal, + struct t_hashtable *hashtable); extern struct t_hook *hook_config (struct t_weechat_plugin *plugin, const char *option, t_hook_callback_config *callback, |