]> git.sesse.net Git - vlc/blobdiff - src/playlist/playlist.c
* ./src/playlist/playlist.c: don't run the playlist by default.
[vlc] / src / playlist / playlist.c
index 888f5195a89cac09e822ae772c1d878395a8889d..eb71b608bee676726d3794e41e6e8ef94174ce57 100644 (file)
@@ -2,7 +2,7 @@
  * playlist.c : Playlist management functions
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: playlist.c,v 1.6 2002/06/07 14:30:41 sam Exp $
+ * $Id: playlist.c,v 1.14 2002/09/29 18:19:53 sam Exp $
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *
@@ -30,7 +30,7 @@
 #include "stream_control.h"
 #include "input_ext-intf.h"
 
-#include "playlist.h"
+#include "vlc_playlist.h"
 
 #define PLAYLIST_STOPPED 0
 #define PLAYLIST_RUNNING 1
@@ -62,12 +62,13 @@ playlist_t * __playlist_Create ( vlc_object_t *p_parent )
     }
 
     p_playlist->p_input = NULL;
-    p_playlist->i_status = PLAYLIST_RUNNING;
+    p_playlist->i_status = PLAYLIST_STOPPED;
     p_playlist->i_index = -1;
     p_playlist->i_size = 0;
     p_playlist->pp_items = NULL;
 
