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 0977e96a 2022-02-17 op playlist_swap(struct playlist *p)
50 82e732c9 2022-02-19 op ssize_t i = -1;
52 b21ec899 2022-02-19 op if (current_song != NULL) {
53 82e732c9 2022-02-19 op /* try to adjust play_off to match the same song */
54 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i) {
55 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
58 82e732c9 2022-02-19 op /* try to match one song before */
59 82e732c9 2022-02-19 op if (i == p->len && play_off >= 1)
60 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i)
61 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
63 82e732c9 2022-02-19 op /* or one song after */
64 82e732c9 2022-02-19 op if (i == p->len && play_off < playlist.len-1)
65 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i)
66 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
68 82e732c9 2022-02-19 op if (i == p->len)
72 0977e96a 2022-02-17 op playlist_truncate();
77 0977e96a 2022-02-17 op playlist.len = p->len;
78 0977e96a 2022-02-17 op playlist.cap = p->cap;
79 0977e96a 2022-02-17 op playlist.songs = p->songs;
83 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
85 3baa2617 2022-02-16 op size_t newcap;
87 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
88 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
89 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
90 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
91 532ca63c 2022-02-17 op playlist->cap = newcap;
94 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
98 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
100 532ca63c 2022-02-17 op playlist_push(&playlist, path);
104 3baa2617 2022-02-16 op playlist_advance(void)
106 8891f624 2022-02-16 op if (playlist.len == 0) {
107 8891f624 2022-02-16 op play_state = STATE_STOPPED;
112 3baa2617 2022-02-16 op if (play_off == playlist.len) {
113 3baa2617 2022-02-16 op if (repeat_all)
114 3baa2617 2022-02-16 op play_off = 0;
116 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
117 3baa2617 2022-02-16 op play_off = -1;
118 74c987d5 2022-02-19 op setsong(play_off);
123 74c987d5 2022-02-19 op setsong(play_off);
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;
143 74c987d5 2022-02-19 op setsong(play_off);
148 74c987d5 2022-02-19 op setsong(play_off);
149 af27e631 2022-02-17 op play_state = STATE_PLAYING;
150 af27e631 2022-02-17 op return playlist.songs[play_off];
154 3baa2617 2022-02-16 op playlist_reset(void)
156 3baa2617 2022-02-16 op play_off = -1;
160 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
164 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
165 cd070aea 2022-02-17 op free(playlist->songs[i]);
166 cd070aea 2022-02-17 op free(playlist->songs);
167 cd070aea 2022-02-17 op playlist->songs = NULL;
169 cd070aea 2022-02-17 op playlist->len = 0;
170 cd070aea 2022-02-17 op playlist->cap = 0;
174 cd070aea 2022-02-17 op playlist_truncate(void)
176 cd070aea 2022-02-17 op playlist_free(&playlist);
177 3baa2617 2022-02-16 op play_off = -1;
181 13b83883 2022-02-16 op playlist_dropcurrent(void)
185 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
188 13b83883 2022-02-16 op free(playlist.songs[play_off]);
190 13b83883 2022-02-16 op playlist.len--;
191 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
192 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
194 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
198 a913de21 2022-02-17 op playlist_jump(const char *arg)
203 a913de21 2022-02-17 op if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
206 a913de21 2022-02-17 op for (i = 0; i < playlist.len; ++i) {
207 a913de21 2022-02-17 op if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
210 a913de21 2022-02-17 op regfree(&re);
212 a913de21 2022-02-17 op if (i == playlist.len)
215 a913de21 2022-02-17 op play_state = STATE_PLAYING;
216 a913de21 2022-02-17 op play_off = i;
217 74c987d5 2022-02-19 op setsong(play_off);
218 a913de21 2022-02-17 op return playlist.songs[i];