Fix a dircache NULL-pointer dereference.
[kugel-rb.git] / apps / filetree.c
blobc9c8b3810abde995a511f7bf869b19a445820167
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"
56 static int compare_sort_dir; /* qsort key for sorting directories */
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 & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
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 /* Start playback of a playlist, checking for bookmark autoload, modified
84 * playlists, etc., as required. Returns false if playback wasn't started,
85 * or started via bookmark autoload, true otherwise.
87 * Pointers to both the full pathname and the separated parts needed to
88 * avoid allocating yet another path buffer on the stack (and save some
89 * code; the caller typically needs to create the full pathname anyway)...
91 bool ft_play_playlist(char* pathname, char* dirname, char* filename)
93 if (global_settings.party_mode && audio_status())
95 splash(HZ, ID2P(LANG_PARTY_MODE));
96 return false;
99 if (bookmark_autoload(pathname))
101 return false;
104 splash(0, ID2P(LANG_WAIT));
106 /* about to create a new current playlist...
107 allow user to cancel the operation */
108 if (!warn_on_pl_erase())
109 return false;
111 if (playlist_create(dirname, filename) != -1)
113 if (global_settings.playlist_shuffle)
115 playlist_shuffle(current_tick, -1);
118 playlist_start(0, 0);
119 return true;
122 return false;
125 /* walk a directory and check all dircache entries if a .talk file exists */
126 static void check_file_thumbnails(struct tree_context* c)
128 int i;
129 struct dirent *entry;
130 struct entry* dircache = c->dircache;
131 DIR *dir;
133 dir = opendir(c->currdir);
134 if(!dir)
135 return;
136 /* mark all files as non talking, except the .talk ones */
137 for (i=0; i < c->filesindir; i++)
139 if (dircache[i].attr & ATTR_DIRECTORY)
140 continue; /* we're not touching directories */
142 if (strcasecmp(file_thumbnail_ext,
143 &dircache[i].name[strlen(dircache[i].name)
144 - strlen(file_thumbnail_ext)]))
145 { /* no .talk file */
146 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
148 else
149 { /* .talk file, we later let them speak themselves */
150 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
154 while((entry = readdir(dir)) != 0) /* walk directory */
156 int ext_pos;
158 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
159 if (ext_pos <= 0 /* too short to carry ".talk" */
160 || (entry->attribute & ATTR_DIRECTORY) /* no file */
161 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
162 { /* or doesn't end with ".talk", no candidate */
163 continue;
166 /* terminate the (disposable) name in dir buffer,
167 this truncates off the ".talk" without needing an extra buffer */
168 entry->d_name[ext_pos] = '\0';
170 /* search corresponding file in dir cache */
171 for (i=0; i < c->filesindir; i++)
173 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
174 { /* match */
175 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
176 break; /* exit search loop, because we found it */
180 closedir(dir);
183 /* support function for qsort() */
184 static int compare(const void* p1, const void* p2)
186 struct entry* e1 = (struct entry*)p1;
187 struct entry* e2 = (struct entry*)p2;
188 int criteria;
190 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
191 { /* two directories */
192 criteria = compare_sort_dir;
194 #ifdef HAVE_MULTIVOLUME
195 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
196 { /* a volume identifier is involved */
197 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
198 criteria = SORT_ALPHA; /* two volumes: sort alphabetically */
199 else /* only one is a volume: volume first */
200 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
202 #endif
205 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
206 { /* two files */
207 criteria = global_settings.sort_file;
209 else /* dir and file, dir goes first */
210 return (e2->attr & ATTR_DIRECTORY) - (e1->attr & ATTR_DIRECTORY);
212 switch(criteria)
214 case SORT_TYPE:
215 case SORT_TYPE_REVERSED:
217 int t1 = e1->attr & FILE_ATTR_MASK;
218 int t2 = e2->attr & FILE_ATTR_MASK;
220 if (!t1) /* unknown type */
221 t1 = INT_MAX; /* gets a high number, to sort after known */
222 if (!t2) /* unknown type */
223 t2 = INT_MAX; /* gets a high number, to sort after known */
225 if (t1 != t2) /* if different */
226 return (t1 - t2) * (criteria == SORT_TYPE_REVERSED ? -1 : 1);
227 /* else fall through to alphabetical sorting */
230 case SORT_DATE:
231 case SORT_DATE_REVERSED:
232 /* Ignore SORT_TYPE */
233 if (criteria == SORT_DATE || criteria == SORT_DATE_REVERSED)
235 if (e1->time_write != e2->time_write)
236 return (e1->time_write - e2->time_write)
237 * (criteria == SORT_DATE_REVERSED ? -1 : 1);
238 /* else fall through to alphabetical sorting */
241 case SORT_ALPHA:
242 case SORT_ALPHA_REVERSED:
244 if (global_settings.sort_case)
246 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
247 return strnatcmp(e1->name, e2->name)
248 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
249 else
250 return strncmp(e1->name, e2->name, MAX_PATH)
251 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
253 else
255 if (global_settings.interpret_numbers == SORT_INTERPRET_AS_NUMBER)
256 return strnatcasecmp(e1->name, e2->name)
257 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
258 else
259 return strncasecmp(e1->name, e2->name, MAX_PATH)
260 * (criteria == SORT_ALPHA_REVERSED ? -1 : 1);
265 return 0; /* never reached */
268 /* load and sort directory into dircache. returns NULL on failure. */
269 int ft_load(struct tree_context* c, const char* tempdir)
271 int i;
272 int name_buffer_used = 0;
273 DIR *dir;
275 if (tempdir)
276 dir = opendir(tempdir);
277 else
278 dir = opendir(c->currdir);
279 if(!dir)
280 return -1; /* not a directory */
282 c->dirsindir = 0;
283 c->dirfull = false;
285 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
286 int len;
287 struct dirent *entry = readdir(dir);
288 struct entry* dptr =
289 (struct entry*)(c->dircache + i * sizeof(struct entry));
290 if (!entry)
291 break;
293 len = strlen((char *)entry->d_name);
295 /* skip directories . and .. */
296 if ((entry->attribute & ATTR_DIRECTORY) &&
297 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
298 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
299 i--;
300 continue;
303 /* Skip FAT volume ID */
304 if (entry->attribute & ATTR_VOLUME_ID) {
305 i--;
306 continue;
309 /* filter out dotfiles and hidden files */
310 if (*c->dirfilter != SHOW_ALL &&
311 ((entry->d_name[0]=='.') ||
312 (entry->attribute & ATTR_HIDDEN))) {
313 i--;
314 continue;
317 dptr->attr = entry->attribute;
319 /* check for known file types */
320 if ( !(dptr->attr & ATTR_DIRECTORY) )
321 dptr->attr |= filetype_get_attr((char *)entry->d_name);
323 /* filter out non-visible files */
324 if ((!(dptr->attr & ATTR_DIRECTORY) && (
325 (*c->dirfilter == SHOW_PLAYLIST &&
326 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
327 ((*c->dirfilter == SHOW_MUSIC &&
328 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
329 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
330 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
331 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
332 #ifdef HAVE_LCD_BITMAP
333 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
334 (*c->dirfilter == SHOW_SBS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_SBS) ||
335 #endif
336 #ifdef HAVE_REMOTE_LCD
337 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
338 (*c->dirfilter == SHOW_RSBS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RSBS) ||
339 #endif
340 #if CONFIG_TUNER
341 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
342 #endif
343 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
344 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
345 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
346 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK &&
347 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LUA))
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;
382 #ifdef HAVE_LCD_BITMAP
383 static void ft_load_font(char *file)
385 #if NB_SCREENS > 1
386 MENUITEM_STRINGLIST(menu, ID2P(LANG_CUSTOM_FONT), NULL,
387 ID2P(LANG_MAIN_SCREEN), ID2P(LANG_REMOTE_SCREEN))
388 switch (do_menu(&menu, NULL, NULL, false))
390 case 0: /* main lcd */
391 splash(0, ID2P(LANG_WAIT));
392 font_load(NULL, file);
393 set_file(file, (char *)global_settings.font_file, MAX_FILENAME);
394 break;
395 case 1: /* remote */
396 splash(0, ID2P(LANG_WAIT));
397 font_load_remoteui(file);
398 set_file(file, (char *)global_settings.remote_font_file, MAX_FILENAME);
399 break;
401 #else
402 splash(0, ID2P(LANG_WAIT));
403 font_load(NULL, file);
404 set_file(file, (char *)global_settings.font_file, MAX_FILENAME);
405 #endif
407 #endif
409 int ft_enter(struct tree_context* c)
411 int rc = 0;
412 char buf[MAX_PATH];
413 struct entry *dircache = c->dircache;
414 struct entry* file = &dircache[c->selected_item];
415 bool reload_dir = false;
416 bool start_wps = false;
417 bool exit_func = false;
419 if (c->currdir[1])
420 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
421 else
422 snprintf(buf,sizeof(buf),"/%s",file->name);
424 if (file->attr & ATTR_DIRECTORY) {
425 memcpy(c->currdir, buf, sizeof(c->currdir));
426 if ( c->dirlevel < MAX_DIR_LEVELS )
427 c->selected_item_history[c->dirlevel] = c->selected_item;
428 c->dirlevel++;
429 c->selected_item=0;
431 else {
432 int seed = current_tick;
433 bool play = false;
434 int start_index=0;
436 switch ( file->attr & FILE_ATTR_MASK ) {
437 case FILE_ATTR_M3U:
438 play = ft_play_playlist(buf, c->currdir, file->name);
440 if (play)
442 start_index = 0;
445 break;
447 case FILE_ATTR_AUDIO:
448 if (bookmark_autoload(c->currdir))
449 break;
451 splash(0, ID2P(LANG_WAIT));
453 /* about to create a new current playlist...
454 allow user to cancel the operation */
455 if (!warn_on_pl_erase())
456 break;
458 if (global_settings.party_mode && audio_status())
460 playlist_insert_track(NULL, buf,
461 PLAYLIST_INSERT_LAST, true, true);
462 splash(HZ, ID2P(LANG_QUEUE_LAST));
464 else if (playlist_create(c->currdir, NULL) != -1)
466 start_index = ft_build_playlist(c, c->selected_item);
467 if (global_settings.playlist_shuffle)
469 start_index = playlist_shuffle(seed, start_index);
471 /* when shuffling dir.: play all files
472 even if the file selected by user is
473 not the first one */
474 if (!global_settings.play_selected)
475 start_index = 0;
478 playlist_start(start_index, 0);
479 play = true;
481 break;
483 #if CONFIG_TUNER
484 /* fmr preset file */
485 case FILE_ATTR_FMR:
486 splash(0, ID2P(LANG_WAIT));
488 /* Preset inside the default folder. */
489 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
491 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
492 radio_load_presets(global_settings.fmr_file);
493 if(!in_radio_screen())
494 radio_screen();
497 * Preset outside default folder, we can choose such only
498 * if we are out of the radio screen, so the check for the
499 * radio status isn't neccessary
501 else
503 radio_load_presets(buf);
504 radio_screen();
507 break;
508 #endif
510 #ifdef HAVE_LCD_BITMAP
511 case FILE_ATTR_SBS:
512 splash(0, ID2P(LANG_WAIT));
513 set_file(buf, (char *)global_settings.sbs_file,
514 MAX_FILENAME);
515 global_settings.statusbar = STATUSBAR_CUSTOM;
516 settings_apply_skins();
517 break;
518 #endif
519 #ifdef HAVE_REMOTE_LCD
520 case FILE_ATTR_RSBS:
521 splash(0, ID2P(LANG_WAIT));
522 set_file(buf, (char *)global_settings.rsbs_file,
523 MAX_FILENAME);
524 global_settings.remote_statusbar = STATUSBAR_CUSTOM;
525 settings_apply_skins();
526 break;
527 #endif
528 /* wps config file */
529 case FILE_ATTR_WPS:
530 splash(0, ID2P(LANG_WAIT));
531 set_file(buf, (char *)global_settings.wps_file,
532 MAX_FILENAME);
533 settings_apply_skins();
534 break;
536 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
537 /* remote-wps config file */
538 case FILE_ATTR_RWPS:
539 splash(0, ID2P(LANG_WAIT));
540 set_file(buf, (char *)global_settings.rwps_file,
541 MAX_FILENAME);
542 settings_apply_skins();
543 break;
544 #endif
546 case FILE_ATTR_CFG:
547 splash(0, ID2P(LANG_WAIT));
548 if (!settings_load_config(buf,true))
549 break;
550 splash(HZ, ID2P(LANG_SETTINGS_LOADED));
551 break;
553 case FILE_ATTR_BMARK:
554 splash(0, ID2P(LANG_WAIT));
555 bookmark_load(buf, false);
556 reload_dir = true;
557 break;
559 case FILE_ATTR_LNG:
560 splash(0, ID2P(LANG_WAIT));
561 if (lang_core_load(buf))
563 splash(HZ, ID2P(LANG_FAILED));
564 break;
566 set_file(buf, (char *)global_settings.lang_file,
567 MAX_FILENAME);
568 talk_init(); /* use voice of same language */
569 viewportmanager_theme_changed(THEME_LANGUAGE);
570 settings_apply_skins();
571 splash(HZ, ID2P(LANG_LANGUAGE_LOADED));
572 break;
574 #ifdef HAVE_LCD_BITMAP
575 case FILE_ATTR_FONT:
576 ft_load_font(buf);
577 break;
579 case FILE_ATTR_KBD:
580 splash(0, ID2P(LANG_WAIT));
581 if (!load_kbd(buf))
582 splash(HZ, ID2P(LANG_KEYBOARD_LOADED));
583 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
584 break;
585 #endif
587 #ifndef SIMULATOR
588 /* firmware file */
589 case FILE_ATTR_MOD:
590 splash(0, ID2P(LANG_WAIT));
591 rolo_load(buf);
592 break;
593 #endif
595 /* plugin file */
596 case FILE_ATTR_ROCK:
597 case FILE_ATTR_LUA:
599 char *plugin = buf, *argument = NULL;
600 int ret;
602 if ((file->attr & FILE_ATTR_MASK) == FILE_ATTR_LUA) {
603 plugin = VIEWERS_DIR "/lua.rock"; /* Use a #define here ? */
604 argument = buf;
607 if (global_settings.party_mode && audio_status()) {
608 splash(HZ, ID2P(LANG_PARTY_MODE));
609 break;
611 ret = plugin_load(plugin, argument);
612 switch (ret)
614 case PLUGIN_GOTO_WPS:
615 play = true;
616 break;
617 case PLUGIN_USB_CONNECTED:
618 if(*c->dirfilter > NUM_FILTER_MODES)
619 /* leave sub-browsers after usb, doing
620 otherwise might be confusing to the user */
621 exit_func = true;
622 else
623 reload_dir = true;
624 break;
626 case PLUGIN_ERROR:
627 case PLUGIN_OK:
629 default:
630 break;
632 break;
634 case FILE_ATTR_CUE:
635 display_cuesheet_content(buf);
636 break;
638 default:
640 const char* plugin;
642 if (global_settings.party_mode && audio_status()) {
643 splash(HZ, ID2P(LANG_PARTY_MODE));
644 break;
647 plugin = filetype_get_plugin(file);
648 if (plugin)
650 switch (plugin_load(plugin,buf))
652 case PLUGIN_USB_CONNECTED:
653 reload_dir = true;
654 break;
655 case PLUGIN_GOTO_WPS:
656 play = true;
657 break;
659 case PLUGIN_OK:
660 case PLUGIN_ERROR:
662 default:
663 break;
666 break;
670 if ( play ) {
671 /* the resume_index must always be the index in the
672 shuffled list in case shuffle is enabled */
673 global_status.resume_index = start_index;
674 global_status.resume_offset = 0;
675 status_save();
677 start_wps = true;
679 else {
680 if (*c->dirfilter > NUM_FILTER_MODES &&
681 *c->dirfilter != SHOW_CFG &&
682 *c->dirfilter != SHOW_FONT &&
683 *c->dirfilter != SHOW_PLUGINS)
685 exit_func = true;
690 if (reload_dir)
691 rc = 1;
692 if (start_wps)
693 rc = 2;
694 if (exit_func)
695 rc = 3;
697 return rc;
700 int ft_exit(struct tree_context* c)
702 extern char lastfile[]; /* from tree.c */
703 char buf[MAX_PATH];
704 int rc = 0;
705 bool exit_func = false;
707 int i = strlen(c->currdir);
708 if (i>1) {
709 while (c->currdir[i-1]!='/')
710 i--;
711 strcpy(buf,&c->currdir[i]);
712 if (i==1)
713 c->currdir[i]=0;
714 else
715 c->currdir[i-1]=0;
717 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
718 exit_func = true;
720 c->dirlevel--;
721 if ( c->dirlevel < MAX_DIR_LEVELS )
722 c->selected_item=c->selected_item_history[c->dirlevel];
723 else
724 c->selected_item=0;
726 /* if undefined position */
727 if (c->selected_item == -1)
728 strcpy(lastfile, buf);
730 else
732 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
733 exit_func = true;
736 if (exit_func)
737 rc = 3;
739 return rc;