2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a913de21 2022-02-17 op #include <sys/types.h>
19 a913de21 2022-02-17 op #include <regex.h>
20 3baa2617 2022-02-16 op #include <stdlib.h>
21 3baa2617 2022-02-16 op #include <syslog.h>
23 3baa2617 2022-02-16 op #include "log.h"
24 3baa2617 2022-02-16 op #include "xmalloc.h"
25 3baa2617 2022-02-16 op #include "playlist.h"
27 3baa2617 2022-02-16 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 3baa2617 2022-02-16 op struct playlist playlist;
30 3baa2617 2022-02-16 op enum play_state play_state;
31 3baa2617 2022-02-16 op int repeat_one;
32 3baa2617 2022-02-16 op int repeat_all = 1;
33 3baa2617 2022-02-16 op ssize_t play_off = -1;
36 0977e96a 2022-02-17 op playlist_swap(struct playlist *p)
38 0977e96a 2022-02-17 op playlist_truncate();
40 0977e96a 2022-02-17 op playlist.len = p->len;
41 0977e96a 2022-02-17 op playlist.cap = p->cap;
42 0977e96a 2022-02-17 op playlist.songs = p->songs;
46 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
48 3baa2617 2022-02-16 op size_t newcap;
50 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
51 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
52 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
53 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
54 532ca63c 2022-02-17 op playlist->cap = newcap;
57 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
61 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
63 532ca63c 2022-02-17 op playlist_push(&playlist, path);
67 3baa2617 2022-02-16 op playlist_current(void)
69 8891f624 2022-02-16 op if (playlist.len == 0 || play_off == -1) {
70 8891f624 2022-02-16 op play_state = STATE_STOPPED;
74 3baa2617 2022-02-16 op return playlist.songs[play_off];
78 3baa2617 2022-02-16 op playlist_advance(void)
80 8891f624 2022-02-16 op if (playlist.len == 0) {
81 8891f624 2022-02-16 op play_state = STATE_STOPPED;
86 3baa2617 2022-02-16 op if (play_off == playlist.len) {
87 3baa2617 2022-02-16 op if (repeat_all)
90 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
91 3baa2617 2022-02-16 op play_off = -1;
96 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
97 3baa2617 2022-02-16 op return playlist.songs[play_off];
101 af27e631 2022-02-17 op playlist_previous(void)
103 af27e631 2022-02-17 op if (playlist.len == 0) {
104 af27e631 2022-02-17 op play_state = STATE_STOPPED;
109 af27e631 2022-02-17 op if (play_off < 0) {
110 af27e631 2022-02-17 op if (repeat_all)
111 af27e631 2022-02-17 op play_off = playlist.len - 1;
113 af27e631 2022-02-17 op play_state = STATE_STOPPED;
114 af27e631 2022-02-17 op play_off = -1;
119 af27e631 2022-02-17 op play_state = STATE_PLAYING;
120 af27e631 2022-02-17 op return playlist.songs[play_off];
124 3baa2617 2022-02-16 op playlist_reset(void)
126 3baa2617 2022-02-16 op play_off = -1;
130 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
134 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
135 cd070aea 2022-02-17 op free(playlist->songs[i]);
136 cd070aea 2022-02-17 op free(playlist->songs);
137 cd070aea 2022-02-17 op playlist->songs = NULL;
139 cd070aea 2022-02-17 op playlist->len = 0;
140 cd070aea 2022-02-17 op playlist->cap = 0;
144 cd070aea 2022-02-17 op playlist_truncate(void)
146 cd070aea 2022-02-17 op playlist_free(&playlist);
147 3baa2617 2022-02-16 op play_off = -1;
151 13b83883 2022-02-16 op playlist_dropcurrent(void)
155 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
158 13b83883 2022-02-16 op free(playlist.songs[play_off]);
160 13b83883 2022-02-16 op playlist.len--;
161 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
162 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
164 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
168 a913de21 2022-02-17 op playlist_jump(const char *arg)
173 a913de21 2022-02-17 op if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
176 a913de21 2022-02-17 op for (i = 0; i < playlist.len; ++i) {
177 a913de21 2022-02-17 op if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
180 a913de21 2022-02-17 op regfree(&re);
182 a913de21 2022-02-17 op if (i == playlist.len)
185 a913de21 2022-02-17 op play_state = STATE_PLAYING;
186 a913de21 2022-02-17 op play_off = i;
187 a913de21 2022-02-17 op return playlist.songs[i];