From a6a1f4b8336c0d18c0588922da6b786b6408209f Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 28 Jun 2013 22:16:29 +0200 Subject: 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. --- DOCS/man/en/input.rst | 4 +++- core/command.c | 63 +++++++++++++++++++++++++++++++++++++++++++-------- core/mp_core.h | 1 + core/mplayer.c | 10 ++++++++ core/playlist.c | 33 +++++++++++++++++++++++++++ core/playlist.h | 4 ++++ 6 files changed, 105 insertions(+), 10 deletions(-) diff --git a/DOCS/man/en/input.rst b/DOCS/man/en/input.rst index bc20a5c42e..cb5e57e7a2 100644 --- a/DOCS/man/en/input.rst +++ b/DOCS/man/en/input.rst @@ -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`` =========================== = ================================================== diff --git a/core/command.c b/core/command.c index 0418e7a82c..5a617c20f5 100644 --- a/core/command.c +++ b/core/command.c @@ -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); diff --git a/core/mp_core.h b/core/mp_core.h index 0bd6ecda15..08f84dd817 100644 --- a/core/mp_core.h +++ b/core/mp_core.h @@ -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); diff --git a/core/mplayer.c b/core/mplayer.c index 4377f9f9bc..f12560b589 100644 --- a/core/mplayer.c +++ b/core/mplayer.c @@ -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, diff --git a/core/playlist.c b/core/playlist.c index 7188392bc6..4132ebec10 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -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; + } +} + diff --git a/core/playlist.h b/core/playlist.h index f896751eb6..3e34390707 100644 --- a/core/playlist.h +++ b/core/playlist.h @@ -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 -- cgit v1.2.3