1 /*****************************************************************************
2 * item.c: input_item management
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
31 #include "vlc_playlist.h"
32 #include "vlc_interface.h"
37 static int GuessType( const input_item_t *p_item );
39 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
41 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
43 memset( p_i, 0, sizeof(input_item_t) );
47 TAB_INIT( p_i->i_es, p_i->es );
48 TAB_INIT( p_i->i_options, p_i->ppsz_options );
49 p_i->optflagv = NULL, p_i->optflagc = 0;
50 TAB_INIT( p_i->i_categories, p_i->pp_categories );
51 TAB_INIT( p_i->i_epg, p_i->pp_epg );
53 p_i->i_type = ITEM_TYPE_UNKNOWN;
54 p_i->b_fixed_name = true;
59 vlc_mutex_init( &p_i->lock );
60 vlc_event_manager_t * p_em = &p_i->event_manager;
61 vlc_event_manager_init( p_em, p_i, p_o );
62 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
63 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
64 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
65 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
66 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
67 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
68 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
69 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
72 static inline void input_item_Clean( input_item_t *p_i )
76 vlc_event_manager_fini( &p_i->event_manager );
78 free( p_i->psz_name );
82 vlc_mutex_destroy( &p_i->p_stats->lock );
87 vlc_meta_Delete( p_i->p_meta );
89 for( i = 0; i < p_i->i_options; i++ )
90 free( p_i->ppsz_options[i] );
91 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
94 for( i = 0; i < p_i->i_es; i++ )
96 es_format_Clean( p_i->es[i] );
99 TAB_CLEAN( p_i->i_es, p_i->es );
101 for( i = 0; i < p_i->i_epg; i++ )
102 vlc_epg_Delete( p_i->pp_epg[i] );
103 TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
105 for( i = 0; i < p_i->i_categories; i++ )
106 info_category_Delete( p_i->pp_categories[i] );
107 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
109 vlc_mutex_destroy( &p_i->lock );
111 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
115 vlc_mutex_lock( &p_i->lock );
117 b_changed = p_i->b_error_when_reading != b_error;
118 p_i->b_error_when_reading = b_error;
120 vlc_mutex_unlock( &p_i->lock );
126 event.type = vlc_InputItemErrorWhenReadingChanged;
127 event.u.input_item_error_when_reading_changed.new_value = b_error;
128 vlc_event_send( &p_i->event_manager, &event );
131 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
133 bool b_send_event = false;
135 vlc_mutex_lock( &p_i->lock );
138 p_i->p_meta = vlc_meta_New();
140 int status = vlc_meta_GetStatus(p_i->p_meta);
143 new_status = status | ITEM_PREPARSED;
145 new_status = status & ~ITEM_PREPARSED;
146 if( status != new_status )
148 vlc_meta_SetStatus(p_i->p_meta, new_status);
152 vlc_mutex_unlock( &p_i->lock );
157 event.type = vlc_InputItemPreparsedChanged;
158 event.u.input_item_preparsed_changed.new_status = new_status;
159 vlc_event_send( &p_i->event_manager, &event );
163 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
165 vlc_mutex_lock( &p_i->lock );
168 p_i->p_meta = vlc_meta_New();
170 int status = vlc_meta_GetStatus(p_i->p_meta);
173 status |= ITEM_ART_NOTFOUND;
175 status &= ~ITEM_ART_NOTFOUND;
177 vlc_meta_SetStatus(p_i->p_meta, status);
179 vlc_mutex_unlock( &p_i->lock );
182 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
184 vlc_mutex_lock( &p_i->lock );
187 p_i->p_meta = vlc_meta_New();
189 int status = vlc_meta_GetStatus(p_i->p_meta);
192 status |= ITEM_ART_FETCHED;
194 status &= ~ITEM_ART_FETCHED;
196 vlc_meta_SetStatus(p_i->p_meta, status);
198 vlc_mutex_unlock( &p_i->lock );
201 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
205 vlc_mutex_lock( &p_i->lock );
207 p_i->p_meta = vlc_meta_New();
208 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
209 vlc_mutex_unlock( &p_i->lock );
211 /* Notify interested third parties */
212 event.type = vlc_InputItemMetaChanged;
213 event.u.input_item_meta_changed.meta_type = meta_type;
214 vlc_event_send( &p_i->event_manager, &event );
217 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
218 * consistant with (nearly?) all or copy funcs */
219 void input_item_CopyOptions( input_item_t *p_parent,
220 input_item_t *p_child )
222 vlc_mutex_lock( &p_parent->lock );
224 for( int i = 0 ; i< p_parent->i_options; i++ )
226 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
229 input_item_AddOption( p_child,
230 p_parent->ppsz_options[i],
231 p_parent->optflagv[i] );
234 vlc_mutex_unlock( &p_parent->lock );
237 static void notify_subitem_added(input_item_t *p_parent, input_item_t *p_child)
239 /* Notify interested third parties */
241 event.type = vlc_InputItemSubItemAdded;
242 event.u.input_item_subitem_added.p_new_child = p_child;
243 vlc_event_send( &p_parent->event_manager, &event );
246 /* This won't hold the item, but can tell to interested third parties
247 * Like the playlist, that there is a new sub item. With this design
248 * It is not the input item's responsability to keep all the ref of
249 * the input item children. */
250 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
252 vlc_mutex_lock( &p_parent->lock );
253 p_parent->i_type = ITEM_TYPE_PLAYLIST;
254 vlc_mutex_unlock( &p_parent->lock );
256 input_item_node_t *p_node = input_item_node_Create( p_parent );
257 input_item_node_AppendItem( p_node, p_child );
258 input_item_node_PostAndDelete( p_node );
261 bool input_item_HasErrorWhenReading( input_item_t *p_item )
263 vlc_mutex_lock( &p_item->lock );
265 bool b_error = p_item->b_error_when_reading;
267 vlc_mutex_unlock( &p_item->lock );
272 bool input_item_MetaMatch( input_item_t *p_i,
273 vlc_meta_type_t meta_type, const char *psz )
275 vlc_mutex_lock( &p_i->lock );
279 vlc_mutex_unlock( &p_i->lock );
282 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
283 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
285 vlc_mutex_unlock( &p_i->lock );
290 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
292 vlc_mutex_lock( &p_i->lock );
296 vlc_mutex_unlock( &p_i->lock );
301 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
302 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
304 vlc_mutex_unlock( &p_i->lock );
308 /* Get the title of a given item or fallback to the name if the title is empty */
309 char *input_item_GetTitleFbName( input_item_t *p_item )
312 vlc_mutex_lock( &p_item->lock );
314 if( !p_item->p_meta )
316 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
317 vlc_mutex_unlock( &p_item->lock );
321 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
322 if( !EMPTY_STR( psz_title ) )
323 psz_ret = strdup( psz_title );
325 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
327 vlc_mutex_unlock( &p_item->lock );
331 char *input_item_GetName( input_item_t *p_item )
333 vlc_mutex_lock( &p_item->lock );
335 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
337 vlc_mutex_unlock( &p_item->lock );
340 void input_item_SetName( input_item_t *p_item, const char *psz_name )
342 vlc_mutex_lock( &p_item->lock );
344 free( p_item->psz_name );
345 p_item->psz_name = strdup( psz_name );
347 vlc_mutex_unlock( &p_item->lock );
350 char *input_item_GetURI( input_item_t *p_i )
352 vlc_mutex_lock( &p_i->lock );
354 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
356 vlc_mutex_unlock( &p_i->lock );
359 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
361 vlc_mutex_lock( &p_i->lock );
363 if( !strstr( psz_uri, "://" ) || strstr( psz_uri, " " ) || strstr( psz_uri, "\"" ) )
364 fprintf( stderr, "input_item_SetURI() was likely called with a path. FIXME\n" );
367 free( p_i->psz_uri );
368 p_i->psz_uri = strdup( psz_uri );
370 p_i->i_type = GuessType( p_i );
375 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
377 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
379 if( psz_filename && *psz_filename == '/' )
381 if( psz_filename && *psz_filename )
382 p_i->psz_name = strdup( psz_filename );
384 /* Make the name more readable */
386 decode_URI( p_i->psz_name );
389 { /* Strip login and password from title */
393 vlc_UrlParse( &url, psz_uri, 0 );
394 if( url.psz_protocol )
397 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
398 url.psz_host, url.i_port,
399 url.psz_path ? url.psz_path : "" );
401 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
402 url.psz_host ? url.psz_host : "",
403 url.psz_path ? url.psz_path : "" );
408 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
409 url.psz_path ? url.psz_path : "" );
411 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
412 url.psz_path ? url.psz_path : "" );
414 vlc_UrlClean( &url );
416 p_i->psz_name=NULL; /* recover from undefined value */
419 vlc_mutex_unlock( &p_i->lock );
422 mtime_t input_item_GetDuration( input_item_t *p_i )
424 vlc_mutex_lock( &p_i->lock );
426 mtime_t i_duration = p_i->i_duration;
428 vlc_mutex_unlock( &p_i->lock );
432 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
434 bool b_send_event = false;
436 vlc_mutex_lock( &p_i->lock );
437 if( p_i->i_duration != i_duration )
439 p_i->i_duration = i_duration;
442 vlc_mutex_unlock( &p_i->lock );
448 event.type = vlc_InputItemDurationChanged;
449 event.u.input_item_duration_changed.new_duration = i_duration;
450 vlc_event_send( &p_i->event_manager, &event );
455 bool input_item_IsPreparsed( input_item_t *p_item )
457 vlc_mutex_lock( &p_item->lock );
458 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
459 vlc_mutex_unlock( &p_item->lock );
464 bool input_item_IsArtFetched( input_item_t *p_item )
466 vlc_mutex_lock( &p_item->lock );
467 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
468 vlc_mutex_unlock( &p_item->lock );
473 static void input_item_Destroy ( gc_object_t *p_gc )
475 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
477 input_item_Clean( p_item );
481 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
484 int err = VLC_SUCCESS;
486 if( psz_option == NULL )
489 vlc_mutex_lock( &p_input->lock );
490 if (flags & VLC_INPUT_OPTION_UNIQUE)
492 for (int i = 0 ; i < p_input->i_options; i++)
493 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
497 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
503 p_input->optflagv = flagv;
504 flagv[p_input->optflagc++] = flags;
506 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
507 p_input->i_options, strdup( psz_option ) );
509 vlc_mutex_unlock( &p_input->lock );
513 static info_category_t *InputItemFindCat( input_item_t *p_item,
514 int *pi_index, const char *psz_cat )
516 vlc_assert_locked( &p_item->lock );
517 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
519 info_category_t *p_cat = p_item->pp_categories[i];
521 if( !strcmp( p_cat->psz_name, psz_cat ) )
532 * Get a info item from a given category in a given input item.
534 * \param p_i The input item to get info from
535 * \param psz_cat String representing the category for the info
536 * \param psz_name String representing the name of the desired info
537 * \return A pointer to the string with the given info if found, or an
538 * empty string otherwise. The caller should free the returned
541 char *input_item_GetInfo( input_item_t *p_i,
543 const char *psz_name )
545 vlc_mutex_lock( &p_i->lock );
547 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
550 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
551 if( p_info && p_info->psz_value )
553 char *psz_ret = strdup( p_info->psz_value );
554 vlc_mutex_unlock( &p_i->lock );
558 vlc_mutex_unlock( &p_i->lock );
562 static int InputItemVaAddInfo( input_item_t *p_i,
564 const char *psz_name,
565 const char *psz_format, va_list args )
567 vlc_assert_locked( &p_i->lock );
569 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
572 p_cat = info_category_New( psz_cat );
575 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
578 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
579 if( !p_info || !p_info->psz_value )
584 static int InputItemAddInfo( input_item_t *p_i,
586 const char *psz_name,
587 const char *psz_format, ... )
591 va_start( args, psz_format );
592 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
598 int input_item_AddInfo( input_item_t *p_i,
600 const char *psz_name,
601 const char *psz_format, ... )
605 vlc_mutex_lock( &p_i->lock );
607 va_start( args, psz_format );
608 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
611 vlc_mutex_unlock( &p_i->lock );
618 event.type = vlc_InputItemInfoChanged;
619 vlc_event_send( &p_i->event_manager, &event );
624 int input_item_DelInfo( input_item_t *p_i,
626 const char *psz_name )
628 vlc_mutex_lock( &p_i->lock );
630 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
633 vlc_mutex_unlock( &p_i->lock );
639 /* Remove a specific info */
640 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
643 vlc_mutex_unlock( &p_i->lock );
649 /* Remove the complete categorie */
650 info_category_Delete( p_cat );
651 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
653 vlc_mutex_unlock( &p_i->lock );
657 event.type = vlc_InputItemInfoChanged;
658 vlc_event_send( &p_i->event_manager, &event );
662 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
664 vlc_mutex_lock( &p_item->lock );
666 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
669 info_category_Delete( p_old );
670 p_item->pp_categories[i_cat] = p_cat;
674 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
677 vlc_mutex_unlock( &p_item->lock );
681 event.type = vlc_InputItemInfoChanged;
682 vlc_event_send( &p_item->event_manager, &event );
684 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
686 vlc_mutex_lock( &p_item->lock );
687 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
690 for( int i = 0; i < p_cat->i_infos; i++ )
691 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
692 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
693 info_category_Delete( p_cat );
697 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
700 vlc_mutex_unlock( &p_item->lock );
704 event.type = vlc_InputItemInfoChanged;
705 vlc_event_send( &p_item->event_manager, &event );
709 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
711 vlc_mutex_lock( &p_item->lock );
714 vlc_epg_t *p_epg = NULL;
715 for( int i = 0; i < p_item->i_epg; i++ )
717 vlc_epg_t *p_tmp = p_item->pp_epg[i];
719 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
721 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
731 p_epg = vlc_epg_New( p_update->psz_name );
733 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
736 vlc_epg_Merge( p_epg, p_update );
738 vlc_mutex_unlock( &p_item->lock );
742 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
745 input_item_DelInfo( p_item, psz_epg, NULL );
747 vlc_mutex_lock( &p_item->lock );
748 for( int i = 0; i < p_epg->i_event; i++ )
750 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
751 time_t t_start = (time_t)p_evt->i_start;
755 localtime_r( &t_start, &tm_start );
757 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
758 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
759 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
760 if( p_evt->psz_short_description || p_evt->psz_description )
761 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
763 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
764 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
765 p_evt->psz_description ? p_evt->psz_description : "" );
767 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
769 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
771 vlc_mutex_unlock( &p_item->lock );
776 if( p_epg->i_event > 0 )
778 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
779 vlc_event_send( &p_item->event_manager, &event );
783 void input_item_SetEpgOffline( input_item_t *p_item )
785 vlc_mutex_lock( &p_item->lock );
786 for( int i = 0; i < p_item->i_epg; i++ )
787 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
788 vlc_mutex_unlock( &p_item->lock );
791 vlc_mutex_lock( &p_item->lock );
792 const int i_epg_info = p_item->i_epg;
793 char *ppsz_epg_info[i_epg_info];
794 for( int i = 0; i < p_item->i_epg; i++ )
796 const vlc_epg_t *p_epg = p_item->pp_epg[i];
797 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
798 ppsz_epg_info[i] = NULL;
800 vlc_mutex_unlock( &p_item->lock );
802 for( int i = 0; i < i_epg_info; i++ )
804 if( !ppsz_epg_info[i] )
806 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
807 free( ppsz_epg_info[i] );
811 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
812 vlc_event_send( &p_item->event_manager, &event );
815 #undef input_item_NewExt
816 input_item_t *input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
817 const char *psz_name,
819 const char *const *ppsz_options,
820 unsigned i_option_flags,
823 return input_item_NewWithType( p_obj, psz_uri, psz_name,
824 i_options, ppsz_options, i_option_flags,
825 i_duration, ITEM_TYPE_UNKNOWN );
829 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
830 const char *psz_name,
832 const char *const *ppsz_options,
833 unsigned i_option_flags,
837 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
838 static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
840 input_item_t* p_input = malloc( sizeof(input_item_t ) );
844 input_item_Init( p_obj, p_input );
845 vlc_gc_init( p_input, input_item_Destroy );
847 vlc_mutex_lock( &input_id_lock );
848 p_input->i_id = ++priv->i_last_input_id;
849 vlc_mutex_unlock( &input_id_lock );
851 p_input->b_fixed_name = false;
853 p_input->i_type = i_type;
854 p_input->b_prefers_tree = false;
857 input_item_SetURI( p_input, psz_uri );
859 if( i_type != ITEM_TYPE_UNKNOWN )
860 p_input->i_type = i_type;
863 input_item_SetName( p_input, psz_name );
865 p_input->i_duration = i_duration;
867 for( int i = 0; i < i_options; i++ )
868 input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
872 struct item_type_entry
874 const char psz_scheme[7];
878 static int typecmp( const void *key, const void *entry )
880 const struct item_type_entry *type = entry;
881 const char *uri = key, *scheme = type->psz_scheme;
883 return strncmp( uri, scheme, strlen( scheme ) );
886 /* Guess the type of the item using the beginning of the mrl */
887 static int GuessType( const input_item_t *p_item )
889 static const struct item_type_entry tab[] =
890 { /* /!\ Alphabetical order /!\ */
891 /* Short match work, not just exact match */
892 { "alsa", ITEM_TYPE_CARD },
893 { "atsc", ITEM_TYPE_CARD },
894 { "bd", ITEM_TYPE_DISC },
895 { "cable", ITEM_TYPE_CARD },
896 { "cdda", ITEM_TYPE_CDDA },
897 { "dc1394", ITEM_TYPE_CARD },
898 { "dccp", ITEM_TYPE_NET },
899 { "dir", ITEM_TYPE_DIRECTORY },
900 { "dshow", ITEM_TYPE_CARD },
901 { "dv", ITEM_TYPE_CARD },
902 { "dvb", ITEM_TYPE_CARD },
903 { "dvd", ITEM_TYPE_DISC },
904 { "ftp", ITEM_TYPE_NET },
905 { "http", ITEM_TYPE_NET },
906 { "icyx", ITEM_TYPE_NET },
907 { "itpc", ITEM_TYPE_NET },
908 { "jack", ITEM_TYPE_CARD },
909 { "live", ITEM_TYPE_NET }, /* livedotcom */
910 { "mms", ITEM_TYPE_NET },
911 { "mtp", ITEM_TYPE_DISC },
912 { "ofdm", ITEM_TYPE_CARD },
913 { "oss", ITEM_TYPE_CARD },
914 { "pnm", ITEM_TYPE_NET },
915 { "pvr", ITEM_TYPE_CARD },
916 { "qam", ITEM_TYPE_CARD },
917 { "qpsk", ITEM_TYPE_CARD },
918 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
919 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
920 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
921 { "satell", ITEM_TYPE_CARD }, /* sattelite */
922 { "screen", ITEM_TYPE_CARD },
923 { "sdp", ITEM_TYPE_NET },
924 { "smb", ITEM_TYPE_NET },
925 { "svcd", ITEM_TYPE_DISC },
926 { "tcp", ITEM_TYPE_NET },
927 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
928 { "udp", ITEM_TYPE_NET }, /* udplite too */
929 { "unsv", ITEM_TYPE_NET },
930 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
931 { "v4l", ITEM_TYPE_CARD },
932 { "vcd", ITEM_TYPE_DISC },
933 { "window", ITEM_TYPE_CARD },
935 const struct item_type_entry *e;
937 if( !strstr( p_item->psz_uri, "://" ) )
938 return ITEM_TYPE_FILE;
940 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
941 sizeof( tab[0] ), typecmp );
942 return e ? e->i_type : ITEM_TYPE_FILE;
945 input_item_node_t *input_item_node_Create( input_item_t *p_input )
947 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
953 p_node->p_item = p_input;
954 vlc_gc_incref( p_input );
956 p_node->p_parent = NULL;
957 p_node->i_children = 0;
958 p_node->pp_children = NULL;
963 static void RecursiveNodeDelete( input_item_node_t *p_node )
965 for( int i = 0; i < p_node->i_children; i++ )
966 RecursiveNodeDelete( p_node->pp_children[i] );
968 vlc_gc_decref( p_node->p_item );
969 free( p_node->pp_children );
973 void input_item_node_Delete( input_item_node_t *p_node )
975 if( p_node->p_parent )
977 for( int i = 0; i < p_node->p_parent->i_children; i++ )
978 if( p_node->p_parent->pp_children[i] == p_node )
980 REMOVE_ELEM( p_node->p_parent->pp_children,
981 p_node->p_parent->i_children,
987 RecursiveNodeDelete( p_node );
990 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
992 input_item_node_t *p_new_child = input_item_node_Create( p_item );
993 if( !p_new_child ) return NULL;
994 input_item_node_AppendNode( p_node, p_new_child );
998 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1000 notify_subitem_added(p_parent->p_item, p_child->p_item);
1002 assert( p_parent && p_child && p_child->p_parent == NULL );
1003 INSERT_ELEM( p_parent->pp_children,
1004 p_parent->i_children,
1005 p_parent->i_children,
1007 p_child->p_parent = p_parent;
1010 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1013 event.type = vlc_InputItemSubItemTreeAdded;
1014 event.u.input_item_subitem_tree_added.p_root = p_root;
1015 vlc_event_send( &p_root->p_item->event_manager, &event );
1017 input_item_node_Delete( p_root );