-    if( vlc_thread_create( p_playlist, "playlist", RunThread, VLC_TRUE ) )
+    if( vlc_thread_create( p_playlist, "playlist", RunThread,
+                           VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
     {
         msg_Err( p_playlist, "cannot spawn playlist thread" );
         vlc_object_destroy( p_playlist );
@@ -105,25 +106,30 @@ int playlist_Add( playlist_t *p_playlist, const char * psz_target,
 {
     playlist_item_t *p_item;
 
-    msg_Warn( p_playlist, "adding playlist item « %s »", psz_target );
-
-    vlc_mutex_lock( &p_playlist->object_lock );
+    msg_Dbg( p_playlist, "adding playlist item « %s »", psz_target );
 
     /* Create the new playlist item */
     p_item = malloc( sizeof( playlist_item_t ) );
     if( p_item == NULL )
     {
         msg_Err( p_playlist, "out of memory" );
-        vlc_mutex_unlock( &p_playlist->object_lock );
     }
 
     p_item->psz_name = strdup( psz_target );
     p_item->i_type = 0;
     p_item->i_status = 0;
 
+    vlc_mutex_lock( &p_playlist->object_lock );
+
     /* Do a few boundary checks and allocate space for the item */
     if( i_pos == PLAYLIST_END )
     {
+        if( i_mode & PLAYLIST_INSERT )
+        {
+            i_mode &= ~PLAYLIST_INSERT;
+            i_mode |= PLAYLIST_APPEND;
+        }
+
         i_pos = p_playlist->i_size - 1;
     }
 
@@ -160,14 +166,14 @@ int playlist_Add( playlist_t *p_playlist, const char * psz_target,
         }
 
         /* Now we know exactly where it goes. Just renumber the playlist */
-        for( i_index = p_playlist->i_size - 2; i_index > i_pos ; i_index-- )
+        for( i_index = p_playlist->i_size - 1; i_index > i_pos ; i_index-- )
         {
-            p_playlist->pp_items[i_index + 1] = p_playlist->pp_items[i_index];
+            p_playlist->pp_items[i_index] = p_playlist->pp_items[i_index - 1];
         }
 
         if( p_playlist->i_index >= i_pos )
         {
-            i_index++;
+            p_playlist->i_index++;
         }
     }
     else
@@ -179,7 +185,16 @@ int playlist_Add( playlist_t *p_playlist, const char * psz_target,
     }
 
     p_playlist->pp_items[i_pos] = p_item;
-    p_playlist->i_status = PLAYLIST_RUNNING;
+
+    if( i_mode & PLAYLIST_GO )
+    {
+        p_playlist->i_index = i_pos;
+        if( p_playlist->p_input )
+        {
+            input_StopThread( p_playlist->p_input );
+        }
+        p_playlist->i_status = PLAYLIST_RUNNING;
+    }
 
     vlc_mutex_unlock( &p_playlist->object_lock );
 
@@ -193,8 +208,43 @@ int playlist_Add( playlist_t *p_playlist, const char * psz_target,
  *****************************************************************************/
 int playlist_Delete( playlist_t * p_playlist, int i_pos )
 {
+    int i_index;
+
     vlc_mutex_lock( &p_playlist->object_lock );
 
+    if( i_pos >= 0 && i_pos < p_playlist->i_size )
+    {
+        msg_Dbg( p_playlist, "deleting playlist item « %s »",
+                             p_playlist->pp_items[i_pos]->psz_name );
+
+        free( p_playlist->pp_items[i_pos]->psz_name );
+        free( p_playlist->pp_items[i_pos] );
+        /* XXX: what if the item is still in use? */
+
+        if( i_pos < p_playlist->i_index )
+        {
+            p_playlist->i_index--;
+        }
+
+        /* Renumber the playlist */
+        for( i_index = i_pos + 1; i_index < p_playlist->i_size; i_index++ )
+        {
+            p_playlist->pp_items[i_index - 1] = p_playlist->pp_items[i_index];
+        }
+
+        p_playlist->i_size--;
+        if( p_playlist->i_size )
+        {
+            p_playlist->pp_items = realloc( p_playlist->pp_items,
+                                        p_playlist->i_size * sizeof(void*) );
+        }
+        else
+        {
+            free( p_playlist->pp_items );
+            p_playlist->pp_items = NULL;
+        }
+    }
+
     vlc_mutex_unlock( &p_playlist->object_lock );
 
     return 0;
@@ -280,7 +330,7 @@ static void RunThread ( playlist_t *p_playlist )
                 /* Unlink current input */
                 p_input = p_playlist->p_input;
                 p_playlist->p_input = NULL;
-                vlc_object_detach_all( p_input );
+                vlc_object_detach( p_input );
 
                 /* Release the playlist lock, because we may get stuck
                  * in input_DestroyThread() for some time. */
@@ -338,7 +388,7 @@ static void RunThread ( playlist_t *p_playlist )
             /* Unlink current input */
             p_input = p_playlist->p_input;
             p_playlist->p_input = NULL;
-            vlc_object_detach_all( p_input );
+            vlc_object_detach( p_input );
             vlc_mutex_unlock( &p_playlist->object_lock );
 
             /* Destroy input */
@@ -377,6 +427,7 @@ static void RunThread ( playlist_t *p_playlist )
 static void SkipItem( playlist_t *p_playlist, int i_arg )
 {
     int i_oldindex = p_playlist->i_index;
+    vlc_bool_t b_random;
 
     /* If the playlist is empty, there is no current item */
     if( p_playlist->i_size == 0 )
@@ -385,16 +436,33 @@ static void SkipItem( playlist_t *p_playlist, int i_arg )
         return;
     }
 
+    b_random = config_GetInt( p_playlist, "random" );
+
     /* Increment */
+    if( b_random )
+    {
+        srand( mdate() );
+
+        /* Simple random stuff - we cheat a bit to minimize the chances to
+         * get the same index again. */
+        i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
+        if( i_arg == 0 )
+        {
+            i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
+        }
+    }
+
     p_playlist->i_index += i_arg;
 
     /* Boundary check */
     if( p_playlist->i_index >= p_playlist->i_size )
     {
         if( p_playlist->i_status == PLAYLIST_STOPPED
+             || b_random
              || config_GetInt( p_playlist, "loop" ) )
         {
-            p_playlist->i_index = 0;
+            p_playlist->i_index -= p_playlist->i_size
+                         * ( p_playlist->i_index / p_playlist->i_size );
         }
         else
         {