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