22 #include "libmpcodecs/img_format.h"
23 #include "libmpcodecs/mp_image.h"
26 #include "menu_list.h"
27 #include "input/input.h"
28 #include "osdep/keycodes.h"
30 #define MENU_KEEP_PATH "/tmp/mp_current_path"
33 char *menu_chroot
= NULL
;
34 extern char *filename
;
43 char* dir
; // current dir
53 static struct menu_priv_s cfg_dflt
= {
65 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
67 static m_option_t cfg_fields
[] = {
68 MENU_LIST_PRIV_FIELDS
,
69 { "path", ST_OFF(path
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
70 { "title", ST_OFF(title
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
71 { "file-action", ST_OFF(file_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
72 { "dir-action", ST_OFF(dir_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
73 { "actions", ST_OFF(actions
), CONF_TYPE_STRING_LIST
, 0, 0, 0, NULL
},
74 { "filter", ST_OFF(filter
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
75 { NULL
, NULL
, NULL
, 0,0,0,NULL
}
78 #define mpriv (menu->priv)
80 static void free_entry(list_entry_t
* entry
) {
85 static char* replace_path(char* title
, char* dir
) {
86 char *p
= strstr(title
,"%p");
88 int tl
= strlen(title
);
92 char *r
, *n
, *d
= dir
;
96 if (*d
== '\\' || *d
== term
)
103 if (*dir
== '\\' || *dir
== term
)
105 } while ((*n
++ = *dir
++));
113 typedef int (*kill_warn
)(const void*, const void*);
115 static int mylstat(char *dir
, char *file
,struct stat
* st
) {
116 int l
= strlen(dir
) + strlen(file
);
118 if (!strcmp("..", file
)) {
122 #if defined(__MINGW32__) || defined(__CYGWIN__)
123 if (s
[l
] == '/' || s
[l
] == '\\')
128 slash
= strrchr(s
, '/');
129 #if defined(__MINGW32__) || defined(__CYGWIN__)
131 slash
= strrchr(s
,'\\');
138 sprintf(s
,"%s/%s",dir
,file
);
142 static int compare(char **a
, char **b
){
143 if((*a
)[strlen(*a
) - 1] == '/') {
144 if((*b
)[strlen(*b
) - 1] == '/')
145 return strcmp(*b
, *a
) ;
149 if((*b
)[strlen(*b
) - 1] == '/')
152 return strcmp(*b
, *a
);
156 static char **get_extensions(menu_t
*menu
){
157 char **extensions
, ext
[32];
164 fp
= fopen(mpriv
->filter
, "r");
168 extensions
= (char **) malloc(sizeof(*extensions
));
171 while(fgets(ext
,sizeof(ext
),fp
)) {
173 int s
= strlen (ext
);
175 if(ext
[s
-1] == '\n') {
179 e
= (char *) malloc(s
+1);
180 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
181 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
183 for (l
=extensions
; *l
; l
++);
192 static void free_extensions(char **extensions
){
194 char **l
= extensions
;
201 static int open_dir(menu_t
* menu
,char* args
) {
202 char **namelist
, **tp
;
210 extern int file_filter
;
211 char **extensions
, **elem
, *ext
;
213 menu_list_init(menu
);
217 mpriv
->dir
= strdup(args
);
218 if(mpriv
->p
.title
&& mpriv
->p
.title
!= mpriv
->title
&& mpriv
->p
.title
!= cfg_dflt
.p
.title
)
219 free(mpriv
->p
.title
);
220 p
= strstr(mpriv
->title
,"%p");
222 mpriv
->p
.title
= replace_path(mpriv
->title
,mpriv
->dir
);
224 if ((dirp
= opendir (mpriv
->dir
)) == NULL
){
225 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_OpendirError
, strerror(errno
));
230 path_fp
= open (MENU_KEEP_PATH
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
232 write (path_fp
, mpriv
->dir
, strlen (mpriv
->dir
));
237 namelist
= (char **) malloc(sizeof(char *));
238 extensions
= get_extensions(menu
);
241 while ((dp
= readdir(dirp
)) != NULL
) {
242 if(dp
->d_name
[0] == '.' && strcmp(dp
->d_name
,"..") != 0)
244 if (menu_chroot
&& !strcmp (dp
->d_name
,"..")) {
245 int len
= strlen (menu_chroot
);
246 if ((strlen (mpriv
->dir
) == len
|| strlen (mpriv
->dir
) == len
+ 1)
247 && !strncmp (mpriv
->dir
, menu_chroot
, len
))
250 if (mylstat(args
,dp
->d_name
,&st
))
252 if (file_filter
&& extensions
&& !S_ISDIR(st
.st_mode
)) {
253 if((ext
= strrchr(dp
->d_name
,'.')) == NULL
)
258 if (!strcasecmp(ext
, *elem
))
264 if(n
%20 == 0){ // Get some more mem
265 if((tp
= (char **) realloc(namelist
, (n
+20) * sizeof (char *)))
267 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_ReallocError
, strerror(errno
));
274 namelist
[n
] = (char *) malloc(strlen(dp
->d_name
) + 2);
275 if(namelist
[n
] == NULL
){
276 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_MallocError
, strerror(errno
));
281 strcpy(namelist
[n
], dp
->d_name
);
282 if(S_ISDIR(st
.st_mode
))
283 strcat(namelist
[n
], "/");
288 free_extensions (extensions
);
291 qsort(namelist
, n
, sizeof(char *), (kill_warn
)compare
);
294 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_ReaddirError
,strerror(errno
));
298 if((e
= calloc(1,sizeof(list_entry_t
))) != NULL
){
300 e
->p
.txt
= strdup(namelist
[n
]);
301 if(strchr(namelist
[n
], '/') != NULL
)
303 menu_list_add_entry(menu
,e
);
305 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_MallocError
, strerror(errno
));
316 static void read_cmd(menu_t
* menu
,int cmd
) {
319 mpriv
->p
.current
= mpriv
->p
.menu
; // Hack : we consider that the first entry is ../
323 if(mpriv
->p
.current
->d
&& !mpriv
->dir_action
) {
324 // Default action : open this dirctory ourself
325 int l
= strlen(mpriv
->dir
);
326 char *slash
= NULL
, *p
= NULL
;
327 if(strcmp(mpriv
->p
.current
->p
.txt
,"../") == 0) {
329 mpriv
->dir
[l
-1] = '\0';
330 slash
= strrchr(mpriv
->dir
,'/');
331 #if defined(__MINGW32__) || defined(__CYGWIN__)
333 slash
= strrchr(mpriv
->dir
,'\\');
337 p
= strdup(mpriv
->dir
);
339 p
= malloc(l
+ strlen(mpriv
->p
.current
->p
.txt
) + 1);
340 sprintf(p
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
342 menu_list_uninit(menu
,free_entry
);
343 if(!open_dir(menu
,p
)) {
344 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_CantOpenDirectory
,p
);
348 } else { // File and directory dealt with action string.
349 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
350 char filename
[fname_len
];
352 char *action
= mpriv
->p
.current
->d
? mpriv
->dir_action
:mpriv
->file_action
;
353 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
354 str
= replace_path(action
, filename
);
355 mp_input_parse_and_queue_cmds(str
);
360 case MENU_CMD_ACTION
: {
361 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
362 char filename
[fname_len
];
364 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
365 str
= replace_path(action
, filename
);
366 mp_input_parse_and_queue_cmds(str
);
371 menu_list_read_cmd(menu
,cmd
);
375 static int read_key(menu_t
* menu
,int c
){
377 for (str
=mpriv
->actions
; str
&& *str
; str
++)
378 if (c
== (*str
)[0]) {
380 read_cmd(menu
,MENU_CMD_ACTION
);
383 if (menu_dflt_read_key(menu
, c
))
385 return menu_list_jump_to_key(menu
, c
);
388 static void clos(menu_t
* menu
) {
389 menu_list_uninit(menu
,free_entry
);
393 static int open_fs(menu_t
* menu
, char* args
) {
394 char *path
= mpriv
->path
, *freepath
= NULL
;
396 char wd
[PATH_MAX
+1], b
[PATH_MAX
+1];
397 args
= NULL
; // Warning kill
399 menu
->draw
= menu_list_draw
;
400 menu
->read_cmd
= read_cmd
;
401 menu
->read_key
= read_key
;
405 if (!path
|| path
[0] == '\0') {
409 path_fp
= open (MENU_KEEP_PATH
, O_RDONLY
);
411 if (!fstat (path_fp
, &st
) && (st
.st_size
> 0)) {
412 path
= malloc(st
.st_size
+1);
413 if ((read(path_fp
, path
, st
.st_size
) == st
.st_size
) && path
[0] == '/'
414 && !stat(path
, &st
) && S_ISDIR(st
.st_mode
)){
416 path
[st
.st_size
] = '\0';
429 if (!path
|| path
[0] == '\0') {
431 if (filename
&& !strstr(filename
, "://") && (path
=realpath(filename
, b
))) {
432 slash
= strrchr(path
, '/');
433 #if defined(__MINGW32__) || defined(__CYGWIN__)
434 // FIXME: Do we need and can convert all '\\' in path to '/' on win32?
436 slash
= strrchr(path
, '\\');
444 if (path
[0] != '/') {
445 if(path
[strlen(path
)-1] != '/')
446 snprintf(b
,sizeof(b
),"%s/%s/",wd
,path
);
448 snprintf(b
,sizeof(b
),"%s/%s",wd
,path
);
450 } else if (path
[strlen(path
)-1]!='/') {
451 sprintf(b
,"%s/",path
);
454 if (menu_chroot
&& menu_chroot
[0] == '/') {
455 int l
= strlen(menu_chroot
);
456 if (l
> 0 && menu_chroot
[l
-1] == '/')
458 if (strncmp(menu_chroot
, path
, l
) || (path
[l
] != '\0' && path
[l
] != '/')) {
459 if (menu_chroot
[l
] == '/')
462 sprintf(b
,"%s/",menu_chroot
);
467 r
= open_dir(menu
,path
);
475 const menu_info_t menu_info_filesel
= {
482 sizeof(struct menu_priv_s
),