Cleanup quickscreen text drawing by using text alignment flags for viewports.
[kugel-rb.git] / apps / filetree.c
blob3b313583210c8e4d2fd60592d148ffcf77e700eb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdlib.h>
22 #include <file.h>
23 #include <dir.h>
24 #include <string.h>
25 #include <kernel.h>
26 #include <lcd.h>
27 #include <debug.h>
28 #include <font.h>
29 #include <limits.h>
30 #include "bookmark.h"
31 #include "tree.h"
32 #include "settings.h"
33 #include "filetypes.h"
34 #include "talk.h"
35 #include "playlist.h"
36 #include "lang.h"
37 #include "language.h"
38 #include "screens.h"
39 #include "plugin.h"
40 #include "rolo.h"
41 #include "sprintf.h"
42 #include "splash.h"
43 #include "cuesheet.h"
44 #include "filetree.h"
45 #include "misc.h"
46 #include "strnatcmp.h"
47 #ifdef HAVE_LCD_BITMAP
48 #include "keyboard.h"
49 #endif
51 #if CONFIG_TUNER
52 #include "radio.h"
53 #endif
54 #include "wps.h"
55 #include "backdrop.h"
57 static int compare_sort_dir; /* qsort key for sorting directories */
59 int ft_build_playlist(struct tree_context* c, int start_index)
61 int i;
62 int start=start_index;
64 struct entry *dircache = c->dircache;
66 for(i = 0;i < c->filesindir;i++)
68 if((dircache[i].attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
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 /* Start playback of a playlist, checking for bookmark autoload, modified
85 * playlists, etc., as required. Returns false if playback wasn't started,
86 * or started via bookmark autoload, true otherwise.
88 * Pointers to both the full pathname and the separated parts needed to
89 * avoid allocating yet another path buffer on the stack (and save some
90 * code; the caller typically needs to create the full pathname anyway)...
92 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
94 if (global_settings.party_mode && audio_status())
96 splash(HZ, ID2P(LANG_PARTY_MODE));
97 return false;
100 if (bookmark_autoload(pathname))
102 return false;
105 splash(0, ID2P(LANG_WAIT));
107 /* about to create a new current playlist...
108 allow user to cancel the operation */
109 if (!warn_on_pl_erase())
110 return false;
112 if (playlist_create(dirname, filename) != -1)
114 if (global_settings.playlist_shuffle)
116 playlist_shuffle(current_tick, -1);
119 playlist_start(0, 0);
120 return true;
123 return false;
126 /* walk a directory and check all dircache entries if a .talk file exists */
127 static void check_file_thumbnails(struct tree_context* c)
129 int i;
130 struct dirent *entry;
131 struct entry* dircache = c->dircache;
132 DIR *dir;
134 dir = opendir(c->currdir);
135 if(!dir)
136 return;
137 /* mark all files as non talking, except the .talk ones */
138 for (i=0; i < c->filesindir; i++)
140 if (dircache[i].attr & ATTR_DIRECTORY)
141 continue; /* we're not touching directories */
143 if (strcasecmp(file_thumbnail_ext,
144 &dircache[i].name[strlen(dircache[i].name)
145 - strlen(file_thumbnail_ext)]))
146 { /* no .talk file */
147 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
149 else
150 { /* .talk file, we later let them speak themselves */
151 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
155 while((entry = readdir(dir)) != 0) /* walk directory */
157 int ext_pos;
159 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
160 if (ext_pos <= 0 /* too short to carry ".talk" */
161 || (entry->attribute & ATTR_DIRECTORY) /* no file */
162 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
163 { /* or doesn't end with ".talk", no candidate */
164 continue;
167 /* terminate the (disposable) name in dir buffer,
168 this truncates off the ".talk" without needing an extra buffer */
169 entry->d_name[ext_pos] = '\0';
171 /* search corresponding file in dir cache */
172 for (i=0; i < c->filesindir; i++)
174 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
175 { /* match */
176 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
177 break; /* exit search loop, because we found it */
181 closedir(dir);
184 /* support function for qsort() */
185 static int compare(const void* p1, const void* p2)
187 struct entry* e1 = (struct entry*)p1;
188 struct entry* e2 = (struct entry*)p2;
189 int criteria;
191 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
192 { /* two directories */
193 criteria = compare_sort_dir;
195 #ifdef HAVE_MULTIVOLUME
196 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
197 { /* a volume identifier is involved */
198 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
199 criteria = SORT_ALPHA; /* two volumes: sort alphabetically */
200 else /* only one is a volume: volume first */
201 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
203 #endif
206 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
207 { /* two files */
208 criteria = global_settings.sort_file;
210 else /* dir and file, dir goes first */
211 return (e2->attr & ATTR_DIRECTORY) - (e1->attr & ATTR_DIRECTORY);
213 switch(criteria)
215 case SORT_TYPE:
216 case SORT_TYPE_REVERSED:
218 int t1 = e1->attr & FILE_ATTR_MASK;
219 int t2 = e2->attr & FILE_ATTR_MASK;
221 if (!t1) /* unknown type */
222 t1 = INT_MAX; /* gets a high number, to sort after known */
223 if (!t2) /* unknown type */
224 t2 = INT_MAX; /* gets a high number, to sort after known */
226 if (t1 != t2) /* if different */
227 return (t1 - t2) * (criteria == SORT_TYPE_REVERSED ? -1 : 1);
228 /* else fall through to alphabetical sorting */
231 case SORT_DATE:
232 case SORT_DATE_REVERSED:
233 /* Ignore SORT_TYPE */
234 if (criteria == SORT_DATE || criteria == SORT_DATE_REVERSED)
236 if (e1->time_write != e2->time_write)
237 return (e1->time_write - e2->time_write)
238 * (criteria == SORT_DATE_REVERSED ? -1 : 1);
239 /* else fall through to alphabetical sorting */
242 case SORT_ALPHA:
243 case SORT_ALPHA_REVERSED:
245 if (global_settings.sort_case)
247 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
248 return strnatcmp(e1->name, e2->name)
249 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
250 else
251 return strncmp(e1->name, e2->name, MAX_PATH)
252 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
254 else
256 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
257 return strnatcasecmp(e1->name, e2->name)
258 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
259 else
260 return strncasecmp(e1->name, e2->name, MAX_PATH)
261 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
266 return 0; /* never reached */
269 /* load and sort directory into dircache. returns NULL on failure. */
270 int ft_load(struct tree_context* c, const char* tempdir)
272 int i;
273 int name_buffer_used = 0;
274 DIR *dir;
276 if (tempdir)
277 dir = opendir(tempdir);
278 else
279 dir = opendir(c->currdir);
280 if(!dir)
281 return -1; /* not a directory */
283 c->dirsindir = 0;
284 c->dirfull = false;
286 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
287 int len;
288 struct dirent *entry = readdir(dir);
289 struct entry* dptr =
290 (struct entry*)(c->dircache + i * sizeof(struct entry));
291 if (!entry)
292 break;
294 len = strlen((char *)entry->d_name);
296 /* skip directories . and .. */
297 if ((entry->attribute & ATTR_DIRECTORY) &&
298 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
299 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
300 i--;
301 continue;
304 /* Skip FAT volume ID */
305 if (entry->attribute & ATTR_VOLUME_ID) {
306 i--;
307 continue;
310 /* filter out dotfiles and hidden files */
311 if (*c->dirfilter != SHOW_ALL &&
312 ((entry->d_name[0]=='.') ||
313 (entry->attribute & ATTR_HIDDEN))) {
314 i--;
315 continue;
318 dptr->attr = entry->attribute;
320 /* check for known file types */
321 if ( !(dptr->attr & ATTR_DIRECTORY) )
322 dptr->attr |= filetype_get_attr((char *)entry->d_name);
324 /* filter out non-visible files */
325 if ((!(dptr->attr & ATTR_DIRECTORY) && (
326 (*c->dirfilter == SHOW_PLAYLIST &&
327 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
328 ((*c->dirfilter == SHOW_MUSIC &&
329 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
330 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
331 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
332 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
333 #ifdef HAVE_LCD_BITMAP
334 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
335 (*c->dirfilter == SHOW_SBS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_SBS) ||
336 #endif
337 #ifdef HAVE_REMOTE_LCD
338 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
339 (*c->dirfilter == SHOW_RSBS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RSBS) ||
340 #endif
341 #if CONFIG_TUNER
342 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
343 #endif
344 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
345 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
346 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
347 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
349 i--;
350 continue;
353 if (len > c->name_buffer_size - name_buffer_used - 1) {
354 /* Tell the world that we ran out of buffer space */
355 c->dirfull = true;
356 break;
358 dptr->name = &c->name_buffer[name_buffer_used];
359 dptr->time_write =
360 (long)entry->wrtdate<<16 |
361 (long)entry->wrttime; /* in one # */
362 strcpy(dptr->name, (char *)entry->d_name);
363 name_buffer_used += len + 1;
365 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
366 c->dirsindir++;
368 c->filesindir = i;
369 c->dirlength = i;
370 closedir(dir);
372 compare_sort_dir = c->sort_dir;
373 qsort(c->dircache,i,sizeof(struct entry),compare);
375 /* If thumbnail talking is enabled, make an extra run to mark files with
376 associated thumbnails, so we don't do unsuccessful spinups later. */
377 if (global_settings.talk_file_clip)
378 check_file_thumbnails(c); /* map .talk to ours */
380 return 0;
383 int ft_enter(struct tree_context* c)
385 int rc = 0;
386 char buf[MAX_PATH];
387 struct entry *dircache = c->dircache;
388 struct entry* file = &dircache[c->selected_item];
389 bool reload_dir = false;
390 bool start_wps = false;
391 bool exit_func = false;
393 if (c->currdir[1])
394 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
395 else
396 snprintf(buf,sizeof(buf),"/%s",file->name);
398 if (file->attr & ATTR_DIRECTORY) {
399 memcpy(c->currdir, buf, sizeof(c->currdir));
400 if ( c->dirlevel < MAX_DIR_LEVELS )
401 c->selected_item_history[c->dirlevel] = c->selected_item;
402 c->dirlevel++;
403 c->selected_item=0;
405 else {
406 int seed = current_tick;
407 bool play = false;
408 int start_index=0;
410 switch ( file->attr & FILE_ATTR_MASK ) {
411 case FILE_ATTR_M3U:
412 play = ft_play_playlist(buf, c->currdir, file->name);
414 if (play)
416 start_index = 0;
419 break;
421 case FILE_ATTR_AUDIO:
422 if (bookmark_autoload(c->currdir))
423 break;
425 splash(0, ID2P(LANG_WAIT));
427 /* about to create a new current playlist...
428 allow user to cancel the operation */
429 if (!warn_on_pl_erase())
430 break;
432 if (global_settings.party_mode && audio_status())
434 playlist_insert_track(NULL, buf,
435 PLAYLIST_INSERT_LAST, true, true);
436 splash(HZ, ID2P(LANG_QUEUE_LAST));
438 else if (playlist_create(c->currdir, NULL) != -1)
440 start_index = ft_build_playlist(c, c->selected_item);
441 if (global_settings.playlist_shuffle)
443 start_index = playlist_shuffle(seed, start_index);
445 /* when shuffling dir.: play all files
446 even if the file selected by user is
447 not the first one */
448 if (!global_settings.play_selected)
449 start_index = 0;
452 playlist_start(start_index, 0);
453 play = true;
455 break;
457 #if CONFIG_TUNER
458 /* fmr preset file */
459 case FILE_ATTR_FMR:
460 splash(0, ID2P(LANG_WAIT));
462 /* Preset inside the default folder. */
463 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
465 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
466 radio_load_presets(global_settings.fmr_file);
467 if(!in_radio_screen())
468 radio_screen();
471 * Preset outside default folder, we can choose such only
472 * if we are out of the radio screen, so the check for the
473 * radio status isn't neccessary
475 else
477 radio_load_presets(buf);
478 radio_screen();
481 break;
482 #endif
484 #ifdef HAVE_LCD_BITMAP
485 case FILE_ATTR_SBS:
486 splash(0, ID2P(LANG_WAIT));
487 set_file(buf, (char *)global_settings.sbs_file,
488 MAX_FILENAME);
489 global_settings.statusbar = STATUSBAR_CUSTOM;
490 settings_apply_skins();
491 break;
492 #endif
493 #ifdef HAVE_REMOTE_LCD
494 case FILE_ATTR_RSBS:
495 splash(0, ID2P(LANG_WAIT));
496 set_file(buf, (char *)global_settings.rsbs_file,
497 MAX_FILENAME);
498 global_settings.remote_statusbar = STATUSBAR_CUSTOM;
499 settings_apply_skins();
500 break;
501 #endif
502 /* wps config file */
503 case FILE_ATTR_WPS:
504 splash(0, ID2P(LANG_WAIT));
505 #if LCD_DEPTH > 1
506 backdrop_unload(BACKDROP_SKIN_WPS);
507 #endif
508 set_file(buf, (char *)global_settings.wps_file,
509 MAX_FILENAME);
510 settings_apply_skins();
511 break;
513 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
514 /* remote-wps config file */
515 case FILE_ATTR_RWPS:
516 splash(0, ID2P(LANG_WAIT));
517 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
518 remote_backdrop_unload(BACKDROP_SKIN_WPS);
519 #endif
520 set_file(buf, (char *)global_settings.rwps_file,
521 MAX_FILENAME);
522 settings_apply_skins();
523 break;
524 #endif
526 case FILE_ATTR_CFG:
527 splash(0, ID2P(LANG_WAIT));
528 if (!settings_load_config(buf,true))
529 break;
530 /* do both steps seperately so that the redrawing after theme
531 * changing is independant of whether the theme has a custom ui
532 * vp or not */
533 send_event(GUI_EVENT_REFRESH, NULL);
534 /* for the statusbar */
535 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
536 tree_drawlists();
537 splash(HZ, ID2P(LANG_SETTINGS_LOADED));
538 break;
540 case FILE_ATTR_BMARK:
541 splash(0, ID2P(LANG_WAIT));
542 bookmark_load(buf, false);
543 reload_dir = true;
544 break;
546 case FILE_ATTR_LNG:
547 splash(0, ID2P(LANG_WAIT));
548 if (lang_core_load(buf))
550 splash(HZ, ID2P(LANG_FAILED));
551 break;
553 set_file(buf, (char *)global_settings.lang_file,
554 MAX_FILENAME);
555 talk_init(); /* use voice of same language */
556 viewportmanager_theme_changed(THEME_LANGUAGE);
557 splash(HZ, ID2P(LANG_LANGUAGE_LOADED));
558 break;
560 #ifdef HAVE_LCD_BITMAP
561 case FILE_ATTR_FONT:
562 splash(0, ID2P(LANG_WAIT));
563 font_load(buf);
564 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
565 break;
567 case FILE_ATTR_KBD:
568 splash(0, ID2P(LANG_WAIT));
569 if (!load_kbd(buf))
570 splash(HZ, ID2P(LANG_KEYBOARD_LOADED));
571 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
572 break;
573 #endif
575 #ifndef SIMULATOR
576 /* firmware file */
577 case FILE_ATTR_MOD:
578 splash(0, ID2P(LANG_WAIT));
579 rolo_load(buf);
580 break;
581 #endif
583 /* plugin file */
584 case FILE_ATTR_ROCK:
586 int ret;
587 if (global_settings.party_mode && audio_status()) {
588 splash(HZ, ID2P(LANG_PARTY_MODE));
589 break;
591 ret = plugin_load(buf,NULL);
592 switch (ret)
594 case PLUGIN_GOTO_WPS:
595 play = true;
596 break;
597 case PLUGIN_USB_CONNECTED:
598 if(*c->dirfilter > NUM_FILTER_MODES)
599 /* leave sub-browsers after usb, doing
600 otherwise might be confusing to the user */
601 exit_func = true;
602 else
603 reload_dir = true;
604 break;
606 case PLUGIN_ERROR:
607 case PLUGIN_OK:
609 default:
610 break;
612 break;
614 case FILE_ATTR_CUE:
615 display_cuesheet_content(buf);
616 break;
618 default:
620 const char* plugin;
622 if (global_settings.party_mode && audio_status()) {
623 splash(HZ, ID2P(LANG_PARTY_MODE));
624 break;
627 plugin = filetype_get_plugin(file);
628 if (plugin)
630 switch (plugin_load(plugin,buf))
632 case PLUGIN_USB_CONNECTED:
633 reload_dir = true;
634 break;
635 case PLUGIN_GOTO_WPS:
636 play = true;
637 break;
639 case PLUGIN_OK:
640 case PLUGIN_ERROR:
642 default:
643 break;
646 break;
650 send_event(GUI_EVENT_REFRESH, tree_drawlists);
652 if ( play ) {
653 /* the resume_index must always be the index in the
654 shuffled list in case shuffle is enabled */
655 global_status.resume_index = start_index;
656 global_status.resume_offset = 0;
657 status_save();
659 start_wps = true;
661 else {
662 if (*c->dirfilter > NUM_FILTER_MODES &&
663 *c->dirfilter != SHOW_CFG &&
664 *c->dirfilter != SHOW_FONT &&
665 *c->dirfilter != SHOW_PLUGINS)
667 exit_func = true;
672 if (reload_dir)
673 rc = 1;
674 if (start_wps)
675 rc = 2;
676 if (exit_func)
677 rc = 3;
679 return rc;
682 int ft_exit(struct tree_context* c)
684 extern char lastfile[]; /* from tree.c */
685 char buf[MAX_PATH];
686 int rc = 0;
687 bool exit_func = false;
689 int i = strlen(c->currdir);
690 if (i>1) {
691 while (c->currdir[i-1]!='/')
692 i--;
693 strcpy(buf,&c->currdir[i]);
694 if (i==1)
695 c->currdir[i]=0;
696 else
697 c->currdir[i-1]=0;
699 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
700 exit_func = true;
702 c->dirlevel--;
703 if ( c->dirlevel < MAX_DIR_LEVELS )
704 c->selected_item=c->selected_item_history[c->dirlevel];
705 else
706 c->selected_item=0;
708 /* if undefined position */
709 if (c->selected_item == -1)
710 strcpy(lastfile, buf);
712 else
714 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
715 exit_func = true;
718 if (exit_func)
719 rc = 3;
721 return rc;