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.
17 #include <sys/types.h>
28 #define MAX(a, b) ((a) > (b) ? (a) : (b))
30 struct playlist playlist;
31 enum play_state play_state;
34 ssize_t play_off = -1;
35 const char *current_song;
40 free((char *)current_song);
44 current_song = xstrdup(playlist.songs[i]);
48 playlist_swap(struct playlist *p, ssize_t off)
55 if (current_song != NULL && off < 0) {
56 /* try to adjust play_off to match the same song */
57 for (i = 0; i < p->len; ++i) {
58 if (!strcmp(current_song, p->songs[i]))
61 /* try to match one song before */
62 if (i == p->len && play_off >= 1)
63 for (i = 0; i < p->len; ++i)
64 if (!strcmp(current_song, p->songs[i]))
66 /* or one song after */
67 if (i == p->len && play_off < playlist.len-1)
68 for (i = 0; i < p->len; ++i)
69 if (!strcmp(current_song, p->songs[i]))
82 playlist.len = p->len;
83 playlist.cap = p->cap;
84 playlist.songs = p->songs;
86 if (play_state == STATE_STOPPED)
91 playlist_push(struct playlist *playlist, const char *path)
95 if (playlist->len == playlist->cap) {
96 newcap = MAX(16, playlist->cap * 1.5);
97 playlist->songs = xrecallocarray(playlist->songs,
98 playlist->cap, newcap, sizeof(*playlist->songs));
99 playlist->cap = newcap;
102 playlist->songs[playlist->len++] = xstrdup(path);
106 playlist_enqueue(const char *path)
108 playlist_push(&playlist, path);
112 playlist_advance(void)
114 if (playlist.len == 0) {
115 play_state = STATE_STOPPED;
120 if (play_off == playlist.len) {
124 play_state = STATE_STOPPED;
132 play_state = STATE_PLAYING;
133 return playlist.songs[play_off];
137 playlist_previous(void)
139 if (playlist.len == 0) {
140 play_state = STATE_STOPPED;
147 play_off = playlist.len - 1;
149 play_state = STATE_STOPPED;
157 play_state = STATE_PLAYING;
158 return playlist.songs[play_off];
168 playlist_free(struct playlist *playlist)
172 for (i = 0; i < playlist->len; ++i)
173 free(playlist->songs[i]);
174 free(playlist->songs);
175 playlist->songs = NULL;
182 playlist_truncate(void)
184 playlist_free(&playlist);
189 playlist_dropcurrent(void)
193 if (play_off == -1 || playlist.len == 0)
196 free(playlist.songs[play_off]);
200 for (i = play_off; i < playlist.len; ++i)
201 playlist.songs[i] = playlist.songs[i+1];
204 playlist.songs[playlist.len] = NULL;
208 playlist_jump(const char *arg)
213 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
216 for (i = 0; i < playlist.len; ++i) {
217 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
222 if (i == playlist.len)
225 play_state = STATE_PLAYING;
228 return playlist.songs[i];