Rename variables sectorbuf and verbose to avoid clashes in rbutil. Cleanup exports...
[Rockbox.git] / apps / filetree.c
blob4f14e48104586af914cfea0be4fcbdf2d5f68290
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 "splash.h"
42 #include "yesno.h"
43 #include "cuesheet.h"
44 #include "filetree.h"
45 #include "misc.h"
46 #ifdef HAVE_LCD_BITMAP
47 #include "keyboard.h"
48 #endif
50 #if CONFIG_TUNER
51 #include "radio.h"
52 #endif
54 #include "backdrop.h"
56 int ft_build_playlist(struct tree_context* c, int start_index)
58 int i;
59 int start=start_index;
61 struct entry *dircache = c->dircache;
63 for(i = 0;i < c->filesindir;i++)
65 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
67 if (playlist_add(dircache[i].name) < 0)
68 break;
70 else
72 /* Adjust the start index when se skip non-MP3 entries */
73 if(i < start)
74 start_index--;
78 return start_index;
81 /* Start playback of a playlist, checking for bookmark autoload, modified
82 * playlists, etc., as required. Returns false if playback wasn't started,
83 * or started via bookmark autoload, true otherwise.
85 * Pointers to both the full pathname and the separated parts needed to
86 * avoid allocating yet another path buffer on the stack (and save some
87 * code; the caller typically needs to create the full pathname anyway)...
89 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
91 if (global_settings.party_mode)
93 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
94 return false;
97 if (bookmark_autoload(pathname))
99 return false;
102 gui_syncsplash(0, ID2P(LANG_WAIT));
104 /* about to create a new current playlist...
105 allow user to cancel the operation */
106 if (!warn_on_pl_erase())
107 return false;
109 if (playlist_create(dirname, filename) != -1)
111 if (global_settings.playlist_shuffle)
113 playlist_shuffle(current_tick, -1);
116 playlist_start(0, 0);
117 return true;
120 return false;
123 /* walk a directory and check all dircache entries if a .talk file exists */
124 static void check_file_thumbnails(struct tree_context* c)
126 int i;
127 struct dirent *entry;
128 struct entry* dircache = c->dircache;
129 DIR *dir;
131 dir = opendir(c->currdir);
132 if(!dir)
133 return;
134 /* mark all files as non talking, except the .talk ones */
135 for (i=0; i < c->filesindir; i++)
137 if (dircache[i].attr & ATTR_DIRECTORY)
138 continue; /* we're not touching directories */
140 if (strcasecmp(file_thumbnail_ext,
141 &dircache[i].name[strlen(dircache[i].name)
142 - strlen(file_thumbnail_ext)]))
143 { /* no .talk file */
144 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
146 else
147 { /* .talk file, we later let them speak themselves */
148 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
152 while((entry = readdir(dir)) != 0) /* walk directory */
154 int ext_pos;
156 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
157 if (ext_pos <= 0 /* too short to carry ".talk" */
158 || (entry->attribute & ATTR_DIRECTORY) /* no file */
159 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
160 { /* or doesn't end with ".talk", no candidate */
161 continue;
164 /* terminate the (disposable) name in dir buffer,
165 this truncates off the ".talk" without needing an extra buffer */
166 entry->d_name[ext_pos] = '\0';
168 /* search corresponding file in dir cache */
169 for (i=0; i < c->filesindir; i++)
171 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
172 { /* match */
173 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
174 break; /* exit search loop, because we found it */
178 closedir(dir);
181 /* support function for qsort() */
182 static int compare(const void* p1, const void* p2)
184 struct entry* e1 = (struct entry*)p1;
185 struct entry* e2 = (struct entry*)p2;
186 int criteria;
188 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
189 { /* two directories */
190 criteria = global_settings.sort_dir;
192 #ifdef HAVE_MULTIVOLUME
193 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
194 { /* a volume identifier is involved */
195 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
196 criteria = 0; /* two volumes: sort alphabetically */
197 else /* only one is a volume: volume first */
198 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
200 #endif
203 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
204 { /* two files */
205 criteria = global_settings.sort_file;
207 else /* dir and file, dir goes first */
208 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
210 switch(criteria)
212 case 3: /* sort type */
214 int t1 = e1->attr & FILE_ATTR_MASK;
215 int t2 = e2->attr & FILE_ATTR_MASK;
217 if (!t1) /* unknown type */
218 t1 = INT_MAX; /* gets a high number, to sort after known */
219 if (!t2) /* unknown type */
220 t2 = INT_MAX; /* gets a high number, to sort after known */
222 if (t1 - t2) /* if different */
223 return t1 - t2;
224 /* else fall through to alphabetical sorting */
226 case 0: /* sort alphabetically asc */
227 if (global_settings.sort_case)
228 return strncmp(e1->name, e2->name, MAX_PATH);
229 else
230 return strncasecmp(e1->name, e2->name, MAX_PATH);
232 case 4: /* sort alphabetically desc */
233 if (global_settings.sort_case)
234 return strncmp(e2->name, e1->name, MAX_PATH);
235 else
236 return strncasecmp(e2->name, e1->name, MAX_PATH);
238 case 1: /* sort date */
239 return e1->time_write - e2->time_write;
241 case 2: /* sort date, newest first */
242 return e2->time_write - e1->time_write;
244 return 0; /* never reached */
247 /* load and sort directory into dircache. returns NULL on failure. */
248 int ft_load(struct tree_context* c, const char* tempdir)
250 int i;
251 int name_buffer_used = 0;
252 DIR *dir;
254 if (tempdir)
255 dir = opendir(tempdir);
256 else
257 dir = opendir(c->currdir);
258 if(!dir)
259 return -1; /* not a directory */
261 c->dirsindir = 0;
262 c->dirfull = false;
264 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
265 int len;
266 struct dirent *entry = readdir(dir);
267 struct entry* dptr =
268 (struct entry*)(c->dircache + i * sizeof(struct entry));
269 if (!entry)
270 break;
272 len = strlen((char *)entry->d_name);
274 /* skip directories . and .. */
275 if ((entry->attribute & ATTR_DIRECTORY) &&
276 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
277 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
278 i--;
279 continue;
282 /* Skip FAT volume ID */
283 if (entry->attribute & ATTR_VOLUME_ID) {
284 i--;
285 continue;
288 /* filter out dotfiles and hidden files */
289 if (*c->dirfilter != SHOW_ALL &&
290 ((entry->d_name[0]=='.') ||
291 (entry->attribute & ATTR_HIDDEN))) {
292 i--;
293 continue;
296 dptr->attr = entry->attribute;
298 /* check for known file types */
299 if ( !(dptr->attr & ATTR_DIRECTORY) )
300 dptr->attr |= filetype_get_attr((char *)entry->d_name);
302 /* filter out non-visible files */
303 if ((!(dptr->attr & ATTR_DIRECTORY) && (
304 (*c->dirfilter == SHOW_PLAYLIST &&
305 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
306 ((*c->dirfilter == SHOW_MUSIC &&
307 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
308 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
309 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
310 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
311 #ifdef HAVE_REMOTE_LCD
312 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
313 #endif
314 #if CONFIG_TUNER
315 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
316 #endif
317 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
318 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
319 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
320 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
321 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
323 i--;
324 continue;
327 if (len > c->name_buffer_size - name_buffer_used - 1) {
328 /* Tell the world that we ran out of buffer space */
329 c->dirfull = true;
330 break;
332 dptr->name = &c->name_buffer[name_buffer_used];
333 dptr->time_write =
334 (long)entry->wrtdate<<16 |
335 (long)entry->wrttime; /* in one # */
336 strcpy(dptr->name, (char *)entry->d_name);
337 name_buffer_used += len + 1;
339 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
340 c->dirsindir++;
342 c->filesindir = i;
343 c->dirlength = i;
344 closedir(dir);
346 qsort(c->dircache,i,sizeof(struct entry),compare);
348 /* If thumbnail talking is enabled, make an extra run to mark files with
349 associated thumbnails, so we don't do unsuccessful spinups later. */
350 if (global_settings.talk_file_clip)
351 check_file_thumbnails(c); /* map .talk to ours */
353 return 0;
356 int ft_enter(struct tree_context* c)
358 int rc = 0;
359 char buf[MAX_PATH];
360 struct entry *dircache = c->dircache;
361 struct entry* file = &dircache[c->selected_item];
362 bool reload_dir = false;
363 bool start_wps = false;
364 bool exit_func = false;
366 if (c->currdir[1])
367 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
368 else
369 snprintf(buf,sizeof(buf),"/%s",file->name);
371 if (file->attr & ATTR_DIRECTORY) {
372 memcpy(c->currdir, buf, sizeof(c->currdir));
373 if ( c->dirlevel < MAX_DIR_LEVELS )
374 c->selected_item_history[c->dirlevel] = c->selected_item;
375 c->dirlevel++;
376 c->selected_item=0;
378 else {
379 int seed = current_tick;
380 bool play = false;
381 int start_index=0;
383 switch ( file->attr & FILE_ATTR_MASK ) {
384 case FILE_ATTR_M3U:
385 play = ft_play_playlist(buf, c->currdir, file->name);
387 if (play)
389 start_index = 0;
392 break;
394 case FILE_ATTR_AUDIO:
395 if (bookmark_autoload(c->currdir))
396 break;
398 gui_syncsplash(0, ID2P(LANG_WAIT));
400 /* about to create a new current playlist...
401 allow user to cancel the operation */
402 if (!warn_on_pl_erase())
403 break;
405 if (global_settings.party_mode)
407 playlist_insert_track(NULL, buf,
408 PLAYLIST_INSERT_LAST, true, true);
409 gui_syncsplash(HZ, ID2P(LANG_QUEUE_LAST));
411 else if (playlist_create(c->currdir, NULL) != -1)
413 start_index = ft_build_playlist(c, c->selected_item);
414 if (global_settings.playlist_shuffle)
416 start_index = playlist_shuffle(seed, start_index);
418 /* when shuffling dir.: play all files
419 even if the file selected by user is
420 not the first one */
421 if (!global_settings.play_selected)
422 start_index = 0;
425 playlist_start(start_index, 0);
426 play = true;
428 break;
430 #if CONFIG_TUNER
431 /* fmr preset file */
432 case FILE_ATTR_FMR:
434 gui_syncsplash(0, ID2P(LANG_WAIT));
436 /* Preset inside the default folder. */
437 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
439 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
440 radio_load_presets(global_settings.fmr_file);
441 if(!in_radio_screen())
442 radio_screen();
445 * Preset outside default folder, we can choose such only
446 * if we are out of the radio screen, so the check for the
447 * radio status isn't neccessary
449 else
451 radio_load_presets(buf);
452 radio_screen();
455 break;
456 #endif
459 /* wps config file */
460 case FILE_ATTR_WPS:
461 gui_syncsplash(0, ID2P(LANG_WAIT));
462 #if LCD_DEPTH > 1
463 unload_wps_backdrop();
464 #endif
465 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
466 set_file(buf, (char *)global_settings.wps_file,
467 MAX_FILENAME);
468 break;
470 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
471 /* remote-wps config file */
472 case FILE_ATTR_RWPS:
473 gui_syncsplash(0, ID2P(LANG_WAIT));
474 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
475 unload_remote_wps_backdrop();
476 #endif
477 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
478 set_file(buf, (char *)global_settings.rwps_file,
479 MAX_FILENAME);
480 break;
481 #endif
483 case FILE_ATTR_CFG:
484 gui_syncsplash(0, ID2P(LANG_WAIT));
485 if (!settings_load_config(buf,true))
486 break;
487 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_LOADED));
488 break;
490 case FILE_ATTR_BMARK:
491 gui_syncsplash(0, ID2P(LANG_WAIT));
492 bookmark_load(buf, false);
493 reload_dir = true;
494 break;
496 case FILE_ATTR_LNG:
497 gui_syncsplash(0, ID2P(LANG_WAIT));
498 if(!lang_load(buf)) {
499 set_file(buf, (char *)global_settings.lang_file,
500 MAX_FILENAME);
501 talk_init(); /* use voice of same language */
502 gui_syncsplash(HZ, ID2P(LANG_LANGUAGE_LOADED));
504 break;
506 #ifdef HAVE_LCD_BITMAP
507 case FILE_ATTR_FONT:
508 gui_syncsplash(0, ID2P(LANG_WAIT));
509 font_load(buf);
510 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
511 break;
513 case FILE_ATTR_KBD:
514 gui_syncsplash(0, ID2P(LANG_WAIT));
515 if (!load_kbd(buf))
516 gui_syncsplash(HZ, ID2P(LANG_KEYBOARD_LOADED));
517 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
518 break;
519 #endif
521 #ifndef SIMULATOR
522 /* firmware file */
523 case FILE_ATTR_MOD:
524 gui_syncsplash(0, ID2P(LANG_WAIT));
525 rolo_load(buf);
526 break;
527 #endif
529 /* plugin file */
530 case FILE_ATTR_ROCK:
531 if (global_settings.party_mode) {
532 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
533 break;
536 gui_syncsplash(0, ID2P(LANG_WAIT));
538 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
540 if(*c->dirfilter > NUM_FILTER_MODES)
541 /* leave sub-browsers after usb, doing
542 otherwise might be confusing to the user */
543 exit_func = true;
544 else
545 reload_dir = true;
547 break;
549 case FILE_ATTR_CUE:
550 display_cuesheet_content(buf);
551 break;
553 default:
555 const char* plugin;
557 if (global_settings.party_mode) {
558 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
559 break;
562 plugin = filetype_get_plugin(file);
563 if (plugin)
565 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
566 reload_dir = true;
568 break;
572 if ( play ) {
573 /* the resume_index must always be the index in the
574 shuffled list in case shuffle is enabled */
575 global_status.resume_index = start_index;
576 global_status.resume_offset = 0;
577 status_save();
579 start_wps = true;
581 else {
582 if (*c->dirfilter > NUM_FILTER_MODES &&
583 *c->dirfilter != SHOW_FONT &&
584 *c->dirfilter != SHOW_PLUGINS)
586 exit_func = true;
591 if (reload_dir)
592 rc = 1;
593 if (start_wps)
594 rc = 2;
595 if (exit_func)
596 rc = 3;
598 return rc;
601 int ft_exit(struct tree_context* c)
603 extern char lastfile[]; /* from tree.c */
604 char buf[MAX_PATH];
605 int rc = 0;
606 bool exit_func = false;
608 int i = strlen(c->currdir);
609 if (i>1) {
610 while (c->currdir[i-1]!='/')
611 i--;
612 strcpy(buf,&c->currdir[i]);
613 if (i==1)
614 c->currdir[i]=0;
615 else
616 c->currdir[i-1]=0;
618 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
619 exit_func = true;
621 c->dirlevel--;
622 if ( c->dirlevel < MAX_DIR_LEVELS )
623 c->selected_item=c->selected_item_history[c->dirlevel];
624 else
625 c->selected_item=0;
627 /* if undefined position */
628 if (c->selected_item == -1)
629 strcpy(lastfile, buf);
631 else
633 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
634 exit_func = true;
637 if (exit_func)
638 rc = 3;
640 return rc;