2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #define MAX(a, b) ((a) > (b) ? (a) : (b))
26 struct playlist playlist;
27 enum play_state play_state;
30 ssize_t play_off = -1;
33 playlist_enqueue(const char *path)
37 if (playlist.len == playlist.cap) {
38 newcap = MAX(16, playlist.cap * 1.5);
39 playlist.songs = xrecallocarray(playlist.songs, playlist.cap,
40 newcap, sizeof(*playlist.songs));
41 playlist.cap = newcap;
44 playlist.songs[playlist.len++] = xstrdup(path);
48 playlist_current(void)
50 if (playlist.len == 0 || play_off == -1)
53 return playlist.songs[play_off];
57 playlist_advance(void)
59 if (playlist.len == 0)
63 if (play_off == playlist.len) {
67 play_state = STATE_STOPPED;
73 play_state = STATE_PLAYING;
74 return playlist.songs[play_off];
84 playlist_truncate(void)
88 for (i = 0; i < playlist.len; ++i)
89 free(playlist.songs[i]);
91 playlist.songs = NULL;