#include <vlc_common.h>
#include <assert.h>
#include <vlc_playlist.h>
+#include <vlc_rand.h>
#include "playlist_internal.h"
static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
playlist_item_t * );
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
-static playlist_item_t *ItemToNode( playlist_t *, playlist_item_t *, bool );
+static int RecursiveAddIntoParent (
+ playlist_t *p_playlist, playlist_item_t *p_parent,
+ input_item_node_t *p_node, int i_pos, bool b_flat,
+ playlist_item_t **pp_first_leaf );
+static int RecursiveInsertCopy (
+ playlist_t *p_playlist, playlist_item_t *p_item,
+ playlist_item_t *p_parent, int i_pos, bool b_flat );
/*****************************************************************************
* An input item has gained subitems (Event Callback)
playlist_ItemGetByInput( p_playlist, p_input );
assert( p_item != NULL );
- playlist_item_t *p_parent = p_item->p_parent;
- assert( p_parent != NULL );
- bool b_play = var_CreateGetBool( p_playlist, "playlist-autostart" ) &&
- get_current_status_item( p_playlist ) == p_item;
- bool b_stop = b_play && p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
+ bool b_current = get_current_status_item( p_playlist ) == p_item;
+ bool b_autostart = var_CreateGetBool( p_playlist, "playlist-autostart" );
+ bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
+ bool b_flat = false;
+
p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
- int pos = 0;
- for( int i = 0; i < p_parent->i_children; i++ )
- {
- if( p_parent->pp_children[i] == p_item )
+ /* We will have to flatten the tree out if we are in "the playlist" node and
+ the user setting demands flat playlist */
+
+ if( !pl_priv(p_playlist)->b_tree ) {
+ playlist_item_t *p_up = p_item;
+ while( p_up->p_parent )
{
- pos = i;
- break;
+ if( p_up->p_parent == p_playlist->p_playing )
+ {
+ b_flat = true;
+ break;
+ }
+ p_up = p_up->p_parent;
}
}
- bool b_flat = false;
- playlist_item_t *p_up = p_item;
- while( p_up->p_parent )
+ int pos = 0;
+
+ /* If we have to flatten out, then take the item's position in the parent as
+ insertion point and delete the item */
+
+ if( b_flat )
{
- if( p_up->p_parent == p_playlist->p_playing ||
- p_up->p_parent == p_playlist->p_media_library )
+ playlist_item_t *p_parent = p_item->p_parent;
+ assert( p_parent != NULL );
+
+ int i;
+ for( i = 0; i < p_parent->i_children; i++ )
{
- if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
- break;
+ if( p_parent->pp_children[i] == p_item )
+ {
+ pos = i;
+ break;
+ }
}
- p_up = p_up->p_parent;
- }
- if( b_flat )
- {
+ assert( i < p_parent->i_children );
+
playlist_DeleteItem( p_playlist, p_item, true );
- p_item = playlist_InsertInputItemTree( p_playlist, p_parent,
- p_new_root, pos, true );
+
+ p_item = p_parent;
}
else
- p_item = playlist_InsertInputItemTree( p_playlist, p_item,
- p_new_root, 0, false );
-
- if( b_stop )
{
- PL_UNLOCK;
- playlist_Stop( p_playlist );
- return;
+ pos = p_item->i_children >= 0 ? p_item->i_children : 0;
}
- else if( b_play )
+
+ /* At this point:
+ "p_item" is the node where sub-items should be inserted,
+ "pos" is the insertion position in that node */
+
+ int last_pos = playlist_InsertInputItemTree( p_playlist,
+ p_item,
+ p_new_root,
+ pos,
+ b_flat );
+
+ if( !b_flat ) var_SetAddress( p_playlist, "leaf-to-parent", p_input );
+
+ //control playback only if it was the current playing item that got subitems
+ if( b_current )
{
- playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
- pl_Locked, get_current_status_node( p_playlist ), p_item );
+ if( last_pos == pos || ( b_stop && !b_flat ) || !b_autostart )
+ {
+ /* We stop, either because no sub-item was actually created, or some
+ flags/settings want us to do so at this point */
+ PL_UNLOCK;
+ playlist_Stop( p_playlist );
+ return;
+ }
+ else
+ {
+ /* Continue to play, either random or the first new item */
+ playlist_item_t *p_play_item;
+
+ if( var_GetBool( p_playlist, "random" ) )
+ {
+ unsigned rand_pos =
+ ((unsigned)vlc_mrand48()) % (last_pos - pos);
+ rand_pos += pos;
+ p_play_item = p_item->pp_children[rand_pos];
+ }
+ else
+ {
+ p_play_item = p_item->pp_children[pos];
+ /* NOTE: this is a work around the general bug:
+ if node-to-be-played contains sub-nodes, then second instead
+ of first leaf starts playing (only in case the leafs have just
+ been instered and playlist has not yet been rebuilt.)
+ */
+ while( p_play_item->i_children > 0 )
+ p_play_item = p_play_item->pp_children[0];
+ }
+
+ playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
+ pl_Locked,
+ get_current_status_node( p_playlist ),
+ p_play_item );
+ }
}
PL_UNLOCK;
if( p_item == NULL ) return NULL;
AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
+ GoAndPreparse( p_playlist, i_mode, p_item );
+
PL_UNLOCK_IF( !b_locked );
return p_item;
}
+/**
+ * Copy an item (and all its children, if any) into another node
+ *
+ * \param p_playlist the playlist to operate on
+ * \param p_item the playlist item to copy
+ * \param p_parent the parent item to copy into
+ * \param i_pos the position in the parent item for the new copy;
+ * if this is PLAYLIST_END, the copy is appended after all
+ * parent's children
+ * \return the position in parent item just behind the last new item inserted
+ */
+int playlist_NodeAddCopy (
+ playlist_t *p_playlist, playlist_item_t *p_item,
+ playlist_item_t *p_parent, int i_pos )
+{
+ PL_ASSERT_LOCKED;
+ assert( p_parent != NULL && p_item != NULL );
+ assert( p_parent->i_children > -1 );
+
+ if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
+
+ bool b_flat = false;
+
+ playlist_item_t *p_up = p_parent;
+ while( p_up )
+ {
+ if( p_up == p_playlist->p_playing )
+ if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
+ if( p_up == p_item )
+ /* TODO: We don't support copying a node into itself (yet),
+ because we insert items as we copy. Instead, we should copy
+ all items first and then insert. */
+ return i_pos;
+ p_up = p_up->p_parent;
+ }
+
+ return RecursiveInsertCopy( p_playlist, p_item, p_parent, i_pos, b_flat );
+}
+
/**
* Insert a tree of input items into a given playlist node
*
* \param b_flat TRUE if the new tree contents should be flattened into a list
* \return the first new leaf inserted (in playing order)
*/
-
-playlist_item_t *playlist_InsertInputItemTree (
+int playlist_InsertInputItemTree (
playlist_t *p_playlist, playlist_item_t *p_parent,
input_item_node_t *p_node, int i_pos, bool b_flat )
{
playlist_item_t *p_first_leaf = NULL;
-
- if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
-
- if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
-
- for( int i = 0; i < p_node->i_children; i++, i_pos++ )
- {
- playlist_item_t *p_child = NULL;
- if( b_flat ? p_node->pp_children[i]->i_children == 0 : 1 )
- {
- printf("creating a leaf: %i\n", i_pos);
- p_child = playlist_NodeAddInput( p_playlist,
- p_node->pp_children[i]->p_item,
- p_parent,
- PLAYLIST_INSERT, i_pos,
- pl_Locked );
- printf("leaf done\n");
- }
- if( p_node->pp_children[i]->i_children > 0 )
- {
- if( b_flat )
- {
- printf("flat -> subnode into parent\n");
- p_child = playlist_InsertInputItemTree( p_playlist, p_parent,
- p_node->pp_children[i], i_pos, true );
- i_pos += p_node->i_children - 1; /* i_pos += 1 on loop */
- }
- else
- {
- printf("tree -> subnode on its own\n");
- p_child = playlist_InsertInputItemTree( p_playlist, p_child,
- p_node->pp_children[i], 0, false );
- }
- }
- if( i == 0 ) p_first_leaf = p_child;
- }
- printf("leaving a node\n");
- return p_first_leaf;
+ int i = RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
+ return i;
}
+
/*****************************************************************************
* Playlist item misc operations
*****************************************************************************/
-/**
- * Item to node
- *
- * Transform an item to a node. Return the node in the category tree, or NULL
- * if not found there
- * This function must be entered without the playlist lock
- * \param p_playlist the playlist object
- * \param p_item the item to transform
- * \param b_locked TRUE if the playlist is locked
- * \return the item transform in a node
- */
-static playlist_item_t *ItemToNode( playlist_t *p_playlist,
- playlist_item_t *p_item,
- bool b_locked )
-{
- PL_LOCK_IF( !b_locked );
-
- assert( p_item->p_parent );
-
- bool b_flat = false;
- playlist_item_t *p_up = p_item;
- while( p_up->p_parent )
- {
- if( p_up->p_parent == p_playlist->p_playing ||
- p_up->p_parent == p_playlist->p_media_library )
- {
- if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
- break;
- }
- p_up = p_up->p_parent;
- }
-
- if( !b_flat )
- {
- ChangeToNode( p_playlist, p_item );
- if( p_up == p_playlist->p_root )
- var_SetAddress( p_playlist, "item-change", p_item->p_input );
- PL_UNLOCK_IF( !b_locked );
- return p_item;
- }
- else
- {
- playlist_item_t *p_status_item = get_current_status_item( p_playlist );
- playlist_item_t *p_status_node = get_current_status_node( p_playlist );
- if( p_item == p_status_item )
- {
- /* We're deleting the current playlist item. Update
- * the playlist object to point at the previous item
- * so the playlist won't be restarted */
- playlist_item_t *p_prev_status_item = NULL;
- int i = 0;
- while( i < p_status_node->i_children &&
- p_status_node->pp_children[i] != p_status_item )
- {
- p_prev_status_item = p_status_node->pp_children[i];
- i++;
- }
- if( i == p_status_node->i_children )
- p_prev_status_item = NULL;
- if( p_prev_status_item )
- set_current_status_item( p_playlist, p_prev_status_item );
- }
-
- DeleteFromInput( p_playlist, p_item->p_input,
- p_playlist->p_root, false );
-
- pl_priv(p_playlist)->b_reset_currently_playing = true;
- vlc_cond_signal( &pl_priv(p_playlist)->signal );
-
- PL_UNLOCK_IF( !b_locked );
- return p_item->p_parent;
- }
-}
-
/**
* Find an item within a root, given its input id.
*
pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
vlc_cond_signal( &pl_priv(p_playlist)->signal );
}
- /* Preparse if PREPARSE or SPREPARSE & not enough meta */
+ /* Preparse if no artist/album info, and hasn't been preparsed allready
+ and if user has some preparsing option (auto-preparse variable)
+ enabled*/
char *psz_artist = input_item_GetArtist( p_item->p_input );
char *psz_album = input_item_GetAlbum( p_item->p_input );
if( pl_priv(p_playlist)->b_auto_preparse &&
- (i_mode & PLAYLIST_PREPARSE ||
- ( i_mode & PLAYLIST_SPREPARSE &&
+ input_item_IsPreparsed( p_item->p_input ) == false &&
( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
- ) ) )
- playlist_PreparseEnqueue( p_playlist, p_item->p_input, pl_Locked );
- /* If we already have it, signal it */
- else if( !EMPTY_STR( psz_artist ) && !EMPTY_STR( psz_album ) )
- input_item_SetPreparsed( p_item->p_input, true );
+ )
+ playlist_PreparseEnqueue( p_playlist, p_item->p_input );
free( psz_artist );
free( psz_album );
}
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
{
int i;
- if( p_item->i_children == -1 )
- p_item->i_children = 0;
+ if( p_item->i_children != -1 ) return;
+
+ p_item->i_children = 0;
+
+ input_item_t *p_input = p_item->p_input;
+ vlc_mutex_lock( &p_input->lock );
+ p_input->i_type = ITEM_TYPE_NODE;
+ vlc_mutex_unlock( &p_input->lock );
+
+ var_SetAddress( p_playlist, "item-change", p_item->p_input );
/* Remove it from the array of available items */
ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
bool b_stop )
{
+ assert( b_stop );
+ return playlist_NodeDelete( p_playlist, p_item, true, false );
+#if 0
int i;
int i_id = p_item->i_id;
PL_ASSERT_LOCKED;
playlist_ItemRelease( p_item );
return VLC_SUCCESS;
+#endif
+}
+
+static int RecursiveAddIntoParent (
+ playlist_t *p_playlist, playlist_item_t *p_parent,
+ input_item_node_t *p_node, int i_pos, bool b_flat,
+ playlist_item_t **pp_first_leaf )
+{
+ *pp_first_leaf = NULL;
+
+ if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
+
+ if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
+
+ for( int i = 0; i < p_node->i_children; i++ )
+ {
+ input_item_node_t *p_child_node = p_node->pp_children[i];
+
+ playlist_item_t *p_new_item = NULL;
+ bool b_children = p_child_node->i_children > 0;
+
+ //Create the playlist item represented by input node, if allowed.
+ if( !(b_flat && b_children) )
+ {
+ p_new_item = playlist_NodeAddInput( p_playlist,
+ p_child_node->p_item,
+ p_parent,
+ PLAYLIST_INSERT, i_pos,
+ pl_Locked );
+ if( !p_new_item ) return i_pos;
+
+ i_pos++;
+ }
+ //Recurse if any children
+ if( b_children )
+ {
+ //Substitute p_new_item for first child leaf
+ //(If flat, continue counting from current position)
+ int i_last_pos = RecursiveAddIntoParent(
+ p_playlist,
+ p_new_item ? p_new_item : p_parent,
+ p_child_node,
+ ( b_flat ? i_pos : 0 ),
+ b_flat,
+ &p_new_item );
+ //If flat, take position after recursion as current position
+ if( b_flat ) i_pos = i_last_pos;
+ }
+
+ assert( p_new_item != NULL );
+ if( i == 0 ) *pp_first_leaf = p_new_item;
+ }
+ return i_pos;
+}
+
+static int RecursiveInsertCopy (
+ playlist_t *p_playlist, playlist_item_t *p_item,
+ playlist_item_t *p_parent, int i_pos, bool b_flat )
+{
+ PL_ASSERT_LOCKED;
+ assert( p_parent != NULL && p_item != NULL );
+
+ if( p_item == p_parent ) return i_pos;
+
+ input_item_t *p_input = p_item->p_input;
+
+ if( !(p_item->i_children != -1 && b_flat) )
+ {
+ input_item_t *p_new_input = input_item_Copy( VLC_OBJECT(p_playlist),
+ p_input );
+ if( !p_new_input ) return i_pos;
+
+ playlist_item_t *p_new_item = NULL;
+ if( p_item->i_children == -1 )
+ p_new_item = playlist_NodeAddInput( p_playlist, p_new_input,
+ p_parent, PLAYLIST_INSERT, i_pos,
+ pl_Locked );
+ else
+ p_new_item = playlist_NodeCreate( p_playlist, NULL,
+ p_parent, i_pos, 0, p_new_input );
+ vlc_gc_decref( p_new_input );
+ if( !p_new_item ) return i_pos;
+
+ i_pos++;
+
+ if( p_new_item->i_children != -1 )
+ p_parent = p_new_item;
+ }
+
+ for( int i = 0; i < p_item->i_children; i++ )
+ {
+ if( b_flat )
+ i_pos = RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
+ p_parent, i_pos, true );
+ else
+ RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
+ p_parent, p_parent->i_children, false );
+ }
+
+ return i_pos;
}