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 /* Lock must be held */
94 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
96 if(!queue(p_em)->elements)
97 return false; /* No elements */
99 *listener = queue(p_em)->elements->listener;
100 *event = queue(p_em)->elements->event;
102 struct queue_elmt * elmt = queue(p_em)->elements;
103 queue(p_em)->elements = elmt->next;
108 /* Lock must be held */
109 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
111 struct queue_elmt * iter = queue(p_em)->elements;
112 struct queue_elmt * prev = NULL;
114 if(listeners_are_equal(&iter->listener, listener))
117 queue(p_em)->elements = iter->next;
119 prev->next = iter->next;
127 /**************************************************************************
128 * libvlc_event_async_fini (internal) :
130 * Destroy what might have been created by.
131 **************************************************************************/
133 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
135 if(!is_queue_initialized(p_em)) return;
137 vlc_thread_t thread = queue(p_em)->thread;
141 vlc_join(thread, NULL);
144 vlc_mutex_destroy(&queue(p_em)->lock);
145 vlc_cond_destroy(&queue(p_em)->signal);
147 struct queue_elmt * iter = queue(p_em)->elements;
149 struct queue_elmt * elemt_to_delete = iter;
151 free(elemt_to_delete);
157 /**************************************************************************
158 * libvlc_event_async_init (private) :
160 * Destroy what might have been created by.
161 **************************************************************************/
163 libvlc_event_async_init(libvlc_event_manager_t * p_em)
165 p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
167 int error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
170 free(p_em->async_event_queue);
171 p_em->async_event_queue = NULL;
175 vlc_mutex_init_recursive(&queue(p_em)->lock); // Beware, this is re-entrant
176 vlc_cond_init(&queue(p_em)->signal);
179 /**************************************************************************
180 * libvlc_event_async_ensure_listener_removal (internal) :
182 * Make sure no more message will be issued to the listener.
183 **************************************************************************/
185 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
187 if(!is_queue_initialized(p_em)) return;
189 vlc_mutex_lock(&queue(p_em)->lock);
190 pop_listener(p_em, listener);
191 vlc_mutex_unlock(&queue(p_em)->lock);
194 /**************************************************************************
195 * libvlc_event_async_dispatch (internal) :
197 * Send an event in an asynchronous way.
198 **************************************************************************/
200 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
202 // We do a lazy init here, to prevent constructing the thread when not needed.
203 vlc_mutex_lock(&p_em->object_lock);
205 libvlc_event_async_init(p_em);
206 vlc_mutex_unlock(&p_em->object_lock);
208 vlc_mutex_lock(&queue(p_em)->lock);
209 push(p_em, listener, event);
210 vlc_cond_signal(&queue(p_em)->signal);
211 vlc_mutex_unlock(&queue(p_em)->lock);
214 /**************************************************************************
215 * event_async_loop (private) :
217 * Send queued events.
218 **************************************************************************/
219 static void * event_async_loop(void * arg)
221 libvlc_event_manager_t * p_em = arg;
222 libvlc_event_listener_t listener;
223 libvlc_event_t event;
225 vlc_mutex_lock(&queue(p_em)->lock);
227 int has_listener = pop(p_em, &listener, &event);
229 mutex_cleanup_push(&queue(p_em)->lock);
232 listener.pf_callback( &event, listener.p_user_data ); // This might edit the queue, ->lock is recursive
234 vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
238 vlc_mutex_unlock(&queue(p_em)->lock);