From d5180f7870643e19c37b62909d0e0c545cc23337 Mon Sep 17 00:00:00 2001 From: Frank Gevaerts Date: Wed, 1 Jul 2009 16:59:43 +0000 Subject: Add "Play Shuffled" menu item to random_folder_advance_config, which adds all configured configured directories to the current playlist in random order. (FS#10403) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21594 a1c6a512-1295-4272-9138-f99709370657 --- apps/plugin.c | 1 + apps/plugin.h | 5 +- apps/plugins/random_folder_advance_config.c | 70 +++++++++++++++++++++++++ manual/plugins/random_folder_advance_config.tex | 1 + 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/apps/plugin.c b/apps/plugin.c index 20c7a3733c..ff80776f7e 100644 --- a/apps/plugin.c +++ b/apps/plugin.c @@ -658,6 +658,7 @@ static const struct plugin_api rockbox_api = { appsversion, /* new stuff at the end, sort into place next time the API gets incompatible */ + playlist_insert_directory, }; int plugin_load(const char* plugin, const void* parameter) diff --git a/apps/plugin.h b/apps/plugin.h index 160eb2b1e7..db16800309 100644 --- a/apps/plugin.h +++ b/apps/plugin.h @@ -128,7 +128,7 @@ void* plugin_get_buffer(size_t *buffer_size); #define PLUGIN_MAGIC 0x526F634B /* RocK */ /* increase this every time the api struct changes */ -#define PLUGIN_API_VERSION 157 +#define PLUGIN_API_VERSION 158 /* update this to latest version if a change to the api struct breaks backwards compatibility (and please take the opportunity to sort in any @@ -821,6 +821,9 @@ struct plugin_api { const char *appsversion; /* new stuff at the end, sort into place next time the API gets incompatible */ + int (*playlist_insert_directory)(struct playlist_info* playlist, + const char *dirname, int position, bool queue, + bool recurse); }; /* plugin header */ diff --git a/apps/plugins/random_folder_advance_config.c b/apps/plugins/random_folder_advance_config.c index d0a6a26800..0f540baf8a 100644 --- a/apps/plugins/random_folder_advance_config.c +++ b/apps/plugins/random_folder_advance_config.c @@ -19,6 +19,7 @@ * ****************************************************************************/ #include "plugin.h" +#include "file.h" PLUGIN_HEADER @@ -30,6 +31,7 @@ static int lasttick; #define RFADIR_FILE ROCKBOX_DIR "/folder_advance_dir.txt" #define RFA_FILE_TEXT ROCKBOX_DIR "/folder_advance_list.txt" #define MAX_REMOVED_DIRS 10 +#define MAX_SHUFFLE_SIZE (PLUGIN_BUFFER_SIZE/4 - 10000) char *buffer = NULL; ssize_t buffer_size; @@ -40,6 +42,7 @@ struct file_format { char folder[][MAX_PATH]; }; struct file_format *list = NULL; +static int order[MAX_SHUFFLE_SIZE]; void update_screen(bool clear) { @@ -460,6 +463,68 @@ int import_list_from_file_text(void) return list->count; } +int start_shuffled_play(void) +{ + int i = 0; + /* load the dat file if not already done */ + if ((list == NULL || list->count == 0) && (i = load_list()) != 0) + { + rb->splashf(HZ*2, "Could not load %s, rv = %d", RFA_FILE, i); + return 0; + } + + if (list->count <= 0) + { + rb->splashf(HZ*2, "no dirs in list file: %s", RFA_FILE); + return 0; + } + + /* shuffle the thing */ + rb->srand(*rb->current_tick); + if(list->count>MAX_SHUFFLE_SIZE) + { + rb->splashf(HZ*2, "Too many files: %d", list->count); + return 0; + } + for(i=0;icount;i++) + order[i]=i; + + for(i = list->count - 1; i >= 0; i--) + { + /* the rand is from 0 to RAND_MAX, so adjust to our value range */ + int candidate = rb->rand() % (i + 1); + + /* now swap the values at the 'i' and 'candidate' positions */ + int store = order[candidate]; + order[candidate] = order[i]; + order[i] = store; + } + + /* We don't want whatever is playing */ + if (!(rb->playlist_remove_all_tracks(NULL) == 0 + && rb->playlist_create(NULL, NULL) == 0)) + { + rb->splashf(HZ*2, "Could not clear playlist"); + return 0; + } + + /* add the lot to the playlist */ + for (i = 0; i < list->count; i++) + { + if (list->folder[order[i]][0] != ' ') + { + rb->playlist_insert_directory(NULL,list->folder[order[i]],PLAYLIST_INSERT_LAST,false,false); + } + if (rb->action_userabort(TIMEOUT_NOBLOCK)) + { + break; + } + } + rb->splash(HZ, "Done"); + rb->playlist_start(0,0); + return 1; +} + int main_menu(void) { bool exit = false; @@ -469,6 +534,7 @@ int main_menu(void) "Edit Folder List", "Export List To Textfile", "Import List From Textfile", + "Play Shuffled", "Quit"); switch (rb->do_menu(&menu, NULL, NULL, false)) @@ -527,6 +593,10 @@ int main_menu(void) rb->backlight_on(); break; case 4: + start_shuffled_play(); + exit=true; + break; + case 5: return 1; } return exit?1:0; diff --git a/manual/plugins/random_folder_advance_config.tex b/manual/plugins/random_folder_advance_config.tex index 098407d70b..a4f0a3a53f 100644 --- a/manual/plugins/random_folder_advance_config.tex +++ b/manual/plugins/random_folder_advance_config.tex @@ -21,6 +21,7 @@ it you need to have both \fname{-/CDs} and \fname{CDs} as entries. \fname{/.rockbox/folder\_advance\_list.txt} \item[Import List From Textfile] Imports the list from \fname{/.rockbox/folder\_advance\_list.txt} +\item[Play Shuffled] Starts playback with the selected directories in random order. Tracks within a directory will be played in normal order. The plugin will exit after starting playback. \item[Quit] \end{description} -- cgit v1.2.3