+ i_new = 0;
+ for( i_count = 0; i_count < p_playlist->i_size - 1 ; i_count ++ )
+ {
+ i_new =
+ (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
+ /* Check if the item has not already been played */
+ if( p_playlist->pp_items[i_new]->i_nb_played == 0 )
+ break;
+ }
+ if( i_count == p_playlist->i_size )
+ {
+ /* The whole playlist has been played: reset the counters */
+ while( i_count > 0 )
+ {
+ p_playlist->pp_items[--i_count]->i_nb_played = 0;
+ }
+ if( !b_loop )
+ {
+ return NULL;
+ }
+ }
+ p_playlist->request.i_skip = 0;
+ p_playlist->request.b_request = VLC_FALSE;
+ return p_playlist->pp_items[i_new];
+ }
+
+ /* Start the real work */
+ if( p_playlist->request.b_request )
+ {
+ msg_Dbg( p_playlist,"processing request" );
+ /* We are not playing from a view */
+ if( p_playlist->request.i_view == -1 )
+ {
+#ifdef PLAYLIST_DEBUG
+ msg_Dbg( p_playlist, "non-view mode request");
+#endif
+ /* Directly select the item, just like now */
+ i_skip = p_playlist->request.i_skip;
+ i_goto = p_playlist->request.i_goto;
+
+ if( p_playlist->i_index == -1 ) p_playlist->i_index = 0;
+ p_new = p_playlist->pp_items[p_playlist->i_index];
+
+ if( i_goto >= 0 && i_goto < p_playlist->i_size )
+ {
+ p_playlist->i_index = i_goto;
+ p_new = p_playlist->pp_items[p_playlist->i_index];
+ p_playlist->request.i_goto = -1;
+ }
+
+ if( i_skip != 0 )
+ {
+ if( p_playlist->i_index + i_skip < p_playlist->i_size &&
+ p_playlist->i_index + i_skip >= 0 )
+ {
+ p_playlist->i_index += i_skip;
+ p_new = p_playlist->pp_items[p_playlist->i_index];
+ }
+ }
+ }
+ else
+ {
+#ifdef PLAYLIST_DEBUG
+ msg_Dbg( p_playlist, "view mode request" );
+#endif
+ p_new = p_playlist->request.p_item;
+ i_skip = p_playlist->request.i_skip;
+
+ /* If we are asked for a node, take its first item */
+ if( p_playlist->request.p_item == NULL && i_skip == 0 )
+ {
+ i_skip++;
+ }
+
+ p_view = playlist_ViewFind( p_playlist,p_playlist->request.i_view );
+ p_playlist->status.p_node = p_playlist->request.p_node;
+ p_playlist->status.i_view = p_playlist->request.i_view;
+ if( !p_view )
+ {
+ msg_Err( p_playlist, "p_view is NULL and should not! (FIXME)" );
+ }
+ else if( i_skip > 0 )
+ {
+ for( i = i_skip; i > 0 ; i-- )
+ {
+ p_new = playlist_FindNextFromParent( p_playlist,
+ p_playlist->request.i_view,
+ p_view->p_root,
+ p_playlist->request.p_node,
+ p_new );
+ if( p_new == NULL )
+ {
+ if( b_loop )
+ {
+ p_new = playlist_FindNextFromParent( p_playlist,
+ p_playlist->request.i_view,
+ p_view->p_root,
+ p_playlist->request.p_node,
+ NULL );
+ if( p_new == NULL ) break;
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+ }
+ else if( i_skip < 0 )
+ {
+ for( i = i_skip; i < 0 ; i++ )
+ {
+ p_new = playlist_FindPrevFromParent( p_playlist,
+ p_playlist->request.i_view,
+ p_view->p_root,
+ p_playlist->request.p_node,
+ p_new );
+ if( p_new == NULL ) break;
+ }