- vlc_bool_t b_loop = var_GetBool( p_playlist, "loop" );
- vlc_bool_t b_random = var_GetBool( p_playlist, "random" );
- vlc_bool_t b_repeat = var_GetBool( p_playlist, "repeat" );
- vlc_bool_t b_playstop = var_GetBool( p_playlist, "play-and-stop" );
-
- /* Handle quickly a few special cases */
- /* No items to play */
- if( p_playlist->i_size == 0 )
- {
- msg_Info( p_playlist, "playlist is empty" );
- return NULL;
- }
-
- /* Repeat and play/stop */
- if( !p_playlist->request.b_request && b_repeat == VLC_TRUE &&
- p_playlist->status.p_item )
- {
- msg_Dbg( p_playlist,"repeating item" );
- return p_playlist->status.p_item;
- }
- if( !p_playlist->request.b_request && b_playstop == VLC_TRUE )
- {
- msg_Dbg( p_playlist,"stopping (play and stop)");
- return NULL;
- }
-
- if( !p_playlist->request.b_request && p_playlist->status.p_item &&
- p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
- {
- msg_Dbg( p_playlist, "blocking item, stopping") ;
- return NULL;
- }
-
- /* Random case. This is an exception: if request, but request is skip +- 1
- * we don't go to next item but select a new random one. */
- if( b_random &&
- ( !p_playlist->request.b_request ||
- ( p_playlist->request.b_request &&
- ( p_playlist->request.p_item == NULL ||
- p_playlist->request.i_skip == 1 ||
- p_playlist->request.i_skip == -1 ) ) ) )
- {
- PL_DEBUG( "doing random, have %i items, currently at %i, reset %i\n",
- p_playlist->i_random, p_playlist->i_random_index,
- p_playlist->b_reset_random );
- if( p_playlist->b_reset_random )
- {
- int j;
- FREE( p_playlist->pp_random );
- if( !p_playlist->b_reset_random && !b_loop ) goto end;
- p_playlist->i_random = 0;
- p_playlist->i_random_index = 0;
- p_playlist->i_random = playlist_GetAllEnabledChildren(
- p_playlist,
- p_playlist->status.p_node,
- &p_playlist->pp_random );
- /* Shuffle the array */
- srand( (unsigned int)mdate() );
- int swap = 0;
- for( j = p_playlist->i_random -1; j > 0; j-- )
- {
- swap++;
- int i = rand() % (j+1); /* between 0 and j */
- playlist_item_t *p_tmp;
- p_tmp = p_playlist->pp_random[i];
- p_playlist->pp_random[i] = p_playlist->pp_random[j];
- p_playlist->pp_random[j] = p_tmp;
- }
- p_playlist->b_reset_random = VLC_FALSE;
- PL_DEBUG( "random rebuilt, have %i items", p_playlist->i_random );
- }
- else
- {
- /* Go backward or forward */
- if( !p_playlist->request.b_request || !p_playlist->request.p_item ||
- p_playlist->request.i_skip == 1 )
- p_playlist->i_random_index++;
- else
- p_playlist->i_random_index--;
- /* Handle bounds situations */
- if( p_playlist->i_random_index == -1 )
- {
- if( !b_loop || p_playlist->i_random == 0 ) goto end;
- p_playlist->i_random_index = p_playlist->i_random - 1;
- }
- else if( p_playlist->i_random_index == p_playlist->i_random )
- {
- if( !b_loop || p_playlist->i_random == 0 ) goto end;
- p_playlist->i_random_index = 0;
- }
- }
- PL_DEBUG( "using random item %i", p_playlist->i_random_index );
- if ( p_playlist->i_random == 0 ) goto end; /* Can this happen ?? */
- p_new = p_playlist->pp_random[p_playlist->i_random_index];
-end:
- if( !p_new ) p_playlist->b_reset_random = VLC_TRUE;
- p_playlist->request.i_skip = 0;
- p_playlist->request.b_request = VLC_FALSE;
- return p_new;
- }
-
- /* Start the real work */
- if( p_playlist->request.b_request )
- {
- PL_DEBUG( "processing request node %s item %s skip %i",
- PLI_NAME( p_playlist->request.p_item ),
- PLI_NAME( p_playlist->request.p_node ), i_skip );
- p_new = p_playlist->request.p_item;
- i_skip = p_playlist->request.i_skip;
-
- if( p_playlist->request.p_node )
- p_playlist->status.p_node = p_playlist->request.p_node;
-
- /* If we are asked for a node, dont take it */
- if( i_skip == 0 && ( p_new == NULL || p_new->i_children != -1 ) )
- i_skip++;
-
- if( i_skip > 0 )
- {
- for( i = i_skip; i > 0 ; i-- )
- {
- p_new = playlist_GetNextLeaf( p_playlist,
- p_playlist->request.p_node,
- p_new, VLC_TRUE, VLC_FALSE );
- if( p_new == NULL )
- {
- PL_DEBUG( "looping - restarting at beginning of node" );
- p_new = playlist_GetNextLeaf( p_playlist,
- p_playlist->request.p_node,
- NULL, VLC_TRUE, VLC_FALSE);
- if( p_new == NULL ) break;
- }
- }
- }
- else if( i_skip < 0 )
- {
- for( i = i_skip; i < 0 ; i++ )
- {
- p_new = playlist_GetPrevLeaf( p_playlist,
- p_playlist->request.p_node,
- p_new, VLC_FALSE, VLC_FALSE );
- if( p_new == NULL )
- {
- PL_DEBUG( "looping - restarting at end of node" );
- /** \bug This is needed because GetPrevLeaf does not loop
- * by itself */
- p_new = playlist_GetLastLeaf( p_playlist,
- p_playlist->request.p_node );
- }
- if( p_new == NULL ) break;
- }
- }
- /* Clear the request */
- p_playlist->request.b_request = VLC_FALSE;
- }
- /* "Automatic" item change ( next ) */
- else
- {
- PL_DEBUG( "changing item without a request" );
- /* Cant go to next from current item */
- if( p_playlist->status.p_item &&
- p_playlist->status.p_item->i_flags & PLAYLIST_SKIP_FLAG )
- return NULL;
-
- p_new = playlist_GetNextLeaf( p_playlist,
- p_playlist->status.p_node,
- p_playlist->status.p_item,
- VLC_TRUE, VLC_FALSE );
- if( p_new == NULL && b_loop )
- {
- PL_DEBUG( "looping" );
- p_new = playlist_GetNextLeaf( p_playlist,
- p_playlist->status.p_node,
- NULL, VLC_TRUE, VLC_FALSE );
- }
- /* The new item can't be autoselected */
- if( p_new != NULL && p_new->i_flags & PLAYLIST_SKIP_FLAG )
- return NULL;
- }
- if( p_new == NULL )
- {
- msg_Dbg( p_playlist, "did not find something to play" );
- }
- return p_new;