2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
38 #include "libmpcodecs/img_format.h"
39 #include "libmpcodecs/mp_image.h"
42 #include "menu_list.h"
43 #include "input/input.h"
44 #include "osdep/keycodes.h"
46 #define MENU_KEEP_PATH "/tmp/mp_current_path"
49 char *menu_chroot
= NULL
;
58 char* dir
; // current dir
68 static struct menu_priv_s cfg_dflt
= {
80 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
82 static const m_option_t cfg_fields
[] = {
83 MENU_LIST_PRIV_FIELDS
,
84 { "path", ST_OFF(path
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
85 { "title", ST_OFF(title
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
86 { "file-action", ST_OFF(file_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
87 { "dir-action", ST_OFF(dir_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
88 { "actions", ST_OFF(actions
), CONF_TYPE_STRING_LIST
, 0, 0, 0, NULL
},
89 { "filter", ST_OFF(filter
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
90 { NULL
, NULL
, NULL
, 0,0,0,NULL
}
93 #define mpriv (menu->priv)
95 static void free_entry(list_entry_t
* entry
) {
100 static char* replace_path(char* title
, char* dir
, int escape
) {
101 char *p
= strstr(title
,"%p");
103 int tl
= strlen(title
);
104 int dl
= strlen(dir
);
107 char *r
, *n
, *d
= dir
;
113 else if (*d
== '\'') /* ' -> \'\\\'\' */
124 else if (*dir
== '\'') { /* ' -> \'\\\'\' */
125 *n
++ = '\\'; *n
++ = '\'';
126 *n
++ = '\\'; *n
++ = '\\';
127 *n
++ = '\\'; *n
++ = '\'';
131 } while ((*n
++ = *dir
++));
139 typedef int (*kill_warn
)(const void*, const void*);
141 static int mylstat(char *dir
, char *file
,struct stat
* st
) {
142 int l
= strlen(dir
) + strlen(file
);
144 if (!strcmp("..", file
)) {
149 if (s
[l
] == '/' || s
[l
] == '\\')
154 slash
= strrchr(s
, '/');
157 slash
= strrchr(s
,'\\');
164 sprintf(s
,"%s/%s",dir
,file
);
168 static int compare(char **a
, char **b
){
169 if((*a
)[strlen(*a
) - 1] == '/') {
170 if((*b
)[strlen(*b
) - 1] == '/')
171 return strcmp(*b
, *a
) ;
175 if((*b
)[strlen(*b
) - 1] == '/')
178 return strcmp(*b
, *a
);
182 static char **get_extensions(menu_t
*menu
){
183 char **extensions
, ext
[32];
190 fp
= fopen(mpriv
->filter
, "r");
194 extensions
= malloc(sizeof(*extensions
));
197 while(fgets(ext
,sizeof(ext
),fp
)) {
199 int s
= strlen (ext
);
201 if(ext
[s
-1] == '\n') {
206 extensions
= realloc(extensions
, ++n
* sizeof(*extensions
));
207 extensions
= realloc(extensions
, ++n
* sizeof(*extensions
));
209 for (l
=extensions
; *l
; l
++);
218 static void free_extensions(char **extensions
){
220 char **l
= extensions
;
227 static int open_dir(menu_t
* menu
,char* args
) {
228 char **namelist
, **tp
;
236 extern int file_filter
;
237 char **extensions
, **elem
, *ext
;
239 menu_list_init(menu
);
243 mpriv
->dir
= strdup(args
);
244 if(mpriv
->p
.title
&& mpriv
->p
.title
!= mpriv
->title
&& mpriv
->p
.title
!= cfg_dflt
.p
.title
)
245 free(mpriv
->p
.title
);
246 p
= strstr(mpriv
->title
,"%p");
248 mpriv
->p
.title
= replace_path(mpriv
->title
,mpriv
->dir
,0);
250 if ((dirp
= opendir (mpriv
->dir
)) == NULL
){
251 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] opendir error: %s\n", strerror(errno
));
256 path_fp
= open (MENU_KEEP_PATH
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
258 write (path_fp
, mpriv
->dir
, strlen (mpriv
->dir
));
263 namelist
= malloc(sizeof(char *));
264 extensions
= get_extensions(menu
);
267 while ((dp
= readdir(dirp
)) != NULL
) {
268 if(dp
->d_name
[0] == '.' && strcmp(dp
->d_name
,"..") != 0)
270 if (menu_chroot
&& !strcmp (dp
->d_name
,"..")) {
271 size_t len
= strlen (menu_chroot
);
272 if ((strlen (mpriv
->dir
) == len
|| strlen (mpriv
->dir
) == len
+ 1)
273 && !strncmp (mpriv
->dir
, menu_chroot
, len
))
276 if (mylstat(args
,dp
->d_name
,&st
))
278 if (file_filter
&& extensions
&& !S_ISDIR(st
.st_mode
)) {
279 if((ext
= strrchr(dp
->d_name
,'.')) == NULL
)
284 if (!strcasecmp(ext
, *elem
))
290 if(n
%20 == 0){ // Get some more mem
291 if((tp
= realloc(namelist
, (n
+20) * sizeof (char *)))
293 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] realloc error: %s\n", strerror(errno
));
300 namelist
[n
] = malloc(strlen(dp
->d_name
) + 2);
301 if(namelist
[n
] == NULL
){
302 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] memory allocation error: %s\n", strerror(errno
));
307 strcpy(namelist
[n
], dp
->d_name
);
308 if(S_ISDIR(st
.st_mode
))
309 strcat(namelist
[n
], "/");
314 free_extensions (extensions
);
317 qsort(namelist
, n
, sizeof(char *), (kill_warn
)compare
);
320 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] readdir error: %s\n",strerror(errno
));
324 if((e
= calloc(1,sizeof(list_entry_t
))) != NULL
){
326 e
->p
.txt
= strdup(namelist
[n
]);
327 if(strchr(namelist
[n
], '/') != NULL
)
329 menu_list_add_entry(menu
,e
);
331 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] memory allocation error: %s\n", strerror(errno
));
342 static void read_cmd(menu_t
* menu
,int cmd
) {
345 mpriv
->p
.current
= mpriv
->p
.menu
; // Hack : we consider that the first entry is ../
349 if(mpriv
->p
.current
->d
&& !mpriv
->dir_action
) {
350 // Default action : open this dirctory ourself
351 int l
= strlen(mpriv
->dir
);
352 char *slash
= NULL
, *p
= NULL
;
353 if(strcmp(mpriv
->p
.current
->p
.txt
,"../") == 0) {
355 mpriv
->dir
[l
-1] = '\0';
356 slash
= strrchr(mpriv
->dir
,'/');
359 slash
= strrchr(mpriv
->dir
,'\\');
363 p
= strdup(mpriv
->dir
);
365 p
= malloc(l
+ strlen(mpriv
->p
.current
->p
.txt
) + 1);
366 sprintf(p
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
368 menu_list_uninit(menu
,free_entry
);
369 if(!open_dir(menu
,p
)) {
370 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] Can't open directory %s.\n",p
);
374 } else { // File and directory dealt with action string.
375 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
376 char filename
[fname_len
];
378 char *action
= mpriv
->p
.current
->d
? mpriv
->dir_action
:mpriv
->file_action
;
379 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
380 str
= replace_path(action
, filename
,1);
381 mp_input_parse_and_queue_cmds(menu
->input_ctx
, str
);
386 case MENU_CMD_ACTION
: {
387 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
388 char filename
[fname_len
];
390 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
391 str
= replace_path(action
, filename
,1);
392 mp_input_parse_and_queue_cmds(menu
->input_ctx
, str
);
397 menu_list_read_cmd(menu
,cmd
);
401 static int read_key(menu_t
* menu
,int c
){
403 for (str
=mpriv
->actions
; str
&& *str
; str
++)
404 if (c
== (*str
)[0]) {
406 read_cmd(menu
,MENU_CMD_ACTION
);
409 if (menu_dflt_read_key(menu
, c
))
411 return menu_list_jump_to_key(menu
, c
);
414 static void clos(menu_t
* menu
) {
415 menu_list_uninit(menu
,free_entry
);
419 static int open_fs(menu_t
* menu
, char* args
) {
420 char *path
= mpriv
->path
;
422 char wd
[PATH_MAX
+1], b
[PATH_MAX
+1];
423 args
= NULL
; // Warning kill
425 menu
->draw
= menu_list_draw
;
426 menu
->read_cmd
= read_cmd
;
427 menu
->read_key
= read_key
;
431 if (!path
|| path
[0] == '\0') {
435 path_fp
= open (MENU_KEEP_PATH
, O_RDONLY
);
437 if (!fstat (path_fp
, &st
) && (st
.st_size
> 0)) {
438 path
= malloc(st
.st_size
+1);
439 path
[st
.st_size
] = '\0';
440 if (!((read(path_fp
, path
, st
.st_size
) == st
.st_size
) && path
[0] == '/'
441 && !stat(path
, &st
) && S_ISDIR(st
.st_mode
))) {
452 if (!path
|| path
[0] == '\0') {
455 if (filename
&& !strstr(filename
, "://") && (path
=realpath(filename
, b
))) {
456 slash
= strrchr(path
, '/');
458 // FIXME: Do we need and can convert all '\\' in path to '/' on win32?
460 slash
= strrchr(path
, '\\');
469 if (path
[0] != '/') {
470 if(path
[strlen(path
)-1] != '/')
471 snprintf(b
,sizeof(b
),"%s/%s/",wd
,path
);
473 snprintf(b
,sizeof(b
),"%s/%s",wd
,path
);
475 } else if (path
[strlen(path
)-1]!='/') {
476 sprintf(b
,"%s/",path
);
479 if (menu_chroot
&& menu_chroot
[0] == '/') {
480 int l
= strlen(menu_chroot
);
481 if (l
> 0 && menu_chroot
[l
-1] == '/')
483 if (strncmp(menu_chroot
, path
, l
) || (path
[l
] != '\0' && path
[l
] != '/')) {
484 if (menu_chroot
[l
] == '/')
487 sprintf(b
,"%s/",menu_chroot
);
492 r
= open_dir(menu
,path
);
497 const menu_info_t menu_info_filesel
= {
504 sizeof(struct menu_priv_s
),