summaryrefslogtreecommitdiff
path: root/src/core/signals.c
blob: 0fecd5c233c1f07576cd6f4a317d2faccc20bf1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 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 {
	int emitting; /* signal is being emitted */
	int altered; /* some signal functions are marked as NULL */
	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 *first_signal_rec, *last_signal_rec; /* "signal" and "last signal" */
static SIGNAL_REC *current_emitted_signal;

/* bind a signal */
void signal_add_to(const char *module, int pos, const char *signal,
		   SIGNAL_FUNC func)
{
	SIGNAL_REC *rec;
	int signal_id;

	g_return_if_fail(signal != NULL);
	g_return_if_fail(func != NULL);
	g_return_if_fail(pos >= 0 && pos < SIGNAL_LISTS);

	signal_id = signal_get_uniq_id(signal);

	rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
	if (rec == NULL) {
		rec = g_mem_chunk_alloc0(signals_chunk);
		g_hash_table_insert(signals, GINT_TO_POINTER(signal_id), rec);
	}

	if (strcmp(signal, "signal") == 0)
		first_signal_rec = rec;
	else if (strcmp(signal, "last signal") == 0)
		last_signal_rec = 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);
}

/* Destroy the whole signal */
static void signal_destroy(int signal_id)
{
	SIGNAL_REC *rec;

	rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
	if (rec != NULL) {
		/* remove whole signal from memory */
		g_hash_table_remove(signals, GINT_TO_POINTER(signal_id));
		g_free(rec);
	}

	if (first_signal_rec == rec)
                first_signal_rec = NULL;
	else if (last_signal_rec == rec)
                last_signal_rec = NULL;
}

static int signal_list_find(GPtrArray *array, void *data)
{
	unsigned int n;

	for (n = 0; n < array->len; n++) {
		if (g_ptr_array_index(array, n) == data)
			return n;
	}

	return -1;
}

static void signal_remove_from_list(SIGNAL_REC *rec, int signal_id,
				    int list, int index)
{
	if (rec->emitting) {
		g_ptr_array_index(rec->siglist[list], index) = NULL;
		rec->altered = TRUE;
	} else {
		g_ptr_array_remove_index(rec->siglist[list], index);
		g_ptr_array_remove_index(rec->modulelist[list], index);
		if (signal_is_emitlist_empty(rec))
			signal_destroy(signal_id);
	}
}

/* Remove signal from emit lists */
static int signal_remove_from_lists(SIGNAL_REC *rec, int signal_id,
				    SIGNAL_FUNC func)
{
	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 */
			signal_remove_from_list(rec, signal_id, n, index);
			return 1;
		}
	}

        return 0;
}

/* unbind signal */
void signal_remove(const char *signal, SIGNAL_FUNC func)
{
	SIGNAL_REC *rec;
	int signal_id, found;

	g_return_if_fail(signal != NULL);
	g_return_if_fail(func != NULL);

	signal_id = signal_get_uniq_id(signal);

	rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
	found = rec == NULL ? 0 :
		signal_remove_from_lists(rec, signal_id, func);

	if (!found) {
		g_warning("signal_remove() : signal \"%s\" isn't "
			  "grabbed for %p", signal, func);
	}
}

/* Remove all NULL functions from signal list */
static void signal_list_clean(SIGNAL_REC *rec)
{
	int n, index;

	for (n = 0; n < SIGNAL_LISTS; n++) {
		if (rec->siglist[n] == NULL)
			continue;

		for (index = rec->siglist[n]->len-1; index >= 0; index--) {
			if (g_ptr_array_index(rec->siglist[n], index) == NULL) {
				g_ptr_array_remove_index(rec->siglist[n], index);
				g_ptr_array_remove_index(rec->modulelist[n], index);
			}
		}
	}
}

