Accept FS#7228 by Dagni McPhee enable pitchscreen on sansa
[Rockbox.git] / apps / filetree.c
blobaea81a71d474154fc47084d4a206071a0086ca33
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 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
54 #include "backdrop.h"
55 #endif
57 int ft_build_playlist(struct tree_context* c, int start_index)
59 int i;
60 int start=start_index;
62 struct entry *dircache = c->dircache;
64 for(i = 0;i < c->filesindir;i++)
66 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
68 DEBUGF("Adding %s\n", dircache[i].name);
69 if (playlist_add(dircache[i].name) < 0)
70 break;
72 else
74 /* Adjust the start index when se skip non-MP3 entries */
75 if(i < start)
76 start_index--;
80 return start_index;
83 /* walk a directory and check all dircache entries if a .talk file exists */
84 static void check_file_thumbnails(struct tree_context* c)
86 int i;
87 struct dircache_entry *entry;
88 struct entry* dircache = c->dircache;
89 DIRCACHED *dir;
91 dir = opendir_cached(c->currdir);
92 if(!dir)
93 return;
94 /* mark all files as non talking, except the .talk ones */
95 for (i=0; i < c->filesindir; i++)
97 if (dircache[i].attr & ATTR_DIRECTORY)
98 continue; /* we're not touching directories */
100 if (strcasecmp(file_thumbnail_ext,
101 &dircache[i].name[strlen(dircache[i].name)
102 - strlen(file_thumbnail_ext)]))
103 { /* no .talk file */
104 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
106 else
107 { /* .talk file, we later let them speak themselves */
108 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
112 while((entry = readdir_cached(dir)) != 0) /* walk directory */
114 int ext_pos;
116 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
117 if (ext_pos <= 0 /* too short to carry ".talk" */
118 || (entry->attribute & ATTR_DIRECTORY) /* no file */
119 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
120 { /* or doesn't end with ".talk", no candidate */
121 continue;
124 /* terminate the (disposable) name in dir buffer,
125 this truncates off the ".talk" without needing an extra buffer */
126 entry->d_name[ext_pos] = '\0';
128 /* search corresponding file in dir cache */
129 for (i=0; i < c->filesindir; i++)
131 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
132 { /* match */
133 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
134 break; /* exit search loop, because we found it */
138 closedir_cached(dir);
141 /* support function for qsort() */
142 static int compare(const void* p1, const void* p2)
144 struct entry* e1 = (struct entry*)p1;
145 struct entry* e2 = (struct entry*)p2;
146 int criteria;
148 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
149 { /* two directories */
150 criteria = global_settings.sort_dir;
152 #ifdef HAVE_MULTIVOLUME
153 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
154 { /* a volume identifier is involved */
155 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
156 criteria = 0; /* two volumes: sort alphabetically */
157 else /* only one is a volume: volume first */
158 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
160 #endif
163 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
164 { /* two files */
165 criteria = global_settings.sort_file;
167 else /* dir and file, dir goes first */
168 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
170 switch(criteria)
172 case 3: /* sort type */
174 int t1 = e1->attr & FILE_ATTR_MASK;
175 int t2 = e2->attr & FILE_ATTR_MASK;
177 if (!t1) /* unknown type */
178 t1 = INT_MAX; /* gets a high number, to sort after known */
179 if (!t2) /* unknown type */
180 t2 = INT_MAX; /* gets a high number, to sort after known */
182 if (t1 - t2) /* if different */
183 return t1 - t2;
184 /* else fall through to alphabetical sorting */
186 case 0: /* sort alphabetically asc */
187 if (global_settings.sort_case)
188 return strncmp(e1->name, e2->name, MAX_PATH);
189 else
190 return strncasecmp(e1->name, e2->name, MAX_PATH);
192 case 4: /* sort alphabetically desc */
193 if (global_settings.sort_case)
194 return strncmp(e2->name, e1->name, MAX_PATH);
195 else
196 return strncasecmp(e2->name, e1->name, MAX_PATH);
198 case 1: /* sort date */
199 return e1->time_write - e2->time_write;
201 case 2: /* sort date, newest first */
202 return e2->time_write - e1->time_write;
204 return 0; /* never reached */
207 /* load and sort directory into dircache. returns NULL on failure. */
208 int ft_load(struct tree_context* c, const char* tempdir)
210 int i;
211 int name_buffer_used = 0;
212 DIRCACHED *dir;
214 if (tempdir)
215 dir = opendir_cached(tempdir);
216 else
217 dir = opendir_cached(c->currdir);
218 if(!dir)
219 return -1; /* not a directory */
221 c->dirsindir = 0;
222 c->dirfull = false;
224 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
225 int len;
226 struct dircache_entry *entry = readdir_cached(dir);
227 struct entry* dptr =
228 (struct entry*)(c->dircache + i * sizeof(struct entry));
229 if (!entry)
230 break;
232 len = strlen((char *)entry->d_name);
234 /* skip directories . and .. */
235 if ((entry->attribute & ATTR_DIRECTORY) &&
236 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
237 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
238 i--;
239 continue;
242 /* Skip FAT volume ID */
243 if (entry->attribute & ATTR_VOLUME_ID) {
244 i--;
245 continue;
248 /* filter out dotfiles and hidden files */
249 if (*c->dirfilter != SHOW_ALL &&
250 ((entry->d_name[0]=='.') ||
251 (entry->attribute & ATTR_HIDDEN))) {
252 i--;
253 continue;
256 dptr->attr = entry->attribute;
258 /* check for known file types */
259 if ( !(dptr->attr & ATTR_DIRECTORY) )
260 dptr->attr |= filetype_get_attr((char *)entry->d_name);
262 /* filter out non-visible files */
263 if ((!(dptr->attr & ATTR_DIRECTORY) && (
264 (*c->dirfilter == SHOW_PLAYLIST &&
265 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
266 ((*c->dirfilter == SHOW_MUSIC &&
267 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
268 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
269 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
270 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
271 #ifdef HAVE_REMOTE_LCD
272 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
273 #endif
274 #if CONFIG_TUNER
275 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
276 #endif
277 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
278 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
279 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
280 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
281 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
283 i--;
284 continue;
287 if (len > c->name_buffer_size - name_buffer_used - 1) {
288 /* Tell the world that we ran out of buffer space */
289 c->dirfull = true;
290 break;
292 dptr->name = &c->name_buffer[name_buffer_used];
293 dptr->time_write =
294 (long)entry->wrtdate<<16 |
295 (long)entry->wrttime; /* in one # */
296 strcpy(dptr->name, (char *)entry->d_name);
297 name_buffer_used += len + 1;
299 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
300 c->dirsindir++;
302 c->filesindir = i;
303 c->dirlength = i;
304 closedir_cached(dir);
306 qsort(c->dircache,i,sizeof(struct entry),compare);
308 /* If thumbnail talking is enabled, make an extra run to mark files with
309 associated thumbnails, so we don't do unsuccessful spinups later. */
310 if (global_settings.talk_file == 3)
311 check_file_thumbnails(c); /* map .talk to ours */
313 return 0;
316 int ft_enter(struct tree_context* c)
318 int rc = 0;
319 char buf[MAX_PATH];
320 struct entry *dircache = c->dircache;
321 struct entry* file = &dircache[c->selected_item];
322 bool reload_dir = false;
323 bool start_wps = false;
324 bool exit_func = false;
326 if (c->currdir[1])
327 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
328 else
329 snprintf(buf,sizeof(buf),"/%s",file->name);
331 if (file->attr & ATTR_DIRECTORY) {
332 memcpy(c->currdir, buf, sizeof(c->currdir));
333 if ( c->dirlevel < MAX_DIR_LEVELS )
334 c->selected_item_history[c->dirlevel] = c->selected_item;
335 c->dirlevel++;
336 c->selected_item=0;
338 else {
339 int seed = current_tick;
340 bool play = false;
341 int start_index=0;
343 switch ( file->attr & FILE_ATTR_MASK ) {
344 case FILE_ATTR_M3U:
345 if (global_settings.party_mode) {
346 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
347 break;
350 if (bookmark_autoload(buf))
351 break;
353 gui_syncsplash(0, str(LANG_WAIT));
355 /* about to create a new current playlist...
356 allow user to cancel the operation */
357 if (global_settings.warnon_erase_dynplaylist &&
358 playlist_modified(NULL))
360 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
361 struct text_message message={lines, 1};
363 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
364 break;
367 if (playlist_create(c->currdir, file->name) != -1)
369 if (global_settings.playlist_shuffle)
370 playlist_shuffle(seed, -1);
371 start_index = 0;
372 playlist_start(start_index,0);
373 play = true;
375 break;
377 case FILE_ATTR_AUDIO:
378 if (bookmark_autoload(c->currdir))
379 break;
381 gui_syncsplash(0, str(LANG_WAIT));
383 /* about to create a new current playlist...
384 allow user to cancel the operation */
385 if (global_settings.warnon_erase_dynplaylist &&
386 !global_settings.party_mode &&
387 playlist_modified(NULL))
389 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
390 struct text_message message={lines, 1};
392 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
393 break;
396 if (global_settings.party_mode)
398 playlist_insert_track(NULL, buf,
399 PLAYLIST_INSERT_LAST, true, true);
400 gui_syncsplash(HZ, str(LANG_QUEUE_LAST));
402 else if (playlist_create(c->currdir, NULL) != -1)
404 start_index = ft_build_playlist(c, c->selected_item);
405 if (global_settings.playlist_shuffle)
407 start_index = playlist_shuffle(seed, start_index);
409 /* when shuffling dir.: play all files
410 even if the file selected by user is
411 not the first one */
412 if (!global_settings.play_selected)
413 start_index = 0;
416 playlist_start(start_index, 0);
417 play = true;
419 break;
421 #if CONFIG_TUNER
422 /* fmr preset file */
423 case FILE_ATTR_FMR:
425 gui_syncsplash(0, str(LANG_WAIT));
427 /* Preset inside the default folder. */
428 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
430 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
431 radio_load_presets(global_settings.fmr_file);
432 if(!in_radio_screen())
433 radio_screen();
436 * Preset outside default folder, we can choose such only
437 * if we are out of the radio screen, so the check for the
438 * radio status isn't neccessary
440 else
442 radio_load_presets(buf);
443 radio_screen();
446 break;
447 #endif
450 /* wps config file */
451 case FILE_ATTR_WPS:
452 gui_syncsplash(0, str(LANG_WAIT));
453 #if LCD_DEPTH > 1
454 unload_wps_backdrop();
455 #endif
456 wps_data_load(gui_wps[0].data, buf, true);
457 set_file(buf, (char *)global_settings.wps_file,
458 MAX_FILENAME);
459 break;
461 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
462 /* remote-wps config file */
463 case FILE_ATTR_RWPS:
464 gui_syncsplash(0, str(LANG_WAIT));
465 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
466 unload_remote_wps_backdrop();
467 #endif
468 wps_data_load(gui_wps[1].data, buf, true);
469 set_file(buf, (char *)global_settings.rwps_file,
470 MAX_FILENAME);
471 break;
472 #endif
474 case FILE_ATTR_CFG:
475 gui_syncsplash(0, str(LANG_WAIT));
476 if (!settings_load_config(buf,true))
477 break;
478 gui_syncsplash(HZ, str(LANG_SETTINGS_LOADED));
479 break;
481 case FILE_ATTR_BMARK:
482 gui_syncsplash(0, str(LANG_WAIT));
483 bookmark_load(buf, false);
484 reload_dir = true;
485 break;
487 case FILE_ATTR_LNG:
488 gui_syncsplash(0, str(LANG_WAIT));
489 if(!lang_load(buf)) {
490 set_file(buf, (char *)global_settings.lang_file,
491 MAX_FILENAME);
492 talk_init(); /* use voice of same language */
493 gui_syncsplash(HZ, str(LANG_LANGUAGE_LOADED));
495 break;
497 #ifdef HAVE_LCD_BITMAP
498 case FILE_ATTR_FONT:
499 gui_syncsplash(0, str(LANG_WAIT));
500 font_load(buf);
501 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
502 break;
504 case FILE_ATTR_KBD:
505 gui_syncsplash(0, str(LANG_WAIT));
506 if (!load_kbd(buf))
507 gui_syncsplash(HZ, str(LANG_KEYBOARD_LOADED));
508 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
509 break;
510 #endif
512 #ifndef SIMULATOR
513 /* firmware file */
514 case FILE_ATTR_MOD:
515 gui_syncsplash(0, str(LANG_WAIT));
516 rolo_load(buf);
517 break;
518 #endif
520 /* plugin file */
521 case FILE_ATTR_ROCK:
522 if (global_settings.party_mode) {
523 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
524 break;
527 gui_syncsplash(0, str(LANG_WAIT));
529 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
531 if(*c->dirfilter > NUM_FILTER_MODES)
532 /* leave sub-browsers after usb, doing
533 otherwise might be confusing to the user */
534 exit_func = true;
535 else
536 reload_dir = true;
538 break;
540 case FILE_ATTR_CUE:
541 display_cuesheet_content(buf);
542 break;
544 default:
546 char* plugin;
548 if (global_settings.party_mode) {
549 gui_syncsplash(HZ, str(LANG_PARTY_MODE));
550 break;
553 plugin = filetype_get_plugin(file);
554 if (plugin)
556 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
557 reload_dir = true;
559 break;
563 if ( play ) {
564 /* the resume_index must always be the index in the
565 shuffled list in case shuffle is enabled */
566 global_status.resume_index = start_index;
567 global_status.resume_offset = 0;
568 status_save();
570 start_wps = true;
572 else {
573 if (*c->dirfilter > NUM_FILTER_MODES &&
574 *c->dirfilter != SHOW_FONT &&
575 *c->dirfilter != SHOW_PLUGINS)
577 exit_func = true;
582 if (reload_dir)
583 rc = 1;
584 if (start_wps)
585 rc = 2;
586 if (exit_func)
587 rc = 3;
589 return rc;
592 int ft_exit(struct tree_context* c)
594 extern char lastfile[]; /* from tree.c */
595 char buf[MAX_PATH];
596 int rc = 0;
597 bool exit_func = false;
599 int i = strlen(c->currdir);
600 if (i>1) {
601 while (c->currdir[i-1]!='/')
602 i--;
603 strcpy(buf,&c->currdir[i]);
604 if (i==1)
605 c->currdir[i]=0;
606 else
607 c->currdir[i-1]=0;
609 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
610 exit_func = true;
612 c->dirlevel--;
613 if ( c->dirlevel < MAX_DIR_LEVELS )
614 c->selected_item=c->selected_item_history[c->dirlevel];
615 else
616 c->selected_item=0;
618 /* if undefined position */
619 if (c->selected_item == -1)
620 strcpy(lastfile, buf);
622 else
624 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
625 exit_func = true;
628 if (exit_func)
629 rc = 3;
631 return rc;