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;
88 playlist_push(struct playlist *playlist, const char *path)
92 if (playlist->len == playlist->cap) {
93 newcap = MAX(16, playlist->cap * 1.5);
94 playlist->songs = xrecallocarray(playlist->songs,
95 playlist->cap, newcap, sizeof(*playlist->songs));
96 playlist->cap = newcap;
99 playlist->songs[playlist->len++] = xstrdup(path);
103 playlist_enqueue(const char *path)
105 playlist_push(&playlist, path);
109 playlist_advance(void)
111 if (playlist.len == 0) {
112 play_state = STATE_STOPPED;
117 if (play_off == playlist.len) {
121 play_state = STATE_STOPPED;
129 play_state = STATE_PLAYING;
130 return playlist.songs[play_off];
134 playlist_previous(void)
136 if (playlist.len == 0) {
137 play_state = STATE_STOPPED;
144 play_off = playlist.len - 1;
146 play_state = STATE_STOPPED;
154 play_state = STATE_PLAYING;
155 return playlist.songs[play_off];
165 playlist_free(struct playlist *playlist)
169 for (i = 0; i < playlist->len; ++i)
170 free(playlist->songs[i]);
171 free(playlist->songs);
172 playlist->songs = NULL;
179 playlist_truncate(void)
181 playlist_free(&playlist);
186 playlist_dropcurrent(void)
190 if (play_off == -1 || playlist.len == 0)
193 free(playlist.songs[play_off]);
197 for (i = play_off; i < playlist.len; ++i)
198 playlist.songs[i] = playlist.songs[i+1];
201 playlist.songs[playlist.len] = NULL;
205 playlist_jump(const char *arg)
210 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
213 for (i = 0; i < playlist.len; ++i) {
214 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
219 if (i == playlist.len)
222 play_state = STATE_PLAYING;
225 return playlist.songs[i];