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 "config.h"
19 #include <sys/types.h>
21 #include <regex.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <syslog.h>
26 #include "log.h"
27 #include "xmalloc.h"
28 #include "playlist.h"
30 #define MAX(a, b) ((a) > (b) ? (a) : (b))
32 struct playlist playlist;
33 enum play_state play_state;
34 int repeat_one;
35 int repeat_all = 1;
36 ssize_t play_off = -1;
37 const char *current_song;
38 int64_t current_position;
39 int64_t current_duration;
41 static void
42 setsong(ssize_t i)
43 {
44 free((char *)current_song);
45 if (i == -1)
46 current_song = NULL;
47 else
48 current_song = xstrdup(playlist.songs[i]);
49 }
51 void
52 playlist_swap(struct playlist *p, ssize_t off)
53 {
54 ssize_t i = -1;
56 if (off > p->len)
57 off = -1;
59 if (current_song != NULL && off < 0) {
60 /* try to match the currently played song */
61 for (i = 0; i < p->len; ++i) {
62 if (!strcmp(current_song, p->songs[i]))
63 break;
64 }
65 if (i == p->len)
66 i = -1;
67 }
69 playlist_truncate();
71 if (i != -1)
72 play_off = i;
73 else if (off >= 0)
74 play_off = off;
76 playlist.len = p->len;
77 playlist.cap = p->cap;
78 playlist.songs = p->songs;
80 if (play_state == STATE_STOPPED)
81 setsong(play_off);
82 }
84 void
85 playlist_push(struct playlist *playlist, const char *path)
86 {
87 size_t newcap;
89 if (playlist->len == playlist->cap) {
90 newcap = MAX(16, playlist->cap * 1.5);
91 playlist->songs = xrecallocarray(playlist->songs,
92 playlist->cap, newcap, sizeof(*playlist->songs));
93 playlist->cap = newcap;
94 }
96 playlist->songs[playlist->len++] = xstrdup(path);
97 }
99 void
100 playlist_enqueue(const char *path)
102 playlist_push(&playlist, path);
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 setsong(play_off);
121 return NULL;
125 setsong(play_off);
126 play_state = STATE_PLAYING;
127 return playlist.songs[play_off];
130 const char *
131 playlist_previous(void)
133 if (playlist.len == 0) {
134 play_state = STATE_STOPPED;
135 return NULL;
138 play_off--;
139 if (play_off < 0) {
140 if (repeat_all)
141 play_off = playlist.len - 1;
142 else {
143 play_state = STATE_STOPPED;
144 play_off = -1;
145 setsong(play_off);
146 return NULL;
150 setsong(play_off);
151 play_state = STATE_PLAYING;
152 return playlist.songs[play_off];
155 void
156 playlist_reset(void)
158 play_off = -1;
161 void
162 playlist_free(struct playlist *playlist)
164 size_t i;
166 for (i = 0; i < playlist->len; ++i)
167 free(playlist->songs[i]);
168 free(playlist->songs);
169 playlist->songs = NULL;
171 playlist->len = 0;
172 playlist->cap = 0;
175 void
176 playlist_truncate(void)
178 playlist_free(&playlist);
179 play_off = -1;
182 void
183 playlist_dropcurrent(void)
185 size_t i;
187 if (play_off == -1 || playlist.len == 0)
188 return;
190 free(playlist.songs[play_off]);
191 setsong(-1);
193 playlist.len--;
194 for (i = play_off; i < playlist.len; ++i)
195 playlist.songs[i] = playlist.songs[i+1];
196 play_off--;
198 playlist.songs[playlist.len] = NULL;
201 const char *
202 playlist_jump(const char *arg)
204 size_t i;
205 regex_t re;
207 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
208 return NULL;
210 for (i = 0; i < playlist.len; ++i) {
211 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
212 break;
214 regfree(&re);
216 if (i == playlist.len)
217 return NULL;
219 play_state = STATE_PLAYING;
220 play_off = i;
221 setsong(play_off);
222 return playlist.songs[i];