]> git.sesse.net Git - vlc/blob - src/playlist/item.c
return -1 in a non-void function
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item creation/deletion/add/removal functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #include <vlc/vlc.h>
25 #include <vlc/input.h>
26
27 #include "vlc_playlist.h"
28
29 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
30               playlist_item_t *p_node, int i_pos );
31 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
32                     playlist_item_t *p_item );
33 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
34 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
35                           vlc_bool_t b_stop );
36
37 /*****************************************************************************
38  * Playlist item creation
39  *****************************************************************************/
40 /**
41  * Create a new item, without adding it to the playlist
42  *
43  * \param p_obj a vlc object (anyone will do)
44  * \param psz_uri the mrl of the item
45  * \param psz_name a text giving a name or description of the item
46  * \return the new item or NULL on failure
47  */
48 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
49                                       const char *psz_uri,
50                                       const char *psz_name )
51 {
52     return playlist_ItemNewWithType( p_obj, psz_uri,
53                                      psz_name, 0, NULL, -1,
54                                      ITEM_TYPE_UNKNOWN );
55 }
56
57 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
58                                             const char *psz_uri,
59                                             const char *psz_name,
60                                             int i_options,
61                                             const char **ppsz_options,
62                                             int i_duration,
63                                             int i_type )
64 {
65     if( psz_uri == NULL ) return NULL;
66     input_item_t *p_input = input_ItemNewWithType( p_obj, psz_uri,
67                                         psz_name, i_options, ppsz_options,
68                                         i_duration, i_type );
69     return playlist_ItemNewFromInput( p_obj, p_input );
70 }
71
72 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
73                                             input_item_t *p_input )
74 {
75     /** FIXME !!!!! don't find playlist each time */
76     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_obj, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
77     playlist_item_t * p_item;
78     p_item = malloc( sizeof( playlist_item_t ) );
79     if( p_item == NULL ) return NULL;
80
81     p_item->p_input = p_input;
82     vlc_gc_incref( p_item->p_input );
83
84     p_item->i_id = ++p_playlist->i_last_playlist_id;
85
86     p_item->p_parent = NULL;
87     p_item->i_children = -1;
88     p_item->pp_children = NULL;
89     p_item->i_flags = 0;
90
91     vlc_object_release( p_playlist );
92
93     return p_item;
94 }
95
96 /**
97  * Copy a playlist item - FIXME: Rewrite FIXME
98  *
99  * Creates a new item with name, mrl and meta infor like the
100  * source. Does not copy children for node type items.
101  * \param p_obj any vlc object, needed for mutex init
102  * \param p_item the item to copy
103  * \return pointer to the new item, or NULL on error
104  * \note function takes the lock on p_item
105  */
106 playlist_item_t *__playlist_ItemCopy( vlc_object_t *p_obj,
107                                       playlist_item_t *p_item )
108 {
109     playlist_item_t *p_res;
110     int i;
111     vlc_mutex_lock( &p_item->p_input->lock );
112
113     p_res = malloc( sizeof( playlist_item_t ) );
114     if( p_res == NULL )
115     {
116         vlc_mutex_unlock( &p_item->p_input->lock );
117         return NULL;
118     }
119
120     *p_res = *p_item;
121     vlc_mutex_init( p_obj, &p_res->p_input->lock );
122
123     if( p_item->p_input->i_options )
124         p_res->p_input->ppsz_options =
125             malloc( p_item->p_input->i_options * sizeof(char*) );
126     for( i = 0; i < p_item->p_input->i_options; i++ )
127     {
128         p_res->p_input->ppsz_options[i] = strdup( p_item->p_input->ppsz_options[i] );
129     }
130
131     if( p_item->i_children != -1 )
132     {
133         msg_Warn( p_obj, "not copying playlist-item's children" );
134         p_res->i_children = -1;
135         p_res->pp_children = NULL;
136     }
137     p_res->p_parent = NULL;
138
139     if( p_item->p_input->psz_name )
140         p_res->p_input->psz_name = strdup( p_item->p_input->psz_name );
141     if( p_item->p_input->psz_uri )
142         p_res->p_input->psz_uri = strdup( p_item->p_input->psz_uri );
143
144     if( p_item->p_input->i_es )
145     {
146         p_res->p_input->es =
147             (es_format_t**)malloc( p_item->p_input->i_es * sizeof(es_format_t*));
148         for( i = 0; i < p_item->p_input->i_es; i++ )
149         {
150             p_res->p_input->es[ i ] = (es_format_t*)malloc(sizeof(es_format_t*));
151             es_format_Copy( p_res->p_input->es[ i ],
152                          p_item->p_input->es[ i ] );
153         }
154     }
155     if( p_item->p_input->i_categories )
156     {
157         p_res->p_input->pp_categories = NULL;
158         p_res->p_input->i_categories = 0;
159         for( i = 0; i < p_item->p_input->i_categories; i++ )
160         {
161             info_category_t *p_incat;
162             p_incat = p_item->p_input->pp_categories[i];
163             if( p_incat->i_infos )
164             {
165                 int j;
166                 for( j = 0; j < p_incat->i_infos; j++ )
167                 {
168                     vlc_input_item_AddInfo( p_res->p_input, p_incat->psz_name,
169                                             p_incat->pp_infos[j]->psz_name,
170                                             "%s",
171                                             p_incat->pp_infos[j]->psz_value );
172                 }
173             }
174         }
175     }
176
177     vlc_mutex_unlock( &p_item->p_input->lock );
178     return p_res;
179 }
180
181 /***************************************************************************
182  * Playlist item destruction
183  ***************************************************************************/
184
185 /** Delete a playlist item and detach its input item */
186 int playlist_ItemDelete( playlist_item_t *p_item )
187 {
188     vlc_gc_decref( p_item->p_input );
189     free( p_item );
190     return VLC_SUCCESS;
191 }
192
193 /** Remove an input item from ONELEVEL and CATEGORY */
194 int playlist_DeleteAllFromInput( playlist_t *p_playlist, int i_input_id )
195 {
196     playlist_DeleteFromInput( p_playlist, i_input_id,
197                               p_playlist->p_root_category, VLC_TRUE );
198     playlist_DeleteFromInput( p_playlist, i_input_id,
199                               p_playlist->p_root_onelevel, VLC_TRUE );
200     return VLC_SUCCESS;
201 }
202
203 /** Remove an input item from ONELEVEL and CATEGORY.
204  * This function must be entered without the playlist lock */
205 int playlist_LockDeleteAllFromInput( playlist_t * p_playlist, int i_id )
206 {
207     int i_ret;
208     vlc_mutex_lock( &p_playlist->object_lock );
209     i_ret = playlist_DeleteAllFromInput( p_playlist, i_id );
210     vlc_mutex_unlock( &p_playlist->object_lock );
211     return i_ret;
212 }
213
214 /** Remove an input item when it appears from a root playlist item */
215 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
216                               playlist_item_t *p_root, vlc_bool_t b_do_stop )
217 {
218     int i;
219     for( i = 0 ; i< p_root->i_children ; i++ )
220     {
221         if( p_root->pp_children[i]->i_children == -1 &&
222             p_root->pp_children[i]->p_input->i_id == i_input_id )
223         {
224             DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
225         }
226         else if( p_root->pp_children[i]->i_children >= 0 )
227         {
228             return playlist_DeleteFromInput( p_playlist, i_input_id,
229                                         p_root->pp_children[i], b_do_stop );
230         }
231     }
232     return -1;
233 }
234
235 /** Remove a playlist item from the playlist, given its id */
236 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
237 {
238     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
239     if( !p_item ) return VLC_EGENERIC;
240     return DeleteInner( p_playlist, p_item, VLC_TRUE );
241 }
242
243 /** Remove a playlist item from the playlist, given its id
244  * This function should be entered without the playlist lock */
245 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
246 {
247     int i_ret;
248     vlc_mutex_lock( &p_playlist->object_lock );
249     i_ret = playlist_DeleteFromItemId( p_playlist, i_id );
250     vlc_mutex_unlock( &p_playlist->object_lock );
251     return i_ret;
252 }
253
254 /** Clear the playlist */
255 void playlist_Clear( playlist_t * p_playlist )
256 {
257     playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
258     playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
259 }
260 /** Clear the playlist. This function must be entered without the lock */
261 void playlist_LockClear( playlist_t *p_playlist )
262 {
263     vlc_mutex_lock( &p_playlist->object_lock );
264     playlist_Clear( p_playlist );
265     vlc_mutex_unlock( &p_playlist->object_lock );
266 }
267
268 /***************************************************************************
269  * Playlist item addition
270  ***************************************************************************/
271
272 /**
273  * Add a MRL into the playlist.
274  *
275  * \param p_playlist the playlist to add into
276  * \param psz_uri the mrl to add to the playlist
277  * \param psz_name a text giving a name or description of this item
278  * \param i_mode the mode used when adding
279  * \param i_pos the position in the playlist where to add. If this is
280  *        PLAYLIST_END the item will be added at the end of the playlist
281  *        regardless of it's size
282  * \return The id of the playlist item
283  */
284 int playlist_PlaylistAdd( playlist_t *p_playlist, const char *psz_uri,
285                           const char *psz_name, int i_mode, int i_pos )
286 {
287     return playlist_PlaylistAddExt( p_playlist, psz_uri, psz_name,
288                                     i_mode, i_pos, -1, NULL, 0 );
289 }
290
291 /**
292  * Add a MRL into the playlist, duration and options given
293  *
294  * \param p_playlist the playlist to add into
295  * \param psz_uri the mrl to add to the playlist
296  * \param psz_name a text giving a name or description of this item
297  * \param i_mode the mode used when adding
298  * \param i_pos the position in the playlist where to add. If this is
299  *        PLAYLIST_END the item will be added at the end of the playlist
300  *        regardless of it's size
301  * \param i_duration length of the item in milliseconds.
302  * \param ppsz_options an array of options
303  * \param i_options the number of options
304  * \return The id of the playlist item
305 */
306 int playlist_PlaylistAddExt( playlist_t *p_playlist, const char * psz_uri,
307                              const char *psz_name, int i_mode, int i_pos,
308                              mtime_t i_duration, const char **ppsz_options,
309                              int i_options )
310 {
311     input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
312                                               i_options, ppsz_options,
313                                               i_duration );
314
315     return playlist_PlaylistAddInput( p_playlist, p_input, i_mode, i_pos );
316 }
317
318 /** Add an input item to the playlist node */
319 int playlist_PlaylistAddInput( playlist_t* p_playlist, input_item_t *p_input,
320                                int i_mode, int i_pos )
321 {
322     playlist_item_t *p_item;
323     p_input->i_id = ++p_playlist->i_last_input_id;
324
325     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
326              p_input->psz_name, p_input->psz_uri );
327
328     vlc_mutex_lock( &p_playlist->object_lock );
329
330     /* Add to ONELEVEL */
331     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
332     if( p_item == NULL ) return VLC_EGENERIC;
333     AddItem( p_playlist, p_item,p_playlist->p_local_onelevel, i_pos );
334
335     /* Add to CATEGORY */
336     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
337     if( p_item == NULL ) return VLC_EGENERIC;
338     AddItem( p_playlist, p_item, p_playlist->p_local_category, i_pos );
339
340     GoAndPreparse( p_playlist, i_mode, p_item );
341
342     vlc_mutex_unlock( &p_playlist->object_lock );
343     return VLC_SUCCESS;
344 }
345
346 /** Add an input item to p_direct_parent in the category tree, and to the
347  *  matching top category in onelevel **/
348 int playlist_BothAddInput( playlist_t *p_playlist,
349                            input_item_t *p_input,
350                            playlist_item_t *p_direct_parent,
351                            int i_mode, int i_pos )
352 {
353     playlist_item_t *p_item, *p_up;
354     int i_top;
355     vlc_mutex_lock( & p_playlist->object_lock );
356
357     /* Add to category */
358     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
359     if( p_item == NULL ) return VLC_EGENERIC;
360     fprintf( stderr, "Adding to CATEGORY\n");
361     AddItem( p_playlist, p_item, p_direct_parent, i_pos );
362
363     /* Add to onelevel */
364     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
365     if( p_item == NULL ) return VLC_EGENERIC;
366     fprintf( stderr, "Adding to ONE\n");
367
368     p_up = p_direct_parent;
369     while( p_up->p_parent != p_playlist->p_root_category )
370     {
371         p_up = p_up->p_parent;
372     }
373     for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
374     {
375         if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id == p_up->p_input->i_id )
376         {
377             AddItem( p_playlist, p_item,
378                      p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
379             break;
380         }
381     }
382     GoAndPreparse( p_playlist, i_mode, p_item );
383
384     vlc_mutex_unlock( &p_playlist->object_lock );
385     return VLC_SUCCESS;
386 }
387
388 /**
389  * Add an item where it should be added, when adding from a node
390  * (ex: directory access, playlist demuxers, services discovery, ... )
391  * \param p_playlist the playlist
392  * \param p_input the input to add
393  * \param p_parent the direct node
394  * \param p_item_in_category the item within category root (as returned by playlist_ItemToNode)
395  * \param b_forced_parent Are we forced to add only to p_parent ?
396  */
397 void playlist_AddWhereverNeeded( playlist_t *p_playlist, input_item_t *p_input,
398                                  playlist_item_t *p_parent,
399                                  playlist_item_t *p_item_in_category,
400                                  vlc_bool_t b_forced_parent, int i_mode )
401 {
402     /* If we have forced a parent :
403      *   - Just add the input to the forced parent (which should be p_parent)
404      * Else
405      *    - If we have item in category, add to it, and to onelevel (bothadd)
406      *    - If we don't, just add to p_parent
407      */
408     if( b_forced_parent == VLC_TRUE || !p_item_in_category  )
409     {
410         playlist_NodeAddInput( p_playlist, p_input, p_parent, i_mode,
411                                PLAYLIST_END );
412     }
413     else
414     {
415         playlist_BothAddInput( p_playlist, p_input, p_item_in_category,
416                                i_mode, PLAYLIST_END );
417     }
418 }
419
420
421 /** Add an input item to a given node */
422 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
423                                          input_item_t *p_input,
424                                          playlist_item_t *p_parent,
425                                          int i_mode, int i_pos )
426 {
427     playlist_item_t *p_item;
428
429     /* Sanity checks */
430     if( !p_parent || p_parent->i_children == -1 )
431     {
432         msg_Err( p_playlist, "invalid node" );
433     }
434
435     vlc_mutex_lock( &p_playlist->object_lock );
436
437     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
438     if( p_item == NULL ) return NULL;
439     AddItem( p_playlist, p_item, p_parent, i_pos );
440
441     vlc_mutex_unlock( &p_playlist->object_lock );
442
443     return p_item;
444 }
445
446 /** Add a playlist item to a given node */
447 void playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
448                            playlist_item_t *p_parent, int i_mode, int i_pos )
449 {
450     vlc_mutex_lock( &p_playlist->object_lock );
451     AddItem( p_playlist, p_item, p_parent, i_pos );
452     vlc_mutex_unlock( &p_playlist->object_lock );
453 }
454
455 /*****************************************************************************
456  * Playlist item misc operations
457  *****************************************************************************/
458
459 /**
460  * Transform an item to a node. Return the node in the category tree, or NULL
461  * if not found there
462  * This function must be entered without the playlist lock
463  */
464 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
465                                       playlist_item_t *p_item )
466 {
467     /* What we do
468      * Find the input in CATEGORY.
469      *  - If we find it
470      *    - change it to node
471      *    - we'll return it at the end
472      *    - Delete the input from ONELEVEL
473      *  - If we don't find it, just change to node (we are probably in VLM)
474      *    and return NULL
475      *
476      * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
477      * useful for later BothAddInput )
478      */
479
480     /** \todo First look if we don't already have it */
481     playlist_item_t *p_item_in_category = playlist_ItemFindFromInputAndRoot(
482                                             p_playlist, p_item->p_input->i_id,
483                                             p_playlist->p_root_category );
484
485     if( p_item_in_category )
486     {
487         ChangeToNode( p_playlist, p_item_in_category );
488         playlist_DeleteFromInput( p_playlist, p_item->p_input->i_id,
489                                   p_playlist->p_root_onelevel, VLC_FALSE );
490         var_SetInteger( p_playlist, "item-change", p_item->p_input->i_id );
491         return p_item_in_category;
492     }
493     else
494     {
495         ChangeToNode( p_playlist, p_item );
496         return NULL;
497     }
498 }
499
500 /** Transform an item to a node
501  *  This function must be entered without the playlist lock
502  *  \see playlist_ItemToNode
503  */
504 playlist_item_t * playlist_LockItemToNode( playlist_t *p_playlist,
505                                            playlist_item_t *p_item )
506 {
507     playlist_item_t *p_ret;
508     vlc_mutex_lock( &p_playlist->object_lock );
509     p_ret = playlist_ItemToNode( p_playlist, p_item );
510     vlc_mutex_unlock( &p_playlist->object_lock );
511     return p_ret;
512 }
513
514 /** Find an item within a root, given its input id.
515  * \return the first found item, or NULL if not found
516  */
517 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
518                                                     int i_input_id,
519                                                     playlist_item_t *p_root )
520 {
521     int i;
522     for( i = 0 ; i< p_root->i_children ; i++ )
523     {
524         if( p_root->pp_children[i]->i_children == -1 &&
525             p_root->pp_children[i]->p_input->i_id == i_input_id )
526         {
527             return p_root->pp_children[i];
528         }
529         else if( p_root->pp_children[i]->i_children >= 0 )
530         {
531             playlist_item_t *p_search =
532                  playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
533                                                     p_root->pp_children[i] );
534             if( p_search ) return p_search;
535         }
536     }
537     return NULL;
538 }
539
540
541 /**
542  * Moves an item
543  *
544  * This function must be entered with the playlist lock
545  *
546  * \param p_playlist the playlist
547  * \param p_item the item to move
548  * \param p_node the new parent of the item
549  * \param i_newpos the new position under this new parent
550  * \return VLC_SUCCESS or an error
551  */
552 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
553                        playlist_item_t *p_node, int i_newpos )
554 {
555     int j;
556     playlist_item_t *p_detach = NULL;
557
558     if( p_node->i_children == -1 ) return VLC_EGENERIC;
559
560     p_detach = p_item->p_parent;
561     for( j = 0; j < p_detach->i_children; j++ )
562     {
563          if( p_detach->pp_children[j] == p_item ) break;
564     }
565     REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
566
567     /* Attach to new parent */
568     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
569
570     return VLC_SUCCESS;
571 }
572
573 /** Send a notification that an item has been added to a node */
574 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
575                              int i_node_id )
576 {
577     vlc_value_t val;
578     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
579     p_add->i_item = i_item_id;
580     p_add->i_node = i_node_id;
581     val.p_address = p_add;
582     var_Set( p_playlist, "item-append", val );
583     free( p_add );
584 }
585
586 /*****************************************************************************
587  * Playlist item accessors
588  *****************************************************************************/
589
590 /** Set the name of a playlist item */
591 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
592 {
593     if( psz_name && p_item )
594     {
595         if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
596         p_item->p_input->psz_name = strdup( psz_name );
597         return VLC_SUCCESS;
598     }
599     return VLC_EGENERIC;
600 }
601
602 /** Set the duration of a playlist item
603  * \param i_duration the new duration in microseconds
604  */
605 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
606 {
607     char psz_buffer[MSTRTIME_MAX_SIZE];
608     if( p_item )
609     {
610         p_item->p_input->i_duration = i_duration;
611         if( i_duration != -1 )
612         {
613             secstotimestr( psz_buffer, (int)(i_duration/1000000) );
614         }
615         else
616         {
617             memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
618         }
619         vlc_input_item_AddInfo( p_item->p_input, _("General") , _("Duration"),
620                                 "%s", psz_buffer );
621
622         return VLC_SUCCESS;
623     }
624     return VLC_EGENERIC;
625 }
626
627 /** Add an option to a playlist item */
628 void playlist_ItemAddOption( playlist_item_t *p_item,
629                              const char *psz_option)
630 {
631     vlc_input_item_AddOption( p_item->p_input, psz_option );
632 }
633
634 /***************************************************************************
635  * The following functions are local
636  ***************************************************************************/
637
638 /* Enqueue an item for preparsing, and play it, if needed */
639 void GoAndPreparse( playlist_t *p_playlist, int i_mode,
640                     playlist_item_t *p_item )
641 {
642 #if 0
643     if( (i_mode & PLAYLIST_GO ) && p_view )
644     {
645         p_playlist->request.b_request = VLC_TRUE;
646         /* FIXME ... */
647         p_playlist->request.i_view = VIEW_CATEGORY;
648         p_playlist->request.p_node = p_view->p_root;
649         p_playlist->request.p_item = p_item;
650
651         if( p_playlist->p_input )
652         {
653             input_StopThread( p_playlist->p_input );
654         }
655         p_playlist->status.i_status = PLAYLIST_RUNNING;
656     }
657 #endif
658     if( i_mode & PLAYLIST_PREPARSE &&
659         var_CreateGetBool( p_playlist, "auto-preparse" ) )
660     {
661         playlist_PreparseEnqueue( p_playlist, p_item->p_input );
662     }
663 }
664
665 /* Add the playlist item to the requested node and fire a notification */
666 void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
667               playlist_item_t *p_node, int i_pos )
668 {
669     INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size,
670                  p_playlist->i_size, p_item );
671 #if 0
672     fprintf( stderr, "Adding input %s (id %i) - playlist item id %i "
673                      "to node %s (id %i)\n",
674                      p_item->p_input->psz_name, p_item->p_input->i_id,
675                      p_item->i_id, p_node->p_input->psz_name,
676                      p_node->i_id );
677 #endif
678     INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
679                  p_playlist->i_all_size, p_item );
680     p_playlist->i_enabled ++;
681
682     if( i_pos == PLAYLIST_END )
683     {
684         playlist_NodeAppend( p_playlist, p_item, p_node );
685     }
686     else
687     {
688         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
689     }
690
691     playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
692 }
693
694 /* Actually convert an item to a node */
695 void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
696 {
697     int i;
698     if( p_item->i_children == -1 )
699         p_item->i_children = 0;
700
701     /* Remove it from the array of available items */
702     for( i = 0 ; i < p_playlist->i_size ; i++ )
703     {
704         if( p_item == p_playlist->pp_items[i] )
705         {
706             REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
707         }
708     }
709 }
710
711 /* Do the actual removal */
712 int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
713                 vlc_bool_t b_stop )
714 {
715     int i, i_top, i_bottom;
716     int i_id = p_item->i_id;
717     vlc_bool_t b_flag = VLC_FALSE;
718
719     //fprintf( stderr, "Deleting item %i - %s\n", i_id,
720     //                                            p_item->p_input->psz_name );
721
722     if( p_item->i_children > -1 )
723     {
724         return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
725     }
726     var_SetInteger( p_playlist, "item-deleted", i_id );
727
728     /* Remove the item from the bank */
729     i_bottom = 0; i_top = p_playlist->i_all_size - 1;
730     i = i_top / 2;
731     while( p_playlist->pp_all_items[i]->i_id != i_id &&
732            i_top > i_bottom )
733     {
734         if( p_playlist->pp_all_items[i]->i_id < i_id )
735         {
736             i_bottom = i + 1;
737         }
738         else
739         {
740             i_top = i - 1;
741         }
742         i = i_bottom + ( i_top - i_bottom ) / 2;
743     }
744     if( p_playlist->pp_all_items[i]->i_id == i_id )
745     {
746         REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
747     }
748
749     /* Check if it is the current item */
750     if( p_playlist->status.p_item == p_item )
751     {
752         /* Hack we don't call playlist_Control for lock reasons */
753         if( b_stop )
754         {
755             p_playlist->status.i_status = PLAYLIST_STOPPED;
756             p_playlist->request.b_request = VLC_TRUE;
757             p_playlist->request.p_item = NULL;
758             msg_Info( p_playlist, "stopping playback" );
759         }
760         b_flag = VLC_TRUE;
761     }
762
763     msg_Dbg( p_playlist, "deleting playlist item `%s'",
764                           p_item->p_input->psz_name );
765
766     /* Remove the item from its parent */
767     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
768
769     if( b_flag == VLC_FALSE )
770         playlist_ItemDelete( p_item );
771     else
772         p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
773
774     return VLC_SUCCESS;
775 }