command: add properties for playlist position

playlist-pos can set/get the current playlist index. playlist-count
returns the number of entries in the playlist.
This commit is contained in:
wm4 2013-06-28 22:16:29 +02:00
parent fd7dd83e28
commit a6a1f4b833
6 changed files with 105 additions and 10 deletions

View File

@ -396,9 +396,11 @@ tv-brightness x
tv-contrast x
tv-saturation x
tv-hue x
playlist-pos current position on playlist
playlist-count number of total playlist entries
playlist playlist, current entry marked
track-list list of audio/video/sub tracks, cur. entr. marked
chapter-list list of chapters, current entry marked
playlist playlist, current entry marked
quvi-format x see ``--quvi-format``
=========================== = ==================================================

View File

@ -1494,6 +1494,53 @@ static int mp_property_tv_color(m_option_t *prop, int action, void *arg,
#endif
static int mp_property_playlist_pos(m_option_t *prop, int action, void *arg,
MPContext *mpctx)
{
struct playlist *pl = mpctx->playlist;
if (!pl->first)
return M_PROPERTY_UNAVAILABLE;
switch (action) {
case M_PROPERTY_GET: {
int pos = playlist_entry_to_index(pl, pl->current);
if (pos < 0)
return M_PROPERTY_UNAVAILABLE;
*(int *)arg = pos;
return M_PROPERTY_OK;
}
case M_PROPERTY_SET: {
struct playlist_entry *e = playlist_entry_from_index(pl, *(int *)arg);
if (!e)
return M_PROPERTY_ERROR;
mp_set_playlist_entry(mpctx, e);
return M_PROPERTY_OK;
}
case M_PROPERTY_GET_TYPE: {
struct m_option opt = {
.name = prop->name,
.type = CONF_TYPE_INT,
.flags = CONF_RANGE,
.min = 0,
.max = playlist_entry_count(pl) - 1,
};
*(struct m_option *)arg = opt;
return M_PROPERTY_OK;
}
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
static int mp_property_playlist_count(m_option_t *prop, int action, void *arg,
MPContext *mpctx)
{
if (action == M_PROPERTY_GET) {
*(int *)arg = playlist_entry_count(mpctx->playlist);
return M_PROPERTY_OK;
}
return M_PROPERTY_NOT_IMPLEMENTED;
}
static int mp_property_playlist(m_option_t *prop, int action, void *arg,
MPContext *mpctx)
{
@ -1602,7 +1649,10 @@ static const m_option_t mp_properties[] = {
{ "chapter-list", mp_property_list_chapters, CONF_TYPE_STRING },
{ "track-list", property_list_tracks, CONF_TYPE_STRING },
{ "playlist", mp_property_playlist, CONF_TYPE_STRING },
{ "playlist-pos", mp_property_playlist_pos, CONF_TYPE_INT },
{ "playlist-count", mp_property_playlist_count, CONF_TYPE_INT },
// Audio
{ "volume", mp_property_volume, CONF_TYPE_FLOAT,
@ -2108,11 +2158,8 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
playlist_add(mpctx->playlist, playlist_entry_new(filename));
if (!append) {
mpctx->playlist->current = mpctx->playlist->first;
mpctx->playlist->current_was_replaced = false;
mpctx->stop_play = PT_CURRENT_ENTRY;
}
if (!append)
mp_set_playlist_entry(mpctx, mpctx->playlist->first);
break;
}
@ -2126,10 +2173,8 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
playlist_transfer_entries(mpctx->playlist, pl);
talloc_free(pl);
if (!append) {
mpctx->playlist->current = mpctx->playlist->first;
mpctx->stop_play = PT_CURRENT_ENTRY;
}
if (!append && mpctx->playlist->first)
mp_set_playlist_entry(mpctx, mpctx->playlist->first);
} else {
mp_tmsg(MSGT_CPLAYER, MSGL_ERR,
"\nUnable to load playlist %s.\n", filename);

View File

@ -317,6 +317,7 @@ bool mp_remove_track(struct MPContext *mpctx, struct track *track);
struct playlist_entry *mp_next_file(struct MPContext *mpctx, int direction);
int mp_get_cache_percent(struct MPContext *mpctx);
void mp_write_watch_later_conf(struct MPContext *mpctx);
void mp_set_playlist_entry(struct MPContext *mpctx, struct playlist_entry *e);
void mp_print_version(int always);

View File

@ -4521,6 +4521,16 @@ static void play_files(struct MPContext *mpctx)
}
}
// Abort current playback and set the given entry to play next.
// e must be on the mpctx->playlist.
void mp_set_playlist_entry(struct MPContext *mpctx, struct playlist_entry *e)
{
assert(playlist_entry_to_index(mpctx->playlist, e) >= 0);
mpctx->playlist->current = e;
mpctx->playlist->current_was_replaced = false;
mpctx->stop_play = PT_CURRENT_ENTRY;
}
void mp_print_version(int always)
{
mp_msg(MSGT_CPLAYER, always ? MSGL_INFO : MSGL_V,

View File

@ -196,3 +196,36 @@ void playlist_transfer_entries(struct playlist *pl, struct playlist *source_pl)
playlist_insert(pl, add_after, e);
}
}
// Return number of entries between list start and e.
// Return -1 if e is not on the list, or if e is NULL.
int playlist_entry_to_index(struct playlist *pl, struct playlist_entry *e)
{
struct playlist_entry *cur = pl->first;
int pos = 0;
if (!e)
return -1;
while (cur && cur != e) {
cur = cur->next;
pos++;
}
return cur == e ? pos : -1;
}
int playlist_entry_count(struct playlist *pl)
{
return playlist_entry_to_index(pl, pl->last) + 1;
}
// Return entry for which playlist_entry_to_index() would return index.
// Return NULL if not found.
struct playlist_entry *playlist_entry_from_index(struct playlist *pl, int index)
{
struct playlist_entry *e = pl->first;
for (int n = 0; ; n++) {
if (!e || n == index)
return e;
e = e->next;
}
}

View File

@ -64,4 +64,8 @@ struct playlist_entry *playlist_get_next(struct playlist *pl, int direction);
void playlist_add_base_path(struct playlist *pl, bstr base_path);
void playlist_transfer_entries(struct playlist *pl, struct playlist *source_pl);
int playlist_entry_to_index(struct playlist *pl, struct playlist_entry *e);
int playlist_entry_count(struct playlist *pl);
struct playlist_entry *playlist_entry_from_index(struct playlist *pl, int index);
#endif