1 /*****************************************************************************
2 * event.c: New libvlc event control API
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include <vlc/libvlc.h>
27 #include "libvlc_internal.h"
28 #include "event_internal.h"
31 libvlc_event_listener_t listener;
33 struct queue_elmt * next;
36 struct libvlc_event_async_queue {
37 struct queue_elmt * elements;
47 static void* event_async_loop(void * arg);
49 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
51 return p_em->async_event_queue;
54 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
56 return queue(p_em) != NULL;
59 /* Lock must be held */
60 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
63 static const long MaxQueuedItem = 300000;
67 struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
68 elmt->listener = *listener;
72 /* Append to the end of the queue */
73 struct queue_elmt * iter = queue(p_em)->elements;
76 queue(p_em)->elements = elmt;
83 if(count++ > MaxQueuedItem)
85 fprintf(stderr, "Warning: libvlc event overflow.\n");
93 static inline void queue_lock(libvlc_event_manager_t * p_em)
95 vlc_mutex_lock(&queue(p_em)->lock);
98 static inline void queue_unlock(libvlc_event_manager_t * p_em)
100 vlc_mutex_unlock(&queue(p_em)->lock);
103 /* Lock must be held */
104 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
106 if(!queue(p_em)->elements)
107 return false; /* No elements */
109 *listener = queue(p_em)->elements->listener;
110 *event = queue(p_em)->elements->event;
112 struct queue_elmt * elmt = queue(p_em)->elements;
113 queue(p_em)->elements = elmt->next;
118 /* Lock must be held */
119 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
121 struct queue_elmt * iter = queue(p_em)->elements;
122 struct queue_elmt * prev = NULL;
124 if(listeners_are_equal(&iter->listener, listener))
127 queue(p_em)->elements = iter->next;
129 prev->next = iter->next;
137 /**************************************************************************
138 * libvlc_event_async_fini (internal) :
140 * Destroy what might have been created by.
141 **************************************************************************/
143 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
145 if(!is_queue_initialized(p_em)) return;
147 vlc_thread_t thread = queue(p_em)->thread;
151 vlc_join(thread, NULL);
154 vlc_mutex_destroy(&queue(p_em)->lock);
155 vlc_cond_destroy(&queue(p_em)->signal);
157 struct queue_elmt * iter = queue(p_em)->elements;
159 struct queue_elmt * elemt_to_delete = iter;
161 free(elemt_to_delete);
167 /**************************************************************************
168 * libvlc_event_async_init (private) :
170 * Destroy what might have been created by.
171 **************************************************************************/
173 libvlc_event_async_init(libvlc_event_manager_t * p_em)
175 p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
177 int error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
180 free(p_em->async_event_queue);
181 p_em->async_event_queue = NULL;
185 vlc_mutex_init(&queue(p_em)->lock);
186 vlc_cond_init(&queue(p_em)->signal);
189 /**************************************************************************
190 * libvlc_event_async_ensure_listener_removal (internal) :
192 * Make sure no more message will be issued to the listener.
193 **************************************************************************/
195 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
197 if(!is_queue_initialized(p_em)) return;
200 pop_listener(p_em, listener);
204 /**************************************************************************
205 * libvlc_event_async_dispatch (internal) :
207 * Send an event in an asynchronous way.
208 **************************************************************************/
210 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
212 // We do a lazy init here, to prevent constructing the thread when not needed.
213 vlc_mutex_lock(&p_em->object_lock);
215 libvlc_event_async_init(p_em);
216 vlc_mutex_unlock(&p_em->object_lock);
219 push(p_em, listener, event);
220 vlc_cond_signal(&queue(p_em)->signal);
224 /**************************************************************************
225 * event_async_loop (private) :
227 * Send queued events.
228 **************************************************************************/
229 static void * event_async_loop(void * arg)
231 libvlc_event_manager_t * p_em = arg;
232 libvlc_event_listener_t listener;
233 libvlc_event_t event;
237 int has_listener = pop(p_em, &listener, &event);
242 listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
247 mutex_cleanup_push(&queue(p_em)->lock);
248 vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);