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"
35 static void services_discovery_Destructor ( services_discovery_t * p_sd );
39 * Basically you just listen to Service discovery event through the
41 * That's how the playlist get's Service Discovery information
44 /***********************************************************************
46 ***********************************************************************/
47 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
48 char ***pppsz_longnames )
50 return module_GetModulesNamesForCapability( p_super, "services_discovery",
54 /***********************************************************************
56 ***********************************************************************/
57 services_discovery_t *
58 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
60 services_discovery_t *p_sd;
61 assert( psz_module_name );
62 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
63 "services discovery" );
67 p_sd->psz_localized_name = strdup( "Unnamed service discovery" ); // FIXME: Set that back to NULL
68 p_sd->psz_module = strdup( psz_module_name );
70 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
71 vlc_event_manager_register_event_type( &p_sd->event_manager,
72 vlc_ServicesDiscoveryItemAdded );
73 vlc_event_manager_register_event_type( &p_sd->event_manager,
74 vlc_ServicesDiscoveryItemRemoved );
75 vlc_event_manager_register_event_type( &p_sd->event_manager,
76 vlc_ServicesDiscoveryStarted );
77 vlc_event_manager_register_event_type( &p_sd->event_manager,
78 vlc_ServicesDiscoveryEnded );
80 vlc_object_attach( p_sd, p_super );
85 static void ObjectKillChildrens( vlc_object_t *p_obj )
89 vlc_object_kill( p_obj );
91 p_list = vlc_list_children( p_obj );
92 for( i = 0; i < p_list->i_count; i++ )
93 ObjectKillChildrens( p_list->p_values[i].p_object );
94 vlc_list_release( p_list );
97 /***********************************************************************
99 ***********************************************************************/
100 bool services_discovery_Start ( services_discovery_t * p_sd )
102 assert(!p_sd->p_module);
104 p_sd->p_module = module_need( p_sd, "services_discovery", p_sd->psz_module, true );
106 if( p_sd->p_module == NULL )
108 msg_Err( p_sd, "no suitable services discovery module" );
112 vlc_event_t event = {
113 .type = vlc_ServicesDiscoveryStarted
115 vlc_event_send( &p_sd->event_manager, &event );
119 /***********************************************************************
121 ***********************************************************************/
122 void services_discovery_Stop ( services_discovery_t * p_sd )
124 vlc_event_t event = {
125 .type = vlc_ServicesDiscoveryEnded
128 ObjectKillChildrens( VLC_OBJECT(p_sd) );
130 vlc_event_send( &p_sd->event_manager, &event );
132 module_unneed( p_sd, p_sd->p_module );
133 p_sd->p_module = NULL;
136 /***********************************************************************
138 ***********************************************************************/
139 static void services_discovery_Destructor ( services_discovery_t * p_sd )
141 assert(!p_sd->p_module); /* Forgot to call Stop */
143 vlc_event_manager_fini( &p_sd->event_manager );
145 free( p_sd->psz_module );
146 free( p_sd->psz_localized_name );
148 vlc_object_release( p_sd );
151 /***********************************************************************
153 ***********************************************************************/
155 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
157 return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
160 /***********************************************************************
162 ***********************************************************************/
164 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
166 free( p_sd->psz_localized_name );
167 p_sd->psz_localized_name = strdup(psz);
170 /***********************************************************************
172 ***********************************************************************/
173 vlc_event_manager_t *
174 services_discovery_EventManager ( services_discovery_t * p_sd )
176 return &p_sd->event_manager;
179 /***********************************************************************
181 ***********************************************************************/
183 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
184 const char * psz_category )
187 event.type = vlc_ServicesDiscoveryItemAdded;
188 event.u.services_discovery_item_added.p_new_item = p_item;
189 event.u.services_discovery_item_added.psz_category = psz_category;
191 vlc_event_send( &p_sd->event_manager, &event );
194 /***********************************************************************
196 ***********************************************************************/
198 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
201 event.type = vlc_ServicesDiscoveryItemRemoved;
202 event.u.services_discovery_item_removed.p_item = p_item;
204 vlc_event_send( &p_sd->event_manager, &event );
208 * Playlist - Services discovery bridge
211 /* A new item has been added to a certain sd */
212 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
214 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
215 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
216 playlist_item_t *p_new_item, * p_parent = user_data;
217 playlist_t * p_playlist = p_parent->p_playlist;
219 msg_Dbg( p_playlist, "Adding %s in %s",
220 p_input->psz_name ? p_input->psz_name : "(null)",
221 psz_cat ? psz_cat : "(null)" );
224 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
225 if( !EMPTY_STR(psz_cat) &&
226 p_parent->p_parent == p_playlist->p_root_category )
229 playlist_item_t * p_cat;
230 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
233 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
235 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
240 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
241 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
244 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
245 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
250 /* A new item has been removed from a certain sd */
251 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
253 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
254 playlist_item_t * p_parent = user_data;
255 playlist_item_t * p_pl_item;
257 /* First make sure that if item is a node it will be deleted.
258 * XXX: Why don't we have a function to ensure that in the playlist code ? */
259 vlc_object_lock( p_parent->p_playlist );
260 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
261 p_input->i_id, p_parent, false );
263 if( p_pl_item && p_pl_item->i_children > -1 )
265 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
266 vlc_object_unlock( p_parent->p_playlist );
270 /* Delete the non-node item normally */
271 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
272 p_parent, pl_Locked );
274 vlc_object_unlock( p_parent->p_playlist );
277 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
279 const char *psz_parser = psz_modules ?: "";
280 int retval = VLC_SUCCESS;
284 struct playlist_services_discovery_support_t * p_sds;
285 playlist_item_t * p_cat;
286 playlist_item_t * p_one;
288 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
291 if( *psz_parser == '\0' )
294 const char *psz_next = strchr( psz_parser, ':' );
295 if( psz_next == NULL )
296 psz_next = psz_parser + strlen( psz_parser );
298 char psz_plugin[psz_next - psz_parser + 1];
299 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
300 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
301 psz_parser = psz_next;
303 /* Perform the addition */
304 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
305 services_discovery_t *p_sd;
307 p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
311 /* FIXME: Thanks to previous changeset this is broken */
312 char * psz = services_discovery_GetLocalizedName( p_sd );
315 playlist_NodesPairCreate( p_playlist, psz,
316 &p_cat, &p_one, false );
320 vlc_event_attach( services_discovery_EventManager( p_sd ),
321 vlc_ServicesDiscoveryItemAdded,
322 playlist_sd_item_added,
325 vlc_event_attach( services_discovery_EventManager( p_sd ),
326 vlc_ServicesDiscoveryItemAdded,
327 playlist_sd_item_added,
330 vlc_event_attach( services_discovery_EventManager( p_sd ),
331 vlc_ServicesDiscoveryItemRemoved,
332 playlist_sd_item_removed,
335 vlc_event_attach( services_discovery_EventManager( p_sd ),
336 vlc_ServicesDiscoveryItemRemoved,
337 playlist_sd_item_removed,
340 bool ret = services_discovery_Start( p_sd );
343 vlc_object_release( p_sd );
347 /* Free in playlist_ServicesDiscoveryRemove */
348 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
352 /* We want tree-view for service directory */
353 p_one->p_input->b_prefers_tree = true;
355 p_sds->p_one = p_one;
356 p_sds->p_cat = p_cat;
359 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
367 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
368 const char *psz_module )
370 struct playlist_services_discovery_support_t * p_sds = NULL;
374 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
376 if( !strcmp( psz_module, pl_priv(p_playlist)->pp_sds[i]->p_sd->psz_module ) )
378 p_sds = pl_priv(p_playlist)->pp_sds[i];
379 REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
385 if( !p_sds || !p_sds->p_sd )
387 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
391 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
392 vlc_ServicesDiscoveryItemAdded,
393 playlist_sd_item_added,
396 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
397 vlc_ServicesDiscoveryItemAdded,
398 playlist_sd_item_added,
401 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
402 vlc_ServicesDiscoveryItemRemoved,
403 playlist_sd_item_removed,
406 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
407 vlc_ServicesDiscoveryItemRemoved,
408 playlist_sd_item_removed,
411 /* Remove the sd playlist node if it exists */
413 if( p_sds->p_cat != p_playlist->p_root_category &&
414 p_sds->p_one != p_playlist->p_root_onelevel )
416 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
417 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
421 services_discovery_StopAndRelease( p_sds->p_sd );
427 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
428 const char *psz_module )
433 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
435 if( !strcmp( psz_module, pl_priv(p_playlist)->pp_sds[i]->p_sd->psz_module ) )
445 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
447 while( pl_priv(p_playlist)->i_sds > 0 )
448 playlist_ServicesDiscoveryRemove( p_playlist,
449 pl_priv(p_playlist)->pp_sds[0]->p_sd->psz_module );