]> git.sesse.net Git - vlc/blob - include/vlc_events.h
misc/events.c: Fix event sending, by properly supporting event_manager edition (event...
[vlc] / include / vlc_events.h
1 /*****************************************************************************
2  * events.h: events definitions
3  * Interface used to send events.
4  *****************************************************************************
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont
9  *
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.
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 General Public License for more details.
19  *
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  *****************************************************************************/
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_Create( &p_self->p_event_manager, p_this );
73  *        vlc_event_manager_register_event_type(p_self->p_event_manager,
74  *                vlc_MyCoolObjectDidSomething, p_e)
75  *        ...
76  * }
77  *
78  * vlc_my_cool_object_release()
79  * {
80  *         ...
81  *         vlc_event_manager_release( p_self->p_event_manager );
82  *         ...
83  * }
84  *
85  * vlc_my_cool_object_do_something()
86  * {
87  *        ...
88  *        vlc_event_t event;
89  *        event.type = vlc_MyCoolObjectDidSomething;
90  *        event.u.my_cool_object_did_something.what_it_did = kSomething;
91  *        vlc_event_send( p_self->p_event_manager, &event );
92  * }
93  * */
94  
95   /*****************************************************************************
96  * Event Type
97  *****************************************************************************/
98
99 /* Private structure defined in misc/events.c */
100 struct vlc_event_listeners_group_t;
101
102 /* Event manager type */
103 typedef struct vlc_event_manager_t
104 {
105     void * p_obj;
106     vlc_mutex_t object_lock;
107     vlc_mutex_t event_sending_lock;
108     vlc_object_t *p_parent_object;
109     DECL_ARRAY(struct vlc_event_listeners_group_t *) listeners_groups;
110 } vlc_event_manager_t;
111
112 /* List of event */
113 /* Be sure to keep sync-ed with misc/events.c debug name table */
114 typedef enum vlc_event_type_t {
115     /* Input item events */
116     vlc_InputItemMetaChanged,
117     vlc_InputItemSubItemAdded,
118     vlc_InputItemDurationChanged,
119     vlc_InputItemPreparsedChanged,
120
121     /* Service Discovery event */
122     vlc_ServicesDiscoveryItemAdded,
123     vlc_ServicesDiscoveryItemRemoved,
124     vlc_ServicesDiscoveryStarted,
125     vlc_ServicesDiscoveryEnded
126 } vlc_event_type_t;
127
128 /* Event definition */
129 typedef struct vlc_event_t
130 {
131     vlc_event_type_t type;
132     void * p_obj; /* Sender object, automatically filled by vlc_event_send() */
133     union vlc_event_type_specific
134     {
135         /* Input item events */
136         struct vlc_input_item_meta_changed
137         {
138             vlc_meta_type_t meta_type;
139         } input_item_meta_changed;
140         struct vlc_input_item_subitem_added
141         {
142             input_item_t * p_new_child;
143             vlc_bool_t b_node;
144         } input_item_subitem_added;
145         struct vlc_input_item_duration_changed
146         {
147             mtime_t new_duration;
148         } input_item_duration_changed;
149         struct vlc_input_item_preparsed_changed
150         {
151             int new_status;
152         } input_item_preparsed_changed;
153
154         /* Service discovery events */
155         struct vlc_services_discovery_item_added
156         {
157             input_item_t * p_new_item;
158             const char * psz_category;
159         } services_discovery_item_added;
160         struct vlc_services_discovery_item_removed
161         {
162             input_item_t * p_item;
163         } services_discovery_item_removed;
164         struct vlc_services_discovery_started
165         {
166             void * unused;
167         } services_discovery_started;
168         struct vlc_services_discovery_ended
169         {
170             void * unused;
171         } services_discovery_ended;
172
173     } u;
174 } vlc_event_t;
175
176 /* Event callback type */
177 typedef void ( *vlc_event_callback_t )( const vlc_event_t *, void * );
178
179  /*****************************************************************************
180  * Event manager
181  *****************************************************************************/
182
183 /*
184  * p_obj points to the object that owns the event manager, and from
185  * which events are sent
186  * p_obj is here to give us a libvlc instance
187  */
188 VLC_EXPORT(int, vlc_event_manager_init, ( vlc_event_manager_t * p_em,
189                                           void * p_obj, vlc_object_t * ));
190
191 /*
192  * Destroy
193  */
194 VLC_EXPORT(void, vlc_event_manager_fini, ( vlc_event_manager_t * p_em ));
195
196 /*
197  * Tells a specific event manager that it will handle event_type object
198  */
199 VLC_EXPORT(int, vlc_event_manager_register_event_type,
200                 ( vlc_event_manager_t * p_em, vlc_event_type_t event_type ));
201
202 /*
203  * Send an event to the listener attached to this p_em.
204  */
205 VLC_EXPORT(void, vlc_event_send, ( vlc_event_manager_t * p_em,
206                                    vlc_event_t * p_event ));
207
208 /*
209  * Add a callback for an event.
210  */
211 #define vlc_event_attach(a, b, c, d) __vlc_event_attach(a, b, c, d, #c)
212 VLC_EXPORT(int, __vlc_event_attach, ( vlc_event_manager_t * p_event_manager,
213                                       vlc_event_type_t event_type,
214                                       vlc_event_callback_t pf_callback,
215                                       void *p_user_data,
216                                       const char * psz_debug_name ));
217
218 /*
219  * Remove a callback for an event.
220  */
221 VLC_EXPORT(int, vlc_event_detach, ( vlc_event_manager_t *p_event_manager,
222                                     vlc_event_type_t event_type,
223                                     vlc_event_callback_t pf_callback,
224                                     void *p_user_data ));
225
226 #endif /* VLC_EVENTS_H */