+/**
+ * Synchronise the current index of the playlist
+ * to match the index of the current item.
+ *
+ * \param p_playlist the playlist structure
+ * \param p_cur the current playlist item
+ * \return nothing
+ */
+static void ResyncCurrentIndex( playlist_t *p_playlist, playlist_item_t *p_cur )
+{
+ PL_DEBUG( "resyncing on %s", PLI_NAME( p_cur ) );
+ /* Simply resync index */
+ int i;
+ p_playlist->i_current_index = -1;
+ for( i = 0 ; i< p_playlist->current.i_size; i++ )
+ {
+ if( ARRAY_VAL( p_playlist->current, i ) == p_cur )
+ {
+ p_playlist->i_current_index = i;
+ break;
+ }
+ }
+ PL_DEBUG( "%s is at %i", PLI_NAME( p_cur ), p_playlist->i_current_index );
+}
+
+void ResetCurrentlyPlaying( playlist_t *p_playlist, bool b_random,
+ playlist_item_t *p_cur )
+{
+ playlist_item_t *p_next = NULL;
+ stats_TimerStart( p_playlist, "Items array build",
+ STATS_TIMER_PLAYLIST_BUILD );
+ PL_DEBUG( "rebuilding array of current - root %s",
+ PLI_NAME( p_playlist->status.p_node ) );
+ ARRAY_RESET( p_playlist->current );
+ p_playlist->i_current_index = -1;
+ while( 1 )
+ {
+ /** FIXME: this is *slow* */
+ p_next = playlist_GetNextLeaf( p_playlist,
+ p_playlist->status.p_node,
+ p_next, true, false );
+ if( p_next )
+ {
+ if( p_next == p_cur )
+ p_playlist->i_current_index = p_playlist->current.i_size;
+ ARRAY_APPEND( p_playlist->current, p_next);
+ }
+ else break;
+ }
+ PL_DEBUG("rebuild done - %i items, index %i", p_playlist->current.i_size,
+ p_playlist->i_current_index);
+ if( b_random )
+ {
+ /* Shuffle the array */
+ srand( (unsigned int)mdate() );
+ int j;
+ for( j = p_playlist->current.i_size - 1; j > 0; j-- )
+ {
+ int i = rand() % (j+1); /* between 0 and j */
+ playlist_item_t *p_tmp;
+ /* swap the two items */
+ p_tmp = ARRAY_VAL(p_playlist->current, i);
+ ARRAY_VAL(p_playlist->current,i) = ARRAY_VAL(p_playlist->current,j);
+ ARRAY_VAL(p_playlist->current,j) = p_tmp;
+ }
+ }
+ p_playlist->b_reset_currently_playing = false;
+ stats_TimerStop( p_playlist, STATS_TIMER_PLAYLIST_BUILD );
+}
+
+/**
+ * Compute the next playlist item depending on
+ * the playlist course mode (forward, backward, random, view,...).
+ *
+ * \param p_playlist the playlist object
+ * \return nothing
+ */