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;
85 }
87 void
88 playlist_push(struct playlist *playlist, const char *path)
89 {
90 size_t newcap;
92 if (playlist->len == playlist->cap) {
93 newcap = MAX(16, playlist->cap * 1.5);
94 playlist->songs = xrecallocarray(playlist->songs,
95 playlist->cap, newcap, sizeof(*playlist->songs));
96 playlist->cap = newcap;
97 }
99 playlist->songs[playlist->len++] = xstrdup(path);
102 void
103 playlist_enqueue(const char *path)
105 playlist_push(&playlist, path);
108 const char *
109 playlist_advance(void)
111 if (playlist.len == 0) {
112 play_state = STATE_STOPPED;
113 return NULL;
116 play_off++;
117 if (play_off == playlist.len) {
118 if (repeat_all)
119 play_off = 0;
120 else {
121 play_state = STATE_STOPPED;
122 play_off = -1;
123 setsong(play_off);
124 return NULL;
128 setsong(play_off);
129 play_state = STATE_PLAYING;
130 return playlist.songs[play_off];
133 const char *
134 playlist_previous(void)
136 if (playlist.len == 0) {
137 play_state = STATE_STOPPED;
138 return NULL;
141 play_off--;
142 if (play_off < 0) {
143 if (repeat_all)
144 play_off = playlist.len - 1;
145 else {
146 play_state = STATE_STOPPED;
147 play_off = -1;
148 setsong(play_off);
149 return NULL;
153 setsong(play_off);
154 play_state = STATE_PLAYING;
155 return playlist.songs[play_off];
158 void
159 playlist_reset(void)
161 play_off = -1;
164 void
165 playlist_free(struct playlist *playlist)
167 size_t i;
169 for (i = 0; i < playlist->len; ++i)
170 free(playlist->songs[i]);
171 free(playlist->songs);
172 playlist->songs = NULL;
174 playlist->len = 0;
175 playlist->cap = 0;
178 void
179 playlist_truncate(void)
181 playlist_free(&playlist);
182 play_off = -1;
185 void
186 playlist_dropcurrent(void)
188 size_t i;
190 if (play_off == -1 || playlist.len == 0)
191 return;
193 free(playlist.songs[play_off]);
194 setsong(-1);
196 playlist.len--;
197 for (i = play_off; i < playlist.len; ++i)
198 playlist.songs[i] = playlist.songs[i+1];
199 play_off--;
201 playlist.songs[playlist.len] = NULL;
204 const char *
205 playlist_jump(const char *arg)
207 size_t i;
208 regex_t re;
210 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
211 return NULL;
213 for (i = 0; i < playlist.len; ++i) {
214 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
215 break;
217 regfree(&re);
219 if (i == playlist.len)
220 return NULL;
222 play_state = STATE_PLAYING;
223 play_off = i;
224 setsong(play_off);
225 return playlist.songs[i];