]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* access_directory : only play if we are the current playlist item. Handle the case...
[vlc] / modules / access / directory.c
1 /*****************************************************************************
2  * directory.c: expands a directory (directory: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #   include <sys/types.h>
36 #endif
37 #ifdef HAVE_SYS_STAT_H
38 #   include <sys/stat.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #   include <errno.h>
42 #endif
43 #ifdef HAVE_FCNTL_H
44 #   include <fcntl.h>
45 #endif
46
47 #ifdef HAVE_UNISTD_H
48 #   include <unistd.h>
49 #elif defined( WIN32 ) && !defined( UNDER_CE )
50 #   include <io.h>
51 #endif
52
53 #if (!defined( WIN32 ) || defined(__MINGW32__))
54 /* Mingw has its own version of dirent */
55 #   include <dirent.h>
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 static int  DemuxOpen ( vlc_object_t * );
65
66 #define RECURSIVE_TEXT N_("Subdirectory behavior")
67 #define RECURSIVE_LONGTEXT N_( \
68         "Select whether subdirectories must be expanded.\n" \
69         "none: subdirectories do not appear in the playlist.\n" \
70         "collapse: subdirectories appear but are expanded on first play.\n" \
71         "expand: all subdirectories are expanded.\n" )
72
73 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
74 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
75                                            N_("expand") };
76
77 vlc_module_begin();
78     set_description( _("Standard filesystem directory input") );
79     set_capability( "access2", 55 );
80     add_shortcut( "directory" );
81     add_shortcut( "dir" );
82     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
83                 RECURSIVE_LONGTEXT, VLC_FALSE );
84       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
85     set_callbacks( Open, Close );
86
87     add_submodule();
88         set_description( "Directory EOF");
89         set_capability( "demux2", 0 );
90         add_shortcut( "directory" );
91         set_callbacks( DemuxOpen, NULL );
92 vlc_module_end();
93
94
95 /*****************************************************************************
96  * Local prototypes, constants, structures
97  *****************************************************************************/
98
99 #define MODE_EXPAND 0
100 #define MODE_COLLAPSE 1
101 #define MODE_NONE 2
102
103 static int Read( access_t *, uint8_t *, int );
104 static int ReadNull( access_t *, uint8_t *, int );
105 static int Control( access_t *, int, va_list );
106
107 static int Demux( demux_t *p_demux );
108 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
109
110
111 static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos,
112                     playlist_item_t * );
113
114 /*****************************************************************************
115  * Open: open the directory
116  *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
118 {
119     access_t *p_access = (access_t*)p_this;
120
121 #ifdef HAVE_SYS_STAT_H
122     struct stat stat_info;
123
124     if( ( stat( p_access->psz_path, &stat_info ) == -1 ) ||
125         !S_ISDIR( stat_info.st_mode ) )
126 #else
127     if( strcmp( p_access->psz_access, "dir") &&
128         strcmp( p_access->psz_access, "directory") )
129 #endif
130     {
131         return VLC_EGENERIC;
132     }
133
134     p_access->pf_read  = Read;
135     p_access->pf_block = NULL;
136     p_access->pf_seek  = NULL;
137     p_access->pf_control= Control;
138
139     /* Force a demux */
140     p_access->psz_demux = strdup( "directory" );
141
142     return VLC_SUCCESS;
143 }
144
145 /*****************************************************************************
146  * Close: close the target
147  *****************************************************************************/
148 static void Close( vlc_object_t * p_this )
149 {
150
151 }
152
153 /*****************************************************************************
154  * ReadNull: read the directory
155  *****************************************************************************/
156 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
157 {
158     /* Return fake data */
159     memset( p_buffer, 0, i_len );
160     return i_len;
161 }
162
163 /*****************************************************************************
164  * Read: read the directory
165  *****************************************************************************/
166 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
167 {
168     char *psz_name = NULL;
169     char *psz;
170     int  i_mode, i_pos;
171
172     playlist_item_t *p_item;
173     vlc_bool_t b_play = VLC_FALSE;
174
175     playlist_t *p_playlist =
176         (playlist_t *) vlc_object_find( p_access,
177                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
178
179     if( !p_playlist )
180     {
181         msg_Err( p_access, "can't find playlist" );
182         goto end;
183     }
184
185     /* Remove the ending '/' char */
186     psz_name = strdup( p_access->psz_path );
187     if( psz_name == NULL )
188         goto end;
189
190     if( (psz_name[strlen(psz_name)-1] == '/') ||
191         (psz_name[strlen(psz_name)-1] == '\\') )
192     {
193         psz_name[strlen(psz_name)-1] = '\0';
194     }
195
196     /* Initialize structure */
197     psz = var_CreateGetString( p_access, "recursive" );
198     if( *psz == '\0' || !strncmp( psz, "none" , 4 )  )
199     {
200         i_mode = MODE_NONE;
201     }
202     else if( !strncmp( psz, "collapse", 8 )  )
203     {
204         i_mode = MODE_COLLAPSE;
205     }
206     else
207     {
208         i_mode = MODE_EXPAND;
209     }
210     free( psz );
211
212     /* Make sure we are deleted when we are done */
213     /* The playlist position we will use for the add */
214     i_pos = p_playlist->i_index + 1;
215
216     msg_Dbg( p_access, "opening directory `%s'", psz_name );
217
218     if( &p_playlist->status.p_item->input ==
219         ((input_thread_t *)p_access->p_parent)->input.p_item )
220     {
221         p_item = p_playlist->status.p_item;
222         b_play = VLC_TRUE;
223         msg_Dbg( p_access, "starting directory playback");
224     }
225     else
226     {
227         input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
228                                                         input.p_item;
229         p_item = playlist_ItemGetByInput( p_playlist, p_current );
230         msg_Dbg( p_access, "not starting directory playback");
231         if( !p_item )
232         {
233             msg_Dbg( p_playlist, "unable to find item in playlist");
234             return -1;
235         }
236         b_play = VLC_FALSE;
237     }
238     p_item->input.i_type = ITEM_TYPE_DIRECTORY;
239     if( ReadDir( p_playlist, psz_name , i_mode, &i_pos,
240                  p_item ) != VLC_SUCCESS )
241     {
242     }
243 end:
244
245     /* Begin to read the directory */
246     if( b_play )
247     {
248         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
249                           p_playlist->status.i_view,
250                           p_playlist->status.p_item, NULL );
251     }
252     if( psz_name ) free( psz_name );
253     vlc_object_release( p_playlist );
254
255     /* Return fake data forever */
256     p_access->pf_read = ReadNull;
257     return ReadNull( p_access, p_buffer, i_len );
258 }
259
260 /*****************************************************************************
261  * DemuxOpen:
262  *****************************************************************************/
263 static int Control( access_t *p_access, int i_query, va_list args )
264 {
265     vlc_bool_t   *pb_bool;
266     int          *pi_int;
267     int64_t      *pi_64;
268
269     switch( i_query )
270     {
271         /* */
272         case ACCESS_CAN_SEEK:
273         case ACCESS_CAN_FASTSEEK:
274         case ACCESS_CAN_PAUSE:
275         case ACCESS_CAN_CONTROL_PACE:
276             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
277             *pb_bool = VLC_FALSE;    /* FIXME */
278             break;
279
280         /* */
281         case ACCESS_GET_MTU:
282             pi_int = (int*)va_arg( args, int * );
283             *pi_int = 0;
284             break;
285
286         case ACCESS_GET_PTS_DELAY:
287             pi_64 = (int64_t*)va_arg( args, int64_t * );
288             *pi_64 = DEFAULT_PTS_DELAY * 1000;
289             break;
290
291         /* */
292         case ACCESS_SET_PAUSE_STATE:
293         case ACCESS_GET_TITLE_INFO:
294         case ACCESS_SET_TITLE:
295         case ACCESS_SET_SEEKPOINT:
296         case ACCESS_SET_PRIVATE_ID_STATE:
297             return VLC_EGENERIC;
298
299         default:
300             msg_Warn( p_access, "unimplemented query in control" );
301             return VLC_EGENERIC;
302     }
303     return VLC_SUCCESS;
304 }
305
306 /*****************************************************************************
307  * DemuxOpen:
308  *****************************************************************************/
309 static int DemuxOpen ( vlc_object_t *p_this )
310 {
311     demux_t *p_demux = (demux_t*)p_this;
312
313     if( strcmp( p_demux->psz_demux, "directory" ) )
314         return VLC_EGENERIC;
315
316     p_demux->pf_demux   = Demux;
317     p_demux->pf_control = DemuxControl;
318     return VLC_SUCCESS;
319 }
320
321 /*****************************************************************************
322  * Demux: EOF
323  *****************************************************************************/
324 static int Demux( demux_t *p_demux )
325 {
326     return 0;
327 }
328 /*****************************************************************************
329  * DemuxControl:
330  *****************************************************************************/
331 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
332 {
333     return demux2_vaControlHelper( p_demux->s,
334                                    0, 0, 0, 1,
335                                    i_query, args );
336 }
337
338 /*****************************************************************************
339  * ReadDir: read a directory and add its content to the list
340  *****************************************************************************/
341 static int ReadDir( playlist_t *p_playlist,
342                     char *psz_name , int i_mode, int *pi_position,
343                     playlist_item_t *p_parent )
344 {
345     DIR *                       p_current_dir;
346     struct dirent *             p_dir_content;
347     playlist_item_t *p_node;
348
349    /* Change the item to a node */
350    if( p_parent->i_children == -1)
351    {
352         playlist_ItemToNode( p_playlist,p_parent );
353    }
354     /* Open the dir */
355     p_current_dir = opendir( psz_name );
356
357     if( p_current_dir == NULL )
358     {
359         /* something went bad, get out of here ! */
360 #   ifdef HAVE_ERRNO_H
361         msg_Warn( p_playlist, "cannot open directory `%s' (%s)",
362                   psz_name, strerror(errno));
363 #   else
364         msg_Warn( p_playlist, "cannot open directory `%s'", psz_name );
365 #   endif
366         return VLC_EGENERIC;
367     }
368
369     /* get the first directory entry */
370     p_dir_content = readdir( p_current_dir );
371
372     /* while we still have entries in the directory */
373     while( p_dir_content != NULL )
374     {
375         int i_size_entry = strlen( psz_name ) +
376                            strlen( p_dir_content->d_name ) + 2;
377         char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
378
379         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
380
381         /* if it starts with '.' then forget it */
382         if( p_dir_content->d_name[0] != '.' )
383         {
384 #if defined( S_ISDIR )
385             struct stat stat_data;
386             stat( psz_uri, &stat_data );
387             if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
388 #elif defined( DT_DIR )
389             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
390 #else
391             if( 0 )
392 #endif
393             {
394                 if( i_mode == MODE_NONE )
395                 {
396                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
397                     p_dir_content = readdir( p_current_dir );
398                     continue;
399                 }
400                 else if(i_mode == MODE_EXPAND )
401                 {
402                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
403                     p_node = playlist_NodeCreate( p_playlist,
404                                        p_parent->pp_parents[0]->i_view,
405                                        psz_uri, p_parent );
406
407                     playlist_CopyParents(  p_parent, p_node );
408
409                     p_node->input.i_type = ITEM_TYPE_DIRECTORY;
410
411                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
412                                  pi_position, p_node ) != VLC_SUCCESS )
413                     {
414                         return VLC_EGENERIC;
415                     }
416                 }
417             }
418             else
419             {
420                 playlist_item_t *p_item = playlist_ItemNew( p_playlist,
421                                 psz_uri, p_dir_content->d_name );
422                 playlist_NodeAddItem( p_playlist,p_item,
423                                       p_parent->pp_parents[0]->i_view,
424                                       p_parent,
425                                       PLAYLIST_APPEND, PLAYLIST_END );
426
427                 playlist_CopyParents( p_parent, p_item );
428             }
429         }
430         free( psz_uri );
431         p_dir_content = readdir( p_current_dir );
432     }
433     closedir( p_current_dir );
434     return VLC_SUCCESS;
435 }
436
437