forked from len0rd/rockbox
playlist_catalog remove static playlist_dir in favor of generation at runtime
this needs tested by the heavy playlist users with the addition of initialize_catalog_buf there shouldn't be any stack overflow concerns since we are no longer creating another max_path sized buffer when one is already available this also simplifies the code a bit rather than carrying around the playlist directory just generate it on the fly copies the directory to the supplied buffer add catbroswe_status to keep track of what browse context(s) are currently in use Change-Id: I145ec501f601c84bb52f2241ed28c6aefab6897b
This commit is contained in:
parent
773fa7874d
commit
177a15b2ed
3 changed files with 91 additions and 82 deletions
|
@ -61,8 +61,8 @@ int save_playlist_screen(struct playlist_info* playlist)
|
||||||
|
|
||||||
if (!dot && len <= 1)
|
if (!dot && len <= 1)
|
||||||
{
|
{
|
||||||
snprintf(temp, sizeof(temp), "%s%s",
|
catalog_get_directory(temp, sizeof(temp));
|
||||||
catalog_get_directory(), DEFAULT_DYNAMIC_PLAYLIST_NAME);
|
strlcat(temp, DEFAULT_DYNAMIC_PLAYLIST_NAME, sizeof(temp));
|
||||||
}
|
}
|
||||||
|
|
||||||
dot = strrchr(temp, '.');
|
dot = strrchr(temp, '.');
|
||||||
|
|
|
@ -54,73 +54,73 @@ struct add_track_context {
|
||||||
int count;
|
int count;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum catbrowse_status_flags{
|
||||||
|
CATBROWSE_NOTHING = 0,
|
||||||
|
CATBROWSE_CATVIEW,
|
||||||
|
CATBROWSE_PLAYLIST
|
||||||
|
};
|
||||||
|
|
||||||
/* keep track of most recently used playlist */
|
/* keep track of most recently used playlist */
|
||||||
static char most_recent_playlist[MAX_PATH];
|
static char most_recent_playlist[MAX_PATH];
|
||||||
|
/* we need playlist_dir_length for easy removal of playlist dir prefix */
|
||||||
|
static size_t playlist_dir_length;
|
||||||
|
/* keep track of what browser(s) are current to prevent reentry */
|
||||||
|
static int browser_status = CATBROWSE_NOTHING;
|
||||||
|
|
||||||
/* directory where our playlists our stored */
|
static size_t get_directory(char* dirbuf, size_t dirbuf_sz)
|
||||||
static char playlist_dir[MAX_PATH];
|
|
||||||
static int playlist_dir_length;
|
|
||||||
static bool playlist_dir_exists = false;
|
|
||||||
|
|
||||||
/* Retrieve playlist directory from config file and verify it exists */
|
|
||||||
static bool initialized = false;
|
|
||||||
static int initialize_catalog(void)
|
|
||||||
{
|
{
|
||||||
|
char *pl_dir = PLAYLIST_CATALOG_DEFAULT_DIR;
|
||||||
if (!initialized)
|
|
||||||
{
|
|
||||||
bool default_dir = true;
|
|
||||||
|
|
||||||
/* directory config is of the format: "dir: /path/to/dir" */
|
/* directory config is of the format: "dir: /path/to/dir" */
|
||||||
if (global_settings.playlist_catalog_dir[0] &&
|
if (global_settings.playlist_catalog_dir[0] != '\0')
|
||||||
strcmp(global_settings.playlist_catalog_dir,
|
|
||||||
PLAYLIST_CATALOG_DEFAULT_DIR))
|
|
||||||
{
|
{
|
||||||
strcpy(playlist_dir, global_settings.playlist_catalog_dir);
|
pl_dir = global_settings.playlist_catalog_dir;
|
||||||
default_dir = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fall back to default directory if no or invalid config */
|
|
||||||
if (default_dir)
|
|
||||||
{
|
|
||||||
strcpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR);
|
|
||||||
if (!dir_exists(playlist_dir))
|
|
||||||
mkdir(playlist_dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
playlist_dir_length = strlen(playlist_dir);
|
|
||||||
|
|
||||||
/* remove duplicate leading '/' */
|
/* remove duplicate leading '/' */
|
||||||
if (playlist_dir[0] == '/' && playlist_dir[1] == '/')
|
if (pl_dir[0] == '/' && pl_dir[1] == '/')
|
||||||
{
|
{
|
||||||
memmove(&playlist_dir[0], &playlist_dir[1], playlist_dir_length); /* gets the \0 too */
|
pl_dir++;
|
||||||
playlist_dir_length--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dir_exists(playlist_dir))
|
return strlcpy(dirbuf, pl_dir, dirbuf_sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Retrieve playlist directory from config file and verify it exists
|
||||||
|
* attempts to create directory
|
||||||
|
* catalog dir is returned in dirbuf */
|
||||||
|
static int initialize_catalog_buf(char* dirbuf, size_t dirbuf_sz)
|
||||||
|
{
|
||||||
|
playlist_dir_length = get_directory(dirbuf, dirbuf_sz);
|
||||||
|
if (playlist_dir_length >= dirbuf_sz)
|
||||||
{
|
{
|
||||||
playlist_dir_exists = true;
|
return -2;
|
||||||
memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
|
|
||||||
initialized = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!playlist_dir_exists)
|
if (!dir_exists(dirbuf))
|
||||||
{
|
{
|
||||||
if (mkdir(playlist_dir) < 0) {
|
if (mkdir(dirbuf) < 0) {
|
||||||
splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
|
splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), dirbuf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
playlist_dir_exists = true;
|
|
||||||
memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
|
memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
|
||||||
initialized = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Retrieve playlist directory from config file and verify it exists
|
||||||
|
* attempts to create directory
|
||||||
|
* Don't inline as we want the stack to be freed ASAP */
|
||||||
|
static NO_INLINE int initialize_catalog(void)
|
||||||
|
{
|
||||||
|
char playlist_dir[MAX_PATH];
|
||||||
|
|
||||||
|
return initialize_catalog_buf(playlist_dir, sizeof(playlist_dir));
|
||||||
|
}
|
||||||
|
|
||||||
void catalog_set_directory(const char* directory)
|
void catalog_set_directory(const char* directory)
|
||||||
{
|
{
|
||||||
if (directory == NULL)
|
if (directory == NULL)
|
||||||
|
@ -132,36 +132,44 @@ void catalog_set_directory(const char* directory)
|
||||||
strmemccpy(global_settings.playlist_catalog_dir,
|
strmemccpy(global_settings.playlist_catalog_dir,
|
||||||
directory, sizeof(global_settings.playlist_catalog_dir));
|
directory, sizeof(global_settings.playlist_catalog_dir));
|
||||||
}
|
}
|
||||||
initialized = false;
|
|
||||||
initialize_catalog();
|
initialize_catalog();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* catalog_get_directory(void)
|
void catalog_get_directory(char* dirbuf, size_t dirbuf_sz)
|
||||||
{
|
{
|
||||||
if (initialize_catalog() == -1)
|
if (initialize_catalog_buf(dirbuf, dirbuf_sz) < 0)
|
||||||
return "";
|
{
|
||||||
return playlist_dir;
|
dirbuf[0] = '\0';
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Display all playlists in catalog. Selected "playlist" is returned.
|
/* Display all playlists in catalog. Selected "playlist" is returned.
|
||||||
If "view" mode is set then we're not adding anything into playlist. */
|
* If status is CATBROWSE_CATVIEW then we're not adding anything into playlist */
|
||||||
static int display_playlists(char* playlist, bool view)
|
static int display_playlists(char* playlist, enum catbrowse_status_flags status)
|
||||||
{
|
{
|
||||||
static bool reopen_last_playlist = false;
|
static bool reopen_last_playlist = false;
|
||||||
static int most_recent_selection = 0;
|
static int most_recent_selection = 0;
|
||||||
struct browse_context browse;
|
struct browse_context browse;
|
||||||
char selected_playlist[MAX_PATH];
|
|
||||||
int result = -1;
|
int result = -1;
|
||||||
|
char selected_playlist[MAX_PATH];
|
||||||
|
selected_playlist[0] = '\0';
|
||||||
|
|
||||||
|
browser_status |= status;
|
||||||
|
bool view = (status == CATBROWSE_CATVIEW);
|
||||||
|
|
||||||
browse_context_init(&browse, SHOW_M3U,
|
browse_context_init(&browse, SHOW_M3U,
|
||||||
BROWSE_SELECTONLY|(view? 0: BROWSE_NO_CONTEXT_MENU),
|
BROWSE_SELECTONLY|(view? 0: BROWSE_NO_CONTEXT_MENU),
|
||||||
str(LANG_CATALOG), NOICON,
|
str(LANG_CATALOG), NOICON,
|
||||||
playlist_dir, playlist_dir_length + 1 + most_recent_playlist);
|
selected_playlist,
|
||||||
|
playlist_dir_length + 1 + most_recent_playlist);
|
||||||
|
|
||||||
browse.buf = selected_playlist;
|
browse.buf = selected_playlist;
|
||||||
browse.bufsize = sizeof(selected_playlist);
|
browse.bufsize = sizeof(selected_playlist);
|
||||||
|
|
||||||
restart:
|
restart:
|
||||||
|
/* set / restore the root directory for the browser */
|
||||||
|
catalog_get_directory(selected_playlist, sizeof(selected_playlist));
|
||||||
browse.flags &= ~BROWSE_SELECTED;
|
browse.flags &= ~BROWSE_SELECTED;
|
||||||
|
|
||||||
if (view && reopen_last_playlist)
|
if (view && reopen_last_playlist)
|
||||||
|
@ -169,13 +177,18 @@ restart:
|
||||||
switch (playlist_viewer_ex(most_recent_playlist, &most_recent_selection))
|
switch (playlist_viewer_ex(most_recent_playlist, &most_recent_selection))
|
||||||
{
|
{
|
||||||
case PLAYLIST_VIEWER_OK:
|
case PLAYLIST_VIEWER_OK:
|
||||||
|
{
|
||||||
result = 0;
|
result = 0;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case PLAYLIST_VIEWER_CANCEL:
|
case PLAYLIST_VIEWER_CANCEL:
|
||||||
|
{
|
||||||
reopen_last_playlist = false;
|
reopen_last_playlist = false;
|
||||||
goto restart;
|
goto restart;
|
||||||
|
}
|
||||||
case PLAYLIST_VIEWER_USB:
|
case PLAYLIST_VIEWER_USB:
|
||||||
case PLAYLIST_VIEWER_MAINMENU:
|
case PLAYLIST_VIEWER_MAINMENU:
|
||||||
|
/* Fall through */
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -208,13 +221,18 @@ restart:
|
||||||
{
|
{
|
||||||
switch (playlist_viewer_ex(selected_playlist, &most_recent_selection)) {
|
switch (playlist_viewer_ex(selected_playlist, &most_recent_selection)) {
|
||||||
case PLAYLIST_VIEWER_OK:
|
case PLAYLIST_VIEWER_OK:
|
||||||
|
{
|
||||||
reopen_last_playlist = true;
|
reopen_last_playlist = true;
|
||||||
result = 0;
|
result = 0;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case PLAYLIST_VIEWER_CANCEL:
|
case PLAYLIST_VIEWER_CANCEL:
|
||||||
|
{
|
||||||
goto restart;
|
goto restart;
|
||||||
|
}
|
||||||
case PLAYLIST_VIEWER_USB:
|
case PLAYLIST_VIEWER_USB:
|
||||||
case PLAYLIST_VIEWER_MAINMENU:
|
case PLAYLIST_VIEWER_MAINMENU:
|
||||||
|
/* Fall through */
|
||||||
default:
|
default:
|
||||||
reopen_last_playlist = true;
|
reopen_last_playlist = true;
|
||||||
break;
|
break;
|
||||||
|
@ -227,7 +245,7 @@ restart:
|
||||||
strmemccpy(playlist, selected_playlist, MAX_PATH);
|
strmemccpy(playlist, selected_playlist, MAX_PATH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
browser_status &= ~status;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,32 +374,26 @@ exit:
|
||||||
close(fd);
|
close(fd);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
static bool in_cat_viewer = false;
|
|
||||||
bool catalog_view_playlists(void)
|
bool catalog_view_playlists(void)
|
||||||
{
|
{
|
||||||
bool retval = true;
|
if ((browser_status & CATBROWSE_CATVIEW) == CATBROWSE_CATVIEW)
|
||||||
if (in_cat_viewer)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (initialize_catalog() == -1)
|
if (initialize_catalog() < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
in_cat_viewer = true;
|
return (display_playlists(NULL, CATBROWSE_CATVIEW) >= 0);
|
||||||
retval = (display_playlists(NULL, true) != -1);
|
|
||||||
in_cat_viewer = false;
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool in_add_to_playlist = false;
|
|
||||||
bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
|
bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
|
||||||
bool new_playlist, char *m3u8name)
|
bool new_playlist, char *m3u8name)
|
||||||
{
|
{
|
||||||
int result;
|
|
||||||
char playlist[MAX_PATH + 7]; /* room for /.m3u8\0*/
|
char playlist[MAX_PATH + 7]; /* room for /.m3u8\0*/
|
||||||
if (in_add_to_playlist)
|
if ((browser_status & CATBROWSE_PLAYLIST) == CATBROWSE_PLAYLIST)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (initialize_catalog() == -1)
|
if (initialize_catalog_buf(playlist, sizeof(playlist)) < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (new_playlist)
|
if (new_playlist)
|
||||||
|
@ -391,7 +403,7 @@ bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
|
||||||
{
|
{
|
||||||
const char *name;
|
const char *name;
|
||||||
/* If sel is empty, root, or playlist directory we use 'all' */
|
/* If sel is empty, root, or playlist directory we use 'all' */
|
||||||
if (!sel || !strcmp(sel, "/") || !strcmp(sel, playlist_dir))
|
if (!sel || !strcmp(sel, "/") || !strcmp(sel, playlist))
|
||||||
{
|
{
|
||||||
sel = "/";
|
sel = "/";
|
||||||
name = "/all";
|
name = "/all";
|
||||||
|
@ -399,9 +411,10 @@ bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
|
||||||
else /*If sel is a folder, we prefill the text field with its name*/
|
else /*If sel is a folder, we prefill the text field with its name*/
|
||||||
name = strrchr(sel, '/');
|
name = strrchr(sel, '/');
|
||||||
|
|
||||||
snprintf(playlist, MAX_PATH + 1, "%s/%s",
|
if (name == NULL || ((sel_attr & ATTR_DIRECTORY) != ATTR_DIRECTORY))
|
||||||
playlist_dir,
|
name = "/";
|
||||||
(name!=NULL && (sel_attr & ATTR_DIRECTORY))?name+1:"");
|
|
||||||
|
strlcat(playlist, name, sizeof(playlist));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
strmemccpy(playlist, m3u8name, MAX_PATH);
|
strmemccpy(playlist, m3u8name, MAX_PATH);
|
||||||
|
@ -417,11 +430,7 @@ bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
in_add_to_playlist = true;
|
if (display_playlists(playlist, CATBROWSE_PLAYLIST) < 0)
|
||||||
result = display_playlists(playlist, false);
|
|
||||||
in_add_to_playlist = false;
|
|
||||||
|
|
||||||
if (result == -1)
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
#define _PLAYLIST_CATALOG_H_
|
#define _PLAYLIST_CATALOG_H_
|
||||||
|
|
||||||
/* Gets the configured playlist catalog dir */
|
/* Gets the configured playlist catalog dir */
|
||||||
const char* catalog_get_directory(void);
|
void catalog_get_directory(char* dirbuf, size_t dirbuf_sz);
|
||||||
|
|
||||||
/* Set the playlist catalog dir */
|
/* Set the playlist catalog dir */
|
||||||
void catalog_set_directory(const char* directory);
|
void catalog_set_directory(const char* directory);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue