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 82e732c9 2022-02-19 op #include <string.h>
22 3baa2617 2022-02-16 op #include <syslog.h>
24 3baa2617 2022-02-16 op #include "log.h"
25 3baa2617 2022-02-16 op #include "xmalloc.h"
26 3baa2617 2022-02-16 op #include "playlist.h"
28 3baa2617 2022-02-16 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
30 3baa2617 2022-02-16 op struct playlist playlist;
31 3baa2617 2022-02-16 op enum play_state play_state;
32 3baa2617 2022-02-16 op int repeat_one;
33 3baa2617 2022-02-16 op int repeat_all = 1;
34 3baa2617 2022-02-16 op ssize_t play_off = -1;
37 0977e96a 2022-02-17 op playlist_swap(struct playlist *p)
39 82e732c9 2022-02-19 op ssize_t i = -1;
41 82e732c9 2022-02-19 op if (play_off != -1) {
42 82e732c9 2022-02-19 op /* try to adjust play_off to match the same song */
43 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i) {
44 82e732c9 2022-02-19 op if (!strcmp(playlist.songs[play_off], p->songs[i]))
47 82e732c9 2022-02-19 op /* try to match one song before */
48 82e732c9 2022-02-19 op if (i == p->len && play_off >= 1)
49 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i)
50 82e732c9 2022-02-19 op if (!strcmp(playlist.songs[play_off-1],
53 82e732c9 2022-02-19 op /* or one song after */
54 82e732c9 2022-02-19 op if (i == p->len && play_off < playlist.len-1)
55 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i)
56 82e732c9 2022-02-19 op if (!strcmp(playlist.songs[play_off+1],
59 82e732c9 2022-02-19 op if (i == p->len)
63 0977e96a 2022-02-17 op playlist_truncate();
68 0977e96a 2022-02-17 op playlist.len = p->len;
69 0977e96a 2022-02-17 op playlist.cap = p->cap;
70 0977e96a 2022-02-17 op playlist.songs = p->songs;
74 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
76 3baa2617 2022-02-16 op size_t newcap;
78 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
79 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
80 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
81 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
82 532ca63c 2022-02-17 op playlist->cap = newcap;
85 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
89 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
91 532ca63c 2022-02-17 op playlist_push(&playlist, path);
95 3baa2617 2022-02-16 op playlist_current(void)
97 8891f624 2022-02-16 op if (playlist.len == 0 || play_off == -1) {
98 8891f624 2022-02-16 op play_state = STATE_STOPPED;
102 3baa2617 2022-02-16 op return playlist.songs[play_off];
106 3baa2617 2022-02-16 op playlist_advance(void)
108 8891f624 2022-02-16 op if (playlist.len == 0) {
109 8891f624 2022-02-16 op play_state = STATE_STOPPED;
114 3baa2617 2022-02-16 op if (play_off == playlist.len) {
115 3baa2617 2022-02-16 op if (repeat_all)
116 3baa2617 2022-02-16 op play_off = 0;
118 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
119 3baa2617 2022-02-16 op play_off = -1;
124 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
125 3baa2617 2022-02-16 op return playlist.songs[play_off];
129 af27e631 2022-02-17 op playlist_previous(void)
131 af27e631 2022-02-17 op if (playlist.len == 0) {
132 af27e631 2022-02-17 op play_state = STATE_STOPPED;
137 af27e631 2022-02-17 op if (play_off < 0) {
138 af27e631 2022-02-17 op if (repeat_all)
139 af27e631 2022-02-17 op play_off = playlist.len - 1;
141 af27e631 2022-02-17 op play_state = STATE_STOPPED;
142 af27e631 2022-02-17 op play_off = -1;
147 af27e631 2022-02-17 op play_state = STATE_PLAYING;
148 af27e631 2022-02-17 op return playlist.songs[play_off];
152 3baa2617 2022-02-16 op playlist_reset(void)
154 3baa2617 2022-02-16 op play_off = -1;
158 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
162 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
163 cd070aea 2022-02-17 op free(playlist->songs[i]);
164 cd070aea 2022-02-17 op free(playlist->songs);
165 cd070aea 2022-02-17 op playlist->songs = NULL;
167 cd070aea 2022-02-17 op playlist->len = 0;
168 cd070aea 2022-02-17 op playlist->cap = 0;
172 cd070aea 2022-02-17 op playlist_truncate(void)
174 cd070aea 2022-02-17 op playlist_free(&playlist);
175 3baa2617 2022-02-16 op play_off = -1;
179 13b83883 2022-02-16 op playlist_dropcurrent(void)
183 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
186 13b83883 2022-02-16 op free(playlist.songs[play_off]);
188 13b83883 2022-02-16 op playlist.len--;
189 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
190 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
192 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
196 a913de21 2022-02-17 op playlist_jump(const char *arg)
201 a913de21 2022-02-17 op if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
204 a913de21 2022-02-17 op for (i = 0; i < playlist.len; ++i) {
205 a913de21 2022-02-17 op if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
208 a913de21 2022-02-17 op regfree(&re);
210 a913de21 2022-02-17 op if (i == playlist.len)
213 a913de21 2022-02-17 op play_state = STATE_PLAYING;
214 a913de21 2022-02-17 op play_off = i;
215 a913de21 2022-02-17 op return playlist.songs[i];