Mercurial > mplayer.hg
annotate libmenu/menu_filesel.c @ 8291:abe95dde3223
limits.h required to get a PATH_MAX definition (on solaris)
author | jkeil |
---|---|
date | Tue, 26 Nov 2002 18:54:23 +0000 |
parents | 4b8fcc2af8f1 |
children | ecf6a4cf3272 |
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" | |
25 #include "../linux/keycodes.h" | |
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 | |
93 static int open_dir(menu_t* menu,char* args) { | |
94 struct dirent **namelist; | |
95 struct stat st; | |
96 int n; | |
97 char* p = NULL; | |
98 list_entry_t* e; | |
99 | |
100 int mylstat(char *dir, char *file,struct stat* st) { | |
101 int l = strlen(dir) + strlen(file); | |
102 char s[l+1]; | |
103 sprintf(s,"%s%s",args,file); | |
104 return lstat(s,st); | |
105 } | |
106 | |
107 int compare(struct dirent **a,struct dirent **b) { | |
108 struct stat as,bs; | |
109 mylstat(args,(*a)->d_name,&as); | |
110 mylstat(args,(*b)->d_name,&bs); | |
111 if(S_ISDIR(as.st_mode)) { | |
112 if(S_ISDIR(bs.st_mode)) | |
113 return alphasort(b,a); | |
114 else | |
115 return 1; | |
116 } else { | |
117 if(S_ISDIR(bs.st_mode)) | |
118 return -1; | |
119 else | |
120 return alphasort(b,a); | |
121 } | |
122 } | |
123 | |
124 int select_f(const struct dirent *d) { | |
125 if(d->d_name[0] != '.' || strcmp(d->d_name,"..") == 0) | |
126 return 1; | |
127 return 0; | |
128 } | |
129 | |
130 menu_list_init(menu); | |
131 | |
132 if(mpriv->dir) | |
133 free(mpriv->dir); | |
134 mpriv->dir = strdup(args); | |
135 if(mpriv->p.title && mpriv->p.title != mpriv->title && mpriv->p.title != cfg_dflt.p.title) | |
136 free(mpriv->p.title); | |
137 p = strstr(mpriv->title,"%p"); | |
138 | |
139 mpriv->p.title = replace_path(mpriv->title,mpriv->dir); | |
140 | |
141 n = scandir(mpriv->dir, &namelist, select_f, (kill_warn)compare); | |
142 if (n < 0) { | |
143 printf("scandir error: %s\n",strerror(errno)); | |
144 return 0; | |
145 } | |
146 while(n--) { | |
147 e = calloc(1,sizeof(list_entry_t)); | |
148 mylstat(args,namelist[n]->d_name,&st); | |
149 | |
150 if(S_ISDIR(st.st_mode)) { | |
151 int sl =strlen(namelist[n]->d_name); | |
152 e->p.txt = malloc(sl + 2); | |
153 strncpy(e->p.txt,namelist[n]->d_name,sl); | |
154 e->p.txt[sl] = '/'; | |
155 e->p.txt[sl+1] = '\0'; | |
156 e->d = 1; | |
157 menu_list_add_entry(menu,e); | |
158 } else if(strcmp(namelist[n]->d_name,"..") == 0 || namelist[n]->d_name[0] != '.') { | |
159 e->p.txt = strdup(namelist[n]->d_name); | |
160 menu_list_add_entry(menu,e); | |
161 } | |
162 free(namelist[n]); | |
163 } | |
164 free(namelist); | |
165 | |
166 return 1; | |
167 } | |
168 | |
169 | |
170 static void read_cmd(menu_t* menu,int cmd) { | |
171 mp_cmd_t* c = NULL; | |
172 switch(cmd) { | |
173 case MENU_CMD_OK: { | |
174 // Directory | |
175 if(mpriv->p.current->d) { | |
176 if(mpriv->dir_action) { | |
177 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
178 char filename[fname_len]; | |
179 char* str; | |
180 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
181 str = replace_path(mpriv->dir_action,filename); | |
182 c = mp_input_parse_cmd(str); | |
183 if(str != mpriv->dir_action) | |
184 free(str); | |
185 } else { // Default action : open this dirctory ourself | |
186 int l = strlen(mpriv->dir); | |
187 char *slash = NULL, *p = NULL; | |
188 if(strcmp(mpriv->p.current->p.txt,"../") == 0) { | |
189 if(l <= 1) break; | |
190 mpriv->dir[l-1] = '\0'; | |
191 slash = strrchr(mpriv->dir,'/'); | |
192 if(!slash) break; | |
193 slash[1] = '\0'; | |
194 p = strdup(mpriv->dir); | |
195 } else { | |
196 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1); | |
197 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
198 } | |
199 menu_list_uninit(menu,free_entry); | |
200 if(!open_dir(menu,p)) { | |
201 printf("Can't open directory %s\n",p); | |
202 menu->cl = 1; | |
203 } | |
204 free(p); | |
205 } | |
206 } else { // Files | |
207 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
208 char filename[fname_len]; | |
209 char *str; | |
210 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
211 str = replace_path(mpriv->file_action,filename); | |
212 c = mp_input_parse_cmd(str); | |
213 if(str != mpriv->file_action) | |
214 free(str); | |
215 } | |
216 if(c) { | |
217 mp_input_queue_cmd(c); | |
218 if(mpriv->auto_close) | |
219 menu->cl = 1; | |
220 } | |
221 } break; | |
222 default: | |
223 menu_list_read_cmd(menu,cmd); | |
224 } | |
225 } | |
226 | |
227 static void read_key(menu_t* menu,int c){ | |
228 if(c == KEY_BS) { | |
229 mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../ | |
230 read_cmd(menu,MENU_CMD_OK); | |
231 } else | |
232 menu_list_read_key(menu,c,1); | |
233 } | |
234 | |
235 static void clos(menu_t* menu) { | |
236 menu_list_uninit(menu,free_entry); | |
237 free(mpriv->dir); | |
238 } | |
239 | |
240 static int open_fs(menu_t* menu, char* args) { | |
241 char *path = mpriv->path; | |
242 int r = 0; | |
243 char wd[PATH_MAX+1]; | |
244 args = NULL; // Warning kill | |
245 | |
246 menu->draw = menu_list_draw; | |
247 menu->read_cmd = read_cmd; | |
248 menu->read_key = read_key; | |
249 menu->close = clos; | |
250 | |
251 getcwd(wd,PATH_MAX); | |
252 if(!path || path[0] == '\0') { | |
253 int l = strlen(wd) + 2; | |
254 char b[l]; | |
255 sprintf(b,"%s/",wd); | |
256 r = open_dir(menu,b); | |
257 } else if(path[0] != '/') { | |
258 int al = strlen(path); | |
259 int l = strlen(wd) + al + 3; | |
260 char b[l]; | |
261 if(b[al-1] != '/') | |
262 sprintf(b,"%s/%s/",wd,path); | |
263 else | |
264 sprintf(b,"%s/%s",wd,path); | |
265 r = open_dir(menu,b); | |
266 } else | |
267 r = open_dir(menu,path); | |
268 | |
269 return r; | |
270 } | |
271 | |
272 const menu_info_t menu_info_filesel = { | |
273 "File seletor menu", | |
274 "filesel", | |
275 "Albeu", | |
276 "", | |
277 { | |
278 "fs_cfg", | |
279 sizeof(struct menu_priv_s), | |
280 &cfg_dflt, | |
281 cfg_fields | |
282 }, | |
283 open_fs | |
284 }; |