From ff1f8baecdbdebb29ced0efaf921cae9edace82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Mon, 29 Jul 2024 21:10:44 +0200 Subject: [PATCH] common/playlist: add playlist_set_current() --- common/playlist.c | 21 +++++++++++++++++++++ common/playlist.h | 2 ++ player/loadfile.c | 16 ++-------------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/common/playlist.c b/common/playlist.c index 1851d5c783..a66fbf3a4b 100644 --- a/common/playlist.c +++ b/common/playlist.c @@ -434,3 +434,24 @@ struct playlist *playlist_parse_file(const char *file, struct mp_cancel *cancel, talloc_free(log); return ret; } + +void playlist_set_current(struct playlist *pl) +{ + if (!pl->playlist_dir) + return; + + for (int i = 0; i < pl->num_entries; ++i) { + if (!pl->entries[i]->playlist_path) + continue; + char *path = pl->entries[i]->playlist_path; + if (path[0] != '.') + path = mp_path_join(NULL, pl->playlist_dir, pl->entries[i]->playlist_path); + bool same = !strcmp(pl->entries[i]->filename, path); + if (path != pl->entries[i]->playlist_path) + talloc_free(path); + if (same) { + pl->current = pl->entries[i]; + break; + } + } +} diff --git a/common/playlist.h b/common/playlist.h index 5690ac8adc..fe5410488e 100644 --- a/common/playlist.h +++ b/common/playlist.h @@ -125,4 +125,6 @@ struct playlist *playlist_parse_file(const char *file, struct mp_cancel *cancel, void playlist_entry_unref(struct playlist_entry *e); +void playlist_set_current(struct playlist *pl); + #endif diff --git a/player/loadfile.c b/player/loadfile.c index 45c39ccb9b..65ed7d4310 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -1042,20 +1042,8 @@ void prepare_playlist(struct MPContext *mpctx, struct playlist *pl) if (opts->playlist_pos >= 0) pl->current = playlist_entry_from_index(pl, opts->playlist_pos); - for (int i = 0; i < pl->num_entries && pl->playlist_dir; ++i) { - if (!pl->entries[i]->playlist_path) - continue; - char *path = pl->entries[i]->playlist_path; - if (path[0] != '.') - path = mp_path_join(NULL, pl->playlist_dir, pl->entries[i]->playlist_path); - bool same = !strcmp(pl->entries[i]->filename, path); - if (path != pl->entries[i]->playlist_path) - talloc_free(path); - if (same) { - pl->current = pl->entries[i]; - break; - } - } + if (pl->playlist_dir) + playlist_set_current(pl); if (opts->shuffle) playlist_shuffle(pl);