vlc_events.h 8.21 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * vlc_events.h: events definitions
3 4
 * Interface used to send events.
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
5
 * Copyright (C) 2007 VLC authors and VideoLAN
6 7 8 9
 * $Id$
 *
 * Authors: Pierre d'Herbemont
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * 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
13 14 15 16
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23 24 25 26 27 28
 *****************************************************************************/

#ifndef VLC_EVENTS_H
# define VLC_EVENTS_H

#include <vlc_arrays.h>
29
#include <vlc_meta.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

/**
 * \file
 * This file is the interface definition for events
 * (implementation in src/misc/events.c)
 */

/*****************************************************************************
 * Documentation
 *****************************************************************************/
/*
 **** Background
 *
 * This implements a way to send and receive event for an object (which can be
 * a simple C struct or less).
45
 *
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 * This is in direct concurrency with the Variable based Callback
 * (see src/misc/variables.c).
 *
 * It has the following advantages over Variable based Callback:
 * - No need to implement the whole VLC_COMMON_MEMBERS in the object,
 * thus it reduce it size. This is especially true for input_item_t which
 * doesn't have VLC_COMMON_MEMBERS. This is the first reason of existence of
 * this implementation.
 * - Libvlc can easily be based upon that.
 * - Existing event are clearly declared (in include/vlc_events.h)
 *
 *
 **** Example usage
 *
 * (vlc_cool_object_t doesn't need to have the VLC_COMMON_MEMBERS.)
 *
62
 * struct vlc_cool_object_t
63 64 65 66 67 68 69 70 71
 * {
 *        ...
 *        vlc_event_manager_t p_event_manager;
 *        ...
 * }
 *
 * vlc_my_cool_object_new()
 * {
 *        ...
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
72
 *        vlc_event_manager_init( &p_self->p_event_manager, p_self, p_a_libvlc_object );
73 74 75 76 77 78 79 80
 *        vlc_event_manager_register_event_type(p_self->p_event_manager,
 *                vlc_MyCoolObjectDidSomething, p_e)
 *        ...
 * }
 *
 * vlc_my_cool_object_release()
 * {
 *         ...
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
81
 *         vlc_event_manager_fini( &p_self->p_event_manager );
82 83 84 85 86 87 88 89 90
 *         ...
 * }
 *
 * vlc_my_cool_object_do_something()
 * {
 *        ...
 *        vlc_event_t event;
 *        event.type = vlc_MyCoolObjectDidSomething;
 *        event.u.my_cool_object_did_something.what_it_did = kSomething;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
91
 *        vlc_event_send( &p_self->p_event_manager, &event );
92 93
 * }
 * */
94

95 96 97 98 99 100 101 102 103 104 105
  /*****************************************************************************
 * Event Type
 *****************************************************************************/

/* Private structure defined in misc/events.c */
struct vlc_event_listeners_group_t;

/* Event manager type */
typedef struct vlc_event_manager_t
{
    void * p_obj;
106
    vlc_mutex_t object_lock;
107
    vlc_mutex_t event_sending_lock;
108 109 110 111 112
    DECL_ARRAY(struct vlc_event_listeners_group_t *) listeners_groups;
} vlc_event_manager_t;

/* List of event */
typedef enum vlc_event_type_t {
113
    /* Input item events */
114
    vlc_InputItemMetaChanged,
115
    vlc_InputItemSubItemAdded,
116
    vlc_InputItemSubItemTreeAdded,
117 118
    vlc_InputItemDurationChanged,
    vlc_InputItemPreparsedChanged,
119 120
    vlc_InputItemNameChanged,
    vlc_InputItemInfoChanged,
121
    vlc_InputItemErrorWhenReadingChanged,
122
    vlc_InputItemPreparseEnded,
123 124 125

    /* Service Discovery event */
    vlc_ServicesDiscoveryItemAdded,
126
    vlc_ServicesDiscoveryItemRemoved,
127
    vlc_ServicesDiscoveryItemRemoveAll,
128
    vlc_ServicesDiscoveryStarted,
129 130
    vlc_ServicesDiscoveryEnded,

131 132 133 134
    /* Renderer Discovery events */
    vlc_RendererDiscoveryItemAdded,
    vlc_RendererDiscoveryItemRemoved,

135 136 137 138
    /* Addons Manager events */
    vlc_AddonFound,
    vlc_AddonsDiscoveryEnded,
    vlc_AddonChanged
139 140 141 142 143 144
} vlc_event_type_t;

