File: callback.c

package info (click to toggle)
zapping 0.10~cvs6-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,828 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,816; xml: 2,742; makefile: 1,282; perl: 488
file content (234 lines) | stat: -rw-r--r-- 5,007 bytes parent folder | download | duplicates (7)
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
/*
 *  Copyright (C) 2001-2004 Michael H. Schimek
 *  Copyright (C) 2000-2003 Iaki Garca Etxebarria
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: callback.c,v 1.4 2005/07/04 21:55:52 mschimek Exp $ */

#include <assert.h>
#include <stdlib.h>
#include "callback.h"

struct _tv_callback {
	tv_callback *		next;
	tv_callback **		prev_next;
	tv_callback_fn *	notify;
	tv_callback_fn *	destroy;
	void *			user_data;
	unsigned int		blocked;
};

void
tv_nullify_pointer		(void *			object,
				 void *			user_data)
{
	assert (NULL != user_data);

	object = object;
	* ((void **) user_data)	= NULL;
}

/**
 * Remove a callback function from its list. Argument is the result
 * of tv_callback_add().
 *
 * Intended for tveng clients. Note the destroy handler is not called.
 */
void
tv_callback_remove		(tv_callback *		cb)
{
	tv_callback *next;

	if (!cb)
		return;

	if ((next = cb->next))
		next->prev_next = cb->prev_next;

	if (cb->prev_next)
		*cb->prev_next = next;

	free (cb);
}

void
tv_callback_delete		(tv_callback *		cb,
				 void *			object)
{
	assert (object != NULL);

	if (!cb)
		return;

	if (cb->destroy)
		cb->destroy (object, cb->user_data);

	tv_callback_remove (cb);
}

/**
 * Removes all callbacks from the list with the given notify and destroy
 * functions and user_data.
 *
 * Intended for tveng clients. Note the destroy handler is not called.
 */
void
tv_callback_remove_all		(tv_callback *		list,
				 tv_callback_fn *	notify,
				 tv_callback_fn *	destroy,
				 void *			user_data)
{
	while (list) {
		if ((NULL == user_data || list->user_data == user_data)
		    && (NULL == notify || list->notify == notify)
		    && (NULL == destroy || list->destroy == destroy)) {
			tv_callback *next;

			if ((next = list->next))
				next->prev_next = list->prev_next;

			if (list->prev_next)
				*list->prev_next = next;

			free (list);

			list = next;
		} else {
			list = list->next;
		}
	}
}

void
tv_callback_delete_all		(tv_callback *		list,
				 tv_callback_fn *	notify,
				 tv_callback_fn *	destroy,
				 void *			user_data,
				 void *			object)
{
	assert (object != NULL);

	while (list) {
		if ((NULL == user_data || list->user_data == user_data)
		    && (NULL == notify || list->notify == notify)
		    && (NULL == destroy || list->destroy == destroy)) {
			tv_callback *next;

			/* XXX needs ref counter? */
			if (list->destroy)
				list->destroy (object, list->user_data);

			if ((next = list->next))
				next->prev_next = list->prev_next;

			if (list->prev_next)
				*list->prev_next = next;

			free (list);

			list = next;
		} else {
			list = list->next;
		}
	}
}

/**
 * Add a function to a callback list. The notify function is called
 * on some event, the destroy function (if not NULL) before the list
 * is deleted.
 *
 * Not intended to be called directly by tveng clients.
 */
tv_callback *
tv_callback_add			(tv_callback **		list,
				 tv_callback_fn *	notify,
				 tv_callback_fn *	destroy,
				 void *			user_data)
{
	tv_callback *cb;

	assert (NULL != list);
	assert (NULL != notify || NULL != destroy);

	for (; (cb = *list); list = &cb->next)
		;

	if (!(cb = calloc (1, sizeof (*cb))))
		return NULL;

	*list = cb;

	cb->prev_next = list;

	cb->notify = notify;
	cb->destroy = destroy;
	cb->user_data = user_data;

	return cb;
}

/**
 * Temporarily block calls to the notify handler.
 */
void
tv_callback_block		(tv_callback *		cb)
{
	if (cb)
		++cb->blocked;
}

void
tv_callback_block_all		(tv_callback *		list,
				 tv_callback_fn *	notify,
				 tv_callback_fn *	destroy,
				 void *			user_data)
{
	for (; list; list = list->next) {
		if ((NULL == user_data || list->user_data == user_data)
		    && (NULL == notify || list->notify == notify)
		    && (NULL == destroy || list->destroy == destroy)) {
			++list->blocked;
		}
	}
}

/**
 * Counterpart of tv_callback_block().
 */
void
tv_callback_unblock		(tv_callback *		cb)
{
	if (cb && cb->blocked > 0)
		--cb->blocked;
}

void
tv_callback_unblock_all		(tv_callback *		list,
				 tv_callback_fn *	notify,
				 tv_callback_fn *	destroy,
				 void *			user_data)
{
	for (; list; list = list->next) {
		if ((NULL == user_data || list->user_data == user_data)
		    && (NULL == notify || list->notify == notify)
		    && (NULL == destroy || list->destroy == destroy)) {
			if (list->blocked > 0)
				--list->blocked;
		}
	}
}