1 /*****************************************************************************
2  * vlc_events.h: events definitions
3  * Interface used to send events.
4  *****************************************************************************
5  * Copyright (C) 2007 VLC authors and VideoLAN
6  * $Id: 229f4f677029763a837558932dad3e47b283e61c $
7  *
8  * Authors: Pierre d'Herbemont
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #ifndef VLC_EVENTS_H
26 # define VLC_EVENTS_H
27 
28 #include <vlc_arrays.h>
29 #include <vlc_meta.h>
30 
31 /**
32  * \file
33  * This file is the interface definition for events
34  * (implementation in src/misc/events.c)
35  */
36 
37 /*****************************************************************************
38  * Documentation
39  *****************************************************************************/
40 /*
41  **** Background
42  *
43  * This implements a way to send and receive event for an object (which can be
44  * a simple C struct or less).
45  *
46  * This is in direct concurrency with the Variable based Callback
47  * (see src/misc/variables.c).
48  *
49  * It has the following advantages over Variable based Callback:
50  * - No need to implement the whole VLC_COMMON_MEMBERS in the object,
51  * thus it reduce it size. This is especially true for input_item_t which
52  * doesn't have VLC_COMMON_MEMBERS. This is the first reason of existence of
53  * this implementation.
54  * - Libvlc can easily be based upon that.
55  * - Existing event are clearly declared (in include/vlc_events.h)
56  *
57  *
58  **** Example usage
59  *
60  * (vlc_cool_object_t doesn't need to have the VLC_COMMON_MEMBERS.)
61  *
62  * struct vlc_cool_object_t
63  * {
64  *        ...
65  *        vlc_event_manager_t p_event_manager;
66  *        ...
67  * }
68  *
69  * vlc_my_cool_object_new()
70  * {
71  *        ...
72  *        vlc_event_manager_init( &p_self->p_event_manager, p_self, p_a_libvlc_object );
73  *        ...
74  * }
75  *
76  * vlc_my_cool_object_release()
77  * {
78  *         ...
79  *         vlc_event_manager_fini( &p_self->p_event_manager );
80  *         ...
81  * }
82  *
83  * vlc_my_cool_object_do_something()
84  * {
85  *        ...
86  *        vlc_event_t event;
87  *        event.type = vlc_MyCoolObjectDidSomething;
88  *        event.u.my_cool_object_did_something.what_it_did = kSomething;
89  *        vlc_event_send( &p_self->p_event_manager, &event );
90  * }
91  * */
92 
93   /*****************************************************************************
94  * Event Type
95  *****************************************************************************/
96 
97 /* List of event */
98 typedef enum vlc_event_type_t {
99     /* Input item events */
100     vlc_InputItemMetaChanged,
101     vlc_InputItemSubItemTreeAdded,
102     vlc_InputItemDurationChanged,
103     vlc_InputItemPreparsedChanged,
104     vlc_InputItemNameChanged,
105     vlc_InputItemInfoChanged,
106     vlc_InputItemErrorWhenReadingChanged,
107     vlc_InputItemPreparseEnded,
108 } vlc_event_type_t;
109 
110 typedef struct vlc_event_listeners_group_t
111 {
112     DECL_ARRAY(struct vlc_event_listener_t *) listeners;
113 } vlc_event_listeners_group_t;
114 
115 /* Event manager type */
116 typedef struct vlc_event_manager_t
117 {
118     void * p_obj;
119     vlc_mutex_t lock;
120     vlc_event_listeners_group_t events[vlc_InputItemPreparseEnded + 1];
121 } vlc_event_manager_t;
122 
123 /* Event definition */
124 typedef struct vlc_event_t
125 {
126     vlc_event_type_t type;
127     void * p_obj; /* Sender object, automatically filled by vlc_event_send() */
128     union vlc_event_type_specific
129     {
130         /* Input item events */
131         struct vlc_input_item_meta_changed
132         {
133             vlc_meta_type_t meta_type;
134         } input_item_meta_changed;
135         struct vlc_input_item_subitem_added
136         {
137             input_item_t * p_new_child;
138         } input_item_subitem_added;
139         struct vlc_input_item_subitem_tree_added
140         {
141             input_item_node_t * p_root;
142         } input_item_subitem_tree_added;
143         struct vlc_input_item_duration_changed
144         {
145             mtime_t new_duration;
146         } input_item_duration_changed;
147         struct vlc_input_item_preparsed_changed
148         {
149             int new_status;
150         } input_item_preparsed_changed;
151         struct vlc_input_item_name_changed
152         {
153             const char * new_name;
154         } input_item_name_changed;
155         struct vlc_input_item_info_changed
156         {
157             void * unused;
158         } input_item_info_changed;
159         struct input_item_error_when_reading_changed
160         {
161             bool new_value;
162         } input_item_error_when_reading_changed;
163         struct input_item_preparse_ended
164         {
165             int new_status;
166         } input_item_preparse_ended;
167     } u;
168 } vlc_event_t;
169 
170 /* Event callback type */
171 typedef void ( *vlc_event_callback_t )( const vlc_event_t *, void * );
172 
173  /*****************************************************************************
174  * Event manager
175  *****************************************************************************/
176 
177 /*
178  * p_obj points to the object that owns the event manager, and from
179  * which events are sent
180  */
181 void vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj );
182 
183 /*
184  * Destroy
185  */
186 void vlc_event_manager_fini( vlc_event_manager_t * p_em );
187 
188 /*
189  * Send an event to the listener attached to this p_em.
190  */
191 void vlc_event_send( vlc_event_manager_t * p_em, vlc_event_t * );
192 
193 /*
194  * Add a callback for an event.
195  */
196 VLC_API int vlc_event_attach( vlc_event_manager_t * p_event_manager,
197                               vlc_event_type_t event_type,
198                               vlc_event_callback_t pf_callback,
199                               void *p_user_data );
200 
201 /*
202  * Remove a callback for an event.
203  */
204 VLC_API void vlc_event_detach( vlc_event_manager_t *p_event_manager,
205                                vlc_event_type_t event_type,
206                                vlc_event_callback_t pf_callback,
207                                void *p_user_data );
208 
209 #endif /* VLC_EVENTS_H */
210