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;
36 int64_t current_position;
37 int64_t current_duration;
39 static void
40 setsong(ssize_t i)
41 {
42 free((char *)current_song);
43 if (i == -1)
44 current_song = NULL;
45 else
46 current_song = xstrdup(playlist.songs[i]);
47 }
49 void
50 playlist_swap(struct playlist *p, ssize_t off)
51 {
52 ssize_t i = -1;
54 if (off > p->len)
55 off = -1;
57 if (current_song != NULL && off < 0) {
58 /* try to match the currently played song */
59 for (i = 0; i < p->len; ++i) {
60 if (!strcmp(current_song, p->songs[i]))
61 break;
62 }
63 if (i == p->len)
64 i = -1;
65 }
67 playlist_truncate();
69 if (i != -1)
70 play_off = i;
71 else if (off >= 0)
72 play_off = off;
74 playlist.len = p->len;
75 playlist.cap = p->cap;
76 playlist.songs = p->songs;
78 if (play_state == STATE_STOPPED)
79 setsong(play_off);
80 }
82 void
83 playlist_push(struct playlist *playlist, const char *path)
84 {
85 size_t newcap;
87 if (playlist->len == playlist->cap) {
88 newcap = MAX(16, playlist->cap * 1.5);
89 playlist->songs = xrecallocarray(playlist->songs,
90 playlist->cap, newcap, sizeof(*playlist->songs));
91 playlist->cap = newcap;
92 }
94 playlist->songs[playlist->len++] = xstrdup(path);
95 }
97 void
98 playlist_enqueue(const char *path)
99 {
100 playlist_push(&playlist, path);
103 const char *
104 playlist_advance(void)
106 if (playlist.len == 0) {
107 play_state = STATE_STOPPED;
108 return NULL;
111 play_off++;
112 if (play_off == playlist.len) {
113 if (repeat_all)
114 play_off = 0;
115 else {
116 play_state = STATE_STOPPED;
117 play_off = -1;
118 setsong(play_off);
119 return NULL;
123 setsong(play_off);
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 setsong(play_off);
144 return NULL;
148 setsong(play_off);
149 play_state = STATE_PLAYING;
150 return playlist.songs[play_off];
153 void
154 playlist_reset(void)
156 play_off = -1;
159 void
160 playlist_free(struct playlist *playlist)
162 size_t i;
164 for (i = 0; i < playlist->len; ++i)
165 free(playlist->songs[i]);
166 free(playlist->songs);
167 playlist->songs = NULL;
169 playlist->len = 0;
170 playlist->cap = 0;
173 void
174 playlist_truncate(void)
176 playlist_free(&playlist);
177 play_off = -1;
180 void
181 playlist_dropcurrent(void)
183 size_t i;
185 if (play_off == -1 || playlist.len == 0)
186 return;
188 free(playlist.songs[play_off]);
189 setsong(-1);
191 playlist.len--;
192 for (i = play_off; i < playlist.len; ++i)
193 playlist.songs[i] = playlist.songs[i+1];
194 play_off--;
196 playlist.songs[playlist.len] = NULL;
199 const char *
200 playlist_jump(const char *arg)
202 size_t i;
203 regex_t re;
205 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
206 return NULL;
208 for (i = 0; i < playlist.len; ++i) {
209 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
210 break;
212 regfree(&re);
214 if (i == playlist.len)
215 return NULL;
217 play_state = STATE_PLAYING;
218 play_off = i;
219 setsong(play_off);
220 return playlist.songs[i];