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"
38 * Basically you just listen to Service discovery event through the
40 * That's how the playlist get's Service Discovery information
43 /***********************************************************************
45 ***********************************************************************/
46 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
47 char ***pppsz_longnames )
49 return module_GetModulesNamesForCapability( "services_discovery",
53 /***********************************************************************
55 ***********************************************************************/
56 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
58 services_discovery_t *p_sd;
60 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
61 "services discovery" );
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 vlc_object_attach( p_sd, p_super );
80 /***********************************************************************
82 ***********************************************************************/
83 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
85 assert(!p_sd->p_module);
87 p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
89 if( p_sd->p_module == NULL )
91 msg_Err( p_sd, "no suitable services discovery module" );
96 .type = vlc_ServicesDiscoveryStarted
98 vlc_event_send( &p_sd->event_manager, &event );
102 /***********************************************************************
104 ***********************************************************************/
105 void vlc_sd_Stop ( services_discovery_t * p_sd )
107 vlc_event_t event = {
108 .type = vlc_ServicesDiscoveryEnded
111 vlc_event_send( &p_sd->event_manager, &event );
113 module_unneed( p_sd, p_sd->p_module );
114 p_sd->p_module = NULL;
117 /***********************************************************************
119 ***********************************************************************/
121 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
123 return strdup( module_get_name( p_sd->p_module, true ) );
126 /***********************************************************************
128 ***********************************************************************/
129 vlc_event_manager_t *
130 services_discovery_EventManager ( services_discovery_t * p_sd )
132 return &p_sd->event_manager;
135 /***********************************************************************
137 ***********************************************************************/
139 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
140 const char * psz_category )
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;
147 vlc_event_send( &p_sd->event_manager, &event );
150 /***********************************************************************
152 ***********************************************************************/
154 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
157 event.type = vlc_ServicesDiscoveryItemRemoved;
158 event.u.services_discovery_item_removed.p_item = p_item;
160 vlc_event_send( &p_sd->event_manager, &event );
164 * Playlist - Services discovery bridge
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 )
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;
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)" );
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 )
185 playlist_item_t * p_cat;
186 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
189 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
191 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
196 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
197 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
200 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
201 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
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 )
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;
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 );
219 if( p_pl_item && p_pl_item->i_children > -1 )
221 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
222 vlc_object_unlock( p_parent->p_playlist );
226 /* Delete the non-node item normally */
227 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
228 p_parent, pl_Locked );
230 vlc_object_unlock( p_parent->p_playlist );
233 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
235 const char *psz_parser = psz_modules ?: "";
236 int retval = VLC_SUCCESS;
240 struct playlist_services_discovery_support_t * p_sds;
241 playlist_item_t * p_cat;
242 playlist_item_t * p_one;
244 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
247 if( *psz_parser == '\0' )
250 const char *psz_next = strchr( psz_parser, ':' );
251 if( psz_next == NULL )
252 psz_next = psz_parser + strlen( psz_parser );
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;
259 /* Perform the addition */
260 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
261 services_discovery_t *p_sd;
263 p_sd = vlc_sd_Create( (vlc_object_t*)p_playlist );
267 vlc_event_attach( services_discovery_EventManager( p_sd ),
268 vlc_ServicesDiscoveryItemAdded,
269 playlist_sd_item_added,
272 vlc_event_attach( services_discovery_EventManager( p_sd ),
273 vlc_ServicesDiscoveryItemAdded,
274 playlist_sd_item_added,
277 vlc_event_attach( services_discovery_EventManager( p_sd ),
278 vlc_ServicesDiscoveryItemRemoved,
279 playlist_sd_item_removed,
282 vlc_event_attach( services_discovery_EventManager( p_sd ),
283 vlc_ServicesDiscoveryItemRemoved,
284 playlist_sd_item_removed,
287 if( !vlc_sd_Start( p_sd, psz_plugin ) )
289 vlc_sd_Destroy( p_sd );
293 char *psz = services_discovery_GetLocalizedName( p_sd );
296 playlist_NodesPairCreate( p_playlist, psz,
297 &p_cat, &p_one, false );
301 /* Free in playlist_ServicesDiscoveryRemove */
302 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
306 /* We want tree-view for service directory */
307 p_one->p_input->b_prefers_tree = true;
309 p_sds->p_one = p_one;
310 p_sds->p_cat = p_cat;
313 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_module )
324 struct playlist_services_discovery_support_t * p_sds = NULL;
328 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
330 if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
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 );
341 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
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 );
386 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
387 const char *psz_module )
392 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
394 if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
404 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
406 while( pl_priv(p_playlist)->i_sds > 0 )
407 playlist_ServicesDiscoveryRemove( p_playlist,
408 module_get_object( pl_priv(p_playlist)->pp_sds[0]->p_sd->p_module ) );