]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
remove useless if not harmful hack
[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 "playlist_internal.h"
32 #include "../libvlc.h"
33
34 /*
35  * Services discovery
36  * Basically you just listen to Service discovery event through the
37  * sd's event manager.
38  * That's how the playlist get's Service Discovery information
39  */
40
41 /***********************************************************************
42  * GetServicesNames
43  ***********************************************************************/
44 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
45                                                char ***pppsz_longnames )
46 {
47     return module_GetModulesNamesForCapability( p_super, "services_discovery",
48                                                 pppsz_longnames );
49 }
50
51 /***********************************************************************
52  * Create
53  ***********************************************************************/
54 services_discovery_t *
55 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
56 {
57     services_discovery_t *p_sd;
58     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
59                               "services discovery" );
60     if( !p_sd )
61         return NULL;
62
63     p_sd->psz_localized_name = 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     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
76
77     if( p_sd->p_module == NULL )
78     {
79         msg_Err( p_super, "no suitable services discovery module" );
80         vlc_object_release( p_sd );
81         return NULL;
82     }
83     p_sd->psz_module = strdup( psz_module_name );
84
85     vlc_object_attach( p_sd, p_super );
86
87     vlc_event_t event = {
88         .type = vlc_ServicesDiscoveryStarted
89     };
90     vlc_event_send( &p_sd->event_manager, &event );
91     return p_sd;
92 }
93
94 static void ObjectKillChildrens( vlc_object_t *p_obj )
95 {
96     vlc_list_t *p_list;
97     int i;
98     vlc_object_kill( p_obj );
99
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 );
104 }
105
106 /***********************************************************************
107  * Destroy
108  ***********************************************************************/
109 void services_discovery_Destroy ( services_discovery_t * p_sd )
110 {
111     vlc_event_t event = {
112         .type = vlc_ServicesDiscoveryEnded
113     };
114
115     ObjectKillChildrens( VLC_OBJECT(p_sd) );
116
117     vlc_event_send( &p_sd->event_manager, &event );
118     module_Unneed( p_sd, p_sd->p_module );
119
120     vlc_event_manager_fini( &p_sd->event_manager );
121
122     free( p_sd->psz_module );
123     free( p_sd->psz_localized_name );
124
125     vlc_object_release( p_sd );
126 }
127
128 /***********************************************************************
129  * GetLocalizedName
130  ***********************************************************************/
131 char *
132 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
133 {
134     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
135 }
136
137 /***********************************************************************
138  * SetLocalizedName
139  ***********************************************************************/
140 void
141 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
142 {
143     free( p_sd->psz_localized_name );
144     p_sd->psz_localized_name = strdup(psz);
145 }
146
147 /***********************************************************************
148  * EventManager
149  ***********************************************************************/
150 vlc_event_manager_t *
151 services_discovery_EventManager ( services_discovery_t * p_sd )
152 {
153     return &p_sd->event_manager;
154 }
155
156 /***********************************************************************
157  * AddItem
158  ***********************************************************************/
159 void
160 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
161                              const char * psz_category )
162 {
163     vlc_event_t event;
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;
167
168     vlc_event_send( &p_sd->event_manager, &event );
169 }
170
171 /***********************************************************************
172  * RemoveItem
173  ***********************************************************************/
174 void
175 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
176 {
177     vlc_event_t event;
178     event.type = vlc_ServicesDiscoveryItemRemoved;
179     event.u.services_discovery_item_removed.p_item = p_item;
180
181     vlc_event_send( &p_sd->event_manager, &event );
182 }
183
184 /*
185  * Playlist - Services discovery bridge
186  */
187
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 )
190 {
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;
195
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)" );
199
200     PL_LOCK;
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 )
204     {
205         /* */
206         playlist_item_t * p_cat;
207         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
208         if( !p_cat )
209         {
210             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
211                                          p_parent, 0, NULL );
212             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
213         }
214         p_parent = p_cat;
215     }
216
217     p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
218                                         PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
219     if( p_new_item )
220     {
221         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
222         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
223     }
224     PL_UNLOCK;
225 }
226
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 )
229 {
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;
233
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 );
239
240     if( p_pl_item && p_pl_item->i_children > -1 )
241     {
242         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
243         vlc_object_unlock( p_parent->p_playlist );
244         return;
245     }
246
247     /* Delete the non-node item normally */
248     playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
249                                       p_parent, pl_Locked );
250
251     vlc_object_unlock( p_parent->p_playlist );
252 }
253
254 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
255 {
256     const char *psz_parser = psz_modules ?: "";
257     int retval = VLC_SUCCESS;
258
259     for (;;)
260     {
261         struct playlist_services_discovery_support_t * p_sds;
262         playlist_item_t * p_cat;
263         playlist_item_t * p_one;
264
265         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
266             psz_parser++;
267
268         if( *psz_parser == '\0' )
269             break;
270
271         const char *psz_next = strchr( psz_parser, ':' );
272         if( psz_next == NULL )
273             psz_next = psz_parser + strlen( psz_parser );
274
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;
279
280         /* Perform the addition */
281         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
282         services_discovery_t *p_sd;
283
284         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
285         if( !p_sd )
286             continue;
287
288         char * psz = services_discovery_GetLocalizedName( p_sd );
289         assert( psz );
290         PL_LOCK;
291         playlist_NodesPairCreate( p_playlist, psz,
292                 &p_cat, &p_one, false );
293         PL_UNLOCK;
294         free( psz );
295
296         vlc_event_attach( services_discovery_EventManager( p_sd ),
297                           vlc_ServicesDiscoveryItemAdded,
298                           playlist_sd_item_added,
299                           p_one );
300
301         vlc_event_attach( services_discovery_EventManager( p_sd ),
302                           vlc_ServicesDiscoveryItemAdded,
303                           playlist_sd_item_added,
304                           p_cat );
305
306         vlc_event_attach( services_discovery_EventManager( p_sd ),
307                           vlc_ServicesDiscoveryItemRemoved,
308                           playlist_sd_item_removed,
309                           p_one );
310
311         vlc_event_attach( services_discovery_EventManager( p_sd ),
312                           vlc_ServicesDiscoveryItemRemoved,
313                           playlist_sd_item_removed,
314                           p_cat );
315
316         /* Free in playlist_ServicesDiscoveryRemove */
317         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
318         if( !p_sds )
319             return VLC_ENOMEM;
320
321         /* We want tree-view for service directory */
322         p_one->p_input->b_prefers_tree = true;
323         p_sds->p_sd = p_sd;
324         p_sds->p_one = p_one;
325         p_sds->p_cat = p_cat;
326
327         PL_LOCK;
328         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
329         PL_UNLOCK;
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< p_playlist->i_sds ; i ++ )
343     {
344         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
345         {
346             p_sds = p_playlist->pp_sds[i];
347             REMOVE_ELEM( p_playlist->pp_sds, 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     services_discovery_Destroy( 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< p_playlist->i_sds ; i ++ )
402     {
403         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_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( p_playlist->i_sds > 0 )
416         playlist_ServicesDiscoveryRemove( p_playlist,
417                                      p_playlist->pp_sds[0]->p_sd->psz_module );
418 }
419