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;
35 const char *current_song;
37 static void
38 setsong(ssize_t i)
39 {
40 free((char *)current_song);
41 if (i == -1)
42 current_song = NULL;
43 else
44 current_song = xstrdup(playlist.songs[i]);
45 }
47 void
48 playlist_swap(struct playlist *p, ssize_t off)
49 {
50 ssize_t i = -1;
52 if (off > p->len)
53 off = -1;
55 if (current_song != NULL && off < 0) {
56 /* try to adjust play_off to match the same song */
57 for (i = 0; i < p->len; ++i) {
58 if (!strcmp(current_song, p->songs[i]))
59 break;
60 }
61 /* try to match one song before */
62 if (i == p->len && play_off >= 1)
63 for (i = 0; i < p->len; ++i)
64 if (!strcmp(current_song, p->songs[i]))
65 break;
66 /* or one song after */
67 if (i == p->len && play_off < playlist.len-1)
68 for (i = 0; i < p->len; ++i)
69 if (!strcmp(current_song, p->songs[i]))
70 break;
71 if (i == p->len)
72 i = -1;
73 }
75 playlist_truncate();
77 if (i != -1)
78 play_off = i;
79 else if (off >= 0)
80 play_off = off;
82 playlist.len = p->len;
83 playlist.cap = p->cap;
84 playlist.songs = p->songs;
86 if (play_state == STATE_STOPPED)
87 setsong(play_off);
88 }
90 void
91 playlist_push(struct playlist *playlist, const char *path)
92 {
93 size_t newcap;
95 if (playlist->len == playlist->cap) {
96 newcap = MAX(16, playlist->cap * 1.5);
97 playlist->songs = xrecallocarray(playlist->songs,
98 playlist->cap, newcap, sizeof(*playlist->songs));
99 playlist->cap = newcap;
102 playlist->songs[playlist->len++] = xstrdup(path);
105 void
106 playlist_enqueue(const char *path)
108 playlist_push(&playlist, path);
111 const char *
112 playlist_advance(void)
114 if (playlist.len == 0) {
115 play_state = STATE_STOPPED;
116 return NULL;
119 play_off++;
120 if (play_off == playlist.len) {
121 if (repeat_all)
122 play_off = 0;
123 else {
124 play_state = STATE_STOPPED;
125 play_off = -1;
126 setsong(play_off);
127 return NULL;
131 setsong(play_off);
132 play_state = STATE_PLAYING;
133 return playlist.songs[play_off];
136 const char *
137 playlist_previous(void)
139 if (playlist.len == 0) {
140 play_state = STATE_STOPPED;
141 return NULL;
144 play_off--;
145 if (play_off < 0) {
146 if (repeat_all)
147 play_off = playlist.len - 1;
148 else {
149 play_state = STATE_STOPPED;
150 play_off = -1;
151 setsong(play_off);
152 return NULL;
156 setsong(play_off);
157 play_state = STATE_PLAYING;
158 return playlist.songs[play_off];
161 void
162 playlist_reset(void)
164 play_off = -1;
167 void
168 playlist_free(struct playlist *playlist)
170 size_t i;
172 for (i = 0; i < playlist->len; ++i)
173 free(playlist->songs[i]);
174 free(playlist->songs);
175 playlist->songs = NULL;
177 playlist->len = 0;
178 playlist->cap = 0;
181 void
182 playlist_truncate(void)
184 playlist_free(&playlist);
185 play_off = -1;
188 void
189 playlist_dropcurrent(void)
191 size_t i;
193 if (play_off == -1 || playlist.len == 0)
194 return;
196 free(playlist.songs[play_off]);
197 setsong(-1);
199 playlist.len--;
200 for (i = play_off; i < playlist.len; ++i)
201 playlist.songs[i] = playlist.songs[i+1];
202 play_off--;
204 playlist.songs[playlist.len] = NULL;
207 const char *
208 playlist_jump(const char *arg)
210 size_t i;
211 regex_t re;
213 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
214 return NULL;
216 for (i = 0; i < playlist.len; ++i) {
217 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
218 break;
220 regfree(&re);
222 if (i == playlist.len)
223 return NULL;
225 play_state = STATE_PLAYING;
226 play_off = i;
227 setsong(play_off);
228 return playlist.songs[i];