Mercurial > mplayer.hg
annotate libmenu/menu_filesel.c @ 18025:f7209ad640d2
1.233: Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
1.232: [applied by ppt, rev 1.143]
author | kraymer |
---|---|
date | Sun, 02 Apr 2006 20:50:49 +0000 |
parents | 6927fabaef92 |
children | ac69ba536915 |
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> | |
9 #include <ctype.h> | |
10 #include <unistd.h> | |
8291
abe95dde3223
limits.h required to get a PATH_MAX definition (on solaris)
jkeil
parents:
8226
diff
changeset
|
11 #include <limits.h> |
8197 | 12 |
13 | |
16862 | 14 #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
|
15 #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
|
16 #include "help_mp.h" |
8197 | 17 |
16862 | 18 #include "m_struct.h" |
19 #include "m_option.h" | |
8197 | 20 |
21 #include "img_format.h" | |
22 #include "mp_image.h" | |
23 | |
24 #include "menu.h" | |
25 #include "menu_list.h" | |
16862 | 26 #include "input/input.h" |
27 #include "osdep/keycodes.h" | |
8197 | 28 |
29 struct list_entry_s { | |
30 struct list_entry p; | |
31 int d; | |
32 }; | |
33 | |
34 struct menu_priv_s { | |
35 menu_list_priv_t p; | |
36 char* dir; // current dir | |
37 /// Cfg fields | |
38 char* path; | |
39 char* title; | |
40 char* file_action; | |
41 char* dir_action; | |
42 int auto_close; | |
43 }; | |
44 | |
45 static struct menu_priv_s cfg_dflt = { | |
46 MENU_LIST_PRIV_DFLT, | |
47 NULL, | |
48 | |
49 NULL, | |
50 "Select a file: %p", | |
8226 | 51 "loadfile '%p'", |
8197 | 52 NULL, |
53 0 | |
54 }; | |
55 | |
56 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m) | |
57 | |
58 static m_option_t cfg_fields[] = { | |
59 MENU_LIST_PRIV_FIELDS, | |
60 { "path", ST_OFF(path), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
61 { "title", ST_OFF(title), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
62 { "file-action", ST_OFF(file_action), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
63 { "dir-action", ST_OFF(dir_action), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
64 { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL }, | |
65 { NULL, NULL, NULL, 0,0,0,NULL } | |
66 }; | |
67 | |
68 #define mpriv (menu->priv) | |
69 | |
70 static void free_entry(list_entry_t* entry) { | |
71 free(entry->p.txt); | |
72 free(entry); | |
73 } | |
74 | |
75 static char* replace_path(char* title , char* dir) { | |
76 char *p = strstr(title,"%p"); | |
77 if(p) { | |
78 int tl = strlen(title); | |
79 int dl = strlen(dir); | |
80 int t1l = p-title; | |
81 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
|
82 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
|
83 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
|
84 |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
85 do { |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
86 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
|
87 l++; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
88 } while (*d++); |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
89 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
|
90 n = r + t1l; |
8197 | 91 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
|
92 do { |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
93 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
|
94 *n++ = '\\'; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
95 } while ((*n++ = *dir++)); |
8197 | 96 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
|
97 strcpy(n-1,p+2); |
8197 | 98 return r; |
99 } else | |
100 return title; | |
101 } | |
102 | |
103 typedef int (*kill_warn)(const void*, const void*); | |
104 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
105 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
|
106 int l = strlen(dir) + strlen(file); |
8613 | 107 char s[l+2]; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
108 sprintf(s,"%s/%s",dir,file); |
9249 | 109 return stat(s,st); |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
110 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
111 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
112 static int compare(char **a, char **b){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
113 if((*a)[strlen(*a) - 1] == '/') { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
114 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
115 return strcmp(*b, *a) ; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
116 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
117 return 1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
118 } else { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
119 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
120 return -1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
121 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
122 return strcmp(*b, *a); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
123 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
124 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
125 |
8197 | 126 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
|
127 char **namelist, **tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
128 struct dirent *dp; |
8197 | 129 struct stat st; |
130 int n; | |
131 char* p = NULL; | |
132 list_entry_t* e; | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
133 DIR* dirp; |
8197 | 134 |
135 menu_list_init(menu); | |
136 | |
137 if(mpriv->dir) | |
138 free(mpriv->dir); | |
139 mpriv->dir = strdup(args); | |
140 if(mpriv->p.title && mpriv->p.title != mpriv->title && mpriv->p.title != cfg_dflt.p.title) | |
141 free(mpriv->p.title); | |
142 p = strstr(mpriv->title,"%p"); | |
143 | |
144 mpriv->p.title = replace_path(mpriv->title,mpriv->dir); | |
145 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
146 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
|
147 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
|
148 return 0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
149 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
150 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
151 namelist = (char **) malloc(sizeof(char *)); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
152 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
153 n=0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
154 while ((dp = readdir(dirp)) != NULL) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
155 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
|
156 continue; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
157 if(n%20 == 0){ // Get some more mem |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
158 if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *))) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
159 == NULL) { |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
160 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
|
161 n--; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
162 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
163 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
164 namelist=tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
165 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
166 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
167 namelist[n] = (char *) malloc(strlen(dp->d_name) + 2); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
168 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
|
169 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
|
170 n--; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
171 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
172 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
173 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
174 strcpy(namelist[n], dp->d_name); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
175 mylstat(args,namelist[n],&st); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
176 if(S_ISDIR(st.st_mode)) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
177 strcat(namelist[n], "/"); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
178 n++; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
179 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
180 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
181 bailout: |
16061 | 182 closedir(dirp); |
183 | |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
184 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
|
185 |
8197 | 186 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
|
187 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReaddirError,strerror(errno)); |
8197 | 188 return 0; |
189 } | |
190 while(n--) { | |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
191 if((e = calloc(1,sizeof(list_entry_t))) != NULL){ |
8613 | 192 e->p.next = NULL; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
193 e->p.txt = strdup(namelist[n]); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
194 if(strchr(namelist[n], '/') != NULL) |
8197 | 195 e->d = 1; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
196 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
|
197 }else{ |
17994
6927fabaef92
Part1 of several printf2mp_msg changes in patch from Otvos Attila oattila AT chello DOT hu
reynaldo
parents:
17945
diff
changeset
|
198 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
|
199 } |
8197 | 200 free(namelist[n]); |
201 } | |
202 free(namelist); | |
203 | |
204 return 1; | |
205 } | |
206 | |
207 | |
208 static void read_cmd(menu_t* menu,int cmd) { | |
209 mp_cmd_t* c = NULL; | |
210 switch(cmd) { | |
17945
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
211 case MENU_CMD_LEFT: |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
212 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
|
213 case MENU_CMD_RIGHT: |
8197 | 214 case MENU_CMD_OK: { |
215 // Directory | |
216 if(mpriv->p.current->d) { | |
217 if(mpriv->dir_action) { | |
218 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
219 char filename[fname_len]; | |
220 char* str; | |
221 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
222 str = replace_path(mpriv->dir_action,filename); | |
223 c = mp_input_parse_cmd(str); | |
224 if(str != mpriv->dir_action) | |
225 free(str); | |
226 } else { // Default action : open this dirctory ourself | |
227 int l = strlen(mpriv->dir); | |
228 char *slash = NULL, *p = NULL; | |
229 if(strcmp(mpriv->p.current->p.txt,"../") == 0) { | |
230 if(l <= 1) break; | |
231 mpriv->dir[l-1] = '\0'; | |
232 slash = strrchr(mpriv->dir,'/'); | |
233 if(!slash) break; | |
234 slash[1] = '\0'; | |
235 p = strdup(mpriv->dir); | |
236 } else { | |
237 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1); | |
238 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
239 } | |
240 menu_list_uninit(menu,free_entry); | |
241 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
|
242 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_CantOpenDirectory,p); |
8197 | 243 menu->cl = 1; |
244 } | |
245 free(p); | |
246 } | |
247 } else { // Files | |
248 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
249 char filename[fname_len]; | |
250 char *str; | |
251 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
252 str = replace_path(mpriv->file_action,filename); | |
253 c = mp_input_parse_cmd(str); | |
254 if(str != mpriv->file_action) | |
255 free(str); | |
256 } | |
257 if(c) { | |
258 mp_input_queue_cmd(c); | |
259 if(mpriv->auto_close) | |
260 menu->cl = 1; | |
261 } | |
262 } break; | |
263 default: | |
264 menu_list_read_cmd(menu,cmd); | |
265 } | |
266 } | |
267 | |
268 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
|
269 if(c == KEY_BS) |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
270 read_cmd(menu,MENU_CMD_LEFT); |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
271 else |
8197 | 272 menu_list_read_key(menu,c,1); |
273 } | |
274 | |
275 static void clos(menu_t* menu) { | |
276 menu_list_uninit(menu,free_entry); | |
277 free(mpriv->dir); | |
278 } | |
279 | |
280 static int open_fs(menu_t* menu, char* args) { | |
281 char *path = mpriv->path; | |
282 int r = 0; | |
283 char wd[PATH_MAX+1]; | |
284 args = NULL; // Warning kill | |
285 | |
286 menu->draw = menu_list_draw; | |
287 menu->read_cmd = read_cmd; | |
288 menu->read_key = read_key; | |
289 menu->close = clos; | |
290 | |
291 getcwd(wd,PATH_MAX); | |
292 if(!path || path[0] == '\0') { | |
293 int l = strlen(wd) + 2; | |
294 char b[l]; | |
295 sprintf(b,"%s/",wd); | |
296 r = open_dir(menu,b); | |
297 } else if(path[0] != '/') { | |
298 int al = strlen(path); | |
299 int l = strlen(wd) + al + 3; | |
300 char b[l]; | |
301 if(b[al-1] != '/') | |
302 sprintf(b,"%s/%s/",wd,path); | |
303 else | |
304 sprintf(b,"%s/%s",wd,path); | |
305 r = open_dir(menu,b); | |
306 } else | |
307 r = open_dir(menu,path); | |
308 | |
309 return r; | |
310 } | |
311 | |
312 const menu_info_t menu_info_filesel = { | |
313 "File seletor menu", | |
314 "filesel", | |
315 "Albeu", | |
316 "", | |
317 { | |
318 "fs_cfg", | |
319 sizeof(struct menu_priv_s), | |
320 &cfg_dflt, | |
321 cfg_fields | |
322 }, | |
323 open_fs | |
324 }; |