event.c 13.7 KB
Newer Older
littlejohn's avatar
littlejohn committed
1
/*****************************************************************************
2
 * event.c: New libvlc event control API
littlejohn's avatar
littlejohn committed
3
4
5
6
7
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id $
 *
 * Authors: Filippo Carone <filippo@carone.org>
8
 *          Pierre d'Herbemont <pdherbemont # videolan.org>
littlejohn's avatar
littlejohn committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "libvlc_internal.h"
#include <vlc/libvlc.h>
27
#include <vlc_playlist.h>
littlejohn's avatar
littlejohn committed
28

29
30
31
32
33

/*
 * Private functions
 */

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
static vlc_bool_t
listeners_are_equal( libvlc_event_listener_t * listener1,
                     libvlc_event_listener_t * listener2 )
{
    return listener1->event_type  == listener2->event_type &&
           listener1->pf_callback == listener2->pf_callback &&
           listener1->p_user_data == listener2->p_user_data;
}

static vlc_bool_t
group_contains_listener( libvlc_event_listeners_group_t * group,
                         libvlc_event_listener_t * searched_listener )
{
    int i;
    for( i = 0; i < vlc_array_count(&group->listeners); i++ )
    {
        if( listeners_are_equal(searched_listener, vlc_array_item_at_index(&group->listeners, i)) )
            return VLC_TRUE;
    }
    return VLC_FALSE;
}

56
57
58
/*
 * Internal libvlc functions
 */
littlejohn's avatar
littlejohn committed
59

60
/**************************************************************************
61
 *       libvlc_event_init (internal) :
62
 *
63
 * initialization function.
64
 **************************************************************************/
65
void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
66
{
67
    (void)p_instance;(void)p_e;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
68
    /* Will certainly be used to install libvlc_instance event */
69
70
}

71
/**************************************************************************
72
 *       libvlc_event_fini (internal) :
73
 *
74
 * finalization function.
75
 **************************************************************************/
76
void libvlc_event_fini( libvlc_instance_t *p_instance )
77
{
78
    (void)p_instance;
79
80
}

81
/**************************************************************************
82
 *       libvlc_event_manager_init (internal) :
83
 *
84
 * Init an object's event manager.
85
 **************************************************************************/
86
libvlc_event_manager_t *
87
libvlc_event_manager_new( void * p_obj, libvlc_instance_t * p_libvlc_inst,
88
                           libvlc_exception_t *p_e )
89
{
90
    libvlc_event_manager_t * p_em;
91

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
92
93
94
95
96
97
    p_em = malloc(sizeof( libvlc_event_manager_t ));
    if( !p_em )
    {
        libvlc_exception_raise( p_e, "No Memory left" );
        return NULL;
    }
98

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
99
100
    p_em->p_obj = p_obj;
    p_em->p_libvlc_instance = p_libvlc_inst;
101
    libvlc_retain( p_libvlc_inst );
102
    vlc_array_init( &p_em->listeners_groups );
103
    vlc_mutex_init( p_libvlc_inst->p_libvlc_int, &p_em->object_lock );
104
    vlc_mutex_init_recursive( p_libvlc_inst->p_libvlc_int, &p_em->event_sending_lock );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
105
    return p_em;
106
107
}

108
/**************************************************************************
109
 *       libvlc_event_manager_release (internal) :
110
 *
111
 * Init an object's event manager.
112
 **************************************************************************/
113
void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
114
{
115
116
    libvlc_event_listeners_group_t * p_lg;
    int i,j ;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
117

118
    vlc_mutex_destroy( &p_em->event_sending_lock );
119
120
    vlc_mutex_destroy( &p_em->object_lock );

121
122
123
124
125
126
    for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
    {
        p_lg = vlc_array_item_at_index( &p_em->listeners_groups, i );

        for( j = 0; j < vlc_array_count(&p_lg->listeners); j++)
            free( vlc_array_item_at_index( &p_em->listeners_groups, i ) );
127

128
129
130
131
        vlc_array_clear( &p_lg->listeners );
        free( p_lg );
    }
    vlc_array_clear( &p_em->listeners_groups );
132
    libvlc_release( p_em->p_libvlc_instance );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
133
    free( p_em );
134
135
}

136
/**************************************************************************
137
 *       libvlc_event_manager_register_event_type (internal) :
138
 *
139
 * Init an object's event manager.
140
 **************************************************************************/
141
void libvlc_event_manager_register_event_type(
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
142
143
144
        libvlc_event_manager_t * p_em,
        libvlc_event_type_t event_type,
        libvlc_exception_t * p_e )
