control.c 5.31 KB
Newer Older
1
/*****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
2
 * control.c : Handle control of the playlist & running through it
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2004 VLC authors and VideoLAN
Jean-Paul Saman's avatar
Jean-Paul Saman committed
5
 * $Id$
6 7 8 9
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *          Clément Stenac <zorglub@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
29
#include "vlc_playlist.h"
30
#include "playlist_internal.h"
31
#include <assert.h>
32 33 34 35 36

/*****************************************************************************
 * Playlist control
 *****************************************************************************/

37 38
void playlist_Lock( playlist_t *pl )
{
39
    vlc_mutex_lock( &pl_priv(pl)->lock );
40 41 42 43
}

void playlist_Unlock( playlist_t *pl )
{
44
    vlc_mutex_unlock( &pl_priv(pl)->lock );
45 46 47 48
}

void playlist_AssertLocked( playlist_t *pl )
{
49
    vlc_assert_locked( &pl_priv(pl)->lock );
50 51
}

52
static void playlist_vaControl( playlist_t *p_playlist, int i_query, va_list args )
53
{
54 55
    PL_ASSERT_LOCKED;

56 57
    if( i_query != PLAYLIST_STOP )
        if( pl_priv(p_playlist)->killed || playlist_IsEmpty( p_playlist ) )
58
            return;
59 60 61 62

    switch( i_query )
    {
    case PLAYLIST_STOP:
63 64 65
        pl_priv(p_playlist)->request.i_status = PLAYLIST_STOPPED;
        pl_priv(p_playlist)->request.b_request = true;
        pl_priv(p_playlist)->request.p_item = NULL;
66 67
        break;

68 69
    // Node can be null, it will keep the same. Use with care ...
    // Item null = take the first child of node
70
    case PLAYLIST_VIEWPLAY:
71 72 73 74
    {
        playlist_item_t *p_node = va_arg( args, playlist_item_t * );
        playlist_item_t *p_item = va_arg( args, playlist_item_t * );

75 76
        if ( p_node == NULL )
        {
77
            p_node = get_current_status_node( p_playlist );
78
            assert( p_node );
79
        }
80 81 82 83 84
        pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
        pl_priv(p_playlist)->request.i_skip = 0;
        pl_priv(p_playlist)->request.b_request = true;
        pl_priv(p_playlist)->request.p_node = p_node;
        pl_priv(p_playlist)->request.p_item = p_item;
Clément Stenac's avatar
Clément Stenac committed
85
        if( p_item && var_GetBool( p_playlist, "random" ) )
86
            pl_priv(p_playlist)->b_reset_currently_playing = true;
87
        break;
88
    }
89 90

    case PLAYLIST_PLAY:
91
        if( pl_priv(p_playlist)->p_input )
92
        {
93
            pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
Laurent Aimar's avatar
Laurent Aimar committed
94
            var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
95 96
            break;
        }
97 98
        else
        {
99 100 101 102 103
            pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
            pl_priv(p_playlist)->request.b_request = true;
            pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
            pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
            pl_priv(p_playlist)->request.i_skip = 0;
104
        }
105 106
        break;

107
    case PLAYLIST_TOGGLE_PAUSE:
108
        if( !pl_priv(p_playlist)->p_input )
109 110 111
        {   /* FIXME: is this really useful without input? */
            pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
            /* return without notifying the playlist thread as there is nothing to do */
112
            return;
113 114 115
        }

        if( var_GetInteger( pl_priv(p_playlist)->p_input, "state" ) == PAUSE_S )
116
        {
117
            pl_priv(p_playlist)->status.i_status = PLAYLIST_RUNNING;
118
            var_SetInteger( pl_priv(p_playlist)->p_input, "state", PLAYING_S );
119 120 121
        }
        else
        {
122
            pl_priv(p_playlist)->status.i_status = PLAYLIST_PAUSED;
123
            var_SetInteger( pl_priv(p_playlist)->p_input, "state", PAUSE_S );
124 125 126 127
        }
        break;

    case PLAYLIST_SKIP:
128 129 130
        pl_priv(p_playlist)->request.p_node = get_current_status_node( p_playlist );
        pl_priv(p_playlist)->request.p_item = get_current_status_item( p_playlist );
        pl_priv(p_playlist)->request.i_skip = (int) va_arg( args, int );
131
        /* if already running, keep running */
132 133 134
        if( pl_priv(p_playlist)->status.i_status != PLAYLIST_STOPPED )
            pl_priv(p_playlist)->request.i_status = pl_priv(p_playlist)->status.i_status;
        pl_priv(p_playlist)->request.b_request = true;
135 136
        break;
    }
137
    vlc_cond_signal( &pl_priv(p_playlist)->signal );
138
}
139

140 141 142 143 144 145 146 147 148
void playlist_Control( playlist_t *p_playlist, int query, bool locked, ... )
{
    va_list args;

    PL_LOCK_IF( !locked );
    va_start( args, locked );
    playlist_vaControl( p_playlist, query, args );
    va_end( args );
    PL_UNLOCK_IF( !locked );
149
}