Contribs: update libbluray to 1.1.2
[vlc.git] / lib / event.c
blob28d3d0c23297f4c4ace6e1ff424a7d0c9dc2f49b
1 /*****************************************************************************
2 * event.c: New libvlc event control API
3 *****************************************************************************
4 * Copyright (C) 2007-2010 VLC authors and VideoLAN
6 * Authors: Filippo Carone <filippo@carone.org>
7 * Pierre d'Herbemont <pdherbemont # videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <errno.h>
31 #include <vlc/libvlc.h>
32 #include "libvlc_internal.h"
34 #include <vlc_common.h>
37 * Event Handling
40 /* Example usage
42 * struct libvlc_cool_object_t
43 * {
44 * ...
45 * libvlc_event_manager_t event_manager;
46 * ...
47 * }
49 * libvlc_my_cool_object_new()
50 * {
51 * ...
52 * libvlc_event_manager_init(&p_self->event_manager, p_self)
53 * ...
54 * }
56 * libvlc_my_cool_object_release()
57 * {
58 * ...
59 * libvlc_event_manager_release(&p_self->event_manager);
60 * ...
61 * }
63 * libvlc_my_cool_object_do_something()
64 * {
65 * ...
66 * libvlc_event_t event;
67 * event.type = libvlc_MyCoolObjectDidSomething;
68 * event.u.my_cool_object_did_something.what_it_did = kSomething;
69 * libvlc_event_send(&p_self->event_manager, &event);
70 * }
71 * */
73 typedef struct libvlc_event_listener_t
75 libvlc_event_type_t event_type;
76 void * p_user_data;
77 libvlc_callback_t pf_callback;
78 } libvlc_event_listener_t;
81 * Internal libvlc functions
84 void libvlc_event_manager_init(libvlc_event_manager_t *em, void *obj)
86 em->p_obj = obj;
87 vlc_array_init(&em->listeners);
88 vlc_mutex_init_recursive(&em->lock);
91 void libvlc_event_manager_destroy(libvlc_event_manager_t *em)
93 vlc_mutex_destroy(&em->lock);
95 for (size_t i = 0; i < vlc_array_count(&em->listeners); i++)
96 free(vlc_array_item_at_index(&em->listeners, i));
98 vlc_array_clear(&em->listeners);
101 /**************************************************************************
102 * libvlc_event_send (internal) :
104 * Send a callback.
105 **************************************************************************/
106 void libvlc_event_send( libvlc_event_manager_t * p_em,
107 libvlc_event_t * p_event )
109 /* Fill event with the sending object now */
110 p_event->p_obj = p_em->p_obj;
112 vlc_mutex_lock(&p_em->lock);
113 for (size_t i = 0; i < vlc_array_count(&p_em->listeners); i++)
115 libvlc_event_listener_t *listener;
117 listener = vlc_array_item_at_index(&p_em->listeners, i);
118 if (listener->event_type == p_event->type)
119 listener->pf_callback(p_event, listener->p_user_data);
121 vlc_mutex_unlock(&p_em->lock);
125 * Public libvlc functions
128 /**************************************************************************
129 * libvlc_event_attach (public) :
131 * Add a callback for an event.
132 **************************************************************************/
133 int libvlc_event_attach(libvlc_event_manager_t *em, libvlc_event_type_t type,
134 libvlc_callback_t callback, void *opaque)
136 libvlc_event_listener_t *listener = malloc(sizeof (*listener));
137 if (unlikely(listener == NULL))
138 return ENOMEM;
140 listener->event_type = type;
141 listener->p_user_data = opaque;
142 listener->pf_callback = callback;
144 int i_ret;
145 vlc_mutex_lock(&em->lock);
146 if(vlc_array_append(&em->listeners, listener) != 0)
148 i_ret = VLC_EGENERIC;
149 free(listener);
151 else
152 i_ret = VLC_SUCCESS;
153 vlc_mutex_unlock(&em->lock);
154 return i_ret;
157 /**************************************************************************
158 * libvlc_event_detach (public) :
160 * Remove a callback for an event.
161 **************************************************************************/
162 void libvlc_event_detach(libvlc_event_manager_t *em, libvlc_event_type_t type,
163 libvlc_callback_t callback, void *opaque)
165 vlc_mutex_lock(&em->lock);
166 for (size_t i = 0; i < vlc_array_count(&em->listeners); i++)
168 libvlc_event_listener_t *listener;
170 listener = vlc_array_item_at_index(&em->listeners, i);
172 if (listener->event_type == type
173 && listener->pf_callback == callback
174 && listener->p_user_data == opaque)
175 { /* that's our listener */
176 vlc_array_remove(&em->listeners, i);
177 vlc_mutex_unlock(&em->lock);
178 free(listener);
179 return;
182 abort();