--- onplay.c.old 2005-07-08 21:25:06.000000000 +0100 +++ onplay.c 2005-08-09 22:48:05.888579200 +0100 @@ -242,6 +242,365 @@ return result; } +#define PLAYLIST_CATALOG_FILE ROCKBOX_DIR "/.playlist_catalog" + +static bool add_playlist(void) +{ + int f = open(PLAYLIST_CATALOG_FILE,O_WRONLY|O_CREAT|O_APPEND); + fdprintf(f, "%s\n", selected_file); + close(f); + return true; +} + +static bool remove_playlist_entry(char* buffer, int fs, int start, int end) +{ + memcpy(&buffer[fs], &buffer[end], fs-end); + memcpy(&buffer[start], &buffer[fs], fs-end); + + remove(PLAYLIST_CATALOG_FILE); + int f = open(PLAYLIST_CATALOG_FILE,O_WRONLY|O_CREAT); + if(f == -1) return false; + write(f, buffer, fs-end+start); + close(f); + return true; +} + +bool playlist_list_menu(char* buffer, int fs, int* pstart, int* pend) +{ + struct menu_item items[100]; + char names[100][32]; + int end[100]; + + int i, j, p = 0, n = 0, m, result; + char buf[MAX_PATH]; + for(i = 0; i < fs; ++i) { + if(buffer[i] != '\n') buf[p++] = buffer[i]; + else { + buf[p] = 0; + j = p; + while(buf[j-1] != '/' && j > 0) { + j--; + } + strcpy(names[n], &buf[j]); + items[n].desc = names[n]; + p = 0; + end[n] = i+1; + n ++; + if(n+1 == 100) break; + } + } + + m = menu_init( items, n, NULL, NULL, NULL, NULL ); + result = menu_show(m); + menu_exit(m); + + if(result > 0) { + *pstart = end[result-1]; + *pend = end[result]; + } else if(result == 0) { + *pstart = 0; + *pend = end[0]; + } + + return result; +} + +static bool remove_playlist(void) +{ + int f = open(PLAYLIST_CATALOG_FILE,O_RDONLY); + if(f < 0) { + splash(HZ*2, true, "No catalog found"); + return false; + } + + char *buffer; + int buffer_size; + buffer = plugin_get_buffer(&buffer_size); + if (!buffer) + return false; + + int fs = filesize(f); + + read(f, buffer, fs); + + close(f); + + int start, end; + int result = playlist_list_menu(buffer, fs, &start, &end); + if(result < 0) return false; + remove_playlist_entry(buffer, fs, start, end); + + return true; +} + +static bool use_playlist(void) +{ + int f = open(PLAYLIST_CATALOG_FILE,O_RDONLY); + if(f < 0) { + splash(HZ*2, true, "No catalog found"); + return false; + } + + char *buffer; + int buffer_size; + buffer = plugin_get_buffer(&buffer_size); + if (!buffer) + return false; + + int fs = filesize(f); + + read(f, buffer, fs); + + close(f); + + int start, end; + int result = playlist_list_menu(buffer, fs, &start, &end); + + if(result < 0) return false; + + char playfile[MAX_PATH]; + strncpy(playfile, buffer+start, end-start); + playfile[end-start-1 ] = 0; + + f = open(playfile,0); + if(f < 0) { + close(f); + remove_playlist_entry(buffer, fs, start, end); + splash(HZ*2, true, "Playlist no longer exists!"); + return false; + } + close(f); + + if(result >= 1) { + memcpy(&buffer[fs], &buffer[0], end); + memcpy(&buffer[0], &buffer[start ], end-start); + memcpy(&buffer[end-start ], &buffer[fs], start); + + remove(PLAYLIST_CATALOG_FILE); + f = open(PLAYLIST_CATALOG_FILE,O_WRONLY|O_CREAT); + if(f < 0) { + splash(HZ*2, true, "No catalog found"); + return false; + } + write(f, buffer, fs); + close(f); + } + + char dir[MAX_PATH], file[MAX_PATH]; + char* ptr = strrchr(playfile, '/') + 1; + int pathlen = (ptr - playfile); + strcpy(file, playfile+pathlen); + strncpy(dir, playfile, pathlen); + dir[pathlen] = 0; + + bool is_playing = audio_status() & AUDIO_STATUS_PLAY; + + char *index_buffer = NULL; + int index_buffer_size = 0; + + if (is_playing) + { + /* Something is playing, use half the plugin buffer for playlist + indices */ + index_buffer_size = buffer_size / 2; + index_buffer = buffer; + } + + if (playlist_create(dir, file) != -1) + { + int seed = current_tick; + if (global_settings.playlist_shuffle) + playlist_shuffle(seed, -1); + playlist_start(0,0); + onplay_result = ONPLAY_START_PLAY; + } + + return true; +} + +static bool add_to_a_playlist(int position) +{ + int f = open(PLAYLIST_CATALOG_FILE,O_RDONLY); + if(f < 0) { + splash(HZ*2, true, "No catalog found"); + return false; + } + + char *buffer; + int buffer_size; + buffer = plugin_get_buffer(&buffer_size); + if (!buffer) + return false; + + int fs = filesize(f); + + read(f, buffer, fs); + + close(f); + + int start, end; + int result = playlist_list_menu(buffer, fs, &start, &end); + if(result < 0) return false; + + char playfile[MAX_PATH]; + strncpy(playfile, buffer+start, end-start); + playfile[end-start-1 ] = 0; + + f = open(playfile,0); + if(f < 0) { + close(f); + remove_playlist_entry(buffer, fs, start, end); + splash(HZ*2, true, "Playlist no longer exists!"); + return false; + } + close(f); + + if(result >= 1) { + memcpy(&buffer[fs], &buffer[0], end); + memcpy(&buffer[0], &buffer[start ], end-start); + memcpy(&buffer[end-start ], &buffer[fs], start); + + remove(PLAYLIST_CATALOG_FILE); + f = open(PLAYLIST_CATALOG_FILE,O_WRONLY|O_CREAT); + if(f < 0) { + splash(HZ*2, true, "No catalog found"); + return false; + } + write(f, buffer, fs); + close(f); + } + + char dir[MAX_PATH], file[MAX_PATH]; + char* ptr = strrchr(playfile, '/') + 1; + int pathlen = (ptr - playfile); + strcpy(file, playfile+pathlen); + strncpy(dir, playfile, pathlen); + dir[pathlen] = 0; + + struct playlist_info playlist; + + bool is_playing = audio_status() & AUDIO_STATUS_PLAY; + + char *index_buffer = NULL; + int index_buffer_size = 0; + + if (is_playing) + { + /* Something is playing, use half the plugin buffer for playlist + indices */ + index_buffer_size = buffer_size / 2; + index_buffer = buffer; + } + + + playlist_create_ex(&playlist, dir, file, index_buffer, + index_buffer_size, buffer+index_buffer_size, + buffer_size-index_buffer_size); + + if ((selected_file_attr & TREE_ATTR_MASK) == TREE_ATTR_MPA) + playlist_insert_track(&playlist, selected_file, position, false); + else if (selected_file_attr & ATTR_DIRECTORY) + { + bool recurse = false; + + if (global_settings.recursive_dir_insert != RECURSE_ASK) + recurse = (bool)global_settings.recursive_dir_insert; + else + { + /* Ask if user wants to recurse directory */ + bool exit = false; + + lcd_clear_display(); + lcd_puts_scroll(0,0,str(LANG_RECURSE_DIRECTORY_QUESTION)); + lcd_puts_scroll(0,1,selected_file); + +#ifdef HAVE_LCD_BITMAP + lcd_puts(0,3,str(LANG_CONFIRM_WITH_PLAY_RECORDER)); + lcd_puts(0,4,str(LANG_CANCEL_WITH_ANY_RECORDER)); +#endif + + lcd_update(); + + while (!exit) { + int btn = button_get(true); + switch (btn) { + case SETTINGS_OK: + recurse = true; + exit = true; + break; + + default: + /* ignore button releases */ + if (!(btn & BUTTON_REL)) + exit = true; + break; + } + } + } + + playlist_insert_directory(&playlist, selected_file, position, false, + recurse); + } + else if ((selected_file_attr & TREE_ATTR_MASK) == TREE_ATTR_M3U) + playlist_insert_playlist(&playlist, selected_file, position, false); + + playlist_save(&playlist, playfile); + + playlist_close(&playlist); + + return true; +} + +static bool add_playlist_options(void) +{ + struct menu_item items[12]; + int arg[12]; + int m, i=0, pstart=0, result; + bool ret = false; + + if ((selected_file_attr & TREE_ATTR_MASK) == TREE_ATTR_M3U && + context == CONTEXT_TREE) + { + items[i].desc = "Add Playlist"; + items[i].function = add_playlist; + i++; + pstart++; + } + + if ((audio_status() & AUDIO_STATUS_PLAY && + context == CONTEXT_WPS) || context == CONTEXT_TREE) + { + items[i].desc = "Add Last To Playlist"; + arg[i] = PLAYLIST_INSERT_LAST; + i++; + + items[i].desc = "Add First To Playlist"; + arg[i] = PLAYLIST_PREPEND; + i++; + + items[i].desc = "Add Shuffled To Playlist"; + arg[i] = PLAYLIST_INSERT_SHUFFLED; + i++; + } + + items[i].desc = "Remove Playlist"; + items[i].function = remove_playlist; + i++; + items[i].desc = "Use Playlist"; + items[i].function = use_playlist; + i++; + + m = menu_init( items, i, NULL, NULL, NULL, NULL ); + result = menu_show(m); + if (result >= pstart && result < pstart+3) + ret = add_to_a_playlist(arg[result]); + else if(result >= 0) + ret = items[result].function(); + menu_exit(m); + + return ret; +} + /* Sub-menu for playlist options */ static bool playlist_options(void) { @@ -518,7 +877,7 @@ int onplay(char* file, int attr, int from) { - struct menu_item items[8]; /* increase this if you add entries! */ + struct menu_item items[9]; /* increase this if you add entries! */ int m, i=0, result; onplay_result = ONPLAY_OK; @@ -541,6 +900,9 @@ items[i].desc = ID2P(LANG_PLAYLIST); items[i].function = playlist_options; i++; + items[i].desc = "Playlist catalog"; + items[i].function = add_playlist_options; + i++; } if (context == CONTEXT_WPS)