Hardeep's browse current file patch.  When hitting stop while playing, you will be on the current song, also this is configurable


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2239 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/settings.c b/apps/settings.c
index 14a4a35..4dea1a0 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -67,7 +67,8 @@
 0x0b    0x1f    <backlight byte>
 0x0c    0x20    <poweroff timer byte>
 0x0d    0x21    <resume settings byte>
-0x0e    0x22    <shuffle,mp3filter,sort_case,discharge,statusbar,show_hidden>
+0x0e    0x22    <shuffle,mp3filter,sort_case,discharge,statusbar,show_hidden,
+                 browse_current>
 0x0f    0x23    <scroll speed>
 0x10    0x24    <ff/rewind min step, acceleration rate>
 0x11    0x25    <AVC byte>
@@ -266,7 +267,8 @@
          ((global_settings.discharge & 1) << 3) |
          ((global_settings.statusbar & 1) << 4) |
          ((global_settings.show_hidden_files & 1) << 5) |
-         ((global_settings.scrollbar & 1) << 6));
+         ((global_settings.scrollbar & 1) << 6) |
+         ((global_settings.browse_current & 1) << 7));
 
     config_block[0xf] = (unsigned char)(global_settings.scroll_speed << 3);
     
@@ -355,6 +357,7 @@
             global_settings.statusbar = (config_block[0xe] >> 4) & 1;
             global_settings.show_hidden_files = (config_block[0xe] >> 5) & 1;
             global_settings.scrollbar = (config_block[0xe] >> 6) & 1;
+            global_settings.browse_current = (config_block[0xe] >> 7) & 1;
         }
         
         c = config_block[0xf] >> 3;
@@ -543,6 +546,7 @@
     global_settings.resume_index = -1;
     global_settings.resume_offset = -1;
     global_settings.disk_spindown = 5;
+    global_settings.browse_current = false;
 }
 
 
diff --git a/apps/settings.h b/apps/settings.h
index 64b8608..82b087e 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -94,6 +94,10 @@
     bool show_hidden_files; /* 1=show dotfiles/hidden,
                                0=hide dotfiles/hidden */
     
+    /* goto current song when exiting WPS */
+    bool browse_current; /* 1=goto current song,
+                            0=goto previous location */
+
     /* geeky persistent statistics */
     unsigned int total_uptime; /* total uptime since rockbox was first booted */
 };
diff --git a/apps/settings_menu.c b/apps/settings_menu.c
index 17c72d5..03a59df 100644
--- a/apps/settings_menu.c
+++ b/apps/settings_menu.c
@@ -192,6 +192,12 @@
     return MENU_OK; 
 } 
 
+static Menu browse_current(void)
+{
+    set_bool( "Browse Current Song", &global_settings.browse_current );
+    return MENU_OK;
+}
+
 Menu playback_settings_menu(void)
 {
     int m;
@@ -266,6 +272,7 @@
         { "Sort Mode",       sort_case           },
         { "Music Filter",    mp3_filter          },
         { "Hidden Files",    show_hidden_files   },
+        { "Browse Current",  browse_current      },
     };
 
     m=menu_init( items, sizeof items / sizeof(struct menu_items) );
diff --git a/apps/tree.c b/apps/tree.c
index 9e16da6..b46835e 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -63,8 +63,15 @@
 };
 
 static struct entry dircache[MAX_FILES_IN_DIR];
+static int dircursor;
+static int dirstart;
+static int dirlevel;
 static int filesindir;
-static char lastdir[MAX_PATH] = {0};
+static int dirpos[MAX_DIR_LEVELS];
+static int cursorpos[MAX_DIR_LEVELS];
+static char lastdir[MAX_PATH];
+static char lastfile[MAX_PATH];
+static char currdir[MAX_PATH];
 
 void browse_root(void)
 {
@@ -294,6 +301,20 @@
         }
     }
 
