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 match the currently played song */
57 for (i = 0; i < p->len; ++i) {
58 if (!strcmp(current_song, p->songs[i]))
59 break;
60 }
61 if (i == p->len)
62 i = -1;
63 }
65 playlist_truncate();
67 if (i != -1)
68 play_off = i;
69 else if (off >= 0)
70 play_off = off;
72 playlist.len = p->len;
73 playlist.cap = p->cap;
74 playlist.songs = p->songs;
76 if (play_state == STATE_STOPPED)
77 setsong(play_off);
78 }
80 void
81 playlist_push(struct playlist *playlist, const char *path)
82 {
83 size_t newcap;
85 if (playlist->len == playlist->cap) {
86 newcap = MAX(16, playlist->cap * 1.5);
87 playlist->songs = xrecallocarray(playlist->songs,
88 playlist->cap, newcap, sizeof(*playlist->songs));
89 playlist->cap = newcap;
90 }
92 playlist->songs[playlist->len++] = xstrdup(path);
93 }
95 void
96 playlist_enqueue(const char *path)
97 {
98 playlist_push(&playlist, path);
99 }
101 const char *
102 playlist_advance(void)
104 if (playlist.len == 0) {
105 play_state = STATE_STOPPED;
106 return NULL;
109 play_off++;
110 if (play_off == playlist.len) {
111 if (repeat_all)
112 play_off = 0;
113 else {
114 play_state = STATE_STOPPED;
115 play_off = -1;
116 setsong(play_off);
117 return NULL;
121 setsong(play_off);
122 play_state = STATE_PLAYING;
123 return playlist.songs[play_off];
126 const char *
127 playlist_previous(void)
129 if (playlist.len == 0) {
130 play_state = STATE_STOPPED;
131 return NULL;
134 play_off--;
135 if (play_off < 0) {
136 if (repeat_all)
137 play_off = playlist.len - 1;
138 else {
139 play_state = STATE_STOPPED;
140 play_off = -1;
141 setsong(play_off);
142 return NULL;
146 setsong(play_off);
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]);
187 setsong(-1);
189 playlist.len--;
190 for (i = play_off; i < playlist.len; ++i)
191 playlist.songs[i] = playlist.songs[i+1];
192 play_off--;
194 playlist.songs[playlist.len] = NULL;
197 const char *
198 playlist_jump(const char *arg)
200 size_t i;
201 regex_t re;
203 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
204 return NULL;
206 for (i = 0; i < playlist.len; ++i) {
207 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
208 break;
210 regfree(&re);
212 if (i == playlist.len)
213 return NULL;
215 play_state = STATE_PLAYING;
216 play_off = i;
217 setsong(play_off);
218 return playlist.songs[i];