File: event.c

package info (click to toggle)
zapping 0.10~cvs6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,856 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,812; xml: 2,742; makefile: 1,283; perl: 488
file content (337 lines) | stat: -rw-r--r-- 7,628 bytes parent folder | download | duplicates (6)
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
/*
 *  libzvbi - Events
 *
 *  Copyright (C) 2000, 2001, 2002, 2003, 2004 Michael H. Schimek
 *
 *  Based on code from AleVT 1.5.1
 *  Copyright (C) 1998,1999 Edgar Toernig (froese@gmx.de)
 *
 *  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: event.c,v 1.6 2005/09/01 01:34:40 mschimek Exp $ */

#include <assert.h>
#include <stdlib.h>		/* malloc() */
#include "misc.h"		/* CLEAR() */
#include "event-priv.h"

/**
 * @addtogroup Event Events
 * @ingroup Service
 *
 * Typically the transmission of VBI data like a Teletext or Closed
 * Caption page spans several VBI lines or even video frames. So internally
 * the data service decoder maintains caches accumulating data. When a page
 * or other object is complete it calls the respective event handler to
 * notify the application.
 *
 * Clients can register any number of handlers needed, also different
 * handlers for the same event. They will be called in the order registered
 * from the vbi3_decode() function. Since they block decoding, they should
 * return as soon as possible. The event structure and all data
 * pointed to from there must be read only. The data is only valid until
 * the handler returns.
 */

/** @internal */
const char *
_vbi3_event_name			(vbi3_event_mask	event)
{
	switch (event) {

#undef CASE
#define CASE(s) case VBI3_EVENT_##s : return #s ;

	CASE (NONE)
	CASE (CLOSE)
	CASE (RESET)
	CASE (TTX_PAGE)
	CASE (CC_PAGE)
	CASE (NETWORK)
	CASE (TRIGGER)
	CASE (ASPECT)
	CASE (PROG_INFO)
	CASE (PAGE_TYPE)
	CASE (TOP_CHANGE)
	CASE (LOCAL_TIME)
	CASE (PROG_ID)
	CASE (CC_RAW)
	}

	return NULL;
}

/**
 * @internal
 * @param es Event handler list.
 * @param ev The event to send.
 * 
 * Traverses the list of event handlers and calls each handler waiting
 * for this @a ev->type of event, passing @a ev as parameter.
 */
void
__vbi3_event_handler_list_send	(_vbi3_event_handler_list *es,
				 const vbi3_event *	ev)
{
	vbi3_event_handler *eh;
	vbi3_event_handler *current;

	assert (NULL != es);
	assert (NULL != ev);

	if (0 == (es->event_mask & ev->type))
		return;

	current = es->current;

	eh = es->first;

	while (eh) {
		if ((eh->event_mask & ev->type)
		    && eh->callback
		    && 0 == eh->blocked) {
			vbi3_bool done;

			es->current = eh;
			eh->blocked = 1;

			done = eh->callback (ev, eh->user_data);

			if (es->current == eh) {
				eh->blocked = 0;
				eh = eh->next;
			} else {
				/* eh removed itself in callback. */
				eh = es->current;
			}

			if (done)
				break;
		} else {
			eh = eh->next;
		}
	}

	es->current = current;
}

/**
 * @internal
 * @param es Event handler list.
 * @param event_mask Event mask.
 *
 * Removes all handlers from the list which handle
 * only events given in the @a event_mask.
 */
void
_vbi3_event_handler_list_remove_by_event
			    	(_vbi3_event_handler_list *es,
				 vbi3_event_mask	event_mask)
{
	vbi3_event_handler *eh, **ehp;
	vbi3_event_mask clear_mask;

	assert (NULL != es);

	clear_mask = ~event_mask;

	ehp = &es->first;

	while ((eh = *ehp)) {
		if (0 == (eh->event_mask &= clear_mask)) {
			/* Remove handler. */

			*ehp = eh->next;

			if (es->current == eh)
				es->current = eh->next;

			vbi3_free (eh);
		} else {
			ehp = &eh->next;
		}
	}

	es->event_mask &= clear_mask;
}