145
{
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
146
147
148
149
150
151
152
153
154
    libvlc_event_listeners_group_t * listeners_group;
    listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
    if( !listeners_group )
    {
        libvlc_exception_raise( p_e, "No Memory left" );
        return;
    }

    listeners_group->event_type = event_type;
155
    vlc_array_init( &listeners_group->listeners );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
156

157
    vlc_mutex_lock( &p_em->object_lock );
158
    vlc_array_append( &p_em->listeners_groups, listeners_group );
159
    vlc_mutex_unlock( &p_em->object_lock );
160
}
161

162
/**************************************************************************
163
 *       libvlc_event_send (internal) :
164
 *
165
 * Send a callback.
166
 **************************************************************************/
167
168
void libvlc_event_send( libvlc_event_manager_t * p_em,
                        libvlc_event_t * p_event )
169
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
170
    libvlc_event_listeners_group_t * listeners_group = NULL;
171
    libvlc_event_listener_t * listener_cached;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
172
    libvlc_event_listener_t * listener;
173
174
175
    libvlc_event_listener_t * array_listeners_cached = NULL;
    int i, i_cached_listeners = 0;

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
176
177
178
    /* Fill event with the sending object now */
    p_event->p_obj = p_em->p_obj;

179
180
    /* Here a read/write lock would be nice */

181
    vlc_mutex_lock( &p_em->object_lock );
182
183
184
    for( i = 0; i < vlc_array_count(&p_em->listeners_groups); i++)
    {
        listeners_group = vlc_array_item_at_index(&p_em->listeners_groups, i);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
185
186
        if( listeners_group->event_type == p_event->type )
        {
187
            if( vlc_array_count( &listeners_group->listeners ) <= 0 )
188
189
190
                break;

            /* Cache a copy of the listener to avoid locking issues */
191
            i_cached_listeners = vlc_array_count(&listeners_group->listeners);
192
193
194
195
196
197
198
199
            array_listeners_cached = malloc(sizeof(libvlc_event_listener_t)*(i_cached_listeners));
            if( !array_listeners_cached )
            {
                printf( "Can't alloc memory in libvlc_event_send" );
                break;
            }

            listener_cached = array_listeners_cached;
200
201
202
203
            for( i = 0; i < vlc_array_count(&listeners_group->listeners); i++)
            {
                listener = vlc_array_item_at_index(&listeners_group->listeners, i);
                memcpy( listener_cached, listener, sizeof(libvlc_event_listener_t) );
204
                listener_cached++;
205
            }
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
206
207
            break;
        }
208
    }
209

210
211
212
213
214
215
    if( !listeners_group )
    {
        free( array_listeners_cached );
        return;
    }

216
217
    vlc_mutex_unlock( &p_em->object_lock );

218
    vlc_mutex_lock( &p_em->event_sending_lock );
219
    listener_cached = array_listeners_cached;
220
    listeners_group->b_sublistener_removed = VLC_FALSE;
221
222
    for( i = 0; i < i_cached_listeners; i++ )
    {
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        if( listeners_group->b_sublistener_removed )
        {
            /* If a callback was removed, this gets called */
            vlc_bool_t valid_listener;
            vlc_mutex_lock( &p_em->object_lock );
            valid_listener = group_contains_listener( listeners_group, listener_cached );
            vlc_mutex_unlock( &p_em->object_lock );
            if( !valid_listener )
            {
                listener_cached++;
                continue;
            }
        }
        
237
        listener_cached->pf_callback( p_event, listener_cached->p_user_data );
238
        listener_cached++;
239
    }
240
241
    vlc_mutex_unlock( &p_em->event_sending_lock );

242
    free( array_listeners_cached );
243
244
}

245
246
247
248
/*
 * Public libvlc functions
 */

249
250
251
252
253
254
255
256
/**************************************************************************
 *       libvlc_event_type_name (public) :
 *
 * Get the char * name of an event type.
 **************************************************************************/
static const char * event_type_to_name[] =
{
#define EVENT(a) [a]=#a
257
258
259
260
261
262
263
264
265
266
267
268
269
    EVENT(libvlc_MediaMetaChanged),
    EVENT(libvlc_MediaSubItemAdded),
    EVENT(libvlc_MediaDurationChanged),
    EVENT(libvlc_MediaPreparsedChanged),
    EVENT(libvlc_MediaFreed),
    EVENT(libvlc_MediaStateChanged),

    EVENT(libvlc_MediaPlayerPlayed),
    EVENT(libvlc_MediaPlayerPaused),
    EVENT(libvlc_MediaPlayerEndReached),
    EVENT(libvlc_MediaPlayerStopped),
    EVENT(libvlc_MediaPlayerTimeChanged),
    EVENT(libvlc_MediaPlayerPositionChanged),
270
271
272
273
274
275
276
277
278
279
280
281
282

    EVENT(libvlc_MediaListItemAdded),
    EVENT(libvlc_MediaListWillAddItem),
    EVENT(libvlc_MediaListItemDeleted),
    EVENT(libvlc_MediaListWillDeleteItem),

    EVENT(libvlc_MediaListViewItemAdded),
    EVENT(libvlc_MediaListViewWillAddItem),
    EVENT(libvlc_MediaListViewItemDeleted),
    EVENT(libvlc_MediaListViewWillDeleteItem),

    EVENT(libvlc_MediaListPlayerPlayed),
    EVENT(libvlc_MediaListPlayerNextItemSet),
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
283
284
285
286
    EVENT(libvlc_MediaListPlayerStopped),

    EVENT(libvlc_MediaDiscovererStarted),
    EVENT(libvlc_MediaDiscovererEnded)
287
288
289
290
291
292
293
294
#undef EVENT
};
static const char * unkwown_event_name = "Unknown Event";

