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 "playlist_internal.h"
32 #include "../libvlc.h"
36 * Basically you just listen to Service discovery event through the
38 * That's how the playlist get's Service Discovery information
41 /***********************************************************************
43 ***********************************************************************/
44 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
45 char ***pppsz_longnames )
47 return module_GetModulesNamesForCapability( p_super, "services_discovery",
51 /***********************************************************************
53 ***********************************************************************/
54 services_discovery_t *
55 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
57 services_discovery_t *p_sd;
58 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
59 "services discovery" );
63 p_sd->psz_localized_name = NULL;
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 );
75 p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
77 if( p_sd->p_module == NULL )
79 msg_Err( p_super, "no suitable services discovery module" );
80 vlc_object_release( p_sd );
83 p_sd->psz_module = strdup( psz_module_name );
85 vlc_object_attach( p_sd, p_super );
88 .type = vlc_ServicesDiscoveryStarted
90 vlc_event_send( &p_sd->event_manager, &event );
94 static void ObjectKillChildrens( vlc_object_t *p_obj )
98 vlc_object_kill( p_obj );
100 p_list = vlc_list_children( p_obj );
101 for( i = 0; i < p_list->i_count; i++ )
102 ObjectKillChildrens( p_list->p_values[i].p_object );
103 vlc_list_release( p_list );
106 /***********************************************************************
108 ***********************************************************************/
109 void services_discovery_Destroy ( services_discovery_t * p_sd )
111 vlc_event_t event = {
112 .type = vlc_ServicesDiscoveryEnded
115 ObjectKillChildrens( VLC_OBJECT(p_sd) );
117 vlc_event_send( &p_sd->event_manager, &event );
118 module_Unneed( p_sd, p_sd->p_module );
120 vlc_event_manager_fini( &p_sd->event_manager );
122 free( p_sd->psz_module );
123 free( p_sd->psz_localized_name );
125 vlc_object_release( p_sd );
128 /***********************************************************************
130 ***********************************************************************/
132 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
134 return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
137 /***********************************************************************
139 ***********************************************************************/
141 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
143 free( p_sd->psz_localized_name );
144 p_sd->psz_localized_name = strdup(psz);
147 /***********************************************************************
149 ***********************************************************************/
150 vlc_event_manager_t *
151 services_discovery_EventManager ( services_discovery_t * p_sd )
153 return &p_sd->event_manager;
156 /***********************************************************************
158 ***********************************************************************/
160 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
161 const char * psz_category )
164 event.type = vlc_ServicesDiscoveryItemAdded;
165 event.u.services_discovery_item_added.p_new_item = p_item;
166 event.u.services_discovery_item_added.psz_category = psz_category;
168 vlc_event_send( &p_sd->event_manager, &event );
171 /***********************************************************************
173 ***********************************************************************/
175 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
178 event.type = vlc_ServicesDiscoveryItemRemoved;
179 event.u.services_discovery_item_removed.p_item = p_item;
181 vlc_event_send( &p_sd->event_manager, &event );
185 * Playlist - Services discovery bridge
188 /* A new item has been added to a certain sd */
189 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
191 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
192 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
193 playlist_item_t *p_new_item, * p_parent = user_data;
194 playlist_t * p_playlist = p_parent->p_playlist;
196 msg_Dbg( p_playlist, "Adding %s in %s",
197 p_input->psz_name ? p_input->psz_name : "(null)",
198 psz_cat ? psz_cat : "(null)" );
201 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
202 if( !EMPTY_STR(psz_cat) &&
203 p_parent->p_parent == p_playlist->p_root_category )
206 playlist_item_t * p_cat;
207 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
210 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
212 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
217 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
218 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
221 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
222 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
227 /* A new item has been removed from a certain sd */
228 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
230 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
231 playlist_item_t * p_parent = user_data;
232 playlist_item_t * p_pl_item;
234 /* First make sure that if item is a node it will be deleted.
235 * XXX: Why don't we have a function to ensure that in the playlist code ? */
236 vlc_object_lock( p_parent->p_playlist );
237 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
238 p_input->i_id, p_parent, false );
240 if( p_pl_item && p_pl_item->i_children > -1 )
242 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
243 vlc_object_unlock( p_parent->p_playlist );
247 /* Delete the non-node item normally */
248 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
249 p_parent, pl_Locked );
251 vlc_object_unlock( p_parent->p_playlist );
254 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
256 const char *psz_parser = psz_modules ?: "";
257 int retval = VLC_SUCCESS;
261 struct playlist_services_discovery_support_t * p_sds;
262 playlist_item_t * p_cat;
263 playlist_item_t * p_one;
265 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
268 if( *psz_parser == '\0' )
271 const char *psz_next = strchr( psz_parser, ':' );
272 if( psz_next == NULL )
273 psz_next = psz_parser + strlen( psz_parser );
275 char psz_plugin[psz_next - psz_parser + 1];
276 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
277 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
278 psz_parser = psz_next;
280 /* Perform the addition */
281 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
282 services_discovery_t *p_sd;
284 p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
288 char * psz = services_discovery_GetLocalizedName( p_sd );
291 playlist_NodesPairCreate( p_playlist, psz,
292 &p_cat, &p_one, false );
296 vlc_event_attach( services_discovery_EventManager( p_sd ),
297 vlc_ServicesDiscoveryItemAdded,
298 playlist_sd_item_added,
301 vlc_event_attach( services_discovery_EventManager( p_sd ),
302 vlc_ServicesDiscoveryItemAdded,
303 playlist_sd_item_added,
306 vlc_event_attach( services_discovery_EventManager( p_sd ),
307 vlc_ServicesDiscoveryItemRemoved,
308 playlist_sd_item_removed,
311 vlc_event_attach( services_discovery_EventManager( p_sd ),
312 vlc_ServicesDiscoveryItemRemoved,
313 playlist_sd_item_removed,
316 /* Free in playlist_ServicesDiscoveryRemove */
317 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
321 /* We want tree-view for service directory */
322 p_one->p_input->b_prefers_tree = true;
324 p_sds->p_one = p_one;
325 p_sds->p_cat = p_cat;
328 TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
335 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
336 const char *psz_module )
338 struct playlist_services_discovery_support_t * p_sds = NULL;
342 for( i = 0 ; i< p_playlist->i_sds ; i ++ )
344 if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
346 p_sds = p_playlist->pp_sds[i];
347 REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
353 if( !p_sds || !p_sds->p_sd )
355 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
359 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
360 vlc_ServicesDiscoveryItemAdded,
361 playlist_sd_item_added,
364 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
365 vlc_ServicesDiscoveryItemAdded,
366 playlist_sd_item_added,
369 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
370 vlc_ServicesDiscoveryItemRemoved,
371 playlist_sd_item_removed,
374 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
375 vlc_ServicesDiscoveryItemRemoved,
376 playlist_sd_item_removed,
379 /* Remove the sd playlist node if it exists */
381 if( p_sds->p_cat != p_playlist->p_root_category &&
382 p_sds->p_one != p_playlist->p_root_onelevel )
384 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
385 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
389 services_discovery_Destroy( p_sds->p_sd );
395 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
396 const char *psz_module )
401 for( i = 0 ; i< p_playlist->i_sds ; i ++ )
403 if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
413 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
415 while( p_playlist->i_sds > 0 )
416 playlist_ServicesDiscoveryRemove( p_playlist,
417 p_playlist->pp_sds[0]->p_sd->psz_module );