forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.c
303 lines (265 loc) · 8.46 KB
/
event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*****************************************************************************
* event.c: New libvlc event control API
*****************************************************************************
* Copyright (C) 2007-2010 VLC authors and VideoLAN
* $Id $
*
* Authors: Filippo Carone <[email protected]>
* Pierre d'Herbemont <pdherbemont # videolan.org>
*
* 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
* (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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* 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.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <vlc/libvlc.h>
#include "libvlc_internal.h"
#include <vlc_common.h>
/*
* Event Handling
*/
/* Example usage
*
* struct libvlc_cool_object_t
* {
* ...
* libvlc_event_manager_t * p_event_manager;
* ...
* }
*
* libvlc_my_cool_object_new()
* {
* ...
* p_self->p_event_manager = libvlc_event_manager_new( p_self )
* ...
* }
*
* libvlc_my_cool_object_release()
* {
* ...
* libvlc_event_manager_release( p_self->p_event_manager );
* ...
* }
*
* libvlc_my_cool_object_do_something()
* {
* ...
* libvlc_event_t event;
* event.type = libvlc_MyCoolObjectDidSomething;
* event.u.my_cool_object_did_something.what_it_did = kSomething;
* libvlc_event_send( p_self->p_event_manager, &event );
* }
* */
typedef struct libvlc_event_listener_t
{
libvlc_event_type_t event_type;
void * p_user_data;
libvlc_callback_t pf_callback;
} libvlc_event_listener_t;
typedef struct libvlc_event_manager_t
{
void * p_obj;
vlc_array_t listeners;
vlc_mutex_t lock;
} libvlc_event_sender_t;
/*
* Internal libvlc functions
*/
/**************************************************************************
* libvlc_event_manager_new (internal) :
*
* Init an object's event manager.
**************************************************************************/
libvlc_event_manager_t *
libvlc_event_manager_new( void * p_obj )
{
libvlc_event_manager_t * p_em;
p_em = malloc(sizeof( libvlc_event_manager_t ));
if( !p_em )
{
libvlc_printerr( "Not enough memory" );
return NULL;
}
p_em->p_obj = p_obj;
vlc_array_init(&p_em->listeners);
vlc_mutex_init_recursive(&p_em->lock);
return p_em;
}
/**************************************************************************
* libvlc_event_manager_release (internal) :
*
* Release an object's event manager.
**************************************************************************/
void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
{
vlc_mutex_destroy(&p_em->lock);
for (int i = 0; i < vlc_array_count(&p_em->listeners); i++)
free(vlc_array_item_at_index(&p_em->listeners, i));
vlc_array_clear(&p_em->listeners);
free( p_em );
}
/**************************************************************************
* libvlc_event_send (internal) :
*
* Send a callback.
**************************************************************************/
void libvlc_event_send( libvlc_event_manager_t * p_em,
libvlc_event_t * p_event )
{
/* Fill event with the sending object now */
p_event->p_obj = p_em->p_obj;
vlc_mutex_lock(&p_em->lock);
for (int i = 0; i < vlc_array_count(&p_em->listeners); i++)
{
libvlc_event_listener_t *listener;
listener = vlc_array_item_at_index(&p_em->listeners, i);
if (listener->event_type == p_event->type)
listener->pf_callback(p_event, listener->p_user_data);
}
vlc_mutex_unlock(&p_em->lock);
}
/*
* Public libvlc functions
*/
#define DEF( a ) { libvlc_##a, #a, },
typedef struct
{
int type;
const char name[40];
} event_name_t;
static const event_name_t event_list[] = {
DEF(MediaMetaChanged)
DEF(MediaSubItemAdded)
DEF(MediaDurationChanged)
DEF(MediaParsedChanged)
DEF(MediaFreed)
DEF(MediaStateChanged)
DEF(MediaSubItemTreeAdded)
DEF(MediaPlayerMediaChanged)
DEF(MediaPlayerNothingSpecial)
DEF(MediaPlayerOpening)
DEF(MediaPlayerBuffering)
DEF(MediaPlayerPlaying)
DEF(MediaPlayerPaused)
DEF(MediaPlayerStopped)
DEF(MediaPlayerForward)
DEF(MediaPlayerBackward)
DEF(MediaPlayerEndReached)
DEF(MediaPlayerEncounteredError)
DEF(MediaPlayerTimeChanged)
DEF(MediaPlayerPositionChanged)
DEF(MediaPlayerSeekableChanged)
DEF(MediaPlayerPausableChanged)
DEF(MediaPlayerTitleChanged)
DEF(MediaPlayerSnapshotTaken)
DEF(MediaPlayerLengthChanged)
DEF(MediaPlayerVout)
DEF(MediaPlayerScrambledChanged)
DEF(MediaPlayerESAdded)
DEF(MediaPlayerESDeleted)
DEF(MediaPlayerESSelected)
DEF(MediaPlayerCorked)
DEF(MediaPlayerUncorked)
DEF(MediaPlayerMuted)
DEF(MediaPlayerUnmuted)
DEF(MediaPlayerAudioVolume)
DEF(MediaPlayerAudioDevice)
DEF(MediaPlayerChapterChanged)
DEF(MediaListItemAdded)
DEF(MediaListWillAddItem)
DEF(MediaListItemDeleted)
DEF(MediaListWillDeleteItem)
DEF(MediaListEndReached)
DEF(MediaListViewItemAdded)
DEF(MediaListViewWillAddItem)
DEF(MediaListViewItemDeleted)
DEF(MediaListViewWillDeleteItem)
DEF(MediaListPlayerPlayed)
DEF(MediaListPlayerNextItemSet)
DEF(MediaListPlayerStopped)
DEF(MediaDiscovererStarted)
DEF(MediaDiscovererEnded)
DEF(VlmMediaAdded)
DEF(VlmMediaRemoved)
DEF(VlmMediaChanged)
DEF(VlmMediaInstanceStarted)
DEF(VlmMediaInstanceStopped)
DEF(VlmMediaInstanceStatusInit)
DEF(VlmMediaInstanceStatusOpening)
DEF(VlmMediaInstanceStatusPlaying)
DEF(VlmMediaInstanceStatusPause)
DEF(VlmMediaInstanceStatusEnd)
DEF(VlmMediaInstanceStatusError)
};
#undef DEF
static const char unknown_event_name[] = "Unknown Event";
static int evcmp( const void *a, const void *b )
{
return (*(const int *)a) - ((event_name_t *)b)->type;
}
const char * libvlc_event_type_name( int event_type )
{
const event_name_t *p;
p = bsearch( &event_type, event_list,
sizeof(event_list)/sizeof(event_list[0]), sizeof(*p),
evcmp );
return p ? p->name : unknown_event_name;
}
/**************************************************************************
* libvlc_event_attach (public) :
*
* Add a callback for an event.
**************************************************************************/
int libvlc_event_attach(libvlc_event_manager_t *em, libvlc_event_type_t type,
libvlc_callback_t callback, void *opaque)
{
libvlc_event_listener_t *listener = malloc(sizeof (*listener));
if (unlikely(listener == NULL))
return ENOMEM;
listener->event_type = type;
listener->p_user_data = opaque;
listener->pf_callback = callback;
vlc_mutex_lock(&em->lock);
vlc_array_append(&em->listeners, listener);
vlc_mutex_unlock(&em->lock);
return 0;
}
/**************************************************************************
* libvlc_event_detach (public) :
*
* Remove a callback for an event.
**************************************************************************/
void libvlc_event_detach(libvlc_event_manager_t *em, libvlc_event_type_t type,
libvlc_callback_t callback, void *opaque)
{
vlc_mutex_lock(&em->lock);
for (int i = 0; i < vlc_array_count(&em->listeners); i++)
{
libvlc_event_listener_t *listener;
listener = vlc_array_item_at_index(&em->listeners, i);
if (listener->event_type == type
&& listener->pf_callback == callback
&& listener->p_user_data == opaque)
{ /* that's our listener */
vlc_array_remove(&em->listeners, i);
vlc_mutex_unlock(&em->lock);
free(listener);
return;
}
}
abort();
}