Skip to content

Commit

Permalink
common/playlist: add playlist_set_current()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasper93 committed Aug 10, 2024
1 parent 73b5872 commit ff1f8ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
21 changes: 21 additions & 0 deletions common/playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 2 additions & 0 deletions common/playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 2 additions & 14 deletions player/loadfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ff1f8ba

Please sign in to comment.