]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
Remove suspicious and suspiciously dead code
[vlc] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
27
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"
34
35
36 /*
37  * Services discovery
38  * Basically you just listen to Service discovery event through the
39  * sd's event manager.
40  * That's how the playlist get's Service Discovery information
41  */
42
43 /***********************************************************************
44  * GetServicesNames
45  ***********************************************************************/
46 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
47                                                char ***pppsz_longnames )
48 {
49     return module_GetModulesNamesForCapability( "services_discovery",
50                                                 pppsz_longnames );
51 }
52
53 /***********************************************************************
54  * Create
55  ***********************************************************************/
56 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
57 {
58     services_discovery_t *p_sd;
59
60     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
61                               "services discovery" );
62     if( !p_sd )
63         return NULL;
64
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 );
74
75     vlc_object_attach( p_sd, p_super );
76
77     return p_sd;
78 }
79
80 static void ObjectKillChildrens( vlc_object_t *p_obj )
81 {
82     vlc_list_t *p_list;
83     int i;
84     vlc_object_kill( p_obj );
85
86     p_list = vlc_list_children( p_obj );
87     for( i = 0; i < p_list->i_count; i++ )
88         ObjectKillChildrens( p_list->p_values[i].p_object );
89     vlc_list_release( p_list );
90 }
91
92 /***********************************************************************
93  * Stop
94  ***********************************************************************/
95 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
96 {
97     assert(!p_sd->p_module);
98
99     p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
100
101     if( p_sd->p_module == NULL )
102     {
103         msg_Err( p_sd, "no suitable services discovery module" );
104         return false;
105     }
106         
107     vlc_event_t event = {
108         .type = vlc_ServicesDiscoveryStarted
109     };
110     vlc_event_send( &p_sd->event_manager, &event );
111     return true;
112 }
113                           
114 /***********************************************************************
115  * Stop
116  ***********************************************************************/
117 void vlc_sd_Stop ( services_discovery_t * p_sd )
118 {
119     vlc_event_t event = {
120         .type = vlc_ServicesDiscoveryEnded
121     };
122     
123     ObjectKillChildrens( VLC_OBJECT(p_sd) );
124     
125     vlc_event_send( &p_sd->event_manager, &event );
126
127     module_unneed( p_sd, p_sd->p_module );
128     p_sd->p_module = NULL;
129 }
130
131 /***********************************************************************
132  * GetLocalizedName
133  ***********************************************************************/
134 char *
135 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
136 {
137     return strdup( module_get_name( p_sd->p_module, true ) );
138 }
139
140 /***********************************************************************
141  * EventManager
142  ***********************************************************************/
143 vlc_event_manager_t *
144 services_discovery_EventManager ( services_discovery_t * p_sd )
145 {
146     return &p_sd->event_manager;
147 }
148
149 /***********************************************************************
150  * AddItem
151  ***********************************************************************/
152 void
153 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
154                              const char * psz_category )
155 {
156     vlc_event_t event;
157     event.type = vlc_ServicesDiscoveryItemAdded;
158     event.u.services_discovery_item_added.p_new_item = p_item;
159     event.u.services_discovery_item_added.psz_category = psz_category;
160
161     vlc_event_send( &p_sd->event_manager, &event );
162 }
163
164 /***********************************************************************
165  * RemoveItem
166  ***********************************************************************/
167 void
168 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
169 {
170     vlc_event_t event;
171     event.type = vlc_ServicesDiscoveryItemRemoved;
172     event.u.services_discovery_item_removed.p_item = p_item;
173
174     vlc_event_send( &p_sd->event_manager, &event );
175 }
176
177 /*
178  * Playlist - Services discovery bridge
179  */
180
181  /* A new item has been added to a certain sd */
182 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
183 {
184     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
185     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
186     playlist_item_t *p_new_item, * p_parent = user_data;
187     playlist_t * p_playlist = p_parent->p_playlist;
188
189     msg_Dbg( p_playlist, "Adding %s in %s",
190                 p_input->psz_name ? p_input->psz_name : "(null)",
191                 psz_cat ? psz_cat : "(null)" );
192
193     PL_LOCK;
194     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
195     if( !EMPTY_STR(psz_cat) &&
196         p_parent->p_parent == p_playlist->p_root_category )
197     {
198         /* */
199         playlist_item_t * p_cat;
200         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
201         if( !p_cat )
202         {
203             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
204                                          p_parent, 0, NULL );
205             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
206         }
207         p_parent = p_cat;
208     }
209
210     p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
211                                         PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
212     if( p_new_item )
213     {
214         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
215         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
216     }
217     PL_UNLOCK;
218 }
219
220  /* A new item has been removed from a certain sd */
221 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
222 {
223     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
224     playlist_item_t * p_parent = user_data;
225     playlist_item_t * p_pl_item;
226
227     /* First make sure that if item is a node it will be deleted.
228      * XXX: Why don't we have a function to ensure that in the playlist code ? */
229     vlc_object_lock( p_parent->p_playlist );
230     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
231             p_input->i_id, p_parent, false );
232
233     if( p_pl_item && p_pl_item->i_children > -1 )
234     {
235         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
236         vlc_object_unlock( p_parent->p_playlist );
237         return;
238     }
239
240     /* Delete the non-node item normally */
241     playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
242                                       p_parent, pl_Locked );
243
244     vlc_object_unlock( p_parent->p_playlist );
245 }
246
247 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
248 {
249     const char *psz_parser = psz_modules ?: "";
250     int retval = VLC_SUCCESS;
251
252     for (;;)
253     {
254         struct playlist_services_discovery_support_t * p_sds;
255         playlist_item_t * p_cat;
256         playlist_item_t * p_one;
257
258         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
259             psz_parser++;
260
261         if( *psz_parser == '\0' )
262             break;
263
264         const char *psz_next = strchr( psz_parser, ':' );
265         if( psz_next == NULL )
266             psz_next = psz_parser + strlen( psz_parser );
267
268         char psz_plugin[psz_next - psz_parser + 1];
269         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
270         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
271         psz_parser = psz_next;
272
273         /* Perform the addition */
274         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
275         services_discovery_t *p_sd;
276
277         p_sd = vlc_sd_Create( (vlc_object_t*)p_playlist );
278         if( !p_sd )
279             continue;
280
281         vlc_event_attach( services_discovery_EventManager( p_sd ),
282                           vlc_ServicesDiscoveryItemAdded,
283                           playlist_sd_item_added,
284                           p_one );
285         
286         vlc_event_attach( services_discovery_EventManager( p_sd ),
287                           vlc_ServicesDiscoveryItemAdded,
288                           playlist_sd_item_added,
289                           p_cat );
290
291         vlc_event_attach( services_discovery_EventManager( p_sd ),
292                           vlc_ServicesDiscoveryItemRemoved,
293                           playlist_sd_item_removed,
294                           p_one );
295
296         vlc_event_attach( services_discovery_EventManager( p_sd ),
297                           vlc_ServicesDiscoveryItemRemoved,
298                           playlist_sd_item_removed,
299                           p_cat );
300
301         if( !vlc_sd_Start( p_sd, psz_plugin ) )
302         {
303             vlc_sd_Destroy( p_sd );
304             return VLC_EGENERIC;
305         }        
306
307         char *psz = services_discovery_GetLocalizedName( p_sd );
308         assert( psz );
309         PL_LOCK;
310         playlist_NodesPairCreate( p_playlist, psz,
311                 &p_cat, &p_one, false );
312         PL_UNLOCK;
313         free( psz );
314
315         /* Free in playlist_ServicesDiscoveryRemove */
316         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
317         if( !p_sds )
318             return VLC_ENOMEM;
319
320         /* We want tree-view for service directory */
321         p_one->p_input->b_prefers_tree = true;
322         p_sds->p_sd = p_sd;
323         p_sds->p_one = p_one;
324         p_sds->p_cat = p_cat;
325
326         PL_LOCK;
327         TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
328         PL_UNLOCK;
329
330     }
331
332     return retval;
333 }
334
335 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
336                                       const char *psz_module )
337 {
338     struct playlist_services_discovery_support_t * p_sds = NULL;
339     int i;
340
341     PL_LOCK;
342     for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
343     {
344         if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
345         {
346             p_sds = pl_priv(p_playlist)->pp_sds[i];
347             REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
348             break;
349         }
350     }
351     PL_UNLOCK;
352
353     if( !p_sds || !p_sds->p_sd )
354     {
355         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
356         return VLC_EGENERIC;
357     }
358
359     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
360                         vlc_ServicesDiscoveryItemAdded,
361                         playlist_sd_item_added,
362                         p_sds->p_one );
363
364     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
365                         vlc_ServicesDiscoveryItemAdded,
366                         playlist_sd_item_added,
367                         p_sds->p_cat );
368
369     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
370                         vlc_ServicesDiscoveryItemRemoved,
371                         playlist_sd_item_removed,
372                         p_sds->p_one );
373
374     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
375                         vlc_ServicesDiscoveryItemRemoved,
376                         playlist_sd_item_removed,
377                         p_sds->p_cat );
378
379     /* Remove the sd playlist node if it exists */
380     PL_LOCK;
381     if( p_sds->p_cat != p_playlist->p_root_category &&
382         p_sds->p_one != p_playlist->p_root_onelevel )
383     {
384         playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
385         playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
386     }
387     PL_UNLOCK;
388
389     vlc_sd_StopAndDestroy( p_sds->p_sd );
390     free( p_sds );
391
392     return VLC_SUCCESS;
393 }
394
395 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
396                                               const char *psz_module )
397 {
398     int i;
399     PL_LOCK;
400
401     for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
402     {
403         if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
404         {
405             PL_UNLOCK;
406             return true;
407         }
408     }
409     PL_UNLOCK;
410     return false;
411 }
412
413 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
414 {
415     while( pl_priv(p_playlist)->i_sds > 0 )
416         playlist_ServicesDiscoveryRemove( p_playlist,
417                                           module_get_object( pl_priv(p_playlist)->pp_sds[0]->p_sd->p_module ) );
418 }