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 int consume;
37 ssize_t play_off = -1;
38 const char *current_song;
39 int64_t current_position;
40 int64_t current_duration;
42 static void
43 setsong(ssize_t i)
44 {
45 free((char *)current_song);
46 if (i == -1)
47 current_song = NULL;
48 else
49 current_song = xstrdup(playlist.songs[i]);
50 }
52 void
53 playlist_swap(struct playlist *p, ssize_t off)
54 {
55 ssize_t i = -1;
57 if (off > p->len)
58 off = -1;
60 if (current_song != NULL && off < 0) {
61 /* try to match the currently played song */
62 for (i = 0; i < p->len; ++i) {
63 if (!strcmp(current_song, p->songs[i]))
64 break;
65 }
66 if (i == p->len)
67 i = -1;
68 }
70 playlist_truncate();
72 if (i != -1)
73 play_off = i;
74 else if (off >= 0)
75 play_off = off;
77 playlist.len = p->len;
78 playlist.cap = p->cap;
79 playlist.songs = p->songs;
81 if (play_state == STATE_STOPPED)
82 setsong(play_off);
83 }
85 void
86 playlist_push(struct playlist *playlist, const char *path)
87 {
88 size_t newcap;
90 if (playlist->len == playlist->cap) {
91 newcap = MAX(16, playlist->cap * 1.5);
92 playlist->songs = xrecallocarray(playlist->songs,
93 playlist->cap, newcap, sizeof(*playlist->songs));
94 playlist->cap = newcap;
95 }
97 playlist->songs[playlist->len++] = xstrdup(path);
98 }
100 void
101 playlist_enqueue(const char *path)
103 playlist_push(&playlist, path);
106 const char *
107 playlist_advance(void)
109 if (playlist.len == 0) {
110 play_state = STATE_STOPPED;
111 return NULL;
114 play_off++;
115 if (play_off == playlist.len) {
116 if (repeat_all)
117 play_off = 0;
118 else {
119 play_state = STATE_STOPPED;
120 play_off = -1;
121 setsong(play_off);
122 return NULL;
126 setsong(play_off);
127 play_state = STATE_PLAYING;
128 return playlist.songs[play_off];
131 const char *
132 playlist_previous(void)
134 if (playlist.len == 0) {
135 play_state = STATE_STOPPED;
136 return NULL;
139 play_off--;
140 if (play_off < 0) {
141 if (repeat_all)
142 play_off = playlist.len - 1;
143 else {
144 play_state = STATE_STOPPED;
145 play_off = -1;
146 setsong(play_off);
147 return NULL;
151 setsong(play_off);
152 play_state = STATE_PLAYING;
153 return playlist.songs[play_off];
156 void
157 playlist_reset(void)
159 play_off = -1;
162 void
163 playlist_free(struct playlist *playlist)
165 size_t i;
167 for (i = 0; i < playlist->len; ++i)
168 free(playlist->songs[i]);
169 free(playlist->songs);
170 playlist->songs = NULL;
172 playlist->len = 0;
173 playlist->cap = 0;
176 void
177 playlist_truncate(void)
179 playlist_free(&playlist);
180 play_off = -1;
183 void
184 playlist_dropcurrent(void)
186 size_t i;
188 if (play_off == -1 || playlist.len == 0)
189 return;
191 free(playlist.songs[play_off]);
192 setsong(-1);
194 playlist.len--;
195 for (i = play_off; i < playlist.len; ++i)
196 playlist.songs[i] = playlist.songs[i+1];
197 play_off--;
199 playlist.songs[playlist.len] = NULL;
202 const char *
203 playlist_jump(const char *arg)
205 size_t i;
206 regex_t re;
208 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
209 return NULL;
211 for (i = 0; i < playlist.len; ++i) {
212 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
213 break;
215 regfree(&re);
217 if (i == playlist.len)
218 return NULL;
220 play_state = STATE_PLAYING;
221 play_off = i;
222 setsong(play_off);
223 return playlist.songs[i];