mirror of
https://github.com/irssi/irssi.git
synced 2024-12-04 14:46:39 -05:00
Rewritten signal handling code - it wasn't supposed to come before 0.9 irssi
but it doesn't break much things and is needed for Qt port :) git-svn-id: http://svn.irssi.org/repos/irssi/trunk@2682 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
parent
15667ec7ff
commit
8b9c4823bf
@ -154,7 +154,7 @@ void command_bind_to(const char *module, int pos, const char *cmd,
|
|||||||
|
|
||||||
if (func != NULL) {
|
if (func != NULL) {
|
||||||
str = g_strconcat("command ", cmd, NULL);
|
str = g_strconcat("command ", cmd, NULL);
|
||||||
signal_add_to(module, pos, str, func);
|
signal_add_full(module, pos, str, func, NULL);
|
||||||
g_free(str);
|
g_free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +179,8 @@ void expando_bind(const char *key, int funccount, SIGNAL_FUNC *funcs)
|
|||||||
func = arg < funccount ? funcs[arg] : NULL;
|
func = arg < funccount ? funcs[arg] : NULL;
|
||||||
if (func == NULL) func = funcs[EXPANDO_ARG_NONE];
|
if (func == NULL) func = funcs[EXPANDO_ARG_NONE];
|
||||||
|
|
||||||
signal_add_to_id(MODULE_NAME, 1, rec->signal_ids[n], func);
|
signal_add_full_id(MODULE_NAME, 1, rec->signal_ids[n],
|
||||||
|
func, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,7 +209,7 @@ void expando_unbind(const char *key, int funccount, SIGNAL_FUNC *funcs)
|
|||||||
func = arg < funccount ? funcs[arg] : NULL;
|
func = arg < funccount ? funcs[arg] : NULL;
|
||||||
if (func == NULL) func = funcs[EXPANDO_ARG_NONE];
|
if (func == NULL) func = funcs[EXPANDO_ARG_NONE];
|
||||||
|
|
||||||
signal_remove_id(rec->signal_ids[n], func);
|
signal_remove_id(rec->signal_ids[n], func, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
signals.c : irssi
|
signals.c : irssi
|
||||||
|
|
||||||
Copyright (C) 1999 Timo Sirainen
|
Copyright (C) 1999-2002 Timo Sirainen
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -18,179 +18,205 @@
|
|||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../common.h"
|
#include "module.h"
|
||||||
#include "signals.h"
|
#include "signals.h"
|
||||||
#include "modules.h"
|
#include "modules.h"
|
||||||
|
|
||||||
#define SIGNAL_LISTS 3
|
#define SIGNAL_LISTS 3
|
||||||
|
|
||||||
|
typedef struct _SignalHook {
|
||||||
|
struct _SignalHook *next;
|
||||||
|
|
||||||
|
int priority;
|
||||||
|
const char *module;
|
||||||
|
SIGNAL_FUNC func;
|
||||||
|
void *user_data;
|
||||||
|
} SignalHook;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int id; /* signal id */
|
int id; /* signal id */
|
||||||
int refcount;
|
int refcount;
|
||||||
|
|
||||||
int emitting; /* signal is being emitted */
|
int emitting; /* signal is being emitted */
|
||||||
int stop_emit; /* this signal was stopped */
|
int stop_emit; /* this signal was stopped */
|
||||||
|
int remove_count; /* hooks were removed from signal */
|
||||||
|
|
||||||
GPtrArray *modulelist[SIGNAL_LISTS]; /* list of what signals belong
|
SignalHook *hooks;
|
||||||
to which module */
|
} Signal;
|
||||||
GPtrArray *siglist[SIGNAL_LISTS]; /* signal lists */
|
|
||||||
} SIGNAL_REC;
|
|
||||||
|
|
||||||
#define signal_is_emitlist_empty(a) \
|
#define signal_is_emitlist_empty(a) \
|
||||||
(!(a)->siglist[0] && !(a)->siglist[1] && !(a)->siglist[2])
|
(!(a)->siglist[0] && !(a)->siglist[1] && !(a)->siglist[2])
|
||||||
|
|
||||||
static GMemChunk *signals_chunk;
|
void *signal_user_data;
|
||||||
|
|
||||||
static GHashTable *signals;
|
static GHashTable *signals;
|
||||||
static SIGNAL_REC *current_emitted_signal;
|
static Signal *current_emitted_signal;
|
||||||
|
|
||||||
#define signal_ref(signal) ++(signal)->refcount
|
#define signal_ref(signal) ++(signal)->refcount
|
||||||
#define signal_unref(rec) (signal_unref_full(rec, TRUE))
|
#define signal_unref(signal) (signal_unref_full(signal, TRUE))
|
||||||
|
|
||||||
static int signal_unref_full(SIGNAL_REC *rec, int remove_hash)
|
static int signal_unref_full(Signal *rec, int remove)
|
||||||
{
|
{
|
||||||
if (rec->refcount == 0) {
|
g_assert(rec->refcount > 0);
|
||||||
g_error("signal_unref(%s) : BUG - reference counter == 0",
|
|
||||||
signal_get_id_str(rec->id));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (--rec->refcount != 0)
|
if (--rec->refcount != 0)
|
||||||
return FALSE;
|
return TRUE;
|
||||||
|
|
||||||
/* remove whole signal from memory */
|
/* remove whole signal from memory */
|
||||||
if (!signal_is_emitlist_empty(rec)) {
|
if (rec->hooks != NULL) {
|
||||||
g_error("signal_unref(%s) : BUG - emitlist wasn't empty",
|
g_error("signal_unref(%s) : BUG - hook list wasn't empty",
|
||||||
signal_get_id_str(rec->id));
|
signal_get_id_str(rec->id));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remove_hash)
|
if (remove)
|
||||||
g_hash_table_remove(signals, GINT_TO_POINTER(rec->id));
|
g_hash_table_remove(signals, GINT_TO_POINTER(rec->id));
|
||||||
g_mem_chunk_free(signals_chunk, rec);
|
g_free(rec);
|
||||||
return TRUE;
|
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void signal_unref_count(SIGNAL_REC *rec, int count)
|
static void signal_hash_ref(void *key, Signal *rec)
|
||||||
{
|
{
|
||||||
while (count-- > 0)
|
signal_ref(rec);
|
||||||
signal_unref(rec);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void signal_add_to(const char *module, int pos,
|
static int signal_hash_unref(void *key, Signal *rec)
|
||||||
const char *signal, SIGNAL_FUNC func)
|
|
||||||
{
|
{
|
||||||
g_return_if_fail(signal != NULL);
|
return !signal_unref_full(rec, FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
signal_add_to_id(module, pos, signal_get_uniq_id(signal), func);
|
void signal_add_full(const char *module, int priority,
|
||||||
|
const char *signal, SIGNAL_FUNC func, void *user_data)
|
||||||
|
{
|
||||||
|
signal_add_full_id(module, priority, signal_get_uniq_id(signal),
|
||||||
|
func, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bind a signal */
|
/* bind a signal */
|
||||||
void signal_add_to_id(const char *module, int pos,
|
void signal_add_full_id(const char *module, int priority,
|
||||||
int signal_id, SIGNAL_FUNC func)
|
int signal_id, SIGNAL_FUNC func, void *user_data)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *signal;
|
||||||
|
SignalHook *hook, **tmp;
|
||||||
|
|
||||||
g_return_if_fail(signal_id >= 0);
|
g_return_if_fail(signal_id >= 0);
|
||||||
g_return_if_fail(func != NULL);
|
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));
|
signal = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
||||||
if (rec == NULL) {
|
if (signal == NULL) {
|
||||||
rec = g_mem_chunk_alloc0(signals_chunk);
|
/* new signal */
|
||||||
rec->id = signal_id;
|
signal = g_new0(Signal, 1);
|
||||||
g_hash_table_insert(signals, GINT_TO_POINTER(signal_id), rec);
|
signal->id = signal_id;
|
||||||
|
g_hash_table_insert(signals, GINT_TO_POINTER(signal_id), signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rec->siglist[pos] == NULL) {
|
hook = g_new0(SignalHook, 1);
|
||||||
rec->siglist[pos] = g_ptr_array_new();
|
hook->priority = priority;
|
||||||
rec->modulelist[pos] = g_ptr_array_new();
|
hook->module = module;
|
||||||
|
hook->func = func;
|
||||||
|
hook->user_data = user_data;
|
||||||
|
|
||||||
|
/* insert signal to proper position in list */
|
||||||
|
for (tmp = &signal->hooks; ; tmp = &(*tmp)->next) {
|
||||||
|
if (*tmp == NULL) {
|
||||||
|
/* last in list */
|
||||||
|
*tmp = hook;
|
||||||
|
break;
|
||||||
|
} else if (priority <= (*tmp)->priority) {
|
||||||
|
/* insert before others with same priority */
|
||||||
|
hook->next = *tmp;
|
||||||
|
*tmp = hook;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g_ptr_array_add(rec->siglist[pos], (void *) func);
|
signal_ref(signal);
|
||||||
g_ptr_array_add(rec->modulelist[pos], (void *) module);
|
|
||||||
|
|
||||||
signal_ref(rec);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int signal_list_find(GPtrArray *array, void *data)
|
static void signal_remove_hook(Signal *rec, SignalHook **hook_pos)
|
||||||
{
|
{
|
||||||
unsigned int n;
|
SignalHook *hook;
|
||||||
|
|
||||||
for (n = 0; n < array->len; n++) {
|
hook = *hook_pos;
|
||||||
if (g_ptr_array_index(array, n) == data)
|
*hook_pos = hook->next;
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
g_free(hook);
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns TRUE if the whole signal is removed after this remove */
|
|
||||||
static void signal_remove_from_list(SIGNAL_REC *rec, int list, int index)
|
|
||||||
{
|
|
||||||
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);
|
signal_unref(rec);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove signal from emit lists */
|
/* Remove function from signal's emit list */
|
||||||
static int signal_remove_from_lists(SIGNAL_REC *rec, SIGNAL_FUNC func)
|
static int signal_remove_func(Signal *rec, SIGNAL_FUNC func, void *user_data)
|
||||||
{
|
{
|
||||||
int n, index;
|
SignalHook **hook;
|
||||||
|
|
||||||
for (n = 0; n < SIGNAL_LISTS; n++) {
|
for (hook = &rec->hooks; *hook != NULL; hook = &(*hook)->next) {
|
||||||
if (rec->siglist[n] == NULL)
|
if ((*hook)->func == func && (*hook)->user_data == user_data) {
|
||||||
continue;
|
if (rec->emitting) {
|
||||||
|
/* mark it removed after emitting is done */
|
||||||
index = signal_list_find(rec->siglist[n], (void *) func);
|
(*hook)->func = NULL;
|
||||||
if (index != -1) {
|
rec->remove_count++;
|
||||||
/* remove the function from emit list */
|
} else {
|
||||||
signal_remove_from_list(rec, n, index);
|
/* remove the function from emit list */
|
||||||
return 1;
|
signal_remove_hook(rec, hook);
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void signal_remove_id(int signal_id, SIGNAL_FUNC func)
|
void signal_remove_id(int signal_id, SIGNAL_FUNC func, void *user_data)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
|
|
||||||
g_return_if_fail(signal_id >= 0);
|
g_return_if_fail(signal_id >= 0);
|
||||||
g_return_if_fail(func != NULL);
|
g_return_if_fail(func != NULL);
|
||||||
|
|
||||||
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
||||||
if (rec != NULL)
|
if (rec != NULL)
|
||||||
signal_remove_from_lists(rec, func);
|
signal_remove_func(rec, func, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* unbind signal */
|
/* unbind signal */
|
||||||
void signal_remove(const char *signal, SIGNAL_FUNC func)
|
void signal_remove_full(const char *signal, SIGNAL_FUNC func, void *user_data)
|
||||||
{
|
{
|
||||||
g_return_if_fail(signal != NULL);
|
g_return_if_fail(signal != NULL);
|
||||||
|
|
||||||
signal_remove_id(signal_get_uniq_id(signal), func);
|
signal_remove_id(signal_get_uniq_id(signal), func, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int signal_emit_real(SIGNAL_REC *rec, int params, va_list va)
|
static void signal_hooks_clean(Signal *rec)
|
||||||
{
|
{
|
||||||
gconstpointer arglist[SIGNAL_MAX_ARGUMENTS];
|
SignalHook **hook, **next;
|
||||||
SIGNAL_REC *prev_emitted_signal;
|
int count;
|
||||||
SIGNAL_FUNC func;
|
|
||||||
int n, index, stopped, stop_emit_count;
|
|
||||||
|
|
||||||
for (n = 0; n < SIGNAL_MAX_ARGUMENTS; n++)
|
count = rec->remove_count;
|
||||||
arglist[n] = n >= params ? NULL : va_arg(va, gconstpointer);
|
rec->remove_count = 0;
|
||||||
|
|
||||||
|
for (hook = &rec->hooks; *hook != NULL; hook = next) {
|
||||||
|
next = &(*hook)->next;
|
||||||
|
|
||||||
|
if ((*hook)->func == NULL) {
|
||||||
|
next = hook;
|
||||||
|
signal_remove_hook(rec, hook);
|
||||||
|
|
||||||
|
if (--count == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int signal_emit_real(Signal *rec, int params, va_list va)
|
||||||
|
{
|
||||||
|
const void *arglist[SIGNAL_MAX_ARGUMENTS];
|
||||||
|
Signal *prev_emitted_signal;
|
||||||
|
SignalHook *hook;
|
||||||
|
int i, stopped, stop_emit_count;
|
||||||
|
|
||||||
|
for (i = 0; i < SIGNAL_MAX_ARGUMENTS; i++)
|
||||||
|
arglist[i] = i >= params ? NULL : va_arg(va, const void *);
|
||||||
|
|
||||||
/* signal_stop_by_name("signal"); signal_emit("signal", ...);
|
/* signal_stop_by_name("signal"); signal_emit("signal", ...);
|
||||||
fails if we compare rec->stop_emit against 0. */
|
fails if we compare rec->stop_emit against 0. */
|
||||||
@ -200,37 +226,39 @@ static int signal_emit_real(SIGNAL_REC *rec, int params, va_list va)
|
|||||||
|
|
||||||
stopped = FALSE;
|
stopped = FALSE;
|
||||||
rec->emitting++;
|
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--) {
|
for (hook = rec->hooks; hook != NULL; hook = hook->next) {
|
||||||
func = (SIGNAL_FUNC) g_ptr_array_index(rec->siglist[n], index);
|
if (hook->func == NULL)
|
||||||
|
continue; /* removed */
|
||||||
|
|
||||||
prev_emitted_signal = current_emitted_signal;
|
prev_emitted_signal = current_emitted_signal;
|
||||||
current_emitted_signal = rec;
|
current_emitted_signal = rec;
|
||||||
#if SIGNAL_MAX_ARGUMENTS != 6
|
#if SIGNAL_MAX_ARGUMENTS != 6
|
||||||
# error SIGNAL_MAX_ARGUMENTS changed - update code
|
# error SIGNAL_MAX_ARGUMENTS changed - update code
|
||||||
#endif
|
#endif
|
||||||
func(arglist[0], arglist[1], arglist[2], arglist[3], arglist[4], arglist[5]);
|
signal_user_data = hook->user_data;
|
||||||
current_emitted_signal = prev_emitted_signal;
|
hook->func(arglist[0], arglist[1], arglist[2], arglist[3],
|
||||||
|
arglist[4], arglist[5]);
|
||||||
|
|
||||||
if (rec->stop_emit != stop_emit_count) {
|
current_emitted_signal = prev_emitted_signal;
|
||||||
stopped = TRUE;
|
|
||||||
rec->stop_emit--;
|
if (rec->stop_emit != stop_emit_count) {
|
||||||
n = SIGNAL_LISTS;
|
stopped = TRUE;
|
||||||
break;
|
rec->stop_emit--;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rec->emitting--;
|
rec->emitting--;
|
||||||
|
signal_user_data = NULL;
|
||||||
|
|
||||||
if (!rec->emitting) {
|
if (!rec->emitting) {
|
||||||
if (rec->stop_emit != 0) {
|
if (rec->stop_emit != 0) {
|
||||||
/* signal_stop() used too many times */
|
/* signal_stop() used too many times */
|
||||||
rec->stop_emit = 0;
|
rec->stop_emit = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rec->remove_count > 0)
|
||||||
|
signal_hooks_clean(rec);
|
||||||
}
|
}
|
||||||
|
|
||||||
signal_unref(rec);
|
signal_unref(rec);
|
||||||
@ -239,7 +267,7 @@ static int signal_emit_real(SIGNAL_REC *rec, int params, va_list va)
|
|||||||
|
|
||||||
int signal_emit(const char *signal, int params, ...)
|
int signal_emit(const char *signal, int params, ...)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
va_list va;
|
va_list va;
|
||||||
int signal_id;
|
int signal_id;
|
||||||
|
|
||||||
@ -259,7 +287,7 @@ int signal_emit(const char *signal, int params, ...)
|
|||||||
|
|
||||||
int signal_emit_id(int signal_id, int params, ...)
|
int signal_emit_id(int signal_id, int params, ...)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|
||||||
g_return_val_if_fail(signal_id >= 0, FALSE);
|
g_return_val_if_fail(signal_id >= 0, FALSE);
|
||||||
@ -278,7 +306,7 @@ int signal_emit_id(int signal_id, int params, ...)
|
|||||||
/* stop the current ongoing signal emission */
|
/* stop the current ongoing signal emission */
|
||||||
void signal_stop(void)
|
void signal_stop(void)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
|
|
||||||
rec = current_emitted_signal;
|
rec = current_emitted_signal;
|
||||||
if (rec == NULL)
|
if (rec == NULL)
|
||||||
@ -290,7 +318,7 @@ void signal_stop(void)
|
|||||||
/* stop ongoing signal emission by signal name */
|
/* stop ongoing signal emission by signal name */
|
||||||
void signal_stop_by_name(const char *signal)
|
void signal_stop_by_name(const char *signal)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
int signal_id;
|
int signal_id;
|
||||||
|
|
||||||
signal_id = signal_get_uniq_id(signal);
|
signal_id = signal_get_uniq_id(signal);
|
||||||
@ -310,7 +338,7 @@ const char *signal_get_emitted(void)
|
|||||||
/* return the ID of the signal that is currently being emitted */
|
/* return the ID of the signal that is currently being emitted */
|
||||||
int signal_get_emitted_id(void)
|
int signal_get_emitted_id(void)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
|
|
||||||
rec = current_emitted_signal;
|
rec = current_emitted_signal;
|
||||||
g_return_val_if_fail(rec != NULL, -1);
|
g_return_val_if_fail(rec != NULL, -1);
|
||||||
@ -320,7 +348,7 @@ int signal_get_emitted_id(void)
|
|||||||
/* return TRUE if specified signal was stopped */
|
/* return TRUE if specified signal was stopped */
|
||||||
int signal_is_stopped(int signal_id)
|
int signal_is_stopped(int signal_id)
|
||||||
{
|
{
|
||||||
SIGNAL_REC *rec;
|
Signal *rec;
|
||||||
|
|
||||||
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
|
||||||
g_return_val_if_fail(rec != NULL, FALSE);
|
g_return_val_if_fail(rec != NULL, FALSE);
|
||||||
@ -328,73 +356,56 @@ int signal_is_stopped(int signal_id)
|
|||||||
return rec->emitting <= rec->stop_emit;
|
return rec->emitting <= rec->stop_emit;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void signal_remove_module(void *signal, SIGNAL_REC *rec,
|
static void signal_remove_module(void *signal, Signal *rec,
|
||||||
const char *module)
|
const char *module)
|
||||||
{
|
{
|
||||||
unsigned int index;
|
SignalHook **hook, **next;
|
||||||
int list;
|
|
||||||
|
|
||||||
for (list = 0; list < SIGNAL_LISTS; list++) {
|
for (hook = &rec->hooks; *hook != NULL; hook = next) {
|
||||||
if (rec->modulelist[list] == NULL)
|
next = &(*hook)->next;
|
||||||
continue;
|
|
||||||
|
|
||||||
for (index = rec->modulelist[list]->len; index > 0; index--)
|
if (strcasecmp((*hook)->module, module) == 0) {
|
||||||
if (g_strcasecmp(g_ptr_array_index(rec->modulelist[list], index-1), module) == 0)
|
next = hook;
|
||||||
signal_remove_from_list(rec, list, index-1);
|
signal_remove_hook(rec, hook);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* remove all signals that belong to `module' */
|
/* remove all signals that belong to `module' */
|
||||||
void signals_remove_module(const char *module)
|
void signals_remove_module(const char *module)
|
||||||
{
|
{
|
||||||
g_return_if_fail(module != NULL);
|
g_return_if_fail(module != NULL);
|
||||||
|
|
||||||
g_hash_table_foreach(signals, (GHFunc) signal_foreach_ref, NULL);
|
g_hash_table_foreach(signals, (GHFunc) signal_hash_ref, NULL);
|
||||||
g_hash_table_foreach(signals, (GHFunc) signal_remove_module, (void *) module);
|
g_hash_table_foreach(signals, (GHFunc) signal_remove_module,
|
||||||
g_hash_table_foreach_remove(signals, (GHRFunc) signal_foreach_unref, NULL);
|
(void *) module);
|
||||||
|
g_hash_table_foreach_remove(signals, (GHRFunc) signal_hash_unref, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void signals_init(void)
|
void signals_init(void)
|
||||||
{
|
{
|
||||||
signals_chunk = g_mem_chunk_new("signals", sizeof(SIGNAL_REC),
|
signals = g_hash_table_new(NULL, NULL);
|
||||||
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)
|
static void signal_free(void *key, Signal *rec)
|
||||||
{
|
{
|
||||||
int n;
|
g_warning("signal_free(%s) : signal still has %d references:",
|
||||||
|
signal_get_id_str(rec->id), rec->refcount);
|
||||||
|
|
||||||
signal_ref(rec);
|
while (rec->hooks != NULL) {
|
||||||
|
g_warning(" - module '%s' function %p",
|
||||||
|
rec->hooks->module, rec->hooks->func);
|
||||||
|
|
||||||
for (n = 0; n < SIGNAL_LISTS; n++) {
|
signal_remove_hook(rec, &rec->hooks);
|
||||||
if (rec->siglist[n] != NULL) {
|
|
||||||
signal_unref_count(rec, rec->siglist[n]->len);
|
|
||||||
signal_list_free(rec, n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void signals_deinit(void)
|
void signals_deinit(void)
|
||||||
{
|
{
|
||||||
g_hash_table_foreach(signals, (GHFunc) signal_free, NULL);
|
g_hash_table_foreach(signals, (GHFunc) signal_hash_ref, NULL);
|
||||||
|
g_hash_table_foreach(signals, (GHFunc) signal_free, NULL);
|
||||||
|
g_hash_table_foreach_remove(signals, (GHRFunc) signal_hash_unref, NULL);
|
||||||
g_hash_table_destroy(signals);
|
g_hash_table_destroy(signals);
|
||||||
|
|
||||||
module_uniq_destroy("signals");
|
module_uniq_destroy("signals");
|
||||||
g_mem_chunk_destroy(signals_chunk);
|
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,43 @@
|
|||||||
#ifndef __SIGNAL_H
|
#ifndef __SIGNAL_H
|
||||||
#define __SIGNAL_H
|
#define __SIGNAL_H
|
||||||
|
|
||||||
|
#define SIGNAL_PRIORITY_LOW 100
|
||||||
|
#define SIGNAL_PRIORITY_DEFAULT 0
|
||||||
|
#define SIGNAL_PRIORITY_HIGH -100
|
||||||
|
|
||||||
#define SIGNAL_MAX_ARGUMENTS 6
|
#define SIGNAL_MAX_ARGUMENTS 6
|
||||||
typedef void (*SIGNAL_FUNC) (gconstpointer, gconstpointer,
|
typedef void (*SIGNAL_FUNC) (const void *, const void *,
|
||||||
gconstpointer, gconstpointer,
|
const void *, const void *,
|
||||||
gconstpointer, gconstpointer);
|
const void *, const void *);
|
||||||
|
|
||||||
void signals_init(void);
|
extern void *signal_user_data; /* use signal_get_user_data() macro to access */
|
||||||
void signals_deinit(void);
|
|
||||||
|
|
||||||
/* signal name -> ID */
|
|
||||||
#define signal_get_uniq_id(signal) \
|
|
||||||
module_get_uniq_id_str("signals", signal)
|
|
||||||
/* signal ID -> name */
|
|
||||||
#define signal_get_id_str(signal_id) \
|
|
||||||
module_find_id_str("signals", signal_id)
|
|
||||||
|
|
||||||
/* bind a signal */
|
/* bind a signal */
|
||||||
void signal_add_to(const char *module, int pos,
|
void signal_add_full(const char *module, int priority,
|
||||||
const char *signal, SIGNAL_FUNC func);
|
const char *signal, SIGNAL_FUNC func, void *user_data);
|
||||||
void signal_add_to_id(const char *module, int pos,
|
void signal_add_full_id(const char *module, int priority,
|
||||||
int signal, SIGNAL_FUNC func);
|
int signal, SIGNAL_FUNC func, void *user_data);
|
||||||
#define signal_add(a, b) signal_add_to(MODULE_NAME, 1, a, b)
|
#define signal_add(signal, func) \
|
||||||
#define signal_add_first(a, b) signal_add_to(MODULE_NAME, 0, a, b)
|
signal_add_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, (signal), (SIGNAL_FUNC) (func), NULL)
|
||||||
#define signal_add_last(a, b) signal_add_to(MODULE_NAME, 2, a, b)
|
#define signal_add_first(signal, func) \
|
||||||
|
signal_add_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, (signal), (SIGNAL_FUNC) (func), NULL)
|
||||||
|
#define signal_add_last(signal, func) \
|
||||||
|
signal_add_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, (signal), (SIGNAL_FUNC) (func), NULL)
|
||||||
|
|
||||||
|
#define signal_add_data(signal, func, data) \
|
||||||
|
signal_add_full(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT, (signal), (SIGNAL_FUNC) (func), data)
|
||||||
|
#define signal_add_first_data(signal, func, data) \
|
||||||
|
signal_add_full(MODULE_NAME, SIGNAL_PRIORITY_HIGH, (signal), (SIGNAL_FUNC) (func), data)
|
||||||
|
#define signal_add_last_data(signal, func, data) \
|
||||||
|
signal_add_full(MODULE_NAME, SIGNAL_PRIORITY_LOW, (signal), (SIGNAL_FUNC) (func), data)
|
||||||
|
|
||||||
/* unbind signal */
|
/* unbind signal */
|
||||||
void signal_remove(const char *signal, SIGNAL_FUNC func);
|
void signal_remove_full(const char *signal, SIGNAL_FUNC func, void *user_data);
|
||||||
void signal_remove_id(int signal_id, SIGNAL_FUNC func);
|
#define signal_remove(signal, func) \
|
||||||
|
signal_remove_full((signal), (SIGNAL_FUNC) (func), NULL)
|
||||||
|
#define signal_remove_data(signal, func, data) \
|
||||||
|
signal_remove_full((signal), (SIGNAL_FUNC) (func), data)
|
||||||
|
void signal_remove_id(int signal_id, SIGNAL_FUNC func, void *user_data);
|
||||||
|
|
||||||
/* emit signal */
|
/* emit signal */
|
||||||
int signal_emit(const char *signal, int params, ...);
|
int signal_emit(const char *signal, int params, ...);
|
||||||
@ -44,8 +54,20 @@ const char *signal_get_emitted(void);
|
|||||||
int signal_get_emitted_id(void);
|
int signal_get_emitted_id(void);
|
||||||
/* return TRUE if specified signal was stopped */
|
/* return TRUE if specified signal was stopped */
|
||||||
int signal_is_stopped(int signal_id);
|
int signal_is_stopped(int signal_id);
|
||||||
|
/* return the user data of the signal function currently being emitted */
|
||||||
|
#define signal_get_user_data() signal_user_data
|
||||||
|
|
||||||
/* remove all signals that belong to `module' */
|
/* remove all signals that belong to `module' */
|
||||||
void signals_remove_module(const char *module);
|
void signals_remove_module(const char *module);
|
||||||
|
|
||||||
|
/* signal name -> ID */
|
||||||
|
#define signal_get_uniq_id(signal) \
|
||||||
|
module_get_uniq_id_str("signals", signal)
|
||||||
|
/* signal ID -> name */
|
||||||
|
#define signal_get_id_str(signal_id) \
|
||||||
|
module_find_id_str("signals", signal_id)
|
||||||
|
|
||||||
|
void signals_init(void);
|
||||||
|
void signals_deinit(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -859,8 +859,10 @@ static void statusbar_item_default_signals(SBAR_ITEM_REC *item)
|
|||||||
func = NULL;
|
func = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (func != NULL)
|
if (func != NULL) {
|
||||||
signal_add_to_id(MODULE_NAME, 1, *pos, func);
|
signal_add_full_id(MODULE_NAME, 1,
|
||||||
|
*pos, func, NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_slist_find(list, item) == NULL)
|
if (g_slist_find(list, item) == NULL)
|
||||||
@ -911,10 +913,10 @@ SBAR_ITEM_REC *statusbar_item_create(STATUSBAR_REC *bar,
|
|||||||
|
|
||||||
static void statusbar_signal_remove(int signal_id)
|
static void statusbar_signal_remove(int signal_id)
|
||||||
{
|
{
|
||||||
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_item);
|
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_item, NULL);
|
||||||
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_server);
|
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_server, NULL);
|
||||||
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_window);
|
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_window, NULL);
|
||||||
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_window_item);
|
signal_remove_id(signal_id, (SIGNAL_FUNC) statusbar_update_window_item, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void statusbar_item_remove_signal(SBAR_ITEM_REC *item, int signal_id)
|
static void statusbar_item_remove_signal(SBAR_ITEM_REC *item, int signal_id)
|
||||||
|
@ -288,8 +288,8 @@ static void perl_signal_add_to_int(const char *signal, SV *func,
|
|||||||
g_hash_table_insert(table, signal_idp, siglist);
|
g_hash_table_insert(table, signal_idp, siglist);
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
signal_add_to_id(MODULE_NAME, priority, rec->signal_id,
|
signal_add_full_id(MODULE_NAME, priority, rec->signal_id,
|
||||||
perl_signal_get_func(rec));
|
perl_signal_get_func(rec), NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,7 +321,7 @@ static void perl_signal_remove_list_one(GSList **siglist, PERL_SIGNAL_REC *rec)
|
|||||||
|
|
||||||
*siglist = g_slist_remove(*siglist, rec);
|
*siglist = g_slist_remove(*siglist, rec);
|
||||||
if (*siglist == NULL) {
|
if (*siglist == NULL) {
|
||||||
signal_remove_id(rec->signal_id, perl_signal_get_func(rec));
|
signal_remove_id(rec->signal_id, perl_signal_get_func(rec), NULL);
|
||||||
g_free(siglist);
|
g_free(siglist);
|
||||||
g_hash_table_remove(signals[rec->priority], signal_idp);
|
g_hash_table_remove(signals[rec->priority], signal_idp);
|
||||||
}
|
}
|
||||||
@ -407,7 +407,8 @@ static int signal_destroy_hash(void *key, GSList **list, PERL_SCRIPT_REC *script
|
|||||||
*list = g_slist_remove(*list, rec);
|
*list = g_slist_remove(*list, rec);
|
||||||
if (*list == NULL) {
|
if (*list == NULL) {
|
||||||
signal_remove_id(rec->signal_id,
|
signal_remove_id(rec->signal_id,
|
||||||
perl_signal_get_func(rec));
|
perl_signal_get_func(rec),
|
||||||
|
NULL);
|
||||||
}
|
}
|
||||||
perl_signal_destroy(rec);
|
perl_signal_destroy(rec);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user