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