events.c 13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * events.c: events interface
 * This library provides an interface to the send and receive events.
 * It is more lightweight than variable based callback.
 *****************************************************************************
 * Copyright (C) 1998-2005 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
 *
 * 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
23
24
25
26
27
28
29
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

30
31
32
33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
36
37
38
39
40
41
42
43
44

#include <assert.h>

#include <vlc_events.h>
#include <vlc_arrays.h>

/*****************************************************************************
 * Documentation : Read vlc_events.h
 *****************************************************************************/

45
46
//#define DEBUG_EVENT

47
48
49
50
51
52
53
54
/*****************************************************************************
 *  Private types.
 *****************************************************************************/

typedef struct vlc_event_listener_t
{
    void *               p_user_data;
    vlc_event_callback_t pf_callback;
55
56
57
#ifdef DEBUG_EVENT
    char *               psz_debug_name;
#endif
58
59
60
61
62
63
} vlc_event_listener_t;

typedef struct vlc_event_listeners_group_t
{
    vlc_event_type_t    event_type;
    DECL_ARRAY(struct vlc_event_listener_t *) listeners;
64
65
66
67

   /* Used in vlc_event_send() to make sure to behave
      Correctly when vlc_event_detach was called during
      a callback */
68
    bool          b_sublistener_removed;
69
                                         
70
71
} vlc_event_listeners_group_t;

72
#ifdef DEBUG_EVENT
73
static const char ppsz_event_type_to_name[][40] =
74
{
75
    [vlc_InputStateChanged]             = "vlc_InputStateChanged",
76
    [vlc_InputSelectedStreamChanged]    = "vlc_InputSelectedStreamChanged",
77

78
79
    [vlc_InputItemMetaChanged]          = "vlc_InputItemMetaChanged",
    [vlc_InputItemSubItemAdded]         = "vlc_InputItemSubItemAdded",
80
81
    [vlc_InputItemDurationChanged]      = "vlc_InputItemDurationChanged",
    [vlc_InputItemPreparsedChanged]     = "vlc_InputItemPreparsedChanged",
82
83
    [vlc_InputItemNameChanged]          = "vlc_InputItemNameChanged",
    [vlc_InputItemInfoChanged]          = "vlc_InputItemInfoChanged",
84
    [vlc_InputItemErrorWhenReadingChanged] = "vlc_InputItemErrorWhenReadingChanged",
85

86
    [vlc_ServicesDiscoveryItemAdded]    = "vlc_ServicesDiscoveryItemAdded",
87
88
    [vlc_ServicesDiscoveryItemRemoved]  = "vlc_ServicesDiscoveryItemRemoved",
    [vlc_ServicesDiscoveryStarted]      = "vlc_ServicesDiscoveryStarted",
89
    [vlc_ServicesDiscoveryEnded]        = "vlc_ServicesDiscoveryEnded"
90
};
91
92
#endif

93
static bool
94
95
96
97
98
99
100
listeners_are_equal( vlc_event_listener_t * listener1,
                     vlc_event_listener_t * listener2 )
{
    return listener1->pf_callback == listener2->pf_callback &&
           listener1->p_user_data == listener2->p_user_data;
}

101
static bool
102
103
104
105
106
group_contains_listener( vlc_event_listeners_group_t * group,
                         vlc_event_listener_t * searched_listener )
{
    vlc_event_listener_t * listener;
    FOREACH_ARRAY( listener, group->listeners )
107
        if( listeners_are_equal(searched_listener, listener) )
108
            return true;
109
    FOREACH_END()
110
    return false;
111
112
}

113
/*****************************************************************************
114
 *
115
116
117
118
 *****************************************************************************/

/**
 * Initialize event manager object
119
 * p_obj is the object that contains the event manager. But not
120
121
 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
 * for instance).
122
 * p_parent_obj gives a libvlc instance
123
 */
124
125
int __vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj,
                              vlc_object_t * p_parent_obj )
126
127
{
    p_em->p_obj = p_obj;
128
    p_em->p_parent_object = p_parent_obj;
129
    vlc_mutex_init( &p_em->object_lock );
130
131
132
133
134
135
136

    /* We need a recursive lock here, because we need to be able
     * to call libvlc_event_detach even if vlc_event_send is in
     * the call frame.
     * This ensures that after libvlc_event_detach, the callback
     * will never gets triggered.
     * */
137
    vlc_mutex_init_recursive( &p_em->event_sending_lock );
138
139
140
141
142
143
144
    ARRAY_INIT( p_em->listeners_groups );
    return VLC_SUCCESS;
}

