Reinserted patch 1.279 (was lost in move to filetree.c)
[kugel-rb.git] / apps / filetree.c
blobaaf99dce929009be1f3b7af04b0e6c693432d502
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 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 "bookmark.h"
28 #include "tree.h"
29 #include "settings.h"
30 #include "filetypes.h"
31 #include "talk.h"
32 #include "playlist.h"
33 #include "wps-display.h"
34 #include "lang.h"
35 #include "language.h"
36 #include "screens.h"
37 #include "plugin.h"
38 #include "rolo.h"
40 static int boot_size = 0;
41 static int boot_cluster;
42 extern bool boot_changed;
44 int ft_build_playlist(struct tree_context* c, int start_index)
46 int i;
47 int start=start_index;
49 struct entry *dircache = c->dircache;
51 for(i = 0;i < c->filesindir;i++)
53 if((dircache[i].attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
55 DEBUGF("Adding %s\n", dircache[i].name);
56 if (playlist_add(dircache[i].name) < 0)
57 break;
59 else
61 /* Adjust the start index when se skip non-MP3 entries */
62 if(i < start)
63 start_index--;
67 return start_index;
70 /* walk a directory and check all dircache entries if a .talk file exists */
71 static void check_file_thumbnails(struct tree_context* c)
73 int i;
74 struct dirent *entry;
75 struct entry* dircache = c->dircache;
76 DIR *dir;
78 dir = opendir(c->currdir);
79 if(!dir)
80 return;
82 for (i=0; i < c->filesindir; i++) /* mark all files as non talking, except the .talk ones */
84 if (dircache[i].attr & ATTR_DIRECTORY)
85 continue; /* we're not touching directories */
87 if (strcasecmp(file_thumbnail_ext,
88 &dircache[i].name[strlen(dircache[i].name)
89 - strlen(file_thumbnail_ext)]))
90 { /* no .talk file */
91 dircache[i].attr &= ~TREE_ATTR_THUMBNAIL; /* clear */
93 else
94 { /* .talk file, we later let them speak themselves */
95 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set */
99 while((entry = readdir(dir)) != 0) /* walk directory */
101 int ext_pos;
103 ext_pos = strlen(entry->d_name) - strlen(file_thumbnail_ext);
104 if (ext_pos <= 0 /* too short to carry ".talk" */
105 || (entry->attribute & ATTR_DIRECTORY) /* no file */
106 || strcasecmp(&entry->d_name[ext_pos], file_thumbnail_ext))
107 { /* or doesn't end with ".talk", no candidate */
108 continue;
111 /* terminate the (disposable) name in dir buffer,
112 this truncates off the ".talk" without needing an extra buffer */
113 entry->d_name[ext_pos] = '\0';
115 /* search corresponding file in dir cache */
116 for (i=0; i < c->filesindir; i++)
118 if (!strcasecmp(dircache[i].name, entry->d_name))
119 { /* match */
120 dircache[i].attr |= TREE_ATTR_THUMBNAIL; /* set the flag */
121 break; /* exit search loop, because we found it */
125 closedir(dir);
128 /* support function for qsort() */
129 static int compare(const void* p1, const void* p2)
131 struct entry* e1 = (struct entry*)p1;
132 struct entry* e2 = (struct entry*)p2;
133 int criteria;
135 if (e1->attr & ATTR_DIRECTORY && e2->attr & ATTR_DIRECTORY)
136 { /* two directories */
137 criteria = global_settings.sort_dir;
139 if (e1->attr & ATTR_VOLUME || e2->attr & ATTR_VOLUME)
140 { /* a volume identifier is involved */
141 if (e1->attr & ATTR_VOLUME && e2->attr & ATTR_VOLUME)
142 criteria = 0; /* two volumes: sort alphabetically */
143 else /* only one is a volume: volume first */
144 return (e2->attr & ATTR_VOLUME) - (e1->attr & ATTR_VOLUME);
147 else if (!(e1->attr & ATTR_DIRECTORY) && !(e2->attr & ATTR_DIRECTORY))
148 { /* two files */
149 criteria = global_settings.sort_file;
151 else /* dir and file, dir goes first */
152 return ( e2->attr & ATTR_DIRECTORY ) - ( e1->attr & ATTR_DIRECTORY );
154 switch(criteria)
156 case 3: /* sort type */
158 int t1 = e1->attr & TREE_ATTR_MASK;
159 int t2 = e2->attr & TREE_ATTR_MASK;
161 if (!t1) /* unknown type */
162 t1 = 0x7FFFFFFF; /* gets a high number, to sort after known */
163 if (!t2) /* unknown type */
164 t2 = 0x7FFFFFFF; /* gets a high number, to sort after known */
166 if (t1 - t2) /* if different */
167 return t1 - t2;
168 /* else fall through to alphabetical sorting */
170 case 0: /* sort alphabetically */
171 if (global_settings.sort_case)
172 return strncmp(e1->name, e2->name, MAX_PATH);
173 else
174 return strncasecmp(e1->name, e2->name, MAX_PATH);
176 case 1: /* sort date */
177 return e1->time_write - e2->time_write;
179 case 2: /* sort date, newest first */
180 return e2->time_write - e1->time_write;
182 return 0; /* never reached */
185 /* load and sort directory into dircache. returns NULL on failure. */
186 int ft_load(struct tree_context* c, bool *buffer_full)
188 extern char lastdir[]; /* from tree.c */
189 int i;
190 DIR *dir = opendir(c->currdir);
191 if(!dir)
192 return -1; /* not a directory */
194 int name_buffer_used = 0;
195 c->dirsindir = 0;
196 if (buffer_full)
197 *buffer_full = false;
199 for ( i=0; i < global_settings.max_files_in_dir; i++ ) {
200 int len;
201 struct dirent *entry = readdir(dir);
202 struct entry* dptr = (struct entry*)(c->dircache + i * sizeof(struct entry));
203 if (!entry)
204 break;
206 len = strlen(entry->d_name);
208 /* skip directories . and .. */
209 if ((entry->attribute & ATTR_DIRECTORY) &&
210 (((len == 1) &&
211 (!strncmp(entry->d_name, ".", 1))) ||
212 ((len == 2) &&
213 (!strncmp(entry->d_name, "..", 2))))) {
214 i--;
215 continue;
218 /* Skip FAT volume ID */
219 if (entry->attribute & ATTR_VOLUME_ID) {
220 i--;
221 continue;
224 /* filter out dotfiles and hidden files */
225 if (*c->dirfilter != SHOW_ALL &&
226 ((entry->d_name[0]=='.') ||
227 (entry->attribute & ATTR_HIDDEN))) {
228 i--;
229 continue;
232 dptr->attr = entry->attribute;
234 /* check for known file types */
235 if ( !(dptr->attr & ATTR_DIRECTORY) && (len > 4) )
236 dptr->attr |= filetype_get_attr(entry->d_name);
238 /* memorize/compare details about the boot file */
239 if ((c->currdir[1] == 0) && !strcasecmp(entry->d_name, BOOTFILE)) {
240 if (boot_size) {
241 if ((entry->size != boot_size) ||
242 (entry->startcluster != boot_cluster))
243 boot_changed = true;
245 boot_size = entry->size;
246 boot_cluster = entry->startcluster;
249 /* filter out non-visible files */
250 if (!(dptr->attr & ATTR_DIRECTORY) && (
251 (*c->dirfilter == SHOW_PLAYLIST &&
252 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
253 ((*c->dirfilter == SHOW_MUSIC &&
254 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MPA) &&
255 (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_M3U) ||
256 (*c->dirfilter == SHOW_SUPPORTED && !filetype_supported(dptr->attr)) ||
257 (*c->dirfilter == SHOW_WPS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_WPS) ||
258 (*c->dirfilter == SHOW_CFG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_CFG) ||
259 (*c->dirfilter == SHOW_LNG && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_LNG) ||
260 (*c->dirfilter == SHOW_MOD && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_MOD) ||
261 (*c->dirfilter == SHOW_FONT && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_FONT) ||
262 (*c->dirfilter == SHOW_PLUGINS && (dptr->attr & TREE_ATTR_MASK) != TREE_ATTR_ROCK)))
264 i--;
265 continue;
268 if (len > c->name_buffer_size - name_buffer_used - 1) {
269 /* Tell the world that we ran out of buffer space */
270 if (buffer_full)
271 *buffer_full = true;
272 break;
274 dptr->name = &c->name_buffer[name_buffer_used];
275 dptr->time_write = entry->wrtdate<<16 | entry->wrttime; /* in one # */
276 strcpy(dptr->name,entry->d_name);
277 name_buffer_used += len + 1;
279 if (dptr->attr & ATTR_DIRECTORY) /* count the remaining dirs */
280 c->dirsindir++;
282 c->filesindir = i;
283 closedir(dir);
285 strcpy(lastdir, c->currdir);
287 qsort(c->dircache,i,sizeof(struct entry),compare);
289 /* If thumbnail talking is enabled, make an extra run to mark files with
290 associated thumbnails, so we don't do unsuccessful spinups later. */
291 if (global_settings.talk_file == 3)
292 check_file_thumbnails(c); /* map .talk to ours */
294 return 0;
297 int ft_enter(struct tree_context* c)
299 int rc = 0;
300 char buf[MAX_PATH];
301 struct entry *dircache = c->dircache;
302 struct entry* file = &dircache[c->dircursor + c->dirstart];
303 bool reload_root = false;
304 bool reload_dir = false;
305 bool start_wps = false;
306 bool exit_func = false;
308 if (c->currdir[1])
309 snprintf(buf,sizeof(buf),"%s/%s",c->currdir, file->name);
310 else
311 snprintf(buf,sizeof(buf),"/%s",file->name);
313 if (file->attr & ATTR_DIRECTORY) {
314 memcpy(c->currdir, buf, sizeof(c->currdir));
315 if ( c->dirlevel < MAX_DIR_LEVELS ) {
316 c->dirpos[c->dirlevel] = c->dirstart;
317 c->cursorpos[c->dirlevel] = c->dircursor;
319 c->dirlevel++;
320 c->dircursor=0;
321 c->dirstart=0;
323 else {
324 int seed = current_tick;
325 bool play = false;
326 int start_index=0;
328 lcd_stop_scroll();
329 switch ( file->attr & TREE_ATTR_MASK ) {
330 case TREE_ATTR_M3U:
331 if (bookmark_autoload(buf))
332 break;
334 if (playlist_create(c->currdir, file->name) != -1)
336 if (global_settings.playlist_shuffle)
337 playlist_shuffle(seed, -1);
338 start_index = 0;
339 playlist_start(start_index,0);
340 play = true;
342 break;
344 case TREE_ATTR_MPA:
345 if (bookmark_autoload(c->currdir))
346 break;
348 if (playlist_create(c->currdir, NULL) != -1)
350 start_index =
351 ft_build_playlist(c, c->dircursor + c->dirstart);
352 if (global_settings.playlist_shuffle)
354 start_index = playlist_shuffle(seed, start_index);
356 /* when shuffling dir.: play all files
357 even if the file selected by user is
358 not the first one */
359 if (!global_settings.play_selected)
360 start_index = 0;
363 playlist_start(start_index, 0);
364 play = true;
366 break;
368 /* wps config file */
369 case TREE_ATTR_WPS:
370 wps_load(buf,true);
371 set_file(buf, global_settings.wps_file,
372 MAX_FILENAME);
373 break;
375 case TREE_ATTR_CFG:
376 if (!settings_load_config(buf))
377 break;
378 lcd_clear_display();
379 lcd_puts(0,0,str(LANG_SETTINGS_LOADED1));
380 lcd_puts(0,1,str(LANG_SETTINGS_LOADED2));
381 #ifdef HAVE_LCD_BITMAP
382 lcd_update();
383 #endif
384 sleep(HZ/2);
385 break;
387 case TREE_ATTR_BMARK:
388 bookmark_load(buf, false);
389 reload_dir = true;
390 break;
392 case TREE_ATTR_LNG:
393 if(!lang_load(buf)) {
394 extern bool language_changed; /* from settings_menu.c */
396 set_file(buf, global_settings.lang_file,
397 MAX_FILENAME);
398 talk_init(); /* use voice of same language */
399 splash(HZ, true, str(LANG_LANGUAGE_LOADED));
400 language_changed = true;
402 break;
404 #ifdef HAVE_LCD_BITMAP
405 case TREE_ATTR_FONT:
406 font_load(buf);
407 set_file(buf, global_settings.font_file, MAX_FILENAME);
408 break;
409 #endif
411 #ifndef SIMULATOR
412 /* firmware file */
413 case TREE_ATTR_MOD:
414 rolo_load(buf);
415 break;
416 #endif
418 /* plugin file */
419 case TREE_ATTR_ROCK:
420 if (plugin_load(buf,NULL) == PLUGIN_USB_CONNECTED)
422 if(*c->dirfilter > NUM_FILTER_MODES)
423 /* leave sub-browsers after usb, doing
424 otherwise might be confusing to the user */
425 exit_func = true;
426 else
427 reload_root = true;
429 break;
431 default:
433 char* plugin = filetype_get_plugin(file);
434 if (plugin)
436 if (plugin_load(plugin,buf) == PLUGIN_USB_CONNECTED)
437 reload_root = true;
439 break;
443 if ( play ) {
444 if ( global_settings.resume ) {
445 /* the resume_index must always be the index in the
446 shuffled list in case shuffle is enabled */
447 global_settings.resume_index = start_index;
448 global_settings.resume_offset = 0;
449 settings_save();
452 start_wps = true;
454 else {
455 if (*c->dirfilter > NUM_FILTER_MODES &&
456 *c->dirfilter != SHOW_FONT &&
457 *c->dirfilter != SHOW_PLUGINS)
459 exit_func = true;
464 if (reload_dir)
465 rc = 1;
466 if (reload_root)
467 rc = 2;
468 if (start_wps)
469 rc = 3;
470 if (exit_func)
471 rc = 4;
473 return rc;
476 int ft_exit(struct tree_context* c)
478 extern char lastfile[]; /* from tree.c */
479 char buf[MAX_PATH];
480 int rc = 0;
481 bool exit_func = false;
483 int i = strlen(c->currdir);
484 if (i>1) {
485 while (c->currdir[i-1]!='/')
486 i--;
487 strcpy(buf,&c->currdir[i]);
488 if (i==1)
489 c->currdir[i]=0;
490 else
491 c->currdir[i-1]=0;
493 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
494 exit_func = true;
496 c->dirlevel--;
497 if ( c->dirlevel < MAX_DIR_LEVELS ) {
498 c->dirstart = c->dirpos[c->dirlevel];
499 c->dircursor = c->cursorpos[c->dirlevel];
501 else
502 c->dirstart = c->dircursor = 0;
504 if (c->dirstart == -1)
505 strcpy(lastfile, buf);
507 else
509 if (*c->dirfilter > NUM_FILTER_MODES && c->dirlevel < 1)
510 exit_func = true;
513 if (exit_func)
514 rc = 4;
516 return rc;