Commit 3f75f779 authored by Pierre d'Herbemont's avatar Pierre d'Herbemont

control/event.c: Change the event API to be per-object driven. It is not yet in use.

parent 1605af57
...@@ -884,49 +884,50 @@ VLC_PUBLIC_API libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterat ...@@ -884,49 +884,50 @@ VLC_PUBLIC_API libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterat
/** @} */ /** @} */
/***************************************************************************** /*****************************************************************************
* Callbacks handling * Event handling
*****************************************************************************/ *****************************************************************************/
/** defgroup libvlc_callbacks Callbacks /** defgroup libvlc_callbacks Callbacks
* \ingroup libvlc * \ingroup libvlc
* LibVLC Event Callbacks * LibVLC Events
* @{ * @{
*/ */
/** /**
* Register for a callback notification * Register for an event notification
* \param p_instance the libvlc instance * \param p_event_manager the event manager to which you want to attach to
* \param i_event_type the desired event mask to which we want to listen * Generally it is obtained by vlc_my_object_event_manager() where my_object
* Is the object you want to listen to.
* \param i_event_type the desired event to which we want to listen
* \param f_callback the function to call when i_event_type occurs * \param f_callback the function to call when i_event_type occurs
* \param user_data user provided data to carry with the event * \param user_data user provided data to carry with the event
* \param p_e an initialized exception pointer * \param p_e an initialized exception pointer
*/ */
VLC_PUBLIC_API void libvlc_event_add_callback( libvlc_instance_t *p_instance, VLC_PUBLIC_API void libvlc_event_attach( libvlc_event_manager_t *p_event_manager,
libvlc_event_type_t i_event_type, libvlc_event_type_t i_event_type,
libvlc_callback_t f_callback, libvlc_callback_t f_callback,
void *user_data, void *user_data,
libvlc_exception_t *p_e ); libvlc_exception_t *p_e );
/** /**
* Unregister all callbacks notification from an instance * Unregister an event notification
* \param p_instance the libvlc instance * \param p_event_manager the event manager
* \param i_event_type the desired event to which we want to unregister
* \param f_callback the function to call when i_event_type occurs
* \param p_e an initialized exception pointer * \param p_e an initialized exception pointer
*/ */
VLC_PUBLIC_API void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance, VLC_PUBLIC_API void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
libvlc_exception_t *p_e ); libvlc_event_type_t i_event_type,
libvlc_callback_t f_callback,
void *p_user_data,
libvlc_exception_t *p_e );
/** /**
* Unregister a callback notification * Get an event type name
* \param p_instance the libvlc instance * \param i_event_type the desired event
* \param i_event_type the desired event mask to which we want to unregister
* \param f_callback the function to call when i_event_type occurs
* \param p_e an initialized exception pointer
*/ */
VLC_PUBLIC_API void libvlc_event_remove_callback( libvlc_instance_t *p_instance, #define libvlc_event_type_name(a) #a
libvlc_event_type_t i_event_type,
libvlc_callback_t f_callback,
void *p_user_data,
libvlc_exception_t *p_e );
/** @} */ /** @} */
......
...@@ -182,7 +182,7 @@ typedef struct libvlc_log_message_t ...@@ -182,7 +182,7 @@ typedef struct libvlc_log_message_t
*/ */
/** /**
* Available events: * Available events: (XXX: being reworked)
* - libvlc_VolumeChanged * - libvlc_VolumeChanged
* - libvlc_InputPositionChanged * - libvlc_InputPositionChanged
*/ */
...@@ -192,9 +192,17 @@ typedef enum libvlc_event_type_t { ...@@ -192,9 +192,17 @@ typedef enum libvlc_event_type_t {
libvlc_InputPositionChanged, libvlc_InputPositionChanged,
} libvlc_event_type_t; } libvlc_event_type_t;
/**
* An Event
* \param type the even type
* \param p_obj the sender object
* \param u Event dependent content
*/
typedef struct libvlc_event_t typedef struct libvlc_event_t
{ {
libvlc_event_type_t type; libvlc_event_type_t type;
void * p_obj;
union union
{ {
struct struct
...@@ -208,14 +216,19 @@ typedef struct libvlc_event_t ...@@ -208,14 +216,19 @@ typedef struct libvlc_event_t
} u; } u;
} libvlc_event_t; } libvlc_event_t;
/**
* Event manager that belongs to a libvlc object, and from whom events can
* be received.
*/
typedef struct libvlc_event_manager_t libvlc_event_manager_t;
/** /**
* Callback function notification * Callback function notification
* \param p_instance the libvlc instance
* \param p_event the event triggering the callback * \param p_event the event triggering the callback
* \param p_user_data user provided data
*/ */
typedef void ( *libvlc_callback_t )( struct libvlc_instance_t *, libvlc_event_t *, void * ); typedef void ( *libvlc_callback_t )( const libvlc_event_t * );
/**@} */ /**@} */
......
...@@ -31,217 +31,121 @@ ...@@ -31,217 +31,121 @@
* Private functions * Private functions
*/ */
/**************************************************************************
* handle_event (private)
*
* Callback from the vlc variables
**************************************************************************/
static int handle_event( vlc_object_t *p_this, char const *psz_cmd,
vlc_value_t oldval, vlc_value_t newval,
void *p_data )
{
/* This is thread safe, as the var_*Callback already provide the locking
* facility for p_data */
struct libvlc_callback_entry_t *entry = p_data;
libvlc_event_t event;
event.type = entry->i_event_type;
if (event.type == libvlc_VolumeChanged)
{
if(!strcmp(psz_cmd, "intf-change"))
{
input_thread_t * p_input = (input_thread_t *)p_this;
vlc_value_t val;
var_Get( p_input, "time", &val );
/* Only send event at a reasonable time precision (500ms) */
/* (FIXME: this should be configurable) */
if ((val.i_time % I64C(500000)) != 0)
{
/* Don't send this event */
return VLC_SUCCESS;
}
event.u.input_position_changed.new_position = val.i_time;
}
else
event.u.input_position_changed.new_position = newval.i_time;
event.u.input_position_changed.new_position *= 1000LL;
}
else if (event.type == libvlc_InputPositionChanged)
{
event.u.volume_changed.new_volume = newval.i_int;
}
/* Call the client entry */ /*
entry->f_callback( entry->p_instance, &event, entry->p_user_data ); * Internal libvlc functions
*/
return VLC_SUCCESS;
}
/************************************************************************** /**************************************************************************
* get_input (private) : * libvlc_event_init (internal) :
* *
* Utility function, Object should be released by vlc_object_release * initialization function.
* afterwards
**************************************************************************/ **************************************************************************/
static input_thread_t * get_input(libvlc_instance_t * p_instance) void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
{ {
libvlc_media_instance_t * p_mi; /* Will certainly be used to install libvlc_instance event */
input_thread_t * p_input;
p_mi = libvlc_playlist_get_media_instance( p_instance, NULL );
if( !p_mi )
return NULL;
p_input = libvlc_get_input_thread( p_mi, NULL );
libvlc_media_instance_release( p_mi );
return p_input;
} }
/************************************************************************** /**************************************************************************
* install_input_event (private) : * libvlc_event_fini (internal) :
* *
* vlc variables callback, used to install input event. * finalization function.
* Can be called manually though.
**************************************************************************/ **************************************************************************/
static int install_input_event( vlc_object_t *p_this, char const *psz_cmd, void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
vlc_value_t oldval, vlc_value_t newval,
void *p_data )
{ {
libvlc_instance_t * p_instance = p_data;
struct libvlc_callback_entry_list_t *p_listitem;
input_thread_t * p_input = get_input( p_instance );
if( !p_input )
return VLC_SUCCESS;
vlc_mutex_lock( &p_instance->instance_lock );
p_listitem = p_instance->p_callback_list;
for( ; p_listitem ; p_listitem = p_listitem->next )
{
if (p_listitem->elmt->i_event_type == libvlc_InputPositionChanged)
{
/* FIXME: here we shouldn't listen on intf-change, we have to provide
* in vlc core a more accurate callback */
var_AddCallback( p_input, "intf-change", handle_event, p_listitem->elmt );
var_AddCallback( p_input, "time", handle_event, p_listitem->elmt );
}
}
vlc_mutex_unlock( &p_instance->instance_lock );
vlc_object_release( p_input );
return VLC_SUCCESS;
} }
/************************************************************************** /**************************************************************************
* add_callback_to_list (private) : * libvlc_event_manager_init (internal) :
* *
* callback list utility function. * Init an object's event manager.
**************************************************************************/ **************************************************************************/
static inline void add_callback_to_list( struct libvlc_callback_entry_t *entry, libvlc_event_manager_t *
struct libvlc_callback_entry_list_t **list ) libvlc_event_manager_init( void * p_obj, libvlc_instance_t * p_libvlc_inst,
libvlc_exception_t *p_e )
{ {
struct libvlc_callback_entry_list_t *new_listitem; libvlc_event_manager_t * p_em;
/* malloc/free strategy: p_em = malloc(sizeof( libvlc_event_manager_t ));
* - alloc-ded in add_callback_entry if( !p_em )
* - free-ed by libvlc_event_remove_callback {
* - free-ed in libvlc_destroy threw libvlc_event_remove_callback libvlc_exception_raise( p_e, "No Memory left" );
* when entry is destroyed return NULL;
*/ }
new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
new_listitem->elmt = entry;
new_listitem->next = *list;
new_listitem->prev = NULL;
if(*list) p_em->p_obj = p_obj;
(*list)->prev = new_listitem; p_em->p_libvlc_instance = p_libvlc_inst;
ARRAY_INIT( p_em->listeners_groups );
*list = new_listitem; return p_em;
} }
/************************************************************************** /**************************************************************************
* remove_variable_callback (private) : * libvlc_event_manager_release (internal) :
* *
* Delete the appropriate vlc variables callback for an event. * Init an object's event manager.
**************************************************************************/ **************************************************************************/
static int remove_variable_callback( libvlc_instance_t *p_instance, void libvlc_event_manager_release( libvlc_event_manager_t * p_em )
struct libvlc_callback_entry_t * p_entry )
{ {
input_thread_t * p_input = get_input( p_instance ); libvlc_event_listeners_group_t * listeners_group;
int res = VLC_SUCCESS; libvlc_event_listener_t * listener;
/* Note: Appropriate lock should be held by the caller */ FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
FOREACH_ARRAY( listener, listeners_group->listeners )
switch ( p_entry->i_event_type ) free( listener );
{ FOREACH_END()
case libvlc_VolumeChanged: free( listeners_group );
res = var_DelCallback( p_instance->p_libvlc_int, "volume-change", FOREACH_END()
handle_event, p_entry ); free( p_em );
break;
case libvlc_InputPositionChanged:
/* We may not be deleting the right p_input callback, in this case this
* will be a no-op */
var_DelCallback( p_input, "intf-change", handle_event, p_entry );
var_DelCallback( p_input, "position", handle_event, p_entry );
break;
}
if (p_input)
vlc_object_release( p_input );
return res;
} }
/*
* Internal libvlc functions
*/
/************************************************************************** /**************************************************************************
* libvlc_event_init (internal) : * libvlc_event_manager_register_event_type (internal) :
* *
* initialization function. * Init an object's event manager.
**************************************************************************/ **************************************************************************/
void libvlc_event_init( libvlc_instance_t *p_instance, libvlc_exception_t *p_e ) void libvlc_event_manager_register_event_type(
libvlc_event_manager_t * p_em,
libvlc_event_type_t event_type,
libvlc_exception_t * p_e )
{ {
playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist; libvlc_event_listeners_group_t * listeners_group;
listeners_group = malloc(sizeof(libvlc_event_listeners_group_t));
p_instance->p_callback_list = NULL; if( !listeners_group )
{
if( !p_playlist ) libvlc_exception_raise( p_e, "No Memory left" );
RAISEVOID ("Can't listen to input event"); return;
}
/* Install a Callback for input changes, so
* so we can track input event */ listeners_group->event_type = event_type;
var_AddCallback( p_playlist, "playlist-current", ARRAY_INIT( listeners_group->listeners );
install_input_event, p_instance );
ARRAY_APPEND( p_em->listeners_groups, listeners_group );
} }
/************************************************************************** /**************************************************************************
* libvlc_event_fini (internal) : * libvlc_event_send (internal) :
* *
* finalization function. * Send a callback.
**************************************************************************/ **************************************************************************/
void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e ) void libvlc_event_send( libvlc_event_manager_t * p_em,
libvlc_event_t * p_event )
{ {
playlist_t *p_playlist = p_instance->p_libvlc_int->p_playlist; libvlc_event_listeners_group_t * listeners_group;
libvlc_exception_t p_e_unused; libvlc_event_listener_t * listener;
/* Fill event with the sending object now */
libvlc_event_remove_all_callbacks( p_instance, &p_e_unused ); p_event->p_obj = p_em->p_obj;
if( !p_playlist ) FOREACH_ARRAY( listeners_group, p_em->listeners_groups )
RAISEVOID ("Can't unregister input events"); if( listeners_group->event_type == p_event->type )
{
var_DelCallback( p_playlist, "playlist-current", /* We found the group, now send every one the event */
install_input_event, p_instance ); FOREACH_ARRAY( listener, listeners_group->listeners )
listener->pf_callback( p_event );
FOREACH_END()
break;
}
FOREACH_END()
} }
/* /*
...@@ -249,129 +153,77 @@ void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e ) ...@@ -249,129 +153,77 @@ void libvlc_event_fini( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
*/ */
/************************************************************************** /**************************************************************************
* libvlc_event_add_callback (public) : * libvlc_event_attach (public) :
* *
* Add a callback for an event. * Add a callback for an event.
**************************************************************************/ **************************************************************************/
void libvlc_event_add_callback( libvlc_instance_t *p_instance, void libvlc_event_attach( libvlc_event_manager_t * p_event_manager,
libvlc_event_type_t i_event_type, libvlc_event_type_t event_type,
libvlc_callback_t f_callback, libvlc_callback_t pf_callback,
void *user_data, void *p_user_data,
libvlc_exception_t *p_e ) libvlc_exception_t *p_e )
{ {
struct libvlc_callback_entry_t *entry; libvlc_event_listeners_group_t * listeners_group;
vlc_value_t unused1, unused2; libvlc_event_listener_t * listener;
int res = VLC_SUCCESS; listener = malloc(sizeof(libvlc_event_listener_t));
if( !listener )
if ( !f_callback ) {
RAISEVOID (" Callback function is null "); libvlc_exception_raise( p_e, "No Memory left" );
return;
/* malloc/free strategy: }
* - alloc-ded in libvlc_event_add_callback
* - free-ed by libvlc_event_add_callback on error listener->event_type = event_type;
* - free-ed by libvlc_event_remove_callback listener->p_user_data = p_user_data;
* - free-ed in libvlc_destroy threw libvlc_event_remove_callback listener->pf_callback = pf_callback;
* when entry is destroyed
*/ FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
entry = malloc( sizeof( struct libvlc_callback_entry_t ) ); if( listeners_group->event_type == listener->event_type )
entry->f_callback = f_callback; {
entry->i_event_type = i_event_type; ARRAY_APPEND( listeners_group->listeners, listener );
entry->p_user_data = user_data; return;
}
switch ( i_event_type ) FOREACH_END()
{
case libvlc_VolumeChanged: free(listener);
res = var_AddCallback( p_instance->p_libvlc_int, "volume-change", libvlc_exception_raise( p_e,
handle_event, entry ); "This object event manager doesn't know about '%s' events",
break; libvlc_event_type_name(a));
case libvlc_InputPositionChanged:
install_input_event( NULL, NULL, unused1, unused2, p_instance);
break;
default:
free( entry );
RAISEVOID( "Unsupported event." );
}
if (res != VLC_SUCCESS)
{
free ( entry );
RAISEVOID("Internal callback registration was not successful. Callback not registered.");
}
vlc_mutex_lock( &p_instance->instance_lock );
add_callback_to_list( entry, &p_instance->p_callback_list );
vlc_mutex_unlock( &p_instance->instance_lock );
return;
} }
/************************************************************************** /**************************************************************************
* libvlc_event_remove_callback (public) : * libvlc_event_detach (public) :
* *
* Remove a callback for an event. * Remove a callback for an event.
**************************************************************************/ **************************************************************************/
void libvlc_event_remove_callback( libvlc_instance_t *p_instance, void libvlc_event_detach( libvlc_event_manager_t *p_event_manager,
libvlc_event_type_t i_event_type, libvlc_event_type_t event_type,
libvlc_callback_t f_callback, libvlc_callback_t pf_callback,
void *p_user_data, void *p_user_data,
libvlc_exception_t *p_e ) libvlc_exception_t *p_e )
{ {
struct libvlc_callback_entry_list_t *p_listitem; libvlc_event_listeners_group_t * listeners_group;
libvlc_event_listener_t * listener;
vlc_mutex_lock( &p_instance->instance_lock ); FOREACH_ARRAY( listeners_group, p_event_manager->listeners_groups )
if( listeners_group->event_type == event_type )
p_listitem = p_instance->p_callback_list; {
FOREACH_ARRAY( listener, listeners_group->listeners )
while( p_listitem ) if( listener->event_type == event_type &&
{ listener->pf_callback == pf_callback &&
if( p_listitem->elmt->f_callback == f_callback listener->p_user_data == p_user_data )
&& ( p_listitem->elmt->i_event_type == i_event_type ) {
&& ( p_listitem->elmt->p_user_data == p_user_data ) /* that's our listener */
free( listener );
) ARRAY_REMOVE( listeners_group->listeners,
{ fe_idx /* This comes from the macro (and that's why
remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We should warn on error */ I hate macro) */ );
return;
if( p_listitem->prev ) }
p_listitem->prev->next = p_listitem->next; FOREACH_END()
else }
p_instance->p_callback_list = p_listitem->next; FOREACH_END()
libvlc_exception_raise( p_e,
p_listitem->next->prev = p_listitem->prev; "This object event manager doesn't know about '%i,%p,%p' event observer",
event_type, pf_callback, p_user_data );
free( p_listitem->elmt );
free( p_listitem );
break;
}
p_listitem = p_listitem->next;
}
vlc_mutex_unlock( &p_instance->instance_lock );
} }
/**************************************************************************
* libvlc_event_remove_all_callbacks (public) :
*
* Remove all callbacks for all events.
**************************************************************************/
void libvlc_event_remove_all_callbacks( libvlc_instance_t *p_instance,
libvlc_exception_t *p_e )
{
struct libvlc_callback_entry_list_t *p_listitem;
vlc_mutex_lock( &p_instance->instance_lock );
p_listitem = p_instance->p_callback_list;
while( p_listitem )
{
remove_variable_callback( p_instance, p_listitem->elmt ); /* FIXME: We could warn on error */
p_listitem = p_listitem->next;
}
p_instance->p_callback_list = NULL;
vlc_mutex_unlock( &p_instance->instance_lock );
}
...@@ -32,6 +32,7 @@ extern "C" { ...@@ -32,6 +32,7 @@ extern "C" {
#include <vlc/vlc.h> #include <vlc/vlc.h>
#include <vlc/libvlc_structures.h> #include <vlc/libvlc_structures.h>
#include <vlc_arrays.h>
#include <vlc_input.h> #include <vlc_input.h>
/*************************************************************************** /***************************************************************************
...@@ -52,21 +53,6 @@ VLC_EXPORT (void, libvlc_event_fini, ( libvlc_instance_t *, libvlc_exception_t * ...@@ -52,21 +53,6 @@ VLC_EXPORT (void, libvlc_event_fini, ( libvlc_instance_t *, libvlc_exception_t *
* Opaque structures for libvlc API * Opaque structures for libvlc API
***************************************************************************/ ***************************************************************************/
struct libvlc_callback_entry_t
{
libvlc_instance_t *p_instance;
libvlc_callback_t f_callback;
libvlc_event_type_t i_event_type;
void *p_user_data;
};
struct libvlc_callback_entry_list_t
{
struct libvlc_callback_entry_t *elmt;
struct libvlc_callback_entry_list_t *next;
struct libvlc_callback_entry_list_t *prev;
};
struct libvlc_instance_t struct libvlc_instance_t
{ {
libvlc_int_t *p_libvlc_int; libvlc_int_t *p_libvlc_int;
...@@ -94,24 +80,103 @@ struct libvlc_media_instance_t ...@@ -94,24 +80,103 @@ struct libvlc_media_instance_t
libvlc_media_descriptor_t *p_md; /* current media descriptor */ libvlc_media_descriptor_t *p_md; /* current media descriptor */
}; };
/*
* 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_init( p_self,
* p_self->p_libvlc_instance, p_e);
* libvlc_event_manager_register_event_type(p_self->p_event_manager,
* libvlc_MyCoolObjectDidSomething, p_e)
* ...
* }
*
* 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.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_listeners_group_t
{
libvlc_event_type_t event_type;
DECL_ARRAY(libvlc_event_listener_t *) listeners;