]> git.sesse.net Git - vlc/blob - src/input/item.c
input: remove leading underscores
[vlc] / src / input / item.c
1 /*****************************************************************************
2  * item.c: input_item management
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_url.h>
31 #include "vlc_playlist.h"
32 #include "vlc_interface.h"
33
34 #include "item.h"
35 #include "info.h"
36
37 static int GuessType( const input_item_t *p_item );
38
39 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
40  * anyway. */
41 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
42 {
43     memset( p_i, 0, sizeof(input_item_t) );
44
45     p_i->psz_name = NULL;
46     p_i->psz_uri = NULL;
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 );
52
53     p_i->i_type = ITEM_TYPE_UNKNOWN;
54     p_i->b_fixed_name = true;
55
56     p_i->p_stats = NULL;
57     p_i->p_meta = NULL;
58
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 );
70 }
71
72 static inline void input_item_Clean( input_item_t *p_i )
73 {
74     int i;
75
76     vlc_event_manager_fini( &p_i->event_manager );
77
78     free( p_i->psz_name );
79     free( p_i->psz_uri );
80     if( p_i->p_stats )
81     {
82         vlc_mutex_destroy( &p_i->p_stats->lock );
83         free( p_i->p_stats );
84     }
85
86     if( p_i->p_meta )
87         vlc_meta_Delete( p_i->p_meta );
88
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 );
92     free( p_i->optflagv);
93
94     for( i = 0; i < p_i->i_es; i++ )
95     {
96         es_format_Clean( p_i->es[i] );
97         free( p_i->es[i] );
98     }
99     TAB_CLEAN( p_i->i_es, p_i->es );
100
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 );
104
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 );
108
109     vlc_mutex_destroy( &p_i->lock );
110 }
111 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
112 {
113     bool b_changed;
114
115     vlc_mutex_lock( &p_i->lock );
116
117     b_changed = p_i->b_error_when_reading != b_error;
118     p_i->b_error_when_reading = b_error;
119
120     vlc_mutex_unlock( &p_i->lock );
121
122     if( b_changed )
123     {
124         vlc_event_t event;
125
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 );
129     }
130 }
131 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
132 {
133     bool b_send_event = false;
134
135     vlc_mutex_lock( &p_i->lock );
136
137     if( !p_i->p_meta )
138         p_i->p_meta = vlc_meta_New();
139
140     int status = vlc_meta_GetStatus(p_i->p_meta);
141     int new_status;
142     if( b_preparsed )
143         new_status = status | ITEM_PREPARSED;
144     else
145         new_status = status & ~ITEM_PREPARSED;
146     if( status != new_status )
147     {
148         vlc_meta_SetStatus(p_i->p_meta, new_status);
149         b_send_event = true;
150     }
151
152     vlc_mutex_unlock( &p_i->lock );
153
154     if( b_send_event )
155     {
156         vlc_event_t event;
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 );
160     }
161 }
162
163 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
164 {
165     vlc_mutex_lock( &p_i->lock );
166
167     if( !p_i->p_meta )
168         p_i->p_meta = vlc_meta_New();
169
170     int status = vlc_meta_GetStatus(p_i->p_meta);
171
172     if( b_not_found )
173         status |= ITEM_ART_NOTFOUND;
174     else
175         status &= ~ITEM_ART_NOTFOUND;
176
177     vlc_meta_SetStatus(p_i->p_meta, status);
178
179     vlc_mutex_unlock( &p_i->lock );
180 }
181
182 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
183 {
184     vlc_mutex_lock( &p_i->lock );
185
186     if( !p_i->p_meta )
187         p_i->p_meta = vlc_meta_New();
188
189     int status = vlc_meta_GetStatus(p_i->p_meta);
190
191     if( b_art_fetched )
192         status |= ITEM_ART_FETCHED;
193     else
194         status &= ~ITEM_ART_FETCHED;
195
196     vlc_meta_SetStatus(p_i->p_meta, status);
197
198     vlc_mutex_unlock( &p_i->lock );
199 }
200
201 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
202 {
203     vlc_event_t event;
204
205     vlc_mutex_lock( &p_i->lock );
206     if( !p_i->p_meta )
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 );
210
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 );
215 }
216
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 )
221 {
222     vlc_mutex_lock( &p_parent->lock );
223
224     for( int i = 0 ; i< p_parent->i_options; i++ )
225     {
226         if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
227             continue;
228
229         input_item_AddOption( p_child,
230                               p_parent->ppsz_options[i],
231                               p_parent->optflagv[i] );
232     }
233
234     vlc_mutex_unlock( &p_parent->lock );
235 }
236
237 static void notify_subitem_added(input_item_t *p_parent, input_item_t *p_child)
238 {
239     /* Notify interested third parties */
240     vlc_event_t event;
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 );
244 }
245
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 )
251 {
252     vlc_mutex_lock( &p_parent->lock );
253     p_parent->i_type = ITEM_TYPE_PLAYLIST;
254     vlc_mutex_unlock( &p_parent->lock );
255
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 );
259 }
260
261 bool input_item_HasErrorWhenReading( input_item_t *p_item )
262 {
263     vlc_mutex_lock( &p_item->lock );
264
265     bool b_error = p_item->b_error_when_reading;
266
267     vlc_mutex_unlock( &p_item->lock );
268
269     return b_error;
270 }
271
272 bool input_item_MetaMatch( input_item_t *p_i,
273                            vlc_meta_type_t meta_type, const char *psz )
274 {
275     vlc_mutex_lock( &p_i->lock );
276
277     if( !p_i->p_meta )
278     {
279         vlc_mutex_unlock( &p_i->lock );
280         return false;
281     }
282     const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
283     bool b_ret = psz_meta && strcasestr( psz_meta, psz );
284
285     vlc_mutex_unlock( &p_i->lock );
286
287     return b_ret;
288 }
289
290 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
291 {
292     vlc_mutex_lock( &p_i->lock );
293
294     if( !p_i->p_meta )
295     {
296         vlc_mutex_unlock( &p_i->lock );
297         return NULL;
298     }
299
300     char *psz = NULL;
301     if( vlc_meta_Get( p_i->p_meta, meta_type ) )
302         psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
303
304     vlc_mutex_unlock( &p_i->lock );
305     return psz;
306 }
307
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 )
310 {
311     char *psz_ret;
312     vlc_mutex_lock( &p_item->lock );
313
314     if( !p_item->p_meta )
315     {
316         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
317         vlc_mutex_unlock( &p_item->lock );
318         return psz_ret;
319     }
320
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 );
324     else
325         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
326
327     vlc_mutex_unlock( &p_item->lock );
328     return psz_ret;
329 }
330
331 char *input_item_GetName( input_item_t *p_item )
332 {
333     vlc_mutex_lock( &p_item->lock );
334
335     char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
336
337     vlc_mutex_unlock( &p_item->lock );
338     return psz_name;
339 }
340 void input_item_SetName( input_item_t *p_item, const char *psz_name )
341 {
342     vlc_mutex_lock( &p_item->lock );
343
344     free( p_item->psz_name );
345     p_item->psz_name = strdup( psz_name );
346
347     vlc_mutex_unlock( &p_item->lock );
348 }
349
350 char *input_item_GetURI( input_item_t *p_i )
351 {
352     vlc_mutex_lock( &p_i->lock );
353
354     char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
355
356     vlc_mutex_unlock( &p_i->lock );
357     return psz_s;
358 }
359 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
360 {
361     vlc_mutex_lock( &p_i->lock );
362 #ifndef NDEBUG
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" );
365 #endif
366
367     free( p_i->psz_uri );
368     p_i->psz_uri = strdup( psz_uri );
369
370     p_i->i_type = GuessType( p_i );
371
372     if( p_i->psz_name )
373         ;
374     else
375     if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
376     {
377         const char *psz_filename = strrchr( p_i->psz_uri, '/' );
378
379         if( psz_filename && *psz_filename == '/' )
380             psz_filename++;
381         if( psz_filename && *psz_filename )
382             p_i->psz_name = strdup( psz_filename );
383
384         /* Make the name more readable */
385         if( p_i->psz_name )
386             decode_URI( p_i->psz_name );
387     }
388     else
389     {   /* Strip login and password from title */
390         int r;
391         vlc_url_t url;
392
393         vlc_UrlParse( &url, psz_uri, 0 );
394         if( url.psz_protocol )
395         {
396             if( url.i_port > 0 )
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 : "" );
400             else
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 : "" );
404         }
405         else
406         {
407             if( url.i_port > 0 )
408                 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
409                           url.psz_path ? url.psz_path : "" );
410             else
411                 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
412                           url.psz_path ? url.psz_path : "" );
413         }
414         vlc_UrlClean( &url );
415         if( -1==r )
416             p_i->psz_name=NULL; /* recover from undefined value */
417     }
418
419     vlc_mutex_unlock( &p_i->lock );
420 }
421
422 mtime_t input_item_GetDuration( input_item_t *p_i )
423 {
424     vlc_mutex_lock( &p_i->lock );
425
426     mtime_t i_duration = p_i->i_duration;
427
428     vlc_mutex_unlock( &p_i->lock );
429     return i_duration;
430 }
431
432 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
433 {
434     bool b_send_event = false;
435
436     vlc_mutex_lock( &p_i->lock );
437     if( p_i->i_duration != i_duration )
438     {
439         p_i->i_duration = i_duration;
440         b_send_event = true;
441     }
442     vlc_mutex_unlock( &p_i->lock );
443
444     if( b_send_event )
445     {
446         vlc_event_t event;
447
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 );
451     }
452 }
453
454
455 bool input_item_IsPreparsed( input_item_t *p_item )
456 {
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 );
460
461     return b_preparsed;
462 }
463
464 bool input_item_IsArtFetched( input_item_t *p_item )
465 {
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 );
469
470     return b_fetched;
471 }
472
473 static void input_item_Destroy ( gc_object_t *p_gc )
474 {
475     input_item_t *p_item = vlc_priv( p_gc, input_item_t );
476
477     input_item_Clean( p_item );
478     free( p_item );
479 }
480
481 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
482                           unsigned flags )
483 {
484     int err = VLC_SUCCESS;
485
486     if( psz_option == NULL )
487         return VLC_EGENERIC;
488
489     vlc_mutex_lock( &p_input->lock );
490     if (flags & VLC_INPUT_OPTION_UNIQUE)
491     {
492         for (int i = 0 ; i < p_input->i_options; i++)
493             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
494                 goto out;
495     }
496
497     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
498     if (flagv == NULL)
499     {
500         err = VLC_ENOMEM;
501         goto out;
502     }
503     p_input->optflagv = flagv;
504     flagv[p_input->optflagc++] = flags;
505
506     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
507                  p_input->i_options, strdup( psz_option ) );
508 out:
509     vlc_mutex_unlock( &p_input->lock );
510     return err;
511 }
512
513 static info_category_t *InputItemFindCat( input_item_t *p_item,
514                                           int *pi_index, const char *psz_cat )
515 {
516     vlc_assert_locked( &p_item->lock );
517     for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
518     {
519         info_category_t *p_cat = p_item->pp_categories[i];
520
521         if( !strcmp( p_cat->psz_name, psz_cat ) )
522         {
523             if( pi_index )
524                 *pi_index = i;
525             return p_cat;
526         }
527     }
528     return NULL;
529 }
530
531 /**
532  * Get a info item from a given category in a given input item.
533  *
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
539  *         pointer.
540  */
541 char *input_item_GetInfo( input_item_t *p_i,
542                           const char *psz_cat,
543                           const char *psz_name )
544 {
545     vlc_mutex_lock( &p_i->lock );
546
547     const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
548     if( p_cat )
549     {
550         info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
551         if( p_info && p_info->psz_value )
552         {
553             char *psz_ret = strdup( p_info->psz_value );
554             vlc_mutex_unlock( &p_i->lock );
555             return psz_ret;
556         }
557     }
558     vlc_mutex_unlock( &p_i->lock );
559     return strdup( "" );
560 }
561
562 static int InputItemVaAddInfo( input_item_t *p_i,
563                                const char *psz_cat,
564                                const char *psz_name,
565                                const char *psz_format, va_list args )
566 {
567     vlc_assert_locked( &p_i->lock );
568
569     info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
570     if( !p_cat )
571     {
572         p_cat = info_category_New( psz_cat );
573         if( !p_cat )
574             return VLC_ENOMEM;
575         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
576                      p_cat );
577     }
578     info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
579     if( !p_info || !p_info->psz_value )
580         return VLC_EGENERIC;
581     return VLC_SUCCESS;
582 }
583
584 static int InputItemAddInfo( input_item_t *p_i,
585                              const char *psz_cat,
586                              const char *psz_name,
587                              const char *psz_format, ... )
588 {
589     va_list args;
590
591     va_start( args, psz_format );
592     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
593     va_end( args );
594
595     return i_ret;
596 }
597
598 int input_item_AddInfo( input_item_t *p_i,
599                         const char *psz_cat,
600                         const char *psz_name,
601                         const char *psz_format, ... )
602 {
603     va_list args;
604
605     vlc_mutex_lock( &p_i->lock );
606
607     va_start( args, psz_format );
608     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
609     va_end( args );
610
611     vlc_mutex_unlock( &p_i->lock );
612
613
614     if( !i_ret )
615     {
616         vlc_event_t event;
617
618         event.type = vlc_InputItemInfoChanged;
619         vlc_event_send( &p_i->event_manager, &event );
620     }
621     return i_ret;
622 }
623
624 int input_item_DelInfo( input_item_t *p_i,
625                         const char *psz_cat,
626                         const char *psz_name )
627 {
628     vlc_mutex_lock( &p_i->lock );
629     int i_cat;
630     info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
631     if( !p_cat )
632     {
633         vlc_mutex_unlock( &p_i->lock );
634         return VLC_EGENERIC;
635     }
636
637     if( psz_name )
638     {
639         /* Remove a specific info */
640         int i_ret = info_category_DeleteInfo( p_cat, psz_name );
641         if( i_ret )
642         {
643             vlc_mutex_unlock( &p_i->lock );
644             return VLC_EGENERIC;
645         }
646     }
647     else
648     {
649         /* Remove the complete categorie */
650         info_category_Delete( p_cat );
651         REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
652     }
653     vlc_mutex_unlock( &p_i->lock );
654
655
656     vlc_event_t event;
657     event.type = vlc_InputItemInfoChanged;
658     vlc_event_send( &p_i->event_manager, &event );
659
660     return VLC_SUCCESS;
661 }
662 void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
663 {
664     vlc_mutex_lock( &p_item->lock );
665     int i_cat;
666     info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
667     if( p_old )
668     {
669         info_category_Delete( p_old );
670         p_item->pp_categories[i_cat] = p_cat;
671     }
672     else
673     {
674         INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
675                      p_cat );
676     }
677     vlc_mutex_unlock( &p_item->lock );
678
679
680     vlc_event_t event;
681     event.type = vlc_InputItemInfoChanged;
682     vlc_event_send( &p_item->event_manager, &event );
683 }
684 void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
685 {
686     vlc_mutex_lock( &p_item->lock );
687     info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
688     if( p_old )
689     {
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 );
694     }
695     else
696     {
697         INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
698                      p_cat );
699     }
700     vlc_mutex_unlock( &p_item->lock );
701
702
703     vlc_event_t event;
704     event.type = vlc_InputItemInfoChanged;
705     vlc_event_send( &p_item->event_manager, &event );
706 }
707
708 #define EPG_DEBUG
709 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
710 {
711     vlc_mutex_lock( &p_item->lock );
712
713     /* */
714     vlc_epg_t *p_epg = NULL;
715     for( int i = 0; i < p_item->i_epg; i++ )
716     {
717         vlc_epg_t *p_tmp = p_item->pp_epg[i];
718
719         if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
720             continue;
721         if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
722             continue;
723
724         p_epg = p_tmp;
725         break;
726     }
727
728     /* */
729     if( !p_epg )
730     {
731         p_epg = vlc_epg_New( p_update->psz_name );
732         if( p_epg )
733             TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
734     }
735     if( p_epg )
736         vlc_epg_Merge( p_epg, p_update );
737
738     vlc_mutex_unlock( &p_item->lock );
739
740 #ifdef EPG_DEBUG
741     char *psz_epg;
742     if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
743         goto signal;
744
745     input_item_DelInfo( p_item, psz_epg, NULL );
746
747     vlc_mutex_lock( &p_item->lock );
748     for( int i = 0; i < p_epg->i_event; i++ )
749     {
750         const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
751         time_t t_start = (time_t)p_evt->i_start;
752         struct tm tm_start;
753         char psz_start[128];
754
755         localtime_r( &t_start, &tm_start );
756
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",
762                               p_evt->psz_name,
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 : "" );
766         else
767             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
768                               p_evt->psz_name,
769                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
770     }
771     vlc_mutex_unlock( &p_item->lock );
772     free( psz_epg );
773 signal:
774 #endif
775
776     if( p_epg->i_event > 0 )
777     {
778         vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
779         vlc_event_send( &p_item->event_manager, &event );
780     }
781 }
782
783 void input_item_SetEpgOffline( input_item_t *p_item )
784 {
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 );
789
790 #ifdef EPG_DEBUG
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++ )
795     {
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;
799     }
800     vlc_mutex_unlock( &p_item->lock );
801
802     for( int i = 0; i < i_epg_info; i++ )
803     {
804         if( !ppsz_epg_info[i] )
805             continue;
806         input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
807         free( ppsz_epg_info[i] );
808     }
809 #endif
810
811     vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
812     vlc_event_send( &p_item->event_manager, &event );
813 }
814
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,
818                                  int i_options,
819                                  const char *const *ppsz_options,
820                                  unsigned i_option_flags,
821                                  mtime_t i_duration )
822 {
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 );
826 }
827
828
829 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
830                                 const char *psz_name,
831                                 int i_options,
832                                 const char *const *ppsz_options,
833                                 unsigned i_option_flags,
834                                 mtime_t i_duration,
835                                 int i_type )
836 {
837     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
838     static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
839
840     input_item_t* p_input = malloc( sizeof(input_item_t ) );
841     if( !p_input )
842         return NULL;
843
844     input_item_Init( p_obj, p_input );
845     vlc_gc_init( p_input, input_item_Destroy );
846
847     vlc_mutex_lock( &input_id_lock );
848     p_input->i_id = ++priv->i_last_input_id;
849     vlc_mutex_unlock( &input_id_lock );
850
851     p_input->b_fixed_name = false;
852
853     p_input->i_type = i_type;
854     p_input->b_prefers_tree = false;
855
856     if( psz_uri )
857         input_item_SetURI( p_input, psz_uri );
858
859     if( i_type != ITEM_TYPE_UNKNOWN )
860         p_input->i_type = i_type;
861
862     if( psz_name )
863         input_item_SetName( p_input, psz_name );
864
865     p_input->i_duration = i_duration;
866
867     for( int i = 0; i < i_options; i++ )
868         input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
869     return p_input;
870 }
871
872 struct item_type_entry
873 {
874     const char psz_scheme[7];
875     uint8_t    i_type;
876 };
877
878 static int typecmp( const void *key, const void *entry )
879 {
880     const struct item_type_entry *type = entry;
881     const char *uri = key, *scheme = type->psz_scheme;
882
883     return strncmp( uri, scheme, strlen( scheme ) );
884 }
885
886 /* Guess the type of the item using the beginning of the mrl */
887 static int GuessType( const input_item_t *p_item )
888 {
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 },
934     };
935     const struct item_type_entry *e;
936
937     if( !strstr( p_item->psz_uri, "://" ) )
938         return ITEM_TYPE_FILE;
939
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;
943 }
944
945 input_item_node_t *input_item_node_Create( input_item_t *p_input )
946 {
947     input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
948     if( !p_node )
949         return NULL;
950
951     assert( p_input );
952
953     p_node->p_item = p_input;
954     vlc_gc_incref( p_input );
955
956     p_node->p_parent = NULL;
957     p_node->i_children = 0;
958     p_node->pp_children = NULL;
959
960     return p_node;
961 }
962
963 static void RecursiveNodeDelete( input_item_node_t *p_node )
964 {
965   for( int i = 0; i < p_node->i_children; i++ )
966       RecursiveNodeDelete( p_node->pp_children[i] );
967
968   vlc_gc_decref( p_node->p_item );
969   free( p_node->pp_children );
970   free( p_node );
971 }
972
973 void input_item_node_Delete( input_item_node_t *p_node )
974 {
975   if(  p_node->p_parent )
976   {
977       for( int i = 0; i < p_node->p_parent->i_children; i++ )
978           if( p_node->p_parent->pp_children[i] == p_node )
979           {
980               REMOVE_ELEM( p_node->p_parent->pp_children,
981                            p_node->p_parent->i_children,
982                            i );
983               break;
984           }
985   }
986
987   RecursiveNodeDelete( p_node );
988 }
989
990 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
991 {
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 );
995     return p_new_child;
996 }
997
998 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
999 {
1000     notify_subitem_added(p_parent->p_item, p_child->p_item);
1001
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,
1006                  p_child );
1007     p_child->p_parent = p_parent;
1008 }
1009
1010 void input_item_node_PostAndDelete( input_item_node_t *p_root )
1011 {
1012   vlc_event_t event;
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 );
1016
1017   input_item_node_Delete( p_root );
1018 }