static int signal_emit_real(SIGNAL_REC *rec, gconstpointer *arglist)
{
        SIGNAL_REC *prev_emitted_signal;
        SIGNAL_FUNC func;
	int n, index, stopped, stop_emit_count;

	/* signal_stop_by_name("signal"); signal_emit("signal", ...);
	   fails if we compare rec->stop_emit against 0. */
	stop_emit_count = rec->stop_emit;

	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);

			if (func != NULL) {
                                prev_emitted_signal = current_emitted_signal;
				current_emitted_signal = rec;
				func(arglist[0], arglist[1], arglist[2], arglist[3], arglist[4], arglist[5], arglist[6]);
				current_emitted_signal = prev_emitted_signal;
			}

			if (rec->stop_emit != stop_emit_count) {
				stopped = TRUE;
				rec->stop_emit--;
				n = SIGNAL_LISTS;
				break;
			}
		}
	}
	rec->emitting--;

	if (!rec->emitting) {
		if (rec->stop_emit != 0) {
			/* signal_stop() used too many times */
                        rec->stop_emit = 0;
		}
		if (rec->altered) {
			signal_list_clean(rec);
			rec->altered = FALSE;
		}
	}

	return stopped;
}

static int signal_emitv_id(int signal_id, int params, va_list va)
{
	gconstpointer arglist[8];
	SIGNAL_REC *rec;
	int n;

	g_return_val_if_fail(signal_id >= 0, FALSE);
	g_return_val_if_fail(params >= 0 && params <= sizeof(arglist)/sizeof(arglist[0]), FALSE);

	arglist[0] = GINT_TO_POINTER(signal_id);
	for (n = 1; n < 8; n++)
		arglist[n] = n > params ? NULL : va_arg(va, gconstpointer);

	/* send "signal" */
	if (first_signal_rec != NULL) {
		if (signal_emit_real(first_signal_rec, arglist))
			return TRUE;
	}

	rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
	if (rec != NULL && signal_emit_real(rec, arglist+1))
		return TRUE;

	/* send "last signal" */
	if (last_signal_rec != NULL) {
		if (signal_emit_real(last_signal_rec, arglist))
			return TRUE;
	}

	return rec != NULL;
}

int signal_emit(const char *signal, int params, ...)
{
	va_list va;
	int signal_id, ret;

	/* get arguments */
	signal_id = signal_get_uniq_id(signal);

	va_start(va, params);
	ret = signal_emitv_id(signal_id, params, va);
	va_end(va);

	return ret;
}

int signal_emit_id(int signal_id, int params, ...)
{
	va_list va;
	int ret;

	/* get arguments */
	va_start(va, params);
	ret = signal_emitv_id(signal_id, params, va);
	va_end(va);

	return ret;
}

/* stop the current ongoing signal emission */
void signal_stop(void)
{
	SIGNAL_REC *rec;

	rec = current_emitted_signal;
	if (rec == NULL || rec->emitting <= rec->stop_emit)
		g_warning("signal_stop() : no signals are being emitted currently");
	else
		rec->stop_emit++;
}

/* stop ongoing signal emission by signal name */
void signal_stop_by_name(const char *signal)
{
	SIGNAL_REC *rec;
	int signal_id;

	signal_id = signal_get_uniq_id(signal);
	rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
	if (rec == NULL)
		g_warning("signal_stop_by_name() : unknown signal \"%s\"", signal);
	else if (rec->emitting <= rec->stop_emit)
		g_warning("signal_stop_by_name() : signal \"%s\" not being emitted", signal);
	else
		rec->stop_emit++;
}

static void signal_remove_module(void *signal, SIGNAL_REC *rec, const char *module)
{
	unsigned int index;
	int signal_id, list;

	signal_id = GPOINTER_TO_INT(signal);

	for (list = 0; list < SIGNAL_LISTS; list++) {
		if (rec->modulelist[list] == NULL)
			continue;

		for (index = 0; index < rec->modulelist[list]->len; index++) {
			if (g_strcasecmp(g_ptr_array_index(rec->modulelist[list], index), module) == 0)
				signal_remove_from_list(rec, signal_id, list, index);
		}
	}
}

/* remove all signals that belong to `module' */
void signals_remove_module(const char *module)
{
	g_return_if_fail(module != NULL);

	g_hash_table_foreach(signals, (GHFunc) signal_remove_module, (void *) module);
}

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);

	first_signal_rec = NULL;
	last_signal_rec = NULL;
}

static void signal_free(void *key, SIGNAL_REC *rec)
{
	int n;

	for (n = 0; n < SIGNAL_LISTS; n++) {
		if (rec->siglist[n] != NULL) {
			g_ptr_array_free(rec->siglist[n], TRUE);
			g_ptr_array_free(rec->modulelist[n], TRUE);
		}
	}

	g_mem_chunk_free(signals_chunk, rec);
	current_emitted_signal = NULL;
}

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);
}