summaryrefslogtreecommitdiff
path: root/uisimulator/tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'uisimulator/tree.c')
-rw-r--r--uisimulator/tree.c401
1 files changed, 221 insertions, 180 deletions
diff --git a/uisimulator/tree.c b/uisimulator/tree.c
index ed7ebad000..0382547590 100644
--- a/uisimulator/tree.c
+++ b/uisimulator/tree.c
@@ -32,13 +32,8 @@
32 32
33#include "play.h" 33#include "play.h"
34 34
35#define TREE_MAX_FILENAMELEN 64 35
36#define TREE_MAX_ON_SCREEN 7 36#define TREE_MAX_FILENAMELEN 128
37#define TREE_MAX_LEN_DISPLAY 16 /* max length that fits on screen */
38
39void browse_root(void) {
40 dirbrowse("/");
41}
42 37
43struct entry { 38struct entry {
44 int file; /* TRUE if file, FALSE if dir */ 39 int file; /* TRUE if file, FALSE if dir */
@@ -46,204 +41,250 @@ struct entry {
46 int namelen; 41 int namelen;
47}; 42};
48 43
49#define LINE_Y 8 /* Y position the entry-list starts at */ 44void browse_root(void)
50#define LINE_X 12 /* X position the entry-list starts at */ 45{
51#define LINE_HEIGTH 8 /* pixels for each text line */ 46 dirbrowse("/");
47}
48
52 49
53#ifdef HAVE_LCD_BITMAP 50#ifdef HAVE_LCD_BITMAP
54 51
52#define TREE_MAX_ON_SCREEN 7
53#define TREE_MAX_LEN_DISPLAY 16 /* max length that fits on screen */
54
55#define MARGIN_Y 8 /* Y pixel margin */
56#define MARGIN_X 12 /* X pixel margin */
57#define LINE_Y 0 /* Y position the entry-list starts at */
58#define LINE_X 2 /* X position the entry-list starts at */
59#define LINE_HEIGTH 8 /* pixels for each text line */
60
55extern unsigned char bitmap_icons_6x8[LastIcon][6]; 61extern unsigned char bitmap_icons_6x8[LastIcon][6];
56extern icons_6x8; 62extern icons_6x8;
57 63
64#else /* HAVE_LCD_BITMAP */
65
66#define TREE_MAX_ON_SCREEN 2
67#define TREE_MAX_LEN_DISPLAY 11 /* max length that fits on screen */
68#define LINE_Y 0 /* Y position the entry-list starts at */
69#define LINE_X 1 /* X position the entry-list starts at */
70
71#endif /* HAVE_LCD_BITMAP */
72
58int static showdir(char *path, struct entry *buffer, int start, 73int static showdir(char *path, struct entry *buffer, int start,
59 int scrollpos, int* at_end) 74 int scrollpos, int* at_end)
60{ 75{
61 int i; 76 int i;
62 int j=0; 77 int j=0;
63 int icon_type = 0; 78 int icon_type = 0;
64 DIR *dir = opendir(path); 79 DIR *dir = opendir(path);
65 struct dirent *entry; 80 struct dirent *entry;
66
67 if(!dir)
68 return -1; /* not a directory */
69
70 i=start;
71 *at_end=0; /* Have we displayed the last directory entry? */
72 while((entry = readdir(dir))) {
73 int len;
74
75 if(entry->d_name[0] == '.')
76 /* skip names starting with a dot */
77 continue;
78
79 if(j++ < scrollpos)
80 continue ;
81
82 len = strlen(entry->d_name);
83 if(len < TREE_MAX_FILENAMELEN)
84 /* strncpy() is evil, we memcpy() instead, +1 includes the
85 trailing zero */
86 memcpy(buffer[i].name, entry->d_name, len+1);
87 else
88 memcpy(buffer[i].name, "too long", 9);
89
90 buffer[i].file = !(entry->attribute&ATTR_DIRECTORY);
91
92 buffer[i].file?(icon_type=File):(icon_type=Folder);
93 lcd_bitmap(bitmap_icons_6x8[icon_type], 6, LINE_Y+i*LINE_HEIGTH, 6,
94 8, TRUE);
95
96 if(len < TREE_MAX_LEN_DISPLAY)
97 lcd_puts(LINE_X, LINE_Y+i*LINE_HEIGTH, buffer[i].name, 0);
98 else {
99 char storage = buffer[i].name[TREE_MAX_LEN_DISPLAY];
100 buffer[i].name[TREE_MAX_LEN_DISPLAY]=0;
101 lcd_puts(LINE_X, LINE_Y+i*LINE_HEIGTH, buffer[i].name, 0);
102 buffer[i].name[TREE_MAX_LEN_DISPLAY]=storage;
103 }
104 81
105 if(++i >= TREE_MAX_ON_SCREEN) 82 if(!dir)
106 break; 83 return -1; /* not a directory */
107 } 84
108 85 i=start;
109 if (entry==0) { 86 *at_end=0; /* Have we displayed the last directory entry? */
110 *at_end=1; 87 while((entry = readdir(dir))) {
111 } else { 88 int len;
112 *at_end=(readdir(dir)==0);
113 }
114 j = i ;
115 while (j++ < TREE_MAX_ON_SCREEN) {
116 lcd_puts(LINE_X, LINE_Y+j*LINE_HEIGTH," ", 0);
117 }
118 closedir(dir);
119
120 return i;
121}
122 89
90 if(entry->d_name[0] == '.')
91 /* skip names starting with a dot */
92 continue;
93
94 if(j++ < scrollpos)
95 continue ;
96
97 len = strlen(entry->d_name);
98 if(len < TREE_MAX_FILENAMELEN)
99 /* strncpy() is evil, we memcpy() instead, +1 includes the
100 trailing zero */
101 memcpy(buffer[i].name, entry->d_name, len+1);
102 else
103 memcpy(buffer[i].name, "too long", 9);
104
105 buffer[i].file = !(entry->attribute&ATTR_DIRECTORY);
106
107#ifdef HAVE_LCD_BITMAP
108 if ( buffer[i].file )
109 icon_type=File;
110 else
111 icon_type=Folder;
112 lcd_bitmap(bitmap_icons_6x8[icon_type], 6, MARGIN_Y+i*LINE_HEIGTH, 6,
113 8, TRUE);
123#endif 114#endif
124 115
116 if(len < TREE_MAX_LEN_DISPLAY)
117 lcd_puts(LINE_X, LINE_Y+i, buffer[i].name);
118 else {
119 char storage = buffer[i].name[TREE_MAX_LEN_DISPLAY];
120 buffer[i].name[TREE_MAX_LEN_DISPLAY]=0;
121 lcd_puts(LINE_X, LINE_Y+i, buffer[i].name);
122 buffer[i].name[TREE_MAX_LEN_DISPLAY]=storage;
123 }
124
125 if(++i >= TREE_MAX_ON_SCREEN)
126 break;
127 }
128
129 if (entry==0) {
130 *at_end=1;
131 } else {
132 *at_end=(readdir(dir)==0);
133 }
134 j = i ;
135 while (j++ < TREE_MAX_ON_SCREEN) {
136 lcd_puts(LINE_X, LINE_Y+j," ");
137 }
138 closedir(dir);
139
140 return i;
141}
142
125bool dirbrowse(char *root) 143bool dirbrowse(char *root)
126{ 144{
127 struct entry buffer[TREE_MAX_ON_SCREEN]; 145 struct entry buffer[TREE_MAX_ON_SCREEN];
128 int numentries; 146 int numentries;
129 char buf[255]; 147 char buf[255];
130 char currdir[255]; 148 char currdir[255];
131 int dircursor=0; 149 int dircursor=0;
132 int i; 150 int i;
133 int start=0; 151 int start=0;
134 int at_end=0; 152 int at_end=0;
135 153
136#ifdef HAVE_LCD_BITMAP 154 lcd_clear_display();
137 lcd_clear_display();
138 155
139 lcd_puts(0,0, "[Browse]", 0); 156#ifdef HAVE_LCD_BITMAP
140 memcpy(currdir,root,sizeof(currdir)); 157 lcd_putsxy(0,0, "[Browse]",0);
158 lcd_setmargins(0,MARGIN_Y);
159 lcd_setfont(0);
160#endif
161 memcpy(currdir,root,sizeof(currdir));
141 162
142 numentries = showdir(root, buffer, 0, start, &at_end); 163 numentries = showdir(root, buffer, 0, start, &at_end);
143 164
144 if (numentries == -1) return -1; /* root is not a directory */ 165 if (numentries == -1)
166 return -1; /* root is not a directory */
145 167
146 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0); 168 lcd_puts(0, dircursor, "-");
169 lcd_update();
147 170
148 lcd_update(); 171 while(1) {
172 int key = button_get();
149 173
150 while(1) { 174 if(!key) {
151 int key = button_get(); 175 sleep(1);
176 continue;
177 }
178 switch(key) {
179 case BUTTON_OFF:
180 return FALSE;
181 break;
182
183 case BUTTON_LEFT:
184 i=strlen(currdir);
185 if (i==1) {
186 return FALSE;
187 }
188 else {
189 while (currdir[i-1]!='/')
190 i--;
191 strcpy(buf,&currdir[i]);
192 if (i==1)
193 currdir[i]=0;
194 else
195 currdir[i-1]=0;
196
197 lcd_clear_display();
198#ifdef HAVE_LCD_BITMAP
199 lcd_putsxy(0,0, "[Browse]",0);
200#endif
201 numentries = showdir(currdir, buffer, 0, 0, &at_end);
202 dircursor=0;
203 start=0;
204 while ( (dircursor < TREE_MAX_ON_SCREEN) &&
205 (strcmp(buffer[dircursor].name,buf)!=0))
206 dircursor++;
207 if (dircursor==TREE_MAX_ON_SCREEN)
208 dircursor=0;
209 lcd_puts(0, LINE_Y+dircursor, "-");
210 lcd_update();
211 }
212
213 break;
214
215 case BUTTON_RIGHT:
216 case BUTTON_PLAY:
217 if ((currdir[0]=='/') && (currdir[1]==0)) {
218 sprintf(buf,"%s%s",currdir,buffer[dircursor].name);
219 } else {
220 sprintf(buf,"%s/%s",currdir,buffer[dircursor].name);
221 }
222
223 if (!buffer[dircursor].file) {
224 memcpy(currdir,buf,sizeof(currdir));
225 dircursor=0;
226 start=0;
227 } else {
228 playtune(currdir, buffer[dircursor].name);
229#ifdef HAVE_LCD_BITMAP
230 lcd_setmargins(0, MARGIN_Y);
231 lcd_setfont(0);
232#endif
233 }
152 234
153 if(!key) { 235 lcd_clear_display();
154 sleep(1); 236 numentries = showdir(currdir, buffer, 0, start, &at_end);
155 continue; 237#ifdef HAVE_LCD_BITMAP
156 } 238 lcd_putsxy(0,0, "[Browse]",0);
157 switch(key) { 239#endif
158 case BUTTON_OFF: 240 lcd_puts(0, LINE_Y+dircursor, "-");
159 return FALSE; 241 lcd_update();
160 break; 242 break;
161
162 case BUTTON_LEFT:
163 i=strlen(currdir);
164 if (i==1) {
165 return FALSE;
166 } else {
167 while (currdir[i-1]!='/') i--;
168 strcpy(buf,&currdir[i]);
169 if (i==1) currdir[i]=0;
170 else currdir[i-1]=0;
171
172 lcd_clear_display();
173 lcd_puts(0,0, "[Browse]", 0);
174 numentries = showdir(currdir, buffer, 0, 0, &at_end);
175 dircursor=0;
176 start=0;
177 while ( (dircursor < TREE_MAX_ON_SCREEN) &&
178 (strcmp(buffer[dircursor].name,buf)!=0)) dircursor++;
179 if (dircursor==TREE_MAX_ON_SCREEN) dircursor=0;
180 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0);
181 lcd_update();
182 }
183
184 break;
185
186 case BUTTON_RIGHT:
187 case BUTTON_PLAY:
188 if ((currdir[0]=='/') && (currdir[1]==0)) {
189 sprintf(buf,"%s%s",currdir,buffer[dircursor].name);
190 } else {
191 sprintf(buf,"%s/%s",currdir,buffer[dircursor].name);
192 }
193
194 if (!buffer[dircursor].file) {
195 memcpy(currdir,buf,sizeof(currdir));
196 dircursor=0;
197 start=0;
198 } else {
199 playtune(currdir, buffer[dircursor].name);
200 }
201
202 lcd_clear_display();
203 lcd_puts(0,0, "[Browse]", 0);
204 numentries = showdir(currdir, buffer, 0, start, &at_end);
205 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0);
206 lcd_update();
207 break;
208 243
209 case BUTTON_UP: 244 case BUTTON_UP:
210 if(dircursor) { 245 if(dircursor) {
211 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, " ", 0); 246 lcd_puts(0, LINE_Y+dircursor, " ");
212 dircursor--; 247 dircursor--;
213 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0); 248 lcd_puts(0, LINE_Y+dircursor, "-");
214 lcd_update(); 249 lcd_update();
215 } else 250 } else
216 { 251 {
217 if (start) { 252 if (start) {
218 lcd_clear_display(); 253 lcd_clear_display();
219 lcd_puts(0,0, "[Browse]", 0); 254 start--;
220 numentries = showdir(currdir, buffer, 0, --start, &at_end); 255 numentries = showdir(currdir, buffer, 0,
221 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0); 256 start, &at_end);
222 lcd_update(); 257#ifdef HAVE_LCD_BITMAP
223 } 258 lcd_putsxy(0,0, "[Browse]",0);
224 } 259#endif
225 break; 260 lcd_puts(0, LINE_Y+dircursor, "-");
226 case BUTTON_DOWN: 261 lcd_update();
227 if(dircursor+1 < numentries) { 262 }
228 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, " ", 0); 263 }
229 dircursor++; 264 break;
230 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0); 265 case BUTTON_DOWN:
231 lcd_update(); 266 if(dircursor+1 < numentries) {
232 } else 267 lcd_puts(0, LINE_Y+dircursor, " ");
233 { 268 dircursor++;
234 if (!at_end) { 269 lcd_puts(0, LINE_Y+dircursor, "-");
235 lcd_clear_display(); 270 lcd_update();
236 lcd_puts(0,0, "[Browse]", 0); 271 } else
237 numentries = showdir(currdir, buffer, 0, ++start, &at_end); 272 {
238 273 if (!at_end) {
239 lcd_puts(0, LINE_Y+dircursor*LINE_HEIGTH, "-", 0); 274 lcd_clear_display();
240 lcd_update(); 275 start++;
276 numentries = showdir(currdir, buffer, 0,
277 start, &at_end);
278#ifdef HAVE_LCD_BITMAP
279 lcd_putsxy(0,0, "[Browse]",0);
280#endif
281 lcd_puts(0, LINE_Y+dircursor, "-");
282 lcd_update();
283 }
284 }
285 break;
241 } 286 }
242 }
243 break;
244 } 287 }
245 }
246#endif
247 288
248 return FALSE; 289 return FALSE;
249} 290}