const char * libvlc_event_type_name( libvlc_event_type_t event_type )
{
    if( event_type >= sizeof(event_type_to_name)/sizeof(event_type_to_name[0]))
        return unkwown_event_name;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
295
    return event_type_to_name[event_type];
296
297
}

298
/**************************************************************************
299
 *       libvlc_event_attach (public) :
300
301
302
 *
 * Add a callback for an event.
 **************************************************************************/
303
304
305
306
307
void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
                          libvlc_event_type_t event_type,
                          libvlc_callback_t pf_callback,
                          void *p_user_data,
                          libvlc_exception_t *p_e )
littlejohn's avatar
littlejohn committed
308
{
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
309
310
    libvlc_event_listeners_group_t * listeners_group;
    libvlc_event_listener_t * listener;
311
312
    int i;

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
313
314
315
316
317
318
    listener = malloc(sizeof(libvlc_event_listener_t));
    if( !listener )
    {
        libvlc_exception_raise( p_e, "No Memory left" );
        return;
    }
319
 
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
320
321
322
    listener->event_type = event_type;
    listener->p_user_data = p_user_data;
    listener->pf_callback = pf_callback;
323
324

    vlc_mutex_lock( &p_event_manager->object_lock );
325
326
327
    for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++ )
    {
        listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
328
329
        if( listeners_group->event_type == listener->event_type )
        {
330
            vlc_array_append( &listeners_group->listeners, listener );
331
            vlc_mutex_unlock( &p_event_manager->object_lock );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
332
333
            return;
        }
334
    }
335
336
    vlc_mutex_unlock( &p_event_manager->object_lock );

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
337
338
339
    free(listener);
    libvlc_exception_raise( p_e,
            "This object event manager doesn't know about '%s' events",
340
            libvlc_event_type_name(event_type));
littlejohn's avatar
littlejohn committed
341
342
}

343
/**************************************************************************
344
 *       libvlc_event_detach (public) :
345
346
347
 *
 * Remove a callback for an event.
 **************************************************************************/
348
void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
349
350
351
352
                                     libvlc_event_type_t event_type,
                                     libvlc_callback_t pf_callback,
                                     void *p_user_data,
                                     libvlc_exception_t *p_e )
littlejohn's avatar
littlejohn committed
353
{
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
354
355
    libvlc_event_listeners_group_t * listeners_group;
    libvlc_event_listener_t * listener;
356
    int i, j;
357

358
    vlc_mutex_lock( &p_event_manager->event_sending_lock );
359
    vlc_mutex_lock( &p_event_manager->object_lock );
360
361
362
    for( i = 0; i < vlc_array_count(&p_event_manager->listeners_groups); i++)
    {
        listeners_group = vlc_array_item_at_index(&p_event_manager->listeners_groups, i);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
363
364
        if( listeners_group->event_type == event_type )
        {
365
            for( j = 0; j < vlc_array_count(&listeners_group->listeners); j++)
366
            {
367
                listener = vlc_array_item_at_index(&listeners_group->listeners, j);
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
368
369
370
371
372
                if( listener->event_type == event_type &&
                    listener->pf_callback == pf_callback &&
                    listener->p_user_data == p_user_data )
                {
                    /* that's our listener */
373
374
375
376
377
                    
                    /* Mark this group as edited so that libvlc_event_send
                     * will recheck what listener to call */
                    listeners_group->b_sublistener_removed = VLC_FALSE;

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
378
                    free( listener );
379
                    vlc_array_remove( &listeners_group->listeners, j );
380
                    vlc_mutex_unlock( &p_event_manager->object_lock );
381
                    vlc_mutex_unlock( &p_event_manager->event_sending_lock );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
382
383
                    return;
                }
384
            }
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
385
        }
386
    }
387
    vlc_mutex_unlock( &p_event_manager->object_lock );
388
    vlc_mutex_unlock( &p_event_manager->event_sending_lock );
389

Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
390
    libvlc_exception_raise( p_e,
391
392
            "This object event manager doesn't know about '%s,%p,%p' event observer",
            libvlc_event_type_name(event_type), pf_callback, p_user_data );
littlejohn's avatar
littlejohn committed
393
}