]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
Remove children kill hack
[vlc] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-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
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
27
28 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "vlc_events.h"
31 #include <vlc_services_discovery.h>
32 #include "playlist_internal.h"
33 #include "../libvlc.h"
34
35
36 /*
37  * Services discovery
38  * Basically you just listen to Service discovery event through the
39  * sd's event manager.
40  * That's how the playlist get's Service Discovery information
41  */
42
43 /***********************************************************************
44  * GetServicesNames
45  ***********************************************************************/
46 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
47                                                char ***pppsz_longnames )
48 {
49     return module_GetModulesNamesForCapability( "services_discovery",
50                                                 pppsz_longnames );
51 }
52
53 /***********************************************************************
54  * Create
55  ***********************************************************************/
56 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
57 {
58     services_discovery_t *p_sd;
59
60     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
61                               "services discovery" );
62     if( !p_sd )
63         return NULL;
64
65     vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
66     vlc_event_manager_register_event_type( &p_sd->event_manager,
67             vlc_ServicesDiscoveryItemAdded );
68     vlc_event_manager_register_event_type( &p_sd->event_manager,
69             vlc_ServicesDiscoveryItemRemoved );
70     vlc_event_manager_register_event_type( &p_sd->event_manager,
71             vlc_ServicesDiscoveryStarted );
72     vlc_event_manager_register_event_type( &p_sd->event_manager,
73             vlc_ServicesDiscoveryEnded );
74
75     vlc_object_attach( p_sd, p_super );
76
77     return p_sd;
78 }
79
80 /***********************************************************************
81  * Stop
82  ***********************************************************************/
83 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
84 {
85     assert(!p_sd->p_module);
86
87     p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
88
89     if( p_sd->p_module == NULL )
90     {
91         msg_Err( p_sd, "no suitable services discovery module" );
92         return false;
93     }
94         
95     vlc_event_t event = {
96         .type = vlc_ServicesDiscoveryStarted
97     };
98     vlc_event_send( &p_sd->event_manager, &event );
99     return true;
100 }
101                           
102 /***********************************************************************
103  * Stop
104  ***********************************************************************/
105 void vlc_sd_Stop ( services_discovery_t * p_sd )
106 {
107     vlc_event_t event = {
108         .type = vlc_ServicesDiscoveryEnded
109     };
110     
111     vlc_event_send( &p_sd->event_manager, &event );
112
113     module_unneed( p_sd, p_sd->p_module );
114     p_sd->p_module = NULL;
115 }
116
117 /***********************************************************************
118  * GetLocalizedName
119  ***********************************************************************/
120 char *
121 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
122 {
123     return strdup( module_get_name( p_sd->p_module, true ) );
124 }
125
126 /***********************************************************************
127  * EventManager
128  ***********************************************************************/
129 vlc_event_manager_t *
130 services_discovery_EventManager ( services_discovery_t * p_sd )
131 {
132     return &p_sd->event_manager;
133 }
134
135 /***********************************************************************
136  * AddItem
137  ***********************************************************************/
138 void
139 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
140                              const char * psz_category )
141 {
142     vlc_event_t event;
143     event.type = vlc_ServicesDiscoveryItemAdded;
144     event.u.services_discovery_item_added.p_new_item = p_item;
145     event.u.services_discovery_item_added.psz_category = psz_category;
146
147     vlc_event_send( &p_sd->event_manager, &event );
148 }
149
150 /***********************************************************************
151  * RemoveItem
152  ***********************************************************************/
153 void
154 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
155 {
156     vlc_event_t event;
157     event.type = vlc_ServicesDiscoveryItemRemoved;
158     event.u.services_discovery_item_removed.p_item = p_item;
159
160     vlc_event_send( &p_sd->event_manager, &event );
161 }
162
163 /*
164  * Playlist - Services discovery bridge
165  */
166
167  /* A new item has been added to a certain sd */
168 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
169 {
170     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
171     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
172     playlist_item_t *p_new_item, * p_parent = user_data;
173     playlist_t * p_playlist = p_parent->p_playlist;
174
175     msg_Dbg( p_playlist, "Adding %s in %s",
176                 p_input->psz_name ? p_input->psz_name : "(null)",
177                 psz_cat ? psz_cat : "(null)" );
178
179     PL_LOCK;
180     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
181     if( !EMPTY_STR(psz_cat) &&
182         p_parent->p_parent == p_playlist->p_root_category )
183     {
184         /* */
185         playlist_item_t * p_cat;
186         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
187         if( !p_cat )
188         {
189             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
190                                          p_parent, 0, NULL );
191             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
192         }
193         p_parent = p_cat;
194     }
195
196     p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
197                                         PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
198     if( p_new_item )
199     {
200         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
201         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
202     }
203     PL_UNLOCK;
204 }
205
206  /* A new item has been removed from a certain sd */
207 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
208 {
209     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
210     playlist_item_t * p_parent = user_data;
211     playlist_item_t * p_pl_item;
212
213     /* First make sure that if item is a node it will be deleted.
214      * XXX: Why don't we have a function to ensure that in the playlist code ? */
215     vlc_object_lock( p_parent->p_playlist );
216     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
217             p_input->i_id, p_parent, false );
218
219     if( p_pl_item && p_pl_item->i_children > -1 )
220     {
221         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
222         vlc_object_unlock( p_parent->p_playlist );
223         return;
224     }
225
226     /* Delete the non-node item normally */
227     playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
228                                       p_parent, pl_Locked );
229
230     vlc_object_unlock( p_parent->p_playlist );
231 }
232
233 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
234 {
235     const char *psz_parser = psz_modules ?: "";
236     int retval = VLC_SUCCESS;
237
238     for (;;)
239     {
240         struct playlist_services_discovery_support_t * p_sds;
241         playlist_item_t * p_cat;
242         playlist_item_t * p_one;
243
244         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
245             psz_parser++;
246
247         if( *psz_parser == '\0' )
248             break;
249
250         const char *psz_next = strchr( psz_parser, ':' );
251         if( psz_next == NULL )
252             psz_next = psz_parser + strlen( psz_parser );
253
254         char psz_plugin[psz_next - psz_parser + 1];
255         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
256         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
257         psz_parser = psz_next;
258
259         /* Perform the addition */
260         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
261         services_discovery_t *p_sd;
262
263         p_sd = vlc_sd_Create( (vlc_object_t*)p_playlist );
264         if( !p_sd )
265             continue;
266
267         vlc_event_attach( services_discovery_EventManager( p_sd ),
268                           vlc_ServicesDiscoveryItemAdded,
269                           playlist_sd_item_added,
270                           p_one );
271         
272         vlc_event_attach( services_discovery_EventManager( p_sd ),
273                           vlc_ServicesDiscoveryItemAdded,
274                           playlist_sd_item_added,
275                           p_cat );
276
277         vlc_event_attach( services_discovery_EventManager( p_sd ),
278                           vlc_ServicesDiscoveryItemRemoved,
279                           playlist_sd_item_removed,
280                           p_one );
281
282         vlc_event_attach( services_discovery_EventManager( p_sd ),
283                           vlc_ServicesDiscoveryItemRemoved,
284                           playlist_sd_item_removed,
285                           p_cat );
286
287         if( !vlc_sd_Start( p_sd, psz_plugin ) )
288         {
289             vlc_sd_Destroy( p_sd );
290             return VLC_EGENERIC;
291         }        
292
293         char *psz = services_discovery_GetLocalizedName( p_sd );
294         assert( psz );
295         PL_LOCK;
296         playlist_NodesPairCreate( p_playlist, psz,
297                 &p_cat, &p_one, false );
298         PL_UNLOCK;
299         free( psz );
300
301         /* Free in playlist_ServicesDiscoveryRemove */
302         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
303         if( !p_sds )
304             return VLC_ENOMEM;
305
306         /* We want tree-view for service directory */
307         p_one->p_input->b_prefers_tree = true;
308         p_sds->p_sd = p_sd;
309         p_sds->p_one = p_one;
310         p_sds->p_cat = p_cat;
311
312         PL_LOCK;
313         TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
314         PL_UNLOCK;
315
316     }
317
318     return retval;
319 }
320
321 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
322                                       const char *psz_module )
323 {
324     struct playlist_services_discovery_support_t * p_sds = NULL;
325     int i;
326
327     PL_LOCK;
328     for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
329     {
330         if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
331         {
332             p_sds = pl_priv(p_playlist)->pp_sds[i];
333             REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
334             break;
335         }
336     }
337     PL_UNLOCK;
338
339     if( !p_sds || !p_sds->p_sd )
340     {
341         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
342         return VLC_EGENERIC;
343     }
344
345     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
346                         vlc_ServicesDiscoveryItemAdded,
347                         playlist_sd_item_added,
348                         p_sds->p_one );
349
350     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
351                         vlc_ServicesDiscoveryItemAdded,
352                         playlist_sd_item_added,
353                         p_sds->p_cat );
354
355     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
356                         vlc_ServicesDiscoveryItemRemoved,
357                         playlist_sd_item_removed,
358                         p_sds->p_one );
359
360     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
361                         vlc_ServicesDiscoveryItemRemoved,
362                         playlist_sd_item_removed,
363                         p_sds->p_cat );
364
365     /* Remove the sd playlist node if it exists */
366     PL_LOCK;
367     if( p_sds->p_cat != p_playlist->p_root_category &&
368         p_sds->p_one != p_playlist->p_root_onelevel )
369     {
370         playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
371         playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
372     }
373     PL_UNLOCK;
374
375     vlc_sd_StopAndDestroy( p_sds->p_sd );
376     free( p_sds );
377
378     return VLC_SUCCESS;
379 }
380
381 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
382                                               const char *psz_module )
383 {
384     int i;
385     PL_LOCK;
386
387     for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
388     {
389         if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
390         {
391             PL_UNLOCK;
392             return true;
393         }
394     }
395     PL_UNLOCK;
396     return false;
397 }
398
399 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
400 {
401     while( pl_priv(p_playlist)->i_sds > 0 )
402         playlist_ServicesDiscoveryRemove( p_playlist,
403                                           module_get_object( pl_priv(p_playlist)->pp_sds[0]->p_sd->p_module ) );
404 }