/**
 * Destroy the event manager
 */
145
void vlc_event_manager_fini( vlc_event_manager_t * p_em )
146
147
148
149
{
    struct vlc_event_listeners_group_t * listeners_group;
    struct vlc_event_listener_t * listener;

150
    vlc_mutex_destroy( &p_em->object_lock );
151
    vlc_mutex_destroy( &p_em->event_sending_lock );
152

153
154
155
156
    FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
        FOREACH_ARRAY( listener, listeners_group->listeners )
            free( listener );
        FOREACH_END()
157
        ARRAY_RESET( listeners_group->listeners );
158
159
        free( listeners_group );
    FOREACH_END()
160
    ARRAY_RESET( p_em->listeners_groups );
161
162
163
164
165
}

/**
 * Destroy the event manager
 */
166
int vlc_event_manager_register_event_type(
167
168
169
170
171
172
173
174
175
176
177
        vlc_event_manager_t * p_em,
        vlc_event_type_t event_type )
{
    vlc_event_listeners_group_t * listeners_group;
    listeners_group = malloc(sizeof(vlc_event_listeners_group_t));

    if( !listeners_group )
        return VLC_ENOMEM;

    listeners_group->event_type = event_type;
    ARRAY_INIT( listeners_group->listeners );
178
 
179
    vlc_mutex_lock( &p_em->object_lock );
180
    ARRAY_APPEND( p_em->listeners_groups, listeners_group );
181
    vlc_mutex_unlock( &p_em->object_lock );
182
183
184
185
186
187
188

    return VLC_SUCCESS;
}

/**
 * Send an event to the listener attached to this p_em.
 */
189
190
void vlc_event_send( vlc_event_manager_t * p_em,
                     vlc_event_t * p_event )
191
{
192
    vlc_event_listeners_group_t * listeners_group = NULL;
193
    vlc_event_listener_t * listener;
194
195
196
    vlc_event_listener_t * array_of_cached_listeners = NULL;
    vlc_event_listener_t * cached_listener;
    int i, i_cached_listeners = 0;
197
198
199
200

    /* Fill event with the sending object now */
    p_event->p_obj = p_em->p_obj;

201
    vlc_mutex_lock( &p_em->object_lock );
202
203
204
    FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
        if( listeners_group->event_type == p_event->type )
        {
205
206
207
208
209
210
211
212
213
            if( listeners_group->listeners.i_size <= 0 )
                break;

            /* Save the function to call */
            i_cached_listeners = listeners_group->listeners.i_size;
            array_of_cached_listeners = malloc(
                    sizeof(vlc_event_listener_t)*i_cached_listeners );
            if( !array_of_cached_listeners )
            {
214
                vlc_mutex_unlock( &p_em->object_lock );
215
216
217
218
                return;
            }

            cached_listener = array_of_cached_listeners;
219
            FOREACH_ARRAY( listener, listeners_group->listeners )
220
                memcpy( cached_listener, listener, sizeof(vlc_event_listener_t));
221
#ifdef DEBUG_EVENT
222
                cached_listener->psz_debug_name = strdup(cached_listener->psz_debug_name);
223
#endif
224
                cached_listener++;
225
            FOREACH_END()
226

227
228
229
            break;
        }
    FOREACH_END()
230
    vlc_mutex_unlock( &p_em->object_lock );
231
 
232
233
    /* Call the function attached */
    cached_listener = array_of_cached_listeners;
234

235
236
237
    if( !listeners_group || !array_of_cached_listeners )
    {
        free( array_of_cached_listeners );
238
        return;
239
    }
240

241
    vlc_mutex_lock( &p_em->event_sending_lock ) ;
242
243

    /* Track item removed from *this* thread, with a simple flag */
244
    listeners_group->b_sublistener_removed = false;
245

246
247
248
249
250
251
252
253
    for( i = 0; i < i_cached_listeners; i++ )
    {
#ifdef DEBUG_EVENT
        msg_Dbg( p_em->p_parent_object,
                    "Calling '%s' with a '%s' event (data %p)",
                    cached_listener->psz_debug_name,
                    ppsz_event_type_to_name[p_event->type],
                    cached_listener->p_user_data );
254
        free(cached_listener->psz_debug_name);
255
#endif
256
257
258
259
        /* No need to lock on listeners_group, a listener group can't be removed */
        if( listeners_group->b_sublistener_removed )
        {
            /* If a callback was removed, this gets called */
260
            bool valid_listener;
261
262
263
264
265
266
267
268
269
270
271
272
            vlc_mutex_lock( &p_em->object_lock );
            valid_listener = group_contains_listener( listeners_group, cached_listener );
            vlc_mutex_unlock( &p_em->object_lock );
            if( !valid_listener )
            {
#ifdef DEBUG_EVENT
                msg_Dbg( p_em->p_parent_object, "Callback was removed during execution" );
#endif
                cached_listener++;
                continue;
            }
        }
273
        cached_listener->pf_callback( p_event, cached_listener->p_user_data );
274
        cached_listener++;
275
    }
276
    vlc_mutex_unlock( &p_em->event_sending_lock );
277

278
    free( array_of_cached_listeners );
279
280
281
282
283
}

