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 *****************************************************************************/
31 #include <vlc/libvlc.h>
32 #include "libvlc_internal.h"
34 #include <vlc_common.h>
42 * struct libvlc_cool_object_t
45 * libvlc_event_manager_t event_manager;
49 * libvlc_my_cool_object_new()
52 * libvlc_event_manager_init(&p_self->event_manager, p_self)
56 * libvlc_my_cool_object_release()
59 * libvlc_event_manager_release(&p_self->event_manager);
63 * libvlc_my_cool_object_do_something()
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);
73 typedef struct libvlc_event_listener_t
75 libvlc_event_type_t event_type
;
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
)
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 for (size_t i
= 0; i
< vlc_array_count(&em
->listeners
); i
++)
94 free(vlc_array_item_at_index(&em
->listeners
, i
));
96 vlc_array_clear(&em
->listeners
);
99 /**************************************************************************
100 * libvlc_event_send (internal) :
103 **************************************************************************/
104 void libvlc_event_send( libvlc_event_manager_t
* p_em
,
105 libvlc_event_t
* p_event
)
107 /* Fill event with the sending object now */
108 p_event
->p_obj
= p_em
->p_obj
;
110 vlc_mutex_lock(&p_em
->lock
);
111 for (size_t i
= 0; i
< vlc_array_count(&p_em
->listeners
); i
++)
113 libvlc_event_listener_t
*listener
;
115 listener
= vlc_array_item_at_index(&p_em
->listeners
, i
);
116 if (listener
->event_type
== p_event
->type
)
117 listener
->pf_callback(p_event
, listener
->p_user_data
);
119 vlc_mutex_unlock(&p_em
->lock
);
123 * Public libvlc functions
126 /**************************************************************************
127 * libvlc_event_attach (public) :
129 * Add a callback for an event.
130 **************************************************************************/
131 int libvlc_event_attach(libvlc_event_manager_t
*em
, libvlc_event_type_t type
,
132 libvlc_callback_t callback
, void *opaque
)
134 libvlc_event_listener_t
*listener
= malloc(sizeof (*listener
));
135 if (unlikely(listener
== NULL
))
138 listener
->event_type
= type
;
139 listener
->p_user_data
= opaque
;
140 listener
->pf_callback
= callback
;
143 vlc_mutex_lock(&em
->lock
);
144 if(vlc_array_append(&em
->listeners
, listener
) != 0)
146 i_ret
= VLC_EGENERIC
;
151 vlc_mutex_unlock(&em
->lock
);
155 /**************************************************************************
156 * libvlc_event_detach (public) :
158 * Remove a callback for an event.
159 **************************************************************************/
160 void libvlc_event_detach(libvlc_event_manager_t
*em
, libvlc_event_type_t type
,
161 libvlc_callback_t callback
, void *opaque
)
163 vlc_mutex_lock(&em
->lock
);
164 for (size_t i
= 0; i
< vlc_array_count(&em
->listeners
); i
++)
166 libvlc_event_listener_t
*listener
;
168 listener
= vlc_array_item_at_index(&em
->listeners
, i
);
170 if (listener
->event_type
== type
171 && listener
->pf_callback
== callback
172 && listener
->p_user_data
== opaque
)
173 { /* that's our listener */
174 vlc_array_remove(&em
->listeners
, i
);
175 vlc_mutex_unlock(&em
->lock
);