From b28e41253458f84e5003ad62ae0feb12083d0e3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Cl=C3=A9ment=20Stenac?= Date: Mon, 18 Dec 2006 22:03:30 +0000 Subject: [PATCH] A bit of cleanup in libvlc playlist API. Preliminary work for: Refs:#457 --- include/vlc/libvlc.h | 6 ++++-- include/vlc/vlc.h | 2 ++ src/control/playlist.c | 33 +++++++++++++------------------- src/playlist/item.c | 28 ++++++++++++++++----------- src/playlist/playlist_internal.h | 2 ++ src/playlist/search.c | 23 ++++++++++++++++++++++ 6 files changed, 61 insertions(+), 33 deletions(-) diff --git a/include/vlc/libvlc.h b/include/vlc/libvlc.h index 688d940fa0..12406a36c5 100644 --- a/include/vlc/libvlc.h +++ b/include/vlc/libvlc.h @@ -51,6 +51,7 @@ extern "C" { struct libvlc_exception_t { int b_raised; + int i_code; char *psz_message; }; typedef struct libvlc_exception_t libvlc_exception_t; @@ -142,7 +143,8 @@ void libvlc_destroy( libvlc_instance_t *, libvlc_exception_t * ); /** * Set loop variable */ -void libvlc_playlist_loop( libvlc_instance_t* , vlc_bool_t, libvlc_exception_t * ); +void libvlc_playlist_loop( libvlc_instance_t* , vlc_bool_t, + libvlc_exception_t * ); /** * Start playing. You can give some additionnal playlist item options @@ -233,7 +235,7 @@ int libvlc_playlist_add_extended( libvlc_instance_t *, const char *, const char *, int, const char **, libvlc_exception_t * ); -/** +/** * Delete the playlist item with the given ID. * \param p_instance the instance * \param i_id the id to remove diff --git a/include/vlc/vlc.h b/include/vlc/vlc.h index c2e2cdeffb..df11673c0a 100644 --- a/include/vlc/vlc.h +++ b/include/vlc/vlc.h @@ -122,6 +122,8 @@ struct vlc_list_t #define VLC_ENOVAR -30 /* Variable not found */ #define VLC_EBADVAR -31 /* Bad variable value */ +#define VLC_ENOITEM -40 /**< Item not found */ + #define VLC_EEXIT -255 /* Program exited */ #define VLC_EEXITSUCCESS -999 /* Program exited successfully */ #define VLC_EGENERIC -666 /* Generic error */ diff --git a/src/control/playlist.c b/src/control/playlist.c index 0872b5ac86..22a75a6fc4 100644 --- a/src/control/playlist.c +++ b/src/control/playlist.c @@ -27,13 +27,15 @@ #include +#include "../playlist/playlist_internal.h" + #define PL p_instance->p_libvlc_int->p_playlist void libvlc_playlist_loop( libvlc_instance_t *p_instance, vlc_bool_t loop, libvlc_exception_t *p_e) { assert( PL ); - var_SetBool(PL,"loop",loop); + var_SetBool( PL, "loop", loop ); } void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id, @@ -46,7 +48,8 @@ void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id, if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" ); if( i_id > 0 ) { - playlist_item_t *p_item = playlist_ItemGetById( PL, i_id, VLC_TRUE ); + playlist_item_t *p_item = playlist_ItemGetByInputId( PL, i_id, + PL->status.p_node ); if( !p_item ) RAISEVOID( "Unable to find item" ); playlist_Control( PL, PLAYLIST_VIEWPLAY, VLC_FALSE, @@ -115,31 +118,21 @@ int libvlc_playlist_add_extended( libvlc_instance_t *p_instance, i_options, 1 ); } + + int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id, libvlc_exception_t *p_e ) { - playlist_item_t *p_item; assert( PL ); - vlc_mutex_lock( &PL->object_lock ); - p_item = playlist_ItemGetById( PL, i_id, VLC_TRUE ); - if( p_item && p_item->p_input ) { - int i_ret = playlist_DeleteFromInput( PL, p_item->p_input->i_id, VLC_TRUE ); - if( i_ret ) { - libvlc_exception_raise( p_e, "delete failed" ); - vlc_mutex_unlock( &PL->object_lock ); - return VLC_EGENERIC; - } - else { - vlc_mutex_unlock( &PL->object_lock ); - return VLC_SUCCESS; - } + + if( playlist_DeleteFromInput( PL, i_id, VLC_FALSE ) ) + { + libvlc_exception_raise( p_e, "deletion failed" ); + return VLC_ENOITEM; } - libvlc_exception_raise( p_e, "item not found" ); - vlc_mutex_unlock( &PL->object_lock ); - return VLC_EGENERIC; + return VLC_SUCCESS; } - int libvlc_playlist_isplaying( libvlc_instance_t *p_instance, libvlc_exception_t *p_e ) { diff --git a/src/playlist/item.c b/src/playlist/item.c index e3bfc9578c..cc47aa1104 100644 --- a/src/playlist/item.c +++ b/src/playlist/item.c @@ -113,13 +113,15 @@ static int DeleteFromInput( playlist_t *p_playlist, int i_input_id, int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id, vlc_bool_t b_locked ) { + int i_ret1, i_ret2; if( !b_locked ) PL_LOCK; - DeleteFromInput( p_playlist, i_input_id, - p_playlist->p_root_category, VLC_TRUE ); - DeleteFromInput( p_playlist, i_input_id, + i_ret1 = DeleteFromInput( p_playlist, i_input_id, + p_playlist->p_root_category, VLC_TRUE ); + i_ret2 = DeleteFromInput( p_playlist, i_input_id, p_playlist->p_root_onelevel, VLC_TRUE ); if( !b_locked ) PL_UNLOCK; - return VLC_SUCCESS; + return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ? + VLC_SUCCESS : VLC_ENOITEM; } void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked ) @@ -182,11 +184,15 @@ int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri, mtime_t i_duration, const char *const *ppsz_options, int i_options, vlc_bool_t b_playlist ) { + int i_ret; input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name, i_options, ppsz_options, i_duration ); - return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist ); + i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist ); + if( i_ret == VLC_SUCCESS ) + return p_input->i_id; + return -1; } /** Add an input item to the playlist node */ @@ -199,25 +205,25 @@ int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input, PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name, p_input->psz_uri ); - vlc_mutex_lock( &p_playlist->object_lock ); + PL_LOCK; /* Add to ONELEVEL */ p_item_one = playlist_ItemNewFromInput( p_playlist, p_input ); - if( p_item_one == NULL ) return VLC_EGENERIC; + if( p_item_one == NULL ) return VLC_ENOMEM; AddItem( p_playlist, p_item_one, b_playlist ? p_playlist->p_local_onelevel : p_playlist->p_ml_onelevel , i_mode, i_pos ); /* Add to CATEGORY */ p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input ); - if( p_item_cat == NULL ) return VLC_EGENERIC; + if( p_item_cat == NULL ) return VLC_ENOMEM; AddItem( p_playlist, p_item_cat, b_playlist ? p_playlist->p_local_category : p_playlist->p_ml_category , i_mode, i_pos ); GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one ); - vlc_mutex_unlock( &p_playlist->object_lock ); + PL_UNLOCK; return VLC_SUCCESS; } @@ -236,13 +242,13 @@ int playlist_BothAddInput( playlist_t *p_playlist, /* Add to category */ p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input ); - if( p_item_cat == NULL ) return VLC_EGENERIC; + if( p_item_cat == NULL ) return VLC_ENOMEM; AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos ); /* Add to onelevel */ /** \todo make a faster case for ml import */ p_item_one = playlist_ItemNewFromInput( p_playlist, p_input ); - if( p_item_one == NULL ) return VLC_EGENERIC; + if( p_item_one == NULL ) return VLC_ENOMEM; p_up = p_direct_parent; while( p_up->p_parent != p_playlist->p_root_category ) diff --git a/src/playlist/playlist_internal.h b/src/playlist/playlist_internal.h index e82b677ee5..cbc4bab80e 100644 --- a/src/playlist/playlist_internal.h +++ b/src/playlist/playlist_internal.h @@ -110,6 +110,8 @@ playlist_item_t *playlist_GetLastLeaf( playlist_t *p_playlist, int playlist_DeleteFromItemId( playlist_t*, int ); int playlist_ItemDelete ( playlist_item_t * ); +playlist_item_t *playlist_ItemGetByInputId( playlist_t*, int, playlist_item_t*); + /** * @} */ diff --git a/src/playlist/search.c b/src/playlist/search.c index 659b9a4f37..c831566e33 100644 --- a/src/playlist/search.c +++ b/src/playlist/search.c @@ -81,6 +81,29 @@ playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist , return NULL; } +/** Find the playlist item matching the input id under the given node */ +playlist_item_t * playlist_ItemGetByInputId( playlist_t *p_playlist, + int i_input_id, + playlist_item_t *p_root ) +{ + int i; + assert( p_root != NULL ); + for( i = 0 ; i< p_root->i_children ; i++ ) + { + if( p_root->pp_children[i]->i_children == -1 && + p_root->pp_children[i]->p_input->i_id == i_input_id ) + { + return p_root->pp_children[i]; + } + else if( p_root->pp_children[i]->i_children >= 0 ) + { + return playlist_ItemGetByInputId( p_playlist, i_input_id, + p_root->pp_children[i] ); + } + } + return NULL; +} + /*************************************************************************** * Live search handling ***************************************************************************/ -- 2.39.2