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 "playlist_internal.h"
33 #include "../libvlc.h"
35 struct vlc_sd_internal_t
37 /* the playlist items for category and onelevel */
38 playlist_item_t *p_cat;
39 playlist_item_t *p_one;
40 services_discovery_t *p_sd; /**< Loaded service discovery modules */
44 static void services_discovery_Destructor ( vlc_object_t *p_obj );
48 * Basically you just listen to Service discovery event through the
50 * That's how the playlist get's Service Discovery information
54 * Gets the list of available services discovery plugins.
56 char **vlc_sd_GetNames( char ***pppsz_longnames )
58 return module_GetModulesNamesForCapability( "services_discovery",
62 /***********************************************************************
64 ***********************************************************************/
65 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
67 services_discovery_t *p_sd;
69 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
70 "services discovery" );
74 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
75 vlc_event_manager_register_event_type( &p_sd->event_manager,
76 vlc_ServicesDiscoveryItemAdded );
77 vlc_event_manager_register_event_type( &p_sd->event_manager,
78 vlc_ServicesDiscoveryItemRemoved );
79 vlc_event_manager_register_event_type( &p_sd->event_manager,
80 vlc_ServicesDiscoveryStarted );
81 vlc_event_manager_register_event_type( &p_sd->event_manager,
82 vlc_ServicesDiscoveryEnded );
84 vlc_object_set_destructor( p_sd, services_discovery_Destructor );
85 vlc_object_attach( p_sd, p_super );
90 /***********************************************************************
92 ***********************************************************************/
93 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
95 assert(!p_sd->p_module);
97 p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
99 if( p_sd->p_module == NULL )
101 msg_Err( p_sd, "no suitable services discovery module" );
105 vlc_event_t event = {
106 .type = vlc_ServicesDiscoveryStarted
108 vlc_event_send( &p_sd->event_manager, &event );
112 /***********************************************************************
114 ***********************************************************************/
115 void vlc_sd_Stop ( services_discovery_t * p_sd )
117 vlc_event_t event = {
118 .type = vlc_ServicesDiscoveryEnded
121 vlc_event_send( &p_sd->event_manager, &event );
123 module_unneed( p_sd, p_sd->p_module );
124 p_sd->p_module = NULL;
127 /***********************************************************************
129 ***********************************************************************/
130 static void services_discovery_Destructor ( vlc_object_t *p_obj )
132 services_discovery_t * p_sd = (services_discovery_t *)p_obj;
133 assert(!p_sd->p_module); /* Forgot to call Stop */
134 vlc_event_manager_fini( &p_sd->event_manager );
137 /***********************************************************************
139 ***********************************************************************/
141 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
143 return strdup( module_get_name( p_sd->p_module, true ) );
146 /***********************************************************************
148 ***********************************************************************/
149 vlc_event_manager_t *
150 services_discovery_EventManager ( services_discovery_t * p_sd )
152 return &p_sd->event_manager;
155 /***********************************************************************
157 ***********************************************************************/
159 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
160 const char * psz_category )
163 event.type = vlc_ServicesDiscoveryItemAdded;
164 event.u.services_discovery_item_added.p_new_item = p_item;
165 event.u.services_discovery_item_added.psz_category = psz_category;
167 vlc_event_send( &p_sd->event_manager, &event );
170 /***********************************************************************
172 ***********************************************************************/
174 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
177 event.type = vlc_ServicesDiscoveryItemRemoved;
178 event.u.services_discovery_item_removed.p_item = p_item;
180 vlc_event_send( &p_sd->event_manager, &event );
184 * Playlist - Services discovery bridge
187 /* A new item has been added to a certain sd */
188 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
190 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
191 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
192 playlist_item_t *p_new_item, * p_parent = user_data;
193 playlist_t * p_playlist = p_parent->p_playlist;
195 msg_Dbg( p_playlist, "Adding %s in %s",
196 p_input->psz_name ? p_input->psz_name : "(null)",
197 psz_cat ? psz_cat : "(null)" );
200 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
201 if( !EMPTY_STR(psz_cat) &&
202 p_parent->p_parent == p_playlist->p_root_category )
205 playlist_item_t * p_cat;
206 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
209 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
211 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
216 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
217 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
220 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
221 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
226 /* A new item has been removed from a certain sd */
227 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
229 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
230 playlist_item_t * p_parent = user_data;
231 playlist_item_t * p_pl_item;
233 /* First make sure that if item is a node it will be deleted.
234 * XXX: Why don't we have a function to ensure that in the playlist code ? */
235 playlist_Lock( p_parent->p_playlist );
236 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
237 p_input, p_parent, false );
239 if( p_pl_item && p_pl_item->i_children > -1 )
240 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
242 /* Delete the non-node item normally */
243 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input,
244 p_parent, pl_Locked );
246 playlist_Unlock( p_parent->p_playlist );
249 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
251 /* Perform the addition */
252 msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
254 services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
258 module_t *m = module_find_by_shortcut( psz_module );
261 msg_Err( p_playlist, "No such module: %s", psz_module );
262 vlc_sd_Destroy( p_sd );
266 /* Free in playlist_ServicesDiscoveryRemove */
267 vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
270 vlc_sd_Destroy( p_sd );
275 playlist_item_t * p_cat;
276 playlist_item_t * p_one;
279 playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
280 &p_cat, &p_one, false );
284 vlc_event_attach( services_discovery_EventManager( p_sd ),
285 vlc_ServicesDiscoveryItemAdded,
286 playlist_sd_item_added, p_one );
288 vlc_event_attach( services_discovery_EventManager( p_sd ),
289 vlc_ServicesDiscoveryItemAdded,
290 playlist_sd_item_added, p_cat );
292 vlc_event_attach( services_discovery_EventManager( p_sd ),
293 vlc_ServicesDiscoveryItemRemoved,
294 playlist_sd_item_removed, p_one );
296 vlc_event_attach( services_discovery_EventManager( p_sd ),
297 vlc_ServicesDiscoveryItemRemoved,
298 playlist_sd_item_removed, p_cat );
300 if( !vlc_sd_Start( p_sd, psz_module ) )
302 vlc_sd_Destroy( p_sd );
307 /* We want tree-view for service directory */
308 p_one->p_input->b_prefers_tree = true;
310 p_sds->p_one = p_one;
311 p_sds->p_cat = p_cat;
312 p_sds->psz_name = strdup( psz_module );
315 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
321 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
322 const char *psz_name )
324 playlist_private_t *priv = pl_priv( p_playlist );
325 vlc_sd_internal_t * p_sds = NULL;
328 for( int i = 0; i < priv->i_sds; i++ )
330 if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
332 p_sds = priv->pp_sds[i];
333 REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
341 msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
345 services_discovery_t *p_sd = p_sds->p_sd;
350 vlc_event_detach( services_discovery_EventManager( p_sd ),
351 vlc_ServicesDiscoveryItemAdded,
352 playlist_sd_item_added,
355 vlc_event_detach( services_discovery_EventManager( p_sd ),
356 vlc_ServicesDiscoveryItemAdded,
357 playlist_sd_item_added,
360 vlc_event_detach( services_discovery_EventManager( p_sd ),
361 vlc_ServicesDiscoveryItemRemoved,
362 playlist_sd_item_removed,
365 vlc_event_detach( services_discovery_EventManager( p_sd ),
366 vlc_ServicesDiscoveryItemRemoved,
367 playlist_sd_item_removed,
370 /* Remove the sd playlist node if it exists */
372 if( p_sds->p_cat != p_playlist->p_root_category &&
373 p_sds->p_one != p_playlist->p_root_onelevel )
375 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
376 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
380 vlc_sd_Destroy( p_sd );
381 free( p_sds->psz_name );
387 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
388 const char *psz_name )
390 playlist_private_t *priv = pl_priv( p_playlist );
394 for( int i = 0; i < priv->i_sds; i++ )
396 vlc_sd_internal_t *sd = priv->pp_sds[i];
398 if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
408 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
410 playlist_private_t *priv = pl_priv( p_playlist );
412 while( priv->i_sds > 0 )
413 playlist_ServicesDiscoveryRemove( p_playlist,
414 priv->pp_sds[0]->psz_name );