summaryrefslogtreecommitdiff
path: root/gst/closedcaption/event.c
blob: c192c00d18b7b2e89f2ea00e1583191e4320217d (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
/*
 *  libzvbi - Events
 *
 *  Copyright (C) 2004, 2008 Michael H. Schimek
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 *  Boston, MA  02110-1301  USA.
 */

/* $Id: event.c,v 1.2 2009-12-14 23:43:35 mschimek Exp $ */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "misc.h"               /* CLEAR() */
#include "event-priv.h"

/**
 * @internal
 * @param es Event handler list.
 * @param ev The event to send.
 * 
 * Traverses the list of event handlers and calls each handler waiting
 * for the @a ev->type of event, passing @a ev as parameter.
 */
void
__vbi_event_handler_list_send (_vbi_event_handler_list * el, vbi_event * ev)
{
  vbi_event_handler_rec *eh, **ehp;
  unsigned int ref_count;

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

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

  ref_count = el->ref_count + 1;
  if (unlikely (0 == ref_count))
    return;
  el->ref_count = ref_count;

  for (eh = el->first; NULL != eh; eh = eh->next) {
    if (0 != (eh->event_mask & ev->type) && !eh->remove) {
      eh->callback (ev, eh->user_data);
    }
  }

  el->ref_count = --ref_count;
  if (ref_count > 0)
    return;

  ehp = &el->first;

  while (NULL != (eh = *ehp)) {
    if (eh->remove) {
      *ehp = eh->next;
      vbi_free (eh);
    } else {
      ehp = &eh->next;
    }
  }
}

/**
 * @internal
 * @param el 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 _vbi_event_handler_list_remove_by_event
    (_vbi_event_handler_list * el, vbi_event_mask event_mask)
{
  vbi_event_handler_rec *eh, **ehp;
  vbi_event_mask clear_mask;

  assert (NULL != el);

  clear_mask = ~event_mask;

  ehp = &el->first;

  while (NULL != (eh = *ehp)) {
    if (0 == (eh->event_mask &= clear_mask)) {
      if (0 == el->ref_count) {
        *ehp = eh->next;
        vbi_free (eh);
      } else {
        eh->remove = TRUE;
        ehp = &eh->next;
      }
    } else {
      ehp = &eh->next;
    }
  }

  el->event_mask &= clear_mask;
}

/**
 * @param el 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 _vbi_event_handler_list_remove_by_callback
    (_vbi_event_handler_list * el, vbi_event_handler callback, void *user_data)
{
  _vbi_event_handler_list_add (el, 0, callback, user_data);
}

/**
 * @param el Event handler list.
 * @param eh Event handler.
 * 
 * Removes event handler @a eh if member of the list @a el. You can
 * safely call this function from a handler removing itself or another
 * handler.
 */
void
_vbi_event_handler_list_remove (_vbi_event_handler_list * el,
    vbi_event_handler_rec * eh)
{
  vbi_event_handler_rec *eh1, **ehp;
  vbi_event_mask event_union;

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

  ehp = &el->first;
  event_union = 0;

  while (NULL != (eh1 = *ehp)) {
    if (eh1 == eh) {
      if (0 == el->ref_count) {
        *ehp = eh1->next;
        vbi_free (eh1);
      } else {
        eh1->remove = TRUE;
        ehp = &eh1->next;
      }
    } else {
      event_union |= eh1->event_mask;
      ehp = &eh1->next;
    }
  }

  el->event_mask = event_union;
}

/**
 * @param el Event handler list.
 * @param event_mask Set of events (@c VBI_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
 * zero 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.
 *
 * @return
 * Pointer to opaque vbi_event_handler object, @c NULL on failure or if
 * no handler has been added.
 */
vbi_event_handler_rec *
_vbi_event_handler_list_add (_vbi_event_handler_list * el,
    vbi_event_mask event_mask, vbi_event_handler callback, void *user_data)
{
  vbi_event_handler_rec *eh, **ehp, *found;
  vbi_event_mask event_union;

  assert (NULL != el);

  ehp = &el->first;
  event_union = 0;
  found = NULL;

  while (NULL != (eh = *ehp)) {
    if (eh->callback == callback && eh->user_data == user_data) {
      if (0 == event_mask) {
        if (0 == el->ref_count) {
          *ehp = eh->next;
          vbi_free (eh);
        } else {
          eh->remove = TRUE;
          ehp = &eh->next;
        }

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

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

  if (NULL == found && 0 != event_mask) {
    found = vbi_malloc (sizeof (*found));
    if (NULL != found) {
      CLEAR (*found);

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

      event_union |= event_mask;

      *ehp = found;
    }
  }

  el->event_mask = event_union;

  return found;
}

void
_vbi_event_handler_list_destroy (_vbi_event_handler_list * el)
{
  assert (NULL != el);

  _vbi_event_handler_list_remove_by_event (el, (vbi_event_mask) - 1);

  CLEAR (*el);
}

vbi_bool
_vbi_event_handler_list_init (_vbi_event_handler_list * el)
{
  assert (NULL != el);

  CLEAR (*el);

  return TRUE;
}

/*
Local variables:
c-set-style: K&R
c-basic-offset: 8
End:
*/