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 74c987d5 2022-02-19 op struct playlist playlist;
31 74c987d5 2022-02-19 op enum play_state play_state;
32 74c987d5 2022-02-19 op int repeat_one;
33 74c987d5 2022-02-19 op int repeat_all = 1;
34 74c987d5 2022-02-19 op ssize_t play_off = -1;
35 74c987d5 2022-02-19 op const char *current_song;
38 74c987d5 2022-02-19 op setsong(ssize_t i)
40 74c987d5 2022-02-19 op free((char *)current_song);
42 74c987d5 2022-02-19 op current_song = NULL;
44 74c987d5 2022-02-19 op current_song = xstrdup(playlist.songs[i]);
48 3af93963 2022-03-02 op playlist_swap(struct playlist *p, ssize_t off)
50 82e732c9 2022-02-19 op ssize_t i = -1;
52 3af93963 2022-03-02 op if (off > p->len)
55 3af93963 2022-03-02 op if (current_song != NULL && off < 0) {
56 82e732c9 2022-02-19 op /* try to adjust play_off to match the same song */
57 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i) {
58 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
61 82e732c9 2022-02-19 op /* try to match one song before */
62 82e732c9 2022-02-19 op if (i == p->len && play_off >= 1)
63 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i)
64 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
66 82e732c9 2022-02-19 op /* or one song after */
67 82e732c9 2022-02-19 op if (i == p->len && play_off < playlist.len-1)
68 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i)
69 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
71 82e732c9 2022-02-19 op if (i == p->len)
75 0977e96a 2022-02-17 op playlist_truncate();
79 3af93963 2022-03-02 op else if (off >= 0)
80 3af93963 2022-03-02 op play_off = off;
82 0977e96a 2022-02-17 op playlist.len = p->len;
83 0977e96a 2022-02-17 op playlist.cap = p->cap;
84 0977e96a 2022-02-17 op playlist.songs = p->songs;
86 76079111 2022-03-02 op if (play_state == STATE_STOPPED)
87 76079111 2022-03-02 op setsong(play_off);
91 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
93 3baa2617 2022-02-16 op size_t newcap;
95 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
96 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
97 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
98 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
99 532ca63c 2022-02-17 op playlist->cap = newcap;
102 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
106 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
108 532ca63c 2022-02-17 op playlist_push(&playlist, path);
112 3baa2617 2022-02-16 op playlist_advance(void)
114 8891f624 2022-02-16 op if (playlist.len == 0) {
115 8891f624 2022-02-16 op play_state = STATE_STOPPED;
120 3baa2617 2022-02-16 op if (play_off == playlist.len) {
121 3baa2617 2022-02-16 op if (repeat_all)
122 3baa2617 2022-02-16 op play_off = 0;
124 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
125 3baa2617 2022-02-16 op play_off = -1;
126 74c987d5 2022-02-19 op setsong(play_off);
131 74c987d5 2022-02-19 op setsong(play_off);
132 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
133 3baa2617 2022-02-16 op return playlist.songs[play_off];
137 af27e631 2022-02-17 op playlist_previous(void)
139 af27e631 2022-02-17 op if (playlist.len == 0) {
140 af27e631 2022-02-17 op play_state = STATE_STOPPED;
145 af27e631 2022-02-17 op if (play_off < 0) {
146 af27e631 2022-02-17 op if (repeat_all)
147 af27e631 2022-02-17 op play_off = playlist.len - 1;
149 af27e631 2022-02-17 op play_state = STATE_STOPPED;
150 af27e631 2022-02-17 op play_off = -1;
151 74c987d5 2022-02-19 op setsong(play_off);
156 74c987d5 2022-02-19 op setsong(play_off);
157 af27e631 2022-02-17 op play_state = STATE_PLAYING;
158 af27e631 2022-02-17 op return playlist.songs[play_off];
162 3baa2617 2022-02-16 op playlist_reset(void)
164 3baa2617 2022-02-16 op play_off = -1;
168 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
172 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
173 cd070aea 2022-02-17 op free(playlist->songs[i]);
174 cd070aea 2022-02-17 op free(playlist->songs);
175 cd070aea 2022-02-17 op playlist->songs = NULL;
177 cd070aea 2022-02-17 op playlist->len = 0;
178 cd070aea 2022-02-17 op playlist->cap = 0;
182 cd070aea 2022-02-17 op playlist_truncate(void)
184 cd070aea 2022-02-17 op playlist_free(&playlist);
185 3baa2617 2022-02-16 op play_off = -1;
189 13b83883 2022-02-16 op playlist_dropcurrent(void)
193 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
196 13b83883 2022-02-16 op free(playlist.songs[play_off]);
199 13b83883 2022-02-16 op playlist.len--;
200 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
201 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
204 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
208 a913de21 2022-02-17 op playlist_jump(const char *arg)
213 a913de21 2022-02-17 op if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
216 a913de21 2022-02-17 op for (i = 0; i < playlist.len; ++i) {
217 a913de21 2022-02-17 op if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
220 a913de21 2022-02-17 op regfree(&re);
222 a913de21 2022-02-17 op if (i == playlist.len)
225 a913de21 2022-02-17 op play_state = STATE_PLAYING;
226 a913de21 2022-02-17 op play_off = i;
227 74c987d5 2022-02-19 op setsong(play_off);
228 a913de21 2022-02-17 op return playlist.songs[i];