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"
33 #include <vlc_charset.h>
38 static int GuessType( const input_item_t *p_item );
40 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
42 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
44 memset( p_i, 0, sizeof(input_item_t) );
48 TAB_INIT( p_i->i_es, p_i->es );
49 TAB_INIT( p_i->i_options, p_i->ppsz_options );
50 p_i->optflagv = NULL, p_i->optflagc = 0;
51 TAB_INIT( p_i->i_categories, p_i->pp_categories );
52 TAB_INIT( p_i->i_epg, p_i->pp_epg );
54 p_i->i_type = ITEM_TYPE_UNKNOWN;
55 p_i->b_fixed_name = true;
60 vlc_mutex_init( &p_i->lock );
61 vlc_event_manager_t * p_em = &p_i->event_manager;
62 vlc_event_manager_init( p_em, p_i, p_o );
63 vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
64 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
65 vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
66 vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
67 vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
68 vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
69 vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
70 vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
73 static inline void input_item_Clean( input_item_t *p_i )
77 vlc_event_manager_fini( &p_i->event_manager );
79 free( p_i->psz_name );
83 vlc_mutex_destroy( &p_i->p_stats->lock );
88 vlc_meta_Delete( p_i->p_meta );
90 for( i = 0; i < p_i->i_options; i++ )
91 free( p_i->ppsz_options[i] );
92 TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
95 for( i = 0; i < p_i->i_es; i++ )
97 es_format_Clean( p_i->es[i] );
100 TAB_CLEAN( p_i->i_es, p_i->es );
102 for( i = 0; i < p_i->i_epg; i++ )
103 vlc_epg_Delete( p_i->pp_epg[i] );
104 TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
106 for( i = 0; i < p_i->i_categories; i++ )
107 info_category_Delete( p_i->pp_categories[i] );
108 TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
110 vlc_mutex_destroy( &p_i->lock );
112 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
116 vlc_mutex_lock( &p_i->lock );
118 b_changed = p_i->b_error_when_reading != b_error;
119 p_i->b_error_when_reading = b_error;
121 vlc_mutex_unlock( &p_i->lock );
127 event.type = vlc_InputItemErrorWhenReadingChanged;
128 event.u.input_item_error_when_reading_changed.new_value = b_error;
129 vlc_event_send( &p_i->event_manager, &event );
132 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
134 bool b_send_event = false;
136 vlc_mutex_lock( &p_i->lock );
139 p_i->p_meta = vlc_meta_New();
141 int status = vlc_meta_GetStatus(p_i->p_meta);
144 new_status = status | ITEM_PREPARSED;
146 new_status = status & ~ITEM_PREPARSED;
147 if( status != new_status )
149 vlc_meta_SetStatus(p_i->p_meta, new_status);
153 vlc_mutex_unlock( &p_i->lock );
158 event.type = vlc_InputItemPreparsedChanged;
159 event.u.input_item_preparsed_changed.new_status = new_status;
160 vlc_event_send( &p_i->event_manager, &event );
164 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
166 vlc_mutex_lock( &p_i->lock );
169 p_i->p_meta = vlc_meta_New();
171 int status = vlc_meta_GetStatus(p_i->p_meta);
174 status |= ITEM_ART_NOTFOUND;
176 status &= ~ITEM_ART_NOTFOUND;
178 vlc_meta_SetStatus(p_i->p_meta, status);
180 vlc_mutex_unlock( &p_i->lock );
183 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
185 vlc_mutex_lock( &p_i->lock );
188 p_i->p_meta = vlc_meta_New();
190 int status = vlc_meta_GetStatus(p_i->p_meta);
193 status |= ITEM_ART_FETCHED;
195 status &= ~ITEM_ART_FETCHED;
197 vlc_meta_SetStatus(p_i->p_meta, status);
199 vlc_mutex_unlock( &p_i->lock );
202 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
206 vlc_mutex_lock( &p_i->lock );
208 p_i->p_meta = vlc_meta_New();
209 vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
210 vlc_mutex_unlock( &p_i->lock );
212 /* Notify interested third parties */
213 event.type = vlc_InputItemMetaChanged;
214 event.u.input_item_meta_changed.meta_type = meta_type;
215 vlc_event_send( &p_i->event_manager, &event );
218 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
219 * consistant with (nearly?) all or copy funcs */
220 void input_item_CopyOptions( input_item_t *p_parent,
221 input_item_t *p_child )
223 vlc_mutex_lock( &p_parent->lock );
225 for( int i = 0 ; i< p_parent->i_options; i++ )
227 if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
230 input_item_AddOption( p_child,
231 p_parent->ppsz_options[i],
232 p_parent->optflagv[i] );
235 vlc_mutex_unlock( &p_parent->lock );
238 static void post_subitems( input_item_node_t *p_node )
240 for( int i = 0; i < p_node->i_children; i++ )
243 event.type = vlc_InputItemSubItemAdded;
244 event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
245 vlc_event_send( &p_node->p_item->event_manager, &event );
247 post_subitems( p_node->pp_children[i] );
251 /* This won't hold the item, but can tell to interested third parties
252 * Like the playlist, that there is a new sub item. With this design
253 * It is not the input item's responsability to keep all the ref of
254 * the input item children. */
255 void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
257 input_item_node_t *p_node = input_item_node_Create( p_parent );
258 input_item_node_AppendItem( p_node, p_child );
259 input_item_node_PostAndDelete( p_node );
262 bool input_item_HasErrorWhenReading( input_item_t *p_item )
264 vlc_mutex_lock( &p_item->lock );
266 bool b_error = p_item->b_error_when_reading;
268 vlc_mutex_unlock( &p_item->lock );
273 bool input_item_MetaMatch( input_item_t *p_i,
274 vlc_meta_type_t meta_type, const char *psz )
276 vlc_mutex_lock( &p_i->lock );
280 vlc_mutex_unlock( &p_i->lock );
283 const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
284 bool b_ret = psz_meta && strcasestr( psz_meta, psz );
286 vlc_mutex_unlock( &p_i->lock );
291 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
293 vlc_mutex_lock( &p_i->lock );
297 vlc_mutex_unlock( &p_i->lock );
302 if( vlc_meta_Get( p_i->p_meta, meta_type ) )
303 psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
305 vlc_mutex_unlock( &p_i->lock );
309 /* Get the title of a given item or fallback to the name if the title is empty */
310 char *input_item_GetTitleFbName( input_item_t *p_item )
313 vlc_mutex_lock( &p_item->lock );
315 if( !p_item->p_meta )
317 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
318 vlc_mutex_unlock( &p_item->lock );
322 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
323 if( !EMPTY_STR( psz_title ) )
324 psz_ret = strdup( psz_title );
326 psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
328 vlc_mutex_unlock( &p_item->lock );
332 char *input_item_GetName( input_item_t *p_item )
334 vlc_mutex_lock( &p_item->lock );
336 char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
338 vlc_mutex_unlock( &p_item->lock );
341 void input_item_SetName( input_item_t *p_item, const char *psz_name )
343 vlc_mutex_lock( &p_item->lock );
345 free( p_item->psz_name );
346 p_item->psz_name = strdup( psz_name );
348 vlc_mutex_unlock( &p_item->lock );
351 char *input_item_GetURI( input_item_t *p_i )
353 vlc_mutex_lock( &p_i->lock );
355 char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
357 vlc_mutex_unlock( &p_i->lock );
360 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
363 if( !strstr( psz_uri, "://" )
364 || strchr( psz_uri, ' ' ) || strchr( psz_uri, '"' ) )
365 fprintf( stderr, "Warning: %s(\"%s\"): file path instead of URL.\n",
368 vlc_mutex_lock( &p_i->lock );
369 free( p_i->psz_uri );
370 p_i->psz_uri = strdup( psz_uri );
372 p_i->i_type = GuessType( p_i );
377 if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
379 const char *psz_filename = strrchr( p_i->psz_uri, '/' );
381 if( psz_filename && *psz_filename == '/' )
383 if( psz_filename && *psz_filename )
384 p_i->psz_name = strdup( psz_filename );
386 /* Make the name more readable */
389 decode_URI( p_i->psz_name );
390 EnsureUTF8( p_i->psz_name );
394 { /* Strip login and password from title */
398 vlc_UrlParse( &url, psz_uri, 0 );
399 if( url.psz_protocol )
402 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
403 url.psz_host, url.i_port,
404 url.psz_path ? url.psz_path : "" );
406 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
407 url.psz_host ? url.psz_host : "",
408 url.psz_path ? url.psz_path : "" );
413 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
414 url.psz_path ? url.psz_path : "" );
416 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
417 url.psz_path ? url.psz_path : "" );
419 vlc_UrlClean( &url );
421 p_i->psz_name=NULL; /* recover from undefined value */
424 vlc_mutex_unlock( &p_i->lock );
427 mtime_t input_item_GetDuration( input_item_t *p_i )
429 vlc_mutex_lock( &p_i->lock );
431 mtime_t i_duration = p_i->i_duration;
433 vlc_mutex_unlock( &p_i->lock );
437 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
439 bool b_send_event = false;
441 vlc_mutex_lock( &p_i->lock );
442 if( p_i->i_duration != i_duration )
444 p_i->i_duration = i_duration;
447 vlc_mutex_unlock( &p_i->lock );
453 event.type = vlc_InputItemDurationChanged;
454 event.u.input_item_duration_changed.new_duration = i_duration;
455 vlc_event_send( &p_i->event_manager, &event );
460 bool input_item_IsPreparsed( input_item_t *p_item )
462 vlc_mutex_lock( &p_item->lock );
463 bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
464 vlc_mutex_unlock( &p_item->lock );
469 bool input_item_IsArtFetched( input_item_t *p_item )
471 vlc_mutex_lock( &p_item->lock );
472 bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
473 vlc_mutex_unlock( &p_item->lock );
478 static void input_item_Destroy ( gc_object_t *p_gc )
480 input_item_t *p_item = vlc_priv( p_gc, input_item_t );
482 input_item_Clean( p_item );
486 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
489 int err = VLC_SUCCESS;
491 if( psz_option == NULL )
494 vlc_mutex_lock( &p_input->lock );
495 if (flags & VLC_INPUT_OPTION_UNIQUE)
497 for (int i = 0 ; i < p_input->i_options; i++)
498 if( !strcmp( p_input->ppsz_options[i], psz_option ) )
502 uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
508 p_input->optflagv = flagv;
509 flagv[p_input->optflagc++] = flags;
511 INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
512 p_input->i_options, strdup( psz_option ) );
514 vlc_mutex_unlock( &p_input->lock );
518 static info_category_t *InputItemFindCat( input_item_t *p_item,
519 int *pi_index, const char *psz_cat )
521 vlc_assert_locked( &p_item->lock );
522 for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
524 info_category_t *p_cat = p_item->pp_categories[i];
526 if( !strcmp( p_cat->psz_name, psz_cat ) )
537 * Get a info item from a given category in a given input item.
539 * \param p_i The input item to get info from
540 * \param psz_cat String representing the category for the info
541 * \param psz_name String representing the name of the desired info
542 * \return A pointer to the string with the given info if found, or an
543 * empty string otherwise. The caller should free the returned
546 char *input_item_GetInfo( input_item_t *p_i,
548 const char *psz_name )
550 vlc_mutex_lock( &p_i->lock );
552 const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
555 info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
556 if( p_info && p_info->psz_value )
558 char *psz_ret = strdup( p_info->psz_value );
559 vlc_mutex_unlock( &p_i->lock );
563 vlc_mutex_unlock( &p_i->lock );
567 static int InputItemVaAddInfo( input_item_t *p_i,
569 const char *psz_name,
570 const char *psz_format, va_list args )
572 vlc_assert_locked( &p_i->lock );
574 info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
577 p_cat = info_category_New( psz_cat );
580 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
583 info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
584 if( !p_info || !p_info->psz_value )
589 static int InputItemAddInfo( input_item_t *p_i,
591 const char *psz_name,
592 const char *psz_format, ... )
596 va_start( args, psz_format );
597 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
603 int input_item_AddInfo( input_item_t *p_i,
605 const char *psz_name,
606 const char *psz_format, ... )
610 vlc_mutex_lock( &p_i->lock );
612 va_start( args, psz_format );
613 const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
616 vlc_mutex_unlock( &p_i->lock );
623 event.type = vlc_InputItemInfoChanged;
624 vlc_event_send( &p_i->event_manager, &event );
629 int input_item_DelInfo( input_item_t *p_i,
631 const char *psz_name )
633 vlc_mutex_lock( &p_i->lock );
635 info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
638 vlc_mutex_unlock( &p_i->lock );
644 /* Remove a specific info */
645 int i_ret = info_category_DeleteInfo( p_cat, psz_name );
648 vlc_mutex_unlock( &p_i->lock );
654 /* Remove the complete categorie */
655 info_category_Delete( p_cat );
656 REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
658 vlc_mutex_unlock( &p_i->lock );
662 event.type = vlc_InputItemInfoChanged;
663 vlc_event_send( &p_i->event_manager, &event );
667 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
669 vlc_mutex_lock( &p_item->lock );
671 info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
674 info_category_Delete( p_old );
675 p_item->pp_categories[i_cat] = p_cat;
679 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
682 vlc_mutex_unlock( &p_item->lock );
686 event.type = vlc_InputItemInfoChanged;
687 vlc_event_send( &p_item->event_manager, &event );
689 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
691 vlc_mutex_lock( &p_item->lock );
692 info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
695 for( int i = 0; i < p_cat->i_infos; i++ )
696 info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
697 TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
698 info_category_Delete( p_cat );
702 INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
705 vlc_mutex_unlock( &p_item->lock );
709 event.type = vlc_InputItemInfoChanged;
710 vlc_event_send( &p_item->event_manager, &event );
714 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
716 vlc_mutex_lock( &p_item->lock );
719 vlc_epg_t *p_epg = NULL;
720 for( int i = 0; i < p_item->i_epg; i++ )
722 vlc_epg_t *p_tmp = p_item->pp_epg[i];
724 if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
726 if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
736 p_epg = vlc_epg_New( p_update->psz_name );
738 TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
741 vlc_epg_Merge( p_epg, p_update );
743 vlc_mutex_unlock( &p_item->lock );
747 if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
750 input_item_DelInfo( p_item, psz_epg, NULL );
752 vlc_mutex_lock( &p_item->lock );
753 for( int i = 0; i < p_epg->i_event; i++ )
755 const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
756 time_t t_start = (time_t)p_evt->i_start;
760 localtime_r( &t_start, &tm_start );
762 snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
763 1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
764 tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
765 if( p_evt->psz_short_description || p_evt->psz_description )
766 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
768 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
769 p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
770 p_evt->psz_description ? p_evt->psz_description : "" );
772 InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
774 p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
776 vlc_mutex_unlock( &p_item->lock );
781 if( p_epg->i_event > 0 )
783 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
784 vlc_event_send( &p_item->event_manager, &event );
788 void input_item_SetEpgOffline( input_item_t *p_item )
790 vlc_mutex_lock( &p_item->lock );
791 for( int i = 0; i < p_item->i_epg; i++ )
792 vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
793 vlc_mutex_unlock( &p_item->lock );
796 vlc_mutex_lock( &p_item->lock );
797 const int i_epg_info = p_item->i_epg;
798 char *ppsz_epg_info[i_epg_info];
799 for( int i = 0; i < p_item->i_epg; i++ )
801 const vlc_epg_t *p_epg = p_item->pp_epg[i];
802 if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
803 ppsz_epg_info[i] = NULL;
805 vlc_mutex_unlock( &p_item->lock );
807 for( int i = 0; i < i_epg_info; i++ )
809 if( !ppsz_epg_info[i] )
811 input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
812 free( ppsz_epg_info[i] );
816 vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
817 vlc_event_send( &p_item->event_manager, &event );
820 #undef input_item_NewExt
821 input_item_t *input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
822 const char *psz_name,
824 const char *const *ppsz_options,
825 unsigned i_option_flags,
828 return input_item_NewWithType( p_obj, psz_uri, psz_name,
829 i_options, ppsz_options, i_option_flags,
830 i_duration, ITEM_TYPE_UNKNOWN );
834 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
835 const char *psz_name,
837 const char *const *ppsz_options,
838 unsigned i_option_flags,
842 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
843 static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
845 input_item_t* p_input = malloc( sizeof(input_item_t ) );
849 input_item_Init( p_obj, p_input );
850 vlc_gc_init( p_input, input_item_Destroy );
852 vlc_mutex_lock( &input_id_lock );
853 p_input->i_id = ++priv->i_last_input_id;
854 vlc_mutex_unlock( &input_id_lock );
856 p_input->b_fixed_name = false;
858 p_input->i_type = i_type;
859 p_input->b_prefers_tree = false;
862 input_item_SetURI( p_input, psz_uri );
864 if( i_type != ITEM_TYPE_UNKNOWN )
865 p_input->i_type = i_type;
868 input_item_SetName( p_input, psz_name );
870 p_input->i_duration = i_duration;
872 for( int i = 0; i < i_options; i++ )
873 input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
877 struct item_type_entry
879 const char psz_scheme[7];
883 static int typecmp( const void *key, const void *entry )
885 const struct item_type_entry *type = entry;
886 const char *uri = key, *scheme = type->psz_scheme;
888 return strncmp( uri, scheme, strlen( scheme ) );
891 /* Guess the type of the item using the beginning of the mrl */
892 static int GuessType( const input_item_t *p_item )
894 static const struct item_type_entry tab[] =
895 { /* /!\ Alphabetical order /!\ */
896 /* Short match work, not just exact match */
897 { "alsa", ITEM_TYPE_CARD },
898 { "atsc", ITEM_TYPE_CARD },
899 { "bd", ITEM_TYPE_DISC },
900 { "cable", ITEM_TYPE_CARD },
901 { "cdda", ITEM_TYPE_CDDA },
902 { "dc1394", ITEM_TYPE_CARD },
903 { "dccp", ITEM_TYPE_NET },
904 { "dir", ITEM_TYPE_DIRECTORY },
905 { "dshow", ITEM_TYPE_CARD },
906 { "dv", ITEM_TYPE_CARD },
907 { "dvb", ITEM_TYPE_CARD },
908 { "dvd", ITEM_TYPE_DISC },
909 { "ftp", ITEM_TYPE_NET },
910 { "http", ITEM_TYPE_NET },
911 { "icyx", ITEM_TYPE_NET },
912 { "itpc", ITEM_TYPE_NET },
913 { "jack", ITEM_TYPE_CARD },
914 { "live", ITEM_TYPE_NET }, /* livedotcom */
915 { "mms", ITEM_TYPE_NET },
916 { "mtp", ITEM_TYPE_DISC },
917 { "ofdm", ITEM_TYPE_CARD },
918 { "oss", ITEM_TYPE_CARD },
919 { "pnm", ITEM_TYPE_NET },
920 { "pvr", ITEM_TYPE_CARD },
921 { "qam", ITEM_TYPE_CARD },
922 { "qpsk", ITEM_TYPE_CARD },
923 { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
924 { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
925 { "rt", ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
926 { "satell", ITEM_TYPE_CARD }, /* sattelite */
927 { "screen", ITEM_TYPE_CARD },
928 { "sdp", ITEM_TYPE_NET },
929 { "smb", ITEM_TYPE_NET },
930 { "svcd", ITEM_TYPE_DISC },
931 { "tcp", ITEM_TYPE_NET },
932 { "terres", ITEM_TYPE_CARD }, /* terrestrial */
933 { "udp", ITEM_TYPE_NET }, /* udplite too */
934 { "unsv", ITEM_TYPE_NET },
935 { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
936 { "v4l", ITEM_TYPE_CARD },
937 { "vcd", ITEM_TYPE_DISC },
938 { "window", ITEM_TYPE_CARD },
940 const struct item_type_entry *e;
942 if( !strstr( p_item->psz_uri, "://" ) )
943 return ITEM_TYPE_FILE;
945 e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
946 sizeof( tab[0] ), typecmp );
947 return e ? e->i_type : ITEM_TYPE_FILE;
950 input_item_node_t *input_item_node_Create( input_item_t *p_input )
952 input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
958 p_node->p_item = p_input;
959 vlc_gc_incref( p_input );
961 p_node->p_parent = NULL;
962 p_node->i_children = 0;
963 p_node->pp_children = NULL;
968 static void RecursiveNodeDelete( input_item_node_t *p_node )
970 for( int i = 0; i < p_node->i_children; i++ )
971 RecursiveNodeDelete( p_node->pp_children[i] );
973 vlc_gc_decref( p_node->p_item );
974 free( p_node->pp_children );
978 void input_item_node_Delete( input_item_node_t *p_node )
980 if( p_node->p_parent )
982 for( int i = 0; i < p_node->p_parent->i_children; i++ )
983 if( p_node->p_parent->pp_children[i] == p_node )
985 REMOVE_ELEM( p_node->p_parent->pp_children,
986 p_node->p_parent->i_children,
992 RecursiveNodeDelete( p_node );
995 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
997 input_item_node_t *p_new_child = input_item_node_Create( p_item );
998 if( !p_new_child ) return NULL;
999 input_item_node_AppendNode( p_node, p_new_child );
1003 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1005 assert( p_parent && p_child && p_child->p_parent == NULL );
1006 INSERT_ELEM( p_parent->pp_children,
1007 p_parent->i_children,
1008 p_parent->i_children,
1010 p_child->p_parent = p_parent;
1013 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1015 post_subitems( p_root );
1018 event.type = vlc_InputItemSubItemTreeAdded;
1019 event.u.input_item_subitem_tree_added.p_root = p_root;
1020 vlc_event_send( &p_root->p_item->event_manager, &event );
1022 input_item_node_Delete( p_root );