Oops, correctly calculate the interrupt number in UIE().
[Rockbox.git] / apps / filetree.c
blob72a58c7fe1674604e4d4e18d8f95624ce6306d37
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 #ifdef HAVE_LCD_BITMAP
45 #include "keyboard.h"
46 #endif
48 #ifdef CONFIG_TUNER
49 #include "radio.h"
50 #endif
52 #ifndef SIMULATOR
53 static int boot_size = 0;
54 static int boot_cluster;
55 #endif
56 extern bool boot_changed;
58 int ft_build_playlist(struct tree_context* c, int start_index)
60 int i;
61 int start=start_index;
63 struct entry *dircache = c->dircache;
65 for(i = 0;i < c->filesindir;i++)
67 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
69 DEBUGF("Adding %s\n", dircache[i].name);
70 if (playlist_add(dircache[i].name) < 0)
71 break;
73 else
75 /* Adjust the start index when se skip non-MP3 entries */
76 if(i < start)
77 start_index--;
81 return start_index;
84 /* walk a directory and check all dircache entries if a .talk file exists */
85 static void check_file_thumbnails(struct tree_context* c)
87 int i;
88 struct dircache_entry *entry;
89 struct entry* dircache = c->dircache;
90 DIRCACHED *dir;
92 dir = opendir_cached(c->currdir);
93 if(!dir)
94 return;
95 /* mark all files as non talking, except the .talk ones */
96 for (i=0; i < c->filesindir; i++)
98 if (dircache[i].attr & ATTR_DIRECTORY)
99 continue; /* we're not touching directories */
101 if (strcasecmp(file_thumbnail_ext,
102 &dircache[i].name[strlen(dircache[i].name)
103 - strlen(file_thumbnail_ext)]))
104 { /* no .talk file */
105 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
107 else
108 { /* .talk file, we later let them speak themselves */
109 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
113 while((entry = readdir_cached(dir)) != 0) /* walk directory */
115 int ext_pos;
117 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
118 if (ext_pos <= 0 /* too short to carry ".talk" */
119 || (entry->attribute & ATTR_DIRECTORY) /* no file */
120 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
121 { /* or doesn't end with ".talk", no candidate */
122 continue;
125 /* terminate the (disposable) name in dir buffer,
126 this truncates off the ".talk" without needing an extra buffer */
127 entry->d_name[ext_pos] = '\0';
129 /* search corresponding file in dir cache */
130 for (i=0; i < c->filesindir; i++)
132 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
133 { /* match */
134 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
135 break; /* exit search loop, because we found it */
139 closedir_cached(dir);
142 /* support function for qsort() */
143 static int compare(const void* p1, const void* p2)
145 struct entry* e1 = (struct entry*)p1;
146 struct entry* e2 = (struct entry*)p2;
147 int criteria;
149 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
150 { /* two directories */
151 criteria = global_settings.sort_dir;
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);
161 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
162 { /* two files */
163 criteria = global_settings.sort_file;
165 else /* dir and file, dir goes first */
166 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
168 switch(criteria)
170 case 3: /* sort type */
172 int t1 = e1->attr & TREE_ATTR_MASK;
173 int t2 = e2->attr & TREE_ATTR_MASK;
175 if (!t1) /* unknown type */
176 t1 = INT_MAX; /* gets a high number, to sort after known */
177 if (!t2) /* unknown type */
178 t2 = INT_MAX; /* gets a high number, to sort after known */
180 if (t1 - t2) /* if different */
181 return t1 - t2;
182 /* else fall through to alphabetical sorting */
184 case 0: /* sort alphabetically asc */
185 if (global_settings.sort_case)
186 return strncmp(e1->name, e2->name, MAX_PATH);
187 else
188 return strncasecmp(e1->name, e2->name, MAX_PATH);
190 case 4: /* sort alphabetically desc */
191 if (global_settings.sort_case)
192 return strncmp(e2->name, e1->name, MAX_PATH);
193 else
194 return strncasecmp(e2->name, e1->name, MAX_PATH);
196 case 1: /* sort date */
197 return e1->time_write - e2->time_write;
199 case 2: /* sort date, newest first */
200 return e2->time_write - e1->time_write;
202 return 0; /* never reached */
205 /* load and sort directory into dircache. returns NULL on failure. */
206 int ft_load(struct tree_context* c, const char* tempdir)
208 int i;
209 int name_buffer_used = 0;
210 DIRCACHED *dir;
212 if (tempdir)
213 dir = opendir_cached(tempdir);
214 else
215 dir = opendir_cached(c->currdir);
216 if(!dir)
217 return -1; /* not a directory */
219 c->dirsindir = 0;
220 c->dirfull = false;
222 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
223 int len;
224 struct dircache_entry *entry = readdir_cached(dir);
225 struct entry* dptr =
226 (struct entry*)(c->dircache + i * sizeof(struct entry));
227 if (!entry)
228 break;
230 len = strlen((char *)entry->d_name);
232 /* skip directories . and .. */
233 if ((entry->attribute & ATTR_DIRECTORY) &&
234 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
235 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
236 i--;
237 continue;
240 /* Skip FAT volume ID */
241 if (entry->attribute & ATTR_VOLUME_ID) {
242 i--;
243 continue;
246 /* filter out dotfiles and hidden files */
247 if (*c->dirfilter != SHOW_ALL &&
248 ((entry->d_name[0]=='.') ||
249 (entry->attribute & ATTR_HIDDEN))) {
250 i--;
251 continue;
254 dptr->attr = entry->attribute;
256 /* check for known file types */
257 if ( !(dptr->attr & ATTR_DIRECTORY) )
258 dptr->attr |= filetype_get_attr((char *)entry->d_name);
260 #ifdef BOOTFILE
261 /* memorize/compare details about the boot file */
262 if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
263 if (boot_size) {
264 if ((entry->size != boot_size) ||
265 (entry->startcluster != boot_cluster))
266 boot_changed = true;
268 boot_size = entry->size;
269 boot_cluster = entry->startcluster;
271 #endif
273 /* filter out non-visible files */
274 if ((!(dptr->attr & ATTR_DIRECTORY) && (
275 (*c->dirfilter == SHOW_PLAYLIST &&
276 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
277 ((*c->dirfilter == SHOW_MUSIC &&
278 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
279 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
280 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
281 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
282 #ifdef HAVE_REMOTE_LCD
283 (*c->dirfilter == SHOW_RWPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_RWPS) ||
284 #endif
285 #ifdef CONFIG_TUNER
286 (*c->dirfilter == SHOW_FMR && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FMR) ||
287 #endif
288 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
289 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
290 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
291 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
292 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK))
294 i--;
295 continue;
298 if (len > c->name_buffer_size - name_buffer_used - 1) {
299 /* Tell the world that we ran out of buffer space */
300 c->dirfull = true;
301 break;
303 dptr->name = &c->name_buffer[name_buffer_used];
304 dptr->time_write =
305 (long)entry->wrtdate<<16 |
306 (long)entry->wrttime; /* in one # */
307 strcpy(dptr->name, (char *)entry->d_name);
308 name_buffer_used += len + 1;
310 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
311 c->dirsindir++;
313 c->filesindir = i;
314 c->dirlength = i;
315 closedir_cached(dir);
317 qsort(c->dircache,i,sizeof(struct entry),compare);
319 /* If thumbnail talking is enabled, make an extra run to mark files with
320 associated thumbnails, so we don't do unsuccessful spinups later. */
321 if (global_settings.talk_file == 3)
322 check_file_thumbnails(c); /* map .talk to ours */
324 return 0;
327 int ft_enter(struct tree_context* c)
329 int rc = 0;
330 char buf[MAX_PATH];
331 struct entry *dircache = c->dircache;
332 struct entry* file = &dircache[c->selected_item];
333 bool reload_dir = false;
334 bool start_wps = false;
335 bool exit_func = false;
337 if (c->currdir[1])
338 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
339 else
340 snprintf(buf,sizeof(buf),"/%s",file->name);
342 if (file->attr & ATTR_DIRECTORY) {
343 memcpy(c->currdir, buf, sizeof(c->currdir));
344 if ( c->dirlevel < MAX_DIR_LEVELS )
345 c->selected_item_history[c->dirlevel] = c->selected_item;
346 c->dirlevel++;
347 c->selected_item=0;
349 else {
350 int seed = current_tick;
351 bool play = false;
352 int start_index=0;
354 switch ( file->attr & TREE_ATTR_MASK ) {
355 case TREE_ATTR_M3U:
356 if (global_settings.party_mode) {
357 gui_syncsplash(HZ, true, str(LANG_PARTY_MODE));
358 break;
361 if (bookmark_autoload(buf))
362 break;
364 gui_syncsplash(0, true, str(LANG_WAIT));
366 /* about to create a new current playlist...
367 allow user to cancel the operation */
368 if (global_settings.warnon_erase_dynplaylist &&
369 playlist_modified(NULL))
371 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
372 struct text_message message={lines, 1};
374 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
375 break;
378 if (playlist_create(c->currdir, file->name) != -1)
380 if (global_settings.playlist_shuffle)
381 playlist_shuffle(seed, -1);
382 start_index = 0;
383 playlist_start(start_index,0);
384 play = true;
386 break;
388 case TREE_ATTR_MPA:
389 if (bookmark_autoload(c->currdir))
390 break;
392 gui_syncsplash(0, true, str(LANG_WAIT));
394 /* about to create a new current playlist...
395 allow user to cancel the operation */
396 if (global_settings.warnon_erase_dynplaylist &&
397 !global_settings.party_mode &&
398 playlist_modified(NULL))
400 char *lines[]={str(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
401 struct text_message message={lines, 1};
403 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
404 break;
407 if (global_settings.party_mode) {
408 playlist_insert_track(NULL, buf,
409 PLAYLIST_INSERT_LAST, true);
410 gui_syncsplash(HZ, true, str(LANG_QUEUE_LAST));
412 else if (playlist_create(c->currdir, NULL) != -1)
414 start_index = ft_build_playlist(c, c->selected_item);
415 if (global_settings.playlist_shuffle)
417 start_index = playlist_shuffle(seed, start_index);
419 /* when shuffling dir.: play all files
420 even if the file selected by user is
421 not the first one */
422 if (!global_settings.play_selected)
423 start_index = 0;
426 playlist_start(start_index, 0);
427 play = true;
429 break;
431 #ifdef CONFIG_TUNER
432 /* fmr preset file */
433 case TREE_ATTR_FMR:
435 gui_syncsplash(0, true, str(LANG_WAIT));
437 /* Preset inside the default folder. */
438 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
440 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
441 radio_load_presets(global_settings.fmr_file);
442 if(get_radio_status() != FMRADIO_PLAYING &&
443 get_radio_status() != FMRADIO_PAUSED)
444 radio_screen();
447 * Preset outside default folder, we can choose such only
448 * if we are out of the radio screen, so the check for the
449 * radio status isn't neccessary
451 else
453 radio_load_presets(buf);
454 radio_screen();
457 break;
458 #endif
461 /* wps config file */
462 case TREE_ATTR_WPS:
463 gui_syncsplash(0, true, str(LANG_WAIT));
464 wps_data_load(gui_wps[0].data, buf, true);
465 set_file(buf, (char *)global_settings.wps_file,
466 MAX_FILENAME);
467 break;
469 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
470 /* remote-wps config file */
471 case TREE_ATTR_RWPS:
472 gui_syncsplash(0, true, str(LANG_WAIT));
473 wps_data_load(gui_wps[1].data, buf, true);
474 set_file(buf, (char *)global_settings.rwps_file,
475 MAX_FILENAME);
476 break;
477 #endif
479 case TREE_ATTR_CFG:
480 gui_syncsplash(0, true, str(LANG_WAIT));
481 if (!settings_load_config(buf))
482 break;
483 gui_syncsplash(HZ, true, str(LANG_SETTINGS_LOADED));
484 break;
486 case TREE_ATTR_BMARK:
487 gui_syncsplash(0, true, str(LANG_WAIT));
488 bookmark_load(buf, false);
489 reload_dir = true;
490 break;
492 case TREE_ATTR_LNG:
493 gui_syncsplash(0, true, str(LANG_WAIT));
494 if(!lang_load(buf)) {
495 set_file(buf, (char *)global_settings.lang_file,
496 MAX_FILENAME);
497 talk_init(); /* use voice of same language */
498 gui_syncsplash(HZ, true, str(LANG_LANGUAGE_LOADED));
500 break;
502 #ifdef HAVE_LCD_BITMAP
503 case TREE_ATTR_FONT:
504 gui_syncsplash(0, true, str(LANG_WAIT));
505 font_load(buf);
506 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
507 break;
509 case TREE_ATTR_KBD:
510 gui_syncsplash(0, true, str(LANG_WAIT));
511 if (!load_kbd(buf))
512 gui_syncsplash(HZ, true, str(LANG_KEYBOARD_LOADED));
513 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
514 break;
515 #endif
517 #ifndef SIMULATOR
518 /* firmware file */
519 case TREE_ATTR_MOD:
520 gui_syncsplash(0, true, str(LANG_WAIT));
521 rolo_load(buf);
522 break;
523 #endif
525 /* plugin file */
526 case TREE_ATTR_ROCK:
527 if (global_settings.party_mode) {
528 gui_syncsplash(HZ, true, str(LANG_PARTY_MODE));
529 break;
532 gui_syncsplash(0, true, str(LANG_WAIT));
534 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
536 if(*c->dirfilter > NUM_FILTER_MODES)
537 /* leave sub-browsers after usb, doing
538 otherwise might be confusing to the user */
539 exit_func = true;
540 else
541 reload_dir = true;
543 break;
545 default:
547 char* plugin;
549 if (global_settings.party_mode) {
550 gui_syncsplash(HZ, true, str(LANG_PARTY_MODE));
551 break;
554 plugin = filetype_get_plugin(file);
555 if (plugin)
557 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
558 reload_dir = true;
560 break;
564 if ( play ) {
565 /* the resume_index must always be the index in the
566 shuffled list in case shuffle is enabled */
567 global_settings.resume_index = start_index;
568 global_settings.resume_offset = 0;
569 settings_save();
571 start_wps = true;
573 else {
574 if (*c->dirfilter > NUM_FILTER_MODES &&
575 *c->dirfilter != SHOW_FONT &&
576 *c->dirfilter != SHOW_PLUGINS)
578 exit_func = true;
583 if (reload_dir)
584 rc = 1;
585 if (start_wps)
586 rc = 2;
587 if (exit_func)
588 rc = 3;
590 return rc;
593 int ft_exit(struct tree_context* c)
595 extern char lastfile[]; /* from tree.c */
596 char buf[MAX_PATH];
597 int rc = 0;
598 bool exit_func = false;
600 int i = strlen(c->currdir);
601 if (i>1) {
602 while (c->currdir[i-1]!='/')
603 i--;
604 strcpy(buf,&c->currdir[i]);
605 if (i==1)
606 c->currdir[i]=0;
607 else
608 c->currdir[i-1]=0;
610 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
611 exit_func = true;
613 c->dirlevel--;
614 if ( c->dirlevel < MAX_DIR_LEVELS )
615 c->selected_item=c->selected_item_history[c->dirlevel];
616 else
617 c->selected_item=0;
619 /* if undefined position */
620 if (c->selected_item == -1)
621 strcpy(lastfile, buf);
623 else
625 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
626 exit_func = true;
629 if (exit_func)
630 rc = 3;
632 return rc;