1 /*****************************************************************************
2 * services_discovery.c : Manage playlist services_discovery modules
3 *****************************************************************************
4 * Copyright (C) 1999-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
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "vlc_events.h"
31 #include <vlc_services_discovery.h>
32 #include <vlc_probe.h>
33 #include "playlist_internal.h"
34 #include "../libvlc.h"
36 #undef vlc_sd_GetNames
45 * Gets the list of available services discovery plugins.
47 char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
50 vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
58 char **names = malloc (sizeof(char *) * (count + 1));
59 char **longnames = malloc (sizeof(char *) * (count + 1));
61 if (unlikely (names == NULL || longnames == NULL))
63 for( size_t i = 0; i < count; i++ )
65 names[i] = tab[i].name;
66 longnames[i] = tab[i].longname;
69 names[count] = longnames[count] = NULL;
70 *pppsz_longnames = longnames;
75 struct vlc_sd_internal_t
77 /* the playlist items for category and onelevel */
78 playlist_item_t *p_cat;
79 playlist_item_t *p_one;
80 services_discovery_t *p_sd; /**< Loaded service discovery modules */
84 static void services_discovery_Destructor ( vlc_object_t *p_obj );
88 * Basically you just listen to Service discovery event through the
90 * That's how the playlist get's Service Discovery information
93 /***********************************************************************
95 ***********************************************************************/
96 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
98 services_discovery_t *p_sd;
100 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
101 "services discovery" );
105 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
106 vlc_event_manager_register_event_type( &p_sd->event_manager,
107 vlc_ServicesDiscoveryItemAdded );
108 vlc_event_manager_register_event_type( &p_sd->event_manager,
109 vlc_ServicesDiscoveryItemRemoved );
110 vlc_event_manager_register_event_type( &p_sd->event_manager,
111 vlc_ServicesDiscoveryStarted );
112 vlc_event_manager_register_event_type( &p_sd->event_manager,
113 vlc_ServicesDiscoveryEnded );
115 vlc_object_set_destructor( p_sd, services_discovery_Destructor );
116 vlc_object_attach( p_sd, p_super );
121 /***********************************************************************
123 ***********************************************************************/
124 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
126 assert(!p_sd->p_module);
128 p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
130 if( p_sd->p_module == NULL )
132 msg_Err( p_sd, "no suitable services discovery module" );
136 vlc_event_t event = {
137 .type = vlc_ServicesDiscoveryStarted
139 vlc_event_send( &p_sd->event_manager, &event );
143 /***********************************************************************
145 ***********************************************************************/
146 void vlc_sd_Stop ( services_discovery_t * p_sd )
148 vlc_event_t event = {
149 .type = vlc_ServicesDiscoveryEnded
152 vlc_event_send( &p_sd->event_manager, &event );
154 module_unneed( p_sd, p_sd->p_module );
155 p_sd->p_module = NULL;
158 /***********************************************************************
160 ***********************************************************************/
161 static void services_discovery_Destructor ( vlc_object_t *p_obj )
163 services_discovery_t * p_sd = (services_discovery_t *)p_obj;
164 assert(!p_sd->p_module); /* Forgot to call Stop */
165 vlc_event_manager_fini( &p_sd->event_manager );
168 /***********************************************************************
170 ***********************************************************************/
172 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
174 return strdup( module_get_name( p_sd->p_module, true ) );
177 /***********************************************************************
179 ***********************************************************************/
180 vlc_event_manager_t *
181 services_discovery_EventManager ( services_discovery_t * p_sd )
183 return &p_sd->event_manager;
186 /***********************************************************************
188 ***********************************************************************/
190 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
191 const char * psz_category )
194 event.type = vlc_ServicesDiscoveryItemAdded;
195 event.u.services_discovery_item_added.p_new_item = p_item;
196 event.u.services_discovery_item_added.psz_category = psz_category;
198 vlc_event_send( &p_sd->event_manager, &event );
201 /***********************************************************************
203 ***********************************************************************/
205 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
208 event.type = vlc_ServicesDiscoveryItemRemoved;
209 event.u.services_discovery_item_removed.p_item = p_item;
211 vlc_event_send( &p_sd->event_manager, &event );
215 * Playlist - Services discovery bridge
218 /* A new item has been added to a certain sd */
219 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
221 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
222 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
223 playlist_item_t *p_new_item, * p_parent = user_data;
224 playlist_t * p_playlist = p_parent->p_playlist;
226 msg_Dbg( p_playlist, "Adding %s in %s",
227 p_input->psz_name ? p_input->psz_name : "(null)",
228 psz_cat ? psz_cat : "(null)" );
231 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
232 if( !EMPTY_STR(psz_cat) &&
233 p_parent->p_parent == p_playlist->p_root_category )
236 playlist_item_t * p_cat;
237 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
240 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
242 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
247 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
248 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
251 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
252 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
257 /* A new item has been removed from a certain sd */
258 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
260 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
261 playlist_item_t * p_parent = user_data;
262 playlist_item_t * p_pl_item;
264 /* First make sure that if item is a node it will be deleted.
265 * XXX: Why don't we have a function to ensure that in the playlist code ? */
266 playlist_Lock( p_parent->p_playlist );
267 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
268 p_input, p_parent, false );
270 if( p_pl_item && p_pl_item->i_children > -1 )
271 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
273 /* Delete the non-node item normally */
274 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input,
275 p_parent, pl_Locked );
277 playlist_Unlock( p_parent->p_playlist );
280 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
282 /* Perform the addition */
283 msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
285 services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
289 module_t *m = module_find_by_shortcut( psz_module );
292 msg_Err( p_playlist, "No such module: %s", psz_module );
293 vlc_sd_Destroy( p_sd );
297 /* Free in playlist_ServicesDiscoveryRemove */
298 vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
301 vlc_sd_Destroy( p_sd );
306 playlist_item_t * p_cat;
307 playlist_item_t * p_one;
310 playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
311 &p_cat, &p_one, false );
315 vlc_event_attach( services_discovery_EventManager( p_sd ),
316 vlc_ServicesDiscoveryItemAdded,
317 playlist_sd_item_added, p_one );
319 vlc_event_attach( services_discovery_EventManager( p_sd ),
320 vlc_ServicesDiscoveryItemAdded,
321 playlist_sd_item_added, p_cat );
323 vlc_event_attach( services_discovery_EventManager( p_sd ),
324 vlc_ServicesDiscoveryItemRemoved,
325 playlist_sd_item_removed, p_one );
327 vlc_event_attach( services_discovery_EventManager( p_sd ),
328 vlc_ServicesDiscoveryItemRemoved,
329 playlist_sd_item_removed, p_cat );
331 if( !vlc_sd_Start( p_sd, psz_module ) )
333 vlc_sd_Destroy( p_sd );
338 /* We want tree-view for service directory */
339 p_one->p_input->b_prefers_tree = true;
341 p_sds->p_one = p_one;
342 p_sds->p_cat = p_cat;
343 p_sds->psz_name = strdup( psz_module );
346 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
352 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
353 const char *psz_name )
355 playlist_private_t *priv = pl_priv( p_playlist );
356 vlc_sd_internal_t * p_sds = NULL;
359 for( int i = 0; i < priv->i_sds; i++ )
361 if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
363 p_sds = priv->pp_sds[i];
364 REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
372 msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
376 services_discovery_t *p_sd = p_sds->p_sd;
381 vlc_event_detach( services_discovery_EventManager( p_sd ),
382 vlc_ServicesDiscoveryItemAdded,
383 playlist_sd_item_added,
386 vlc_event_detach( services_discovery_EventManager( p_sd ),
387 vlc_ServicesDiscoveryItemAdded,
388 playlist_sd_item_added,
391 vlc_event_detach( services_discovery_EventManager( p_sd ),
392 vlc_ServicesDiscoveryItemRemoved,
393 playlist_sd_item_removed,
396 vlc_event_detach( services_discovery_EventManager( p_sd ),
397 vlc_ServicesDiscoveryItemRemoved,
398 playlist_sd_item_removed,
401 /* Remove the sd playlist node if it exists */
403 if( p_sds->p_cat != p_playlist->p_root_category &&
404 p_sds->p_one != p_playlist->p_root_onelevel )
406 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
407 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
411 vlc_sd_Destroy( p_sd );
412 free( p_sds->psz_name );
418 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
419 const char *psz_name )
421 playlist_private_t *priv = pl_priv( p_playlist );
425 for( int i = 0; i < priv->i_sds; i++ )
427 vlc_sd_internal_t *sd = priv->pp_sds[i];
429 if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
439 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
441 playlist_private_t *priv = pl_priv( p_playlist );
443 while( priv->i_sds > 0 )
444 playlist_ServicesDiscoveryRemove( p_playlist,
445 priv->pp_sds[0]->psz_name );