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