2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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;
37 ssize_t play_off = -1;
38 const char *current_song;
39 int64_t current_position;
40 int64_t current_duration;
45 free((char *)current_song);
49 current_song = xstrdup(playlist.songs[i]);
53 playlist_swap(struct playlist *p, ssize_t off)
60 if (current_song != NULL && off < 0) {
61 /* try to match the currently played song */
62 for (i = 0; i < p->len; ++i) {
63 if (!strcmp(current_song, p->songs[i]))
77 playlist.len = p->len;
78 playlist.cap = p->cap;
79 playlist.songs = p->songs;
81 if (play_state == STATE_STOPPED)
86 playlist_push(struct playlist *playlist, const char *path)
90 if (playlist->len == playlist->cap) {
91 newcap = MAX(16, playlist->cap * 1.5);
92 playlist->songs = xrecallocarray(playlist->songs,
93 playlist->cap, newcap, sizeof(*playlist->songs));
94 playlist->cap = newcap;
97 playlist->songs[playlist->len++] = xstrdup(path);
101 playlist_enqueue(const char *path)
103 playlist_push(&playlist, path);
107 playlist_advance(void)
109 if (playlist.len == 0) {
110 play_state = STATE_STOPPED;
115 if (play_off == playlist.len) {
119 play_state = STATE_STOPPED;
127 play_state = STATE_PLAYING;
128 return playlist.songs[play_off];
132 playlist_previous(void)
134 if (playlist.len == 0) {
135 play_state = STATE_STOPPED;
142 play_off = playlist.len - 1;
144 play_state = STATE_STOPPED;
152 play_state = STATE_PLAYING;
153 return playlist.songs[play_off];
163 playlist_free(struct playlist *playlist)
167 for (i = 0; i < playlist->len; ++i)
168 free(playlist->songs[i]);
169 free(playlist->songs);
170 playlist->songs = NULL;
177 playlist_truncate(void)
179 playlist_free(&playlist);
184 playlist_dropcurrent(void)
188 if (play_off == -1 || playlist.len == 0)
191 free(playlist.songs[play_off]);
195 for (i = play_off; i < playlist.len; ++i)
196 playlist.songs[i] = playlist.songs[i+1];
199 playlist.songs[playlist.len] = NULL;
203 playlist_jump(const char *arg)
208 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
211 for (i = 0; i < playlist.len; ++i) {
212 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
217 if (i == playlist.len)
220 play_state = STATE_PLAYING;
223 return playlist.songs[i];