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)
49 {
50 ssize_t i = -1;
52 if (current_song != NULL) {
53 /* try to adjust play_off to match the same song */
54 for (i = 0; i < p->len; ++i) {
55 if (!strcmp(current_song, p->songs[i]))
56 break;
57 }
58 /* try to match one song before */
59 if (i == p->len && play_off >= 1)
60 for (i = 0; i < p->len; ++i)
61 if (!strcmp(current_song, p->songs[i]))
62 break;
63 /* or one song after */
64 if (i == p->len && play_off < playlist.len-1)
65 for (i = 0; i < p->len; ++i)
66 if (!strcmp(current_song, p->songs[i]))
67 break;
68 if (i == p->len)
69 i = -1;
70 }
72 playlist_truncate();
74 if (i != -1)
75 play_off = i;
77 playlist.len = p->len;
78 playlist.cap = p->cap;
79 playlist.songs = p->songs;
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]);
190 playlist.len--;
191 for (i = play_off; i < playlist.len; ++i)
192 playlist.songs[i] = playlist.songs[i+1];
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];