Use size_t type for the buffer_size parameter to formatter functions, static two...
[Rockbox.git] / apps / filetree.c
blob35be92e4b50d5f4f1851c8b8e2be1e37cfb7e765
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 #ifdef HAVE_LCD_BITMAP
45 #include "keyboard.h"
46 #endif
48 #if CONFIG_TUNER
49 #include "radio.h"
50 #endif
52 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
53 #include "backdrop.h"
54 #endif
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 DEBUGF("Adding %s\n", dircache[i].name);
68 if (playlist_add(dircache[i].name) < 0)
69 break;
71 else
73 /* Adjust the start index when se skip non-MP3 entries */
74 if(i < start)
75 start_index--;
79 return start_index;
82 /* walk a directory and check all dircache entries if a .talk file exists */
83 static void check_file_thumbnails(struct tree_context* c)
85 int i;
86 struct dirent *entry;
87 struct entry* dircache = c->dircache;
88 DIR *dir;
90 dir = opendir(c->currdir);
91 if(!dir)
92 return;
93 /* mark all files as non talking, except the .talk ones */
94 for (i=0; i < c->filesindir; i++)
96 if (dircache[i].attr & ATTR_DIRECTORY)
97 continue; /* we're not touching directories */
99 if (strcasecmp(file_thumbnail_ext,
100 &dircache[i].name[strlen(dircache[i].name)
101 - strlen(file_thumbnail_ext)]))
102 { /* no .talk file */
103 dircache[i].attr &= ~FILE_ATTR_THUMBNAIL; /* clear */
105 else
106 { /* .talk file, we later let them speak themselves */
107 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set */
111 while((entry = readdir(dir)) != 0) /* walk directory */
113 int ext_pos;
115 ext_pos = strlen((char *)entry->d_name) - strlen(file_thumbnail_ext);
116 if (ext_pos <= 0 /* too short to carry ".talk" */
117 || (entry->attribute & ATTR_DIRECTORY) /* no file */
118 || strcasecmp((char *)&entry->d_name[ext_pos], file_thumbnail_ext))
119 { /* or doesn't end with ".talk", no candidate */
120 continue;
123 /* terminate the (disposable) name in dir buffer,
124 this truncates off the ".talk" without needing an extra buffer */
125 entry->d_name[ext_pos] = '\0';
127 /* search corresponding file in dir cache */
128 for (i=0; i < c->filesindir; i++)
130 if (!strcasecmp(dircache[i].name, (char *)entry->d_name))
131 { /* match */
132 dircache[i].attr |= FILE_ATTR_THUMBNAIL; /* set the flag */
133 break; /* exit search loop, because we found it */
137 closedir(dir);
140 /* support function for qsort() */
141 static int compare(const void* p1, const void* p2)
143 struct entry* e1 = (struct entry*)p1;
144 struct entry* e2 = (struct entry*)p2;
145 int criteria;
147 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
148 { /* two directories */
149 criteria = global_settings.sort_dir;
151 #ifdef HAVE_MULTIVOLUME
152 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
153 { /* a volume identifier is involved */
154 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
155 criteria = 0; /* two volumes: sort alphabetically */
156 else /* only one is a volume: volume first */
157 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
159 #endif
162 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
163 { /* two files */
164 criteria = global_settings.sort_file;
166 else /* dir and file, dir goes first */
167 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
169 switch(criteria)
171 case 3: /* sort type */
173 int t1 = e1->attr & FILE_ATTR_MASK;
174 int t2 = e2->attr & FILE_ATTR_MASK;
176 if (!t1) /* unknown type */
177 t1 = INT_MAX; /* gets a high number, to sort after known */
178 if (!t2) /* unknown type */
179 t2 = INT_MAX; /* gets a high number, to sort after known */
181 if (t1 - t2) /* if different */
182 return t1 - t2;
183 /* else fall through to alphabetical sorting */
185 case 0: /* sort alphabetically asc */
186 if (global_settings.sort_case)
187 return strncmp(e1->name, e2->name, MAX_PATH);
188 else
189 return strncasecmp(e1->name, e2->name, MAX_PATH);
191 case 4: /* sort alphabetically desc */
192 if (global_settings.sort_case)
193 return strncmp(e2->name, e1->name, MAX_PATH);
194 else
195 return strncasecmp(e2->name, e1->name, MAX_PATH);
197 case 1: /* sort date */
198 return e1->time_write - e2->time_write;
200 case 2: /* sort date, newest first */
201 return e2->time_write - e1->time_write;
203 return 0; /* never reached */
206 /* load and sort directory into dircache. returns NULL on failure. */
207 int ft_load(struct tree_context* c, const char* tempdir)
209 int i;
210 int name_buffer_used = 0;
211 DIR *dir;
213 if (tempdir)
214 dir = opendir(tempdir);
215 else
216 dir = opendir(c->currdir);
217 if(!dir)
218 return -1; /* not a directory */
220 c->dirsindir = 0;
221 c->dirfull = false;
223 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
224 int len;
225 struct dirent *entry = readdir(dir);
226 struct entry* dptr =
227 (struct entry*)(c->dircache + i * sizeof(struct entry));
228 if (!entry)
229 break;
231 len = strlen((char *)entry->d_name);
233 /* skip directories . and .. */
234 if ((entry->attribute & ATTR_DIRECTORY) &&
235 (((len == 1) && (!strncmp((char *)entry->d_name, ".", 1))) ||
236 ((len == 2) && (!strncmp((char *)entry->d_name, "..", 2))))) {
237 i--;
238 continue;
241 /* Skip FAT volume ID */
242 if (entry->attribute & ATTR_VOLUME_ID) {
243 i--;
244 continue;
247 /* filter out dotfiles and hidden files */
248 if (*c->dirfilter != SHOW_ALL &&
249 ((entry->d_name[0]=='.') ||
250 (entry->attribute & ATTR_HIDDEN))) {
251 i--;
252 continue;
255 dptr->attr = entry->attribute;
257 /* check for known file types */
258 if ( !(dptr->attr & ATTR_DIRECTORY) )
259 dptr->attr |= filetype_get_attr((char *)entry->d_name);
261 /* filter out non-visible files */
262 if ((!(dptr->attr & ATTR_DIRECTORY) && (
263 (*c->dirfilter == SHOW_PLAYLIST &&
264 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
265 ((*c->dirfilter == SHOW_MUSIC &&
266 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
267 (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) ||
268 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)))) ||
269 (*c->dirfilter == SHOW_WPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_WPS) ||
270 #ifdef HAVE_REMOTE_LCD
271 (*c->dirfilter == SHOW_RWPS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_RWPS) ||
272 #endif
273 #if CONFIG_TUNER
274 (*c->dirfilter == SHOW_FMR && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FMR) ||
275 #endif
276 (*c->dirfilter == SHOW_CFG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_CFG) ||
277 (*c->dirfilter == SHOW_LNG && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_LNG) ||
278 (*c->dirfilter == SHOW_MOD && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_MOD) ||
279 (*c->dirfilter == SHOW_FONT && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_FONT) ||
280 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & FILE_ATTR_MASK) != FILE_ATTR_ROCK))
282 i--;
283 continue;
286 if (len > c->name_buffer_size - name_buffer_used - 1) {
287 /* Tell the world that we ran out of buffer space */
288 c->dirfull = true;
289 break;
291 dptr->name = &c->name_buffer[name_buffer_used];
292 dptr->time_write =
293 (long)entry->wrtdate<<16 |
294 (long)entry->wrttime; /* in one # */
295 strcpy(dptr->name, (char *)entry->d_name);
296 name_buffer_used += len + 1;
298 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
299 c->dirsindir++;
301 c->filesindir = i;
302 c->dirlength = i;
303 closedir(dir);
305 qsort(c->dircache,i,sizeof(struct entry),compare);
307 /* If thumbnail talking is enabled, make an extra run to mark files with
308 associated thumbnails, so we don't do unsuccessful spinups later. */
309 if (global_settings.talk_file_clip)
310 check_file_thumbnails(c); /* map .talk to ours */
312 return 0;
315 int ft_enter(struct tree_context* c)
317 int rc = 0;
318 char buf[MAX_PATH];
319 struct entry *dircache = c->dircache;
320 struct entry* file = &dircache[c->selected_item];
321 bool reload_dir = false;
322 bool start_wps = false;
323 bool exit_func = false;
325 if (c->currdir[1])
326 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
327 else
328 snprintf(buf,sizeof(buf),"/%s",file->name);
330 if (file->attr & ATTR_DIRECTORY) {
331 memcpy(c->currdir, buf, sizeof(c->currdir));
332 if ( c->dirlevel < MAX_DIR_LEVELS )
333 c->selected_item_history[c->dirlevel] = c->selected_item;
334 c->dirlevel++;
335 c->selected_item=0;
337 else {
338 int seed = current_tick;
339 bool play = false;
340 int start_index=0;
342 switch ( file->attr & FILE_ATTR_MASK ) {
343 case FILE_ATTR_M3U:
344 if (global_settings.party_mode) {
345 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
346 break;
349 if (bookmark_autoload(buf))
350 break;
352 gui_syncsplash(0, ID2P(LANG_WAIT));
354 /* about to create a new current playlist...
355 allow user to cancel the operation */
356 if (global_settings.warnon_erase_dynplaylist &&
357 playlist_modified(NULL))
359 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
360 struct text_message message={lines, 1};
362 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
363 break;
366 if (playlist_create(c->currdir, file->name) != -1)
368 if (global_settings.playlist_shuffle)
369 playlist_shuffle(seed, -1);
370 start_index = 0;
371 playlist_start(start_index,0);
372 play = true;
374 break;
376 case FILE_ATTR_AUDIO:
377 if (bookmark_autoload(c->currdir))
378 break;
380 gui_syncsplash(0, ID2P(LANG_WAIT));
382 /* about to create a new current playlist...
383 allow user to cancel the operation */
384 if (global_settings.warnon_erase_dynplaylist &&
385 !global_settings.party_mode &&
386 playlist_modified(NULL))
388 char *lines[]={ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
389 struct text_message message={lines, 1};
391 if(gui_syncyesno_run(&message, NULL, NULL) != YESNO_YES)
392 break;
395 if (global_settings.party_mode)
397 playlist_insert_track(NULL, buf,
398 PLAYLIST_INSERT_LAST, true, true);
399 gui_syncsplash(HZ, ID2P(LANG_QUEUE_LAST));
401 else if (playlist_create(c->currdir, NULL) != -1)
403 start_index = ft_build_playlist(c, c->selected_item);
404 if (global_settings.playlist_shuffle)
406 start_index = playlist_shuffle(seed, start_index);
408 /* when shuffling dir.: play all files
409 even if the file selected by user is
410 not the first one */
411 if (!global_settings.play_selected)
412 start_index = 0;
415 playlist_start(start_index, 0);
416 play = true;
418 break;
420 #if CONFIG_TUNER
421 /* fmr preset file */
422 case FILE_ATTR_FMR:
424 gui_syncsplash(0, ID2P(LANG_WAIT));
426 /* Preset inside the default folder. */
427 if(!strncasecmp(FMPRESET_PATH, buf, strlen(FMPRESET_PATH)))
429 set_file(buf, global_settings.fmr_file, MAX_FILENAME);
430 radio_load_presets(global_settings.fmr_file);
431 if(!in_radio_screen())
432 radio_screen();
435 * Preset outside default folder, we can choose such only
436 * if we are out of the radio screen, so the check for the
437 * radio status isn't neccessary
439 else
441 radio_load_presets(buf);
442 radio_screen();
445 break;
446 #endif
449 /* wps config file */
450 case FILE_ATTR_WPS:
451 gui_syncsplash(0, ID2P(LANG_WAIT));
452 #if LCD_DEPTH > 1
453 unload_wps_backdrop();
454 #endif
455 wps_data_load(gui_wps[0].data, buf, true);
456 set_file(buf, (char *)global_settings.wps_file,
457 MAX_FILENAME);
458 break;
460 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
461 /* remote-wps config file */
462 case FILE_ATTR_RWPS:
463 gui_syncsplash(0, ID2P(LANG_WAIT));
464 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
465 unload_remote_wps_backdrop();
466 #endif
467 wps_data_load(gui_wps[1].data, buf, true);
468 set_file(buf, (char *)global_settings.rwps_file,
469 MAX_FILENAME);
470 break;
471 #endif
473 case FILE_ATTR_CFG:
474 gui_syncsplash(0, ID2P(LANG_WAIT));
475 if (!settings_load_config(buf,true))
476 break;
477 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_LOADED));
478 break;
480 case FILE_ATTR_BMARK:
481 gui_syncsplash(0, ID2P(LANG_WAIT));
482 bookmark_load(buf, false);
483 reload_dir = true;
484 break;
486 case FILE_ATTR_LNG:
487 gui_syncsplash(0, ID2P(LANG_WAIT));
488 if(!lang_load(buf)) {
489 set_file(buf, (char *)global_settings.lang_file,
490 MAX_FILENAME);
491 talk_init(); /* use voice of same language */
492 gui_syncsplash(HZ, ID2P(LANG_LANGUAGE_LOADED));
494 break;
496 #ifdef HAVE_LCD_BITMAP
497 case FILE_ATTR_FONT:
498 gui_syncsplash(0, ID2P(LANG_WAIT));
499 font_load(buf);
500 set_file(buf, (char *)global_settings.font_file, MAX_FILENAME);
501 break;
503 case FILE_ATTR_KBD:
504 gui_syncsplash(0, ID2P(LANG_WAIT));
505 if (!load_kbd(buf))
506 gui_syncsplash(HZ, ID2P(LANG_KEYBOARD_LOADED));
507 set_file(buf, (char *)global_settings.kbd_file, MAX_FILENAME);
508 break;
509 #endif
511 #ifndef SIMULATOR
512 /* firmware file */
513 case FILE_ATTR_MOD:
514 gui_syncsplash(0, ID2P(LANG_WAIT));
515 rolo_load(buf);
516 break;
517 #endif
519 /* plugin file */
520 case FILE_ATTR_ROCK:
521 if (global_settings.party_mode) {
522 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
523 break;
526 gui_syncsplash(0, ID2P(LANG_WAIT));
528 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
530 if(*c->dirfilter > NUM_FILTER_MODES)
531 /* leave sub-browsers after usb, doing
532 otherwise might be confusing to the user */
533 exit_func = true;
534 else
535 reload_dir = true;
537 break;
539 case FILE_ATTR_CUE:
540 display_cuesheet_content(buf);
541 break;
543 default:
545 char* plugin;
547 if (global_settings.party_mode) {
548 gui_syncsplash(HZ, ID2P(LANG_PARTY_MODE));
549 break;
552 plugin = filetype_get_plugin(file);
553 if (plugin)
555 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
556 reload_dir = true;
558 break;
562 if ( play ) {
563 /* the resume_index must always be the index in the
564 shuffled list in case shuffle is enabled */
565 global_status.resume_index = start_index;
566 global_status.resume_offset = 0;
567 status_save();
569 start_wps = true;
571 else {
572 if (*c->dirfilter > NUM_FILTER_MODES &&
573 *c->dirfilter != SHOW_FONT &&
574 *c->dirfilter != SHOW_PLUGINS)
576 exit_func = true;
581 if (reload_dir)
582 rc = 1;
583 if (start_wps)
584 rc = 2;
585 if (exit_func)
586 rc = 3;
588 return rc;
591 int ft_exit(struct tree_context* c)
593 extern char lastfile[]; /* from tree.c */
594 char buf[MAX_PATH];
595 int rc = 0;
596 bool exit_func = false;
598 int i = strlen(c->currdir);
599 if (i>1) {
600 while (c->currdir[i-1]!='/')
601 i--;
602 strcpy(buf,&c->currdir[i]);
603 if (i==1)
604 c->currdir[i]=0;
605 else
606 c->currdir[i-1]=0;
608 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
609 exit_func = true;
611 c->dirlevel--;
612 if ( c->dirlevel < MAX_DIR_LEVELS )
613 c->selected_item=c->selected_item_history[c->dirlevel];
614 else
615 c->selected_item=0;
617 /* if undefined position */
618 if (c->selected_item == -1)
619 strcpy(lastfile, buf);
621 else
623 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
624 exit_func = true;
627 if (exit_func)
628 rc = 3;
630 return rc;