/**
 * @param es Event handler list.
 * @param callback Function to be called on events.
 * @param user_data User pointer passed through to the @a callback function.
 * 
 * Removes all event handlers from the list with this @a callback and
 * @a user_data. You can safely call this function from a handler removing
 * itself or another handler.
 */
void
_vbi3_event_handler_list_remove_by_callback
				(_vbi3_event_handler_list *es,
				 vbi3_event_cb *		callback,
				 void *			user_data)
{
	_vbi3_event_handler_list_add (es, 0, callback, user_data);
}

/**
 * @param es Event handler list.
 * @param eh Event handler.
 * 
 * Removes event handler @a eh if member of the list @a es. You can
 * safely call this function from a handler removing itself or another
 * handler.
 */
void
_vbi3_event_handler_list_remove	(_vbi3_event_handler_list *es,
				 vbi3_event_handler *	eh)
{
	vbi3_event_handler *eh1, **ehp;
	vbi3_event_mask event_union;

	assert (NULL != es);
	assert (NULL != eh);

	ehp = &es->first;

	event_union = 0;

	while ((eh1 = *ehp)) {
		if (eh == eh1) {
			/* Remove handler. */

			*ehp = eh->next;

			if (es->current == eh)
				es->current = eh->next;

			vbi3_free (eh);
		} else {
			event_union |= eh1->event_mask;
			ehp = &eh1->next;
		}
	}

	es->event_mask = event_union;
}

/**
 * @param es Event handler list.
 * @param event_mask Set of events (@c VBI3_EVENT_) the handler is waiting
 *   for, can be -1 for all and 0 for none.
 * @param callback Function to be called on events.
 * @param user_data User pointer passed through to the @a callback function.
 * 
 * Adds a new event handler to the list. When the @a callback with @a
 * user_data is already registered the function merely changes the set
 * of events it will receive in the future. When the @a event_mask is
 * empty the function does nothing or removes an already registered event
 * handler. You can safely call this function from an event handler.
 *
 * Any number of handlers can be added, also different handlers for the
 * same event which will be called in registration order. Handlers are
 * not recursively called when they trigger events.
 *
 * @return
 * Pointer to opaque vbi3_event_handler object, @c NULL on failure or if
 * no handler has been added.
 */
vbi3_event_handler *
_vbi3_event_handler_list_add	(_vbi3_event_handler_list *es,
				 vbi3_event_mask	event_mask,
				 vbi3_event_cb *		callback,
				 void *			user_data)
{
	vbi3_event_handler *eh, **ehp, *found;
	vbi3_event_mask event_union;

	assert (NULL != es);

	ehp = &es->first;

	event_union = 0;

	found = NULL;

	while ((eh = *ehp)) {
		if (eh->callback == callback
		    && eh->user_data == user_data) {
			found = eh;

			if (0 == event_mask) {
				/* Remove handler. */

				*ehp = eh->next;

				if (es->current == eh)
					es->current = eh->next;

				vbi3_free (eh);

				continue;
			} else {
				eh->event_mask = event_mask;
			}
		}

		event_union |= eh->event_mask;
		ehp = &eh->next;
	}

	if (NULL == found && event_mask) {
		/* Add handler. */

		if ((found = vbi3_malloc (sizeof (*found)))) {
			CLEAR (*found);

			found->next		= NULL;
			found->event_mask	= event_mask;

			found->callback		= callback;
			found->user_data	= user_data;

			/* Whoops. Remalloc'ed ourselves? */
			found->blocked = (es->current == found);

			event_union |= event_mask;

			*ehp = found;
		}
	}

	es->event_mask = event_union;

	return found;
}

void
_vbi3_event_handler_list_destroy	(_vbi3_event_handler_list *es)
{
	assert (NULL != es);

	_vbi3_event_handler_list_remove_by_event (es, (vbi3_event_mask) -1);

	CLEAR (*es);
}

vbi3_bool
_vbi3_event_handler_list_init	(_vbi3_event_handler_list *es)
{
	assert (NULL != es);

	CLEAR (*es);

	return TRUE;
}