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 <syslog.h>
23 #include "log.h"
24 #include "xmalloc.h"
25 #include "playlist.h"
27 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 struct playlist playlist;
30 enum play_state play_state;
31 int repeat_one;
32 int repeat_all = 1;
33 ssize_t play_off = -1;
35 void
36 playlist_swap(struct playlist *p)
37 {
38 playlist_truncate();
40 playlist.len = p->len;
41 playlist.cap = p->cap;
42 playlist.songs = p->songs;
43 }
45 void
46 playlist_push(struct playlist *playlist, const char *path)
47 {
48 size_t newcap;
50 if (playlist->len == playlist->cap) {
51 newcap = MAX(16, playlist->cap * 1.5);
52 playlist->songs = xrecallocarray(playlist->songs,
53 playlist->cap, newcap, sizeof(*playlist->songs));
54 playlist->cap = newcap;
55 }
57 playlist->songs[playlist->len++] = xstrdup(path);
58 }
60 void
61 playlist_enqueue(const char *path)
62 {
63 playlist_push(&playlist, path);
64 }
66 const char *
67 playlist_current(void)
68 {
69 if (playlist.len == 0 || play_off == -1) {
70 play_state = STATE_STOPPED;
71 return NULL;
72 }
74 return playlist.songs[play_off];
75 }
77 const char *
78 playlist_advance(void)
79 {
80 if (playlist.len == 0) {
81 play_state = STATE_STOPPED;
82 return NULL;
83 }
85 play_off++;
86 if (play_off == playlist.len) {
87 if (repeat_all)
88 play_off = 0;
89 else {
90 play_state = STATE_STOPPED;
91 play_off = -1;
92 return NULL;
93 }
94 }
96 play_state = STATE_PLAYING;
97 return playlist.songs[play_off];
98 }
100 const char *
101 playlist_previous(void)
103 if (playlist.len == 0) {
104 play_state = STATE_STOPPED;
105 return NULL;
108 play_off--;
109 if (play_off < 0) {
110 if (repeat_all)
111 play_off = playlist.len - 1;
112 else {
113 play_state = STATE_STOPPED;
114 play_off = -1;
115 return NULL;
119 play_state = STATE_PLAYING;
120 return playlist.songs[play_off];
123 void
124 playlist_reset(void)
126 play_off = -1;
129 void
130 playlist_free(struct playlist *playlist)
132 size_t i;
134 for (i = 0; i < playlist->len; ++i)
135 free(playlist->songs[i]);
136 free(playlist->songs);
137 playlist->songs = NULL;
139 playlist->len = 0;
140 playlist->cap = 0;
143 void
144 playlist_truncate(void)
146 playlist_free(&playlist);
147 play_off = -1;
150 void
151 playlist_dropcurrent(void)
153 size_t i;
155 if (play_off == -1 || playlist.len == 0)
156 return;
158 free(playlist.songs[play_off]);
160 playlist.len--;
161 for (i = play_off; i < playlist.len; ++i)
162 playlist.songs[i] = playlist.songs[i+1];
164 playlist.songs[playlist.len] = NULL;
167 const char *
168 playlist_jump(const char *arg)
170 size_t i;
171 regex_t re;
173 if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
174 return NULL;
176 for (i = 0; i < playlist.len; ++i) {
177 if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
178 break;
180 regfree(&re);
182 if (i == playlist.len)
183 return NULL;
185 play_state = STATE_PLAYING;
186 play_off = i;
187 return playlist.songs[i];