2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
signals.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../common.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "modules.h"
|
|
|
|
|
|
|
|
#define SIGNAL_LISTS 3
|
|
|
|
|
|
|
|
typedef struct {
|
2001-01-03 02:34:12 -05:00
|
|
|
int id; /* signal id */
|
2001-08-13 20:03:02 -04:00
|
|
|
int refcount;
|
2001-01-03 02:34:12 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
int emitting; /* signal is being emitted */
|
|
|
|
int stop_emit; /* this signal was stopped */
|
|
|
|
|
|
|
|
GPtrArray *modulelist[SIGNAL_LISTS]; /* list of what signals belong
|
|
|
|
to which module */
|
|
|
|
GPtrArray *siglist[SIGNAL_LISTS]; /* signal lists */
|
|
|
|
} SIGNAL_REC;
|
|
|
|
|
|
|
|
#define signal_is_emitlist_empty(a) \
|
|
|
|
(!(a)->siglist[0] && !(a)->siglist[1] && !(a)->siglist[2])
|
|
|
|
|
|
|
|
static GMemChunk *signals_chunk;
|
|
|
|
static GHashTable *signals;
|
|
|
|
static SIGNAL_REC *current_emitted_signal;
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
#define signal_ref(signal) ++(signal)->refcount
|
|
|
|
#define signal_unref(rec) (signal_unref_full(rec, TRUE))
|
|
|
|
|
|
|
|
static int signal_unref_full(SIGNAL_REC *rec, int remove_hash)
|
|
|
|
{
|
|
|
|
if (rec->refcount == 0) {
|
|
|
|
g_error("signal_unref(%s) : BUG - reference counter == 0",
|
|
|
|
signal_get_id_str(rec->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--rec->refcount != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* remove whole signal from memory */
|
|
|
|
if (!signal_is_emitlist_empty(rec)) {
|
|
|
|
g_error("signal_unref(%s) : BUG - emitlist wasn't empty",
|
|
|
|
signal_get_id_str(rec->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_hash)
|
|
|
|
g_hash_table_remove(signals, GINT_TO_POINTER(rec->id));
|
|
|
|
g_mem_chunk_free(signals_chunk, rec);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_unref_count(SIGNAL_REC *rec, int count)
|
|
|
|
{
|
|
|
|
while (count-- > 0)
|
|
|
|
signal_unref(rec);
|
|
|
|
}
|
|
|
|
|
2000-12-04 18:53:28 -05:00
|
|
|
void signal_add_to(const char *module, int pos,
|
|
|
|
const char *signal, SIGNAL_FUNC func)
|
|
|
|
{
|
|
|
|
g_return_if_fail(signal != NULL);
|
|
|
|
|
|
|
|
signal_add_to_id(module, pos, signal_get_uniq_id(signal), func);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* bind a signal */
|
2000-12-04 18:53:28 -05:00
|
|
|
void signal_add_to_id(const char *module, int pos,
|
|
|
|
int signal_id, SIGNAL_FUNC func)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
|
|
|
|
2000-12-04 18:53:28 -05:00
|
|
|
g_return_if_fail(signal_id >= 0);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_if_fail(func != NULL);
|
|
|
|
g_return_if_fail(pos >= 0 && pos < SIGNAL_LISTS);
|
|
|
|
|
|
|
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
|
|
|
if (rec == NULL) {
|
|
|
|
rec = g_mem_chunk_alloc0(signals_chunk);
|
2001-08-13 20:03:02 -04:00
|
|
|
rec->id = signal_id;
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_insert(signals, GINT_TO_POINTER(signal_id), rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rec->siglist[pos] == NULL) {
|
|
|
|
rec->siglist[pos] = g_ptr_array_new();
|
|
|
|
rec->modulelist[pos] = g_ptr_array_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
g_ptr_array_add(rec->siglist[pos], (void *) func);
|
|
|
|
g_ptr_array_add(rec->modulelist[pos], (void *) module);
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
signal_ref(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int signal_list_find(GPtrArray *array, void *data)
|
|
|
|
{
|
2000-07-10 19:00:56 -04:00
|
|
|
unsigned int n;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
for (n = 0; n < array->len; n++) {
|
|
|
|
if (g_ptr_array_index(array, n) == data)
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-07-29 08:56:37 -04:00
|
|
|
static void signal_list_free(SIGNAL_REC *rec, int list)
|
|
|
|
{
|
|
|
|
g_ptr_array_free(rec->siglist[list], TRUE);
|
|
|
|
g_ptr_array_free(rec->modulelist[list], TRUE);
|
|
|
|
rec->siglist[list] = NULL;
|
|
|
|
rec->modulelist[list] = NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
/* Returns TRUE if the whole signal is removed after this remove */
|
2001-07-29 08:56:37 -04:00
|
|
|
static void signal_remove_from_list(SIGNAL_REC *rec, int list, int index)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-08-13 20:03:02 -04:00
|
|
|
g_ptr_array_remove_index(rec->siglist[list], index);
|
|
|
|
g_ptr_array_remove_index(rec->modulelist[list], index);
|
|
|
|
|
|
|
|
if (rec->siglist[list]->len == 0)
|
|
|
|
signal_list_free(rec, list);
|
|
|
|
|
|
|
|
signal_unref(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove signal from emit lists */
|
2001-07-29 08:56:37 -04:00
|
|
|
static int signal_remove_from_lists(SIGNAL_REC *rec, SIGNAL_FUNC func)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
int n, index;
|
|
|
|
|
|
|
|
for (n = 0; n < SIGNAL_LISTS; n++) {
|
|
|
|
if (rec->siglist[n] == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
index = signal_list_find(rec->siglist[n], (void *) func);
|
|
|
|
if (index != -1) {
|
|
|
|
/* remove the function from emit list */
|
2001-07-29 08:56:37 -04:00
|
|
|
signal_remove_from_list(rec, n, index);
|
2000-04-26 04:03:38 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
void signal_remove_id(int signal_id, SIGNAL_FUNC func)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
g_return_if_fail(signal_id >= 0);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_if_fail(func != NULL);
|
|
|
|
|
|
|
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
2000-12-17 02:39:22 -05:00
|
|
|
if (rec != NULL)
|
2001-07-29 08:56:37 -04:00
|
|
|
signal_remove_from_lists(rec, func);
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
/* unbind signal */
|
|
|
|
void signal_remove(const char *signal, SIGNAL_FUNC func)
|
|
|
|
{
|
|
|
|
g_return_if_fail(signal != NULL);
|
|
|
|
|
|
|
|
signal_remove_id(signal_get_uniq_id(signal), func);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
static int signal_emit_real(SIGNAL_REC *rec, int params, va_list va)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-08-13 20:03:02 -04:00
|
|
|
gconstpointer arglist[SIGNAL_MAX_ARGUMENTS];
|
2000-04-26 04:03:38 -04:00
|
|
|
SIGNAL_REC *prev_emitted_signal;
|
|
|
|
SIGNAL_FUNC func;
|
2000-10-26 21:18:16 -04:00
|
|
|
int n, index, stopped, stop_emit_count;
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
for (n = 0; n < SIGNAL_MAX_ARGUMENTS; n++)
|
|
|
|
arglist[n] = n >= params ? NULL : va_arg(va, gconstpointer);
|
|
|
|
|
2000-10-26 21:18:16 -04:00
|
|
|
/* signal_stop_by_name("signal"); signal_emit("signal", ...);
|
|
|
|
fails if we compare rec->stop_emit against 0. */
|
|
|
|
stop_emit_count = rec->stop_emit;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
signal_ref(rec);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
stopped = FALSE;
|
|
|
|
rec->emitting++;
|
|
|
|
for (n = 0; n < SIGNAL_LISTS; n++) {
|
|
|
|
/* run signals in emit lists */
|
|
|
|
if (rec->siglist[n] == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (index = rec->siglist[n]->len-1; index >= 0; index--) {
|
|
|
|
func = (SIGNAL_FUNC) g_ptr_array_index(rec->siglist[n], index);
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
prev_emitted_signal = current_emitted_signal;
|
|
|
|
current_emitted_signal = rec;
|
2001-01-03 02:34:12 -05:00
|
|
|
#if SIGNAL_MAX_ARGUMENTS != 6
|
2001-08-13 20:03:02 -04:00
|
|
|
# error SIGNAL_MAX_ARGUMENTS changed - update code
|
2001-01-03 02:34:12 -05:00
|
|
|
#endif
|
2001-08-13 20:03:02 -04:00
|
|
|
func(arglist[0], arglist[1], arglist[2], arglist[3], arglist[4], arglist[5]);
|
|
|
|
current_emitted_signal = prev_emitted_signal;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-10-26 21:18:16 -04:00
|
|
|
if (rec->stop_emit != stop_emit_count) {
|
2000-04-26 04:03:38 -04:00
|
|
|
stopped = TRUE;
|
|
|
|
rec->stop_emit--;
|
|
|
|
n = SIGNAL_LISTS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rec->emitting--;
|
|
|
|
|
2000-10-26 21:18:16 -04:00
|
|
|
if (!rec->emitting) {
|
|
|
|
if (rec->stop_emit != 0) {
|
|
|
|
/* signal_stop() used too many times */
|
|
|
|
rec->stop_emit = 0;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
signal_unref(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
return stopped;
|
|
|
|
}
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
int signal_emit(const char *signal, int params, ...)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
2001-08-13 20:03:02 -04:00
|
|
|
va_list va;
|
|
|
|
int signal_id;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-03 02:34:12 -05:00
|
|
|
g_return_val_if_fail(params >= 0 && params <= SIGNAL_MAX_ARGUMENTS, FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
signal_id = signal_get_uniq_id(signal);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
2001-08-13 20:03:02 -04:00
|
|
|
if (rec != NULL) {
|
|
|
|
va_start(va, params);
|
|
|
|
signal_emit_real(rec, params, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
return rec != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int signal_emit_id(int signal_id, int params, ...)
|
|
|
|
{
|
2001-08-13 20:03:02 -04:00
|
|
|
SIGNAL_REC *rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
va_list va;
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
g_return_val_if_fail(signal_id >= 0, FALSE);
|
|
|
|
g_return_val_if_fail(params >= 0 && params <= SIGNAL_MAX_ARGUMENTS, FALSE);
|
|
|
|
|
|
|
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
|
|
|
if (rec != NULL) {
|
|
|
|
va_start(va, params);
|
|
|
|
signal_emit_real(rec, params, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
return rec != NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop the current ongoing signal emission */
|
|
|
|
void signal_stop(void)
|
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
|
|
|
|
|
|
|
rec = current_emitted_signal;
|
2001-01-04 12:27:21 -05:00
|
|
|
if (rec == NULL)
|
2000-04-26 04:03:38 -04:00
|
|
|
g_warning("signal_stop() : no signals are being emitted currently");
|
2001-01-04 12:27:21 -05:00
|
|
|
else if (rec->emitting > rec->stop_emit)
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->stop_emit++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop ongoing signal emission by signal name */
|
|
|
|
void signal_stop_by_name(const char *signal)
|
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
2000-05-29 13:10:22 -04:00
|
|
|
int signal_id;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-25 19:39:23 -04:00
|
|
|
signal_id = signal_get_uniq_id(signal);
|
2000-05-29 13:10:22 -04:00
|
|
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
2000-04-26 04:03:38 -04:00
|
|
|
if (rec == NULL)
|
|
|
|
g_warning("signal_stop_by_name() : unknown signal \"%s\"", signal);
|
2001-01-04 12:27:21 -05:00
|
|
|
else if (rec->emitting > rec->stop_emit)
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->stop_emit++;
|
|
|
|
}
|
|
|
|
|
2001-01-03 02:34:12 -05:00
|
|
|
/* return the name of the signal that is currently being emitted */
|
|
|
|
const char *signal_get_emitted(void)
|
|
|
|
{
|
|
|
|
return signal_get_id_str(signal_get_emitted_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the ID of the signal that is currently being emitted */
|
|
|
|
int signal_get_emitted_id(void)
|
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
|
|
|
|
|
|
|
rec = current_emitted_signal;
|
|
|
|
g_return_val_if_fail(rec != NULL, -1);
|
|
|
|
return rec->id;
|
|
|
|
}
|
|
|
|
|
2001-01-07 03:23:06 -05:00
|
|
|
/* return TRUE if specified signal was stopped */
|
|
|
|
int signal_is_stopped(int signal_id)
|
|
|
|
{
|
|
|
|
SIGNAL_REC *rec;
|
|
|
|
|
|
|
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
|
|
|
g_return_val_if_fail(rec != NULL, FALSE);
|
|
|
|
|
|
|
|
return rec->emitting <= rec->stop_emit;
|
|
|
|
}
|
|
|
|
|
2001-01-03 02:34:12 -05:00
|
|
|
static void signal_remove_module(void *signal, SIGNAL_REC *rec,
|
|
|
|
const char *module)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-08-13 20:03:02 -04:00
|
|
|
unsigned int index;
|
2001-07-29 08:56:37 -04:00
|
|
|
int list;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
for (list = 0; list < SIGNAL_LISTS; list++) {
|
2000-09-27 19:46:20 -04:00
|
|
|
if (rec->modulelist[list] == NULL)
|
|
|
|
continue;
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
for (index = rec->modulelist[list]->len; index > 0; index--)
|
|
|
|
if (g_strcasecmp(g_ptr_array_index(rec->modulelist[list], index-1), module) == 0)
|
|
|
|
signal_remove_from_list(rec, list, index-1);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
static void signal_foreach_ref(void *signal, SIGNAL_REC *rec)
|
|
|
|
{
|
|
|
|
signal_ref(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int signal_foreach_unref(void *signal, SIGNAL_REC *rec)
|
|
|
|
{
|
|
|
|
return signal_unref_full(rec, FALSE);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* remove all signals that belong to `module' */
|
|
|
|
void signals_remove_module(const char *module)
|
|
|
|
{
|
|
|
|
g_return_if_fail(module != NULL);
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
g_hash_table_foreach(signals, (GHFunc) signal_foreach_ref, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_hash_table_foreach(signals, (GHFunc) signal_remove_module, (void *) module);
|
2001-08-13 20:03:02 -04:00
|
|
|
g_hash_table_foreach_remove(signals, (GHRFunc) signal_foreach_unref, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void signals_init(void)
|
|
|
|
{
|
|
|
|
signals_chunk = g_mem_chunk_new("signals", sizeof(SIGNAL_REC),
|
|
|
|
sizeof(SIGNAL_REC)*200, G_ALLOC_AND_FREE);
|
|
|
|
signals = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_free(void *key, SIGNAL_REC *rec)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
signal_ref(rec);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
for (n = 0; n < SIGNAL_LISTS; n++) {
|
|
|
|
if (rec->siglist[n] != NULL) {
|
2001-08-13 20:03:02 -04:00
|
|
|
signal_unref_count(rec, rec->siglist[n]->len);
|
|
|
|
signal_list_free(rec, n);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-13 20:03:02 -04:00
|
|
|
if (!signal_unref_full(rec, FALSE)) {
|
|
|
|
g_error("signal_free(%s) : BUG - signal still has %d references",
|
|
|
|
signal_get_id_str(rec->id), rec->refcount);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void signals_deinit(void)
|
|
|
|
{
|
|
|
|
g_hash_table_foreach(signals, (GHFunc) signal_free, NULL);
|
|
|
|
g_hash_table_destroy(signals);
|
|
|
|
|
|
|
|
module_uniq_destroy("signals");
|
|
|
|
g_mem_chunk_destroy(signals_chunk);
|
|
|
|
}
|