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
;
42 char* dir
; // current dir
53 static struct menu_priv_s cfg_dflt
= {
66 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
68 static m_option_t cfg_fields
[] = {
69 MENU_LIST_PRIV_FIELDS
,
70 { "path", ST_OFF(path
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
71 { "title", ST_OFF(title
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
72 { "file-action", ST_OFF(file_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
73 { "dir-action", ST_OFF(dir_action
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
74 { "auto-close", ST_OFF(auto_close
), CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
75 { "actions", ST_OFF(actions
), CONF_TYPE_STRING_LIST
, 0, 0, 0, NULL
},
76 { "filter", ST_OFF(filter
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
77 { NULL
, NULL
, NULL
, 0,0,0,NULL
}
80 #define mpriv (menu->priv)
82 static void free_entry(list_entry_t
* entry
) {
87 static char* replace_path(char* title
, char* dir
) {
88 char *p
= strstr(title
,"%p");
90 int tl
= strlen(title
);
94 char *r
, *n
, *d
= dir
;
98 if (*d
== '\\' || *d
== term
)
105 if (*dir
== '\\' || *dir
== term
)
107 } while ((*n
++ = *dir
++));
115 typedef int (*kill_warn
)(const void*, const void*);
117 static int mylstat(char *dir
, char *file
,struct stat
* st
) {
118 int l
= strlen(dir
) + strlen(file
);
120 if (!strcmp("..", file
)) {
124 #if defined(__MINGW32__) || defined(__CYGWIN__)
125 if (s
[l
] == '/' || s
[l
] == '\\')
130 slash
= strrchr(s
, '/');
131 #if defined(__MINGW32__) || defined(__CYGWIN__)
133 slash
= strrchr(s
,'\\');
140 sprintf(s
,"%s/%s",dir
,file
);
144 static int compare(char **a
, char **b
){
145 if((*a
)[strlen(*a
) - 1] == '/') {
146 if((*b
)[strlen(*b
) - 1] == '/')
147 return strcmp(*b
, *a
) ;
151 if((*b
)[strlen(*b
) - 1] == '/')
154 return strcmp(*b
, *a
);
158 static char **get_extensions(menu_t
*menu
){
159 char **extensions
, ext
[32];
166 fp
= fopen(mpriv
->filter
, "r");
170 extensions
= (char **) malloc(sizeof(*extensions
));
173 while(fgets(ext
,sizeof(ext
),fp
)) {
175 int s
= strlen (ext
);
177 if(ext
[s
-1] == '\n') {
181 e
= (char *) malloc(s
+1);
182 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
183 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
185 for (l
=extensions
; *l
; l
++);
194 static void free_extensions(char **extensions
){
196 char **l
= extensions
;
203 static int open_dir(menu_t
* menu
,char* args
) {
204 char **namelist
, **tp
;
212 extern int file_filter
;
213 char **extensions
, **elem
, *ext
;
215 menu_list_init(menu
);
219 mpriv
->dir
= strdup(args
);
220 if(mpriv
->p
.title
&& mpriv
->p
.title
!= mpriv
->title
&& mpriv
->p
.title
!= cfg_dflt
.p
.title
)
221 free(mpriv
->p
.title
);
222 p
= strstr(mpriv
->title
,"%p");
224 mpriv
->p
.title
= replace_path(mpriv
->title
,mpriv
->dir
);
226 if ((dirp
= opendir (mpriv
->dir
)) == NULL
){
227 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_OpendirError
, strerror(errno
));
232 path_fp
= open (MENU_KEEP_PATH
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
234 write (path_fp
, mpriv
->dir
, strlen (mpriv
->dir
));
239 namelist
= (char **) malloc(sizeof(char *));
240 extensions
= get_extensions(menu
);
243 while ((dp
= readdir(dirp
)) != NULL
) {
244 if(dp
->d_name
[0] == '.' && strcmp(dp
->d_name
,"..") != 0)
246 if (menu_chroot
&& !strcmp (dp
->d_name
,"..")) {
247 int len
= strlen (menu_chroot
);
248 if ((strlen (mpriv
->dir
) == len
|| strlen (mpriv
->dir
) == len
+ 1)
249 && !strncmp (mpriv
->dir
, menu_chroot
, len
))
252 if (mylstat(args
,dp
->d_name
,&st
))
254 if (file_filter
&& extensions
&& !S_ISDIR(st
.st_mode
)) {
255 if((ext
= strrchr(dp
->d_name
,'.')) == NULL
)
260 if (!strcasecmp(ext
, *elem
))
266 if(n
%20 == 0){ // Get some more mem
267 if((tp
= (char **) realloc(namelist
, (n
+20) * sizeof (char *)))
269 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_ReallocError
, strerror(errno
));
276 namelist
[n
] = (char *) malloc(strlen(dp
->d_name
) + 2);
277 if(namelist
[n
] == NULL
){
278 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_MallocError
, strerror(errno
));
283 strcpy(namelist
[n
], dp
->d_name
);
284 if(S_ISDIR(st
.st_mode
))
285 strcat(namelist
[n
], "/");
290 free_extensions (extensions
);
293 qsort(namelist
, n
, sizeof(char *), (kill_warn
)compare
);
296 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_ReaddirError
,strerror(errno
));
300 if((e
= calloc(1,sizeof(list_entry_t
))) != NULL
){
302 e
->p
.txt
= strdup(namelist
[n
]);
303 if(strchr(namelist
[n
], '/') != NULL
)
305 menu_list_add_entry(menu
,e
);
307 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_MallocError
, strerror(errno
));
318 static void read_cmd(menu_t
* menu
,int cmd
) {
321 mpriv
->p
.current
= mpriv
->p
.menu
; // Hack : we consider that the first entry is ../
325 if(mpriv
->p
.current
->d
&& !mpriv
->dir_action
) {
326 // Default action : open this dirctory ourself
327 int l
= strlen(mpriv
->dir
);
328 char *slash
= NULL
, *p
= NULL
;
329 if(strcmp(mpriv
->p
.current
->p
.txt
,"../") == 0) {
331 mpriv
->dir
[l
-1] = '\0';
332 slash
= strrchr(mpriv
->dir
,'/');
333 #if defined(__MINGW32__) || defined(__CYGWIN__)
335 slash
= strrchr(mpriv
->dir
,'\\');
339 p
= strdup(mpriv
->dir
);
341 p
= malloc(l
+ strlen(mpriv
->p
.current
->p
.txt
) + 1);
342 sprintf(p
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
344 menu_list_uninit(menu
,free_entry
);
345 if(!open_dir(menu
,p
)) {
346 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_CantOpenDirectory
,p
);
350 } else { // File and directory dealt with action string.
351 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
352 char filename
[fname_len
];
354 char *action
= mpriv
->p
.current
->d
? mpriv
->dir_action
:mpriv
->file_action
;
355 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
356 str
= replace_path(action
, filename
);
357 if (mp_input_parse_and_queue_cmds(str
)) {
358 if(mpriv
->auto_close
)
365 case MENU_CMD_ACTION
: {
366 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
367 char filename
[fname_len
];
369 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
370 str
= replace_path(action
, filename
);
371 mp_input_parse_and_queue_cmds(str
);
376 menu_list_read_cmd(menu
,cmd
);
380 static int read_key(menu_t
* menu
,int c
){
382 for (str
=mpriv
->actions
; str
&& *str
; str
++)
383 if (c
== (*str
)[0]) {
385 read_cmd(menu
,MENU_CMD_ACTION
);
388 if (menu_dflt_read_key(menu
, c
))
390 return menu_list_jump_to_key(menu
, c
);
393 static void clos(menu_t
* menu
) {
394 menu_list_uninit(menu
,free_entry
);
398 static int open_fs(menu_t
* menu
, char* args
) {
399 char *path
= mpriv
->path
, *freepath
= NULL
;
401 char wd
[PATH_MAX
+1], b
[PATH_MAX
+1];
402 args
= NULL
; // Warning kill
404 menu
->draw
= menu_list_draw
;
405 menu
->read_cmd
= read_cmd
;
406 menu
->read_key
= read_key
;
410 if (!path
|| path
[0] == '\0') {
414 path_fp
= open (MENU_KEEP_PATH
, O_RDONLY
);
416 if (!fstat (path_fp
, &st
) && (st
.st_size
> 0)) {
417 path
= malloc(st
.st_size
+1);
418 if ((read(path_fp
, path
, st
.st_size
) == st
.st_size
) && path
[0] == '/'
419 && !stat(path
, &st
) && S_ISDIR(st
.st_mode
)){
421 path
[st
.st_size
] = '\0';
434 if (!path
|| path
[0] == '\0')
436 if (path
[0] != '/') {
437 if(path
[strlen(path
)-1] != '/')
438 snprintf(b
,sizeof(b
),"%s/%s/",wd
,path
);
440 snprintf(b
,sizeof(b
),"%s/%s",wd
,path
);
442 } else if (path
[strlen(path
)-1]!='/') {
443 sprintf(b
,"%s/",path
);
446 if (menu_chroot
&& menu_chroot
[0] == '/') {
447 int l
= strlen(menu_chroot
);
448 if (l
> 0 && menu_chroot
[l
-1] == '/')
450 if (strncmp(menu_chroot
, path
, l
) || (path
[l
] != '\0' && path
[l
] != '/')) {
451 if (menu_chroot
[l
] == '/')
454 sprintf(b
,"%s/",menu_chroot
);
459 r
= open_dir(menu
,path
);
467 const menu_info_t menu_info_filesel
= {
474 sizeof(struct menu_priv_s
),