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 f36fd90a 2022-07-09 op #include "config.h"
19 a913de21 2022-02-17 op #include <sys/types.h>
21 a913de21 2022-02-17 op #include <regex.h>
22 3baa2617 2022-02-16 op #include <stdlib.h>
23 82e732c9 2022-02-19 op #include <string.h>
24 3baa2617 2022-02-16 op #include <syslog.h>
26 3baa2617 2022-02-16 op #include "log.h"
27 3baa2617 2022-02-16 op #include "xmalloc.h"
28 3baa2617 2022-02-16 op #include "playlist.h"
30 3baa2617 2022-02-16 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
32 74c987d5 2022-02-19 op struct playlist playlist;
33 74c987d5 2022-02-19 op enum play_state play_state;
34 74c987d5 2022-02-19 op int repeat_one;
35 74c987d5 2022-02-19 op int repeat_all = 1;
36 74c987d5 2022-02-19 op ssize_t play_off = -1;
37 74c987d5 2022-02-19 op const char *current_song;
38 ff06024f 2022-07-08 op int64_t current_position;
39 ff06024f 2022-07-08 op int64_t current_duration;
42 74c987d5 2022-02-19 op setsong(ssize_t i)
44 74c987d5 2022-02-19 op free((char *)current_song);
46 74c987d5 2022-02-19 op current_song = NULL;
48 74c987d5 2022-02-19 op current_song = xstrdup(playlist.songs[i]);
52 3af93963 2022-03-02 op playlist_swap(struct playlist *p, ssize_t off)
54 82e732c9 2022-02-19 op ssize_t i = -1;
56 3af93963 2022-03-02 op if (off > p->len)
59 3af93963 2022-03-02 op if (current_song != NULL && off < 0) {
60 c86624f6 2022-03-10 op /* try to match the currently played song */
61 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i) {
62 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
65 82e732c9 2022-02-19 op if (i == p->len)
69 0977e96a 2022-02-17 op playlist_truncate();
73 3af93963 2022-03-02 op else if (off >= 0)
74 3af93963 2022-03-02 op play_off = off;
76 0977e96a 2022-02-17 op playlist.len = p->len;
77 0977e96a 2022-02-17 op playlist.cap = p->cap;
78 0977e96a 2022-02-17 op playlist.songs = p->songs;
80 76079111 2022-03-02 op if (play_state == STATE_STOPPED)
81 76079111 2022-03-02 op setsong(play_off);
85 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
87 3baa2617 2022-02-16 op size_t newcap;
89 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
90 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
91 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
92 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
93 532ca63c 2022-02-17 op playlist->cap = newcap;
96 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
100 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
102 532ca63c 2022-02-17 op playlist_push(&playlist, path);
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;
120 74c987d5 2022-02-19 op setsong(play_off);
125 74c987d5 2022-02-19 op setsong(play_off);
126 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
127 3baa2617 2022-02-16 op return playlist.songs[play_off];
131 af27e631 2022-02-17 op playlist_previous(void)
133 af27e631 2022-02-17 op if (playlist.len == 0) {
134 af27e631 2022-02-17 op play_state = STATE_STOPPED;
139 af27e631 2022-02-17 op if (play_off < 0) {
140 af27e631 2022-02-17 op if (repeat_all)
141 af27e631 2022-02-17 op play_off = playlist.len - 1;
143 af27e631 2022-02-17 op play_state = STATE_STOPPED;
144 af27e631 2022-02-17 op play_off = -1;
145 74c987d5 2022-02-19 op setsong(play_off);
150 74c987d5 2022-02-19 op setsong(play_off);
151 af27e631 2022-02-17 op play_state = STATE_PLAYING;
152 af27e631 2022-02-17 op return playlist.songs[play_off];
156 3baa2617 2022-02-16 op playlist_reset(void)
158 3baa2617 2022-02-16 op play_off = -1;
162 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
166 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
167 cd070aea 2022-02-17 op free(playlist->songs[i]);
168 cd070aea 2022-02-17 op free(playlist->songs);
169 cd070aea 2022-02-17 op playlist->songs = NULL;
171 cd070aea 2022-02-17 op playlist->len = 0;
172 cd070aea 2022-02-17 op playlist->cap = 0;
176 cd070aea 2022-02-17 op playlist_truncate(void)
178 cd070aea 2022-02-17 op playlist_free(&playlist);
179 3baa2617 2022-02-16 op play_off = -1;
183 13b83883 2022-02-16 op playlist_dropcurrent(void)
187 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
190 13b83883 2022-02-16 op free(playlist.songs[play_off]);
193 13b83883 2022-02-16 op playlist.len--;
194 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
195 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
198 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
202 a913de21 2022-02-17 op playlist_jump(const char *arg)
207 a913de21 2022-02-17 op if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
210 a913de21 2022-02-17 op for (i = 0; i < playlist.len; ++i) {
211 a913de21 2022-02-17 op if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
214 a913de21 2022-02-17 op regfree(&re);
216 a913de21 2022-02-17 op if (i == playlist.len)
219 a913de21 2022-02-17 op play_state = STATE_PLAYING;
220 a913de21 2022-02-17 op play_off = i;
221 74c987d5 2022-02-19 op setsong(play_off);
222 a913de21 2022-02-17 op return playlist.songs[i];