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