+    if (start == -1)
+    {
+        /* use lastfile to determine start (default=0) */
+        start = dirstart = 0;
+        for (i=0; i<filesindir; i++)
+        {
+            if (!strcasecmp(dircache[i].name, lastfile))
+            {
+                start = dirstart = i;
+                break;
+            }
+        }
+    }
+
     lcd_stop_scroll();
 #ifdef HAVE_NEW_CHARCELL_LCD
     lcd_double_height(false);
@@ -476,15 +497,53 @@
     }
 }
 
+void set_current_file(char *path)
+{
+    char *name;
+    unsigned int i;
+
+    /* separate directory from filename */
+    name = strrchr(path,'/');
+    if (name)
+    {
+        *name = 0;
+        strcpy(currdir, path);
+        *name = '/';
+        name++;
+    }
+    else
+    {
+        strcpy(currdir, "/");
+        name = path+1;
+    }
+
+    strcpy(lastfile, name);
+
+    dircursor    =  0;
+    dirstart     = -1;
+
+    if (strncmp(currdir,lastdir,sizeof(lastdir)))
+    {
+        dirlevel            =  0;
+        dirpos[dirlevel]    = -1;
+        cursorpos[dirlevel] =  0;
+        
+        /* use '/' to calculate dirlevel */
+        for (i=1; i<strlen(path)+1; i++)
+        {
+            if (path[i] == '/')
+            {
+                dirlevel++;
+                dirpos[dirlevel]    = -1;
+                cursorpos[dirlevel] =  0;
+            }
+        }
+    }
+}
+
 bool dirbrowse(char *root)
 {
     int numentries=0;
-    int dircursor=0;
-    int start=0;
-    int dirpos[MAX_DIR_LEVELS];
-    int cursorpos[MAX_DIR_LEVELS];
-    int dirlevel=0;
-    char currdir[MAX_PATH];
     char buf[MAX_PATH];
     int i;
     int lasti=-1;
@@ -506,15 +565,19 @@
     start_resume();
     button_set_release(RELEASE_MASK);
 
+    dircursor=0;
+    dirstart=0;
+    dirlevel=0;
+
     memcpy(currdir,root,sizeof(currdir));
-    numentries = showdir(root, start);
+    numentries = showdir(root, dirstart);
     if (numentries == -1) 
         return -1;  /* root is not a directory */
 
     put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
 
     while(1) {
-        struct entry* file = &dircache[dircursor+start];
+        struct entry* file = &dircache[dircursor+dirstart];
 
         bool restore = false;
 
@@ -534,11 +597,15 @@
 
                     dirlevel--;
                     if ( dirlevel < MAX_DIR_LEVELS ) {
-                        start = dirpos[dirlevel];
+                        dirstart = dirpos[dirlevel];
                         dircursor = cursorpos[dirlevel];
                     }
                     else
-                        start = dircursor = 0;
+                        dirstart = dircursor = 0;
+
+                    if (dirstart == -1)
+                        strcpy(lastfile, buf);
+
                     restore = true;
                 }
                 break;
@@ -577,12 +644,12 @@
                 if (file->attr & ATTR_DIRECTORY) {
                     memcpy(currdir,buf,sizeof(currdir));
                     if ( dirlevel < MAX_DIR_LEVELS ) {
-                        dirpos[dirlevel] = start;
+                        dirpos[dirlevel] = dirstart;
                         cursorpos[dirlevel] = dircursor;
                     }
                     dirlevel++;
                     dircursor=0;
-                    start=0;
+                    dirstart=0;
                 } else {
                     int seed = current_tick;
                     bool play = false;
@@ -603,7 +670,7 @@
                             if ( global_settings.resume )
                                 strncpy(global_settings.resume_file,
                                         currdir, MAX_PATH);
-                            start_index = build_playlist(dircursor+start);
+                            start_index = build_playlist(dircursor+dirstart);
 
                             /* it is important that we get back the index in
                                the (shuffled) list and stor that */
@@ -675,9 +742,9 @@
                         put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
                     }
                     else {
-                        if (start) {
-                            start--;
-                            numentries = showdir(currdir, start);
+                        if (dirstart) {
+                            dirstart--;
+                            numentries = showdir(currdir, dirstart);
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
                         }
                         else {
@@ -689,9 +756,9 @@
                                              true);
                             }
                             else {
-                                start = numentries - tree_max_on_screen;
+                                dirstart = numentries - tree_max_on_screen;
                                 dircursor = tree_max_on_screen - 1;
-                                numentries = showdir(currdir, start);
+                                numentries = showdir(currdir, dirstart);
                                 put_cursorxy(CURSOR_X, CURSOR_Y +
                                              tree_max_on_screen - 1, true);
                             }
@@ -706,7 +773,7 @@
             case BUTTON_VOL_DOWN:
                 if(filesindir)
                 {
-                    if (dircursor + start + 1 < numentries ) {
+                    if (dircursor + dirstart + 1 < numentries ) {
                         if(dircursor+1 < tree_max_on_screen) {
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor,
                                          false);
@@ -714,8 +781,8 @@
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
                         } 
                         else {
-                            start++;
-                            numentries = showdir(currdir, start);
+                            dirstart++;
+                            numentries = showdir(currdir, dirstart);
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
                         }
                     }
@@ -723,12 +790,12 @@
                         if(numentries < tree_max_on_screen) {
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor,
                                          false);
-                            start = dircursor = 0;
+                            dirstart = dircursor = 0;
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
                         } 
                         else {
-                            start = dircursor = 0;
-                            numentries = showdir(currdir, start);
+                            dirstart = dircursor = 0;
+                            numentries = showdir(currdir, dirstart);
                             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
                         }
                     }
@@ -811,7 +878,7 @@
                 reload_root = false;
             }
             dircursor = 0;
