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;
37 playlist_swap(struct playlist *p)
42 /* try to adjust play_off to match the same song */
43 for (i = 0; i < p->len; ++i) {
44 if (!strcmp(playlist.songs[play_off], p->songs[i]))
47 /* try to match one song before */
48 if (i == p->len && play_off >= 1)
49 for (i = 0; i < p->len; ++i)
50 if (!strcmp(playlist.songs[play_off-1],
53 /* or one song after */
54 if (i == p->len && play_off < playlist.len-1)
55 for (i = 0; i < p->len; ++i)
56 if (!strcmp(playlist.songs[play_off+1],
68 playlist.len = p->len;
69 playlist.cap = p->cap;
70 playlist.songs = p->songs;
74 playlist_push(struct playlist *playlist, const char *path)
78 if (playlist->len == playlist->cap) {
79 newcap = MAX(16, playlist->cap * 1.5);
80 playlist->songs = xrecallocarray(playlist->songs,
81 playlist->cap, newcap, sizeof(*playlist->songs));
82 playlist->cap = newcap;
85 playlist->songs[playlist->len++] = xstrdup(path);
89 playlist_enqueue(const char *path)
91 playlist_push(&playlist, path);
95 playlist_current(void)
97 if (playlist.len == 0 || play_off == -1) {
98 play_state = STATE_STOPPED;
102 return playlist.songs[play_off];
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;
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;
147 play_state = STATE_PLAYING;
148 return playlist.songs[play_off];
158 playlist_free(struct playlist *playlist)
162 for (i = 0; i < playlist->len; ++i)
163 free(playlist->songs[i]);
164 free(playlist->songs);
165 playlist->songs = NULL;
172 playlist_truncate(void)
174 playlist_free(&playlist);
179 playlist_dropcurrent(void)
183 if (play_off == -1 || playlist.len == 0)
186 free(playlist.songs[play_off]);
189 for (i = play_off; i < playlist.len; ++i)
190 playlist.songs[i] = playlist.songs[i+1];
192 playlist.songs[playlist.len] = NULL;
196 playlist_jump(const char *arg)
201 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
204 for (i = 0; i < playlist.len; ++i) {
205 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
210 if (i == playlist.len)
213 play_state = STATE_PLAYING;
215 return playlist.songs[i];