Repaired broken resume
[kugel-rb.git] / apps / filetree.c
blob324266e64fe0dfc5db98ba0ce17e55dd1511e782
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdlib.h>
20 #include <file.h>
21 #include <dir.h>
22 #include <string.h>
23 #include <kernel.h>
24 #include <lcd.h>
25 #include <debug.h>
26 #include <font.h>
27 #include "bookmark.h"
28 #include "tree.h"
29 #include "settings.h"
30 #include "filetypes.h"
31 #include "talk.h"
32 #include "playlist.h"
33 #include "wps-display.h"
34 #include "lang.h"
35 #include "language.h"
36 #include "screens.h"
37 #include "plugin.h"
38 #include "rolo.h"
39 #include "sprintf.h"
41 static int boot_size = 0;
42 static int boot_cluster;
43 extern bool boot_changed;
45 int ft_build_playlist(struct tree_context* c, int start_index)
47 int i;
48 int start=start_index;
50 struct entry *dircache = c->dircache;
52 for(i = 0;i < c->filesindir;i++)
54 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
56 DEBUGF("Adding %s\n", dircache[i].name);
57 if (playlist_add(dircache[i].name) < 0)
58 break;
60 else
62 /* Adjust the start index when se skip non-MP3 entries */
63 if(i < start)
64 start_index--;
68 return start_index;
71 /* walk a directory and check all dircache entries if a .talk file exists */
72 static void check_file_thumbnails(struct tree_context* c)
74 int i;
75 struct dirent *entry;
76 struct entry* dircache = c->dircache;
77 DIR *dir;
79 dir = opendir(c->currdir);
80 if(!dir)
81 return;
83 for (i=0; i < c->filesindir; i++) /* mark all files as non talking, except the .talk ones */
85 if (dircache[i].attr & ATTR_DIRECTORY)
86 continue; /* we're not touching directories */
88 if (strcasecmp(file_thumbnail_ext,
89 &dircache[i].name[strlen(dircache[i].name)
90 - strlen(file_thumbnail_ext)]))
91 { /* no .talk file */
92 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
94 else
95 { /* .talk file, we later let them speak themselves */
96 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
100 while((entry = readdir(dir)) != 0) /* walk directory */
102 int ext_pos;
104 ext_pos = strlen(entry->d_name) - strlen(file_thumbnail_ext);
105 if (ext_pos <= 0 /* too short to carry ".talk" */
106 || (entry->attribute & ATTR_DIRECTORY) /* no file */
107 || strcasecmp(&entry->d_name[ext_pos], file_thumbnail_ext))
108 { /* or doesn't end with ".talk", no candidate */
109 continue;
112 /* terminate the (disposable) name in dir buffer,
113 this truncates off the ".talk" without needing an extra buffer */
114 entry->d_name[ext_pos] = '\0';
116 /* search corresponding file in dir cache */
117 for (i=0; i < c->filesindir; i++)
119 if (!strcasecmp(dircache[i].name, entry->d_name))
120 { /* match */
121 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
122 break; /* exit search loop, because we found it */
126 closedir(dir);
129 /* support function for qsort() */
130 static int compare(const void* p1, const void* p2)
132 struct entry* e1 = (struct entry*)p1;
133 struct entry* e2 = (struct entry*)p2;
134 int criteria;
136 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
137 { /* two directories */
138 criteria = global_settings.sort_dir;
140 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
141 { /* a volume identifier is involved */
142 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
143 criteria = 0; /* two volumes: sort alphabetically */
144 else /* only one is a volume: volume first */
145 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
148 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
149 { /* two files */
150 criteria = global_settings.sort_file;
152 else /* dir and file, dir goes first */
153 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
155 switch(criteria)
157 case 3: /* sort type */
159 int t1 = e1->attr & TREE_ATTR_MASK;
160 int t2 = e2->attr & TREE_ATTR_MASK;
162 if (!t1) /* unknown type */
163 t1 = 0x7FFFFFFF; /* gets a high number, to sort after known */
164 if (!t2) /* unknown type */
165 t2 = 0x7FFFFFFF; /* gets a high number, to sort after known */
167 if (t1 - t2) /* if different */
168 return t1 - t2;
169 /* else fall through to alphabetical sorting */
171 case 0: /* sort alphabetically */
172 if (global_settings.sort_case)
173 return strncmp(e1->name, e2->name, MAX_PATH);
174 else
175 return strncasecmp(e1->name, e2->name, MAX_PATH);
177 case 1: /* sort date */
178 return e1->time_write - e2->time_write;
180 case 2: /* sort date, newest first */
181 return e2->time_write - e1->time_write;
183 return 0; /* never reached */
186 /* load and sort directory into dircache. returns NULL on failure. */
187 int ft_load(struct tree_context* c, const char* tempdir)
189 int i;
190 int name_buffer_used = 0;
191 DIR *dir;
193 if (tempdir)
194 dir = opendir(tempdir);
195 else
196 dir = opendir(c->currdir);
197 if(!dir)
198 return -1; /* not a directory */
200 c->dirsindir = 0;
201 c->dirfull = false;
203 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
204 int len;
205 struct dirent *entry = readdir(dir);
206 struct entry* dptr =
207 (struct entry*)(c->dircache + i * sizeof(struct entry));
208 if (!entry)
209 break;
211 len = strlen(entry->d_name);
213 /* skip directories . and .. */
214 if ((entry->attribute & ATTR_DIRECTORY) &&
215 (((len == 1) &&
216 (!strncmp(entry->d_name, ".", 1))) ||
217 ((len == 2) &&
218 (!strncmp(entry->d_name, "..", 2))))) {
219 i--;
220 continue;
223 /* Skip FAT volume ID */
224 if (entry->attribute & ATTR_VOLUME_ID) {
225 i--;
226 continue;
229 /* filter out dotfiles and hidden files */
230 if (*c->dirfilter != SHOW_ALL &&
231 ((entry->d_name[0]=='.') ||
232 (entry->attribute & ATTR_HIDDEN))) {
233 i--;
234 continue;
237 dptr->attr = entry->attribute;
239 /* check for known file types */
240 if ( !(dptr->attr & ATTR_DIRECTORY) && (len > 4) )
241 dptr->attr |= filetype_get_attr(entry->d_name);
243 /* memorize/compare details about the boot file */
244 if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
245 if (boot_size) {
246 if ((entry->size != boot_size) ||
247 (entry->startcluster != boot_cluster))
248 boot_changed = true;
250 boot_size = entry->size;
251 boot_cluster = entry->startcluster;
254 /* filter out non-visible files */
255 if (!(dptr->attr & ATTR_DIRECTORY) && (
256 (*c->dirfilter == SHOW_PLAYLIST &&
257 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
258 ((*c->dirfilter == SHOW_MUSIC &&
259 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
260 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
261 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)) ||
262 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
263 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
264 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
265 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
266 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
267 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK)))
269 i--;
270 continue;
273 if (len > c->name_buffer_size - name_buffer_used - 1) {
274 /* Tell the world that we ran out of buffer space */
275 c->dirfull = true;
276 break;
278 dptr->name = &c->name_buffer[name_buffer_used];
279 dptr->time_write = entry->wrtdate<<16 | entry->wrttime; /* in one # */
280 strcpy(dptr->name,entry->d_name);
281 name_buffer_used += len + 1;
283 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
284 c->dirsindir++;
286 c->filesindir = i;
287 c->dirlength = i;
288 closedir(dir);
290 qsort(c->dircache,i,sizeof(struct entry),compare);
292 /* If thumbnail talking is enabled, make an extra run to mark files with
293 associated thumbnails, so we don't do unsuccessful spinups later. */
294 if (global_settings.talk_file == 3)
295 check_file_thumbnails(c); /* map .talk to ours */
297 return 0;
300 int ft_enter(struct tree_context* c)
302 int rc = 0;
303 char buf[MAX_PATH];
304 struct entry *dircache = c->dircache;
305 struct entry* file = &dircache[c->dircursor + c->dirstart];
306 bool reload_root = false;
307 bool reload_dir = false;
308 bool start_wps = false;
309 bool exit_func = false;
311 if (c->currdir[1])
312 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
313 else
314 snprintf(buf,sizeof(buf),"/%s",file->name);
316 if (file->attr & ATTR_DIRECTORY) {
317 memcpy(c->currdir, buf, sizeof(c->currdir));
318 if ( c->dirlevel < MAX_DIR_LEVELS ) {
319 c->dirpos[c->dirlevel] = c->dirstart;
320 c->cursorpos[c->dirlevel] = c->dircursor;
322 c->dirlevel++;
323 c->dircursor=0;
324 c->dirstart=0;
326 else {
327 int seed = current_tick;
328 bool play = false;
329 int start_index=0;
331 lcd_stop_scroll();
332 switch ( file->attr & TREE_ATTR_MASK ) {
333 case TREE_ATTR_M3U:
334 if (bookmark_autoload(buf))
335 break;
337 if (playlist_create(c->currdir, file->name) != -1)
339 if (global_settings.playlist_shuffle)
340 playlist_shuffle(seed, -1);
341 start_index = 0;
342 playlist_start(start_index,0);
343 play = true;
345 break;
347 case TREE_ATTR_MPA:
348 if (bookmark_autoload(c->currdir))
349 break;
351 if (playlist_create(c->currdir, NULL) != -1)
353 start_index =
354 ft_build_playlist(c, c->dircursor + c->dirstart);
355 if (global_settings.playlist_shuffle)
357 start_index = playlist_shuffle(seed, start_index);
359 /* when shuffling dir.: play all files
360 even if the file selected by user is
361 not the first one */
362 if (!global_settings.play_selected)
363 start_index = 0;
366 playlist_start(start_index, 0);
367 play = true;
369 break;
371 /* wps config file */
372 case TREE_ATTR_WPS:
373 wps_load(buf,true);
374 set_file(buf, global_settings.wps_file,
375 MAX_FILENAME);
376 break;
378 case TREE_ATTR_CFG:
379 if (!settings_load_config(buf))
380 break;
381 lcd_clear_display();
382 lcd_puts(0,0,str(LANG_SETTINGS_LOADED1));
383 lcd_puts(0,1,str(LANG_SETTINGS_LOADED2));
384 #ifdef HAVE_LCD_BITMAP
385 lcd_update();
386 #endif
387 sleep(HZ/2);
388 break;
390 case TREE_ATTR_BMARK:
391 bookmark_load(buf, false);
392 reload_dir = true;
393 break;
395 case TREE_ATTR_LNG:
396 if(!lang_load(buf)) {
397 set_file(buf, global_settings.lang_file,
398 MAX_FILENAME);
399 talk_init(); /* use voice of same language */
400 splash(HZ, true, str(LANG_LANGUAGE_LOADED));
402 break;
404 #ifdef HAVE_LCD_BITMAP
405 case TREE_ATTR_FONT:
406 font_load(buf);
407 set_file(buf, global_settings.font_file, MAX_FILENAME);
408 break;
409 #endif
411 #ifndef SIMULATOR
412 /* firmware file */
413 case TREE_ATTR_MOD:
414 rolo_load(buf);
415 break;
416 #endif
418 /* plugin file */
419 case TREE_ATTR_ROCK:
420 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
422 if(*c->dirfilter > NUM_FILTER_MODES)
423 /* leave sub-browsers after usb, doing
424 otherwise might be confusing to the user */
425 exit_func = true;
426 else
427 reload_root = true;
429 break;
431 default:
433 char* plugin = filetype_get_plugin(file);
434 if (plugin)
436 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
437 reload_root = true;
439 break;
443 if ( play ) {
444 if ( global_settings.resume ) {
445 /* the resume_index must always be the index in the
446 shuffled list in case shuffle is enabled */
447 global_settings.resume_index = start_index;
448 global_settings.resume_offset = 0;
449 settings_save();
452 start_wps = true;
454 else {
455 if (*c->dirfilter > NUM_FILTER_MODES &&
456 *c->dirfilter != SHOW_FONT &&
457 *c->dirfilter != SHOW_PLUGINS)
459 exit_func = true;
464 if (reload_dir)
465 rc = 1;
466 if (reload_root)
467 rc = 2;
468 if (start_wps)
469 rc = 3;
470 if (exit_func)
471 rc = 4;
473 return rc;
476 int ft_exit(struct tree_context* c)
478 extern char lastfile[]; /* from tree.c */
479 char buf[MAX_PATH];
480 int rc = 0;
481 bool exit_func = false;
483 int i = strlen(c->currdir);
484 if (i>1) {
485 while (c->currdir[i-1]!='/')
486 i--;
487 strcpy(buf,&c->currdir[i]);
488 if (i==1)
489 c->currdir[i]=0;
490 else
491 c->currdir[i-1]=0;
493 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
494 exit_func = true;
496 c->dirlevel--;
497 if ( c->dirlevel < MAX_DIR_LEVELS ) {
498 c->dirstart = c->dirpos[c->dirlevel];
499 c->dircursor = c->cursorpos[c->dirlevel];
501 else
502 c->dirstart = c->dircursor = 0;
504 if (c->dirstart == -1)
505 strcpy(lastfile, buf);
507 else
509 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
510 exit_func = true;
513 if (exit_func)
514 rc = 4;
516 return rc;