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.
19 #include <sys/types.h>
30 #define MAX(a, b) ((a) > (b) ? (a) : (b))
32 struct playlist playlist;
33 enum play_state play_state;
36 ssize_t play_off = -1;
37 const char *current_song;
38 int64_t current_position;
39 int64_t current_duration;
44 free((char *)current_song);
48 current_song = xstrdup(playlist.songs[i]);
52 playlist_swap(struct playlist *p, ssize_t off)
59 if (current_song != NULL && off < 0) {
60 /* try to match the currently played song */
61 for (i = 0; i < p->len; ++i) {
62 if (!strcmp(current_song, p->songs[i]))
76 playlist.len = p->len;
77 playlist.cap = p->cap;
78 playlist.songs = p->songs;
80 if (play_state == STATE_STOPPED)
85 playlist_push(struct playlist *playlist, const char *path)
89 if (playlist->len == playlist->cap) {
90 newcap = MAX(16, playlist->cap * 1.5);
91 playlist->songs = xrecallocarray(playlist->songs,
92 playlist->cap, newcap, sizeof(*playlist->songs));
93 playlist->cap = newcap;
96 playlist->songs[playlist->len++] = xstrdup(path);
100 playlist_enqueue(const char *path)
102 playlist_push(&playlist, path);
106 playlist_advance(void)
108 if (playlist.len == 0) {
109 play_state = STATE_STOPPED;
114 if (play_off == playlist.len) {
118 play_state = STATE_STOPPED;
126 play_state = STATE_PLAYING;
127 return playlist.songs[play_off];
131 playlist_previous(void)
133 if (playlist.len == 0) {
134 play_state = STATE_STOPPED;
141 play_off = playlist.len - 1;
143 play_state = STATE_STOPPED;
151 play_state = STATE_PLAYING;
152 return playlist.songs[play_off];
162 playlist_free(struct playlist *playlist)
166 for (i = 0; i < playlist->len; ++i)
167 free(playlist->songs[i]);
168 free(playlist->songs);
169 playlist->songs = NULL;
176 playlist_truncate(void)
178 playlist_free(&playlist);
183 playlist_dropcurrent(void)
187 if (play_off == -1 || playlist.len == 0)
190 free(playlist.songs[play_off]);
194 for (i = play_off; i < playlist.len; ++i)
195 playlist.songs[i] = playlist.songs[i+1];
198 playlist.songs[playlist.len] = NULL;
202 playlist_jump(const char *arg)
207 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
210 for (i = 0; i < playlist.len; ++i) {
211 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
216 if (i == playlist.len)
219 play_state = STATE_PLAYING;
222 return playlist.songs[i];