events.c 5.36 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * events.c: events interface
 * This library provides an interface to the send and receive events.
 * It is more lightweight than variable based callback.
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
6
 * Copyright (C) 1998-2005 VLC authors and VideoLAN
7 8 9 10
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org >
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
14 15 16 17
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * You should have received a copy of the GNU Lesser 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.
24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * 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 45 46 47 48 49 50 51 52 53 54 55

#include <assert.h>

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

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

/*****************************************************************************
 *  Private types.
 *****************************************************************************/

typedef struct vlc_event_listener_t
{
    void *               p_user_data;
    vlc_event_callback_t pf_callback;
} vlc_event_listener_t;

/*****************************************************************************
56
 *
57 58
 *****************************************************************************/

59
#undef vlc_event_manager_init
60 61
/**
 * Initialize event manager object
62
 * p_obj is the object that contains the event manager. But not
63 64 65
 * necessarily a vlc_object_t (an input_item_t is not a vlc_object_t
 * for instance).
 */
66
void vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj )
67 68
{
    p_em->p_obj = p_obj;
69 70 71
    /* This is an unsafe work-around for a long-standing playlist bug.
     * Do not rely on this. */
    vlc_mutex_init_recursive( &p_em->lock );
72 73 74

    for( size_t i = 0; i < ARRAY_SIZE(p_em->events); i++ )
       ARRAY_INIT( p_em->events[i].listeners );
75 76 77 78 79
}

/**
 * Destroy the event manager
 */
80
void vlc_event_manager_fini( vlc_event_manager_t * p_em )
81 82 83
{
    struct vlc_event_listener_t * listener;

84
    vlc_mutex_destroy( &p_em->lock );
85

86 87 88 89 90
    for( size_t i = 0; i < ARRAY_SIZE(p_em->events); i++ )
    {
        struct vlc_event_listeners_group_t *slot = p_em->events + i;

        FOREACH_ARRAY( listener, slot->listeners )
91 92
            free( listener );
        FOREACH_END()
93 94
        ARRAY_RESET( slot->listeners );
    }
95 96 97 98 99
}

/**
 * Send an event to the listener attached to this p_em.
 */
100 101
void vlc_event_send( vlc_event_manager_t * p_em,
                     vlc_event_t * p_event )
102
{
103
    vlc_event_listeners_group_t *slot = &p_em->events[p_event->type];
104 105 106 107 108
    vlc_event_listener_t * listener;

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

109
    vlc_mutex_lock( &p_em->lock ) ;
Rafaël Carré's avatar
Rafaël Carré committed
110

111
    FOREACH_ARRAY( listener, slot->listeners )
112
        listener->pf_callback( p_event, listener->p_user_data );
113
    FOREACH_END()
Rafaël Carré's avatar
Rafaël Carré committed
114

115
    vlc_mutex_unlock( &p_em->lock );
116 117
}

118
#undef vlc_event_attach
119 120 121
/**
 * Add a callback for an event.
 */
122 123 124
int vlc_event_attach( vlc_event_manager_t * p_em,
                      vlc_event_type_t event_type,
                      vlc_event_callback_t pf_callback,
125
                      void *p_user_data )
126 127
{
    vlc_event_listener_t * listener;
128 129
    vlc_event_listeners_group_t *slot = &p_em->events[event_type];

130 131 132
    listener = malloc(sizeof(vlc_event_listener_t));
    if( !listener )
        return VLC_ENOMEM;
133

134 135
    listener->p_user_data = p_user_data;
    listener->pf_callback = pf_callback;
136

137
    vlc_mutex_lock( &p_em->lock );
138
    ARRAY_APPEND( slot->listeners, listener );
139
    vlc_mutex_unlock( &p_em->lock );
140
    return VLC_SUCCESS;
141 142 143 144 145
}

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

147 148 149 150
void vlc_event_detach( vlc_event_manager_t *p_em,
                       vlc_event_type_t event_type,
                       vlc_event_callback_t pf_callback,
                       void *p_user_data )
151
{
152
    vlc_event_listeners_group_t *slot = &p_em->events[event_type];
153
    struct vlc_event_listener_t * listener;
154

155
    vlc_mutex_lock( &p_em->lock );
156 157 158 159

    FOREACH_ARRAY( listener, slot->listeners )
        if( listener->pf_callback == pf_callback &&
            listener->p_user_data == p_user_data )
160
        {
161 162 163 164
            /* that's our listener */
            ARRAY_REMOVE( slot->listeners,
                          fe_idx /* This comes from the macro (and that's why
                                    I hate macro) */ );
165
            vlc_mutex_unlock( &p_em->lock );
166 167
            free( listener );
            return;
168 169 170
        }
    FOREACH_END()

171
    vlc_assert_unreachable();
172
}