Mercurial > mplayer.hg
annotate libmenu/menu_filesel.c @ 23337:10a7279b8e56
get rid of -menu-utf8 and -menu-unicode once for all, patch by Guillaume Lecerf
author | ben |
---|---|
date | Sun, 20 May 2007 15:17:56 +0000 |
parents | ae2a2d5ca64b |
children | 9bf57f60bf0a |
rev | line source |
---|---|
8197 | 1 |
2 #include <stdlib.h> | |
3 #include <stdio.h> | |
4 #include <dirent.h> | |
5 #include <errno.h> | |
6 #include <string.h> | |
7 #include <sys/types.h> | |
8 #include <sys/stat.h> | |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
9 #include <fcntl.h> |
8197 | 10 #include <ctype.h> |
11 #include <unistd.h> | |
8291
abe95dde3223
limits.h required to get a PATH_MAX definition (on solaris)
jkeil
parents:
8226
diff
changeset
|
12 #include <limits.h> |
8197 | 13 |
14 | |
16862 | 15 #include "config.h" |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
16 #include "mp_msg.h" |
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
17 #include "help_mp.h" |
8197 | 18 |
16862 | 19 #include "m_struct.h" |
20 #include "m_option.h" | |
8197 | 21 |
19431
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
17994
diff
changeset
|
22 #include "libmpcodecs/img_format.h" |
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
17994
diff
changeset
|
23 #include "libmpcodecs/mp_image.h" |
8197 | 24 |
25 #include "menu.h" | |
26 #include "menu_list.h" | |
16862 | 27 #include "input/input.h" |
28 #include "osdep/keycodes.h" | |
8197 | 29 |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
30 #define MENU_KEEP_PATH "/tmp/mp_current_path" |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
31 |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
32 int menu_keepdir = 0; |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
33 |
8197 | 34 struct list_entry_s { |
35 struct list_entry p; | |
36 int d; | |
37 }; | |
38 | |
39 struct menu_priv_s { | |
40 menu_list_priv_t p; | |
41 char* dir; // current dir | |
42 /// Cfg fields | |
43 char* path; | |
44 char* title; | |
45 char* file_action; | |
46 char* dir_action; | |
47 int auto_close; | |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
48 char** actions; |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
49 char* filter; |
8197 | 50 }; |
51 | |
52 static struct menu_priv_s cfg_dflt = { | |
53 MENU_LIST_PRIV_DFLT, | |
54 NULL, | |
55 | |
56 NULL, | |
57 "Select a file: %p", | |
8226 | 58 "loadfile '%p'", |
8197 | 59 NULL, |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
60 0, |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
61 NULL, |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
62 NULL |
8197 | 63 }; |
64 | |
65 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m) | |
66 | |
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 { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL }, | |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
74 { "actions", ST_OFF(actions), CONF_TYPE_STRING_LIST, 0, 0, 0, NULL}, |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
75 { "filter", ST_OFF(filter), CONF_TYPE_STRING, 0, 0, 0, NULL}, |
8197 | 76 { NULL, NULL, NULL, 0,0,0,NULL } |
77 }; | |
78 | |
79 #define mpriv (menu->priv) | |
80 | |
81 static void free_entry(list_entry_t* entry) { | |
82 free(entry->p.txt); | |
83 free(entry); | |
84 } | |
85 | |
86 static char* replace_path(char* title , char* dir) { | |
87 char *p = strstr(title,"%p"); | |
88 if(p) { | |
89 int tl = strlen(title); | |
90 int dl = strlen(dir); | |
91 int t1l = p-title; | |
92 int l = tl - 2 + dl; | |
10624
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
93 char *r, *n, *d = dir; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
94 char term = *(p-1); |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
95 |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
96 do { |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
97 if (*d == '\\' || *d == term) |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
98 l++; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
99 } while (*d++); |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
100 r = malloc(l + 1); |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
101 n = r + t1l; |
8197 | 102 memcpy(r,title,t1l); |
10624
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
103 do { |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
104 if (*dir == '\\' || *dir == term) |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
105 *n++ = '\\'; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
106 } while ((*n++ = *dir++)); |
8197 | 107 if(tl - t1l - 2 > 0) |
10624
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
108 strcpy(n-1,p+2); |
8197 | 109 return r; |
110 } else | |
111 return title; | |
112 } | |
113 | |
114 typedef int (*kill_warn)(const void*, const void*); | |
115 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
116 static int mylstat(char *dir, char *file,struct stat* st) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
117 int l = strlen(dir) + strlen(file); |
8613 | 118 char s[l+2]; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
119 sprintf(s,"%s/%s",dir,file); |
9249 | 120 return stat(s,st); |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
121 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
122 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
123 static int compare(char **a, char **b){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
124 if((*a)[strlen(*a) - 1] == '/') { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
125 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
126 return strcmp(*b, *a) ; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
127 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
128 return 1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
129 } else { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
130 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
131 return -1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
132 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
133 return strcmp(*b, *a); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
134 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
135 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
136 |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
137 static char **get_extensions(menu_t *menu){ |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
138 char **extensions, ext[32]; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
139 FILE *fp; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
140 int n = 1; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
141 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
142 if (!mpriv->filter) |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
143 return NULL; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
144 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
145 fp = fopen(mpriv->filter, "r"); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
146 if(!fp) |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
147 return NULL; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
148 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
149 extensions = (char **) malloc(sizeof(*extensions)); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
150 *extensions = NULL; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
151 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
152 while(fgets(ext,sizeof(ext),fp)) { |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
153 char **l, *e; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
154 int s = strlen (ext); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
155 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
156 if(ext[s-1] == '\n') { |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
157 ext[s-1] = '\0'; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
158 s--; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
159 } |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
160 e = (char *) malloc(s+1); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
161 extensions = (char **) realloc(extensions, ++n * sizeof(*extensions)); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
162 extensions = (char **) realloc(extensions, ++n * sizeof(*extensions)); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
163 strcpy (e, ext); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
164 for (l=extensions; *l; l++); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
165 *l++ = e; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
166 *l = NULL; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
167 } |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
168 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
169 fclose (fp); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
170 return extensions; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
171 } |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
172 |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
173 static void free_extensions(char **extensions){ |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
174 if (extensions) { |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
175 char **l = extensions; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
176 while (*l) |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
177 free (*l++); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
178 free (extensions); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
179 } |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
180 } |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
181 |
8197 | 182 static int open_dir(menu_t* menu,char* args) { |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
183 char **namelist, **tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
184 struct dirent *dp; |
8197 | 185 struct stat st; |
186 int n; | |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
187 int path_fp; |
8197 | 188 char* p = NULL; |
189 list_entry_t* e; | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
190 DIR* dirp; |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
191 extern int file_filter; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
192 char **extensions, **elem, *ext; |
8197 | 193 |
194 menu_list_init(menu); | |
195 | |
196 if(mpriv->dir) | |
197 free(mpriv->dir); | |
198 mpriv->dir = strdup(args); | |
199 if(mpriv->p.title && mpriv->p.title != mpriv->title && mpriv->p.title != cfg_dflt.p.title) | |
200 free(mpriv->p.title); | |
201 p = strstr(mpriv->title,"%p"); | |
202 | |
203 mpriv->p.title = replace_path(mpriv->title,mpriv->dir); | |
204 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
205 if ((dirp = opendir (mpriv->dir)) == NULL){ |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
206 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_OpendirError, strerror(errno)); |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
207 return 0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
208 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
209 |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
210 if (menu_keepdir) { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
211 path_fp = open (MENU_KEEP_PATH, O_CREAT | O_WRONLY | O_TRUNC, 0666); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
212 if (path_fp >= 0) { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
213 write (path_fp, mpriv->dir, strlen (mpriv->dir)); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
214 close (path_fp); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
215 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
216 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
217 |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
218 namelist = (char **) malloc(sizeof(char *)); |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
219 extensions = get_extensions(menu); |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
220 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
221 n=0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
222 while ((dp = readdir(dirp)) != NULL) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
223 if(dp->d_name[0] == '.' && strcmp(dp->d_name,"..") != 0) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
224 continue; |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
225 mylstat(args,dp->d_name,&st); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
226 if (file_filter && extensions && !S_ISDIR(st.st_mode)) { |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
227 if((ext = strrchr(dp->d_name,'.')) == NULL) |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
228 continue; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
229 ext++; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
230 elem = extensions; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
231 do { |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
232 if (!strcasecmp(ext, *elem)) |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
233 break; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
234 } while (*++elem); |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
235 if (*elem == NULL) |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
236 continue; |
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
237 } |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
238 if(n%20 == 0){ // Get some more mem |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
239 if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *))) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
240 == NULL) { |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
241 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReallocError, strerror(errno)); |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
242 n--; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
243 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
244 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
245 namelist=tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
246 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
247 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
248 namelist[n] = (char *) malloc(strlen(dp->d_name) + 2); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
249 if(namelist[n] == NULL){ |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
250 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_MallocError, strerror(errno)); |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
251 n--; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
252 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
253 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
254 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
255 strcpy(namelist[n], dp->d_name); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
256 if(S_ISDIR(st.st_mode)) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
257 strcat(namelist[n], "/"); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
258 n++; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
259 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
260 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
261 bailout: |
19491
10d8f2cae948
added new filter option to menu file browser to only display some files according to extension
ben
parents:
19490
diff
changeset
|
262 free_extensions (extensions); |
16061 | 263 closedir(dirp); |
264 | |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
265 qsort(namelist, n, sizeof(char *), (kill_warn)compare); |
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
266 |
8197 | 267 if (n < 0) { |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
268 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReaddirError,strerror(errno)); |
8197 | 269 return 0; |
270 } | |
271 while(n--) { | |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
272 if((e = calloc(1,sizeof(list_entry_t))) != NULL){ |
8613 | 273 e->p.next = NULL; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
274 e->p.txt = strdup(namelist[n]); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
275 if(strchr(namelist[n], '/') != NULL) |
8197 | 276 e->d = 1; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
277 menu_list_add_entry(menu,e); |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
278 }else{ |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
279 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_MallocError, strerror(errno)); |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
280 } |
8197 | 281 free(namelist[n]); |
282 } | |
283 free(namelist); | |
284 | |
285 return 1; | |
286 } | |
287 | |
288 | |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
289 static char *action; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
290 |
8197 | 291 static void read_cmd(menu_t* menu,int cmd) { |
292 mp_cmd_t* c = NULL; | |
293 switch(cmd) { | |
17945
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
294 case MENU_CMD_LEFT: |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
295 mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../ |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
296 case MENU_CMD_RIGHT: |
8197 | 297 case MENU_CMD_OK: { |
298 // Directory | |
299 if(mpriv->p.current->d) { | |
300 if(mpriv->dir_action) { | |
301 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
302 char filename[fname_len]; | |
303 char* str; | |
304 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
305 str = replace_path(mpriv->dir_action,filename); | |
306 c = mp_input_parse_cmd(str); | |
307 if(str != mpriv->dir_action) | |
308 free(str); | |
309 } else { // Default action : open this dirctory ourself | |
310 int l = strlen(mpriv->dir); | |
311 char *slash = NULL, *p = NULL; | |
312 if(strcmp(mpriv->p.current->p.txt,"../") == 0) { | |
313 if(l <= 1) break; | |
314 mpriv->dir[l-1] = '\0'; | |
315 slash = strrchr(mpriv->dir,'/'); | |
20505 | 316 #if defined(__MINGW32__) || defined(__CYGWIN__) |
317 if (!slash) | |
318 slash = strrchr(mpriv->dir,'\\'); | |
319 #endif | |
8197 | 320 if(!slash) break; |
321 slash[1] = '\0'; | |
322 p = strdup(mpriv->dir); | |
323 } else { | |
324 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1); | |
325 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
326 } | |
327 menu_list_uninit(menu,free_entry); | |
328 if(!open_dir(menu,p)) { | |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
329 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_CantOpenDirectory,p); |
8197 | 330 menu->cl = 1; |
331 } | |
332 free(p); | |
333 } | |
334 } else { // Files | |
335 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
336 char filename[fname_len]; | |
337 char *str; | |
338 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
339 str = replace_path(mpriv->file_action,filename); | |
340 c = mp_input_parse_cmd(str); | |
341 if(str != mpriv->file_action) | |
342 free(str); | |
343 } | |
344 if(c) { | |
345 mp_input_queue_cmd(c); | |
346 if(mpriv->auto_close) | |
347 menu->cl = 1; | |
348 } | |
349 } break; | |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
350 case MENU_CMD_ACTION: { |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
351 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
352 char filename[fname_len]; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
353 char *str; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
354 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
355 str = replace_path(action, filename); |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
356 mp_input_queue_cmd(mp_input_parse_cmd(str)); |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
357 if(str != action) |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
358 free(str); |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
359 } break; |
8197 | 360 default: |
361 menu_list_read_cmd(menu,cmd); | |
362 } | |
363 } | |
364 | |
365 static void read_key(menu_t* menu,int c){ | |
17945
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
366 if(c == KEY_BS) |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
367 read_cmd(menu,MENU_CMD_LEFT); |
19490
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
368 else { |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
369 char **str; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
370 for (str=mpriv->actions; str && *str; str++) |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
371 if (c == (*str)[0]) { |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
372 action = &(*str)[2]; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
373 read_cmd(menu,MENU_CMD_ACTION); |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
374 break; |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
375 } |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
376 if (!str || !*str) |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
377 menu_list_read_key(menu,c,1); |
f57977ac0394
support multiple actions in menu's file browser using key bindings
ben
parents:
19431
diff
changeset
|
378 } |
8197 | 379 } |
380 | |
381 static void clos(menu_t* menu) { | |
382 menu_list_uninit(menu,free_entry); | |
383 free(mpriv->dir); | |
384 } | |
385 | |
386 static int open_fs(menu_t* menu, char* args) { | |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
387 char *path = mpriv->path, *freepath = NULL; |
8197 | 388 int r = 0; |
389 char wd[PATH_MAX+1]; | |
390 args = NULL; // Warning kill | |
391 | |
392 menu->draw = menu_list_draw; | |
393 menu->read_cmd = read_cmd; | |
394 menu->read_key = read_key; | |
395 menu->close = clos; | |
396 | |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
397 if (menu_keepdir) { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
398 if (!path || path[0] == '\0') { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
399 struct stat st; |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
400 int path_fp; |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
401 |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
402 path_fp = open (MENU_KEEP_PATH, O_RDONLY); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
403 if (path_fp >= 0) { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
404 if (!fstat (path_fp, &st) && (st.st_size > 0)) { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
405 path = malloc(st.st_size+1); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
406 if ((read(path_fp, path, st.st_size) == st.st_size) && path[0] != '\0'){ |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
407 freepath = path; |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
408 path[st.st_size] = '\0'; |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
409 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
410 else { |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
411 free(path); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
412 path = NULL; |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
413 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
414 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
415 close (path_fp); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
416 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
417 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
418 } |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
419 |
8197 | 420 getcwd(wd,PATH_MAX); |
421 if(!path || path[0] == '\0') { | |
422 int l = strlen(wd) + 2; | |
423 char b[l]; | |
424 sprintf(b,"%s/",wd); | |
425 r = open_dir(menu,b); | |
426 } else if(path[0] != '/') { | |
427 int al = strlen(path); | |
428 int l = strlen(wd) + al + 3; | |
429 char b[l]; | |
430 if(b[al-1] != '/') | |
431 sprintf(b,"%s/%s/",wd,path); | |
432 else | |
433 sprintf(b,"%s/%s",wd,path); | |
434 r = open_dir(menu,b); | |
435 } else | |
436 r = open_dir(menu,path); | |
437 | |
23229
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
438 if (freepath) |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
439 free(freepath); |
ae2a2d5ca64b
new -menu-keepdir option that allows libmenu file browser to always restart browsing from the last place we were instead of current dir
ben
parents:
20505
diff
changeset
|
440 |
8197 | 441 return r; |
442 } | |
443 | |
444 const menu_info_t menu_info_filesel = { | |
445 "File seletor menu", | |
446 "filesel", | |
447 "Albeu", | |
448 "", | |
449 { | |
450 "fs_cfg", | |
451 sizeof(struct menu_priv_s), | |
452 &cfg_dflt, | |
453 cfg_fields | |
454 }, | |
455 open_fs | |
456 }; |