-            start = 0;
+            dirstart = 0;
             lastdir[0] = 0;
             lastfilter = global_settings.mp3filter;
             lastsortcase = global_settings.sort_case;
@@ -824,15 +891,15 @@
             /* We need to adjust if the number of lines on screen have
                changed because of a status bar change */
             if(CURSOR_Y+LINE_Y+dircursor>tree_max_on_screen) {
-                start++;
+                dirstart++;
                 dircursor--;
             }
-            numentries = showdir(currdir, start);
+            numentries = showdir(currdir, dirstart);
             put_cursorxy(CURSOR_X, CURSOR_Y + dircursor, true);
         }
 
         if ( numentries ) {
-            i = start+dircursor;
+            i = dirstart+dircursor;
 
             /* if MP3 filter is on, cut off the extension */
             if(lasti!=i || restore) {
diff --git a/apps/tree.h b/apps/tree.h
index c438ceb..25e6539 100644
--- a/apps/tree.h
+++ b/apps/tree.h
@@ -22,6 +22,7 @@
 #include <stdbool.h>
 
 void browse_root(void);
+void set_current_file(char *path);
 bool dirbrowse(char *root);
 
 #endif
diff --git a/apps/wps.c b/apps/wps.c
index 82fccae..5b15da4 100644
--- a/apps/wps.c
+++ b/apps/wps.c
@@ -796,6 +796,10 @@
                 lcd_icon(ICON_RECORD, false);
                 lcd_icon(ICON_AUDIO, false);
 #endif
+                /* set dir browser to current playing song */
+                if (global_settings.browse_current && id3)
+                    set_current_file(id3->path);
+
                 button_set_release(old_release_mask);
                 return 0;
                 
@@ -922,6 +926,10 @@
                 lcd_icon(ICON_RECORD, false);
                 lcd_icon(ICON_AUDIO, false);
 #endif
+                /* set dir browser to current playing song */
+                if (global_settings.browse_current && id3)
+                    set_current_file(id3->path);
+
                 mpeg_stop();
                 status_set_playmode(STATUS_STOP);
                 button_set_release(old_release_mask);