vlc_events.h 6.46 KB
Newer Older
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
/*****************************************************************************
 * events.h: events definitions
 * Interface used to send events.
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/

#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 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
 * {
 *        ...
 *        vlc_event_manager_t p_event_manager;
 *        ...
 * }
 *
 * vlc_my_cool_object_new()
 * {
 *        ...
 *        vlc_event_manager_Create( &p_self->p_event_manager, p_this );
 *        vlc_event_manager_register_event_type(p_self->p_event_manager,
 *                vlc_MyCoolObjectDidSomething, p_e)
 *        ...
 * }
 *
 * vlc_my_cool_object_release()
 * {
 *         ...
 *         vlc_event_manager_release( p_self->p_event_manager );
 *         ...
 * }
 *
 * 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;
 *        vlc_event_send( p_self->p_event_manager, &event );
 * }
 * */
 
  /*****************************************************************************
 * 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_object_t *p_parent_object;
108 109 110 111
    DECL_ARRAY(struct vlc_event_listeners_group_t *) listeners_groups;
} vlc_event_manager_t;

/* List of event */
112
/* Be sure to keep sync-ed with misc/events.c debug name table */
113
typedef enum vlc_event_type_t {
114
    /* Input item events */
115
    vlc_InputItemMetaChanged,
116
    vlc_InputItemSubItemAdded,
117 118 119 120

    /* Service Discovery event */
    vlc_ServicesDiscoveryItemAdded,
    vlc_ServicesDiscoveryItemRemoved
121 122 123 124 125 126
} vlc_event_type_t;

/* Event definition */
typedef struct vlc_event_t
{
    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 136 137 138
        struct vlc_input_item_subitem_added
        {
            input_item_t * p_new_child;
        } input_item_subitem_added;
139
 
140 141 142 143 144 145 146 147 148 149 150
        /* 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;

151 152 153 154 155 156 157 158 159 160 161 162 163
    } 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
164
 * p_obj is here to give us a libvlc instance
165
 */
166
VLC_EXPORT(int, vlc_event_manager_init, ( vlc_event_manager_t * p_em,
167
                                          void * p_obj, vlc_object_t * ));
168 169 170 171

/*
 * Destroy
 */
172
VLC_EXPORT(void, vlc_event_manager_fini, ( vlc_event_manager_t * p_em ));
173 174 175 176

/*
 * Tells a specific event manager that it will handle event_type object
 */
177 178
VLC_EXPORT(int, vlc_event_manager_register_event_type,
                ( vlc_event_manager_t * p_em, vlc_event_type_t event_type ));
179 180 181 182

/*
 * Send an event to the listener attached to this p_em.
 */
183 184
VLC_EXPORT(void, vlc_event_send, ( vlc_event_manager_t * p_em,
                                   vlc_event_t * p_event ));
185 186 187 188

/*
 * Add a callback for an event.
 */
189 190 191 192 193 194
#define vlc_event_attach(a, b, c, d) __vlc_event_attach(a, b, c, d, #c)
VLC_EXPORT(int, __vlc_event_attach, ( vlc_event_manager_t * p_event_manager,
                                      vlc_event_type_t event_type,
                                      vlc_event_callback_t pf_callback,
                                      void *p_user_data,
                                      const char * psz_debug_name ));
195 196 197 198

/*
 * Remove a callback for an event.
 */
199 200 201
VLC_EXPORT(int, vlc_event_detach, ( vlc_event_manager_t *p_event_manager,
                                    vlc_event_type_t event_type,
                                    vlc_event_callback_t pf_callback,
202
                                    void *p_user_data ));
203 204

#endif /* VLC_EVENTS_H */