From c7320514aaf0e97b4a628fec301fd9c4d1fe2a7d Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 16 Apr 2002 18:11:06 +0000 Subject: [PATCH] fixes for new signaling code. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@2691 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/commands.c | 54 ++++++++------ src/core/commands.h | 31 +++++--- src/core/expandos.c | 4 +- src/fe-text/statusbar.c | 3 +- src/perl/common/Core.xs | 25 +++++-- src/perl/perl-signals.c | 153 +++++++++++----------------------------- src/perl/perl-signals.h | 8 +-- 7 files changed, 121 insertions(+), 157 deletions(-) diff --git a/src/core/commands.c b/src/core/commands.c index 9c0ab3fa..96a74008 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -72,10 +72,10 @@ static COMMAND_MODULE_REC *command_module_find(COMMAND_REC *rec, return NULL; } -static COMMAND_MODULE_REC *command_module_find_func(COMMAND_REC *rec, - SIGNAL_FUNC func) +static COMMAND_MODULE_REC * +command_module_find_and_remove(COMMAND_REC *rec, SIGNAL_FUNC func) { - GSList *tmp; + GSList *tmp, *tmp2; g_return_val_if_fail(rec != NULL, NULL); g_return_val_if_fail(func != NULL, NULL); @@ -83,8 +83,15 @@ static COMMAND_MODULE_REC *command_module_find_func(COMMAND_REC *rec, for (tmp = rec->modules; tmp != NULL; tmp = tmp->next) { COMMAND_MODULE_REC *rec = tmp->data; - if (g_slist_find(rec->signals, (void *) func) != NULL) - return rec; + for (tmp2 = rec->callbacks; tmp2 != NULL; tmp2 = tmp2->next) { + COMMAND_CALLBACK_REC *cb = tmp2->data; + + if (cb->func == func) { + rec->callbacks = + g_slist_remove(rec->callbacks, cb); + return rec; + } + } } return NULL; @@ -131,11 +138,13 @@ command_module_get(COMMAND_REC *rec, const char *module, int protocol) return modrec; } -void command_bind_to(const char *module, int pos, const char *cmd, - int protocol, const char *category, SIGNAL_FUNC func) +void command_bind_full(const char *module, int priority, const char *cmd, + int protocol, const char *category, SIGNAL_FUNC func, + void *user_data) { COMMAND_REC *rec; - COMMAND_MODULE_REC *modrec; + COMMAND_MODULE_REC *modrec; + COMMAND_CALLBACK_REC *cb; char *str; g_return_if_fail(module != NULL); @@ -150,11 +159,14 @@ void command_bind_to(const char *module, int pos, const char *cmd, } modrec = command_module_get(rec, module, protocol); - modrec->signals = g_slist_append(modrec->signals, (void *) func); + cb = g_new0(COMMAND_CALLBACK_REC, 1); + cb->func = func; + cb->user_data = user_data; + modrec->callbacks = g_slist_append(modrec->callbacks, cb); if (func != NULL) { str = g_strconcat("command ", cmd, NULL); - signal_add_full(module, pos, str, func, NULL); + signal_add_full(module, priority, str, func, user_data); g_free(str); } @@ -176,7 +188,8 @@ static void command_module_free(COMMAND_MODULE_REC *modrec, COMMAND_REC *rec) { rec->modules = g_slist_remove(rec->modules, modrec); - g_slist_free(modrec->signals); + g_slist_foreach(modrec->callbacks, (GFunc) g_free, NULL); + g_slist_free(modrec->callbacks); g_free(modrec->name); g_free_not_null(modrec->options); g_free(modrec); @@ -196,7 +209,7 @@ static void command_module_destroy(COMMAND_REC *rec, for (tmp = rec->modules; tmp != NULL; tmp = tmp->next) { COMMAND_MODULE_REC *rec = tmp->data; - if (rec->signals == NULL) + if (rec->callbacks == NULL) freelist = g_slist_append(freelist, rec); else { g_slist_free(freelist); @@ -212,10 +225,10 @@ static void command_module_destroy(COMMAND_REC *rec, command_free(rec); } -void command_unbind(const char *cmd, SIGNAL_FUNC func) +void command_unbind_full(const char *cmd, SIGNAL_FUNC func, void *user_data) { COMMAND_REC *rec; - COMMAND_MODULE_REC *modrec; + COMMAND_MODULE_REC *modrec; char *str; g_return_if_fail(cmd != NULL); @@ -223,17 +236,15 @@ void command_unbind(const char *cmd, SIGNAL_FUNC func) rec = command_find(cmd); if (rec != NULL) { - modrec = command_module_find_func(rec, func); + modrec = command_module_find_and_remove(rec, func); g_return_if_fail(modrec != NULL); - modrec->signals = - g_slist_remove(modrec->signals, (void *) func); - if (modrec->signals == NULL) + if (modrec->callbacks == NULL) command_module_destroy(rec, modrec); } str = g_strconcat("command ", cmd, NULL); - signal_remove(str, func); + signal_remove_data(str, func, user_data); g_free(str); } @@ -767,10 +778,11 @@ static void command_module_unbind_all(COMMAND_REC *rec, { GSList *tmp, *next; - for (tmp = modrec->signals; tmp != NULL; tmp = next) { + for (tmp = modrec->callbacks; tmp != NULL; tmp = next) { + COMMAND_CALLBACK_REC *cb = tmp->data; next = tmp->next; - command_unbind(rec->cmd, (SIGNAL_FUNC) tmp->data); + command_unbind_full(rec->cmd, cb->func, cb->user_data); } if (g_slist_find(commands, rec) != NULL) { diff --git a/src/core/commands.h b/src/core/commands.h index e3bf4374..1733c589 100644 --- a/src/core/commands.h +++ b/src/core/commands.h @@ -3,11 +3,16 @@ #include "signals.h" +typedef struct { + SIGNAL_FUNC func; + void *user_data; +} COMMAND_CALLBACK_REC; + typedef struct { char *name; char *options; int protocol; /* chat protocol required for this command */ - GSList *signals; + GSList *callbacks; } COMMAND_MODULE_REC; typedef struct { @@ -57,17 +62,23 @@ extern GSList *commands; extern char *current_command; /* the command we're right now. */ /* Bind command to specified function. */ -void command_bind_to(const char *module, int pos, const char *cmd, - int protocol, const char *category, SIGNAL_FUNC func); -#define command_bind(a, b, c) command_bind_to(MODULE_NAME, 1, a, -1, b, c) -#define command_bind_first(a, b, c) command_bind_to(MODULE_NAME, 0, a, -1, b, c) -#define command_bind_last(a, b, c) command_bind_to(MODULE_NAME, 2, a, -1, b, c) +void command_bind_full(const char *module, int priority, const char *cmd, + int protocol, const char *category, SIGNAL_FUNC func, + void *user_data); +#define command_bind(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, -1, b, c, NULL) +#define command_bind_first(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, -1, b, c, NULL) +#define command_bind_last(a, b, c) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, -1, b, c, NULL) -#define command_bind_proto(a, b, c, d) command_bind_to(MODULE_NAME, 1, a, b, c, d) -#define command_bind_proto_first(a, b, c, d) command_bind_to(MODULE_NAME, 0, a, b, c, d) -#define command_bind_proto_last(a, b, c, d) command_bind_to(MODULE_NAME, 2, a, b, c, d) +#define command_bind_data(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, -1, b, c, d) +#define command_bind_data_first(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, -1, b, c, d) +#define command_bind_data_last(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, -1, b, c, d) -void command_unbind(const char *cmd, SIGNAL_FUNC func); +#define command_bind_proto(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, a, b, c, d, NULL) +#define command_bind_proto_first(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, a, b, c, d, NULL) +#define command_bind_proto_last(a, b, c, d) command_bind_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, a, b, c, d, NULL) + +void command_unbind_full(const char *cmd, SIGNAL_FUNC func, void *user_data); +#define command_unbind(cmd, func) command_unbind_full(cmd, func, NULL) /* Run subcommand, `cmd' contains the base command, first word in `data' contains the subcommand */ diff --git a/src/core/expandos.c b/src/core/expandos.c index 5eb76410..5cec5561 100644 --- a/src/core/expandos.c +++ b/src/core/expandos.c @@ -179,8 +179,8 @@ void expando_bind(const char *key, int funccount, SIGNAL_FUNC *funcs) func = arg < funccount ? funcs[arg] : NULL; if (func == NULL) func = funcs[EXPANDO_ARG_NONE]; - signal_add_full_id(MODULE_NAME, 1, rec->signal_ids[n], - func, NULL); + signal_add_full_id(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, + rec->signal_ids[n], func, NULL); } } diff --git a/src/fe-text/statusbar.c b/src/fe-text/statusbar.c index ac9a9d7e..5ad86998 100644 --- a/src/fe-text/statusbar.c +++ b/src/fe-text/statusbar.c @@ -860,7 +860,8 @@ static void statusbar_item_default_signals(SBAR_ITEM_REC *item) break; } if (func != NULL) { - signal_add_full_id(MODULE_NAME, 1, + signal_add_full_id(MODULE_NAME, + SIGNAL_PRIORITY_DEFAULT, *pos, func, NULL); } } diff --git a/src/perl/common/Core.xs b/src/perl/common/Core.xs index 20cf32f1..d997bd58 100644 --- a/src/perl/common/Core.xs +++ b/src/perl/common/Core.xs @@ -82,9 +82,10 @@ CODE: if (items != 1 && items != 2) croak("Usage: Irssi::signal_add(signal, func)"); if (items == 2) - perl_signal_add((char *)SvPV(ST(0),PL_na), ST(1)); + perl_signal_add_full((char *)SvPV(ST(0),PL_na), ST(1), + SIGNAL_PRIORITY_DEFAULT); else - perl_signal_add_hash(1, ST(0)); + perl_signal_add_hash(SIGNAL_PRIORITY_DEFAULT, ST(0)); void signal_add_first(...) @@ -92,9 +93,10 @@ CODE: if (items != 1 && items != 2) croak("Usage: Irssi::signal_add_first(signal, func)"); if (items == 2) - perl_signal_add_first((char *)SvPV(ST(0),PL_na), ST(1)); + perl_signal_add_full((char *)SvPV(ST(0),PL_na), ST(1), + SIGNAL_PRIORITY_HIGH); else - perl_signal_add_hash(0, ST(0)); + perl_signal_add_hash(SIGNAL_PRIORITY_HIGH, ST(0)); void signal_add_last(...) @@ -102,9 +104,20 @@ CODE: if (items != 1 && items != 2) croak("Usage: Irssi::signal_add_last(signal, func)"); if (items == 2) - perl_signal_add_last((char *)SvPV(ST(0),PL_na), ST(1)); + perl_signal_add_full((char *)SvPV(ST(0),PL_na), ST(1), + SIGNAL_PRIORITY_LOW); else - perl_signal_add_hash(2, ST(0)); + perl_signal_add_hash(SIGNAL_PRIORITY_LOW, ST(0)); + +void +signal_add_priority(...) +CODE: + if (items != 2 && items != 3) + croak("Usage: Irssi::signal_add_priority(signal, func, priority)"); + if (items == 3) + perl_signal_add_full((char *)SvPV(ST(0),PL_na), ST(1), SvIV(ST(2))); + else + perl_signal_add_hash(SvIV(ST(0)), ST(1)); void signal_remove(signal, func) diff --git a/src/perl/perl-signals.c b/src/perl/perl-signals.c index eced9970..3c423a64 100644 --- a/src/perl/perl-signals.c +++ b/src/perl/perl-signals.c @@ -33,9 +33,7 @@ typedef struct { PERL_SCRIPT_REC *script; int signal_id; char *signal; - SV *func; - int priority; } PERL_SIGNAL_REC; typedef struct { @@ -45,7 +43,7 @@ typedef struct { #include "perl-signals-list.h" -static GHashTable *signals[3]; +static GHashTable *signals; static GHashTable *perl_signal_args_hash; static GSList *perl_signal_args_partial; @@ -208,103 +206,72 @@ static void perl_call_signal(PERL_SCRIPT_REC *script, SV *func, LEAVE; } -static void sig_func(int priority, gconstpointer *args) +static void sig_func(const void *p1, const void *p2, + const void *p3, const void *p4, + const void *p5, const void *p6) { - GSList **list, *tmp, *next; - int signal_id; + PERL_SIGNAL_REC *rec; + const void *args[6]; - signal_id = signal_get_emitted_id(); - list = g_hash_table_lookup(signals[priority], - GINT_TO_POINTER(signal_id)); - for (tmp = list == NULL ? NULL : *list; tmp != NULL; tmp = next) { - PERL_SIGNAL_REC *rec = tmp->data; + args[0] = p1; args[1] = p2; args[2] = p3; + args[3] = p4; args[4] = p5; args[5] = p6; - next = tmp->next; - perl_call_signal(rec->script, rec->func, signal_id, args); - if (signal_is_stopped(signal_id)) - break; - } + rec = signal_get_user_data(); + perl_call_signal(rec->script, rec->func, signal_get_emitted_id(), args); } -#define SIG_FUNC_DECL(priority, priority_name) \ -static void sig_func_##priority_name(gconstpointer p1, gconstpointer p2, \ - gconstpointer p3, gconstpointer p4, \ - gconstpointer p5, gconstpointer p6) \ -{ \ - gconstpointer args[6]; \ - args[0] = p1; args[1] = p2; args[2] = p3; \ - args[3] = p4; args[4] = p5; args[5] = p6; \ - sig_func(priority, args); \ -} - -SIG_FUNC_DECL(0, first); -SIG_FUNC_DECL(1, default); -SIG_FUNC_DECL(2, last); - -#define priority_get_func(priority) \ - (priority == 0 ? sig_func_first : \ - priority == 1 ? sig_func_default : sig_func_last) - -#define perl_signal_get_func(rec) \ - (priority_get_func((rec)->priority)) - -static void perl_signal_add_to_int(const char *signal, SV *func, - int priority, int command) +static void perl_signal_add_full_int(const char *signal, SV *func, + int priority, int command, + const char *category) { PERL_SCRIPT_REC *script; PERL_SIGNAL_REC *rec; - GHashTable *table; GSList **siglist; void *signal_idp; g_return_if_fail(signal != NULL); g_return_if_fail(func != NULL); - g_return_if_fail(priority >= 0 && priority <= 2); script = perl_script_find_package(perl_get_package()); g_return_if_fail(script != NULL); - if (!command && strncmp(signal, "command ", 8) == 0) { - /* we used Irssi::signal_add() instead of - Irssi::command_bind() - oh well, allow this.. */ - command_bind_to(MODULE_NAME, priority, signal+8, -1, - NULL, priority_get_func(priority)); - command = TRUE; - } - rec = g_new(PERL_SIGNAL_REC, 1); rec->script = script; rec->signal_id = signal_get_uniq_id(signal); rec->signal = g_strdup(signal); rec->func = perl_func_sv_inc(func, perl_get_package()); - rec->priority = priority; - table = signals[priority]; + if (command || strncmp(signal, "command ", 8) == 0) { + /* we used Irssi::signal_add() instead of + Irssi::command_bind() - oh well, allow this.. */ + command_bind_full(MODULE_NAME, priority, signal+8, -1, + category, sig_func, rec); + } else { + signal_add_full_id(MODULE_NAME, priority, rec->signal_id, + sig_func, rec); + } + signal_idp = GINT_TO_POINTER(rec->signal_id); - - siglist = g_hash_table_lookup(table, signal_idp); + siglist = g_hash_table_lookup(signals, signal_idp); if (siglist == NULL) { siglist = g_new0(GSList *, 1); - g_hash_table_insert(table, signal_idp, siglist); - - if (!command) { - signal_add_full_id(MODULE_NAME, priority, rec->signal_id, - perl_signal_get_func(rec), NULL); - } + g_hash_table_insert(signals, signal_idp, siglist); } *siglist = g_slist_append(*siglist, rec); } -void perl_signal_add_to(const char *signal, SV *func, int priority) +void perl_signal_add_full(const char *signal, SV *func, int priority) { - perl_signal_add_to_int(signal, func, priority, FALSE); + perl_signal_add_full_int(signal, func, priority, FALSE, NULL); } static void perl_signal_destroy(PERL_SIGNAL_REC *rec) { if (strncmp(rec->signal, "command ", 8) == 0) - command_unbind(rec->signal+8, perl_signal_get_func(rec)); + command_unbind_full(rec->signal+8, sig_func, rec); + else + signal_remove_id(rec->signal_id, sig_func, rec); SvREFCNT_dec(rec->func); g_free(rec->signal); @@ -313,17 +280,10 @@ static void perl_signal_destroy(PERL_SIGNAL_REC *rec) static void perl_signal_remove_list_one(GSList **siglist, PERL_SIGNAL_REC *rec) { - void *signal_idp; - - g_return_if_fail(rec != NULL); - - signal_idp = GINT_TO_POINTER(rec->signal_id); - *siglist = g_slist_remove(*siglist, rec); if (*siglist == NULL) { - signal_remove_id(rec->signal_id, perl_signal_get_func(rec), NULL); g_free(siglist); - g_hash_table_remove(signals[rec->priority], signal_idp); + g_hash_table_remove(signals, GINT_TO_POINTER(rec->signal_id)); } perl_signal_destroy(rec); @@ -337,8 +297,6 @@ static void perl_signal_remove_list(GSList **list, SV *func) { GSList *tmp; - g_return_if_fail(list != NULL); - for (tmp = *list; tmp != NULL; tmp = tmp->next) { PERL_SIGNAL_REC *rec = tmp->data; @@ -353,17 +311,15 @@ void perl_signal_remove(const char *signal, SV *func) { GSList **list; void *signal_idp; - int n; signal_idp = GINT_TO_POINTER(signal_get_uniq_id(signal)); + list = g_hash_table_lookup(signals, signal_idp); - func = perl_func_sv_inc(func, perl_get_package()); - for (n = 0; n < sizeof(signals)/sizeof(signals[0]); n++) { - list = g_hash_table_lookup(signals[n], signal_idp); - if (list != NULL) - perl_signal_remove_list(list, func); + if (list != NULL) { + func = perl_func_sv_inc(func, perl_get_package()); + perl_signal_remove_list(list, func); + SvREFCNT_dec(func); } - SvREFCNT_dec(func); } void perl_command_bind_to(const char *cmd, const char *category, @@ -371,11 +327,8 @@ void perl_command_bind_to(const char *cmd, const char *category, { char *signal; - command_bind_to(MODULE_NAME, priority, cmd, -1, - category, priority_get_func(priority)); - signal = g_strconcat("command ", cmd, NULL); - perl_signal_add_to_int(signal, func, priority, TRUE); + perl_signal_add_full_int(signal, func, priority, TRUE, category); g_free(signal); } @@ -405,11 +358,6 @@ static int signal_destroy_hash(void *key, GSList **list, PERL_SCRIPT_REC *script next = tmp->next; if (script == NULL || rec->script == script) { *list = g_slist_remove(*list, rec); - if (*list == NULL) { - signal_remove_id(rec->signal_id, - perl_signal_get_func(rec), - NULL); - } perl_signal_destroy(rec); } } @@ -424,35 +372,20 @@ static int signal_destroy_hash(void *key, GSList **list, PERL_SCRIPT_REC *script /* destroy all signals used by script */ void perl_signal_remove_script(PERL_SCRIPT_REC *script) { - int n; - - for (n = 0; n < sizeof(signals)/sizeof(signals[0]); n++) { - g_hash_table_foreach_remove(signals[n], - (GHRFunc) signal_destroy_hash, - script); - } + g_hash_table_foreach_remove(signals, (GHRFunc) signal_destroy_hash, + script); } void perl_signals_start(void) { - int n; - - for (n = 0; n < sizeof(signals)/sizeof(signals[0]); n++) { - signals[n] = g_hash_table_new((GHashFunc) g_direct_hash, - (GCompareFunc) g_direct_equal); - } + signals = g_hash_table_new(NULL, NULL); } void perl_signals_stop(void) { - int n; - - for (n = 0; n < sizeof(signals)/sizeof(signals[0]); n++) { - g_hash_table_foreach(signals[n], - (GHFunc) signal_destroy_hash, NULL); - g_hash_table_destroy(signals[n]); - signals[n] = NULL; - } + g_hash_table_foreach(signals, (GHFunc) signal_destroy_hash, NULL); + g_hash_table_destroy(signals); + signals = NULL; } void perl_signals_init(void) diff --git a/src/perl/perl-signals.h b/src/perl/perl-signals.h index 33e0c1b8..92196d04 100644 --- a/src/perl/perl-signals.h +++ b/src/perl/perl-signals.h @@ -1,13 +1,7 @@ #ifndef __PERL_SIGNALS_H #define __PERL_SIGNALS_H -void perl_signal_add_to(const char *signal, SV *func, int priority); -#define perl_signal_add_first(signal, func) \ - perl_signal_add_to(signal, func, 0) -#define perl_signal_add(signal, func) \ - perl_signal_add_to(signal, func, 1) -#define perl_signal_add_last(signal, func) \ - perl_signal_add_to(signal, func, 2) +void perl_signal_add_full(const char *signal, SV *func, int priority); void perl_signal_remove(const char *signal, SV *func); /* remove all signals used by script */