Player: Save a bit of space by only using 7 bytes/char in the glyph table.
[Rockbox.git] / apps / filetree.c
blob5660a9b75ba72319c8601e70552a663303214a33
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 <limits.h>
28 #include "bookmark.h"
29 #include "tree.h"
30 #include "settings.h"
31 #include "filetypes.h"
32 #include "talk.h"
33 #include "playlist.h"
34 #include "gwps.h"
35 #include "lang.h"
36 #include "language.h"
37 #include "screens.h"
38 #include "plugin.h"
39 #include "rolo.h"
40 #include "sprintf.h"
41 #include "dircache.h"
42 #include "splash.h"
43 #include "yesno.h"
44 #include "cuesheet.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "keyboard.h"
47 #endif
49 #if CONFIG_TUNER
50 #include "radio.h"
51 #endif
53 #ifndef SIMULATOR
54 static int boot_size = 0;
55 static int boot_cluster;
56 #endif
58 #if LCD_DEPTH > 1
59 #include "backdrop.h"
60 #endif
62 extern bool boot_changed;
64 int ft_build_playlist(struct tree_context* c, int start_index)
66 int i;
67 int start=start_index;
69 struct entry *dircache = c->dircache;
71 for(i = 0;i < c->filesindir;i++)
73 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
75 DEBUGF("Adding %s\n", dircache[i].name);
76 if (playlist_add(dircache[i].name) < 0)
77 break;
79 else
81 /* Adjust the start index when se skip non-MP3 entries */
82 if(i < start)
83 start_index--;
87 return start_index;
90 /* walk a directory and check all dircache entries if a .talk file exists */
91 static void check_file_thumbnails(struct tree_context* c)
93 int i;
94 struct dircache_entry *entry;
95 struct entry* dircache = c->dircache;
96 DIRCACHED *dir;
98 dir = opendir_cached(c->currdir);
99 if(!dir)
100 return;
101 /* mark all files as non talking, except the .talk ones */
102 for (i=0; i < c->filesindir; i++)
104 if (dircache[i].attr & ATTR_DIRECTORY)
105 continue; /* we're not touching directories */
107 if (strcasecmp(file_thumbnail_ext,
108 &dircache[i].name[strlen(dircache[i].name)
109 - strlen(file_thumbnail_ext)]))
110 { /* no .talk file */
111 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
113 else
114 { /* .talk file, we later let them speak themselves */
115 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
119 while((entry = readdir_cached(dir)) != 0) /* walk directory */
121 int ext_pos;
123 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
124 if (ext_pos <= 0 /* too short to carry ".talk" */
125 || (entry->attribute & ATTR_DIRECTORY) /* no file */
126 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
127 { /* or doesn't end with ".talk", no candidate */
128 continue;
131 /* terminate the (disposable) name in dir buffer,
132 this truncates off the ".talk" without needing an extra buffer */
133 entry->d_name[ext_pos] = '\0';
135 /* search corresponding file in dir cache */
136 for (i=0; i < c->filesindir; i++)
138 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
139 { /* match */
140 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
141 break; /* exit search loop, because we found it */
145 closedir_cached(dir);
148 /* support function for qsort() */
149 static int compare(const void* p1, const void* p2)
151 struct entry* e1 = (struct entry*)p1;
152 struct entry* e2 = (struct entry*)p2;
153 int criteria;
155 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
156 { /* two directories */
157 criteria = global_settings.sort_dir;
159 #ifdef HAVE_MULTIVOLUME
160 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
161 { /* a volume identifier is involved */
162 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
163 criteria = 0; /* two volumes: sort alphabetically */
164 else /* only one is a volume: volume first */
165 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
167 #endif
170 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
171 { /* two files */
172 criteria = global_settings.sort_file;
174 else /* dir and file, dir goes first */
175 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
177 switch(criteria)
179 case 3: /* sort type */
181 int t1 = e1->attr & TREE_ATTR_MASK;
182 int t2 = e2->attr & TREE_ATTR_MASK;
184 if (!t1) /* unknown type */
185 t1 = INT_MAX; /* gets a high number, to sort after known */
186 if (!t2) /* unknown type */
187 t2 = INT_MAX; /* gets a high number, to sort after known */
189 if (t1 - t2) /* if different */
190 return t1 - t2;
191 /* else fall through to alphabetical sorting */
193 case 0: /* sort alphabetically asc */
194 if (global_settings.sort_case)
195 return strncmp(e1->name, e2->name, MAX_PATH);
196 else
197 return strncasecmp(e1->name, e2->name, MAX_PATH);
199 case 4: /* sort alphabetically desc */
200 if (global_settings.sort_case)
201 return strncmp(e2->name, e1->name, MAX_PATH);
202 else
203 return strncasecmp(e2->name, e1->name, MAX_PATH);
205 case 1: /* sort date */
206 return e1->time_write - e2->time_write;
208 case 2: /* sort date, newest first */
209 return e2->time_write - e1->time_write;
211 return 0; /* never reached */
214 /* load and sort directory into dircache. returns NULL on failure. */
215 int ft_load(struct tree_context* c, const char* tempdir)
217 int i;
218 int name_buffer_used = 0;
219 DIRCACHED *dir;
221 if (tempdir)
222 dir = opendir_cached(tempdir);
223 else
224 dir = opendir_cached(c->currdir);
225 if(!dir)
226 return -1; /* not a directory */
228 c->dirsindir = 0;
229 c->dirfull = false;
231 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
232 int len;
233 struct dircache_entry *entry = readdir_cached(dir);
234 struct entry* dptr =
235 (struct entry*)(c->dircache + i * sizeof(struct entry));
236 if (!entry)
237 break;
239 len = strlen((char *)entry->d_name);
241 /* skip directories . and .. */
242 if ((entry->attribute & ATTR_DIRECTORY) &&
243 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
244 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
245 i--;
246 continue;
249 /* Skip FAT volume ID */
250 if (entry->attribute & ATTR_VOLUME_ID) {
251 i--;
252 continue;
255 /* filter out dotfiles and hidden files */
256 if (*c->dirfilter != SHOW_ALL &&
257 ((entry->d_name[0]=='.') ||
258 (entry->attribute & ATTR_HIDDEN))) {
259 i--;
260 continue;
263 dptr->attr = entry->attribute;
265 /* check for known file types */
266 if ( !(dptr->attr & ATTR_DIRECTORY) )
267 dptr->attr |= filetype_get_attr((char *)entry->d_name);
269 #ifdef BOOTFILE
270 /* memorize/compare details about the boot file */
271 if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
272 if (boot_size) {
273 if ((entry->size != boot_size) ||
274 (entry->startcluster != boot_cluster))
275 boot_changed = true;
277 boot_size = entry->size;
278 boot_cluster = entry->startcluster;
280 #endif
282 /* filter out non-visible files */
283 if ((!(dptr->attr & ATTR_DIRECTORY) && (
284 (*c->dirfilter == SHOW_PLAYLIST &&
285 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
286 ((*c->dirfilter == SHOW_MUSIC &&
287 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
288 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
289 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
290 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
291 #ifdef HAVE_REMOTE_LCD
292 (*c->dirfilter == SHOW_RWPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_RWPS) ||
293 #endif
294 #if CONFIG_TUNER
295 (*c->dirfilter == SHOW_FMR && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FMR) ||
296 #endif
297 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
298 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
299 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
300 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
301 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK))
303 i--;
304 continue;
307 if (len > c->name_buffer_size - name_buffer_used - 1) {
308 /* Tell the world that we ran out of buffer space */
309 c->dirfull = true;
310 break;
312 dptr->name = &c->name_buffer[name_buffer_used];
313 dptr->time_write =
314 (long)entry->wrtdate<<16 |
315 (long)entry->wrttime; /* in one # */
316 strcpy(dptr->name, (char *)entry->d_name);
317 name_buffer_used += len + 1;
319 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
320 c->dirsindir++;
322 c->filesindir = i;
323 c->dirlength = i;
324 closedir_cached(dir);
326 qsort(c->dircache,i,sizeof(struct entry),compare);
328 /* If thumbnail talking is enabled, make an extra run to mark files with
329 associated thumbnails, so we don't do unsuccessful spinups later. */
330 if (global_settings.talk_file == 3)
331 check_file_thumbnails(c); /* map .talk to ours */
333 return 0;
336 int ft_enter(struct tree_context* c)
338 int rc = 0;
339 char buf[MAX_PATH];
340 struct entry *dircache = c->dircache;
341 struct entry* file = &dircache[c->selected_item];
342 bool reload_dir = false;
343 bool start_wps = false;
344 bool exit_func = false;
346 if (c->currdir[1])
347 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
348 else
349 snprintf(buf,sizeof(buf),"/%s",file->name);
351 if (file->attr & ATTR_DIRECTORY) {
352 memcpy(c->currdir, buf, sizeof(c->currdir));
353 if ( c->dirlevel < MAX_DIR_LEVELS )
354 c->selected_item_history[c->dirlevel] = c->selected_item;
355 c->dirlevel++;
356 c->selected_item=0;
358 else {
359 int seed = current_tick;
360 bool play = false;
361 int start_index=0;
363 switch ( file->attr & TREE_ATTR_MASK ) {
364 case TREE_ATTR_M3U:
365 if (global_settings.party_mode) {
366 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
367 break;
370 if (bookmark_autoload(buf))
371 break;
373 gui_syncsplash(0, str(LANG_WAIT));
375 /* about to create a new current playlist...
376 allow user to cancel the operation */
377 if (global_settings.warnon_erase_dynplaylist &&
378 playlist_modified(NULL))
380 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
381 struct text_message message={lines, 1};
383 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
384 break;
387 if (playlist_create(c->currdir, file->name) != -1)
389 if (global_settings.playlist_shuffle)
390 playlist_shuffle(seed, -1);
391 start_index = 0;
392 playlist_start(start_index,0);
393 play = true;
395 break;
397 case TREE_ATTR_MPA:
398 if (bookmark_autoload(c->currdir))
399 break;
401 gui_syncsplash(0, str(LANG_WAIT));
403 /* about to create a new current playlist...
404 allow user to cancel the operation */
405 if (global_settings.warnon_erase_dynplaylist &&
406 !global_settings.party_mode &&
407 playlist_modified(NULL))
409 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
410 struct text_message message={lines, 1};
412 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
413 break;
416 if (global_settings.party_mode)
418 playlist_insert_track(NULL, buf,
419 PLAYLIST_INSERT_LAST, true, true);
420 gui_syncsplash(HZ, str(LANG_QUEUE_LAST));
422 else if (playlist_create(c->currdir, NULL) != -1)
424 start_index = ft_build_playlist(c, c->selected_item);
425 if (global_settings.playlist_shuffle)
427 start_index = playlist_shuffle(seed, start_index);
429 /* when shuffling dir.: play all files
430 even if the file selected by user is
431 not the first one */
432 if (!global_settings.play_selected)
433 start_index = 0;
436 playlist_start(start_index, 0);
437 play = true;
439 break;
441 #if CONFIG_TUNER
442 /* fmr preset file */
443 case TREE_ATTR_FMR:
445 gui_syncsplash(0, str(LANG_WAIT));
447 /* Preset inside the default folder. */
448 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
450 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
451 radio_load_presets(global_settings.fmr_file);
452 if(!in_radio_screen())
453 radio_screen();
456 * Preset outside default folder, we can choose such only
457 * if we are out of the radio screen, so the check for the
458 * radio status isn't neccessary
460 else
462 radio_load_presets(buf);
463 radio_screen();
466 break;
467 #endif
470 /* wps config file */
471 case TREE_ATTR_WPS:
472 gui_syncsplash(0, str(LANG_WAIT));
473 #if LCD_DEPTH > 1
474 unload_wps_backdrop();
475 #endif
476 wps_data_load(gui_wps[0].data, buf, true);
477 set_file(buf, (char *)global_settings.wps_file,
478 MAX_FILENAME);
479 break;
481 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
482 /* remote-wps config file */
483 case TREE_ATTR_RWPS:
484 gui_syncsplash(0, str(LANG_WAIT));
485 wps_data_load(gui_wps[1].data, buf, true);
486 set_file(buf, (char *)global_settings.rwps_file,
487 MAX_FILENAME);
488 break;
489 #endif
491 case TREE_ATTR_CFG:
492 gui_syncsplash(0, str(LANG_WAIT));
493 if (!settings_load_config(buf,true))
494 break;
495 gui_syncsplash(HZ, str(LANG_SETTINGS_LOADED));
496 break;
498 case TREE_ATTR_BMARK:
499 gui_syncsplash(0, str(LANG_WAIT));
500 bookmark_load(buf, false);
501 reload_dir = true;
502 break;
504 case TREE_ATTR_LNG:
505 gui_syncsplash(0, str(LANG_WAIT));
506 if(!lang_load(buf)) {
507 set_file(buf, (char *)global_settings.lang_file,
508 MAX_FILENAME);
509 talk_init(); /* use voice of same language */
510 gui_syncsplash(HZ, str(LANG_LANGUAGE_LOADED));
512 break;
514 #ifdef HAVE_LCD_BITMAP
515 case TREE_ATTR_FONT:
516 gui_syncsplash(0, str(LANG_WAIT));
517 font_load(buf);
518 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
519 break;
521 case TREE_ATTR_KBD:
522 gui_syncsplash(0, str(LANG_WAIT));
523 if (!load_kbd(buf))
524 gui_syncsplash(HZ, str(LANG_KEYBOARD_LOADED));
525 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
526 break;
527 #endif
529 #ifndef SIMULATOR
530 /* firmware file */
531 case TREE_ATTR_MOD:
532 gui_syncsplash(0, str(LANG_WAIT));
533 rolo_load(buf);
534 break;
535 #endif
537 /* plugin file */
538 case TREE_ATTR_ROCK:
539 if (global_settings.party_mode) {
540 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
541 break;
544 gui_syncsplash(0, str(LANG_WAIT));
546 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
548 if(*c->dirfilter > NUM_FILTER_MODES)
549 /* leave sub-browsers after usb, doing
550 otherwise might be confusing to the user */
551 exit_func = true;
552 else
553 reload_dir = true;
555 break;
557 case TREE_ATTR_CUE:
558 display_cuesheet_content(buf);
559 break;
561 default:
563 char* plugin;
565 if (global_settings.party_mode) {
566 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
567 break;
570 plugin = filetype_get_plugin(file);
571 if (plugin)
573 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
574 reload_dir = true;
576 break;
580 if ( play ) {
581 /* the resume_index must always be the index in the
582 shuffled list in case shuffle is enabled */
583 global_status.resume_index = start_index;
584 global_status.resume_offset = 0;
585 status_save();
587 start_wps = true;
589 else {
590 if (*c->dirfilter > NUM_FILTER_MODES &&
591 *c->dirfilter != SHOW_FONT &&
592 *c->dirfilter != SHOW_PLUGINS)
594 exit_func = true;
599 if (reload_dir)
600 rc = 1;
601 if (start_wps)
602 rc = 2;
603 if (exit_func)
604 rc = 3;
606 return rc;
609 int ft_exit(struct tree_context* c)
611 extern char lastfile[]; /* from tree.c */
612 char buf[MAX_PATH];
613 int rc = 0;
614 bool exit_func = false;
616 int i = strlen(c->currdir);
617 if (i>1) {
618 while (c->currdir[i-1]!='/')
619 i--;
620 strcpy(buf,&c->currdir[i]);
621 if (i==1)
622 c->currdir[i]=0;
623 else
624 c->currdir[i-1]=0;
626 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
627 exit_func = true;
629 c->dirlevel--;
630 if ( c->dirlevel < MAX_DIR_LEVELS )
631 c->selected_item=c->selected_item_history[c->dirlevel];
632 else
633 c->selected_item=0;
635 /* if undefined position */
636 if (c->selected_item == -1)
637 strcpy(lastfile, buf);
639 else
641 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
642 exit_func = true;
645 if (exit_func)
646 rc = 3;
648 return rc;