get some new maintainers
[mplayer/glamo.git] / libmenu / menu_filesel.c
blob4dd2d69e8b7914c0382203f04b5d2dd0eda4c832
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 qsort(namelist, n, sizeof(char *), (kill_warn)compare);
182 if (n < 0) {
183 printf("readdir error: %s\n",strerror(errno));
184 return 0;
186 while(n--) {
187 if((e = calloc(1,sizeof(list_entry_t))) != NULL){
188 e->p.next = NULL;
189 e->p.txt = strdup(namelist[n]);
190 if(strchr(namelist[n], '/') != NULL)
191 e->d = 1;
192 menu_list_add_entry(menu,e);
193 }else{
194 printf("malloc error: %s", strerror(errno));
196 free(namelist[n]);
198 free(namelist);
200 return 1;
204 static void read_cmd(menu_t* menu,int cmd) {
205 mp_cmd_t* c = NULL;
206 switch(cmd) {
207 case MENU_CMD_OK: {
208 // Directory
209 if(mpriv->p.current->d) {
210 if(mpriv->dir_action) {
211 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
212 char filename[fname_len];
213 char* str;
214 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
215 str = replace_path(mpriv->dir_action,filename);
216 c = mp_input_parse_cmd(str);
217 if(str != mpriv->dir_action)
218 free(str);
219 } else { // Default action : open this dirctory ourself
220 int l = strlen(mpriv->dir);
221 char *slash = NULL, *p = NULL;
222 if(strcmp(mpriv->p.current->p.txt,"../") == 0) {
223 if(l <= 1) break;
224 mpriv->dir[l-1] = '\0';
225 slash = strrchr(mpriv->dir,'/');
226 if(!slash) break;
227 slash[1] = '\0';
228 p = strdup(mpriv->dir);
229 } else {
230 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1);
231 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
233 menu_list_uninit(menu,free_entry);
234 if(!open_dir(menu,p)) {
235 printf("Can't open directory %s\n",p);
236 menu->cl = 1;
238 free(p);
240 } else { // Files
241 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
242 char filename[fname_len];
243 char *str;
244 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
245 str = replace_path(mpriv->file_action,filename);
246 c = mp_input_parse_cmd(str);
247 if(str != mpriv->file_action)
248 free(str);
250 if(c) {
251 mp_input_queue_cmd(c);
252 if(mpriv->auto_close)
253 menu->cl = 1;
255 } break;
256 default:
257 menu_list_read_cmd(menu,cmd);
261 static void read_key(menu_t* menu,int c){
262 if(c == KEY_BS) {
263 mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../
264 read_cmd(menu,MENU_CMD_OK);
265 } else
266 menu_list_read_key(menu,c,1);
269 static void clos(menu_t* menu) {
270 menu_list_uninit(menu,free_entry);
271 free(mpriv->dir);
274 static int open_fs(menu_t* menu, char* args) {
275 char *path = mpriv->path;
276 int r = 0;
277 char wd[PATH_MAX+1];
278 args = NULL; // Warning kill
280 menu->draw = menu_list_draw;
281 menu->read_cmd = read_cmd;
282 menu->read_key = read_key;
283 menu->close = clos;
285 getcwd(wd,PATH_MAX);
286 if(!path || path[0] == '\0') {
287 int l = strlen(wd) + 2;
288 char b[l];
289 sprintf(b,"%s/",wd);
290 r = open_dir(menu,b);
291 } else if(path[0] != '/') {
292 int al = strlen(path);
293 int l = strlen(wd) + al + 3;
294 char b[l];
295 if(b[al-1] != '/')
296 sprintf(b,"%s/%s/",wd,path);
297 else
298 sprintf(b,"%s/%s",wd,path);
299 r = open_dir(menu,b);
300 } else
301 r = open_dir(menu,path);
303 return r;
306 const menu_info_t menu_info_filesel = {
307 "File seletor menu",
308 "filesel",
309 "Albeu",
312 "fs_cfg",
313 sizeof(struct menu_priv_s),
314 &cfg_dflt,
315 cfg_fields
317 open_fs