diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-05-28 16:26:12 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-05-28 16:26:12 +0000 |
commit | 361aaf6acd1881141d681bded83575320be7feb5 (patch) | |
tree | 23284da0bceba652c299eaaab0fc4729da87c395 /apps | |
parent | 4886f19810f809389f049ea060bbbe2238a48c99 (diff) | |
download | rockbox-361aaf6acd1881141d681bded83575320be7feb5.tar.gz rockbox-361aaf6acd1881141d681bded83575320be7feb5.zip |
Now caches dir. 200 files max, no sorting yet
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@771 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r-- | apps/tree.c | 194 |
1 files changed, 84 insertions, 110 deletions
diff --git a/apps/tree.c b/apps/tree.c index 30548139ad..a7f871caa1 100644 --- a/apps/tree.c +++ b/apps/tree.c @@ -30,21 +30,23 @@ #include "tree.h" #include "play.h" #include "main_menu.h" -#include "mpeg.h" #ifdef HAVE_LCD_BITMAP #include "icons.h" #endif +#define MAX_FILES_IN_DIR 200 #define TREE_MAX_FILENAMELEN 128 #define MAX_DIR_LEVELS 10 struct entry { bool file; /* true if file, false if dir */ char name[TREE_MAX_FILENAMELEN]; - int namelen; }; +static struct entry buffer[MAX_FILES_IN_DIR]; +static int filesindir; + void browse_root(void) { dirbrowse("/"); @@ -63,6 +65,7 @@ void browse_root(void) #define LINE_HEIGTH 8 /* pixels for each text line */ extern unsigned char bitmap_icons_6x8[LastIcon][6]; +extern icons_6x8; #else /* HAVE_LCD_BITMAP */ @@ -73,88 +76,83 @@ extern unsigned char bitmap_icons_6x8[LastIcon][6]; #endif /* HAVE_LCD_BITMAP */ -static int showdir(char *path, struct entry *buffer, int start, - int scrollpos, int* at_end) +static int showdir(char *path, int start) { + static char lastdir[256] = {0}; + #ifdef HAVE_LCD_BITMAP int icon_type = 0; #endif int i; - int j=0; - DIR *dir = opendir(path); - struct dirent *entry; - - if(!dir) - return -1; /* not a directory */ - - i=start; - *at_end=0; /* Have we displayed the last directory entry? */ - while((entry = readdir(dir))) { - int len; - - if(entry->d_name[0] == '.') - /* skip names starting with a dot */ - continue; - if(j++ < scrollpos) - continue ; + /* new dir? cache it */ + if (strncmp(path,lastdir,sizeof(lastdir))) { + debugf("Caching dir %s\n",path); + DIR *dir = opendir(path); + if(!dir) + return -1; /* not a directory */ + for ( i=0; i<MAX_FILES_IN_DIR; i++ ) { + struct dirent *entry = readdir(dir); + if (!entry) + break; + if(entry->d_name[0] == '.') { + /* skip names starting with a dot */ + i--; + continue; + } + buffer[i].file = !(entry->attribute & ATTR_DIRECTORY); + strncpy(buffer[i].name,entry->d_name,TREE_MAX_FILENAMELEN); + buffer[i].name[TREE_MAX_FILENAMELEN-1]=0; + } + filesindir = i; + closedir(dir); + strncpy(lastdir,path,sizeof(lastdir)); + lastdir[sizeof(lastdir)-1] = 0; + } - len = strlen(entry->d_name); - if(len < TREE_MAX_FILENAMELEN) - /* strncpy() is evil, we memcpy() instead, +1 includes the - trailing zero */ - memcpy(buffer[i].name, entry->d_name, len+1); - else - memcpy(buffer[i].name, "too long", 9); + lcd_clear_display(); +#ifdef HAVE_LCD_BITMAP + lcd_putsxy(0,0, "[Browse]",0); + lcd_update(); +#endif - buffer[i].file = !(entry->attribute&ATTR_DIRECTORY); + for ( i=start; i < start+TREE_MAX_ON_SCREEN; i++ ) { + if ( i < filesindir ) { + int len = strlen(buffer[i].name); #ifdef HAVE_LCD_BITMAP - if ( buffer[i].file ) - icon_type=File; - else - icon_type=Folder; - lcd_bitmap(bitmap_icons_6x8[icon_type], 6, MARGIN_Y+i*LINE_HEIGTH, 6, - 8, true); + if ( buffer[i].file ) + icon_type=File; + else + icon_type=Folder; + lcd_bitmap(bitmap_icons_6x8[icon_type], 6, MARGIN_Y+i*LINE_HEIGTH, 6, + 8, true); #endif - if(len < TREE_MAX_LEN_DISPLAY) - lcd_puts(LINE_X, LINE_Y+i, buffer[i].name); - else { - char storage = buffer[i].name[TREE_MAX_LEN_DISPLAY]; - buffer[i].name[TREE_MAX_LEN_DISPLAY]=0; - lcd_puts(LINE_X, LINE_Y+i, buffer[i].name); - buffer[i].name[TREE_MAX_LEN_DISPLAY]=storage; + if(len < TREE_MAX_LEN_DISPLAY) + lcd_puts(LINE_X, LINE_Y+i-start, buffer[i].name); + else { + char storage = buffer[i].name[TREE_MAX_LEN_DISPLAY]; + buffer[i].name[TREE_MAX_LEN_DISPLAY]=0; + lcd_puts(LINE_X, LINE_Y+i-start, buffer[i].name); + buffer[i].name[TREE_MAX_LEN_DISPLAY]=storage; + } } - - if(++i >= TREE_MAX_ON_SCREEN) - break; - } - - if (entry==0) { - *at_end=1; - } else { - *at_end=(readdir(dir)==0); - } - j = i ; - while (j++ < TREE_MAX_ON_SCREEN) { - lcd_puts(LINE_X, LINE_Y+j," "); + else + lcd_puts(LINE_X, LINE_Y+i-start," "); } - closedir(dir); - return i; + return filesindir; } bool dirbrowse(char *root) { - struct entry buffer[TREE_MAX_ON_SCREEN]; int numentries; char buf[255]; char currdir[255]; int dircursor=0; int i; int start=0; - int at_end=0; int dirpos[MAX_DIR_LEVELS]; int dirlevel=0; @@ -167,7 +165,7 @@ bool dirbrowse(char *root) #endif memcpy(currdir,root,sizeof(currdir)); - numentries = showdir(root, buffer, 0, start, &at_end); + numentries = showdir(root, start); if (numentries == -1) return -1; /* root is not a directory */ @@ -178,7 +176,13 @@ bool dirbrowse(char *root) #endif while(1) { - switch ( button_get(true) ) { + int key = button_get(); + + if(!key) { + sleep(1); + continue; + } + switch(key) { #if defined(SIMULATOR) && defined(HAVE_RECODER_KEYPAD) case BUTTON_OFF: return false; @@ -199,28 +203,19 @@ bool dirbrowse(char *root) else currdir[i-1]=0; - lcd_clear_display(); -#ifdef HAVE_LCD_BITMAP - lcd_putsxy(0,0, "[Browse]",0); -#endif dirlevel--; if ( dirlevel < MAX_DIR_LEVELS ) start = dirpos[dirlevel]; else start = 0; - numentries = showdir(currdir, buffer, 0, start, &at_end); + numentries = showdir(currdir, start); dircursor=0; while ( (dircursor < TREE_MAX_ON_SCREEN) && - (strcmp(buffer[dircursor].name,buf)!=0)) + (strcmp(buffer[dircursor+start].name,buf)!=0)) dircursor++; if (dircursor==TREE_MAX_ON_SCREEN) dircursor=0; lcd_puts(0, LINE_Y+dircursor, "-"); - lcd_update(); - } - else { - /* if in root, stop stops playback */ - mpeg_stop(); } break; @@ -230,12 +225,12 @@ bool dirbrowse(char *root) #endif case BUTTON_PLAY: if ((currdir[0]=='/') && (currdir[1]==0)) { - snprintf(buf,sizeof(buf),"%s%s",currdir,buffer[dircursor].name); + snprintf(buf,sizeof(buf),"%s%s",currdir,buffer[dircursor+start].name); } else { - snprintf(buf,sizeof(buf),"%s/%s",currdir,buffer[dircursor].name); + snprintf(buf,sizeof(buf),"%s/%s",currdir,buffer[dircursor+start].name); } - if (!buffer[dircursor].file) { + if (!buffer[dircursor+start].file) { memcpy(currdir,buf,sizeof(currdir)); if ( dirlevel < MAX_DIR_LEVELS ) dirpos[dirlevel] = start+dircursor; @@ -243,20 +238,15 @@ bool dirbrowse(char *root) dircursor=0; start=0; } else { - playtune(currdir, buffer[dircursor].name); + playtune(currdir, buffer[dircursor+start].name); #ifdef HAVE_LCD_BITMAP lcd_setmargins(0, MARGIN_Y); lcd_setfont(0); #endif } - lcd_clear_display(); - numentries = showdir(currdir, buffer, 0, start, &at_end); + numentries = showdir(currdir, start); lcd_puts(0, LINE_Y+dircursor, "-"); -#ifdef HAVE_LCD_BITMAP - lcd_putsxy(0,0, "[Browse]",0); - lcd_update(); -#endif break; #ifdef HAVE_RECORDER_KEYPAD @@ -271,16 +261,10 @@ bool dirbrowse(char *root) lcd_update(); } else { - if (start) { - lcd_clear_display(); + if (start) { start--; - numentries = showdir(currdir, buffer, 0, - start, &at_end); + numentries = showdir(currdir, start); lcd_puts(0, LINE_Y+dircursor, "-"); -#ifdef HAVE_LCD_BITMAP - lcd_putsxy(0,0, "[Browse]",0); - lcd_update(); -#endif } } break; @@ -290,27 +274,17 @@ bool dirbrowse(char *root) #else case BUTTON_RIGHT: #endif - if(dircursor+1 < numentries) { + if(dircursor+1 < TREE_MAX_ON_SCREEN) { lcd_puts(0, LINE_Y+dircursor, " "); dircursor++; lcd_puts(0, LINE_Y+dircursor, "-"); -#ifdef HAVE_LCD_BITMAP - lcd_update(); -#endif - } else - { - if (!at_end) { - lcd_clear_display(); - start++; - numentries = showdir(currdir, buffer, 0, - start, &at_end); - lcd_puts(0, LINE_Y+dircursor, "-"); -#ifdef HAVE_LCD_BITMAP - lcd_putsxy(0,0, "[Browse]",0); - lcd_update(); -#endif - } + } + else { + start++; + numentries = showdir(currdir, start); + lcd_puts(0, LINE_Y+dircursor, "-"); } + debugf("s:%d d:%d\n",start,dircursor); break; #ifdef HAVE_RECORDER_KEYPAD @@ -330,18 +304,18 @@ bool dirbrowse(char *root) lcd_setmargins(0,MARGIN_Y); lcd_setfont(0); #endif - numentries = showdir(currdir, buffer, 0, start, &at_end); + numentries = showdir(currdir, start); dircursor=0; while ( (dircursor < TREE_MAX_ON_SCREEN) && - (strcmp(buffer[dircursor].name,buf)!=0)) + (strcmp(buffer[dircursor+start].name,buf)!=0)) dircursor++; if (dircursor==TREE_MAX_ON_SCREEN) dircursor=0; lcd_puts(0, LINE_Y+dircursor, "-"); - lcd_update(); break; } + lcd_update(); } return false; |