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)
52 if (current_song != NULL) {
53 /* try to adjust play_off to match the same song */
54 for (i = 0; i < p->len; ++i) {
55 if (!strcmp(current_song, p->songs[i]))
58 /* try to match one song before */
59 if (i == p->len && play_off >= 1)
60 for (i = 0; i < p->len; ++i)
61 if (!strcmp(current_song, p->songs[i]))
63 /* or one song after */
64 if (i == p->len && play_off < playlist.len-1)
65 for (i = 0; i < p->len; ++i)
66 if (!strcmp(current_song, p->songs[i]))
77 playlist.len = p->len;
78 playlist.cap = p->cap;
79 playlist.songs = p->songs;
83 playlist_push(struct playlist *playlist, const char *path)
87 if (playlist->len == playlist->cap) {
88 newcap = MAX(16, playlist->cap * 1.5);
89 playlist->songs = xrecallocarray(playlist->songs,
90 playlist->cap, newcap, sizeof(*playlist->songs));
91 playlist->cap = newcap;
94 playlist->songs[playlist->len++] = xstrdup(path);
98 playlist_enqueue(const char *path)
100 playlist_push(&playlist, path);
104 playlist_advance(void)
106 if (playlist.len == 0) {
107 play_state = STATE_STOPPED;
112 if (play_off == playlist.len) {
116 play_state = STATE_STOPPED;
124 play_state = STATE_PLAYING;
125 return playlist.songs[play_off];
129 playlist_previous(void)
131 if (playlist.len == 0) {
132 play_state = STATE_STOPPED;
139 play_off = playlist.len - 1;
141 play_state = STATE_STOPPED;
149 play_state = STATE_PLAYING;
150 return playlist.songs[play_off];
160 playlist_free(struct playlist *playlist)
164 for (i = 0; i < playlist->len; ++i)
165 free(playlist->songs[i]);
166 free(playlist->songs);
167 playlist->songs = NULL;
174 playlist_truncate(void)
176 playlist_free(&playlist);
181 playlist_dropcurrent(void)
185 if (play_off == -1 || playlist.len == 0)
188 free(playlist.songs[play_off]);
191 for (i = play_off; i < playlist.len; ++i)
192 playlist.songs[i] = playlist.songs[i+1];
194 playlist.songs[playlist.len] = NULL;
198 playlist_jump(const char *arg)
203 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
206 for (i = 0; i < playlist.len; ++i) {
207 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
212 if (i == playlist.len)
215 play_state = STATE_PLAYING;
218 return playlist.songs[i];