/* Event definition */
typedef struct vlc_event_t
{
    vlc_event_type_t type;
145
    void * p_obj; /* Sender object, automatically filled by vlc_event_send() */
146
    union vlc_event_type_specific
147
    {
148
        /* Input item events */
149
        struct vlc_input_item_meta_changed
150
        {
151
            vlc_meta_type_t meta_type;
152
        } input_item_meta_changed;
153 154 155 156
        struct vlc_input_item_subitem_added
        {
            input_item_t * p_new_child;
        } input_item_subitem_added;
157 158 159 160
        struct vlc_input_item_subitem_tree_added
        {
            input_item_node_t * p_root;
        } input_item_subitem_tree_added;
161 162 163 164 165 166 167 168
        struct vlc_input_item_duration_changed
        {
            mtime_t new_duration;
        } input_item_duration_changed;
        struct vlc_input_item_preparsed_changed
        {
            int new_status;
        } input_item_preparsed_changed;
169 170 171 172 173 174 175 176
        struct vlc_input_item_name_changed
        {
            const char * new_name;
        } input_item_name_changed;
        struct vlc_input_item_info_changed
        {
            void * unused;
        } input_item_info_changed;
177 178 179 180
        struct input_item_error_when_reading_changed
        {
            bool new_value;
        } input_item_error_when_reading_changed;
181 182 183 184
        struct input_item_preparse_ended
        {
            int new_status;
        } input_item_preparse_ended;
185

186 187 188 189 190 191 192 193 194 195
        /* Service discovery events */
        struct vlc_services_discovery_item_added
        {
            input_item_t * p_new_item;
            const char * psz_category;
        } services_discovery_item_added;
        struct vlc_services_discovery_item_removed
        {
            input_item_t * p_item;
        } services_discovery_item_removed;
196 197 198 199 200 201 202 203
        struct vlc_services_discovery_started
        {
            void * unused;
        } services_discovery_started;
        struct vlc_services_discovery_ended
        {
            void * unused;
        } services_discovery_ended;
204

205 206 207
        /* Renderer discovery events */
        struct vlc_renderer_discovery_item_added
        {
Thomas Guillem's avatar
Thomas Guillem committed
208
            vlc_renderer_item_t * p_new_item;
209 210 211
        } renderer_discovery_item_added;
        struct vlc_renderer_discovery_item_removed
        {
Thomas Guillem's avatar
Thomas Guillem committed
212
            vlc_renderer_item_t * p_item;
213 214
        } renderer_discovery_item_removed;

215 216 217 218 219
        /* Addons */
        struct vlc_addon_generic_event
        {
            addon_entry_t * p_entry;
        } addon_generic_event;
220 221 222 223 224 225 226 227 228 229 230 231 232 233
    } u;
} vlc_event_t;

/* Event callback type */
typedef void ( *vlc_event_callback_t )( const vlc_event_t *, void * );

 /*****************************************************************************
 * Event manager
 *****************************************************************************/

/*
 * p_obj points to the object that owns the event manager, and from
 * which events are sent
 */
234
VLC_API int vlc_event_manager_init( vlc_event_manager_t * p_em, void * p_obj );
235 236 237 238

/*
 * Destroy
 */
239
VLC_API void vlc_event_manager_fini( vlc_event_manager_t * p_em );
240 241 242 243

/*
 * Tells a specific event manager that it will handle event_type object
 */
244 245
VLC_API int vlc_event_manager_register_event_type( vlc_event_manager_t * p_em,
                                                   vlc_event_type_t );
246 247 248 249

/*
 * Send an event to the listener attached to this p_em.
 */
250
VLC_API void vlc_event_send( vlc_event_manager_t * p_em, vlc_event_t * );
251 252 253 254

/*
 * Add a callback for an event.
 */
255 256 257
VLC_API int vlc_event_attach( vlc_event_manager_t * p_event_manager,
                              vlc_event_type_t event_type,
                              vlc_event_callback_t pf_callback,
258
                              void *p_user_data );
259 260 261 262

/*
 * Remove a callback for an event.
 */
263 264 265 266
VLC_API void vlc_event_detach( vlc_event_manager_t *p_event_manager,
                               vlc_event_type_t event_type,
                               vlc_event_callback_t pf_callback,
                               void *p_user_data );
267 268

#endif /* VLC_EVENTS_H */