support for Geforce FX5500 based on patch by Pascal Yu <yu_pascal at hotmail.com>
[mplayer/greg.git] / libmenu / menu_filesel.c
blob7d40047cc920da5a4505365d9d7294774af5877a
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>
11 #include <limits.h>
14 #include "config.h"
16 #include "m_struct.h"
17 #include "m_option.h"
19 #include "img_format.h"
20 #include "mp_image.h"
22 #include "menu.h"
23 #include "menu_list.h"
24 #include "input/input.h"
25 #include "osdep/keycodes.h"
27 struct list_entry_s {
28 struct list_entry p;
29 int d;
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;
43 static struct menu_priv_s cfg_dflt = {
44 MENU_LIST_PRIV_DFLT,
45 NULL,
47 NULL,
48 "Select a file: %p",
49 "loadfile '%p'",
50 NULL,
54 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
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 }
66 #define mpriv (menu->priv)
68 static void free_entry(list_entry_t* entry) {
69 free(entry->p.txt);
70 free(entry);
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, *n, *d = dir;
81 char term = *(p-1);
83 do {
84 if (*d == '\\' || *d == term)
85 l++;
86 } while (*d++);
87 r = malloc(l + 1);
88 n = r + t1l;
89 memcpy(r,title,t1l);
90 do {
91 if (*dir == '\\' || *dir == term)
92 *n++ = '\\';
93 } while ((*n++ = *dir++));
94 if(tl - t1l - 2 > 0)
95 strcpy(n-1,p+2);
96 return r;
97 } else
98 return title;
101 typedef int (*kill_warn)(const void*, const void*);
103 static int mylstat(char *dir, char *file,struct stat* st) {
104 int l = strlen(dir) + strlen(file);
105 char s[l+2];
106 sprintf(s,"%s/%s",dir,file);
107 return stat(s,st);
110 static int compare(char **a, char **b){
111 if((*a)[strlen(*a) - 1] == '/') {
112 if((*b)[strlen(*b) - 1] == '/')
113 return strcmp(*b, *a) ;
114 else
115 return 1;
116 } else {
117 if((*b)[strlen(*b) - 1] == '/')
118 return -1;
119 else
120 return strcmp(*b, *a);
124 static int open_dir(menu_t* menu,char* args) {
125 char **namelist, **tp;
126 struct dirent *dp;
127 struct stat st;
128 int n;
129 char* p = NULL;
130 list_entry_t* e;
131 DIR* dirp;
133 menu_list_init(menu);
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");
142 mpriv->p.title = replace_path(mpriv->title,mpriv->dir);
144 if ((dirp = opendir (mpriv->dir)) == NULL){
145 printf("opendir error: %s", strerror(errno));
146 return 0;
149 namelist = (char **) malloc(sizeof(char *));
151 n=0;
152 while ((dp = readdir(dirp)) != NULL) {
153 if(dp->d_name[0] == '.' && strcmp(dp->d_name,"..") != 0)
154 continue;
155 if(n%20 == 0){ // Get some more mem
156 if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *)))
157 == NULL) {
158 printf("realloc error: %s", strerror(errno));
159 n--;
160 goto bailout;
162 namelist=tp;
165 namelist[n] = (char *) malloc(strlen(dp->d_name) + 2);
166 if(namelist[n] == NULL){
167 printf("malloc error: %s", strerror(errno));
168 n--;
169 goto bailout;
172 strcpy(namelist[n], dp->d_name);
173 mylstat(args,namelist[n],&st);
174 if(S_ISDIR(st.st_mode))
175 strcat(namelist[n], "/");
176 n++;
179 bailout:
180 closedir(dirp);
182 qsort(namelist, n, sizeof(char *), (kill_warn)compare);
184 if (n < 0) {
185 printf("readdir error: %s\n",strerror(errno));
186 return 0;
188 while(n--) {
189 if((e = calloc(1,sizeof(list_entry_t))) != NULL){
190 e->p.next = NULL;
191 e->p.txt = strdup(namelist[n]);
192 if(strchr(namelist[n], '/') != NULL)
193 e->d = 1;
194 menu_list_add_entry(menu,e);
195 }else{
196 printf("malloc error: %s", strerror(errno));
198 free(namelist[n]);
200 free(namelist);
202 return 1;
206 static void read_cmd(menu_t* menu,int cmd) {
207 mp_cmd_t* c = NULL;
208 switch(cmd) {
209 case MENU_CMD_OK: {
210 // Directory
211 if(mpriv->p.current->d) {
212 if(mpriv->dir_action) {
213 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
214 char filename[fname_len];
215 char* str;
216 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
217 str = replace_path(mpriv->dir_action,filename);
218 c = mp_input_parse_cmd(str);
219 if(str != mpriv->dir_action)
220 free(str);
221 } else { // Default action : open this dirctory ourself
222 int l = strlen(mpriv->dir);
223 char *slash = NULL, *p = NULL;
224 if(strcmp(mpriv->p.current->p.txt,"../") == 0) {
225 if(l <= 1) break;
226 mpriv->dir[l-1] = '\0';
227 slash = strrchr(mpriv->dir,'/');
228 if(!slash) break;
229 slash[1] = '\0';
230 p = strdup(mpriv->dir);
231 } else {
232 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1);
233 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
235 menu_list_uninit(menu,free_entry);
236 if(!open_dir(menu,p)) {
237 printf("Can't open directory %s\n",p);
238 menu->cl = 1;
240 free(p);
242 } else { // Files
243 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
244 char filename[fname_len];
245 char *str;
246 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
247 str = replace_path(mpriv->file_action,filename);
248 c = mp_input_parse_cmd(str);
249 if(str != mpriv->file_action)
250 free(str);
252 if(c) {
253 mp_input_queue_cmd(c);
254 if(mpriv->auto_close)
255 menu->cl = 1;
257 } break;
258 default:
259 menu_list_read_cmd(menu,cmd);
263 static void read_key(menu_t* menu,int c){
264 if(c == KEY_BS) {
265 mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../
266 read_cmd(menu,MENU_CMD_OK);
267 } else
268 menu_list_read_key(menu,c,1);
271 static void clos(menu_t* menu) {
272 menu_list_uninit(menu,free_entry);
273 free(mpriv->dir);
276 static int open_fs(menu_t* menu, char* args) {
277 char *path = mpriv->path;
278 int r = 0;
279 char wd[PATH_MAX+1];
280 args = NULL; // Warning kill
282 menu->draw = menu_list_draw;
283 menu->read_cmd = read_cmd;
284 menu->read_key = read_key;
285 menu->close = clos;
287 getcwd(wd,PATH_MAX);
288 if(!path || path[0] == '\0') {
289 int l = strlen(wd) + 2;
290 char b[l];
291 sprintf(b,"%s/",wd);
292 r = open_dir(menu,b);
293 } else if(path[0] != '/') {
294 int al = strlen(path);
295 int l = strlen(wd) + al + 3;
296 char b[l];
297 if(b[al-1] != '/')
298 sprintf(b,"%s/%s/",wd,path);
299 else
300 sprintf(b,"%s/%s",wd,path);
301 r = open_dir(menu,b);
302 } else
303 r = open_dir(menu,path);
305 return r;
308 const menu_info_t menu_info_filesel = {
309 "File seletor menu",
310 "filesel",
311 "Albeu",
314 "fs_cfg",
315 sizeof(struct menu_priv_s),
316 &cfg_dflt,
317 cfg_fields
319 open_fs