/**
 * Add a callback for an event.
 */
284
285
286
287
288
int __vlc_event_attach( vlc_event_manager_t * p_em,
                        vlc_event_type_t event_type,
                        vlc_event_callback_t pf_callback,
                        void *p_user_data,
                        const char * psz_debug_name )
289
290
291
292
293
294
{
    vlc_event_listeners_group_t * listeners_group;
    vlc_event_listener_t * listener;
    listener = malloc(sizeof(vlc_event_listener_t));
    if( !listener )
        return VLC_ENOMEM;
295
 
296
297
    listener->p_user_data = p_user_data;
    listener->pf_callback = pf_callback;
298
299
300
301
302
#ifdef DEBUG_EVENT
    listener->psz_debug_name = strdup( psz_debug_name );
#else
    (void)psz_debug_name;
#endif
303
304
305

    vlc_mutex_lock( &p_em->object_lock );
    FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
306
307
308
        if( listeners_group->event_type == event_type )
        {
            ARRAY_APPEND( listeners_group->listeners, listener );
309
#ifdef DEBUG_EVENT
310
311
                msg_Dbg( p_em->p_parent_object,
                    "Listening to '%s' event with '%s' (data %p)",
312
313
314
315
                    ppsz_event_type_to_name[event_type],
                    listener->psz_debug_name,
                    listener->p_user_data );
#endif
316
            vlc_mutex_unlock( &p_em->object_lock );
317
318
319
            return VLC_SUCCESS;
        }
    FOREACH_END()
320
321
    vlc_mutex_unlock( &p_em->object_lock );

322
    msg_Err( p_em->p_parent_object, "Can't attach to an object event manager event" );
323
324
325
326
327
328
329
    free(listener);
    return VLC_EGENERIC;
}

/**
 * Remove a callback for an event.
 */
330

331
int vlc_event_detach( vlc_event_manager_t *p_em,
332
333
334
                      vlc_event_type_t event_type,
                      vlc_event_callback_t pf_callback,
                      void *p_user_data )
335
336
337
{
    vlc_event_listeners_group_t * listeners_group;
    struct vlc_event_listener_t * listener;
338
339

    vlc_mutex_lock( &p_em->object_lock );
340
    vlc_mutex_lock( &p_em->event_sending_lock );
341
    FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
342
343
344
345
346
347
        if( listeners_group->event_type == event_type )
        {
            FOREACH_ARRAY( listener, listeners_group->listeners )
                if( listener->pf_callback == pf_callback &&
                    listener->p_user_data == p_user_data )
                {
348
349
                    /* Tell vlc_event_send, we did remove an item from that group,
                       in case vlc_event_send is in our caller stack  */
350
                    listeners_group->b_sublistener_removed = true;
351

352
353
354
355
                    /* that's our listener */
                    ARRAY_REMOVE( listeners_group->listeners,
                        fe_idx /* This comes from the macro (and that's why
                                  I hate macro) */ );
356
#ifdef DEBUG_EVENT
357
                    msg_Dbg( p_em->p_parent_object,
358
                        "Detaching '%s' from '%s' event (data %p)",
359
360
361
362
363
364
                        listener->psz_debug_name,
                        ppsz_event_type_to_name[event_type],
                        listener->p_user_data );

                    free( listener->psz_debug_name );
#endif
365
                    free( listener );
366
                    vlc_mutex_unlock( &p_em->event_sending_lock );
367
                    vlc_mutex_unlock( &p_em->object_lock );
368
369
370
371
372
                    return VLC_SUCCESS;
                }
            FOREACH_END()
        }
    FOREACH_END()
373
    vlc_mutex_unlock( &p_em->event_sending_lock );
374
    vlc_mutex_unlock( &p_em->object_lock );
375

376
377
    msg_Warn( p_em->p_parent_object, "Can't detach to an object event manager event" );

378
379
    return VLC_EGENERIC;
}
380