Mercurial > mplayer.hg
annotate libmenu/menu_filesel.c @ 17972:03170287aea9
10l missing header for strdup
mp_msg.c:103: warning: assignment makes pointer from integer without a cast
author | ods15 |
---|---|
date | Mon, 27 Mar 2006 08:38:32 +0000 |
parents | 98f4e3704a76 |
children | 6927fabaef92 |
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" |
8197 | 15 |
16862 | 16 #include "m_struct.h" |
17 #include "m_option.h" | |
8197 | 18 |
19 #include "img_format.h" | |
20 #include "mp_image.h" | |
21 | |
22 #include "menu.h" | |
23 #include "menu_list.h" | |
16862 | 24 #include "input/input.h" |
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; | |
10624
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
80 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
|
81 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
|
82 |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
83 do { |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
84 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
|
85 l++; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
86 } while (*d++); |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
87 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
|
88 n = r + t1l; |
8197 | 89 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
|
90 do { |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
91 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
|
92 *n++ = '\\'; |
cdfd4a43c406
I've juste found a bug which prevent to load a file whose name contain
albeu
parents:
9380
diff
changeset
|
93 } while ((*n++ = *dir++)); |
8197 | 94 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
|
95 strcpy(n-1,p+2); |
8197 | 96 return r; |
97 } else | |
98 return title; | |
99 } | |
100 | |
101 typedef int (*kill_warn)(const void*, const void*); | |
102 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
103 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
|
104 int l = strlen(dir) + strlen(file); |
8613 | 105 char s[l+2]; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
106 sprintf(s,"%s/%s",dir,file); |
9249 | 107 return stat(s,st); |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
108 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
109 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
110 static int compare(char **a, char **b){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
111 if((*a)[strlen(*a) - 1] == '/') { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
112 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
113 return strcmp(*b, *a) ; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
114 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
115 return 1; |
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 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
118 return -1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
119 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
120 return strcmp(*b, *a); |
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 |
8197 | 124 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
|
125 char **namelist, **tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
126 struct dirent *dp; |
8197 | 127 struct stat st; |
128 int n; | |
129 char* p = NULL; | |
130 list_entry_t* e; | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
131 DIR* dirp; |
8197 | 132 |
133 menu_list_init(menu); | |
134 | |
135 if(mpriv->dir) | |
136 free(mpriv->dir); | |
137 mpriv->dir = strdup(args); | |
138 if(mpriv->p.title && mpriv->p.title != mpriv->title && mpriv->p.title != cfg_dflt.p.title) | |
139 free(mpriv->p.title); | |
140 p = strstr(mpriv->title,"%p"); | |
141 | |
142 mpriv->p.title = replace_path(mpriv->title,mpriv->dir); | |
143 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
144 if ((dirp = opendir (mpriv->dir)) == NULL){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
145 printf("opendir error: %s", strerror(errno)); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
146 return 0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
147 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
148 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
149 namelist = (char **) malloc(sizeof(char *)); |
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 n=0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
152 while ((dp = readdir(dirp)) != NULL) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
153 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
|
154 continue; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
155 if(n%20 == 0){ // Get some more mem |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
156 if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *))) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
157 == NULL) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
158 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
|
159 n--; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
160 goto bailout; |
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 namelist=tp; |
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 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
165 namelist[n] = (char *) malloc(strlen(dp->d_name) + 2); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
166 if(namelist[n] == NULL){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
167 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
|
168 n--; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
169 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
170 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
171 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
172 strcpy(namelist[n], dp->d_name); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
173 mylstat(args,namelist[n],&st); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
174 if(S_ISDIR(st.st_mode)) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
175 strcat(namelist[n], "/"); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
176 n++; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
177 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
178 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
179 bailout: |
16061 | 180 closedir(dirp); |
181 | |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
182 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
|
183 |
8197 | 184 if (n < 0) { |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
185 printf("readdir error: %s\n",strerror(errno)); |
8197 | 186 return 0; |
187 } | |
188 while(n--) { | |
9104
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
189 if((e = calloc(1,sizeof(list_entry_t))) != NULL){ |
8613 | 190 e->p.next = NULL; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
191 e->p.txt = strdup(namelist[n]); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
192 if(strchr(namelist[n], '/') != NULL) |
8197 | 193 e->d = 1; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
194 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
|
195 }else{ |
a0aacfb492a5
Also attached some cleanup to menu_filesel.c, mainly to make it more
arpi
parents:
8853
diff
changeset
|
196 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
|
197 } |
8197 | 198 free(namelist[n]); |
199 } | |
200 free(namelist); | |
201 | |
202 return 1; | |
203 } | |
204 | |
205 | |
206 static void read_cmd(menu_t* menu,int cmd) { | |
207 mp_cmd_t* c = NULL; | |
208 switch(cmd) { | |
17945
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
209 case MENU_CMD_LEFT: |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
210 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
|
211 case MENU_CMD_RIGHT: |
8197 | 212 case MENU_CMD_OK: { |
213 // Directory | |
214 if(mpriv->p.current->d) { | |
215 if(mpriv->dir_action) { | |
216 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
217 char filename[fname_len]; | |
218 char* str; | |
219 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
220 str = replace_path(mpriv->dir_action,filename); | |
221 c = mp_input_parse_cmd(str); | |
222 if(str != mpriv->dir_action) | |
223 free(str); | |
224 } else { // Default action : open this dirctory ourself | |
225 int l = strlen(mpriv->dir); | |
226 char *slash = NULL, *p = NULL; | |
227 if(strcmp(mpriv->p.current->p.txt,"../") == 0) { | |
228 if(l <= 1) break; | |
229 mpriv->dir[l-1] = '\0'; | |
230 slash = strrchr(mpriv->dir,'/'); | |
231 if(!slash) break; | |
232 slash[1] = '\0'; | |
233 p = strdup(mpriv->dir); | |
234 } else { | |
235 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1); | |
236 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
237 } | |
238 menu_list_uninit(menu,free_entry); | |
239 if(!open_dir(menu,p)) { | |
240 printf("Can't open directory %s\n",p); | |
241 menu->cl = 1; | |
242 } | |
243 free(p); | |
244 } | |
245 } else { // Files | |
246 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
247 char filename[fname_len]; | |
248 char *str; | |
249 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
250 str = replace_path(mpriv->file_action,filename); | |
251 c = mp_input_parse_cmd(str); | |
252 if(str != mpriv->file_action) | |
253 free(str); | |
254 } | |
255 if(c) { | |
256 mp_input_queue_cmd(c); | |
257 if(mpriv->auto_close) | |
258 menu->cl = 1; | |
259 } | |
260 } break; | |
261 default: | |
262 menu_list_read_cmd(menu,cmd); | |
263 } | |
264 } | |
265 | |
266 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
|
267 if(c == KEY_BS) |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
268 read_cmd(menu,MENU_CMD_LEFT); |
98f4e3704a76
Allow 6 ways (up/down/left/right/ok/cancel) navigation.
albeu
parents:
16862
diff
changeset
|
269 else |
8197 | 270 menu_list_read_key(menu,c,1); |
271 } | |
272 | |
273 static void clos(menu_t* menu) { | |
274 menu_list_uninit(menu,free_entry); | |
275 free(mpriv->dir); | |
276 } | |
277 | |
278 static int open_fs(menu_t* menu, char* args) { | |
279 char *path = mpriv->path; | |
280 int r = 0; | |
281 char wd[PATH_MAX+1]; | |
282 args = NULL; // Warning kill | |
283 | |
284 menu->draw = menu_list_draw; | |
285 menu->read_cmd = read_cmd; | |
286 menu->read_key = read_key; | |
287 menu->close = clos; | |
288 | |
289 getcwd(wd,PATH_MAX); | |
290 if(!path || path[0] == '\0') { | |
291 int l = strlen(wd) + 2; | |
292 char b[l]; | |
293 sprintf(b,"%s/",wd); | |
294 r = open_dir(menu,b); | |
295 } else if(path[0] != '/') { | |
296 int al = strlen(path); | |
297 int l = strlen(wd) + al + 3; | |
298 char b[l]; | |
299 if(b[al-1] != '/') | |
300 sprintf(b,"%s/%s/",wd,path); | |
301 else | |
302 sprintf(b,"%s/%s",wd,path); | |
303 r = open_dir(menu,b); | |
304 } else | |
305 r = open_dir(menu,path); | |
306 | |
307 return r; | |
308 } | |
309 | |
310 const menu_info_t menu_info_filesel = { | |
311 "File seletor menu", | |
312 "filesel", | |
313 "Albeu", | |
314 "", | |
315 { | |
316 "fs_cfg", | |
317 sizeof(struct menu_priv_s), | |
318 &cfg_dflt, | |
319 cfg_fields | |
320 }, | |
321 open_fs | |
322 }; |