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>
39 #include "libmpcodecs/img_format.h"
40 #include "libmpcodecs/mp_image.h"
43 #include "menu_list.h"
44 #include "input/input.h"
45 #include "osdep/keycodes.h"
47 #define MENU_KEEP_PATH "/tmp/mp_current_path"
50 char *menu_chroot
= NULL
;
59 char* dir
; // current dir
69 static struct menu_priv_s cfg_dflt
= {
81 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
83 static m_option_t cfg_fields
[] = {
84 MENU_LIST_PRIV_FIELDS
,
85 { "path", ST_OFF(path
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
86 { "title", ST_OFF(title
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
87 { "file-action", ST_OFF(file_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
88 { "dir-action", ST_OFF(dir_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
89 { "actions", ST_OFF(actions
), CONF_TYPE_STRING_LIST
, 0, 0, 0, NULL
},
90 { "filter", ST_OFF(filter
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
91 { NULL
, NULL
, NULL
, 0,0,0,NULL
}
94 #define mpriv (menu->priv)
96 static void free_entry(list_entry_t
* entry
) {
101 static char* replace_path(char* title
, char* dir
, int escape
) {
102 char *p
= strstr(title
,"%p");
104 int tl
= strlen(title
);
105 int dl
= strlen(dir
);
108 char *r
, *n
, *d
= dir
;
114 else if (*d
== '\'') /* ' -> \'\\\'\' */
125 else if (*dir
== '\'') { /* ' -> \'\\\'\' */
126 *n
++ = '\\'; *n
++ = '\'';
127 *n
++ = '\\'; *n
++ = '\\';
128 *n
++ = '\\'; *n
++ = '\'';
132 } while ((*n
++ = *dir
++));
140 typedef int (*kill_warn
)(const void*, const void*);
142 static int mylstat(char *dir
, char *file
,struct stat
* st
) {
143 int l
= strlen(dir
) + strlen(file
);
145 if (!strcmp("..", file
)) {
150 if (s
[l
] == '/' || s
[l
] == '\\')
155 slash
= strrchr(s
, '/');
158 slash
= strrchr(s
,'\\');
165 sprintf(s
,"%s/%s",dir
,file
);
169 static int compare(char **a
, char **b
){
170 if((*a
)[strlen(*a
) - 1] == '/') {
171 if((*b
)[strlen(*b
) - 1] == '/')
172 return strcmp(*b
, *a
) ;
176 if((*b
)[strlen(*b
) - 1] == '/')
179 return strcmp(*b
, *a
);
183 static char **get_extensions(menu_t
*menu
){
184 char **extensions
, ext
[32];
191 fp
= fopen(mpriv
->filter
, "r");
195 extensions
= (char **) malloc(sizeof(*extensions
));
198 while(fgets(ext
,sizeof(ext
),fp
)) {
200 int s
= strlen (ext
);
202 if(ext
[s
-1] == '\n') {
206 e
= (char *) malloc(s
+1);
207 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
208 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
210 for (l
=extensions
; *l
; l
++);
219 static void free_extensions(char **extensions
){
221 char **l
= extensions
;
228 static int open_dir(menu_t
* menu
,char* args
) {
229 char **namelist
, **tp
;
237 extern int file_filter
;
238 char **extensions
, **elem
, *ext
;
240 menu_list_init(menu
);
244 mpriv
->dir
= strdup(args
);
245 if(mpriv
->p
.title
&& mpriv
->p
.title
!= mpriv
->title
&& mpriv
->p
.title
!= cfg_dflt
.p
.title
)
246 free(mpriv
->p
.title
);
247 p
= strstr(mpriv
->title
,"%p");
249 mpriv
->p
.title
= replace_path(mpriv
->title
,mpriv
->dir
,0);
251 if ((dirp
= opendir (mpriv
->dir
)) == NULL
){
252 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] opendir error: %s\n", strerror(errno
));
257 path_fp
= open (MENU_KEEP_PATH
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
259 write (path_fp
, mpriv
->dir
, strlen (mpriv
->dir
));
264 namelist
= (char **) malloc(sizeof(char *));
265 extensions
= get_extensions(menu
);
268 while ((dp
= readdir(dirp
)) != NULL
) {
269 if(dp
->d_name
[0] == '.' && strcmp(dp
->d_name
,"..") != 0)
271 if (menu_chroot
&& !strcmp (dp
->d_name
,"..")) {
272 size_t len
= strlen (menu_chroot
);
273 if ((strlen (mpriv
->dir
) == len
|| strlen (mpriv
->dir
) == len
+ 1)
274 && !strncmp (mpriv
->dir
, menu_chroot
, len
))
277 if (mylstat(args
,dp
->d_name
,&st
))
279 if (file_filter
&& extensions
&& !S_ISDIR(st
.st_mode
)) {
280 if((ext
= strrchr(dp
->d_name
,'.')) == NULL
)
285 if (!strcasecmp(ext
, *elem
))
291 if(n
%20 == 0){ // Get some more mem
292 if((tp
= (char **) realloc(namelist
, (n
+20) * sizeof (char *)))
294 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] realloc error: %s\n", strerror(errno
));
301 namelist
[n
] = (char *) malloc(strlen(dp
->d_name
) + 2);
302 if(namelist
[n
] == NULL
){
303 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] memory allocation error: %s\n", strerror(errno
));
308 strcpy(namelist
[n
], dp
->d_name
);
309 if(S_ISDIR(st
.st_mode
))
310 strcat(namelist
[n
], "/");
315 free_extensions (extensions
);
318 qsort(namelist
, n
, sizeof(char *), (kill_warn
)compare
);
321 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] readdir error: %s\n",strerror(errno
));
325 if((e
= calloc(1,sizeof(list_entry_t
))) != NULL
){
327 e
->p
.txt
= strdup(namelist
[n
]);
328 if(strchr(namelist
[n
], '/') != NULL
)
330 menu_list_add_entry(menu
,e
);
332 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] memory allocation error: %s\n", strerror(errno
));
343 static void read_cmd(menu_t
* menu
,int cmd
) {
346 mpriv
->p
.current
= mpriv
->p
.menu
; // Hack : we consider that the first entry is ../
350 if(mpriv
->p
.current
->d
&& !mpriv
->dir_action
) {
351 // Default action : open this dirctory ourself
352 int l
= strlen(mpriv
->dir
);
353 char *slash
= NULL
, *p
= NULL
;
354 if(strcmp(mpriv
->p
.current
->p
.txt
,"../") == 0) {
356 mpriv
->dir
[l
-1] = '\0';
357 slash
= strrchr(mpriv
->dir
,'/');
360 slash
= strrchr(mpriv
->dir
,'\\');
364 p
= strdup(mpriv
->dir
);
366 p
= malloc(l
+ strlen(mpriv
->p
.current
->p
.txt
) + 1);
367 sprintf(p
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
369 menu_list_uninit(menu
,free_entry
);
370 if(!open_dir(menu
,p
)) {
371 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[MENU] Can't open directory %s.\n",p
);
375 } else { // File and directory dealt with action string.
376 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
377 char filename
[fname_len
];
379 char *action
= mpriv
->p
.current
->d
? mpriv
->dir_action
:mpriv
->file_action
;
380 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
381 str
= replace_path(action
, filename
,1);
382 mp_input_parse_and_queue_cmds(menu
->input_ctx
, str
);
387 case MENU_CMD_ACTION
: {
388 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
389 char filename
[fname_len
];
391 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
392 str
= replace_path(action
, filename
,1);
393 mp_input_parse_and_queue_cmds(menu
->input_ctx
, str
);
398 menu_list_read_cmd(menu
,cmd
);
402 static int read_key(menu_t
* menu
,int c
){
404 for (str
=mpriv
->actions
; str
&& *str
; str
++)
405 if (c
== (*str
)[0]) {
407 read_cmd(menu
,MENU_CMD_ACTION
);
410 if (menu_dflt_read_key(menu
, c
))
412 return menu_list_jump_to_key(menu
, c
);
415 static void clos(menu_t
* menu
) {
416 menu_list_uninit(menu
,free_entry
);
420 static int open_fs(menu_t
* menu
, char* args
) {
421 char *path
= mpriv
->path
;
423 char wd
[PATH_MAX
+1], b
[PATH_MAX
+1];
424 args
= NULL
; // Warning kill
426 menu
->draw
= menu_list_draw
;
427 menu
->read_cmd
= read_cmd
;
428 menu
->read_key
= read_key
;
432 if (!path
|| path
[0] == '\0') {
436 path_fp
= open (MENU_KEEP_PATH
, O_RDONLY
);
438 if (!fstat (path_fp
, &st
) && (st
.st_size
> 0)) {
439 path
= malloc(st
.st_size
+1);
440 path
[st
.st_size
] = '\0';
441 if (!((read(path_fp
, path
, st
.st_size
) == st
.st_size
) && path
[0] == '/'
442 && !stat(path
, &st
) && S_ISDIR(st
.st_mode
))) {
453 if (!path
|| path
[0] == '\0') {
456 if (filename
&& !strstr(filename
, "://") && (path
=realpath(filename
, b
))) {
457 slash
= strrchr(path
, '/');
459 // FIXME: Do we need and can convert all '\\' in path to '/' on win32?
461 slash
= strrchr(path
, '\\');
470 if (path
[0] != '/') {
471 if(path
[strlen(path
)-1] != '/')
472 snprintf(b
,sizeof(b
),"%s/%s/",wd
,path
);
474 snprintf(b
,sizeof(b
),"%s/%s",wd
,path
);
476 } else if (path
[strlen(path
)-1]!='/') {
477 sprintf(b
,"%s/",path
);
480 if (menu_chroot
&& menu_chroot
[0] == '/') {
481 int l
= strlen(menu_chroot
);
482 if (l
> 0 && menu_chroot
[l
-1] == '/')
484 if (strncmp(menu_chroot
, path
, l
) || (path
[l
] != '\0' && path
[l
] != '/')) {
485 if (menu_chroot
[l
] == '/')
488 sprintf(b
,"%s/",menu_chroot
);
493 r
= open_dir(menu
,path
);
498 const menu_info_t menu_info_filesel
= {
505 sizeof(struct menu_priv_s
),