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 sprintf(s
,"%s/%s",dir
,file
);
124 static int compare(char **a
, char **b
){
125 if((*a
)[strlen(*a
) - 1] == '/') {
126 if((*b
)[strlen(*b
) - 1] == '/')
127 return strcmp(*b
, *a
) ;
131 if((*b
)[strlen(*b
) - 1] == '/')
134 return strcmp(*b
, *a
);
138 static char **get_extensions(menu_t
*menu
){
139 char **extensions
, ext
[32];
146 fp
= fopen(mpriv
->filter
, "r");
150 extensions
= (char **) malloc(sizeof(*extensions
));
153 while(fgets(ext
,sizeof(ext
),fp
)) {
155 int s
= strlen (ext
);
157 if(ext
[s
-1] == '\n') {
161 e
= (char *) malloc(s
+1);
162 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
163 extensions
= (char **) realloc(extensions
, ++n
* sizeof(*extensions
));
165 for (l
=extensions
; *l
; l
++);
174 static void free_extensions(char **extensions
){
176 char **l
= extensions
;
183 static int open_dir(menu_t
* menu
,char* args
) {
184 char **namelist
, **tp
;
192 extern int file_filter
;
193 char **extensions
, **elem
, *ext
;
195 menu_list_init(menu
);
199 mpriv
->dir
= strdup(args
);
200 if(mpriv
->p
.title
&& mpriv
->p
.title
!= mpriv
->title
&& mpriv
->p
.title
!= cfg_dflt
.p
.title
)
201 free(mpriv
->p
.title
);
202 p
= strstr(mpriv
->title
,"%p");
204 mpriv
->p
.title
= replace_path(mpriv
->title
,mpriv
->dir
);
206 if ((dirp
= opendir (mpriv
->dir
)) == NULL
){
207 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_OpendirError
, strerror(errno
));
212 path_fp
= open (MENU_KEEP_PATH
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
214 write (path_fp
, mpriv
->dir
, strlen (mpriv
->dir
));
219 namelist
= (char **) malloc(sizeof(char *));
220 extensions
= get_extensions(menu
);
223 while ((dp
= readdir(dirp
)) != NULL
) {
224 if(dp
->d_name
[0] == '.' && strcmp(dp
->d_name
,"..") != 0)
226 if (menu_chroot
&& !strcmp (dp
->d_name
,"..")) {
227 int len
= strlen (menu_chroot
);
228 if ((strlen (mpriv
->dir
) == len
|| strlen (mpriv
->dir
) == len
+ 1)
229 && !strncmp (mpriv
->dir
, menu_chroot
, len
))
232 mylstat(args
,dp
->d_name
,&st
);
233 if (file_filter
&& extensions
&& !S_ISDIR(st
.st_mode
)) {
234 if((ext
= strrchr(dp
->d_name
,'.')) == NULL
)
239 if (!strcasecmp(ext
, *elem
))
245 if(n
%20 == 0){ // Get some more mem
246 if((tp
= (char **) realloc(namelist
, (n
+20) * sizeof (char *)))
248 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_ReallocError
, strerror(errno
));
255 namelist
[n
] = (char *) malloc(strlen(dp
->d_name
) + 2);
256 if(namelist
[n
] == NULL
){
257 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_MallocError
, strerror(errno
));
262 strcpy(namelist
[n
], dp
->d_name
);
263 if(S_ISDIR(st
.st_mode
))
264 strcat(namelist
[n
], "/");
269 free_extensions (extensions
);
272 qsort(namelist
, n
, sizeof(char *), (kill_warn
)compare
);
275 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_ReaddirError
,strerror(errno
));
279 if((e
= calloc(1,sizeof(list_entry_t
))) != NULL
){
281 e
->p
.txt
= strdup(namelist
[n
]);
282 if(strchr(namelist
[n
], '/') != NULL
)
284 menu_list_add_entry(menu
,e
);
286 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_MallocError
, strerror(errno
));
298 static void read_cmd(menu_t
* menu
,int cmd
) {
302 mpriv
->p
.current
= mpriv
->p
.menu
; // Hack : we consider that the first entry is ../
306 if(mpriv
->p
.current
->d
) {
307 if(mpriv
->dir_action
) {
308 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
309 char filename
[fname_len
];
311 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
312 str
= replace_path(mpriv
->dir_action
,filename
);
313 c
= mp_input_parse_cmd(str
);
314 if(str
!= mpriv
->dir_action
)
316 } else { // Default action : open this dirctory ourself
317 int l
= strlen(mpriv
->dir
);
318 char *slash
= NULL
, *p
= NULL
;
319 if(strcmp(mpriv
->p
.current
->p
.txt
,"../") == 0) {
321 mpriv
->dir
[l
-1] = '\0';
322 slash
= strrchr(mpriv
->dir
,'/');
323 #if defined(__MINGW32__) || defined(__CYGWIN__)
325 slash
= strrchr(mpriv
->dir
,'\\');
329 p
= strdup(mpriv
->dir
);
331 p
= malloc(l
+ strlen(mpriv
->p
.current
->p
.txt
) + 1);
332 sprintf(p
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
334 menu_list_uninit(menu
,free_entry
);
335 if(!open_dir(menu
,p
)) {
336 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_LIBMENU_CantOpenDirectory
,p
);
342 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
343 char filename
[fname_len
];
345 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
346 str
= replace_path(mpriv
->file_action
,filename
);
347 c
= mp_input_parse_cmd(str
);
348 if(str
!= mpriv
->file_action
)
352 mp_input_queue_cmd(c
);
353 if(mpriv
->auto_close
)
357 case MENU_CMD_ACTION
: {
358 int fname_len
= strlen(mpriv
->dir
) + strlen(mpriv
->p
.current
->p
.txt
) + 1;
359 char filename
[fname_len
];
361 sprintf(filename
,"%s%s",mpriv
->dir
,mpriv
->p
.current
->p
.txt
);
362 str
= replace_path(action
, filename
);
363 mp_input_queue_cmd(mp_input_parse_cmd(str
));
368 menu_list_read_cmd(menu
,cmd
);
372 static void read_key(menu_t
* menu
,int c
){
374 read_cmd(menu
,MENU_CMD_LEFT
);
377 for (str
=mpriv
->actions
; str
&& *str
; str
++)
378 if (c
== (*str
)[0]) {
380 read_cmd(menu
,MENU_CMD_ACTION
);
384 menu_list_read_key(menu
,c
,1);
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
;
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] != '\0'){
415 path
[st
.st_size
] = '\0';
428 if(!path
|| path
[0] == '\0') {
429 int l
= strlen(wd
) + 2;
432 r
= open_dir(menu
,b
);
433 } else if(path
[0] != '/') {
434 int al
= strlen(path
);
435 int l
= strlen(wd
) + al
+ 3;
438 sprintf(b
,"%s/%s/",wd
,path
);
440 sprintf(b
,"%s/%s",wd
,path
);
441 r
= open_dir(menu
,b
);
443 r
= open_dir(menu
,path
);
451 const menu_info_t menu_info_filesel
= {
458 sizeof(struct menu_priv_s
),