2 e3317c86 2023-05-02 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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;
37 74c987d5 2022-02-19 op ssize_t play_off = -1;
38 74c987d5 2022-02-19 op const char *current_song;
39 ff06024f 2022-07-08 op int64_t current_position;
40 ff06024f 2022-07-08 op int64_t current_duration;
43 74c987d5 2022-02-19 op setsong(ssize_t i)
45 74c987d5 2022-02-19 op free((char *)current_song);
47 74c987d5 2022-02-19 op current_song = NULL;
49 74c987d5 2022-02-19 op current_song = xstrdup(playlist.songs[i]);
53 3af93963 2022-03-02 op playlist_swap(struct playlist *p, ssize_t off)
55 82e732c9 2022-02-19 op ssize_t i = -1;
57 3af93963 2022-03-02 op if (off > p->len)
60 3af93963 2022-03-02 op if (current_song != NULL && off < 0) {
61 c86624f6 2022-03-10 op /* try to match the currently played song */
62 82e732c9 2022-02-19 op for (i = 0; i < p->len; ++i) {
63 b21ec899 2022-02-19 op if (!strcmp(current_song, p->songs[i]))
66 82e732c9 2022-02-19 op if (i == p->len)
70 0977e96a 2022-02-17 op playlist_truncate();
74 3af93963 2022-03-02 op else if (off >= 0)
75 3af93963 2022-03-02 op play_off = off;
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;
81 76079111 2022-03-02 op if (play_state == STATE_STOPPED)
82 76079111 2022-03-02 op setsong(play_off);
86 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
88 3baa2617 2022-02-16 op size_t newcap;
90 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
91 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
92 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
93 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
94 532ca63c 2022-02-17 op playlist->cap = newcap;
97 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
101 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
103 532ca63c 2022-02-17 op playlist_push(&playlist, path);
107 3baa2617 2022-02-16 op playlist_advance(void)
109 8891f624 2022-02-16 op if (playlist.len == 0) {
110 8891f624 2022-02-16 op play_state = STATE_STOPPED;
115 3baa2617 2022-02-16 op if (play_off == playlist.len) {
116 3baa2617 2022-02-16 op if (repeat_all)
117 3baa2617 2022-02-16 op play_off = 0;
119 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
120 3baa2617 2022-02-16 op play_off = -1;
121 74c987d5 2022-02-19 op setsong(play_off);
126 74c987d5 2022-02-19 op setsong(play_off);
127 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
128 3baa2617 2022-02-16 op return playlist.songs[play_off];
132 af27e631 2022-02-17 op playlist_previous(void)
134 af27e631 2022-02-17 op if (playlist.len == 0) {
135 af27e631 2022-02-17 op play_state = STATE_STOPPED;
140 af27e631 2022-02-17 op if (play_off < 0) {
141 af27e631 2022-02-17 op if (repeat_all)
142 af27e631 2022-02-17 op play_off = playlist.len - 1;
144 af27e631 2022-02-17 op play_state = STATE_STOPPED;
145 af27e631 2022-02-17 op play_off = -1;
146 74c987d5 2022-02-19 op setsong(play_off);
151 74c987d5 2022-02-19 op setsong(play_off);
152 af27e631 2022-02-17 op play_state = STATE_PLAYING;
153 af27e631 2022-02-17 op return playlist.songs[play_off];
157 3baa2617 2022-02-16 op playlist_reset(void)
159 3baa2617 2022-02-16 op play_off = -1;
163 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
167 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
168 cd070aea 2022-02-17 op free(playlist->songs[i]);
169 cd070aea 2022-02-17 op free(playlist->songs);
170 cd070aea 2022-02-17 op playlist->songs = NULL;
172 cd070aea 2022-02-17 op playlist->len = 0;
173 cd070aea 2022-02-17 op playlist->cap = 0;
177 cd070aea 2022-02-17 op playlist_truncate(void)
179 cd070aea 2022-02-17 op playlist_free(&playlist);
180 3baa2617 2022-02-16 op play_off = -1;
184 13b83883 2022-02-16 op playlist_dropcurrent(void)
188 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
191 13b83883 2022-02-16 op free(playlist.songs[play_off]);
194 13b83883 2022-02-16 op playlist.len--;
195 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
196 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
199 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
203 a913de21 2022-02-17 op playlist_jump(const char *arg)
208 a913de21 2022-02-17 op if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
211 a913de21 2022-02-17 op for (i = 0; i < playlist.len; ++i) {
212 a913de21 2022-02-17 op if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
215 a913de21 2022-02-17 op regfree(&re);
217 a913de21 2022-02-17 op if (i == playlist.len)
220 a913de21 2022-02-17 op play_state = STATE_PLAYING;
221 a913de21 2022-02-17 op play_off = i;
222 74c987d5 2022-02-19 op setsong(play_off);
223 a913de21 2022-02-17 op return playlist.songs[i];