Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
19 #include <regex.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <syslog.h>
24 #include "log.h"
25 #include "xmalloc.h"
26 #include "playlist.h"
28 #define MAX(a, b) ((a) > (b) ? (a) : (b))
30 struct playlist playlist;
31 enum play_state play_state;
32 int repeat_one;
33 int repeat_all = 1;
34 ssize_t play_off = -1;
36 void
37 playlist_swap(struct playlist *p)
38 {
39 ssize_t i = -1;
41 if (play_off != -1) {
42 /* try to adjust play_off to match the same song */
43 for (i = 0; i < p->len; ++i) {
44 if (!strcmp(playlist.songs[play_off], p->songs[i]))
45 break;
46 }
47 /* try to match one song before */
48 if (i == p->len && play_off >= 1)
49 for (i = 0; i < p->len; ++i)
50 if (!strcmp(playlist.songs[play_off-1],
51 p->songs[i]))
52 break;
53 /* or one song after */
54 if (i == p->len && play_off < playlist.len-1)
55 for (i = 0; i < p->len; ++i)
56 if (!strcmp(playlist.songs[play_off+1],
57 p->songs[i]))
58 break;
59 if (i == p->len)
60 i = -1;
61 }
63 playlist_truncate();
65 if (i != -1)
66 play_off = i;
68 playlist.len = p->len;
69 playlist.cap = p->cap;
70 playlist.songs = p->songs;
71 }
73 void
74 playlist_push(struct playlist *playlist, const char *path)
75 {
76 size_t newcap;
78 if (playlist->len == playlist->cap) {
79 newcap = MAX(16, playlist->cap * 1.5);
80 playlist->songs = xrecallocarray(playlist->songs,
81 playlist->cap, newcap, sizeof(*playlist->songs));
82 playlist->cap = newcap;
83 }
85 playlist->songs[playlist->len++] = xstrdup(path);
86 }
88 void
89 playlist_enqueue(const char *path)
90 {
91 playlist_push(&playlist, path);
92 }
94 const char *
95 playlist_current(void)
96 {
97 if (playlist.len == 0 || play_off == -1) {
98 play_state = STATE_STOPPED;
99 return NULL;
102 return playlist.songs[play_off];
105 const char *
106 playlist_advance(void)
108 if (playlist.len == 0) {
109 play_state = STATE_STOPPED;
110 return NULL;
113 play_off++;
114 if (play_off == playlist.len) {
115 if (repeat_all)
116 play_off = 0;
117 else {
118 play_state = STATE_STOPPED;
119 play_off = -1;
120 return NULL;
124 play_state = STATE_PLAYING;
125 return playlist.songs[play_off];
128 const char *
129 playlist_previous(void)
131 if (playlist.len == 0) {
132 play_state = STATE_STOPPED;
133 return NULL;
136 play_off--;
137 if (play_off < 0) {
138 if (repeat_all)
139 play_off = playlist.len - 1;
140 else {
141 play_state = STATE_STOPPED;
142 play_off = -1;
143 return NULL;
147 play_state = STATE_PLAYING;
148 return playlist.songs[play_off];
151 void
152 playlist_reset(void)
154 play_off = -1;
157 void
158 playlist_free(struct playlist *playlist)
160 size_t i;
162 for (i = 0; i < playlist->len; ++i)
163 free(playlist->songs[i]);
164 free(playlist->songs);
165 playlist->songs = NULL;
167 playlist->len = 0;
168 playlist->cap = 0;
171 void
172 playlist_truncate(void)
174 playlist_free(&playlist);
175 play_off = -1;
178 void
179 playlist_dropcurrent(void)
181 size_t i;
183 if (play_off == -1 || playlist.len == 0)
184 return;
186 free(playlist.songs[play_off]);
188 playlist.len--;
189 for (i = play_off; i < playlist.len; ++i)
190 playlist.songs[i] = playlist.songs[i+1];
192 playlist.songs[playlist.len] = NULL;
195 const char *
196 playlist_jump(const char *arg)
198 size_t i;
199 regex_t re;
201 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
202 return NULL;
204 for (i = 0; i < playlist.len; ++i) {
205 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
206 break;
208 regfree(&re);
210 if (i == playlist.len)
211 return NULL;
213 play_state = STATE_PLAYING;
214 play_off = i;
215 return playlist.songs[i];