Commit 9886670b authored by Rafaël Carré's avatar Rafaël Carré

ncurses: Rewrite playlist code to make it more readable

Split some functions
parent 3cc4799c
...@@ -309,68 +309,75 @@ static void PlaylistDestroy(intf_sys_t *p_sys) ...@@ -309,68 +309,75 @@ static void PlaylistDestroy(intf_sys_t *p_sys)
p_sys->pp_plist = NULL; p_sys->pp_plist = NULL;
} }
static void PlaylistAddNode(intf_thread_t *p_intf, playlist_item_t *p_node, static inline playlist_item_t *PlaylistGetRoot(intf_thread_t *p_intf)
int i, const char *c)
{ {
intf_sys_t *p_sys = p_intf->p_sys; playlist_t *p_playlist = pl_Get(p_intf);
playlist_item_t *p_child; return p_intf->p_sys->i_current_view == VIEW_CATEGORY ?
int k; p_playlist->p_root_category :
p_playlist->p_root_onelevel;
}
for(k = 0; k < p_node->i_children; k++) static bool PlaylistAddChild(intf_sys_t *p_sys, playlist_item_t *p_child,
{ const char *c, const char d)
char *psz_display; {
p_child = p_node->pp_children[k]; int ret;
char *psz_name = input_item_GetTitleFbName(p_child->p_input); char *psz_name = input_item_GetTitleFbName(p_child->p_input);
struct pl_item_t *p_pl_item = malloc(sizeof *p_pl_item);
if (c && *c) if (!psz_name || !p_pl_item)
{ goto error;
if (asprintf(&psz_display, "%s%c-%s", c,
k == p_node->i_children - 1 ? '`' : '|', psz_name) == -1)
return;
}
else if (asprintf(&psz_display, " %s", psz_name) == -1)
return;
free(psz_name); p_pl_item->p_item = p_child;
struct pl_item_t *p_pl_item = malloc(sizeof(struct pl_item_t));
if (!p_pl_item) if (c && *c)
ret = asprintf(&p_pl_item->psz_display, "%s%c-%s", c, d, psz_name);
else
ret = asprintf(&p_pl_item->psz_display, " %s", psz_name);
free(psz_name);
psz_name = NULL;
if (ret == -1)
goto error;
INSERT_ELEM(p_sys->pp_plist, p_sys->i_plist_entries,
p_sys->i_plist_entries, p_pl_item);
return true;
error:
free(psz_name);
free(p_pl_item);
return false;
}
static void PlaylistAddNode(intf_sys_t *p_sys, playlist_item_t *p_node,
const char *c)
{
for(int k = 0; k < p_node->i_children; k++)
{
playlist_item_t *p_child = p_node->pp_children[k];
char d = k == p_node->i_children - 1 ? '`' : '|';
if(!PlaylistAddChild(p_sys, p_child, c, d))
return; return;
p_pl_item->psz_display = psz_display;
p_pl_item->p_item = p_child;
INSERT_ELEM(p_sys->pp_plist, p_sys->i_plist_entries,
p_sys->i_plist_entries, p_pl_item);
i++;
if (p_child->i_children > 0) if (p_child->i_children <= 0)
continue;
if (*c)
{ {
char *psz_tmp; char *psz_tmp;
if (asprintf(&psz_tmp, "%s%c ", c, if (asprintf(&psz_tmp, "%s%c ", c,
k == p_node->i_children - 1 ? ' ' : '|') == -1) k == p_node->i_children - 1 ? ' ' : '|') == -1)
return; return;
PlaylistAddNode(p_intf, p_child, i, PlaylistAddNode(p_sys, p_child, psz_tmp);
strlen(c) ? psz_tmp : " ");
free(psz_tmp); free(psz_tmp);
} }
else
PlaylistAddNode(p_sys, p_child, " ");
} }
} }
static playlist_item_t *PlaylistGetRoot(intf_thread_t *p_intf)
{
intf_sys_t *p_sys = p_intf->p_sys;
playlist_t *p_playlist = pl_Get(p_intf);
playlist_item_t *p_item;
switch(p_sys->i_current_view)
{
case VIEW_CATEGORY:
p_item = p_playlist->p_root_category;
break;
default:
p_item = p_playlist->p_root_onelevel;
}
return p_item;
}
static void PlaylistRebuild(intf_thread_t *p_intf) static void PlaylistRebuild(intf_thread_t *p_intf)
{ {
intf_sys_t *p_sys = p_intf->p_sys; intf_sys_t *p_sys = p_intf->p_sys;
...@@ -378,12 +385,8 @@ static void PlaylistRebuild(intf_thread_t *p_intf) ...@@ -378,12 +385,8 @@ static void PlaylistRebuild(intf_thread_t *p_intf)
PL_LOCK; PL_LOCK;
/* First clear the old one */
PlaylistDestroy(p_sys); PlaylistDestroy(p_sys);
PlaylistAddNode(p_sys, PlaylistGetRoot(p_intf), "");
/* Build the new one */
PlaylistAddNode(p_intf, PlaylistGetRoot(p_intf), 0, "");
p_sys->b_need_update = false; p_sys->b_need_update = false;
PL_UNLOCK; PL_UNLOCK;
...@@ -394,96 +397,90 @@ static int PlaylistChanged(vlc_object_t *p_this, const char *psz_variable, ...@@ -394,96 +397,90 @@ static int PlaylistChanged(vlc_object_t *p_this, const char *psz_variable,
{ {
VLC_UNUSED(p_this); VLC_UNUSED(psz_variable); VLC_UNUSED(p_this); VLC_UNUSED(psz_variable);
VLC_UNUSED(oval); VLC_UNUSED(nval); VLC_UNUSED(oval); VLC_UNUSED(nval);
intf_thread_t *p_intf = (intf_thread_t *)param;
playlist_t *p_playlist = pl_Get(p_intf); intf_thread_t *p_intf = (intf_thread_t *)param;
p_intf->p_sys->b_need_update = true; intf_sys_t *p_sys = p_intf->p_sys;
p_intf->p_sys->p_node = playlist_CurrentPlayingItem(p_playlist) ? playlist_CurrentPlayingItem(p_playlist)->p_parent : NULL; playlist_item_t *p_node = playlist_CurrentPlayingItem(pl_Get(p_intf));
p_sys->b_need_update = true;
p_sys->p_node = p_node ? p_node->p_parent : NULL;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
/* Playlist suxx */ /* Playlist suxx */
/* This function have to be called with the playlist locked */ /* This function have to be called with the playlist locked */
static inline bool PlaylistIsPlaying(playlist_t *p_playlist, static inline bool PlaylistIsPlaying(playlist_t *p_playlist,
playlist_item_t *p_item) playlist_item_t *p_item)
{ {
playlist_item_t *p_played_item = playlist_CurrentPlayingItem(p_playlist); playlist_item_t *p_played_item = playlist_CurrentPlayingItem(p_playlist);
return(p_item && p_played_item && p_item->p_input && p_played_item->p_input return p_item && p_played_item
&& p_item->p_input->i_id == p_played_item->p_input->i_id); && p_item->p_input && p_played_item->p_input
&& p_item->p_input->i_id == p_played_item->p_input->i_id;
} }
static int SubSearchPlaylist(intf_thread_t *p_intf, char *psz_searchstring, static int SubSearchPlaylist(intf_sys_t *p_sys, char *psz_searchstring,
int i_start, int i_stop) int i_start, int i_stop)
{ {
intf_sys_t *p_sys = p_intf->p_sys; for(int i = i_start + 1; i < i_stop; i++)
int i, i_item = -1;
for(i = i_start + 1; i < i_stop; i++)
if (strcasestr(p_sys->pp_plist[i]->psz_display, psz_searchstring)) if (strcasestr(p_sys->pp_plist[i]->psz_display, psz_searchstring))
{ return i;
i_item = i;
break;
}
return i_item; return -1;
} }
static void SearchPlaylist(intf_thread_t *p_intf, char *psz_searchstring) static void SearchPlaylist(intf_sys_t *p_sys, char *psz_searchstring)
{ {
int i_max; int i_item, i_first = p_sys->i_before_search;
int i_first = 0 ;
int i_item = -1;
intf_sys_t *p_sys = p_intf->p_sys;
if (p_sys->i_before_search >= 0) if (i_first < 0)
i_first = p_sys->i_before_search; i_first = 0;
if ((! psz_searchstring) || strlen(psz_searchstring) <= 0) if (!psz_searchstring || !*psz_searchstring)
{ {
p_sys->i_box_plidx = p_sys->i_before_search; p_sys->i_box_plidx = p_sys->i_before_search;
return; return;
} }
i_max = p_sys->i_plist_entries; i_item = SubSearchPlaylist(p_sys, psz_searchstring, i_first + 1,
p_sys->i_plist_entries);
i_item = SubSearchPlaylist(p_intf, psz_searchstring, i_first + 1, i_max);
if (i_item < 0) if (i_item < 0)
i_item = SubSearchPlaylist(p_intf, psz_searchstring, 0, i_first); i_item = SubSearchPlaylist(p_sys, psz_searchstring, 0, i_first);
if (i_item < 0 || i_item >= i_max) return; if (i_item > 0)
p_sys->i_box_plidx = i_item;
}
p_sys->i_box_plidx = i_item; static inline bool IsIndex(intf_sys_t *p_sys, playlist_t *p_playlist, int i)
{
playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
return (p_item->i_children == 0 && p_item == p_sys->p_node) ||
PlaylistIsPlaying(p_playlist, p_item);
} }
static void FindIndex(intf_thread_t *p_intf, playlist_t *p_playlist) static void FindIndex(intf_sys_t *p_sys, playlist_t *p_playlist)
{ {
intf_sys_t *p_sys = p_intf->p_sys; int plidx = p_sys->i_box_plidx;
int i; PL_LOCK;
if (p_sys->i_box_plidx < p_sys->i_plist_entries && p_sys->i_box_plidx >= 0) if (plidx < 0 || plidx >= p_sys->i_plist_entries ||
!IsIndex(p_sys, p_playlist, plidx))
{ {
playlist_item_t *p_item = p_sys->pp_plist[p_sys->i_box_plidx]->p_item; for(int i = 0; i < p_sys->i_plist_entries; i++)
PL_LOCK; if (IsIndex(p_sys, p_playlist, i))
if ((p_item->i_children == 0 && p_item == p_sys->p_node) || {
PlaylistIsPlaying(p_playlist, p_item)) p_sys->i_box_plidx = i;
{ break;
PL_UNLOCK; }
return;
}
} }
for(i = 0; i < p_sys->i_plist_entries; i++)
{
playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
if ((p_item->i_children == 0 && p_item == p_sys->p_node) ||
PlaylistIsPlaying(p_playlist, p_item))
{
p_sys->i_box_plidx = i;
break;
}
}
PL_UNLOCK; PL_UNLOCK;
} }
/****************************************************************************
* Drawing
****************************************************************************/
static void start_color_and_pairs(intf_thread_t *p_intf) static void start_color_and_pairs(intf_thread_t *p_intf)
{ {
assert(p_intf->p_sys->b_color && !p_intf->p_sys->b_color_started); assert(p_intf->p_sys->b_color && !p_intf->p_sys->b_color_started);
...@@ -536,10 +533,6 @@ static void start_color_and_pairs(intf_thread_t *p_intf) ...@@ -536,10 +533,6 @@ static void start_color_and_pairs(intf_thread_t *p_intf)
p_intf->p_sys->b_color_started = true; p_intf->p_sys->b_color_started = true;
} }
/****************************************************************************
* Drawing
****************************************************************************/
static void DrawBox(WINDOW *win, int y, int x, int h, int w, const char *title, bool b_color) static void DrawBox(WINDOW *win, int y, int x, int h, int w, const char *title, bool b_color)
{ {
int i; int i;
...@@ -1359,7 +1352,7 @@ static void Redraw(intf_thread_t *p_intf, time_t *t_last_refresh) ...@@ -1359,7 +1352,7 @@ static void Redraw(intf_thread_t *p_intf, time_t *t_last_refresh)
if (p_sys->b_need_update || !p_sys->pp_plist) if (p_sys->b_need_update || !p_sys->pp_plist)
PlaylistRebuild(p_intf); PlaylistRebuild(p_intf);
if (p_sys->b_box_plidx_follow) if (p_sys->b_box_plidx_follow)
FindIndex(p_intf, p_playlist); FindIndex(p_sys, p_playlist);
if (p_sys->i_box_plidx < 0) p_sys->i_box_plidx = 0; if (p_sys->i_box_plidx < 0) p_sys->i_box_plidx = 0;
if (p_sys->i_box_plidx < 0) p_sys->i_box_plidx = 0; if (p_sys->i_box_plidx < 0) p_sys->i_box_plidx = 0;
...@@ -1692,7 +1685,7 @@ static int HandleKey(intf_thread_t *p_intf, int i_key) ...@@ -1692,7 +1685,7 @@ static int HandleKey(intf_thread_t *p_intf, int i_key)
/* Playlist navigation */ /* Playlist navigation */
case 'g': case 'g':
FindIndex(p_intf, p_playlist); FindIndex(p_sys, p_playlist);
break; break;
case KEY_HOME: case KEY_HOME:
p_sys->i_box_plidx = 0; p_sys->i_box_plidx = 0;
...@@ -1962,7 +1955,7 @@ static int HandleKey(intf_thread_t *p_intf, int i_key) ...@@ -1962,7 +1955,7 @@ static int HandleKey(intf_thread_t *p_intf, int i_key)
if (i_chain_len > 0) if (i_chain_len > 0)
p_sys->psz_old_search = strdup(p_sys->psz_search_chain); p_sys->psz_old_search = strdup(p_sys->psz_search_chain);
else if (p_sys->psz_old_search) else if (p_sys->psz_old_search)
SearchPlaylist(p_intf, p_sys->psz_old_search); SearchPlaylist(p_sys, p_sys->psz_old_search);
p_sys->i_box_type = BOX_PLAYLIST; p_sys->i_box_type = BOX_PLAYLIST;
goto end; goto end;
case 0x1b: /* ESC */ case 0x1b: /* ESC */
...@@ -2015,7 +2008,7 @@ static int HandleKey(intf_thread_t *p_intf, int i_key) ...@@ -2015,7 +2008,7 @@ static int HandleKey(intf_thread_t *p_intf, int i_key)
} }
free(p_sys->psz_old_search); free(p_sys->psz_old_search);
p_sys->psz_old_search = NULL; p_sys->psz_old_search = NULL;
SearchPlaylist(p_intf, p_sys->psz_search_chain); SearchPlaylist(p_sys, p_sys->psz_search_chain);
goto end; goto end;
} }
else if (p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain) else if (p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain)
...@@ -2323,7 +2316,7 @@ static void Run(intf_thread_t *p_intf) ...@@ -2323,7 +2316,7 @@ static void Run(intf_thread_t *p_intf)
if (p_sys->b_box_plidx_follow && playlist_CurrentPlayingItem(p_playlist)) if (p_sys->b_box_plidx_follow && playlist_CurrentPlayingItem(p_playlist))
{ {
PL_UNLOCK; PL_UNLOCK;
FindIndex(p_intf, p_playlist); FindIndex(p_sys, p_playlist);
} }
else else
PL_UNLOCK; PL_UNLOCK;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment